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