Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU host block devices |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or |
| 7 | * later. See the COPYING file in the top-level directory. |
Markus Armbruster | 3618a09 | 2013-03-14 13:59:53 +0100 | [diff] [blame] | 8 | * |
| 9 | * This file incorporates work covered by the following copyright and |
| 10 | * permission notice: |
| 11 | * |
| 12 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 13 | * |
| 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | * of this software and associated documentation files (the "Software"), to deal |
| 16 | * in the Software without restriction, including without limitation the rights |
| 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 18 | * copies of the Software, and to permit persons to whom the Software is |
| 19 | * furnished to do so, subject to the following conditions: |
| 20 | * |
| 21 | * The above copyright notice and this permission notice shall be included in |
| 22 | * all copies or substantial portions of the Software. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 30 | * THE SOFTWARE. |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 31 | */ |
| 32 | |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 33 | #include "sysemu/blockdev.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 34 | #include "hw/block/block.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 35 | #include "block/blockjob.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 36 | #include "monitor/monitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 37 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 38 | #include "qemu/option.h" |
| 39 | #include "qemu/config-file.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 40 | #include "qapi/qmp/types.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 41 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 42 | #include "block/block_int.h" |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 43 | #include "qmp-commands.h" |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 44 | #include "trace.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 45 | #include "sysemu/arch_init.h" |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 46 | |
Markus Armbruster | c9b62a7 | 2010-06-02 18:55:22 +0200 | [diff] [blame] | 47 | static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives); |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 48 | extern QemuOptsList qemu_common_drive_opts; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 49 | |
Markus Armbruster | 1960966 | 2011-01-28 11:21:39 +0100 | [diff] [blame] | 50 | static const char *const if_name[IF_COUNT] = { |
| 51 | [IF_NONE] = "none", |
| 52 | [IF_IDE] = "ide", |
| 53 | [IF_SCSI] = "scsi", |
| 54 | [IF_FLOPPY] = "floppy", |
| 55 | [IF_PFLASH] = "pflash", |
| 56 | [IF_MTD] = "mtd", |
| 57 | [IF_SD] = "sd", |
| 58 | [IF_VIRTIO] = "virtio", |
| 59 | [IF_XEN] = "xen", |
| 60 | }; |
| 61 | |
| 62 | static const int if_max_devs[IF_COUNT] = { |
Markus Armbruster | 27d6bf4 | 2011-01-28 11:21:40 +0100 | [diff] [blame] | 63 | /* |
| 64 | * Do not change these numbers! They govern how drive option |
| 65 | * index maps to unit and bus. That mapping is ABI. |
| 66 | * |
| 67 | * All controllers used to imlement if=T drives need to support |
| 68 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. |
| 69 | * Otherwise, some index values map to "impossible" bus, unit |
| 70 | * values. |
| 71 | * |
| 72 | * For instance, if you change [IF_SCSI] to 255, -drive |
| 73 | * if=scsi,index=12 no longer means bus=1,unit=5, but |
| 74 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), |
| 75 | * the drive can't be set up. Regression. |
| 76 | */ |
| 77 | [IF_IDE] = 2, |
| 78 | [IF_SCSI] = 7, |
Markus Armbruster | 1960966 | 2011-01-28 11:21:39 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 81 | /* |
| 82 | * We automatically delete the drive when a device using it gets |
| 83 | * unplugged. Questionable feature, but we can't just drop it. |
| 84 | * Device models call blockdev_mark_auto_del() to schedule the |
| 85 | * automatic deletion, and generic qdev code calls blockdev_auto_del() |
| 86 | * when deletion is actually safe. |
| 87 | */ |
| 88 | void blockdev_mark_auto_del(BlockDriverState *bs) |
| 89 | { |
| 90 | DriveInfo *dinfo = drive_get_by_blockdev(bs); |
| 91 | |
Paolo Bonzini | 12bde0e | 2012-03-30 13:17:10 +0200 | [diff] [blame] | 92 | if (bs->job) { |
| 93 | block_job_cancel(bs->job); |
| 94 | } |
Ryan Harper | 0fc0f1f | 2010-12-08 10:05:00 -0600 | [diff] [blame] | 95 | if (dinfo) { |
| 96 | dinfo->auto_del = 1; |
| 97 | } |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void blockdev_auto_del(BlockDriverState *bs) |
| 101 | { |
| 102 | DriveInfo *dinfo = drive_get_by_blockdev(bs); |
| 103 | |
Ryan Harper | 0fc0f1f | 2010-12-08 10:05:00 -0600 | [diff] [blame] | 104 | if (dinfo && dinfo->auto_del) { |
Marcelo Tosatti | 84fb392 | 2011-01-26 12:12:32 -0200 | [diff] [blame] | 105 | drive_put_ref(dinfo); |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Markus Armbruster | 505a7fb | 2011-01-28 11:21:43 +0100 | [diff] [blame] | 109 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
| 110 | { |
| 111 | int max_devs = if_max_devs[type]; |
| 112 | return max_devs ? index / max_devs : 0; |
| 113 | } |
| 114 | |
| 115 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) |
| 116 | { |
| 117 | int max_devs = if_max_devs[type]; |
| 118 | return max_devs ? index % max_devs : index; |
| 119 | } |
| 120 | |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 121 | QemuOpts *drive_def(const char *optstr) |
| 122 | { |
| 123 | return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); |
| 124 | } |
| 125 | |
| 126 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, |
Markus Armbruster | 5645b0f | 2011-01-31 11:50:09 +0100 | [diff] [blame] | 127 | const char *optstr) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 128 | { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 129 | QemuOpts *opts; |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 130 | char buf[32]; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 131 | |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 132 | opts = drive_def(optstr); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 133 | if (!opts) { |
| 134 | return NULL; |
| 135 | } |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 136 | if (type != IF_DEFAULT) { |
| 137 | qemu_opt_set(opts, "if", if_name[type]); |
| 138 | } |
| 139 | if (index >= 0) { |
| 140 | snprintf(buf, sizeof(buf), "%d", index); |
| 141 | qemu_opt_set(opts, "index", buf); |
| 142 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 143 | if (file) |
| 144 | qemu_opt_set(opts, "file", file); |
| 145 | return opts; |
| 146 | } |
| 147 | |
| 148 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
| 149 | { |
| 150 | DriveInfo *dinfo; |
| 151 | |
| 152 | /* seek interface, bus and unit */ |
| 153 | |
| 154 | QTAILQ_FOREACH(dinfo, &drives, next) { |
| 155 | if (dinfo->type == type && |
| 156 | dinfo->bus == bus && |
| 157 | dinfo->unit == unit) |
| 158 | return dinfo; |
| 159 | } |
| 160 | |
| 161 | return NULL; |
| 162 | } |
| 163 | |
Markus Armbruster | f1bd51a | 2011-01-28 11:21:44 +0100 | [diff] [blame] | 164 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
| 165 | { |
| 166 | return drive_get(type, |
| 167 | drive_index_to_bus_id(type, index), |
| 168 | drive_index_to_unit_id(type, index)); |
| 169 | } |
| 170 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 171 | int drive_get_max_bus(BlockInterfaceType type) |
| 172 | { |
| 173 | int max_bus; |
| 174 | DriveInfo *dinfo; |
| 175 | |
| 176 | max_bus = -1; |
| 177 | QTAILQ_FOREACH(dinfo, &drives, next) { |
| 178 | if(dinfo->type == type && |
| 179 | dinfo->bus > max_bus) |
| 180 | max_bus = dinfo->bus; |
| 181 | } |
| 182 | return max_bus; |
| 183 | } |
| 184 | |
Markus Armbruster | 1383997 | 2011-01-28 11:21:37 +0100 | [diff] [blame] | 185 | /* Get a block device. This should only be used for single-drive devices |
| 186 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the |
| 187 | appropriate bus. */ |
| 188 | DriveInfo *drive_get_next(BlockInterfaceType type) |
| 189 | { |
| 190 | static int next_block_unit[IF_COUNT]; |
| 191 | |
| 192 | return drive_get(type, 0, next_block_unit[type]++); |
| 193 | } |
| 194 | |
Markus Armbruster | e4700e5 | 2010-06-24 17:25:32 +0200 | [diff] [blame] | 195 | DriveInfo *drive_get_by_blockdev(BlockDriverState *bs) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 196 | { |
| 197 | DriveInfo *dinfo; |
| 198 | |
| 199 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Markus Armbruster | e4700e5 | 2010-06-24 17:25:32 +0200 | [diff] [blame] | 200 | if (dinfo->bdrv == bs) { |
| 201 | return dinfo; |
| 202 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 203 | } |
Markus Armbruster | e4700e5 | 2010-06-24 17:25:32 +0200 | [diff] [blame] | 204 | return NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 207 | static void bdrv_format_print(void *opaque, const char *name) |
| 208 | { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 209 | error_printf(" %s", name); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Marcelo Tosatti | 84fb392 | 2011-01-26 12:12:32 -0200 | [diff] [blame] | 212 | static void drive_uninit(DriveInfo *dinfo) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 213 | { |
| 214 | qemu_opts_del(dinfo->opts); |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 215 | bdrv_unref(dinfo->bdrv); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 216 | g_free(dinfo->id); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 217 | QTAILQ_REMOVE(&drives, dinfo, next); |
Kevin Wolf | bb44619 | 2013-03-15 10:35:06 +0100 | [diff] [blame] | 218 | g_free(dinfo->serial); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 219 | g_free(dinfo); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Marcelo Tosatti | 84fb392 | 2011-01-26 12:12:32 -0200 | [diff] [blame] | 222 | void drive_put_ref(DriveInfo *dinfo) |
| 223 | { |
| 224 | assert(dinfo->refcount); |
| 225 | if (--dinfo->refcount == 0) { |
| 226 | drive_uninit(dinfo); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void drive_get_ref(DriveInfo *dinfo) |
| 231 | { |
| 232 | dinfo->refcount++; |
| 233 | } |
| 234 | |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 235 | typedef struct { |
| 236 | QEMUBH *bh; |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 237 | BlockDriverState *bs; |
| 238 | } BDRVPutRefBH; |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 239 | |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 240 | static void bdrv_put_ref_bh(void *opaque) |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 241 | { |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 242 | BDRVPutRefBH *s = opaque; |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 243 | |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 244 | bdrv_unref(s->bs); |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 245 | qemu_bh_delete(s->bh); |
| 246 | g_free(s); |
| 247 | } |
| 248 | |
| 249 | /* |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 250 | * Release a BDS reference in a BH |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 251 | * |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 252 | * It is not safe to use bdrv_unref() from a callback function when the callers |
| 253 | * still need the BlockDriverState. In such cases we schedule a BH to release |
| 254 | * the reference. |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 255 | */ |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 256 | static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 257 | { |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 258 | BDRVPutRefBH *s; |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 259 | |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 260 | s = g_new(BDRVPutRefBH, 1); |
| 261 | s->bh = qemu_bh_new(bdrv_put_ref_bh, s); |
| 262 | s->bs = bs; |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 263 | qemu_bh_schedule(s->bh); |
| 264 | } |
| 265 | |
Paolo Bonzini | 1ceee0d | 2012-09-28 17:22:56 +0200 | [diff] [blame] | 266 | static int parse_block_error_action(const char *buf, bool is_read) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 267 | { |
| 268 | if (!strcmp(buf, "ignore")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 269 | return BLOCKDEV_ON_ERROR_IGNORE; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 270 | } else if (!is_read && !strcmp(buf, "enospc")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 271 | return BLOCKDEV_ON_ERROR_ENOSPC; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 272 | } else if (!strcmp(buf, "stop")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 273 | return BLOCKDEV_ON_ERROR_STOP; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 274 | } else if (!strcmp(buf, "report")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 275 | return BLOCKDEV_ON_ERROR_REPORT; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 276 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 277 | error_report("'%s' invalid %s error action", |
| 278 | buf, is_read ? "read" : "write"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 279 | return -1; |
| 280 | } |
| 281 | } |
| 282 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 283 | static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 284 | { |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 285 | if (throttle_conflicting(cfg)) { |
| 286 | error_setg(errp, "bps/iops/max total values and read/write values" |
| 287 | " cannot be used at the same time"); |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 291 | if (!throttle_is_valid(cfg)) { |
| 292 | error_setg(errp, "bps/iops/maxs values must be 0 or greater"); |
Stefan Hajnoczi | 7d81c14 | 2013-02-13 16:53:43 +0100 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 296 | return true; |
| 297 | } |
| 298 | |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 299 | static DriveInfo *blockdev_init(QemuOpts *all_opts, |
| 300 | BlockInterfaceType block_default_type) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 301 | { |
| 302 | const char *buf; |
| 303 | const char *file = NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 304 | const char *serial; |
| 305 | const char *mediastr = ""; |
| 306 | BlockInterfaceType type; |
| 307 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 308 | int bus_id, unit_id; |
| 309 | int cyls, heads, secs, translation; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 310 | int max_devs; |
| 311 | int index; |
| 312 | int ro = 0; |
| 313 | int bdrv_flags = 0; |
| 314 | int on_read_error, on_write_error; |
| 315 | const char *devaddr; |
| 316 | DriveInfo *dinfo; |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 317 | ThrottleConfig cfg; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 318 | int snapshot = 0; |
Stefan Hajnoczi | fb0490f | 2011-11-17 13:40:32 +0000 | [diff] [blame] | 319 | bool copy_on_read; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 320 | int ret; |
Stefan Hajnoczi | c546194 | 2013-02-13 16:53:42 +0100 | [diff] [blame] | 321 | Error *error = NULL; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 322 | QemuOpts *opts; |
| 323 | QDict *bs_opts; |
| 324 | const char *id; |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 325 | bool has_driver_specific_opts; |
Mike Qiu | 6db5f5d | 2013-08-08 10:45:16 -0400 | [diff] [blame] | 326 | BlockDriver *drv = NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 327 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 328 | translation = BIOS_ATA_TRANSLATION_AUTO; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 329 | media = MEDIA_DISK; |
| 330 | |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 331 | /* Check common options by copying from all_opts to opts, all other options |
| 332 | * are stored in bs_opts. */ |
| 333 | id = qemu_opts_id(all_opts); |
| 334 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
| 335 | if (error_is_set(&error)) { |
| 336 | qerror_report_err(error); |
| 337 | error_free(error); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | bs_opts = qdict_new(); |
| 342 | qemu_opts_to_qdict(all_opts, bs_opts); |
| 343 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
| 344 | if (error_is_set(&error)) { |
| 345 | qerror_report_err(error); |
| 346 | error_free(error); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | if (id) { |
| 351 | qdict_del(bs_opts, "id"); |
| 352 | } |
| 353 | |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 354 | has_driver_specific_opts = !!qdict_size(bs_opts); |
| 355 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 356 | /* extract parameters */ |
| 357 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 358 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 359 | index = qemu_opt_get_number(opts, "index", -1); |
| 360 | |
| 361 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 362 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 363 | secs = qemu_opt_get_number(opts, "secs", 0); |
| 364 | |
| 365 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
Kevin Wolf | 0f227a9 | 2013-07-19 20:07:29 +0200 | [diff] [blame] | 366 | ro = qemu_opt_get_bool(opts, "read-only", 0); |
Stefan Hajnoczi | fb0490f | 2011-11-17 13:40:32 +0000 | [diff] [blame] | 367 | copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 368 | |
| 369 | file = qemu_opt_get(opts, "file"); |
| 370 | serial = qemu_opt_get(opts, "serial"); |
| 371 | |
| 372 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
Markus Armbruster | 1960966 | 2011-01-28 11:21:39 +0100 | [diff] [blame] | 373 | for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++) |
| 374 | ; |
| 375 | if (type == IF_COUNT) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 376 | error_report("unsupported bus type '%s'", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 377 | return NULL; |
| 378 | } |
Luiz Capitulino | 2d3999f | 2011-07-01 10:46:12 -0300 | [diff] [blame] | 379 | } else { |
Christian Borntraeger | 2d0d283 | 2012-11-20 15:30:34 +0100 | [diff] [blame] | 380 | type = block_default_type; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 381 | } |
Luiz Capitulino | 2d3999f | 2011-07-01 10:46:12 -0300 | [diff] [blame] | 382 | |
Markus Armbruster | 1960966 | 2011-01-28 11:21:39 +0100 | [diff] [blame] | 383 | max_devs = if_max_devs[type]; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 384 | |
| 385 | if (cyls || heads || secs) { |
Markus Armbruster | aaea3f3 | 2012-07-10 11:12:54 +0200 | [diff] [blame] | 386 | if (cyls < 1) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 387 | error_report("invalid physical cyls number"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 388 | return NULL; |
| 389 | } |
Markus Armbruster | aaea3f3 | 2012-07-10 11:12:54 +0200 | [diff] [blame] | 390 | if (heads < 1) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 391 | error_report("invalid physical heads number"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 392 | return NULL; |
| 393 | } |
Markus Armbruster | aaea3f3 | 2012-07-10 11:12:54 +0200 | [diff] [blame] | 394 | if (secs < 1) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 395 | error_report("invalid physical secs number"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 396 | return NULL; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
| 401 | if (!cyls) { |
Markus Armbruster | e4080f9 | 2011-06-22 14:03:57 +0200 | [diff] [blame] | 402 | error_report("'%s' trans must be used with cyls, heads and secs", |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 403 | buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 404 | return NULL; |
| 405 | } |
| 406 | if (!strcmp(buf, "none")) |
| 407 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 408 | else if (!strcmp(buf, "lba")) |
| 409 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 410 | else if (!strcmp(buf, "auto")) |
| 411 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 412 | else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 413 | error_report("'%s' invalid translation type", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 414 | return NULL; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
| 419 | if (!strcmp(buf, "disk")) { |
| 420 | media = MEDIA_DISK; |
| 421 | } else if (!strcmp(buf, "cdrom")) { |
| 422 | if (cyls || secs || heads) { |
Luiz Capitulino | e7ff8f0 | 2011-07-01 10:46:13 -0300 | [diff] [blame] | 423 | error_report("CHS can't be set with media=%s", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
| 426 | media = MEDIA_CDROM; |
| 427 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 428 | error_report("'%s' invalid media", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 429 | return NULL; |
| 430 | } |
| 431 | } |
| 432 | |
Paolo Bonzini | a9384af | 2013-02-08 14:06:12 +0100 | [diff] [blame] | 433 | if ((buf = qemu_opt_get(opts, "discard")) != NULL) { |
| 434 | if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) { |
| 435 | error_report("invalid discard option"); |
| 436 | return NULL; |
| 437 | } |
| 438 | } |
| 439 | |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 440 | if (qemu_opt_get_bool(opts, "cache.writeback", true)) { |
| 441 | bdrv_flags |= BDRV_O_CACHE_WB; |
| 442 | } |
| 443 | if (qemu_opt_get_bool(opts, "cache.direct", false)) { |
| 444 | bdrv_flags |= BDRV_O_NOCACHE; |
| 445 | } |
Paolo Bonzini | 1df6fa4 | 2013-09-19 18:48:53 +0200 | [diff] [blame] | 446 | if (qemu_opt_get_bool(opts, "cache.no-flush", false)) { |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 447 | bdrv_flags |= BDRV_O_NO_FLUSH; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | #ifdef CONFIG_LINUX_AIO |
| 451 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 452 | if (!strcmp(buf, "native")) { |
| 453 | bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 454 | } else if (!strcmp(buf, "threads")) { |
| 455 | /* this is the default */ |
| 456 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 457 | error_report("invalid aio option"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 458 | return NULL; |
| 459 | } |
| 460 | } |
| 461 | #endif |
| 462 | |
| 463 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 464 | if (is_help_option(buf)) { |
| 465 | error_printf("Supported formats:"); |
| 466 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 467 | error_printf("\n"); |
| 468 | return NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 469 | } |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 470 | |
Mike Qiu | 6db5f5d | 2013-08-08 10:45:16 -0400 | [diff] [blame] | 471 | drv = bdrv_find_whitelisted_format(buf, ro); |
| 472 | if (!drv) { |
Fam Zheng | 7780d47 | 2013-08-22 15:36:59 +0800 | [diff] [blame] | 473 | if (!ro && bdrv_find_whitelisted_format(buf, !ro)) { |
| 474 | error_report("'%s' can be only used as read-only device.", buf); |
| 475 | } else { |
| 476 | error_report("'%s' invalid format", buf); |
| 477 | } |
Mike Qiu | 6db5f5d | 2013-08-08 10:45:16 -0400 | [diff] [blame] | 478 | return NULL; |
| 479 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 482 | /* disk I/O throttling */ |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 483 | memset(&cfg, 0, sizeof(cfg)); |
| 484 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 485 | qemu_opt_get_number(opts, "throttling.bps-total", 0); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 486 | cfg.buckets[THROTTLE_BPS_READ].avg = |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 487 | qemu_opt_get_number(opts, "throttling.bps-read", 0); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 488 | cfg.buckets[THROTTLE_BPS_WRITE].avg = |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 489 | qemu_opt_get_number(opts, "throttling.bps-write", 0); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 490 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 491 | qemu_opt_get_number(opts, "throttling.iops-total", 0); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 492 | cfg.buckets[THROTTLE_OPS_READ].avg = |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 493 | qemu_opt_get_number(opts, "throttling.iops-read", 0); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 494 | cfg.buckets[THROTTLE_OPS_WRITE].avg = |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 495 | qemu_opt_get_number(opts, "throttling.iops-write", 0); |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 496 | |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 497 | cfg.buckets[THROTTLE_BPS_TOTAL].max = |
| 498 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); |
| 499 | cfg.buckets[THROTTLE_BPS_READ].max = |
| 500 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); |
| 501 | cfg.buckets[THROTTLE_BPS_WRITE].max = |
| 502 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); |
| 503 | cfg.buckets[THROTTLE_OPS_TOTAL].max = |
| 504 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); |
| 505 | cfg.buckets[THROTTLE_OPS_READ].max = |
| 506 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); |
| 507 | cfg.buckets[THROTTLE_OPS_WRITE].max = |
| 508 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 509 | |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 510 | cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 511 | |
| 512 | if (!check_throttle_config(&cfg, &error)) { |
Stefan Hajnoczi | c546194 | 2013-02-13 16:53:42 +0100 | [diff] [blame] | 513 | error_report("%s", error_get_pretty(error)); |
| 514 | error_free(error); |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 515 | return NULL; |
| 516 | } |
| 517 | |
Jan Kiszka | 0d92d17 | 2012-10-05 14:51:43 -0300 | [diff] [blame] | 518 | if (qemu_opt_get(opts, "boot") != NULL) { |
| 519 | fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " |
| 520 | "ignored. Future versions will reject this parameter. Please " |
| 521 | "update your scripts.\n"); |
| 522 | } |
| 523 | |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 524 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 525 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
| 526 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 527 | error_report("werror is not supported by this bus type"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 528 | return NULL; |
| 529 | } |
| 530 | |
| 531 | on_write_error = parse_block_error_action(buf, 0); |
| 532 | if (on_write_error < 0) { |
| 533 | return NULL; |
| 534 | } |
| 535 | } |
| 536 | |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 537 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 538 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 539 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 540 | error_report("rerror is not supported by this bus type"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | on_read_error = parse_block_error_action(buf, 1); |
| 545 | if (on_read_error < 0) { |
| 546 | return NULL; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
| 551 | if (type != IF_VIRTIO) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 552 | error_report("addr is not supported by this bus type"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 553 | return NULL; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* compute bus and unit according index */ |
| 558 | |
| 559 | if (index != -1) { |
| 560 | if (bus_id != 0 || unit_id != -1) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 561 | error_report("index cannot be used with bus and unit"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
Markus Armbruster | 505a7fb | 2011-01-28 11:21:43 +0100 | [diff] [blame] | 564 | bus_id = drive_index_to_bus_id(type, index); |
| 565 | unit_id = drive_index_to_unit_id(type, index); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* if user doesn't specify a unit_id, |
| 569 | * try to find the first free |
| 570 | */ |
| 571 | |
| 572 | if (unit_id == -1) { |
| 573 | unit_id = 0; |
| 574 | while (drive_get(type, bus_id, unit_id) != NULL) { |
| 575 | unit_id++; |
| 576 | if (max_devs && unit_id >= max_devs) { |
| 577 | unit_id -= max_devs; |
| 578 | bus_id++; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* check unit id */ |
| 584 | |
| 585 | if (max_devs && unit_id >= max_devs) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 586 | error_report("unit %d too big (max is %d)", |
| 587 | unit_id, max_devs - 1); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 588 | return NULL; |
| 589 | } |
| 590 | |
| 591 | /* |
Markus Armbruster | 4e5d9b5 | 2011-01-28 11:21:45 +0100 | [diff] [blame] | 592 | * catch multiple definitions |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 593 | */ |
| 594 | |
| 595 | if (drive_get(type, bus_id, unit_id) != NULL) { |
Markus Armbruster | 4e5d9b5 | 2011-01-28 11:21:45 +0100 | [diff] [blame] | 596 | error_report("drive with bus=%d, unit=%d (index=%d) exists", |
| 597 | bus_id, unit_id, index); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | /* init */ |
| 602 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 603 | dinfo = g_malloc0(sizeof(*dinfo)); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 604 | if ((buf = qemu_opts_id(opts)) != NULL) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 605 | dinfo->id = g_strdup(buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 606 | } else { |
| 607 | /* no id supplied -> create one */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 608 | dinfo->id = g_malloc0(32); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 609 | if (type == IF_IDE || type == IF_SCSI) |
| 610 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 611 | if (max_devs) |
| 612 | snprintf(dinfo->id, 32, "%s%i%s%i", |
Markus Armbruster | 79d21d5 | 2012-06-05 14:52:53 +0200 | [diff] [blame] | 613 | if_name[type], bus_id, mediastr, unit_id); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 614 | else |
| 615 | snprintf(dinfo->id, 32, "%s%s%i", |
Markus Armbruster | 79d21d5 | 2012-06-05 14:52:53 +0200 | [diff] [blame] | 616 | if_name[type], mediastr, unit_id); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 617 | } |
| 618 | dinfo->bdrv = bdrv_new(dinfo->id); |
Kevin Shanahan | 80dd1aa | 2012-09-21 08:50:22 +0930 | [diff] [blame] | 619 | dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0; |
| 620 | dinfo->bdrv->read_only = ro; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 621 | dinfo->devaddr = devaddr; |
| 622 | dinfo->type = type; |
| 623 | dinfo->bus = bus_id; |
| 624 | dinfo->unit = unit_id; |
Markus Armbruster | 317bb41 | 2012-07-10 11:12:39 +0200 | [diff] [blame] | 625 | dinfo->cyls = cyls; |
| 626 | dinfo->heads = heads; |
| 627 | dinfo->secs = secs; |
| 628 | dinfo->trans = translation; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 629 | dinfo->opts = all_opts; |
Marcelo Tosatti | 84fb392 | 2011-01-26 12:12:32 -0200 | [diff] [blame] | 630 | dinfo->refcount = 1; |
Kevin Wolf | bb44619 | 2013-03-15 10:35:06 +0100 | [diff] [blame] | 631 | if (serial != NULL) { |
| 632 | dinfo->serial = g_strdup(serial); |
| 633 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 634 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
| 635 | |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 636 | bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); |
| 637 | |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 638 | /* disk I/O throttling */ |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 639 | if (throttle_enabled(&cfg)) { |
| 640 | bdrv_io_limits_enable(dinfo->bdrv); |
| 641 | bdrv_set_io_limits(dinfo->bdrv, &cfg); |
| 642 | } |
Zhi Yong Wu | 0563e19 | 2011-11-03 16:57:25 +0800 | [diff] [blame] | 643 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 644 | switch(type) { |
| 645 | case IF_IDE: |
| 646 | case IF_SCSI: |
| 647 | case IF_XEN: |
| 648 | case IF_NONE: |
Markus Armbruster | 2b58495 | 2012-07-10 11:12:50 +0200 | [diff] [blame] | 649 | dinfo->media_cd = media == MEDIA_CDROM; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 650 | break; |
| 651 | case IF_SD: |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 652 | case IF_FLOPPY: |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 653 | case IF_PFLASH: |
| 654 | case IF_MTD: |
| 655 | break; |
| 656 | case IF_VIRTIO: |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 657 | { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 658 | /* add virtio block device */ |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 659 | QemuOpts *devopts; |
| 660 | devopts = qemu_opts_create_nofail(qemu_find_opts("device")); |
Anthony Liguori | eeb9c1b | 2012-02-03 11:13:30 -0600 | [diff] [blame] | 661 | if (arch_type == QEMU_ARCH_S390X) { |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 662 | qemu_opt_set(devopts, "driver", "virtio-blk-s390"); |
Anthony Liguori | eeb9c1b | 2012-02-03 11:13:30 -0600 | [diff] [blame] | 663 | } else { |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 664 | qemu_opt_set(devopts, "driver", "virtio-blk-pci"); |
Anthony Liguori | eeb9c1b | 2012-02-03 11:13:30 -0600 | [diff] [blame] | 665 | } |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 666 | qemu_opt_set(devopts, "drive", dinfo->id); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 667 | if (devaddr) |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 668 | qemu_opt_set(devopts, "addr", devaddr); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 669 | break; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 670 | } |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 671 | default: |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 672 | abort(); |
| 673 | } |
Markus Armbruster | dd5b0d7 | 2010-05-11 15:36:46 +0200 | [diff] [blame] | 674 | if (!file || !*file) { |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 675 | if (has_driver_specific_opts) { |
Kevin Wolf | c2ad1b0 | 2013-03-18 16:40:51 +0100 | [diff] [blame] | 676 | file = NULL; |
| 677 | } else { |
| 678 | return dinfo; |
| 679 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 680 | } |
| 681 | if (snapshot) { |
| 682 | /* always use cache=unsafe with snapshot */ |
| 683 | bdrv_flags &= ~BDRV_O_CACHE_MASK; |
| 684 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); |
| 685 | } |
| 686 | |
Stefan Hajnoczi | fb0490f | 2011-11-17 13:40:32 +0000 | [diff] [blame] | 687 | if (copy_on_read) { |
| 688 | bdrv_flags |= BDRV_O_COPY_ON_READ; |
| 689 | } |
| 690 | |
Benoît Canet | ed9d420 | 2012-03-23 08:36:50 +0100 | [diff] [blame] | 691 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 692 | bdrv_flags |= BDRV_O_INCOMING; |
| 693 | } |
| 694 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 695 | if (media == MEDIA_CDROM) { |
| 696 | /* CDROM is fine for any interface, don't check. */ |
| 697 | ro = 1; |
| 698 | } else if (ro == 1) { |
Jordan Justen | 1e9eb78 | 2012-02-21 23:18:48 -0800 | [diff] [blame] | 699 | if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && |
| 700 | type != IF_NONE && type != IF_PFLASH) { |
Kevin Wolf | 0f227a9 | 2013-07-19 20:07:29 +0200 | [diff] [blame] | 701 | error_report("read-only not supported by this bus type"); |
Markus Armbruster | a9ae2bf | 2011-02-08 15:12:39 +0100 | [diff] [blame] | 702 | goto err; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; |
| 707 | |
Stefan Hajnoczi | 04d4abe | 2012-06-27 18:03:13 +0100 | [diff] [blame] | 708 | if (ro && copy_on_read) { |
Kevin Wolf | 0f227a9 | 2013-07-19 20:07:29 +0200 | [diff] [blame] | 709 | error_report("warning: disabling copy_on_read on read-only drive"); |
Stefan Hajnoczi | 04d4abe | 2012-06-27 18:03:13 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 712 | QINCREF(bs_opts); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 713 | ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error); |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 714 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 715 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 716 | error_report("could not open disk image %s: %s", |
| 717 | file ?: dinfo->id, error_get_pretty(error)); |
Markus Armbruster | a9ae2bf | 2011-02-08 15:12:39 +0100 | [diff] [blame] | 718 | goto err; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | if (bdrv_key_required(dinfo->bdrv)) |
| 722 | autostart = 0; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 723 | |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 724 | QDECREF(bs_opts); |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 725 | qemu_opts_del(opts); |
| 726 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 727 | return dinfo; |
Markus Armbruster | a9ae2bf | 2011-02-08 15:12:39 +0100 | [diff] [blame] | 728 | |
| 729 | err: |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 730 | qemu_opts_del(opts); |
| 731 | QDECREF(bs_opts); |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 732 | bdrv_unref(dinfo->bdrv); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 733 | g_free(dinfo->id); |
Markus Armbruster | a9ae2bf | 2011-02-08 15:12:39 +0100 | [diff] [blame] | 734 | QTAILQ_REMOVE(&drives, dinfo, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 735 | g_free(dinfo); |
Markus Armbruster | a9ae2bf | 2011-02-08 15:12:39 +0100 | [diff] [blame] | 736 | return NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 739 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to) |
| 740 | { |
| 741 | const char *value; |
| 742 | |
| 743 | value = qemu_opt_get(opts, from); |
| 744 | if (value) { |
| 745 | qemu_opt_set(opts, to, value); |
| 746 | qemu_opt_unset(opts, from); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type) |
| 751 | { |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 752 | const char *value; |
| 753 | |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 754 | /* Change legacy command line options into QMP ones */ |
| 755 | qemu_opt_rename(all_opts, "iops", "throttling.iops-total"); |
| 756 | qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read"); |
| 757 | qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write"); |
| 758 | |
| 759 | qemu_opt_rename(all_opts, "bps", "throttling.bps-total"); |
| 760 | qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read"); |
| 761 | qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write"); |
| 762 | |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 763 | qemu_opt_rename(all_opts, "iops_max", "throttling.iops-total-max"); |
| 764 | qemu_opt_rename(all_opts, "iops_rd_max", "throttling.iops-read-max"); |
| 765 | qemu_opt_rename(all_opts, "iops_wr_max", "throttling.iops-write-max"); |
| 766 | |
| 767 | qemu_opt_rename(all_opts, "bps_max", "throttling.bps-total-max"); |
| 768 | qemu_opt_rename(all_opts, "bps_rd_max", "throttling.bps-read-max"); |
| 769 | qemu_opt_rename(all_opts, "bps_wr_max", "throttling.bps-write-max"); |
| 770 | |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 771 | qemu_opt_rename(all_opts, |
| 772 | "iops_size", "throttling.iops-size"); |
| 773 | |
Kevin Wolf | 0f227a9 | 2013-07-19 20:07:29 +0200 | [diff] [blame] | 774 | qemu_opt_rename(all_opts, "readonly", "read-only"); |
| 775 | |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 776 | value = qemu_opt_get(all_opts, "cache"); |
| 777 | if (value) { |
| 778 | int flags = 0; |
| 779 | |
| 780 | if (bdrv_parse_cache_flags(value, &flags) != 0) { |
| 781 | error_report("invalid cache option"); |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | /* Specific options take precedence */ |
| 786 | if (!qemu_opt_get(all_opts, "cache.writeback")) { |
| 787 | qemu_opt_set_bool(all_opts, "cache.writeback", |
| 788 | !!(flags & BDRV_O_CACHE_WB)); |
| 789 | } |
| 790 | if (!qemu_opt_get(all_opts, "cache.direct")) { |
| 791 | qemu_opt_set_bool(all_opts, "cache.direct", |
| 792 | !!(flags & BDRV_O_NOCACHE)); |
| 793 | } |
| 794 | if (!qemu_opt_get(all_opts, "cache.no-flush")) { |
| 795 | qemu_opt_set_bool(all_opts, "cache.no-flush", |
| 796 | !!(flags & BDRV_O_NO_FLUSH)); |
| 797 | } |
| 798 | qemu_opt_unset(all_opts, "cache"); |
| 799 | } |
| 800 | |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 801 | return blockdev_init(all_opts, block_default_type); |
| 802 | } |
| 803 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 804 | void do_commit(Monitor *mon, const QDict *qdict) |
| 805 | { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 806 | const char *device = qdict_get_str(qdict, "device"); |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 807 | BlockDriverState *bs; |
Stefan Hajnoczi | e887749 | 2012-03-05 18:10:11 +0000 | [diff] [blame] | 808 | int ret; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 809 | |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 810 | if (!strcmp(device, "all")) { |
Stefan Hajnoczi | e887749 | 2012-03-05 18:10:11 +0000 | [diff] [blame] | 811 | ret = bdrv_commit_all(); |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 812 | } else { |
| 813 | bs = bdrv_find(device); |
Markus Armbruster | ac59eb9 | 2010-06-02 18:55:19 +0200 | [diff] [blame] | 814 | if (!bs) { |
Jeff Cody | 58513bd | 2013-01-18 12:45:35 -0500 | [diff] [blame] | 815 | monitor_printf(mon, "Device '%s' not found\n", device); |
Markus Armbruster | ac59eb9 | 2010-06-02 18:55:19 +0200 | [diff] [blame] | 816 | return; |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 817 | } |
Stefan Hajnoczi | 2d3735d | 2012-01-18 14:40:41 +0000 | [diff] [blame] | 818 | ret = bdrv_commit(bs); |
Jeff Cody | 58513bd | 2013-01-18 12:45:35 -0500 | [diff] [blame] | 819 | } |
| 820 | if (ret < 0) { |
| 821 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, |
| 822 | strerror(-ret)); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 826 | static void blockdev_do_action(int kind, void *data, Error **errp) |
| 827 | { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 828 | TransactionAction action; |
| 829 | TransactionActionList list; |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 830 | |
| 831 | action.kind = kind; |
| 832 | action.data = data; |
| 833 | list.value = &action; |
| 834 | list.next = NULL; |
| 835 | qmp_transaction(&list, errp); |
| 836 | } |
| 837 | |
Luiz Capitulino | 6106e24 | 2011-11-25 16:15:19 -0200 | [diff] [blame] | 838 | void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file, |
| 839 | bool has_format, const char *format, |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 840 | bool has_mode, enum NewImageMode mode, |
Luiz Capitulino | 6106e24 | 2011-11-25 16:15:19 -0200 | [diff] [blame] | 841 | Error **errp) |
Jes Sorensen | f888256 | 2010-12-16 13:52:16 +0100 | [diff] [blame] | 842 | { |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 843 | BlockdevSnapshot snapshot = { |
| 844 | .device = (char *) device, |
| 845 | .snapshot_file = (char *) snapshot_file, |
| 846 | .has_format = has_format, |
| 847 | .format = (char *) format, |
| 848 | .has_mode = has_mode, |
| 849 | .mode = mode, |
| 850 | }; |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 851 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, |
| 852 | &snapshot, errp); |
Jes Sorensen | f888256 | 2010-12-16 13:52:16 +0100 | [diff] [blame] | 853 | } |
| 854 | |
Wenchao Xia | f323bc9 | 2013-09-11 14:04:35 +0800 | [diff] [blame] | 855 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
| 856 | const char *name, |
| 857 | Error **errp) |
| 858 | { |
| 859 | BlockdevSnapshotInternal snapshot = { |
| 860 | .device = (char *) device, |
| 861 | .name = (char *) name |
| 862 | }; |
| 863 | |
| 864 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, |
| 865 | &snapshot, errp); |
| 866 | } |
| 867 | |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 868 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
| 869 | bool has_id, |
| 870 | const char *id, |
| 871 | bool has_name, |
| 872 | const char *name, |
| 873 | Error **errp) |
| 874 | { |
| 875 | BlockDriverState *bs = bdrv_find(device); |
| 876 | QEMUSnapshotInfo sn; |
| 877 | Error *local_err = NULL; |
| 878 | SnapshotInfo *info = NULL; |
| 879 | int ret; |
| 880 | |
| 881 | if (!bs) { |
| 882 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 883 | return NULL; |
| 884 | } |
| 885 | |
| 886 | if (!has_id) { |
| 887 | id = NULL; |
| 888 | } |
| 889 | |
| 890 | if (!has_name) { |
| 891 | name = NULL; |
| 892 | } |
| 893 | |
| 894 | if (!id && !name) { |
| 895 | error_setg(errp, "Name or id must be provided"); |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); |
| 900 | if (error_is_set(&local_err)) { |
| 901 | error_propagate(errp, local_err); |
| 902 | return NULL; |
| 903 | } |
| 904 | if (!ret) { |
| 905 | error_setg(errp, |
| 906 | "Snapshot with id '%s' and name '%s' does not exist on " |
| 907 | "device '%s'", |
| 908 | STR_OR_NULL(id), STR_OR_NULL(name), device); |
| 909 | return NULL; |
| 910 | } |
| 911 | |
| 912 | bdrv_snapshot_delete(bs, id, name, &local_err); |
| 913 | if (error_is_set(&local_err)) { |
| 914 | error_propagate(errp, local_err); |
| 915 | return NULL; |
| 916 | } |
| 917 | |
| 918 | info = g_malloc0(sizeof(SnapshotInfo)); |
| 919 | info->id = g_strdup(sn.id_str); |
| 920 | info->name = g_strdup(sn.name); |
| 921 | info->date_nsec = sn.date_nsec; |
| 922 | info->date_sec = sn.date_sec; |
| 923 | info->vm_state_size = sn.vm_state_size; |
| 924 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; |
| 925 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; |
| 926 | |
| 927 | return info; |
| 928 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 929 | |
| 930 | /* New and old BlockDriverState structs for group snapshots */ |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 931 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 932 | typedef struct BlkTransactionState BlkTransactionState; |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 933 | |
| 934 | /* Only prepare() may fail. In a single transaction, only one of commit() or |
| 935 | abort() will be called, clean() will always be called if it present. */ |
| 936 | typedef struct BdrvActionOps { |
| 937 | /* Size of state struct, in bytes. */ |
| 938 | size_t instance_size; |
| 939 | /* Prepare the work, must NOT be NULL. */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 940 | void (*prepare)(BlkTransactionState *common, Error **errp); |
Stefan Hajnoczi | f9ea81e | 2013-06-24 17:13:16 +0200 | [diff] [blame] | 941 | /* Commit the changes, can be NULL. */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 942 | void (*commit)(BlkTransactionState *common); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 943 | /* Abort the changes on fail, can be NULL. */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 944 | void (*abort)(BlkTransactionState *common); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 945 | /* Clean up resource in the end, can be NULL. */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 946 | void (*clean)(BlkTransactionState *common); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 947 | } BdrvActionOps; |
| 948 | |
| 949 | /* |
| 950 | * This structure must be arranged as first member in child type, assuming |
| 951 | * that compiler will also arrange it to the same address with parent instance. |
| 952 | * Later it will be used in free(). |
| 953 | */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 954 | struct BlkTransactionState { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 955 | TransactionAction *action; |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 956 | const BdrvActionOps *ops; |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 957 | QSIMPLEQ_ENTRY(BlkTransactionState) entry; |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 958 | }; |
| 959 | |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 960 | /* internal snapshot private data */ |
| 961 | typedef struct InternalSnapshotState { |
| 962 | BlkTransactionState common; |
| 963 | BlockDriverState *bs; |
| 964 | QEMUSnapshotInfo sn; |
| 965 | } InternalSnapshotState; |
| 966 | |
| 967 | static void internal_snapshot_prepare(BlkTransactionState *common, |
| 968 | Error **errp) |
| 969 | { |
| 970 | const char *device; |
| 971 | const char *name; |
| 972 | BlockDriverState *bs; |
| 973 | QEMUSnapshotInfo old_sn, *sn; |
| 974 | bool ret; |
| 975 | qemu_timeval tv; |
| 976 | BlockdevSnapshotInternal *internal; |
| 977 | InternalSnapshotState *state; |
| 978 | int ret1; |
| 979 | |
| 980 | g_assert(common->action->kind == |
| 981 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); |
| 982 | internal = common->action->blockdev_snapshot_internal_sync; |
| 983 | state = DO_UPCAST(InternalSnapshotState, common, common); |
| 984 | |
| 985 | /* 1. parse input */ |
| 986 | device = internal->device; |
| 987 | name = internal->name; |
| 988 | |
| 989 | /* 2. check for validation */ |
| 990 | bs = bdrv_find(device); |
| 991 | if (!bs) { |
| 992 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | if (!bdrv_is_inserted(bs)) { |
| 997 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
| 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | if (bdrv_is_read_only(bs)) { |
| 1002 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | if (!bdrv_can_snapshot(bs)) { |
| 1007 | error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, |
| 1008 | bs->drv->format_name, device, "internal snapshot"); |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | if (!strlen(name)) { |
| 1013 | error_setg(errp, "Name is empty"); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | /* check whether a snapshot with name exist */ |
| 1018 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, errp); |
| 1019 | if (error_is_set(errp)) { |
| 1020 | return; |
| 1021 | } else if (ret) { |
| 1022 | error_setg(errp, |
| 1023 | "Snapshot with name '%s' already exists on device '%s'", |
| 1024 | name, device); |
| 1025 | return; |
| 1026 | } |
| 1027 | |
| 1028 | /* 3. take the snapshot */ |
| 1029 | sn = &state->sn; |
| 1030 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 1031 | qemu_gettimeofday(&tv); |
| 1032 | sn->date_sec = tv.tv_sec; |
| 1033 | sn->date_nsec = tv.tv_usec * 1000; |
| 1034 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 1035 | |
| 1036 | ret1 = bdrv_snapshot_create(bs, sn); |
| 1037 | if (ret1 < 0) { |
| 1038 | error_setg_errno(errp, -ret1, |
| 1039 | "Failed to create snapshot '%s' on device '%s'", |
| 1040 | name, device); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | /* 4. succeed, mark a snapshot is created */ |
| 1045 | state->bs = bs; |
| 1046 | } |
| 1047 | |
| 1048 | static void internal_snapshot_abort(BlkTransactionState *common) |
| 1049 | { |
| 1050 | InternalSnapshotState *state = |
| 1051 | DO_UPCAST(InternalSnapshotState, common, common); |
| 1052 | BlockDriverState *bs = state->bs; |
| 1053 | QEMUSnapshotInfo *sn = &state->sn; |
| 1054 | Error *local_error = NULL; |
| 1055 | |
| 1056 | if (!bs) { |
| 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { |
| 1061 | error_report("Failed to delete snapshot with id '%s' and name '%s' on " |
| 1062 | "device '%s' in abort: %s", |
| 1063 | sn->id_str, |
| 1064 | sn->name, |
| 1065 | bdrv_get_device_name(bs), |
| 1066 | error_get_pretty(local_error)); |
| 1067 | error_free(local_error); |
| 1068 | } |
| 1069 | } |
| 1070 | |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1071 | /* external snapshot private data */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1072 | typedef struct ExternalSnapshotState { |
| 1073 | BlkTransactionState common; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1074 | BlockDriverState *old_bs; |
| 1075 | BlockDriverState *new_bs; |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1076 | } ExternalSnapshotState; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1077 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1078 | static void external_snapshot_prepare(BlkTransactionState *common, |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1079 | Error **errp) |
| 1080 | { |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1081 | BlockDriver *drv; |
| 1082 | int flags, ret; |
| 1083 | Error *local_err = NULL; |
Wenchao Xia | e2a31e8 | 2013-05-08 18:25:13 +0800 | [diff] [blame] | 1084 | const char *device; |
| 1085 | const char *new_image_file; |
| 1086 | const char *format = "qcow2"; |
| 1087 | enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1088 | ExternalSnapshotState *state = |
| 1089 | DO_UPCAST(ExternalSnapshotState, common, common); |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1090 | TransactionAction *action = common->action; |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1091 | |
Wenchao Xia | e2a31e8 | 2013-05-08 18:25:13 +0800 | [diff] [blame] | 1092 | /* get parameters */ |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1093 | g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); |
Wenchao Xia | e2a31e8 | 2013-05-08 18:25:13 +0800 | [diff] [blame] | 1094 | |
| 1095 | device = action->blockdev_snapshot_sync->device; |
| 1096 | new_image_file = action->blockdev_snapshot_sync->snapshot_file; |
| 1097 | if (action->blockdev_snapshot_sync->has_format) { |
| 1098 | format = action->blockdev_snapshot_sync->format; |
| 1099 | } |
| 1100 | if (action->blockdev_snapshot_sync->has_mode) { |
| 1101 | mode = action->blockdev_snapshot_sync->mode; |
| 1102 | } |
| 1103 | |
| 1104 | /* start processing */ |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1105 | drv = bdrv_find_format(format); |
| 1106 | if (!drv) { |
| 1107 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
| 1108 | return; |
| 1109 | } |
| 1110 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1111 | state->old_bs = bdrv_find(device); |
| 1112 | if (!state->old_bs) { |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1113 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1114 | return; |
| 1115 | } |
| 1116 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1117 | if (!bdrv_is_inserted(state->old_bs)) { |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1118 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
| 1119 | return; |
| 1120 | } |
| 1121 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1122 | if (bdrv_in_use(state->old_bs)) { |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1123 | error_set(errp, QERR_DEVICE_IN_USE, device); |
| 1124 | return; |
| 1125 | } |
| 1126 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1127 | if (!bdrv_is_read_only(state->old_bs)) { |
| 1128 | if (bdrv_flush(state->old_bs)) { |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1129 | error_set(errp, QERR_IO_ERROR); |
| 1130 | return; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Benoît Canet | f6186f4 | 2013-10-02 14:33:48 +0200 | [diff] [blame] | 1134 | if (bdrv_check_ext_snapshot(state->old_bs) != EXT_SNAPSHOT_ALLOWED) { |
| 1135 | error_set(errp, QERR_FEATURE_DISABLED, "snapshot"); |
| 1136 | return; |
| 1137 | } |
| 1138 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1139 | flags = state->old_bs->open_flags; |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1140 | |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1141 | /* create new image w/backing file */ |
| 1142 | if (mode != NEW_IMAGE_MODE_EXISTING) { |
| 1143 | bdrv_img_create(new_image_file, format, |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1144 | state->old_bs->filename, |
| 1145 | state->old_bs->drv->format_name, |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1146 | NULL, -1, flags, &local_err, false); |
| 1147 | if (error_is_set(&local_err)) { |
| 1148 | error_propagate(errp, local_err); |
| 1149 | return; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /* We will manually add the backing_hd field to the bs later */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1154 | state->new_bs = bdrv_new(""); |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1155 | /* TODO Inherit bs->options or only take explicit options with an |
| 1156 | * extended QMP command? */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1157 | ret = bdrv_open(state->new_bs, new_image_file, NULL, |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1158 | flags | BDRV_O_NO_BACKING, drv, &local_err); |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1159 | if (ret != 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1160 | error_propagate(errp, local_err); |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1164 | static void external_snapshot_commit(BlkTransactionState *common) |
Wenchao Xia | 3b0047e | 2013-05-08 18:25:14 +0800 | [diff] [blame] | 1165 | { |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1166 | ExternalSnapshotState *state = |
| 1167 | DO_UPCAST(ExternalSnapshotState, common, common); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1168 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1169 | /* This removes our old bs and adds the new bs */ |
| 1170 | bdrv_append(state->new_bs, state->old_bs); |
Wenchao Xia | 3b0047e | 2013-05-08 18:25:14 +0800 | [diff] [blame] | 1171 | /* We don't need (or want) to use the transactional |
| 1172 | * bdrv_reopen_multiple() across all the entries at once, because we |
| 1173 | * don't want to abort all of them if one of them fails the reopen */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1174 | bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR, |
Wenchao Xia | 3b0047e | 2013-05-08 18:25:14 +0800 | [diff] [blame] | 1175 | NULL); |
| 1176 | } |
| 1177 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1178 | static void external_snapshot_abort(BlkTransactionState *common) |
Wenchao Xia | 96b86bf | 2013-05-08 18:25:15 +0800 | [diff] [blame] | 1179 | { |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1180 | ExternalSnapshotState *state = |
| 1181 | DO_UPCAST(ExternalSnapshotState, common, common); |
| 1182 | if (state->new_bs) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1183 | bdrv_unref(state->new_bs); |
Wenchao Xia | 96b86bf | 2013-05-08 18:25:15 +0800 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1187 | typedef struct DriveBackupState { |
| 1188 | BlkTransactionState common; |
| 1189 | BlockDriverState *bs; |
| 1190 | BlockJob *job; |
| 1191 | } DriveBackupState; |
| 1192 | |
| 1193 | static void drive_backup_prepare(BlkTransactionState *common, Error **errp) |
| 1194 | { |
| 1195 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); |
| 1196 | DriveBackup *backup; |
| 1197 | Error *local_err = NULL; |
| 1198 | |
| 1199 | assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); |
| 1200 | backup = common->action->drive_backup; |
| 1201 | |
| 1202 | qmp_drive_backup(backup->device, backup->target, |
| 1203 | backup->has_format, backup->format, |
Stefan Hajnoczi | b53169e | 2013-06-26 14:11:57 +0200 | [diff] [blame] | 1204 | backup->sync, |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1205 | backup->has_mode, backup->mode, |
| 1206 | backup->has_speed, backup->speed, |
| 1207 | backup->has_on_source_error, backup->on_source_error, |
| 1208 | backup->has_on_target_error, backup->on_target_error, |
| 1209 | &local_err); |
| 1210 | if (error_is_set(&local_err)) { |
| 1211 | error_propagate(errp, local_err); |
| 1212 | state->bs = NULL; |
| 1213 | state->job = NULL; |
| 1214 | return; |
| 1215 | } |
| 1216 | |
| 1217 | state->bs = bdrv_find(backup->device); |
| 1218 | state->job = state->bs->job; |
| 1219 | } |
| 1220 | |
| 1221 | static void drive_backup_abort(BlkTransactionState *common) |
| 1222 | { |
| 1223 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); |
| 1224 | BlockDriverState *bs = state->bs; |
| 1225 | |
| 1226 | /* Only cancel if it's the job we started */ |
| 1227 | if (bs && bs->job && bs->job == state->job) { |
| 1228 | block_job_cancel_sync(bs->job); |
| 1229 | } |
| 1230 | } |
| 1231 | |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 1232 | static void abort_prepare(BlkTransactionState *common, Error **errp) |
| 1233 | { |
| 1234 | error_setg(errp, "Transaction aborted using Abort action"); |
| 1235 | } |
| 1236 | |
| 1237 | static void abort_commit(BlkTransactionState *common) |
| 1238 | { |
Stefan Weil | dfc6f86 | 2013-07-25 18:21:28 +0200 | [diff] [blame] | 1239 | g_assert_not_reached(); /* this action never succeeds */ |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1242 | static const BdrvActionOps actions[] = { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1243 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1244 | .instance_size = sizeof(ExternalSnapshotState), |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1245 | .prepare = external_snapshot_prepare, |
| 1246 | .commit = external_snapshot_commit, |
| 1247 | .abort = external_snapshot_abort, |
| 1248 | }, |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1249 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
| 1250 | .instance_size = sizeof(DriveBackupState), |
| 1251 | .prepare = drive_backup_prepare, |
| 1252 | .abort = drive_backup_abort, |
| 1253 | }, |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 1254 | [TRANSACTION_ACTION_KIND_ABORT] = { |
| 1255 | .instance_size = sizeof(BlkTransactionState), |
| 1256 | .prepare = abort_prepare, |
| 1257 | .commit = abort_commit, |
| 1258 | }, |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1259 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
| 1260 | .instance_size = sizeof(InternalSnapshotState), |
| 1261 | .prepare = internal_snapshot_prepare, |
| 1262 | .abort = internal_snapshot_abort, |
| 1263 | }, |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1264 | }; |
| 1265 | |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1266 | /* |
| 1267 | * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail |
| 1268 | * then we do not pivot any of the devices in the group, and abandon the |
| 1269 | * snapshots |
| 1270 | */ |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1271 | void qmp_transaction(TransactionActionList *dev_list, Error **errp) |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1272 | { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1273 | TransactionActionList *dev_entry = dev_list; |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1274 | BlkTransactionState *state, *next; |
Luiz Capitulino | 43e1704 | 2012-11-30 10:52:07 -0200 | [diff] [blame] | 1275 | Error *local_err = NULL; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1276 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1277 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1278 | QSIMPLEQ_INIT(&snap_bdrv_states); |
| 1279 | |
| 1280 | /* drain all i/o before any snapshots */ |
| 1281 | bdrv_drain_all(); |
| 1282 | |
| 1283 | /* We don't do anything in this loop that commits us to the snapshot */ |
| 1284 | while (NULL != dev_entry) { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1285 | TransactionAction *dev_info = NULL; |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1286 | const BdrvActionOps *ops; |
Paolo Bonzini | 52e7c24 | 2012-03-06 18:55:57 +0100 | [diff] [blame] | 1287 | |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1288 | dev_info = dev_entry->value; |
| 1289 | dev_entry = dev_entry->next; |
| 1290 | |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1291 | assert(dev_info->kind < ARRAY_SIZE(actions)); |
| 1292 | |
| 1293 | ops = &actions[dev_info->kind]; |
Max Reitz | aa3fe71 | 2013-09-12 14:57:27 +0200 | [diff] [blame] | 1294 | assert(ops->instance_size > 0); |
| 1295 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1296 | state = g_malloc0(ops->instance_size); |
| 1297 | state->ops = ops; |
| 1298 | state->action = dev_info; |
| 1299 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1300 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1301 | state->ops->prepare(state, &local_err); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1302 | if (error_is_set(&local_err)) { |
| 1303 | error_propagate(errp, local_err); |
| 1304 | goto delete_and_fail; |
Paolo Bonzini | 52e7c24 | 2012-03-06 18:55:57 +0100 | [diff] [blame] | 1305 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1306 | } |
| 1307 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1308 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
Stefan Hajnoczi | f9ea81e | 2013-06-24 17:13:16 +0200 | [diff] [blame] | 1309 | if (state->ops->commit) { |
| 1310 | state->ops->commit(state); |
| 1311 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* success */ |
| 1315 | goto exit; |
| 1316 | |
| 1317 | delete_and_fail: |
| 1318 | /* |
| 1319 | * failure, and it is all-or-none; abandon each new bs, and keep using |
| 1320 | * the original bs for all images |
| 1321 | */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1322 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
| 1323 | if (state->ops->abort) { |
| 1324 | state->ops->abort(state); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1325 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1326 | } |
| 1327 | exit: |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1328 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
| 1329 | if (state->ops->clean) { |
| 1330 | state->ops->clean(state); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1331 | } |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1332 | g_free(state); |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1333 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1337 | static void eject_device(BlockDriverState *bs, int force, Error **errp) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1338 | { |
Stefan Hajnoczi | 2d3735d | 2012-01-18 14:40:41 +0000 | [diff] [blame] | 1339 | if (bdrv_in_use(bs)) { |
| 1340 | error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); |
| 1341 | return; |
| 1342 | } |
Markus Armbruster | 2c6942f | 2011-09-06 18:58:51 +0200 | [diff] [blame] | 1343 | if (!bdrv_dev_has_removable_media(bs)) { |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1344 | error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs)); |
| 1345 | return; |
Markus Armbruster | ea8f942 | 2011-07-20 18:23:35 +0200 | [diff] [blame] | 1346 | } |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1347 | |
Paolo Bonzini | 025ccaa | 2011-11-07 17:50:13 +0100 | [diff] [blame] | 1348 | if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) { |
| 1349 | bdrv_dev_eject_request(bs, force); |
| 1350 | if (!force) { |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1351 | error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); |
| 1352 | return; |
Paolo Bonzini | 025ccaa | 2011-11-07 17:50:13 +0100 | [diff] [blame] | 1353 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1354 | } |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1355 | |
Eduardo Habkost | 3b5276b | 2010-06-01 19:12:19 -0300 | [diff] [blame] | 1356 | bdrv_close(bs); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
Luiz Capitulino | c245b6a | 2011-12-07 16:02:36 -0200 | [diff] [blame] | 1359 | void qmp_eject(const char *device, bool has_force, bool force, Error **errp) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1360 | { |
| 1361 | BlockDriverState *bs; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1362 | |
Luiz Capitulino | c245b6a | 2011-12-07 16:02:36 -0200 | [diff] [blame] | 1363 | bs = bdrv_find(device); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1364 | if (!bs) { |
Luiz Capitulino | c245b6a | 2011-12-07 16:02:36 -0200 | [diff] [blame] | 1365 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1366 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1367 | } |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1368 | |
Luiz Capitulino | c245b6a | 2011-12-07 16:02:36 -0200 | [diff] [blame] | 1369 | eject_device(bs, force, errp); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 1372 | void qmp_block_passwd(const char *device, const char *password, Error **errp) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1373 | { |
| 1374 | BlockDriverState *bs; |
| 1375 | int err; |
| 1376 | |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 1377 | bs = bdrv_find(device); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1378 | if (!bs) { |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 1379 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1380 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 1383 | err = bdrv_set_key(bs, password); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1384 | if (err == -EINVAL) { |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 1385 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
| 1386 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1387 | } else if (err < 0) { |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 1388 | error_set(errp, QERR_INVALID_PASSWORD); |
| 1389 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1390 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1391 | } |
| 1392 | |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1393 | static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, |
| 1394 | int bdrv_flags, BlockDriver *drv, |
| 1395 | const char *password, Error **errp) |
| 1396 | { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1397 | Error *local_err = NULL; |
Luiz Capitulino | 0eef407 | 2013-06-07 14:33:48 -0400 | [diff] [blame] | 1398 | int ret; |
| 1399 | |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1400 | ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv, &local_err); |
Luiz Capitulino | 0eef407 | 2013-06-07 14:33:48 -0400 | [diff] [blame] | 1401 | if (ret < 0) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1402 | error_propagate(errp, local_err); |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | |
| 1406 | if (bdrv_key_required(bs)) { |
| 1407 | if (password) { |
| 1408 | if (bdrv_set_key(bs, password) < 0) { |
| 1409 | error_set(errp, QERR_INVALID_PASSWORD); |
| 1410 | } |
| 1411 | } else { |
| 1412 | error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), |
| 1413 | bdrv_get_encrypted_filename(bs)); |
| 1414 | } |
| 1415 | } else if (password) { |
| 1416 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | void qmp_change_blockdev(const char *device, const char *filename, |
| 1421 | bool has_format, const char *format, Error **errp) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1422 | { |
| 1423 | BlockDriverState *bs; |
| 1424 | BlockDriver *drv = NULL; |
| 1425 | int bdrv_flags; |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1426 | Error *err = NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1427 | |
| 1428 | bs = bdrv_find(device); |
| 1429 | if (!bs) { |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1430 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1431 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1432 | } |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1433 | |
| 1434 | if (format) { |
Fam Zheng | b64ec4e | 2013-05-29 19:35:40 +0800 | [diff] [blame] | 1435 | drv = bdrv_find_whitelisted_format(format, bs->read_only); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1436 | if (!drv) { |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1437 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
| 1438 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1439 | } |
| 1440 | } |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1441 | |
Luiz Capitulino | 92d4855 | 2011-12-07 15:47:23 -0200 | [diff] [blame] | 1442 | eject_device(bs, 0, &err); |
| 1443 | if (error_is_set(&err)) { |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1444 | error_propagate(errp, err); |
| 1445 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1446 | } |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1447 | |
Markus Armbruster | 528f766 | 2010-06-25 15:26:48 +0200 | [diff] [blame] | 1448 | bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; |
Blue Swirl | 199630b | 2010-07-25 20:49:34 +0000 | [diff] [blame] | 1449 | bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; |
Luiz Capitulino | 333a96e | 2011-12-08 11:13:50 -0200 | [diff] [blame] | 1450 | |
| 1451 | qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1452 | } |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 1453 | |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1454 | /* throttling disk I/O limits */ |
Luiz Capitulino | 80047da | 2011-12-14 16:49:14 -0200 | [diff] [blame] | 1455 | void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 1456 | int64_t bps_wr, |
| 1457 | int64_t iops, |
| 1458 | int64_t iops_rd, |
| 1459 | int64_t iops_wr, |
| 1460 | bool has_bps_max, |
| 1461 | int64_t bps_max, |
| 1462 | bool has_bps_rd_max, |
| 1463 | int64_t bps_rd_max, |
| 1464 | bool has_bps_wr_max, |
| 1465 | int64_t bps_wr_max, |
| 1466 | bool has_iops_max, |
| 1467 | int64_t iops_max, |
| 1468 | bool has_iops_rd_max, |
| 1469 | int64_t iops_rd_max, |
| 1470 | bool has_iops_wr_max, |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 1471 | int64_t iops_wr_max, |
| 1472 | bool has_iops_size, |
| 1473 | int64_t iops_size, Error **errp) |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1474 | { |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1475 | ThrottleConfig cfg; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1476 | BlockDriverState *bs; |
| 1477 | |
Luiz Capitulino | 80047da | 2011-12-14 16:49:14 -0200 | [diff] [blame] | 1478 | bs = bdrv_find(device); |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1479 | if (!bs) { |
Luiz Capitulino | 80047da | 2011-12-14 16:49:14 -0200 | [diff] [blame] | 1480 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1481 | return; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1484 | memset(&cfg, 0, sizeof(cfg)); |
| 1485 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; |
| 1486 | cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; |
| 1487 | cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1488 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1489 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; |
| 1490 | cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; |
| 1491 | cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; |
| 1492 | |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 1493 | if (has_bps_max) { |
| 1494 | cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; |
| 1495 | } |
| 1496 | if (has_bps_rd_max) { |
| 1497 | cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; |
| 1498 | } |
| 1499 | if (has_bps_wr_max) { |
| 1500 | cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; |
| 1501 | } |
| 1502 | if (has_iops_max) { |
| 1503 | cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; |
| 1504 | } |
| 1505 | if (has_iops_rd_max) { |
| 1506 | cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; |
| 1507 | } |
| 1508 | if (has_iops_wr_max) { |
| 1509 | cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; |
| 1510 | } |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1511 | |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 1512 | if (has_iops_size) { |
| 1513 | cfg.op_size = iops_size; |
| 1514 | } |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1515 | |
| 1516 | if (!check_throttle_config(&cfg, errp)) { |
Luiz Capitulino | 80047da | 2011-12-14 16:49:14 -0200 | [diff] [blame] | 1517 | return; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1518 | } |
| 1519 | |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1520 | if (!bs->io_limits_enabled && throttle_enabled(&cfg)) { |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1521 | bdrv_io_limits_enable(bs); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1522 | } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) { |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1523 | bdrv_io_limits_disable(bs); |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | if (bs->io_limits_enabled) { |
| 1527 | bdrv_set_io_limits(bs, &cfg); |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1528 | } |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 1529 | } |
| 1530 | |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 1531 | int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
| 1532 | { |
| 1533 | const char *id = qdict_get_str(qdict, "id"); |
| 1534 | BlockDriverState *bs; |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 1535 | |
| 1536 | bs = bdrv_find(id); |
| 1537 | if (!bs) { |
| 1538 | qerror_report(QERR_DEVICE_NOT_FOUND, id); |
| 1539 | return -1; |
| 1540 | } |
Marcelo Tosatti | 8591675 | 2011-01-26 12:12:35 -0200 | [diff] [blame] | 1541 | if (bdrv_in_use(bs)) { |
| 1542 | qerror_report(QERR_DEVICE_IN_USE, id); |
| 1543 | return -1; |
| 1544 | } |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 1545 | |
| 1546 | /* quiesce block driver; prevent further io */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1547 | bdrv_drain_all(); |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 1548 | bdrv_flush(bs); |
| 1549 | bdrv_close(bs); |
| 1550 | |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1551 | /* if we have a device attached to this BlockDriverState |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 1552 | * then we need to make the drive anonymous until the device |
| 1553 | * can be removed. If this is a drive with no device backing |
| 1554 | * then we can just get rid of the block driver state right here. |
| 1555 | */ |
Markus Armbruster | fa879d6 | 2011-08-03 15:07:40 +0200 | [diff] [blame] | 1556 | if (bdrv_get_attached_dev(bs)) { |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 1557 | bdrv_make_anon(bs); |
Stefan Hajnoczi | 293c51a | 2013-06-05 10:33:14 +0200 | [diff] [blame] | 1558 | |
| 1559 | /* Further I/O must not pause the guest */ |
| 1560 | bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT, |
| 1561 | BLOCKDEV_ON_ERROR_REPORT); |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 1562 | } else { |
| 1563 | drive_uninit(drive_get_by_blockdev(bs)); |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 1564 | } |
| 1565 | |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1568 | |
Luiz Capitulino | 5e7caac | 2011-11-25 14:57:10 -0200 | [diff] [blame] | 1569 | void qmp_block_resize(const char *device, int64_t size, Error **errp) |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1570 | { |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1571 | BlockDriverState *bs; |
Kevin Wolf | 8732901 | 2013-05-02 15:32:55 +0200 | [diff] [blame] | 1572 | int ret; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1573 | |
| 1574 | bs = bdrv_find(device); |
| 1575 | if (!bs) { |
Luiz Capitulino | 5e7caac | 2011-11-25 14:57:10 -0200 | [diff] [blame] | 1576 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1577 | return; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | if (size < 0) { |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 1581 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
Luiz Capitulino | 5e7caac | 2011-11-25 14:57:10 -0200 | [diff] [blame] | 1582 | return; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
Peter Lieven | 92b7a08 | 2013-03-11 11:04:24 +0100 | [diff] [blame] | 1585 | /* complete all in-flight operations before resizing the device */ |
| 1586 | bdrv_drain_all(); |
| 1587 | |
Kevin Wolf | 8732901 | 2013-05-02 15:32:55 +0200 | [diff] [blame] | 1588 | ret = bdrv_truncate(bs, size); |
| 1589 | switch (ret) { |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 1590 | case 0: |
| 1591 | break; |
| 1592 | case -ENOMEDIUM: |
| 1593 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
| 1594 | break; |
| 1595 | case -ENOTSUP: |
| 1596 | error_set(errp, QERR_UNSUPPORTED); |
| 1597 | break; |
| 1598 | case -EACCES: |
| 1599 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); |
| 1600 | break; |
| 1601 | case -EBUSY: |
| 1602 | error_set(errp, QERR_DEVICE_IN_USE, device); |
| 1603 | break; |
| 1604 | default: |
Kevin Wolf | 8732901 | 2013-05-02 15:32:55 +0200 | [diff] [blame] | 1605 | error_setg_errno(errp, -ret, "Could not resize"); |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 1606 | break; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1607 | } |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 1608 | } |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1609 | |
Jeff Cody | 9abf2db | 2012-09-27 13:29:14 -0400 | [diff] [blame] | 1610 | static void block_job_cb(void *opaque, int ret) |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1611 | { |
| 1612 | BlockDriverState *bs = opaque; |
| 1613 | QObject *obj; |
| 1614 | |
Jeff Cody | 9abf2db | 2012-09-27 13:29:14 -0400 | [diff] [blame] | 1615 | trace_block_job_cb(bs, bs->job, ret); |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1616 | |
| 1617 | assert(bs->job); |
| 1618 | obj = qobject_from_block_job(bs->job); |
| 1619 | if (ret < 0) { |
| 1620 | QDict *dict = qobject_to_qdict(obj); |
| 1621 | qdict_put(dict, "error", qstring_from_str(strerror(-ret))); |
| 1622 | } |
| 1623 | |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 1624 | if (block_job_is_cancelled(bs->job)) { |
| 1625 | monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj); |
| 1626 | } else { |
| 1627 | monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj); |
| 1628 | } |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1629 | qobject_decref(obj); |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 1630 | |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 1631 | bdrv_put_ref_bh_schedule(bs); |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | void qmp_block_stream(const char *device, bool has_base, |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 1635 | const char *base, bool has_speed, int64_t speed, |
| 1636 | bool has_on_error, BlockdevOnError on_error, |
| 1637 | Error **errp) |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1638 | { |
| 1639 | BlockDriverState *bs; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 1640 | BlockDriverState *base_bs = NULL; |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 1641 | Error *local_err = NULL; |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1642 | |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 1643 | if (!has_on_error) { |
| 1644 | on_error = BLOCKDEV_ON_ERROR_REPORT; |
| 1645 | } |
| 1646 | |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1647 | bs = bdrv_find(device); |
| 1648 | if (!bs) { |
| 1649 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1650 | return; |
| 1651 | } |
| 1652 | |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1653 | if (base) { |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 1654 | base_bs = bdrv_find_backing_image(bs, base); |
| 1655 | if (base_bs == NULL) { |
| 1656 | error_set(errp, QERR_BASE_NOT_FOUND, base); |
| 1657 | return; |
| 1658 | } |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Stefan Hajnoczi | c83c66c | 2012-04-25 16:51:03 +0100 | [diff] [blame] | 1661 | stream_start(bs, base_bs, base, has_speed ? speed : 0, |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 1662 | on_error, block_job_cb, bs, &local_err); |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 1663 | if (error_is_set(&local_err)) { |
| 1664 | error_propagate(errp, local_err); |
| 1665 | return; |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | trace_qmp_block_stream(bs, bs->job); |
| 1669 | } |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 1670 | |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 1671 | void qmp_block_commit(const char *device, |
| 1672 | bool has_base, const char *base, const char *top, |
| 1673 | bool has_speed, int64_t speed, |
| 1674 | Error **errp) |
| 1675 | { |
| 1676 | BlockDriverState *bs; |
| 1677 | BlockDriverState *base_bs, *top_bs; |
| 1678 | Error *local_err = NULL; |
| 1679 | /* This will be part of the QMP command, if/when the |
| 1680 | * BlockdevOnError change for blkmirror makes it in |
| 1681 | */ |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 1682 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 1683 | |
| 1684 | /* drain all i/o before commits */ |
| 1685 | bdrv_drain_all(); |
| 1686 | |
| 1687 | bs = bdrv_find(device); |
| 1688 | if (!bs) { |
| 1689 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1690 | return; |
| 1691 | } |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 1692 | |
| 1693 | /* default top_bs is the active layer */ |
| 1694 | top_bs = bs; |
| 1695 | |
| 1696 | if (top) { |
| 1697 | if (strcmp(bs->filename, top) != 0) { |
| 1698 | top_bs = bdrv_find_backing_image(bs, top); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | if (top_bs == NULL) { |
| 1703 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); |
| 1704 | return; |
| 1705 | } |
| 1706 | |
Jeff Cody | d5208c4 | 2012-10-16 15:49:10 -0400 | [diff] [blame] | 1707 | if (has_base && base) { |
| 1708 | base_bs = bdrv_find_backing_image(top_bs, base); |
| 1709 | } else { |
| 1710 | base_bs = bdrv_find_base(top_bs); |
| 1711 | } |
| 1712 | |
| 1713 | if (base_bs == NULL) { |
| 1714 | error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); |
| 1715 | return; |
| 1716 | } |
| 1717 | |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 1718 | commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, |
| 1719 | &local_err); |
| 1720 | if (local_err != NULL) { |
| 1721 | error_propagate(errp, local_err); |
| 1722 | return; |
| 1723 | } |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 1724 | } |
| 1725 | |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1726 | void qmp_drive_backup(const char *device, const char *target, |
| 1727 | bool has_format, const char *format, |
Stefan Hajnoczi | b53169e | 2013-06-26 14:11:57 +0200 | [diff] [blame] | 1728 | enum MirrorSyncMode sync, |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1729 | bool has_mode, enum NewImageMode mode, |
| 1730 | bool has_speed, int64_t speed, |
| 1731 | bool has_on_source_error, BlockdevOnError on_source_error, |
| 1732 | bool has_on_target_error, BlockdevOnError on_target_error, |
| 1733 | Error **errp) |
| 1734 | { |
| 1735 | BlockDriverState *bs; |
| 1736 | BlockDriverState *target_bs; |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 1737 | BlockDriverState *source = NULL; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1738 | BlockDriver *drv = NULL; |
| 1739 | Error *local_err = NULL; |
| 1740 | int flags; |
| 1741 | int64_t size; |
| 1742 | int ret; |
| 1743 | |
| 1744 | if (!has_speed) { |
| 1745 | speed = 0; |
| 1746 | } |
| 1747 | if (!has_on_source_error) { |
| 1748 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; |
| 1749 | } |
| 1750 | if (!has_on_target_error) { |
| 1751 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; |
| 1752 | } |
| 1753 | if (!has_mode) { |
| 1754 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; |
| 1755 | } |
| 1756 | |
| 1757 | bs = bdrv_find(device); |
| 1758 | if (!bs) { |
| 1759 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | if (!bdrv_is_inserted(bs)) { |
| 1764 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
| 1765 | return; |
| 1766 | } |
| 1767 | |
| 1768 | if (!has_format) { |
| 1769 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; |
| 1770 | } |
| 1771 | if (format) { |
| 1772 | drv = bdrv_find_format(format); |
| 1773 | if (!drv) { |
| 1774 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
| 1775 | return; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | if (bdrv_in_use(bs)) { |
| 1780 | error_set(errp, QERR_DEVICE_IN_USE, device); |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | flags = bs->open_flags | BDRV_O_RDWR; |
| 1785 | |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 1786 | /* See if we have a backing HD we can use to create our new image |
| 1787 | * on top of. */ |
| 1788 | if (sync == MIRROR_SYNC_MODE_TOP) { |
| 1789 | source = bs->backing_hd; |
| 1790 | if (!source) { |
| 1791 | sync = MIRROR_SYNC_MODE_FULL; |
| 1792 | } |
| 1793 | } |
| 1794 | if (sync == MIRROR_SYNC_MODE_NONE) { |
| 1795 | source = bs; |
| 1796 | } |
| 1797 | |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1798 | size = bdrv_getlength(bs); |
| 1799 | if (size < 0) { |
| 1800 | error_setg_errno(errp, -size, "bdrv_getlength failed"); |
| 1801 | return; |
| 1802 | } |
| 1803 | |
| 1804 | if (mode != NEW_IMAGE_MODE_EXISTING) { |
| 1805 | assert(format && drv); |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 1806 | if (source) { |
| 1807 | bdrv_img_create(target, format, source->filename, |
| 1808 | source->drv->format_name, NULL, |
| 1809 | size, flags, &local_err, false); |
| 1810 | } else { |
| 1811 | bdrv_img_create(target, format, NULL, NULL, NULL, |
| 1812 | size, flags, &local_err, false); |
| 1813 | } |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | if (error_is_set(&local_err)) { |
| 1817 | error_propagate(errp, local_err); |
| 1818 | return; |
| 1819 | } |
| 1820 | |
| 1821 | target_bs = bdrv_new(""); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1822 | ret = bdrv_open(target_bs, target, NULL, flags, drv, &local_err); |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1823 | if (ret < 0) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1824 | bdrv_unref(target_bs); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1825 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1826 | return; |
| 1827 | } |
| 1828 | |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 1829 | backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error, |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1830 | block_job_cb, bs, &local_err); |
| 1831 | if (local_err != NULL) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1832 | bdrv_unref(target_bs); |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1833 | error_propagate(errp, local_err); |
| 1834 | return; |
| 1835 | } |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 1836 | } |
| 1837 | |
Paolo Bonzini | 08e4ed6 | 2013-01-22 09:03:13 +0100 | [diff] [blame] | 1838 | #define DEFAULT_MIRROR_BUF_SIZE (10 << 20) |
| 1839 | |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1840 | void qmp_drive_mirror(const char *device, const char *target, |
| 1841 | bool has_format, const char *format, |
| 1842 | enum MirrorSyncMode sync, |
| 1843 | bool has_mode, enum NewImageMode mode, |
Paolo Bonzini | b952b55 | 2012-10-18 16:49:28 +0200 | [diff] [blame] | 1844 | bool has_speed, int64_t speed, |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 1845 | bool has_granularity, uint32_t granularity, |
Paolo Bonzini | 08e4ed6 | 2013-01-22 09:03:13 +0100 | [diff] [blame] | 1846 | bool has_buf_size, int64_t buf_size, |
Paolo Bonzini | b952b55 | 2012-10-18 16:49:28 +0200 | [diff] [blame] | 1847 | bool has_on_source_error, BlockdevOnError on_source_error, |
| 1848 | bool has_on_target_error, BlockdevOnError on_target_error, |
| 1849 | Error **errp) |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1850 | { |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1851 | BlockDriverState *bs; |
| 1852 | BlockDriverState *source, *target_bs; |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1853 | BlockDriver *drv = NULL; |
| 1854 | Error *local_err = NULL; |
| 1855 | int flags; |
Stefan Hajnoczi | ac3c5d8 | 2013-06-24 17:13:13 +0200 | [diff] [blame] | 1856 | int64_t size; |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1857 | int ret; |
| 1858 | |
| 1859 | if (!has_speed) { |
| 1860 | speed = 0; |
| 1861 | } |
Paolo Bonzini | b952b55 | 2012-10-18 16:49:28 +0200 | [diff] [blame] | 1862 | if (!has_on_source_error) { |
| 1863 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; |
| 1864 | } |
| 1865 | if (!has_on_target_error) { |
| 1866 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; |
| 1867 | } |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1868 | if (!has_mode) { |
| 1869 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; |
| 1870 | } |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 1871 | if (!has_granularity) { |
| 1872 | granularity = 0; |
| 1873 | } |
Paolo Bonzini | 08e4ed6 | 2013-01-22 09:03:13 +0100 | [diff] [blame] | 1874 | if (!has_buf_size) { |
| 1875 | buf_size = DEFAULT_MIRROR_BUF_SIZE; |
| 1876 | } |
| 1877 | |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 1878 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
| 1879 | error_set(errp, QERR_INVALID_PARAMETER, device); |
| 1880 | return; |
| 1881 | } |
| 1882 | if (granularity & (granularity - 1)) { |
| 1883 | error_set(errp, QERR_INVALID_PARAMETER, device); |
| 1884 | return; |
| 1885 | } |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1886 | |
| 1887 | bs = bdrv_find(device); |
| 1888 | if (!bs) { |
| 1889 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
| 1893 | if (!bdrv_is_inserted(bs)) { |
| 1894 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
| 1895 | return; |
| 1896 | } |
| 1897 | |
| 1898 | if (!has_format) { |
| 1899 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; |
| 1900 | } |
| 1901 | if (format) { |
| 1902 | drv = bdrv_find_format(format); |
| 1903 | if (!drv) { |
| 1904 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
| 1905 | return; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | if (bdrv_in_use(bs)) { |
| 1910 | error_set(errp, QERR_DEVICE_IN_USE, device); |
| 1911 | return; |
| 1912 | } |
| 1913 | |
| 1914 | flags = bs->open_flags | BDRV_O_RDWR; |
| 1915 | source = bs->backing_hd; |
| 1916 | if (!source && sync == MIRROR_SYNC_MODE_TOP) { |
| 1917 | sync = MIRROR_SYNC_MODE_FULL; |
| 1918 | } |
| 1919 | |
Stefan Hajnoczi | ac3c5d8 | 2013-06-24 17:13:13 +0200 | [diff] [blame] | 1920 | size = bdrv_getlength(bs); |
| 1921 | if (size < 0) { |
| 1922 | error_setg_errno(errp, -size, "bdrv_getlength failed"); |
| 1923 | return; |
| 1924 | } |
| 1925 | |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1926 | if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) { |
| 1927 | /* create new image w/o backing file */ |
| 1928 | assert(format && drv); |
Luiz Capitulino | cf8f242 | 2012-11-30 10:52:08 -0200 | [diff] [blame] | 1929 | bdrv_img_create(target, format, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1930 | NULL, NULL, NULL, size, flags, &local_err, false); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1931 | } else { |
| 1932 | switch (mode) { |
| 1933 | case NEW_IMAGE_MODE_EXISTING: |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1934 | break; |
| 1935 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: |
| 1936 | /* create new image with backing file */ |
Luiz Capitulino | cf8f242 | 2012-11-30 10:52:08 -0200 | [diff] [blame] | 1937 | bdrv_img_create(target, format, |
| 1938 | source->filename, |
| 1939 | source->drv->format_name, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1940 | NULL, size, flags, &local_err, false); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1941 | break; |
| 1942 | default: |
| 1943 | abort(); |
| 1944 | } |
| 1945 | } |
| 1946 | |
Luiz Capitulino | cf8f242 | 2012-11-30 10:52:08 -0200 | [diff] [blame] | 1947 | if (error_is_set(&local_err)) { |
| 1948 | error_propagate(errp, local_err); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1949 | return; |
| 1950 | } |
| 1951 | |
Paolo Bonzini | b812f67 | 2013-01-21 17:09:43 +0100 | [diff] [blame] | 1952 | /* Mirroring takes care of copy-on-write using the source's backing |
| 1953 | * file. |
| 1954 | */ |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1955 | target_bs = bdrv_new(""); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1956 | ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv, |
| 1957 | &local_err); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1958 | if (ret < 0) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1959 | bdrv_unref(target_bs); |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 1960 | error_propagate(errp, local_err); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1961 | return; |
| 1962 | } |
| 1963 | |
Paolo Bonzini | 08e4ed6 | 2013-01-22 09:03:13 +0100 | [diff] [blame] | 1964 | mirror_start(bs, target_bs, speed, granularity, buf_size, sync, |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 1965 | on_source_error, on_target_error, |
Paolo Bonzini | b952b55 | 2012-10-18 16:49:28 +0200 | [diff] [blame] | 1966 | block_job_cb, bs, &local_err); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1967 | if (local_err != NULL) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1968 | bdrv_unref(target_bs); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1969 | error_propagate(errp, local_err); |
| 1970 | return; |
| 1971 | } |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 1972 | } |
| 1973 | |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 1974 | static BlockJob *find_block_job(const char *device) |
| 1975 | { |
| 1976 | BlockDriverState *bs; |
| 1977 | |
| 1978 | bs = bdrv_find(device); |
| 1979 | if (!bs || !bs->job) { |
| 1980 | return NULL; |
| 1981 | } |
| 1982 | return bs->job; |
| 1983 | } |
| 1984 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 1985 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 1986 | { |
| 1987 | BlockJob *job = find_block_job(device); |
| 1988 | |
| 1989 | if (!job) { |
Paolo Bonzini | 7ef1507 | 2012-09-28 17:22:44 +0200 | [diff] [blame] | 1990 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 1991 | return; |
| 1992 | } |
| 1993 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 1994 | block_job_set_speed(job, speed, errp); |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 1995 | } |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 1996 | |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 1997 | void qmp_block_job_cancel(const char *device, |
| 1998 | bool has_force, bool force, Error **errp) |
| 1999 | { |
| 2000 | BlockJob *job = find_block_job(device); |
| 2001 | |
| 2002 | if (!has_force) { |
| 2003 | force = false; |
| 2004 | } |
| 2005 | |
| 2006 | if (!job) { |
| 2007 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
| 2008 | return; |
| 2009 | } |
| 2010 | if (job->paused && !force) { |
| 2011 | error_set(errp, QERR_BLOCK_JOB_PAUSED, device); |
| 2012 | return; |
| 2013 | } |
| 2014 | |
| 2015 | trace_qmp_block_job_cancel(job); |
| 2016 | block_job_cancel(job); |
| 2017 | } |
| 2018 | |
| 2019 | void qmp_block_job_pause(const char *device, Error **errp) |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 2020 | { |
| 2021 | BlockJob *job = find_block_job(device); |
| 2022 | |
| 2023 | if (!job) { |
Paolo Bonzini | 7ef1507 | 2012-09-28 17:22:44 +0200 | [diff] [blame] | 2024 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 2027 | |
| 2028 | trace_qmp_block_job_pause(job); |
| 2029 | block_job_pause(job); |
| 2030 | } |
| 2031 | |
| 2032 | void qmp_block_job_resume(const char *device, Error **errp) |
| 2033 | { |
| 2034 | BlockJob *job = find_block_job(device); |
| 2035 | |
| 2036 | if (!job) { |
| 2037 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
Paolo Bonzini | 8acc72a | 2012-09-28 17:22:50 +0200 | [diff] [blame] | 2038 | return; |
| 2039 | } |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 2040 | |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 2041 | trace_qmp_block_job_resume(job); |
| 2042 | block_job_resume(job); |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 2043 | } |
Stefan Hajnoczi | fb5458c | 2012-01-18 14:40:49 +0000 | [diff] [blame] | 2044 | |
Paolo Bonzini | aeae883 | 2012-10-18 16:49:21 +0200 | [diff] [blame] | 2045 | void qmp_block_job_complete(const char *device, Error **errp) |
| 2046 | { |
| 2047 | BlockJob *job = find_block_job(device); |
| 2048 | |
| 2049 | if (!job) { |
| 2050 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
| 2051 | return; |
| 2052 | } |
| 2053 | |
| 2054 | trace_qmp_block_job_complete(job); |
| 2055 | block_job_complete(job, errp); |
| 2056 | } |
| 2057 | |
Stefan Hajnoczi | fb5458c | 2012-01-18 14:40:49 +0000 | [diff] [blame] | 2058 | static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs) |
| 2059 | { |
| 2060 | BlockJobInfoList **prev = opaque; |
| 2061 | BlockJob *job = bs->job; |
| 2062 | |
| 2063 | if (job) { |
Paolo Bonzini | 30e628b | 2012-09-28 17:22:48 +0200 | [diff] [blame] | 2064 | BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); |
| 2065 | elem->value = block_job_query(bs->job); |
Stefan Hajnoczi | fb5458c | 2012-01-18 14:40:49 +0000 | [diff] [blame] | 2066 | (*prev)->next = elem; |
| 2067 | *prev = elem; |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
| 2072 | { |
| 2073 | /* Dummy is a fake list element for holding the head pointer */ |
| 2074 | BlockJobInfoList dummy = {}; |
| 2075 | BlockJobInfoList *prev = &dummy; |
| 2076 | bdrv_iterate(do_qmp_query_block_jobs_one, &prev); |
| 2077 | return dummy.next; |
| 2078 | } |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2079 | |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 2080 | QemuOptsList qemu_common_drive_opts = { |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2081 | .name = "drive", |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 2082 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2083 | .desc = { |
| 2084 | { |
| 2085 | .name = "bus", |
| 2086 | .type = QEMU_OPT_NUMBER, |
| 2087 | .help = "bus number", |
| 2088 | },{ |
| 2089 | .name = "unit", |
| 2090 | .type = QEMU_OPT_NUMBER, |
| 2091 | .help = "unit number (i.e. lun for scsi)", |
| 2092 | },{ |
| 2093 | .name = "if", |
| 2094 | .type = QEMU_OPT_STRING, |
| 2095 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", |
| 2096 | },{ |
| 2097 | .name = "index", |
| 2098 | .type = QEMU_OPT_NUMBER, |
| 2099 | .help = "index number", |
| 2100 | },{ |
| 2101 | .name = "cyls", |
| 2102 | .type = QEMU_OPT_NUMBER, |
| 2103 | .help = "number of cylinders (ide disk geometry)", |
| 2104 | },{ |
| 2105 | .name = "heads", |
| 2106 | .type = QEMU_OPT_NUMBER, |
| 2107 | .help = "number of heads (ide disk geometry)", |
| 2108 | },{ |
| 2109 | .name = "secs", |
| 2110 | .type = QEMU_OPT_NUMBER, |
| 2111 | .help = "number of sectors (ide disk geometry)", |
| 2112 | },{ |
| 2113 | .name = "trans", |
| 2114 | .type = QEMU_OPT_STRING, |
| 2115 | .help = "chs translation (auto, lba. none)", |
| 2116 | },{ |
| 2117 | .name = "media", |
| 2118 | .type = QEMU_OPT_STRING, |
| 2119 | .help = "media type (disk, cdrom)", |
| 2120 | },{ |
| 2121 | .name = "snapshot", |
| 2122 | .type = QEMU_OPT_BOOL, |
| 2123 | .help = "enable/disable snapshot mode", |
| 2124 | },{ |
| 2125 | .name = "file", |
| 2126 | .type = QEMU_OPT_STRING, |
| 2127 | .help = "disk image", |
| 2128 | },{ |
Paolo Bonzini | a9384af | 2013-02-08 14:06:12 +0100 | [diff] [blame] | 2129 | .name = "discard", |
| 2130 | .type = QEMU_OPT_STRING, |
| 2131 | .help = "discard operation (ignore/off, unmap/on)", |
| 2132 | },{ |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 2133 | .name = "cache.writeback", |
| 2134 | .type = QEMU_OPT_BOOL, |
| 2135 | .help = "enables writeback mode for any caches", |
| 2136 | },{ |
| 2137 | .name = "cache.direct", |
| 2138 | .type = QEMU_OPT_BOOL, |
| 2139 | .help = "enables use of O_DIRECT (bypass the host page cache)", |
| 2140 | },{ |
| 2141 | .name = "cache.no-flush", |
| 2142 | .type = QEMU_OPT_BOOL, |
| 2143 | .help = "ignore any flush requests for the device", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2144 | },{ |
| 2145 | .name = "aio", |
| 2146 | .type = QEMU_OPT_STRING, |
| 2147 | .help = "host AIO implementation (threads, native)", |
| 2148 | },{ |
| 2149 | .name = "format", |
| 2150 | .type = QEMU_OPT_STRING, |
| 2151 | .help = "disk format (raw, qcow2, ...)", |
| 2152 | },{ |
| 2153 | .name = "serial", |
| 2154 | .type = QEMU_OPT_STRING, |
| 2155 | .help = "disk serial number", |
| 2156 | },{ |
| 2157 | .name = "rerror", |
| 2158 | .type = QEMU_OPT_STRING, |
| 2159 | .help = "read error action", |
| 2160 | },{ |
| 2161 | .name = "werror", |
| 2162 | .type = QEMU_OPT_STRING, |
| 2163 | .help = "write error action", |
| 2164 | },{ |
| 2165 | .name = "addr", |
| 2166 | .type = QEMU_OPT_STRING, |
| 2167 | .help = "pci address (virtio only)", |
| 2168 | },{ |
Kevin Wolf | 0f227a9 | 2013-07-19 20:07:29 +0200 | [diff] [blame] | 2169 | .name = "read-only", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2170 | .type = QEMU_OPT_BOOL, |
| 2171 | .help = "open drive file as read-only", |
| 2172 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 2173 | .name = "throttling.iops-total", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2174 | .type = QEMU_OPT_NUMBER, |
| 2175 | .help = "limit total I/O operations per second", |
| 2176 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 2177 | .name = "throttling.iops-read", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2178 | .type = QEMU_OPT_NUMBER, |
| 2179 | .help = "limit read operations per second", |
| 2180 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 2181 | .name = "throttling.iops-write", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2182 | .type = QEMU_OPT_NUMBER, |
| 2183 | .help = "limit write operations per second", |
| 2184 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 2185 | .name = "throttling.bps-total", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2186 | .type = QEMU_OPT_NUMBER, |
| 2187 | .help = "limit total bytes per second", |
| 2188 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 2189 | .name = "throttling.bps-read", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2190 | .type = QEMU_OPT_NUMBER, |
| 2191 | .help = "limit read bytes per second", |
| 2192 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 2193 | .name = "throttling.bps-write", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2194 | .type = QEMU_OPT_NUMBER, |
| 2195 | .help = "limit write bytes per second", |
| 2196 | },{ |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 2197 | .name = "throttling.iops-total-max", |
| 2198 | .type = QEMU_OPT_NUMBER, |
| 2199 | .help = "I/O operations burst", |
| 2200 | },{ |
| 2201 | .name = "throttling.iops-read-max", |
| 2202 | .type = QEMU_OPT_NUMBER, |
| 2203 | .help = "I/O operations read burst", |
| 2204 | },{ |
| 2205 | .name = "throttling.iops-write-max", |
| 2206 | .type = QEMU_OPT_NUMBER, |
| 2207 | .help = "I/O operations write burst", |
| 2208 | },{ |
| 2209 | .name = "throttling.bps-total-max", |
| 2210 | .type = QEMU_OPT_NUMBER, |
| 2211 | .help = "total bytes burst", |
| 2212 | },{ |
| 2213 | .name = "throttling.bps-read-max", |
| 2214 | .type = QEMU_OPT_NUMBER, |
| 2215 | .help = "total bytes read burst", |
| 2216 | },{ |
| 2217 | .name = "throttling.bps-write-max", |
| 2218 | .type = QEMU_OPT_NUMBER, |
| 2219 | .help = "total bytes write burst", |
| 2220 | },{ |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 2221 | .name = "throttling.iops-size", |
| 2222 | .type = QEMU_OPT_NUMBER, |
| 2223 | .help = "when limiting by iops max size of an I/O in bytes", |
| 2224 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2225 | .name = "copy-on-read", |
| 2226 | .type = QEMU_OPT_BOOL, |
| 2227 | .help = "copy read data from backing file into image file", |
| 2228 | },{ |
| 2229 | .name = "boot", |
| 2230 | .type = QEMU_OPT_BOOL, |
| 2231 | .help = "(deprecated, ignored)", |
| 2232 | }, |
| 2233 | { /* end of list */ } |
| 2234 | }, |
| 2235 | }; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 2236 | |
| 2237 | QemuOptsList qemu_drive_opts = { |
| 2238 | .name = "drive", |
| 2239 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), |
| 2240 | .desc = { |
Kevin Wolf | 492fdc6 | 2013-06-19 13:44:17 +0200 | [diff] [blame] | 2241 | /* |
| 2242 | * no elements => accept any params |
| 2243 | * validation will happen later |
| 2244 | */ |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 2245 | { /* end of list */ } |
| 2246 | }, |
| 2247 | }; |