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