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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 33 | #include "qemu/osdep.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 34 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/blockdev.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 36 | #include "hw/block/block.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 37 | #include "block/blockjob.h" |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 38 | #include "block/throttle-groups.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 39 | #include "monitor/monitor.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 40 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 41 | #include "qemu/option.h" |
| 42 | #include "qemu/config-file.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 43 | #include "qapi/qmp/types.h" |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 44 | #include "qapi-visit.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 45 | #include "qapi/qmp/qerror.h" |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 46 | #include "qapi/qmp-output-visitor.h" |
Peter Lieven | 9e7dac7 | 2014-08-13 19:20:17 +0200 | [diff] [blame] | 47 | #include "qapi/util.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 48 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 49 | #include "block/block_int.h" |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 50 | #include "qmp-commands.h" |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 51 | #include "trace.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 52 | #include "sysemu/arch_init.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 53 | #include "qemu/cutils.h" |
| 54 | #include "qemu/help_option.h" |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 55 | |
Max Reitz | 9c4218e | 2016-01-29 16:36:12 +0100 | [diff] [blame] | 56 | static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = |
| 57 | QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); |
| 58 | |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 59 | static int do_open_tray(const char *device, bool force, Error **errp); |
| 60 | |
Markus Armbruster | 1960966 | 2011-01-28 11:21:39 +0100 | [diff] [blame] | 61 | static const char *const if_name[IF_COUNT] = { |
| 62 | [IF_NONE] = "none", |
| 63 | [IF_IDE] = "ide", |
| 64 | [IF_SCSI] = "scsi", |
| 65 | [IF_FLOPPY] = "floppy", |
| 66 | [IF_PFLASH] = "pflash", |
| 67 | [IF_MTD] = "mtd", |
| 68 | [IF_SD] = "sd", |
| 69 | [IF_VIRTIO] = "virtio", |
| 70 | [IF_XEN] = "xen", |
| 71 | }; |
| 72 | |
John Snow | 21dff8c | 2014-10-01 14:19:25 -0400 | [diff] [blame] | 73 | static int if_max_devs[IF_COUNT] = { |
Markus Armbruster | 27d6bf4 | 2011-01-28 11:21:40 +0100 | [diff] [blame] | 74 | /* |
| 75 | * Do not change these numbers! They govern how drive option |
| 76 | * index maps to unit and bus. That mapping is ABI. |
| 77 | * |
Wei Jiangang | 547cb15 | 2016-04-26 18:12:43 +0800 | [diff] [blame] | 78 | * All controllers used to implement if=T drives need to support |
Markus Armbruster | 27d6bf4 | 2011-01-28 11:21:40 +0100 | [diff] [blame] | 79 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. |
| 80 | * Otherwise, some index values map to "impossible" bus, unit |
| 81 | * values. |
| 82 | * |
| 83 | * For instance, if you change [IF_SCSI] to 255, -drive |
| 84 | * if=scsi,index=12 no longer means bus=1,unit=5, but |
| 85 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), |
| 86 | * the drive can't be set up. Regression. |
| 87 | */ |
| 88 | [IF_IDE] = 2, |
| 89 | [IF_SCSI] = 7, |
Markus Armbruster | 1960966 | 2011-01-28 11:21:39 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
John Snow | 21dff8c | 2014-10-01 14:19:25 -0400 | [diff] [blame] | 92 | /** |
| 93 | * Boards may call this to offer board-by-board overrides |
| 94 | * of the default, global values. |
| 95 | */ |
| 96 | void override_max_devs(BlockInterfaceType type, int max_devs) |
| 97 | { |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 98 | BlockBackend *blk; |
John Snow | 21dff8c | 2014-10-01 14:19:25 -0400 | [diff] [blame] | 99 | DriveInfo *dinfo; |
| 100 | |
| 101 | if (max_devs <= 0) { |
| 102 | return; |
| 103 | } |
| 104 | |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 105 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
| 106 | dinfo = blk_legacy_dinfo(blk); |
John Snow | 21dff8c | 2014-10-01 14:19:25 -0400 | [diff] [blame] | 107 | if (dinfo->type == type) { |
| 108 | fprintf(stderr, "Cannot override units-per-bus property of" |
| 109 | " the %s interface, because a drive of that type has" |
| 110 | " already been added.\n", if_name[type]); |
| 111 | g_assert_not_reached(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if_max_devs[type] = max_devs; |
| 116 | } |
| 117 | |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 118 | /* |
| 119 | * We automatically delete the drive when a device using it gets |
| 120 | * unplugged. Questionable feature, but we can't just drop it. |
| 121 | * Device models call blockdev_mark_auto_del() to schedule the |
| 122 | * automatic deletion, and generic qdev code calls blockdev_auto_del() |
| 123 | * when deletion is actually safe. |
| 124 | */ |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 125 | void blockdev_mark_auto_del(BlockBackend *blk) |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 126 | { |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 127 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 128 | BlockDriverState *bs = blk_bs(blk); |
Stefan Hajnoczi | 91fddb0 | 2014-10-21 12:03:52 +0100 | [diff] [blame] | 129 | AioContext *aio_context; |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 130 | |
Markus Armbruster | 26f8b3a | 2014-10-07 13:59:22 +0200 | [diff] [blame] | 131 | if (!dinfo) { |
Kevin Wolf | 2d246f0 | 2013-09-18 15:14:47 +0200 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 135 | if (bs) { |
| 136 | aio_context = bdrv_get_aio_context(bs); |
| 137 | aio_context_acquire(aio_context); |
Stefan Hajnoczi | 91fddb0 | 2014-10-21 12:03:52 +0100 | [diff] [blame] | 138 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 139 | if (bs->job) { |
| 140 | block_job_cancel(bs->job); |
| 141 | } |
| 142 | |
| 143 | aio_context_release(aio_context); |
Paolo Bonzini | 12bde0e | 2012-03-30 13:17:10 +0200 | [diff] [blame] | 144 | } |
Stefan Hajnoczi | 91fddb0 | 2014-10-21 12:03:52 +0100 | [diff] [blame] | 145 | |
Markus Armbruster | 26f8b3a | 2014-10-07 13:59:22 +0200 | [diff] [blame] | 146 | dinfo->auto_del = 1; |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 149 | void blockdev_auto_del(BlockBackend *blk) |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 150 | { |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 151 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 152 | |
Ryan Harper | 0fc0f1f | 2010-12-08 10:05:00 -0600 | [diff] [blame] | 153 | if (dinfo && dinfo->auto_del) { |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 154 | monitor_remove_blk(blk); |
Markus Armbruster | b9fe8a7 | 2014-10-07 13:59:09 +0200 | [diff] [blame] | 155 | blk_unref(blk); |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
John Snow | d8f94e1 | 2014-10-01 14:19:27 -0400 | [diff] [blame] | 159 | /** |
| 160 | * Returns the current mapping of how many units per bus |
| 161 | * a particular interface can support. |
| 162 | * |
| 163 | * A positive integer indicates n units per bus. |
| 164 | * 0 implies the mapping has not been established. |
| 165 | * -1 indicates an invalid BlockInterfaceType was given. |
| 166 | */ |
| 167 | int drive_get_max_devs(BlockInterfaceType type) |
| 168 | { |
| 169 | if (type >= IF_IDE && type < IF_COUNT) { |
| 170 | return if_max_devs[type]; |
| 171 | } |
| 172 | |
| 173 | return -1; |
| 174 | } |
| 175 | |
Markus Armbruster | 505a7fb | 2011-01-28 11:21:43 +0100 | [diff] [blame] | 176 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
| 177 | { |
| 178 | int max_devs = if_max_devs[type]; |
| 179 | return max_devs ? index / max_devs : 0; |
| 180 | } |
| 181 | |
| 182 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) |
| 183 | { |
| 184 | int max_devs = if_max_devs[type]; |
| 185 | return max_devs ? index % max_devs : index; |
| 186 | } |
| 187 | |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 188 | QemuOpts *drive_def(const char *optstr) |
| 189 | { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 190 | return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, |
Markus Armbruster | 5645b0f | 2011-01-31 11:50:09 +0100 | [diff] [blame] | 194 | const char *optstr) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 195 | { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 196 | QemuOpts *opts; |
| 197 | |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 198 | opts = drive_def(optstr); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 199 | if (!opts) { |
| 200 | return NULL; |
| 201 | } |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 202 | if (type != IF_DEFAULT) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 203 | qemu_opt_set(opts, "if", if_name[type], &error_abort); |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 204 | } |
| 205 | if (index >= 0) { |
Markus Armbruster | a8b18f8 | 2015-02-13 15:50:43 +0100 | [diff] [blame] | 206 | qemu_opt_set_number(opts, "index", index, &error_abort); |
Markus Armbruster | 2292dda | 2011-01-28 11:21:41 +0100 | [diff] [blame] | 207 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 208 | if (file) |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 209 | qemu_opt_set(opts, "file", file, &error_abort); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 210 | return opts; |
| 211 | } |
| 212 | |
| 213 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
| 214 | { |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 215 | BlockBackend *blk; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 216 | DriveInfo *dinfo; |
| 217 | |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 218 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
| 219 | dinfo = blk_legacy_dinfo(blk); |
| 220 | if (dinfo && dinfo->type == type |
| 221 | && dinfo->bus == bus && dinfo->unit == unit) { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 222 | return dinfo; |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 223 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
John Snow | a66c9dc | 2014-10-01 14:19:24 -0400 | [diff] [blame] | 229 | bool drive_check_orphaned(void) |
| 230 | { |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 231 | BlockBackend *blk; |
John Snow | a66c9dc | 2014-10-01 14:19:24 -0400 | [diff] [blame] | 232 | DriveInfo *dinfo; |
| 233 | bool rs = false; |
| 234 | |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 235 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
| 236 | dinfo = blk_legacy_dinfo(blk); |
John Snow | a66c9dc | 2014-10-01 14:19:24 -0400 | [diff] [blame] | 237 | /* If dinfo->bdrv->dev is NULL, it has no device attached. */ |
| 238 | /* Unless this is a default drive, this may be an oversight. */ |
Markus Armbruster | a7f53e2 | 2014-10-07 13:59:25 +0200 | [diff] [blame] | 239 | if (!blk_get_attached_dev(blk) && !dinfo->is_default && |
John Snow | a66c9dc | 2014-10-01 14:19:24 -0400 | [diff] [blame] | 240 | dinfo->type != IF_NONE) { |
| 241 | fprintf(stderr, "Warning: Orphaned drive without device: " |
| 242 | "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 243 | blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", |
| 244 | if_name[dinfo->type], dinfo->bus, dinfo->unit); |
John Snow | a66c9dc | 2014-10-01 14:19:24 -0400 | [diff] [blame] | 245 | rs = true; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return rs; |
| 250 | } |
| 251 | |
Markus Armbruster | f1bd51a | 2011-01-28 11:21:44 +0100 | [diff] [blame] | 252 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
| 253 | { |
| 254 | return drive_get(type, |
| 255 | drive_index_to_bus_id(type, index), |
| 256 | drive_index_to_unit_id(type, index)); |
| 257 | } |
| 258 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 259 | int drive_get_max_bus(BlockInterfaceType type) |
| 260 | { |
| 261 | int max_bus; |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 262 | BlockBackend *blk; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 263 | DriveInfo *dinfo; |
| 264 | |
| 265 | max_bus = -1; |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 266 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
| 267 | dinfo = blk_legacy_dinfo(blk); |
| 268 | if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 269 | max_bus = dinfo->bus; |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 270 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 271 | } |
| 272 | return max_bus; |
| 273 | } |
| 274 | |
Markus Armbruster | 1383997 | 2011-01-28 11:21:37 +0100 | [diff] [blame] | 275 | /* Get a block device. This should only be used for single-drive devices |
| 276 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the |
| 277 | appropriate bus. */ |
| 278 | DriveInfo *drive_get_next(BlockInterfaceType type) |
| 279 | { |
| 280 | static int next_block_unit[IF_COUNT]; |
| 281 | |
| 282 | return drive_get(type, 0, next_block_unit[type]++); |
| 283 | } |
| 284 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 285 | static void bdrv_format_print(void *opaque, const char *name) |
| 286 | { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 287 | error_printf(" %s", name); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 290 | typedef struct { |
| 291 | QEMUBH *bh; |
Fam Zheng | fa510eb | 2013-08-23 09:14:51 +0800 | [diff] [blame] | 292 | BlockDriverState *bs; |
| 293 | } BDRVPutRefBH; |
Stefan Hajnoczi | aa398a5 | 2012-01-18 14:40:50 +0000 | [diff] [blame] | 294 | |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 295 | 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] | 296 | { |
| 297 | if (!strcmp(buf, "ignore")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 298 | return BLOCKDEV_ON_ERROR_IGNORE; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 299 | } else if (!is_read && !strcmp(buf, "enospc")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 300 | return BLOCKDEV_ON_ERROR_ENOSPC; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 301 | } else if (!strcmp(buf, "stop")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 302 | return BLOCKDEV_ON_ERROR_STOP; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 303 | } else if (!strcmp(buf, "report")) { |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 304 | return BLOCKDEV_ON_ERROR_REPORT; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 305 | } else { |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 306 | error_setg(errp, "'%s' invalid %s error action", |
| 307 | buf, is_read ? "read" : "write"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 308 | return -1; |
| 309 | } |
| 310 | } |
| 311 | |
Alberto Garcia | 40119ef | 2015-11-16 11:28:38 +0200 | [diff] [blame] | 312 | static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, |
| 313 | Error **errp) |
| 314 | { |
| 315 | const QListEntry *entry; |
| 316 | for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { |
| 317 | switch (qobject_type(entry->value)) { |
| 318 | |
| 319 | case QTYPE_QSTRING: { |
| 320 | unsigned long long length; |
| 321 | const char *str = qstring_get_str(qobject_to_qstring(entry->value)); |
| 322 | if (parse_uint_full(str, &length, 10) == 0 && |
| 323 | length > 0 && length <= UINT_MAX) { |
| 324 | block_acct_add_interval(stats, (unsigned) length); |
| 325 | } else { |
| 326 | error_setg(errp, "Invalid interval length: %s", str); |
| 327 | return false; |
| 328 | } |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | case QTYPE_QINT: { |
| 333 | int64_t length = qint_get_int(qobject_to_qint(entry->value)); |
| 334 | if (length > 0 && length <= UINT_MAX) { |
| 335 | block_acct_add_interval(stats, (unsigned) length); |
| 336 | } else { |
| 337 | error_setg(errp, "Invalid interval length: %" PRId64, length); |
| 338 | return false; |
| 339 | } |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | default: |
| 344 | error_setg(errp, "The specification of stats-intervals is invalid"); |
| 345 | return false; |
| 346 | } |
| 347 | } |
| 348 | return true; |
| 349 | } |
| 350 | |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 351 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
| 352 | |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 353 | /* All parameters but @opts are optional and may be set to NULL. */ |
| 354 | static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, |
| 355 | const char **throttling_group, ThrottleConfig *throttle_cfg, |
| 356 | BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) |
| 357 | { |
| 358 | const char *discard; |
| 359 | Error *local_error = NULL; |
| 360 | const char *aio; |
| 361 | |
| 362 | if (bdrv_flags) { |
| 363 | if (!qemu_opt_get_bool(opts, "read-only", false)) { |
| 364 | *bdrv_flags |= BDRV_O_RDWR; |
| 365 | } |
| 366 | if (qemu_opt_get_bool(opts, "copy-on-read", false)) { |
| 367 | *bdrv_flags |= BDRV_O_COPY_ON_READ; |
| 368 | } |
| 369 | |
| 370 | if ((discard = qemu_opt_get(opts, "discard")) != NULL) { |
| 371 | if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { |
| 372 | error_setg(errp, "Invalid discard option"); |
| 373 | return; |
| 374 | } |
| 375 | } |
| 376 | |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 377 | if ((aio = qemu_opt_get(opts, "aio")) != NULL) { |
| 378 | if (!strcmp(aio, "native")) { |
| 379 | *bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 380 | } else if (!strcmp(aio, "threads")) { |
| 381 | /* this is the default */ |
| 382 | } else { |
| 383 | error_setg(errp, "invalid aio option"); |
| 384 | return; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* disk I/O throttling */ |
| 390 | if (throttling_group) { |
| 391 | *throttling_group = qemu_opt_get(opts, "throttling.group"); |
| 392 | } |
| 393 | |
| 394 | if (throttle_cfg) { |
Alberto Garcia | 1588ab5 | 2016-02-18 12:27:00 +0200 | [diff] [blame] | 395 | throttle_config_init(throttle_cfg); |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 396 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = |
| 397 | qemu_opt_get_number(opts, "throttling.bps-total", 0); |
| 398 | throttle_cfg->buckets[THROTTLE_BPS_READ].avg = |
| 399 | qemu_opt_get_number(opts, "throttling.bps-read", 0); |
| 400 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = |
| 401 | qemu_opt_get_number(opts, "throttling.bps-write", 0); |
| 402 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = |
| 403 | qemu_opt_get_number(opts, "throttling.iops-total", 0); |
| 404 | throttle_cfg->buckets[THROTTLE_OPS_READ].avg = |
| 405 | qemu_opt_get_number(opts, "throttling.iops-read", 0); |
| 406 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = |
| 407 | qemu_opt_get_number(opts, "throttling.iops-write", 0); |
| 408 | |
| 409 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = |
| 410 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); |
| 411 | throttle_cfg->buckets[THROTTLE_BPS_READ].max = |
| 412 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); |
| 413 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = |
| 414 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); |
| 415 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = |
| 416 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); |
| 417 | throttle_cfg->buckets[THROTTLE_OPS_READ].max = |
| 418 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); |
| 419 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = |
| 420 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); |
| 421 | |
Alberto Garcia | 8a0fc18 | 2016-02-18 12:27:02 +0200 | [diff] [blame] | 422 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = |
| 423 | qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); |
| 424 | throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = |
| 425 | qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); |
| 426 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = |
| 427 | qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); |
| 428 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = |
| 429 | qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); |
| 430 | throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = |
| 431 | qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); |
| 432 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = |
| 433 | qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); |
| 434 | |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 435 | throttle_cfg->op_size = |
| 436 | qemu_opt_get_number(opts, "throttling.iops-size", 0); |
| 437 | |
Alberto Garcia | d585108 | 2016-02-18 12:26:59 +0200 | [diff] [blame] | 438 | if (!throttle_is_valid(throttle_cfg, errp)) { |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if (detect_zeroes) { |
| 444 | *detect_zeroes = |
| 445 | qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, |
| 446 | qemu_opt_get(opts, "detect-zeroes"), |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 447 | BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 448 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
| 449 | &local_error); |
| 450 | if (local_error) { |
| 451 | error_propagate(errp, local_error); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | if (bdrv_flags && |
| 456 | *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && |
| 457 | !(*bdrv_flags & BDRV_O_UNMAP)) |
| 458 | { |
| 459 | error_setg(errp, "setting detect-zeroes to unmap is not allowed " |
| 460 | "without setting discard operation to unmap"); |
| 461 | return; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
Kevin Wolf | f298d07 | 2013-09-10 12:01:20 +0200 | [diff] [blame] | 466 | /* Takes the ownership of bs_opts */ |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 467 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
| 468 | Error **errp) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 469 | { |
| 470 | const char *buf; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 471 | int bdrv_flags = 0; |
| 472 | int on_read_error, on_write_error; |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 473 | bool account_invalid, account_failed; |
Kevin Wolf | e4b24b4 | 2016-03-15 15:39:42 +0100 | [diff] [blame] | 474 | bool writethrough; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 475 | BlockBackend *blk; |
Markus Armbruster | a0f1eab | 2014-09-12 21:26:21 +0200 | [diff] [blame] | 476 | BlockDriverState *bs; |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 477 | ThrottleConfig cfg; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 478 | int snapshot = 0; |
Stefan Hajnoczi | c546194 | 2013-02-13 16:53:42 +0100 | [diff] [blame] | 479 | Error *error = NULL; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 480 | QemuOpts *opts; |
Alberto Garcia | 40119ef | 2015-11-16 11:28:38 +0200 | [diff] [blame] | 481 | QDict *interval_dict = NULL; |
| 482 | QList *interval_list = NULL; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 483 | const char *id; |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 484 | BlockdevDetectZeroesOptions detect_zeroes = |
| 485 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; |
| 486 | const char *throttling_group = NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 487 | |
Kevin Wolf | f298d07 | 2013-09-10 12:01:20 +0200 | [diff] [blame] | 488 | /* Check common options by copying from bs_opts to opts, all other options |
| 489 | * stay in bs_opts for processing by bdrv_open(). */ |
| 490 | id = qdict_get_try_str(bs_opts, "id"); |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 491 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 492 | if (error) { |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 493 | error_propagate(errp, error); |
Markus Armbruster | 6376f95 | 2014-05-28 11:17:01 +0200 | [diff] [blame] | 494 | goto err_no_opts; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 497 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 498 | if (error) { |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 499 | error_propagate(errp, error); |
Stefan Hajnoczi | ec9c10d | 2013-10-30 14:54:30 +0100 | [diff] [blame] | 500 | goto early_err; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (id) { |
| 504 | qdict_del(bs_opts, "id"); |
| 505 | } |
| 506 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 507 | /* extract parameters */ |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 508 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 509 | |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 510 | account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); |
| 511 | account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); |
| 512 | |
Kevin Wolf | e4b24b4 | 2016-03-15 15:39:42 +0100 | [diff] [blame] | 513 | writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); |
| 514 | |
Alberto Garcia | ff356ee | 2016-07-08 17:03:00 +0300 | [diff] [blame] | 515 | id = qemu_opts_id(opts); |
| 516 | |
Alberto Garcia | 40119ef | 2015-11-16 11:28:38 +0200 | [diff] [blame] | 517 | qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); |
| 518 | qdict_array_split(interval_dict, &interval_list); |
| 519 | |
| 520 | if (qdict_size(interval_dict) != 0) { |
| 521 | error_setg(errp, "Invalid option stats-intervals.%s", |
| 522 | qdict_first(interval_dict)->key); |
| 523 | goto early_err; |
| 524 | } |
Alberto Garcia | 2be5506 | 2015-10-28 17:33:07 +0200 | [diff] [blame] | 525 | |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 526 | extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, |
| 527 | &detect_zeroes, &error); |
| 528 | if (error) { |
| 529 | error_propagate(errp, error); |
| 530 | goto early_err; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 531 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 532 | |
| 533 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 534 | if (is_help_option(buf)) { |
| 535 | error_printf("Supported formats:"); |
| 536 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 537 | error_printf("\n"); |
Stefan Hajnoczi | ec9c10d | 2013-10-30 14:54:30 +0100 | [diff] [blame] | 538 | goto early_err; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 539 | } |
Kevin Wolf | 74fe54f | 2013-07-09 11:09:02 +0200 | [diff] [blame] | 540 | |
Max Reitz | e4342ce | 2015-02-05 13:58:14 -0500 | [diff] [blame] | 541 | if (qdict_haskey(bs_opts, "driver")) { |
| 542 | error_setg(errp, "Cannot specify both 'driver' and 'format'"); |
Stefan Hajnoczi | ec9c10d | 2013-10-30 14:54:30 +0100 | [diff] [blame] | 543 | goto early_err; |
Mike Qiu | 6db5f5d | 2013-08-08 10:45:16 -0400 | [diff] [blame] | 544 | } |
Max Reitz | e4342ce | 2015-02-05 13:58:14 -0500 | [diff] [blame] | 545 | qdict_put(bs_opts, "driver", qstring_from_str(buf)); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 548 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 549 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 550 | on_write_error = parse_block_error_action(buf, 0, &error); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 551 | if (error) { |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 552 | error_propagate(errp, error); |
Stefan Hajnoczi | ec9c10d | 2013-10-30 14:54:30 +0100 | [diff] [blame] | 553 | goto early_err; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 557 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 558 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 559 | on_read_error = parse_block_error_action(buf, 1, &error); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 560 | if (error) { |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 561 | error_propagate(errp, error); |
Stefan Hajnoczi | ec9c10d | 2013-10-30 14:54:30 +0100 | [diff] [blame] | 562 | goto early_err; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 566 | if (snapshot) { |
Kevin Wolf | 91a097e | 2015-05-08 17:49:53 +0200 | [diff] [blame] | 567 | bdrv_flags |= BDRV_O_SNAPSHOT; |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Kevin Wolf | 326642b | 2013-07-11 12:52:34 +0200 | [diff] [blame] | 570 | /* init */ |
Kevin Wolf | 39c4ae9 | 2015-11-13 14:45:42 +0100 | [diff] [blame] | 571 | if ((!file || !*file) && !qdict_size(bs_opts)) { |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 572 | BlockBackendRootState *blk_rs; |
| 573 | |
Max Reitz | 109525a | 2016-05-17 16:41:34 +0200 | [diff] [blame] | 574 | blk = blk_new(); |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 575 | blk_rs = blk_get_root_state(blk); |
| 576 | blk_rs->open_flags = bdrv_flags; |
Max Reitz | fbf8175 | 2015-10-19 17:53:31 +0200 | [diff] [blame] | 577 | blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 578 | blk_rs->detect_zeroes = detect_zeroes; |
| 579 | |
Max Reitz | e4342ce | 2015-02-05 13:58:14 -0500 | [diff] [blame] | 580 | QDECREF(bs_opts); |
| 581 | } else { |
| 582 | if (file && !*file) { |
| 583 | file = NULL; |
| 584 | } |
| 585 | |
Kevin Wolf | 91a097e | 2015-05-08 17:49:53 +0200 | [diff] [blame] | 586 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
| 587 | * with other callers) rather than what we want as the real defaults. |
| 588 | * Apply the defaults here instead. */ |
Kevin Wolf | 91a097e | 2015-05-08 17:49:53 +0200 | [diff] [blame] | 589 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
| 590 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); |
Kevin Wolf | 72e775c | 2016-03-15 14:34:37 +0100 | [diff] [blame] | 591 | assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); |
Kevin Wolf | 91a097e | 2015-05-08 17:49:53 +0200 | [diff] [blame] | 592 | |
Kevin Wolf | 12d5ee3 | 2016-02-19 16:48:10 +0100 | [diff] [blame] | 593 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 594 | bdrv_flags |= BDRV_O_INACTIVE; |
| 595 | } |
| 596 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 597 | blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); |
Max Reitz | e4342ce | 2015-02-05 13:58:14 -0500 | [diff] [blame] | 598 | if (!blk) { |
| 599 | goto err_no_bs_opts; |
| 600 | } |
| 601 | bs = blk_bs(blk); |
Max Reitz | e4342ce | 2015-02-05 13:58:14 -0500 | [diff] [blame] | 602 | |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 603 | bs->detect_zeroes = detect_zeroes; |
| 604 | |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 605 | if (bdrv_key_required(bs)) { |
| 606 | autostart = 0; |
| 607 | } |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 608 | |
| 609 | block_acct_init(blk_get_stats(blk), account_invalid, account_failed); |
Alberto Garcia | 2be5506 | 2015-10-28 17:33:07 +0200 | [diff] [blame] | 610 | |
Alberto Garcia | 40119ef | 2015-11-16 11:28:38 +0200 | [diff] [blame] | 611 | if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { |
| 612 | blk_unref(blk); |
| 613 | blk = NULL; |
| 614 | goto err_no_bs_opts; |
Alberto Garcia | 2be5506 | 2015-10-28 17:33:07 +0200 | [diff] [blame] | 615 | } |
Max Reitz | 5ec18f8 | 2015-10-19 17:53:30 +0200 | [diff] [blame] | 616 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 617 | |
Kevin Wolf | 7ca7f0f | 2016-03-22 13:00:08 +0100 | [diff] [blame] | 618 | /* disk I/O throttling */ |
| 619 | if (throttle_enabled(&cfg)) { |
| 620 | if (!throttling_group) { |
Alberto Garcia | ff356ee | 2016-07-08 17:03:00 +0300 | [diff] [blame] | 621 | throttling_group = id; |
Kevin Wolf | 7ca7f0f | 2016-03-22 13:00:08 +0100 | [diff] [blame] | 622 | } |
| 623 | blk_io_limits_enable(blk, throttling_group); |
| 624 | blk_set_io_limits(blk, &cfg); |
| 625 | } |
| 626 | |
Kevin Wolf | e4b24b4 | 2016-03-15 15:39:42 +0100 | [diff] [blame] | 627 | blk_set_enable_write_cache(blk, !writethrough); |
Max Reitz | 373340b | 2015-10-19 17:53:22 +0200 | [diff] [blame] | 628 | blk_set_on_error(blk, on_read_error, on_write_error); |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 629 | |
Alberto Garcia | ff356ee | 2016-07-08 17:03:00 +0300 | [diff] [blame] | 630 | if (!monitor_add_blk(blk, id, errp)) { |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 631 | blk_unref(blk); |
| 632 | blk = NULL; |
| 633 | goto err_no_bs_opts; |
| 634 | } |
| 635 | |
Max Reitz | e4342ce | 2015-02-05 13:58:14 -0500 | [diff] [blame] | 636 | err_no_bs_opts: |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 637 | qemu_opts_del(opts); |
Alberto Garcia | 40119ef | 2015-11-16 11:28:38 +0200 | [diff] [blame] | 638 | QDECREF(interval_dict); |
| 639 | QDECREF(interval_list); |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 640 | return blk; |
Markus Armbruster | a9ae2bf | 2011-02-08 15:12:39 +0100 | [diff] [blame] | 641 | |
Stefan Hajnoczi | ec9c10d | 2013-10-30 14:54:30 +0100 | [diff] [blame] | 642 | early_err: |
Stefan Hajnoczi | ec9c10d | 2013-10-30 14:54:30 +0100 | [diff] [blame] | 643 | qemu_opts_del(opts); |
Alberto Garcia | 40119ef | 2015-11-16 11:28:38 +0200 | [diff] [blame] | 644 | QDECREF(interval_dict); |
| 645 | QDECREF(interval_list); |
Markus Armbruster | 6376f95 | 2014-05-28 11:17:01 +0200 | [diff] [blame] | 646 | err_no_opts: |
| 647 | QDECREF(bs_opts); |
Markus Armbruster | a9ae2bf | 2011-02-08 15:12:39 +0100 | [diff] [blame] | 648 | return NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Max Reitz | bd745e2 | 2015-10-19 17:53:32 +0200 | [diff] [blame] | 651 | static QemuOptsList qemu_root_bds_opts; |
| 652 | |
| 653 | /* Takes the ownership of bs_opts */ |
| 654 | static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) |
| 655 | { |
| 656 | BlockDriverState *bs; |
| 657 | QemuOpts *opts; |
| 658 | Error *local_error = NULL; |
| 659 | BlockdevDetectZeroesOptions detect_zeroes; |
Max Reitz | bd745e2 | 2015-10-19 17:53:32 +0200 | [diff] [blame] | 660 | int bdrv_flags = 0; |
| 661 | |
| 662 | opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); |
| 663 | if (!opts) { |
| 664 | goto fail; |
| 665 | } |
| 666 | |
| 667 | qemu_opts_absorb_qdict(opts, bs_opts, &local_error); |
| 668 | if (local_error) { |
| 669 | error_propagate(errp, local_error); |
| 670 | goto fail; |
| 671 | } |
| 672 | |
| 673 | extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, |
| 674 | &detect_zeroes, &local_error); |
| 675 | if (local_error) { |
| 676 | error_propagate(errp, local_error); |
| 677 | goto fail; |
| 678 | } |
| 679 | |
Kevin Wolf | a81d616 | 2016-03-07 14:23:04 +0100 | [diff] [blame] | 680 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
| 681 | * with other callers) rather than what we want as the real defaults. |
| 682 | * Apply the defaults here instead. */ |
Kevin Wolf | a81d616 | 2016-03-07 14:23:04 +0100 | [diff] [blame] | 683 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
| 684 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); |
| 685 | |
Kevin Wolf | 12d5ee3 | 2016-02-19 16:48:10 +0100 | [diff] [blame] | 686 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 687 | bdrv_flags |= BDRV_O_INACTIVE; |
| 688 | } |
| 689 | |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 690 | bs = bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); |
| 691 | if (!bs) { |
Max Reitz | bd745e2 | 2015-10-19 17:53:32 +0200 | [diff] [blame] | 692 | goto fail_no_bs_opts; |
| 693 | } |
| 694 | |
| 695 | bs->detect_zeroes = detect_zeroes; |
| 696 | |
| 697 | fail_no_bs_opts: |
| 698 | qemu_opts_del(opts); |
| 699 | return bs; |
| 700 | |
| 701 | fail: |
| 702 | qemu_opts_del(opts); |
| 703 | QDECREF(bs_opts); |
| 704 | return NULL; |
| 705 | } |
| 706 | |
Max Reitz | 9c4218e | 2016-01-29 16:36:12 +0100 | [diff] [blame] | 707 | void blockdev_close_all_bdrv_states(void) |
| 708 | { |
| 709 | BlockDriverState *bs, *next_bs; |
| 710 | |
| 711 | QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { |
| 712 | AioContext *ctx = bdrv_get_aio_context(bs); |
| 713 | |
| 714 | aio_context_acquire(ctx); |
| 715 | bdrv_unref(bs); |
| 716 | aio_context_release(ctx); |
| 717 | } |
| 718 | } |
| 719 | |
Max Reitz | 262b4e8 | 2016-03-16 19:54:41 +0100 | [diff] [blame] | 720 | /* Iterates over the list of monitor-owned BlockDriverStates */ |
| 721 | BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) |
| 722 | { |
| 723 | return bs ? QTAILQ_NEXT(bs, monitor_list) |
| 724 | : QTAILQ_FIRST(&monitor_bdrv_states); |
| 725 | } |
| 726 | |
Kevin Wolf | 5abbf0e | 2014-09-18 11:48:34 +0200 | [diff] [blame] | 727 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
| 728 | Error **errp) |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 729 | { |
| 730 | const char *value; |
| 731 | |
| 732 | value = qemu_opt_get(opts, from); |
| 733 | if (value) { |
Kevin Wolf | 5abbf0e | 2014-09-18 11:48:34 +0200 | [diff] [blame] | 734 | if (qemu_opt_find(opts, to)) { |
| 735 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " |
| 736 | "same time", to, from); |
| 737 | return; |
| 738 | } |
Jun Li | 20d6cd4 | 2014-09-24 13:45:27 +0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* rename all items in opts */ |
| 742 | while ((value = qemu_opt_get(opts, from))) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 743 | qemu_opt_set(opts, to, value, &error_abort); |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 744 | qemu_opt_unset(opts, from); |
| 745 | } |
| 746 | } |
| 747 | |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 748 | QemuOptsList qemu_legacy_drive_opts = { |
| 749 | .name = "drive", |
| 750 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), |
| 751 | .desc = { |
| 752 | { |
Kevin Wolf | 87a899c | 2013-09-10 15:48:13 +0200 | [diff] [blame] | 753 | .name = "bus", |
| 754 | .type = QEMU_OPT_NUMBER, |
| 755 | .help = "bus number", |
| 756 | },{ |
| 757 | .name = "unit", |
| 758 | .type = QEMU_OPT_NUMBER, |
| 759 | .help = "unit number (i.e. lun for scsi)", |
| 760 | },{ |
| 761 | .name = "index", |
| 762 | .type = QEMU_OPT_NUMBER, |
| 763 | .help = "index number", |
| 764 | },{ |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 765 | .name = "media", |
| 766 | .type = QEMU_OPT_STRING, |
| 767 | .help = "media type (disk, cdrom)", |
Kevin Wolf | 593d464 | 2013-08-28 17:24:51 +0200 | [diff] [blame] | 768 | },{ |
| 769 | .name = "if", |
| 770 | .type = QEMU_OPT_STRING, |
| 771 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", |
Kevin Wolf | b41a733 | 2013-09-09 16:49:49 +0200 | [diff] [blame] | 772 | },{ |
| 773 | .name = "cyls", |
| 774 | .type = QEMU_OPT_NUMBER, |
| 775 | .help = "number of cylinders (ide disk geometry)", |
| 776 | },{ |
| 777 | .name = "heads", |
| 778 | .type = QEMU_OPT_NUMBER, |
| 779 | .help = "number of heads (ide disk geometry)", |
| 780 | },{ |
| 781 | .name = "secs", |
| 782 | .type = QEMU_OPT_NUMBER, |
| 783 | .help = "number of sectors (ide disk geometry)", |
| 784 | },{ |
| 785 | .name = "trans", |
| 786 | .type = QEMU_OPT_STRING, |
| 787 | .help = "chs translation (auto, lba, none)", |
Kevin Wolf | 2692929 | 2013-09-09 17:01:03 +0200 | [diff] [blame] | 788 | },{ |
| 789 | .name = "boot", |
| 790 | .type = QEMU_OPT_BOOL, |
| 791 | .help = "(deprecated, ignored)", |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 792 | },{ |
| 793 | .name = "addr", |
| 794 | .type = QEMU_OPT_STRING, |
| 795 | .help = "pci address (virtio only)", |
Max Reitz | d095b46 | 2013-12-20 19:28:14 +0100 | [diff] [blame] | 796 | },{ |
Kevin Wolf | bcf8315 | 2014-06-06 15:21:48 +0200 | [diff] [blame] | 797 | .name = "serial", |
| 798 | .type = QEMU_OPT_STRING, |
| 799 | .help = "disk serial number", |
| 800 | },{ |
Max Reitz | d095b46 | 2013-12-20 19:28:14 +0100 | [diff] [blame] | 801 | .name = "file", |
| 802 | .type = QEMU_OPT_STRING, |
| 803 | .help = "file name", |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 804 | }, |
Kevin Wolf | 0ebd24e | 2013-09-19 15:12:18 +0200 | [diff] [blame] | 805 | |
| 806 | /* Options that are passed on, but have special semantics with -drive */ |
| 807 | { |
| 808 | .name = "read-only", |
| 809 | .type = QEMU_OPT_BOOL, |
| 810 | .help = "open drive file as read-only", |
| 811 | },{ |
Kevin Wolf | ee13ed1 | 2014-02-09 09:52:32 +0100 | [diff] [blame] | 812 | .name = "rerror", |
| 813 | .type = QEMU_OPT_STRING, |
| 814 | .help = "read error action", |
| 815 | },{ |
| 816 | .name = "werror", |
| 817 | .type = QEMU_OPT_STRING, |
| 818 | .help = "write error action", |
| 819 | },{ |
Kevin Wolf | 0ebd24e | 2013-09-19 15:12:18 +0200 | [diff] [blame] | 820 | .name = "copy-on-read", |
| 821 | .type = QEMU_OPT_BOOL, |
| 822 | .help = "copy read data from backing file into image file", |
| 823 | }, |
| 824 | |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 825 | { /* end of list */ } |
| 826 | }, |
| 827 | }; |
| 828 | |
Markus Armbruster | 60e19e0 | 2014-06-06 14:50:58 +0200 | [diff] [blame] | 829 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 830 | { |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 831 | const char *value; |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 832 | BlockBackend *blk; |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 833 | DriveInfo *dinfo = NULL; |
Kevin Wolf | f298d07 | 2013-09-10 12:01:20 +0200 | [diff] [blame] | 834 | QDict *bs_opts; |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 835 | QemuOpts *legacy_opts; |
| 836 | DriveMediaType media = MEDIA_DISK; |
Kevin Wolf | 593d464 | 2013-08-28 17:24:51 +0200 | [diff] [blame] | 837 | BlockInterfaceType type; |
Kevin Wolf | b41a733 | 2013-09-09 16:49:49 +0200 | [diff] [blame] | 838 | int cyls, heads, secs, translation; |
Kevin Wolf | 87a899c | 2013-09-10 15:48:13 +0200 | [diff] [blame] | 839 | int max_devs, bus_id, unit_id, index; |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 840 | const char *devaddr; |
Kevin Wolf | ee13ed1 | 2014-02-09 09:52:32 +0100 | [diff] [blame] | 841 | const char *werror, *rerror; |
Fam Zheng | a7fdbcf | 2013-10-15 17:45:50 +0800 | [diff] [blame] | 842 | bool read_only = false; |
| 843 | bool copy_on_read; |
Kevin Wolf | bcf8315 | 2014-06-06 15:21:48 +0200 | [diff] [blame] | 844 | const char *serial; |
Max Reitz | d095b46 | 2013-12-20 19:28:14 +0100 | [diff] [blame] | 845 | const char *filename; |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 846 | Error *local_err = NULL; |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 847 | int i; |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 848 | |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 849 | /* Change legacy command line options into QMP ones */ |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 850 | static const struct { |
| 851 | const char *from; |
| 852 | const char *to; |
| 853 | } opt_renames[] = { |
| 854 | { "iops", "throttling.iops-total" }, |
| 855 | { "iops_rd", "throttling.iops-read" }, |
| 856 | { "iops_wr", "throttling.iops-write" }, |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 857 | |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 858 | { "bps", "throttling.bps-total" }, |
| 859 | { "bps_rd", "throttling.bps-read" }, |
| 860 | { "bps_wr", "throttling.bps-write" }, |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 861 | |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 862 | { "iops_max", "throttling.iops-total-max" }, |
| 863 | { "iops_rd_max", "throttling.iops-read-max" }, |
| 864 | { "iops_wr_max", "throttling.iops-write-max" }, |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 865 | |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 866 | { "bps_max", "throttling.bps-total-max" }, |
| 867 | { "bps_rd_max", "throttling.bps-read-max" }, |
| 868 | { "bps_wr_max", "throttling.bps-write-max" }, |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 869 | |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 870 | { "iops_size", "throttling.iops-size" }, |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 871 | |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 872 | { "group", "throttling.group" }, |
| 873 | |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 874 | { "readonly", "read-only" }, |
| 875 | }; |
| 876 | |
| 877 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { |
Kevin Wolf | 5abbf0e | 2014-09-18 11:48:34 +0200 | [diff] [blame] | 878 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, |
| 879 | &local_err); |
| 880 | if (local_err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 881 | error_report_err(local_err); |
Kevin Wolf | 5abbf0e | 2014-09-18 11:48:34 +0200 | [diff] [blame] | 882 | return NULL; |
| 883 | } |
Kevin Wolf | 247147f | 2014-09-24 16:37:14 +0200 | [diff] [blame] | 884 | } |
Kevin Wolf | 0f227a9 | 2013-07-19 20:07:29 +0200 | [diff] [blame] | 885 | |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 886 | value = qemu_opt_get(all_opts, "cache"); |
| 887 | if (value) { |
| 888 | int flags = 0; |
Kevin Wolf | 04feb4a | 2016-03-18 15:35:51 +0100 | [diff] [blame] | 889 | bool writethrough; |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 890 | |
Kevin Wolf | 04feb4a | 2016-03-18 15:35:51 +0100 | [diff] [blame] | 891 | if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 892 | error_report("invalid cache option"); |
| 893 | return NULL; |
| 894 | } |
| 895 | |
| 896 | /* Specific options take precedence */ |
Kevin Wolf | 54861b9 | 2015-04-07 16:55:00 +0200 | [diff] [blame] | 897 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { |
| 898 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, |
Kevin Wolf | 04feb4a | 2016-03-18 15:35:51 +0100 | [diff] [blame] | 899 | !writethrough, &error_abort); |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 900 | } |
Kevin Wolf | 54861b9 | 2015-04-07 16:55:00 +0200 | [diff] [blame] | 901 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { |
| 902 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, |
Markus Armbruster | cccb796 | 2015-02-12 16:37:44 +0100 | [diff] [blame] | 903 | !!(flags & BDRV_O_NOCACHE), &error_abort); |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 904 | } |
Kevin Wolf | 54861b9 | 2015-04-07 16:55:00 +0200 | [diff] [blame] | 905 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { |
| 906 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, |
Markus Armbruster | cccb796 | 2015-02-12 16:37:44 +0100 | [diff] [blame] | 907 | !!(flags & BDRV_O_NO_FLUSH), &error_abort); |
Kevin Wolf | 29c4e2b | 2013-07-18 16:31:25 +0200 | [diff] [blame] | 908 | } |
| 909 | qemu_opt_unset(all_opts, "cache"); |
| 910 | } |
| 911 | |
Kevin Wolf | f298d07 | 2013-09-10 12:01:20 +0200 | [diff] [blame] | 912 | /* Get a QDict for processing the options */ |
| 913 | bs_opts = qdict_new(); |
| 914 | qemu_opts_to_qdict(all_opts, bs_opts); |
| 915 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 916 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
| 917 | &error_abort); |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 918 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 919 | if (local_err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 920 | error_report_err(local_err); |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 921 | goto fail; |
| 922 | } |
| 923 | |
Kevin Wolf | 2692929 | 2013-09-09 17:01:03 +0200 | [diff] [blame] | 924 | /* Deprecated option boot=[on|off] */ |
| 925 | if (qemu_opt_get(legacy_opts, "boot") != NULL) { |
| 926 | fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " |
| 927 | "ignored. Future versions will reject this parameter. Please " |
| 928 | "update your scripts.\n"); |
| 929 | } |
| 930 | |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 931 | /* Media type */ |
| 932 | value = qemu_opt_get(legacy_opts, "media"); |
| 933 | if (value) { |
| 934 | if (!strcmp(value, "disk")) { |
| 935 | media = MEDIA_DISK; |
| 936 | } else if (!strcmp(value, "cdrom")) { |
| 937 | media = MEDIA_CDROM; |
Fam Zheng | a7fdbcf | 2013-10-15 17:45:50 +0800 | [diff] [blame] | 938 | read_only = true; |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 939 | } else { |
| 940 | error_report("'%s' invalid media", value); |
| 941 | goto fail; |
| 942 | } |
| 943 | } |
| 944 | |
Kevin Wolf | 0ebd24e | 2013-09-19 15:12:18 +0200 | [diff] [blame] | 945 | /* copy-on-read is disabled with a warning for read-only devices */ |
Fam Zheng | a7fdbcf | 2013-10-15 17:45:50 +0800 | [diff] [blame] | 946 | read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); |
Kevin Wolf | 0ebd24e | 2013-09-19 15:12:18 +0200 | [diff] [blame] | 947 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
| 948 | |
| 949 | if (read_only && copy_on_read) { |
| 950 | error_report("warning: disabling copy-on-read on read-only drive"); |
| 951 | copy_on_read = false; |
| 952 | } |
| 953 | |
| 954 | qdict_put(bs_opts, "read-only", |
| 955 | qstring_from_str(read_only ? "on" : "off")); |
| 956 | qdict_put(bs_opts, "copy-on-read", |
| 957 | qstring_from_str(copy_on_read ? "on" :"off")); |
| 958 | |
Kevin Wolf | 593d464 | 2013-08-28 17:24:51 +0200 | [diff] [blame] | 959 | /* Controller type */ |
| 960 | value = qemu_opt_get(legacy_opts, "if"); |
| 961 | if (value) { |
| 962 | for (type = 0; |
| 963 | type < IF_COUNT && strcmp(value, if_name[type]); |
| 964 | type++) { |
| 965 | } |
| 966 | if (type == IF_COUNT) { |
| 967 | error_report("unsupported bus type '%s'", value); |
| 968 | goto fail; |
| 969 | } |
| 970 | } else { |
| 971 | type = block_default_type; |
| 972 | } |
| 973 | |
Kevin Wolf | b41a733 | 2013-09-09 16:49:49 +0200 | [diff] [blame] | 974 | /* Geometry */ |
| 975 | cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); |
| 976 | heads = qemu_opt_get_number(legacy_opts, "heads", 0); |
| 977 | secs = qemu_opt_get_number(legacy_opts, "secs", 0); |
| 978 | |
| 979 | if (cyls || heads || secs) { |
| 980 | if (cyls < 1) { |
| 981 | error_report("invalid physical cyls number"); |
| 982 | goto fail; |
| 983 | } |
| 984 | if (heads < 1) { |
| 985 | error_report("invalid physical heads number"); |
| 986 | goto fail; |
| 987 | } |
| 988 | if (secs < 1) { |
| 989 | error_report("invalid physical secs number"); |
| 990 | goto fail; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 995 | value = qemu_opt_get(legacy_opts, "trans"); |
| 996 | if (value != NULL) { |
| 997 | if (!cyls) { |
| 998 | error_report("'%s' trans must be used with cyls, heads and secs", |
| 999 | value); |
| 1000 | goto fail; |
| 1001 | } |
| 1002 | if (!strcmp(value, "none")) { |
| 1003 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 1004 | } else if (!strcmp(value, "lba")) { |
| 1005 | translation = BIOS_ATA_TRANSLATION_LBA; |
Paolo Bonzini | f31c41f | 2014-02-08 11:01:54 +0100 | [diff] [blame] | 1006 | } else if (!strcmp(value, "large")) { |
| 1007 | translation = BIOS_ATA_TRANSLATION_LARGE; |
| 1008 | } else if (!strcmp(value, "rechs")) { |
| 1009 | translation = BIOS_ATA_TRANSLATION_RECHS; |
Kevin Wolf | b41a733 | 2013-09-09 16:49:49 +0200 | [diff] [blame] | 1010 | } else if (!strcmp(value, "auto")) { |
| 1011 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 1012 | } else { |
| 1013 | error_report("'%s' invalid translation type", value); |
| 1014 | goto fail; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if (media == MEDIA_CDROM) { |
| 1019 | if (cyls || secs || heads) { |
| 1020 | error_report("CHS can't be set with media=cdrom"); |
| 1021 | goto fail; |
| 1022 | } |
| 1023 | } |
| 1024 | |
Kevin Wolf | 87a899c | 2013-09-10 15:48:13 +0200 | [diff] [blame] | 1025 | /* Device address specified by bus/unit or index. |
| 1026 | * If none was specified, try to find the first free one. */ |
| 1027 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); |
| 1028 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); |
| 1029 | index = qemu_opt_get_number(legacy_opts, "index", -1); |
| 1030 | |
| 1031 | max_devs = if_max_devs[type]; |
| 1032 | |
| 1033 | if (index != -1) { |
| 1034 | if (bus_id != 0 || unit_id != -1) { |
| 1035 | error_report("index cannot be used with bus and unit"); |
| 1036 | goto fail; |
| 1037 | } |
| 1038 | bus_id = drive_index_to_bus_id(type, index); |
| 1039 | unit_id = drive_index_to_unit_id(type, index); |
| 1040 | } |
| 1041 | |
| 1042 | if (unit_id == -1) { |
| 1043 | unit_id = 0; |
| 1044 | while (drive_get(type, bus_id, unit_id) != NULL) { |
| 1045 | unit_id++; |
| 1046 | if (max_devs && unit_id >= max_devs) { |
| 1047 | unit_id -= max_devs; |
| 1048 | bus_id++; |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | if (max_devs && unit_id >= max_devs) { |
| 1054 | error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); |
| 1055 | goto fail; |
| 1056 | } |
| 1057 | |
| 1058 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 1059 | error_report("drive with bus=%d, unit=%d (index=%d) exists", |
| 1060 | bus_id, unit_id, index); |
| 1061 | goto fail; |
| 1062 | } |
| 1063 | |
Kevin Wolf | bcf8315 | 2014-06-06 15:21:48 +0200 | [diff] [blame] | 1064 | /* Serial number */ |
| 1065 | serial = qemu_opt_get(legacy_opts, "serial"); |
| 1066 | |
Kevin Wolf | 87a899c | 2013-09-10 15:48:13 +0200 | [diff] [blame] | 1067 | /* no id supplied -> create one */ |
| 1068 | if (qemu_opts_id(all_opts) == NULL) { |
| 1069 | char *new_id; |
| 1070 | const char *mediastr = ""; |
| 1071 | if (type == IF_IDE || type == IF_SCSI) { |
| 1072 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 1073 | } |
| 1074 | if (max_devs) { |
| 1075 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, |
| 1076 | mediastr, unit_id); |
| 1077 | } else { |
| 1078 | new_id = g_strdup_printf("%s%s%i", if_name[type], |
| 1079 | mediastr, unit_id); |
| 1080 | } |
| 1081 | qdict_put(bs_opts, "id", qstring_from_str(new_id)); |
| 1082 | g_free(new_id); |
| 1083 | } |
| 1084 | |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 1085 | /* Add virtio block device */ |
| 1086 | devaddr = qemu_opt_get(legacy_opts, "addr"); |
| 1087 | if (devaddr && type != IF_VIRTIO) { |
| 1088 | error_report("addr is not supported by this bus type"); |
| 1089 | goto fail; |
| 1090 | } |
| 1091 | |
| 1092 | if (type == IF_VIRTIO) { |
| 1093 | QemuOpts *devopts; |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 1094 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
| 1095 | &error_abort); |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 1096 | if (arch_type == QEMU_ARCH_S390X) { |
Alexander Graf | 1f68f1d | 2015-06-16 23:06:33 +0200 | [diff] [blame] | 1097 | qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 1098 | } else { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 1099 | qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 1100 | } |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 1101 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), |
| 1102 | &error_abort); |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 1103 | if (devaddr) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 1104 | qemu_opt_set(devopts, "addr", devaddr, &error_abort); |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
Max Reitz | d095b46 | 2013-12-20 19:28:14 +0100 | [diff] [blame] | 1108 | filename = qemu_opt_get(legacy_opts, "file"); |
| 1109 | |
Kevin Wolf | ee13ed1 | 2014-02-09 09:52:32 +0100 | [diff] [blame] | 1110 | /* Check werror/rerror compatibility with if=... */ |
| 1111 | werror = qemu_opt_get(legacy_opts, "werror"); |
| 1112 | if (werror != NULL) { |
| 1113 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && |
| 1114 | type != IF_NONE) { |
| 1115 | error_report("werror is not supported by this bus type"); |
| 1116 | goto fail; |
| 1117 | } |
| 1118 | qdict_put(bs_opts, "werror", qstring_from_str(werror)); |
| 1119 | } |
| 1120 | |
| 1121 | rerror = qemu_opt_get(legacy_opts, "rerror"); |
| 1122 | if (rerror != NULL) { |
| 1123 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && |
| 1124 | type != IF_NONE) { |
| 1125 | error_report("rerror is not supported by this bus type"); |
| 1126 | goto fail; |
| 1127 | } |
| 1128 | qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); |
| 1129 | } |
| 1130 | |
Kevin Wolf | 2d246f0 | 2013-09-18 15:14:47 +0200 | [diff] [blame] | 1131 | /* Actual block device init: Functionality shared with blockdev-add */ |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 1132 | blk = blockdev_init(filename, bs_opts, &local_err); |
Markus Armbruster | 3cb0e25 | 2014-05-28 11:17:02 +0200 | [diff] [blame] | 1133 | bs_opts = NULL; |
Markus Armbruster | 18e46a0 | 2014-10-07 13:59:06 +0200 | [diff] [blame] | 1134 | if (!blk) { |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1135 | if (local_err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 1136 | error_report_err(local_err); |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 1137 | } |
Kevin Wolf | 2d246f0 | 2013-09-18 15:14:47 +0200 | [diff] [blame] | 1138 | goto fail; |
Kevin Wolf | b681072 | 2013-09-20 11:33:11 +0200 | [diff] [blame] | 1139 | } else { |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1140 | assert(!local_err); |
Kevin Wolf | 2d246f0 | 2013-09-18 15:14:47 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
Markus Armbruster | 26f8b3a | 2014-10-07 13:59:22 +0200 | [diff] [blame] | 1143 | /* Create legacy DriveInfo */ |
| 1144 | dinfo = g_malloc0(sizeof(*dinfo)); |
Kevin Wolf | f298d07 | 2013-09-10 12:01:20 +0200 | [diff] [blame] | 1145 | dinfo->opts = all_opts; |
Kevin Wolf | 2d246f0 | 2013-09-18 15:14:47 +0200 | [diff] [blame] | 1146 | |
Kevin Wolf | b41a733 | 2013-09-09 16:49:49 +0200 | [diff] [blame] | 1147 | dinfo->cyls = cyls; |
| 1148 | dinfo->heads = heads; |
| 1149 | dinfo->secs = secs; |
| 1150 | dinfo->trans = translation; |
| 1151 | |
Kevin Wolf | ee13ed1 | 2014-02-09 09:52:32 +0100 | [diff] [blame] | 1152 | dinfo->type = type; |
Kevin Wolf | 87a899c | 2013-09-10 15:48:13 +0200 | [diff] [blame] | 1153 | dinfo->bus = bus_id; |
| 1154 | dinfo->unit = unit_id; |
Kevin Wolf | 394c7d4 | 2013-09-13 14:09:17 +0200 | [diff] [blame] | 1155 | dinfo->devaddr = devaddr; |
Kevin Wolf | bcf8315 | 2014-06-06 15:21:48 +0200 | [diff] [blame] | 1156 | dinfo->serial = g_strdup(serial); |
| 1157 | |
Markus Armbruster | 26f8b3a | 2014-10-07 13:59:22 +0200 | [diff] [blame] | 1158 | blk_set_legacy_dinfo(blk, dinfo); |
| 1159 | |
Kevin Wolf | e34ef04 | 2013-09-19 14:24:10 +0200 | [diff] [blame] | 1160 | switch(type) { |
| 1161 | case IF_IDE: |
| 1162 | case IF_SCSI: |
| 1163 | case IF_XEN: |
| 1164 | case IF_NONE: |
| 1165 | dinfo->media_cd = media == MEDIA_CDROM; |
| 1166 | break; |
| 1167 | default: |
| 1168 | break; |
| 1169 | } |
| 1170 | |
Kevin Wolf | 2d246f0 | 2013-09-18 15:14:47 +0200 | [diff] [blame] | 1171 | fail: |
Kevin Wolf | 33cb7dc | 2013-08-28 17:00:13 +0200 | [diff] [blame] | 1172 | qemu_opts_del(legacy_opts); |
Markus Armbruster | 3cb0e25 | 2014-05-28 11:17:02 +0200 | [diff] [blame] | 1173 | QDECREF(bs_opts); |
Kevin Wolf | 2d246f0 | 2013-09-18 15:14:47 +0200 | [diff] [blame] | 1174 | return dinfo; |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Kevin Wolf | b6c1bae | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 1177 | static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) |
| 1178 | { |
| 1179 | BlockDriverState *bs; |
| 1180 | |
| 1181 | bs = bdrv_lookup_bs(name, name, errp); |
| 1182 | if (bs == NULL) { |
| 1183 | return NULL; |
| 1184 | } |
| 1185 | |
| 1186 | if (!bdrv_is_root_node(bs)) { |
| 1187 | error_setg(errp, "Need a root block node"); |
| 1188 | return NULL; |
| 1189 | } |
| 1190 | |
| 1191 | if (!bdrv_is_inserted(bs)) { |
| 1192 | error_setg(errp, "Device has no medium"); |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | |
| 1196 | return bs; |
| 1197 | } |
| 1198 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1199 | void hmp_commit(Monitor *mon, const QDict *qdict) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1200 | { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1201 | const char *device = qdict_get_str(qdict, "device"); |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1202 | BlockBackend *blk; |
Stefan Hajnoczi | e887749 | 2012-03-05 18:10:11 +0000 | [diff] [blame] | 1203 | int ret; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1204 | |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 1205 | if (!strcmp(device, "all")) { |
Max Reitz | da31d59 | 2016-03-16 19:54:32 +0100 | [diff] [blame] | 1206 | ret = blk_commit_all(); |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 1207 | } else { |
Stefan Hajnoczi | 84aa014 | 2015-11-04 20:27:23 +0300 | [diff] [blame] | 1208 | BlockDriverState *bs; |
| 1209 | AioContext *aio_context; |
| 1210 | |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1211 | blk = blk_by_name(device); |
| 1212 | if (!blk) { |
Jeff Cody | 58513bd | 2013-01-18 12:45:35 -0500 | [diff] [blame] | 1213 | monitor_printf(mon, "Device '%s' not found\n", device); |
Markus Armbruster | ac59eb9 | 2010-06-02 18:55:19 +0200 | [diff] [blame] | 1214 | return; |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 1215 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1216 | if (!blk_is_available(blk)) { |
| 1217 | monitor_printf(mon, "Device '%s' has no medium\n", device); |
| 1218 | return; |
| 1219 | } |
Stefan Hajnoczi | 84aa014 | 2015-11-04 20:27:23 +0300 | [diff] [blame] | 1220 | |
| 1221 | bs = blk_bs(blk); |
| 1222 | aio_context = bdrv_get_aio_context(bs); |
| 1223 | aio_context_acquire(aio_context); |
| 1224 | |
| 1225 | ret = bdrv_commit(bs); |
| 1226 | |
| 1227 | aio_context_release(aio_context); |
Jeff Cody | 58513bd | 2013-01-18 12:45:35 -0500 | [diff] [blame] | 1228 | } |
| 1229 | if (ret < 0) { |
| 1230 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, |
| 1231 | strerror(-ret)); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1235 | static void blockdev_do_action(TransactionAction *action, Error **errp) |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 1236 | { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1237 | TransactionActionList list; |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 1238 | |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1239 | list.value = action; |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 1240 | list.next = NULL; |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1241 | qmp_transaction(&list, false, NULL, errp); |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 1242 | } |
| 1243 | |
Benoît Canet | 0901f67 | 2014-01-23 21:31:38 +0100 | [diff] [blame] | 1244 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
| 1245 | bool has_node_name, const char *node_name, |
| 1246 | const char *snapshot_file, |
| 1247 | bool has_snapshot_node_name, |
| 1248 | const char *snapshot_node_name, |
Luiz Capitulino | 6106e24 | 2011-11-25 16:15:19 -0200 | [diff] [blame] | 1249 | bool has_format, const char *format, |
Benoît Canet | 0901f67 | 2014-01-23 21:31:38 +0100 | [diff] [blame] | 1250 | bool has_mode, NewImageMode mode, Error **errp) |
Jes Sorensen | f888256 | 2010-12-16 13:52:16 +0100 | [diff] [blame] | 1251 | { |
Alberto Garcia | a911e6a | 2015-10-26 14:27:14 +0200 | [diff] [blame] | 1252 | BlockdevSnapshotSync snapshot = { |
Benoît Canet | 0901f67 | 2014-01-23 21:31:38 +0100 | [diff] [blame] | 1253 | .has_device = has_device, |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 1254 | .device = (char *) device, |
Benoît Canet | 0901f67 | 2014-01-23 21:31:38 +0100 | [diff] [blame] | 1255 | .has_node_name = has_node_name, |
| 1256 | .node_name = (char *) node_name, |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 1257 | .snapshot_file = (char *) snapshot_file, |
Benoît Canet | 0901f67 | 2014-01-23 21:31:38 +0100 | [diff] [blame] | 1258 | .has_snapshot_node_name = has_snapshot_node_name, |
| 1259 | .snapshot_node_name = (char *) snapshot_node_name, |
Paolo Bonzini | 6cc2a41 | 2012-03-06 18:55:59 +0100 | [diff] [blame] | 1260 | .has_format = has_format, |
| 1261 | .format = (char *) format, |
| 1262 | .has_mode = has_mode, |
| 1263 | .mode = mode, |
| 1264 | }; |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1265 | TransactionAction action = { |
| 1266 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1267 | .u.blockdev_snapshot_sync.data = &snapshot, |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1268 | }; |
| 1269 | blockdev_do_action(&action, errp); |
Jes Sorensen | f888256 | 2010-12-16 13:52:16 +0100 | [diff] [blame] | 1270 | } |
| 1271 | |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1272 | void qmp_blockdev_snapshot(const char *node, const char *overlay, |
| 1273 | Error **errp) |
| 1274 | { |
| 1275 | BlockdevSnapshot snapshot_data = { |
| 1276 | .node = (char *) node, |
| 1277 | .overlay = (char *) overlay |
| 1278 | }; |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1279 | TransactionAction action = { |
| 1280 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1281 | .u.blockdev_snapshot.data = &snapshot_data, |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1282 | }; |
| 1283 | blockdev_do_action(&action, errp); |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1284 | } |
| 1285 | |
Wenchao Xia | f323bc9 | 2013-09-11 14:04:35 +0800 | [diff] [blame] | 1286 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
| 1287 | const char *name, |
| 1288 | Error **errp) |
| 1289 | { |
| 1290 | BlockdevSnapshotInternal snapshot = { |
| 1291 | .device = (char *) device, |
| 1292 | .name = (char *) name |
| 1293 | }; |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1294 | TransactionAction action = { |
| 1295 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1296 | .u.blockdev_snapshot_internal_sync.data = &snapshot, |
Eric Blake | 10f7590 | 2016-03-03 09:16:50 -0700 | [diff] [blame] | 1297 | }; |
| 1298 | blockdev_do_action(&action, errp); |
Wenchao Xia | f323bc9 | 2013-09-11 14:04:35 +0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1301 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
| 1302 | bool has_id, |
| 1303 | const char *id, |
| 1304 | bool has_name, |
| 1305 | const char *name, |
| 1306 | Error **errp) |
| 1307 | { |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1308 | BlockDriverState *bs; |
| 1309 | BlockBackend *blk; |
Stefan Hajnoczi | 4ef3982 | 2014-11-19 14:19:42 +0000 | [diff] [blame] | 1310 | AioContext *aio_context; |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1311 | QEMUSnapshotInfo sn; |
| 1312 | Error *local_err = NULL; |
| 1313 | SnapshotInfo *info = NULL; |
| 1314 | int ret; |
| 1315 | |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1316 | blk = blk_by_name(device); |
| 1317 | if (!blk) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 1318 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1319 | "Device '%s' not found", device); |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1320 | return NULL; |
| 1321 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1322 | |
| 1323 | aio_context = blk_get_aio_context(blk); |
| 1324 | aio_context_acquire(aio_context); |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1325 | |
| 1326 | if (!has_id) { |
| 1327 | id = NULL; |
| 1328 | } |
| 1329 | |
| 1330 | if (!has_name) { |
| 1331 | name = NULL; |
| 1332 | } |
| 1333 | |
| 1334 | if (!id && !name) { |
| 1335 | error_setg(errp, "Name or id must be provided"); |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1336 | goto out_aio_context; |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1339 | if (!blk_is_available(blk)) { |
| 1340 | error_setg(errp, "Device '%s' has no medium", device); |
| 1341 | goto out_aio_context; |
| 1342 | } |
| 1343 | bs = blk_bs(blk); |
Stefan Hajnoczi | 4ef3982 | 2014-11-19 14:19:42 +0000 | [diff] [blame] | 1344 | |
Stefan Hajnoczi | 0b92885 | 2014-11-19 14:19:43 +0000 | [diff] [blame] | 1345 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { |
| 1346 | goto out_aio_context; |
| 1347 | } |
| 1348 | |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1349 | 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] | 1350 | if (local_err) { |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1351 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 4ef3982 | 2014-11-19 14:19:42 +0000 | [diff] [blame] | 1352 | goto out_aio_context; |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1353 | } |
| 1354 | if (!ret) { |
| 1355 | error_setg(errp, |
| 1356 | "Snapshot with id '%s' and name '%s' does not exist on " |
| 1357 | "device '%s'", |
| 1358 | STR_OR_NULL(id), STR_OR_NULL(name), device); |
Stefan Hajnoczi | 4ef3982 | 2014-11-19 14:19:42 +0000 | [diff] [blame] | 1359 | goto out_aio_context; |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | bdrv_snapshot_delete(bs, id, name, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1363 | if (local_err) { |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1364 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 4ef3982 | 2014-11-19 14:19:42 +0000 | [diff] [blame] | 1365 | goto out_aio_context; |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Stefan Hajnoczi | 4ef3982 | 2014-11-19 14:19:42 +0000 | [diff] [blame] | 1368 | aio_context_release(aio_context); |
| 1369 | |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 1370 | info = g_new0(SnapshotInfo, 1); |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1371 | info->id = g_strdup(sn.id_str); |
| 1372 | info->name = g_strdup(sn.name); |
| 1373 | info->date_nsec = sn.date_nsec; |
| 1374 | info->date_sec = sn.date_sec; |
| 1375 | info->vm_state_size = sn.vm_state_size; |
| 1376 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; |
| 1377 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; |
| 1378 | |
| 1379 | return info; |
Stefan Hajnoczi | 4ef3982 | 2014-11-19 14:19:42 +0000 | [diff] [blame] | 1380 | |
| 1381 | out_aio_context: |
| 1382 | aio_context_release(aio_context); |
| 1383 | return NULL; |
Wenchao Xia | 44e3e05 | 2013-09-11 14:04:36 +0800 | [diff] [blame] | 1384 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1385 | |
John Snow | 341ebc2 | 2015-04-17 19:49:52 -0400 | [diff] [blame] | 1386 | /** |
| 1387 | * block_dirty_bitmap_lookup: |
| 1388 | * Return a dirty bitmap (if present), after validating |
| 1389 | * the node reference and bitmap names. |
| 1390 | * |
| 1391 | * @node: The name of the BDS node to search for bitmaps |
| 1392 | * @name: The name of the bitmap to search for |
| 1393 | * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. |
| 1394 | * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. |
| 1395 | * @errp: Output pointer for error information. Can be NULL. |
| 1396 | * |
| 1397 | * @return: A bitmap object on success, or NULL on failure. |
| 1398 | */ |
| 1399 | static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, |
| 1400 | const char *name, |
| 1401 | BlockDriverState **pbs, |
| 1402 | AioContext **paio, |
| 1403 | Error **errp) |
| 1404 | { |
| 1405 | BlockDriverState *bs; |
| 1406 | BdrvDirtyBitmap *bitmap; |
| 1407 | AioContext *aio_context; |
| 1408 | |
| 1409 | if (!node) { |
| 1410 | error_setg(errp, "Node cannot be NULL"); |
| 1411 | return NULL; |
| 1412 | } |
| 1413 | if (!name) { |
| 1414 | error_setg(errp, "Bitmap name cannot be NULL"); |
| 1415 | return NULL; |
| 1416 | } |
| 1417 | bs = bdrv_lookup_bs(node, node, NULL); |
| 1418 | if (!bs) { |
| 1419 | error_setg(errp, "Node '%s' not found", node); |
| 1420 | return NULL; |
| 1421 | } |
| 1422 | |
| 1423 | aio_context = bdrv_get_aio_context(bs); |
| 1424 | aio_context_acquire(aio_context); |
| 1425 | |
| 1426 | bitmap = bdrv_find_dirty_bitmap(bs, name); |
| 1427 | if (!bitmap) { |
| 1428 | error_setg(errp, "Dirty bitmap '%s' not found", name); |
| 1429 | goto fail; |
| 1430 | } |
| 1431 | |
| 1432 | if (pbs) { |
| 1433 | *pbs = bs; |
| 1434 | } |
| 1435 | if (paio) { |
| 1436 | *paio = aio_context; |
| 1437 | } else { |
| 1438 | aio_context_release(aio_context); |
| 1439 | } |
| 1440 | |
| 1441 | return bitmap; |
| 1442 | |
| 1443 | fail: |
| 1444 | aio_context_release(aio_context); |
| 1445 | return NULL; |
| 1446 | } |
| 1447 | |
Stefan Hajnoczi | b756b9c | 2014-11-21 10:48:57 +0000 | [diff] [blame] | 1448 | /* New and old BlockDriverState structs for atomic group operations */ |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1449 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1450 | typedef struct BlkActionState BlkActionState; |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1451 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1452 | /** |
| 1453 | * BlkActionOps: |
| 1454 | * Table of operations that define an Action. |
| 1455 | * |
| 1456 | * @instance_size: Size of state struct, in bytes. |
| 1457 | * @prepare: Prepare the work, must NOT be NULL. |
| 1458 | * @commit: Commit the changes, can be NULL. |
| 1459 | * @abort: Abort the changes on fail, can be NULL. |
| 1460 | * @clean: Clean up resources after all transaction actions have called |
| 1461 | * commit() or abort(). Can be NULL. |
| 1462 | * |
| 1463 | * Only prepare() may fail. In a single transaction, only one of commit() or |
| 1464 | * abort() will be called. clean() will always be called if it is present. |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1465 | */ |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1466 | typedef struct BlkActionOps { |
| 1467 | size_t instance_size; |
| 1468 | void (*prepare)(BlkActionState *common, Error **errp); |
| 1469 | void (*commit)(BlkActionState *common); |
| 1470 | void (*abort)(BlkActionState *common); |
| 1471 | void (*clean)(BlkActionState *common); |
| 1472 | } BlkActionOps; |
| 1473 | |
| 1474 | /** |
| 1475 | * BlkActionState: |
| 1476 | * Describes one Action's state within a Transaction. |
| 1477 | * |
| 1478 | * @action: QAPI-defined enum identifying which Action to perform. |
| 1479 | * @ops: Table of ActionOps this Action can perform. |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1480 | * @block_job_txn: Transaction which this action belongs to. |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1481 | * @entry: List membership for all Actions in this Transaction. |
| 1482 | * |
| 1483 | * This structure must be arranged as first member in a subclassed type, |
| 1484 | * assuming that the compiler will also arrange it to the same offsets as the |
| 1485 | * base class. |
| 1486 | */ |
| 1487 | struct BlkActionState { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1488 | TransactionAction *action; |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1489 | const BlkActionOps *ops; |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1490 | BlockJobTxn *block_job_txn; |
| 1491 | TransactionProperties *txn_props; |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1492 | QSIMPLEQ_ENTRY(BlkActionState) entry; |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1493 | }; |
| 1494 | |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1495 | /* internal snapshot private data */ |
| 1496 | typedef struct InternalSnapshotState { |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1497 | BlkActionState common; |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1498 | BlockDriverState *bs; |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1499 | AioContext *aio_context; |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1500 | QEMUSnapshotInfo sn; |
Fam Zheng | 507306c | 2015-10-23 11:08:13 +0800 | [diff] [blame] | 1501 | bool created; |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1502 | } InternalSnapshotState; |
| 1503 | |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1504 | |
| 1505 | static int action_check_completion_mode(BlkActionState *s, Error **errp) |
| 1506 | { |
| 1507 | if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { |
| 1508 | error_setg(errp, |
| 1509 | "Action '%s' does not support Transaction property " |
| 1510 | "completion-mode = %s", |
| 1511 | TransactionActionKind_lookup[s->action->type], |
| 1512 | ActionCompletionMode_lookup[s->txn_props->completion_mode]); |
| 1513 | return -1; |
| 1514 | } |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1518 | static void internal_snapshot_prepare(BlkActionState *common, |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1519 | Error **errp) |
| 1520 | { |
Markus Armbruster | f70edf9 | 2014-04-25 16:50:34 +0200 | [diff] [blame] | 1521 | Error *local_err = NULL; |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1522 | const char *device; |
| 1523 | const char *name; |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1524 | BlockBackend *blk; |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1525 | BlockDriverState *bs; |
| 1526 | QEMUSnapshotInfo old_sn, *sn; |
| 1527 | bool ret; |
| 1528 | qemu_timeval tv; |
| 1529 | BlockdevSnapshotInternal *internal; |
| 1530 | InternalSnapshotState *state; |
| 1531 | int ret1; |
| 1532 | |
Eric Blake | 6a8f966 | 2015-10-26 16:34:54 -0600 | [diff] [blame] | 1533 | g_assert(common->action->type == |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1534 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1535 | internal = common->action->u.blockdev_snapshot_internal_sync.data; |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1536 | state = DO_UPCAST(InternalSnapshotState, common, common); |
| 1537 | |
| 1538 | /* 1. parse input */ |
| 1539 | device = internal->device; |
| 1540 | name = internal->name; |
| 1541 | |
| 1542 | /* 2. check for validation */ |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1543 | if (action_check_completion_mode(common, errp) < 0) { |
| 1544 | return; |
| 1545 | } |
| 1546 | |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1547 | blk = blk_by_name(device); |
| 1548 | if (!blk) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 1549 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1550 | "Device '%s' not found", device); |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1551 | return; |
| 1552 | } |
| 1553 | |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1554 | /* AioContext is released in .clean() */ |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1555 | state->aio_context = blk_get_aio_context(blk); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1556 | aio_context_acquire(state->aio_context); |
| 1557 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1558 | if (!blk_is_available(blk)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1559 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1560 | return; |
| 1561 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1562 | bs = blk_bs(blk); |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1563 | |
Fam Zheng | 507306c | 2015-10-23 11:08:13 +0800 | [diff] [blame] | 1564 | state->bs = bs; |
| 1565 | bdrv_drained_begin(bs); |
| 1566 | |
Stefan Hajnoczi | 3dc7ca3 | 2014-11-21 10:49:00 +0000 | [diff] [blame] | 1567 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { |
| 1568 | return; |
| 1569 | } |
| 1570 | |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1571 | if (bdrv_is_read_only(bs)) { |
Alberto Garcia | 81e5f78 | 2015-04-08 12:29:19 +0300 | [diff] [blame] | 1572 | error_setg(errp, "Device '%s' is read only", device); |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1573 | return; |
| 1574 | } |
| 1575 | |
| 1576 | if (!bdrv_can_snapshot(bs)) { |
Alberto Garcia | 81e5f78 | 2015-04-08 12:29:19 +0300 | [diff] [blame] | 1577 | error_setg(errp, "Block format '%s' used by device '%s' " |
| 1578 | "does not support internal snapshots", |
| 1579 | bs->drv->format_name, device); |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | if (!strlen(name)) { |
| 1584 | error_setg(errp, "Name is empty"); |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | /* check whether a snapshot with name exist */ |
Markus Armbruster | f70edf9 | 2014-04-25 16:50:34 +0200 | [diff] [blame] | 1589 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
| 1590 | &local_err); |
| 1591 | if (local_err) { |
| 1592 | error_propagate(errp, local_err); |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1593 | return; |
| 1594 | } else if (ret) { |
| 1595 | error_setg(errp, |
| 1596 | "Snapshot with name '%s' already exists on device '%s'", |
| 1597 | name, device); |
| 1598 | return; |
| 1599 | } |
| 1600 | |
| 1601 | /* 3. take the snapshot */ |
| 1602 | sn = &state->sn; |
| 1603 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 1604 | qemu_gettimeofday(&tv); |
| 1605 | sn->date_sec = tv.tv_sec; |
| 1606 | sn->date_nsec = tv.tv_usec * 1000; |
| 1607 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 1608 | |
| 1609 | ret1 = bdrv_snapshot_create(bs, sn); |
| 1610 | if (ret1 < 0) { |
| 1611 | error_setg_errno(errp, -ret1, |
| 1612 | "Failed to create snapshot '%s' on device '%s'", |
| 1613 | name, device); |
| 1614 | return; |
| 1615 | } |
| 1616 | |
| 1617 | /* 4. succeed, mark a snapshot is created */ |
Fam Zheng | 507306c | 2015-10-23 11:08:13 +0800 | [diff] [blame] | 1618 | state->created = true; |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1619 | } |
| 1620 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1621 | static void internal_snapshot_abort(BlkActionState *common) |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1622 | { |
| 1623 | InternalSnapshotState *state = |
| 1624 | DO_UPCAST(InternalSnapshotState, common, common); |
| 1625 | BlockDriverState *bs = state->bs; |
| 1626 | QEMUSnapshotInfo *sn = &state->sn; |
| 1627 | Error *local_error = NULL; |
| 1628 | |
Fam Zheng | 507306c | 2015-10-23 11:08:13 +0800 | [diff] [blame] | 1629 | if (!state->created) { |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 1634 | error_reportf_err(local_error, |
| 1635 | "Failed to delete snapshot with id '%s' and " |
| 1636 | "name '%s' on device '%s' in abort: ", |
| 1637 | sn->id_str, sn->name, |
| 1638 | bdrv_get_device_name(bs)); |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1642 | static void internal_snapshot_clean(BlkActionState *common) |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1643 | { |
| 1644 | InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, |
| 1645 | common, common); |
| 1646 | |
| 1647 | if (state->aio_context) { |
Fam Zheng | 507306c | 2015-10-23 11:08:13 +0800 | [diff] [blame] | 1648 | if (state->bs) { |
| 1649 | bdrv_drained_end(state->bs); |
| 1650 | } |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1651 | aio_context_release(state->aio_context); |
| 1652 | } |
| 1653 | } |
| 1654 | |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1655 | /* external snapshot private data */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1656 | typedef struct ExternalSnapshotState { |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1657 | BlkActionState common; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1658 | BlockDriverState *old_bs; |
| 1659 | BlockDriverState *new_bs; |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1660 | AioContext *aio_context; |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1661 | } ExternalSnapshotState; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 1662 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1663 | static void external_snapshot_prepare(BlkActionState *common, |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1664 | Error **errp) |
| 1665 | { |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 1666 | int flags = 0; |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1667 | QDict *options = NULL; |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1668 | Error *local_err = NULL; |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1669 | /* Device and node name of the image to generate the snapshot from */ |
Wenchao Xia | e2a31e8 | 2013-05-08 18:25:13 +0800 | [diff] [blame] | 1670 | const char *device; |
Benoît Canet | 0901f67 | 2014-01-23 21:31:38 +0100 | [diff] [blame] | 1671 | const char *node_name; |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1672 | /* Reference to the new image (for 'blockdev-snapshot') */ |
| 1673 | const char *snapshot_ref; |
| 1674 | /* File name of the new image (for 'blockdev-snapshot-sync') */ |
Wenchao Xia | e2a31e8 | 2013-05-08 18:25:13 +0800 | [diff] [blame] | 1675 | const char *new_image_file; |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1676 | ExternalSnapshotState *state = |
| 1677 | DO_UPCAST(ExternalSnapshotState, common, common); |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 1678 | TransactionAction *action = common->action; |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1679 | |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1680 | /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar |
| 1681 | * purpose but a different set of parameters */ |
| 1682 | switch (action->type) { |
| 1683 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: |
| 1684 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1685 | BlockdevSnapshot *s = action->u.blockdev_snapshot.data; |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1686 | device = s->node; |
| 1687 | node_name = s->node; |
| 1688 | new_image_file = NULL; |
| 1689 | snapshot_ref = s->overlay; |
| 1690 | } |
| 1691 | break; |
| 1692 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: |
| 1693 | { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1694 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1695 | device = s->has_device ? s->device : NULL; |
| 1696 | node_name = s->has_node_name ? s->node_name : NULL; |
| 1697 | new_image_file = s->snapshot_file; |
| 1698 | snapshot_ref = NULL; |
| 1699 | } |
| 1700 | break; |
| 1701 | default: |
| 1702 | g_assert_not_reached(); |
Wenchao Xia | e2a31e8 | 2013-05-08 18:25:13 +0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | /* start processing */ |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1706 | if (action_check_completion_mode(common, errp) < 0) { |
| 1707 | return; |
| 1708 | } |
| 1709 | |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1710 | state->old_bs = bdrv_lookup_bs(device, node_name, errp); |
| 1711 | if (!state->old_bs) { |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1712 | return; |
| 1713 | } |
| 1714 | |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1715 | /* Acquire AioContext now so any threads operating on old_bs stop */ |
| 1716 | state->aio_context = bdrv_get_aio_context(state->old_bs); |
| 1717 | aio_context_acquire(state->aio_context); |
Fam Zheng | da763e8 | 2015-10-23 11:08:10 +0800 | [diff] [blame] | 1718 | bdrv_drained_begin(state->old_bs); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1719 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1720 | if (!bdrv_is_inserted(state->old_bs)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1721 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1722 | return; |
| 1723 | } |
| 1724 | |
Fam Zheng | 3718d8a | 2014-05-23 21:29:43 +0800 | [diff] [blame] | 1725 | if (bdrv_op_is_blocked(state->old_bs, |
| 1726 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1727 | return; |
| 1728 | } |
| 1729 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1730 | if (!bdrv_is_read_only(state->old_bs)) { |
| 1731 | if (bdrv_flush(state->old_bs)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1732 | error_setg(errp, QERR_IO_ERROR); |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1733 | return; |
| 1734 | } |
| 1735 | } |
| 1736 | |
Benoît Canet | 212a5a8 | 2014-01-23 21:31:36 +0100 | [diff] [blame] | 1737 | if (!bdrv_is_first_non_filter(state->old_bs)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1738 | error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); |
Benoît Canet | f6186f4 | 2013-10-02 14:33:48 +0200 | [diff] [blame] | 1739 | return; |
| 1740 | } |
| 1741 | |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1742 | if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1743 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1744 | const char *format = s->has_format ? s->format : "qcow2"; |
| 1745 | enum NewImageMode mode; |
| 1746 | const char *snapshot_node_name = |
| 1747 | s->has_snapshot_node_name ? s->snapshot_node_name : NULL; |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1748 | |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1749 | if (node_name && !snapshot_node_name) { |
| 1750 | error_setg(errp, "New snapshot node name missing"); |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1751 | return; |
| 1752 | } |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1753 | |
| 1754 | if (snapshot_node_name && |
| 1755 | bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { |
| 1756 | error_setg(errp, "New snapshot node name already in use"); |
| 1757 | return; |
| 1758 | } |
| 1759 | |
| 1760 | flags = state->old_bs->open_flags; |
Kevin Wolf | 4c84498 | 2016-02-29 13:12:26 +0100 | [diff] [blame] | 1761 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1762 | |
| 1763 | /* create new image w/backing file */ |
| 1764 | mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; |
| 1765 | if (mode != NEW_IMAGE_MODE_EXISTING) { |
Kevin Wolf | f86b8b5 | 2016-03-02 12:16:44 +0100 | [diff] [blame] | 1766 | int64_t size = bdrv_getlength(state->old_bs); |
| 1767 | if (size < 0) { |
| 1768 | error_setg_errno(errp, -size, "bdrv_getlength failed"); |
| 1769 | return; |
| 1770 | } |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1771 | bdrv_img_create(new_image_file, format, |
| 1772 | state->old_bs->filename, |
| 1773 | state->old_bs->drv->format_name, |
Kevin Wolf | f86b8b5 | 2016-03-02 12:16:44 +0100 | [diff] [blame] | 1774 | NULL, size, flags, &local_err, false); |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1775 | if (local_err) { |
| 1776 | error_propagate(errp, local_err); |
| 1777 | return; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | options = qdict_new(); |
| 1782 | if (s->has_snapshot_node_name) { |
| 1783 | qdict_put(options, "node-name", |
| 1784 | qstring_from_str(snapshot_node_name)); |
| 1785 | } |
| 1786 | qdict_put(options, "driver", qstring_from_str(format)); |
| 1787 | |
| 1788 | flags |= BDRV_O_NO_BACKING; |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1789 | } |
| 1790 | |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 1791 | state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, |
| 1792 | errp); |
Max Reitz | f67503e | 2014-02-18 18:33:05 +0100 | [diff] [blame] | 1793 | /* We will manually add the backing_hd field to the bs later */ |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 1794 | if (!state->new_bs) { |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1795 | return; |
| 1796 | } |
| 1797 | |
Kevin Wolf | 1f0c461 | 2016-03-22 18:38:44 +0100 | [diff] [blame] | 1798 | if (bdrv_has_blk(state->new_bs)) { |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1799 | error_setg(errp, "The snapshot is already in use by %s", |
Kevin Wolf | 1f0c461 | 2016-03-22 18:38:44 +0100 | [diff] [blame] | 1800 | bdrv_get_parent_name(state->new_bs)); |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 1801 | return; |
| 1802 | } |
| 1803 | |
| 1804 | if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, |
| 1805 | errp)) { |
| 1806 | return; |
| 1807 | } |
| 1808 | |
| 1809 | if (state->new_bs->backing != NULL) { |
| 1810 | error_setg(errp, "The snapshot already has a backing image"); |
Alberto Garcia | 08b24cf | 2015-11-03 12:32:35 +0200 | [diff] [blame] | 1811 | return; |
| 1812 | } |
| 1813 | |
| 1814 | if (!state->new_bs->drv->supports_backing) { |
| 1815 | error_setg(errp, "The snapshot does not support backing images"); |
Wenchao Xia | 9b9877e | 2013-05-08 18:25:12 +0800 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1819 | static void external_snapshot_commit(BlkActionState *common) |
Wenchao Xia | 3b0047e | 2013-05-08 18:25:14 +0800 | [diff] [blame] | 1820 | { |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1821 | ExternalSnapshotState *state = |
| 1822 | DO_UPCAST(ExternalSnapshotState, common, common); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 1823 | |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1824 | bdrv_set_aio_context(state->new_bs, state->aio_context); |
| 1825 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1826 | /* This removes our old bs and adds the new bs */ |
| 1827 | bdrv_append(state->new_bs, state->old_bs); |
Wenchao Xia | 3b0047e | 2013-05-08 18:25:14 +0800 | [diff] [blame] | 1828 | /* We don't need (or want) to use the transactional |
| 1829 | * bdrv_reopen_multiple() across all the entries at once, because we |
| 1830 | * don't want to abort all of them if one of them fails the reopen */ |
Kevin Wolf | 4c84498 | 2016-02-29 13:12:26 +0100 | [diff] [blame] | 1831 | if (!state->old_bs->copy_on_read) { |
| 1832 | bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, |
| 1833 | NULL); |
| 1834 | } |
Wenchao Xia | 3b0047e | 2013-05-08 18:25:14 +0800 | [diff] [blame] | 1835 | } |
| 1836 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1837 | static void external_snapshot_abort(BlkActionState *common) |
Wenchao Xia | 96b86bf | 2013-05-08 18:25:15 +0800 | [diff] [blame] | 1838 | { |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 1839 | ExternalSnapshotState *state = |
| 1840 | DO_UPCAST(ExternalSnapshotState, common, common); |
| 1841 | if (state->new_bs) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 1842 | bdrv_unref(state->new_bs); |
Wenchao Xia | 96b86bf | 2013-05-08 18:25:15 +0800 | [diff] [blame] | 1843 | } |
Fam Zheng | da763e8 | 2015-10-23 11:08:10 +0800 | [diff] [blame] | 1844 | } |
| 1845 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1846 | static void external_snapshot_clean(BlkActionState *common) |
Fam Zheng | da763e8 | 2015-10-23 11:08:10 +0800 | [diff] [blame] | 1847 | { |
| 1848 | ExternalSnapshotState *state = |
| 1849 | DO_UPCAST(ExternalSnapshotState, common, common); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1850 | if (state->aio_context) { |
Fam Zheng | da763e8 | 2015-10-23 11:08:10 +0800 | [diff] [blame] | 1851 | bdrv_drained_end(state->old_bs); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1852 | aio_context_release(state->aio_context); |
| 1853 | } |
Wenchao Xia | 96b86bf | 2013-05-08 18:25:15 +0800 | [diff] [blame] | 1854 | } |
| 1855 | |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1856 | typedef struct DriveBackupState { |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1857 | BlkActionState common; |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1858 | BlockDriverState *bs; |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1859 | AioContext *aio_context; |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1860 | BlockJob *job; |
| 1861 | } DriveBackupState; |
| 1862 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 1863 | static void do_drive_backup(const char *job_id, const char *device, |
| 1864 | const char *target, bool has_format, |
| 1865 | const char *format, enum MirrorSyncMode sync, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 1866 | bool has_mode, enum NewImageMode mode, |
| 1867 | bool has_speed, int64_t speed, |
| 1868 | bool has_bitmap, const char *bitmap, |
| 1869 | bool has_on_source_error, |
| 1870 | BlockdevOnError on_source_error, |
| 1871 | bool has_on_target_error, |
| 1872 | BlockdevOnError on_target_error, |
| 1873 | BlockJobTxn *txn, Error **errp); |
| 1874 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1875 | static void drive_backup_prepare(BlkActionState *common, Error **errp) |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1876 | { |
| 1877 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1878 | BlockBackend *blk; |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1879 | DriveBackup *backup; |
| 1880 | Error *local_err = NULL; |
| 1881 | |
Eric Blake | 6a8f966 | 2015-10-26 16:34:54 -0600 | [diff] [blame] | 1882 | assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1883 | backup = common->action->u.drive_backup.data; |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1884 | |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 1885 | blk = blk_by_name(backup->device); |
| 1886 | if (!blk) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 1887 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1888 | "Device '%s' not found", backup->device); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Fam Zheng | 1fdd4b7 | 2015-10-23 11:08:11 +0800 | [diff] [blame] | 1892 | if (!blk_is_available(blk)) { |
| 1893 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); |
| 1894 | return; |
| 1895 | } |
| 1896 | |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1897 | /* AioContext is released in .clean() */ |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1898 | state->aio_context = blk_get_aio_context(blk); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1899 | aio_context_acquire(state->aio_context); |
Fam Zheng | 1fdd4b7 | 2015-10-23 11:08:11 +0800 | [diff] [blame] | 1900 | bdrv_drained_begin(blk_bs(blk)); |
| 1901 | state->bs = blk_bs(blk); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1902 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 1903 | do_drive_backup(backup->has_job_id ? backup->job_id : NULL, |
| 1904 | backup->device, backup->target, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 1905 | backup->has_format, backup->format, |
| 1906 | backup->sync, |
| 1907 | backup->has_mode, backup->mode, |
| 1908 | backup->has_speed, backup->speed, |
| 1909 | backup->has_bitmap, backup->bitmap, |
| 1910 | backup->has_on_source_error, backup->on_source_error, |
| 1911 | backup->has_on_target_error, backup->on_target_error, |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1912 | common->block_job_txn, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1913 | if (local_err) { |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1914 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1915 | return; |
| 1916 | } |
| 1917 | |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1918 | state->job = state->bs->job; |
| 1919 | } |
| 1920 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1921 | static void drive_backup_abort(BlkActionState *common) |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 1922 | { |
| 1923 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); |
| 1924 | BlockDriverState *bs = state->bs; |
| 1925 | |
| 1926 | /* Only cancel if it's the job we started */ |
| 1927 | if (bs && bs->job && bs->job == state->job) { |
| 1928 | block_job_cancel_sync(bs->job); |
| 1929 | } |
| 1930 | } |
| 1931 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1932 | static void drive_backup_clean(BlkActionState *common) |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1933 | { |
| 1934 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); |
| 1935 | |
| 1936 | if (state->aio_context) { |
Fam Zheng | 1fdd4b7 | 2015-10-23 11:08:11 +0800 | [diff] [blame] | 1937 | bdrv_drained_end(state->bs); |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 1938 | aio_context_release(state->aio_context); |
| 1939 | } |
| 1940 | } |
| 1941 | |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1942 | typedef struct BlockdevBackupState { |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1943 | BlkActionState common; |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1944 | BlockDriverState *bs; |
| 1945 | BlockJob *job; |
| 1946 | AioContext *aio_context; |
| 1947 | } BlockdevBackupState; |
| 1948 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 1949 | static void do_blockdev_backup(const char *job_id, const char *device, |
| 1950 | const char *target, enum MirrorSyncMode sync, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 1951 | bool has_speed, int64_t speed, |
| 1952 | bool has_on_source_error, |
| 1953 | BlockdevOnError on_source_error, |
| 1954 | bool has_on_target_error, |
| 1955 | BlockdevOnError on_target_error, |
| 1956 | BlockJobTxn *txn, Error **errp); |
| 1957 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 1958 | static void blockdev_backup_prepare(BlkActionState *common, Error **errp) |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1959 | { |
| 1960 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); |
| 1961 | BlockdevBackup *backup; |
Kevin Wolf | cef34ee | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 1962 | BlockDriverState *bs, *target; |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1963 | Error *local_err = NULL; |
| 1964 | |
Eric Blake | 6a8f966 | 2015-10-26 16:34:54 -0600 | [diff] [blame] | 1965 | assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 1966 | backup = common->action->u.blockdev_backup.data; |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1967 | |
Kevin Wolf | cef34ee | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 1968 | bs = qmp_get_root_bs(backup->device, errp); |
| 1969 | if (!bs) { |
Fam Zheng | ff52bf3 | 2015-10-23 11:08:12 +0800 | [diff] [blame] | 1970 | return; |
| 1971 | } |
| 1972 | |
Kevin Wolf | 39d990a | 2016-08-02 19:22:04 +0200 | [diff] [blame] | 1973 | target = bdrv_lookup_bs(backup->target, backup->target, errp); |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 1974 | if (!target) { |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1975 | return; |
| 1976 | } |
| 1977 | |
| 1978 | /* AioContext is released in .clean() */ |
Kevin Wolf | cef34ee | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 1979 | state->aio_context = bdrv_get_aio_context(bs); |
Kevin Wolf | 39d990a | 2016-08-02 19:22:04 +0200 | [diff] [blame] | 1980 | if (state->aio_context != bdrv_get_aio_context(target)) { |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1981 | state->aio_context = NULL; |
| 1982 | error_setg(errp, "Backup between two IO threads is not implemented"); |
| 1983 | return; |
| 1984 | } |
| 1985 | aio_context_acquire(state->aio_context); |
Kevin Wolf | cef34ee | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 1986 | state->bs = bs; |
Fam Zheng | ff52bf3 | 2015-10-23 11:08:12 +0800 | [diff] [blame] | 1987 | bdrv_drained_begin(state->bs); |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1988 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 1989 | do_blockdev_backup(backup->has_job_id ? backup->job_id : NULL, |
| 1990 | backup->device, backup->target, backup->sync, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 1991 | backup->has_speed, backup->speed, |
| 1992 | backup->has_on_source_error, backup->on_source_error, |
| 1993 | backup->has_on_target_error, backup->on_target_error, |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 1994 | common->block_job_txn, &local_err); |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 1995 | if (local_err) { |
| 1996 | error_propagate(errp, local_err); |
| 1997 | return; |
| 1998 | } |
| 1999 | |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 2000 | state->job = state->bs->job; |
| 2001 | } |
| 2002 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2003 | static void blockdev_backup_abort(BlkActionState *common) |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 2004 | { |
| 2005 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); |
| 2006 | BlockDriverState *bs = state->bs; |
| 2007 | |
| 2008 | /* Only cancel if it's the job we started */ |
| 2009 | if (bs && bs->job && bs->job == state->job) { |
| 2010 | block_job_cancel_sync(bs->job); |
| 2011 | } |
| 2012 | } |
| 2013 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2014 | static void blockdev_backup_clean(BlkActionState *common) |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 2015 | { |
| 2016 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); |
| 2017 | |
| 2018 | if (state->aio_context) { |
Fam Zheng | ff52bf3 | 2015-10-23 11:08:12 +0800 | [diff] [blame] | 2019 | bdrv_drained_end(state->bs); |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 2020 | aio_context_release(state->aio_context); |
| 2021 | } |
| 2022 | } |
| 2023 | |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2024 | typedef struct BlockDirtyBitmapState { |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2025 | BlkActionState common; |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2026 | BdrvDirtyBitmap *bitmap; |
| 2027 | BlockDriverState *bs; |
| 2028 | AioContext *aio_context; |
| 2029 | HBitmap *backup; |
| 2030 | bool prepared; |
| 2031 | } BlockDirtyBitmapState; |
| 2032 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2033 | static void block_dirty_bitmap_add_prepare(BlkActionState *common, |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2034 | Error **errp) |
| 2035 | { |
| 2036 | Error *local_err = NULL; |
| 2037 | BlockDirtyBitmapAdd *action; |
| 2038 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, |
| 2039 | common, common); |
| 2040 | |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2041 | if (action_check_completion_mode(common, errp) < 0) { |
| 2042 | return; |
| 2043 | } |
| 2044 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2045 | action = common->action->u.block_dirty_bitmap_add.data; |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2046 | /* AIO context taken and released within qmp_block_dirty_bitmap_add */ |
| 2047 | qmp_block_dirty_bitmap_add(action->node, action->name, |
| 2048 | action->has_granularity, action->granularity, |
| 2049 | &local_err); |
| 2050 | |
| 2051 | if (!local_err) { |
| 2052 | state->prepared = true; |
| 2053 | } else { |
| 2054 | error_propagate(errp, local_err); |
| 2055 | } |
| 2056 | } |
| 2057 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2058 | static void block_dirty_bitmap_add_abort(BlkActionState *common) |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2059 | { |
| 2060 | BlockDirtyBitmapAdd *action; |
| 2061 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, |
| 2062 | common, common); |
| 2063 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2064 | action = common->action->u.block_dirty_bitmap_add.data; |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2065 | /* Should not be able to fail: IF the bitmap was added via .prepare(), |
| 2066 | * then the node reference and bitmap name must have been valid. |
| 2067 | */ |
| 2068 | if (state->prepared) { |
| 2069 | qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); |
| 2070 | } |
| 2071 | } |
| 2072 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2073 | static void block_dirty_bitmap_clear_prepare(BlkActionState *common, |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2074 | Error **errp) |
| 2075 | { |
| 2076 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, |
| 2077 | common, common); |
| 2078 | BlockDirtyBitmap *action; |
| 2079 | |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2080 | if (action_check_completion_mode(common, errp) < 0) { |
| 2081 | return; |
| 2082 | } |
| 2083 | |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 2084 | action = common->action->u.block_dirty_bitmap_clear.data; |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2085 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
| 2086 | action->name, |
| 2087 | &state->bs, |
| 2088 | &state->aio_context, |
| 2089 | errp); |
| 2090 | if (!state->bitmap) { |
| 2091 | return; |
| 2092 | } |
| 2093 | |
| 2094 | if (bdrv_dirty_bitmap_frozen(state->bitmap)) { |
| 2095 | error_setg(errp, "Cannot modify a frozen bitmap"); |
| 2096 | return; |
| 2097 | } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { |
| 2098 | error_setg(errp, "Cannot clear a disabled bitmap"); |
| 2099 | return; |
| 2100 | } |
| 2101 | |
| 2102 | bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); |
| 2103 | /* AioContext is released in .clean() */ |
| 2104 | } |
| 2105 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2106 | static void block_dirty_bitmap_clear_abort(BlkActionState *common) |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2107 | { |
| 2108 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, |
| 2109 | common, common); |
| 2110 | |
| 2111 | bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); |
| 2112 | } |
| 2113 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2114 | static void block_dirty_bitmap_clear_commit(BlkActionState *common) |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2115 | { |
| 2116 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, |
| 2117 | common, common); |
| 2118 | |
| 2119 | hbitmap_free(state->backup); |
| 2120 | } |
| 2121 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2122 | static void block_dirty_bitmap_clear_clean(BlkActionState *common) |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2123 | { |
| 2124 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, |
| 2125 | common, common); |
| 2126 | |
| 2127 | if (state->aio_context) { |
| 2128 | aio_context_release(state->aio_context); |
| 2129 | } |
| 2130 | } |
| 2131 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2132 | static void abort_prepare(BlkActionState *common, Error **errp) |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 2133 | { |
| 2134 | error_setg(errp, "Transaction aborted using Abort action"); |
| 2135 | } |
| 2136 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2137 | static void abort_commit(BlkActionState *common) |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 2138 | { |
Stefan Weil | dfc6f86 | 2013-07-25 18:21:28 +0200 | [diff] [blame] | 2139 | g_assert_not_reached(); /* this action never succeeds */ |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 2140 | } |
| 2141 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2142 | static const BlkActionOps actions[] = { |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 2143 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { |
| 2144 | .instance_size = sizeof(ExternalSnapshotState), |
| 2145 | .prepare = external_snapshot_prepare, |
| 2146 | .commit = external_snapshot_commit, |
| 2147 | .abort = external_snapshot_abort, |
Alberto Garcia | 4ad6f3d | 2015-11-13 15:00:24 +0200 | [diff] [blame] | 2148 | .clean = external_snapshot_clean, |
Alberto Garcia | 43de7e2 | 2015-10-26 14:27:16 +0200 | [diff] [blame] | 2149 | }, |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 2150 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2151 | .instance_size = sizeof(ExternalSnapshotState), |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 2152 | .prepare = external_snapshot_prepare, |
| 2153 | .commit = external_snapshot_commit, |
| 2154 | .abort = external_snapshot_abort, |
Fam Zheng | da763e8 | 2015-10-23 11:08:10 +0800 | [diff] [blame] | 2155 | .clean = external_snapshot_clean, |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 2156 | }, |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 2157 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
| 2158 | .instance_size = sizeof(DriveBackupState), |
| 2159 | .prepare = drive_backup_prepare, |
| 2160 | .abort = drive_backup_abort, |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 2161 | .clean = drive_backup_clean, |
Stefan Hajnoczi | 3037f36 | 2013-06-24 17:13:17 +0200 | [diff] [blame] | 2162 | }, |
Fam Zheng | bd8baec | 2014-12-18 18:37:06 +0800 | [diff] [blame] | 2163 | [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { |
| 2164 | .instance_size = sizeof(BlockdevBackupState), |
| 2165 | .prepare = blockdev_backup_prepare, |
| 2166 | .abort = blockdev_backup_abort, |
| 2167 | .clean = blockdev_backup_clean, |
| 2168 | }, |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 2169 | [TRANSACTION_ACTION_KIND_ABORT] = { |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2170 | .instance_size = sizeof(BlkActionState), |
Stefan Hajnoczi | 78b18b7 | 2013-06-24 17:13:18 +0200 | [diff] [blame] | 2171 | .prepare = abort_prepare, |
| 2172 | .commit = abort_commit, |
| 2173 | }, |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 2174 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
| 2175 | .instance_size = sizeof(InternalSnapshotState), |
| 2176 | .prepare = internal_snapshot_prepare, |
| 2177 | .abort = internal_snapshot_abort, |
Stefan Hajnoczi | 5d6e96e | 2014-11-21 10:48:59 +0000 | [diff] [blame] | 2178 | .clean = internal_snapshot_clean, |
Wenchao Xia | bbe8601 | 2013-09-11 14:04:34 +0800 | [diff] [blame] | 2179 | }, |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2180 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { |
| 2181 | .instance_size = sizeof(BlockDirtyBitmapState), |
| 2182 | .prepare = block_dirty_bitmap_add_prepare, |
| 2183 | .abort = block_dirty_bitmap_add_abort, |
| 2184 | }, |
| 2185 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { |
| 2186 | .instance_size = sizeof(BlockDirtyBitmapState), |
| 2187 | .prepare = block_dirty_bitmap_clear_prepare, |
| 2188 | .commit = block_dirty_bitmap_clear_commit, |
| 2189 | .abort = block_dirty_bitmap_clear_abort, |
| 2190 | .clean = block_dirty_bitmap_clear_clean, |
| 2191 | } |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 2192 | }; |
| 2193 | |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2194 | /** |
| 2195 | * Allocate a TransactionProperties structure if necessary, and fill |
| 2196 | * that structure with desired defaults if they are unset. |
| 2197 | */ |
| 2198 | static TransactionProperties *get_transaction_properties( |
| 2199 | TransactionProperties *props) |
| 2200 | { |
| 2201 | if (!props) { |
| 2202 | props = g_new0(TransactionProperties, 1); |
| 2203 | } |
| 2204 | |
| 2205 | if (!props->has_completion_mode) { |
| 2206 | props->has_completion_mode = true; |
| 2207 | props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; |
| 2208 | } |
| 2209 | |
| 2210 | return props; |
| 2211 | } |
| 2212 | |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2213 | /* |
Stefan Hajnoczi | b756b9c | 2014-11-21 10:48:57 +0000 | [diff] [blame] | 2214 | * 'Atomic' group operations. The operations are performed as a set, and if |
| 2215 | * any fail then we roll back all operations in the group. |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2216 | */ |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2217 | void qmp_transaction(TransactionActionList *dev_list, |
| 2218 | bool has_props, |
| 2219 | struct TransactionProperties *props, |
| 2220 | Error **errp) |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2221 | { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 2222 | TransactionActionList *dev_entry = dev_list; |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2223 | BlockJobTxn *block_job_txn = NULL; |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2224 | BlkActionState *state, *next; |
Luiz Capitulino | 43e1704 | 2012-11-30 10:52:07 -0200 | [diff] [blame] | 2225 | Error *local_err = NULL; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2226 | |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2227 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2228 | QSIMPLEQ_INIT(&snap_bdrv_states); |
| 2229 | |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2230 | /* Does this transaction get canceled as a group on failure? |
| 2231 | * If not, we don't really need to make a BlockJobTxn. |
| 2232 | */ |
| 2233 | props = get_transaction_properties(props); |
| 2234 | if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { |
| 2235 | block_job_txn = block_job_txn_new(); |
| 2236 | } |
| 2237 | |
Stefan Hajnoczi | b756b9c | 2014-11-21 10:48:57 +0000 | [diff] [blame] | 2238 | /* drain all i/o before any operations */ |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2239 | bdrv_drain_all(); |
| 2240 | |
Stefan Hajnoczi | b756b9c | 2014-11-21 10:48:57 +0000 | [diff] [blame] | 2241 | /* We don't do anything in this loop that commits us to the operations */ |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2242 | while (NULL != dev_entry) { |
Kevin Wolf | c8a83e8 | 2013-05-13 10:43:43 +0200 | [diff] [blame] | 2243 | TransactionAction *dev_info = NULL; |
John Snow | 50f43f0 | 2015-11-05 18:13:09 -0500 | [diff] [blame] | 2244 | const BlkActionOps *ops; |
Paolo Bonzini | 52e7c24 | 2012-03-06 18:55:57 +0100 | [diff] [blame] | 2245 | |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2246 | dev_info = dev_entry->value; |
| 2247 | dev_entry = dev_entry->next; |
| 2248 | |
Eric Blake | 6a8f966 | 2015-10-26 16:34:54 -0600 | [diff] [blame] | 2249 | assert(dev_info->type < ARRAY_SIZE(actions)); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 2250 | |
Eric Blake | 6a8f966 | 2015-10-26 16:34:54 -0600 | [diff] [blame] | 2251 | ops = &actions[dev_info->type]; |
Max Reitz | aa3fe71 | 2013-09-12 14:57:27 +0200 | [diff] [blame] | 2252 | assert(ops->instance_size > 0); |
| 2253 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2254 | state = g_malloc0(ops->instance_size); |
| 2255 | state->ops = ops; |
| 2256 | state->action = dev_info; |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2257 | state->block_job_txn = block_job_txn; |
| 2258 | state->txn_props = props; |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2259 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2260 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2261 | state->ops->prepare(state, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2262 | if (local_err) { |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 2263 | error_propagate(errp, local_err); |
| 2264 | goto delete_and_fail; |
Paolo Bonzini | 52e7c24 | 2012-03-06 18:55:57 +0100 | [diff] [blame] | 2265 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2266 | } |
| 2267 | |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2268 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
Stefan Hajnoczi | f9ea81e | 2013-06-24 17:13:16 +0200 | [diff] [blame] | 2269 | if (state->ops->commit) { |
| 2270 | state->ops->commit(state); |
| 2271 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /* success */ |
| 2275 | goto exit; |
| 2276 | |
| 2277 | delete_and_fail: |
Stefan Hajnoczi | b756b9c | 2014-11-21 10:48:57 +0000 | [diff] [blame] | 2278 | /* failure, and it is all-or-none; roll back all operations */ |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2279 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
| 2280 | if (state->ops->abort) { |
| 2281 | state->ops->abort(state); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 2282 | } |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2283 | } |
| 2284 | exit: |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2285 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
| 2286 | if (state->ops->clean) { |
| 2287 | state->ops->clean(state); |
Wenchao Xia | ba0c86a | 2013-05-08 18:25:16 +0800 | [diff] [blame] | 2288 | } |
Stefan Hajnoczi | ba5d6ab | 2013-06-24 17:13:15 +0200 | [diff] [blame] | 2289 | g_free(state); |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2290 | } |
John Snow | 94d16a6 | 2015-11-05 18:13:18 -0500 | [diff] [blame] | 2291 | if (!has_props) { |
| 2292 | qapi_free_TransactionProperties(props); |
| 2293 | } |
| 2294 | block_job_txn_unref(block_job_txn); |
Jeff Cody | 8802d1f | 2012-02-28 15:54:06 -0500 | [diff] [blame] | 2295 | } |
| 2296 | |
Luiz Capitulino | c245b6a | 2011-12-07 16:02:36 -0200 | [diff] [blame] | 2297 | 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] | 2298 | { |
Max Reitz | 38f54bd | 2015-10-26 21:39:12 +0100 | [diff] [blame] | 2299 | Error *local_err = NULL; |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2300 | int rc; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2301 | |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2302 | if (!has_force) { |
| 2303 | force = false; |
| 2304 | } |
| 2305 | |
| 2306 | rc = do_open_tray(device, force, &local_err); |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 2307 | if (rc && rc != -ENOSYS) { |
Max Reitz | 38f54bd | 2015-10-26 21:39:12 +0100 | [diff] [blame] | 2308 | error_propagate(errp, local_err); |
Luiz Capitulino | c245b6a | 2011-12-07 16:02:36 -0200 | [diff] [blame] | 2309 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2310 | } |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 2311 | error_free(local_err); |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2312 | |
Max Reitz | 6e0abc2 | 2015-12-11 16:23:05 +0100 | [diff] [blame] | 2313 | qmp_x_blockdev_remove_medium(device, errp); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
Benoît Canet | 12d3ba8 | 2014-01-23 21:31:35 +0100 | [diff] [blame] | 2316 | void qmp_block_passwd(bool has_device, const char *device, |
| 2317 | bool has_node_name, const char *node_name, |
| 2318 | const char *password, Error **errp) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2319 | { |
Benoît Canet | 12d3ba8 | 2014-01-23 21:31:35 +0100 | [diff] [blame] | 2320 | Error *local_err = NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2321 | BlockDriverState *bs; |
Stefan Hajnoczi | e344209 | 2014-11-19 14:19:44 +0000 | [diff] [blame] | 2322 | AioContext *aio_context; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2323 | |
Benoît Canet | 12d3ba8 | 2014-01-23 21:31:35 +0100 | [diff] [blame] | 2324 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
| 2325 | has_node_name ? node_name : NULL, |
| 2326 | &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2327 | if (local_err) { |
Benoît Canet | 12d3ba8 | 2014-01-23 21:31:35 +0100 | [diff] [blame] | 2328 | error_propagate(errp, local_err); |
Luiz Capitulino | a4dea8a | 2011-11-23 13:28:21 -0200 | [diff] [blame] | 2329 | return; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2330 | } |
| 2331 | |
Stefan Hajnoczi | e344209 | 2014-11-19 14:19:44 +0000 | [diff] [blame] | 2332 | aio_context = bdrv_get_aio_context(bs); |
| 2333 | aio_context_acquire(aio_context); |
| 2334 | |
Markus Armbruster | 4d2855a | 2015-01-29 10:37:00 +0100 | [diff] [blame] | 2335 | bdrv_add_key(bs, password, errp); |
Stefan Hajnoczi | e344209 | 2014-11-19 14:19:44 +0000 | [diff] [blame] | 2336 | |
Stefan Hajnoczi | e344209 | 2014-11-19 14:19:44 +0000 | [diff] [blame] | 2337 | aio_context_release(aio_context); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 2338 | } |
| 2339 | |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 2340 | /* |
| 2341 | * Attempt to open the tray of @device. |
| 2342 | * If @force, ignore its tray lock. |
| 2343 | * Else, if the tray is locked, don't open it, but ask the guest to open it. |
| 2344 | * On error, store an error through @errp and return -errno. |
| 2345 | * If @device does not exist, return -ENODEV. |
| 2346 | * If it has no removable media, return -ENOTSUP. |
| 2347 | * If it has no tray, return -ENOSYS. |
| 2348 | * If the guest was asked to open the tray, return -EINPROGRESS. |
| 2349 | * Else, return 0. |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2350 | */ |
| 2351 | static int do_open_tray(const char *device, bool force, Error **errp) |
Max Reitz | 7d8a9f7 | 2015-10-26 21:39:08 +0100 | [diff] [blame] | 2352 | { |
| 2353 | BlockBackend *blk; |
| 2354 | bool locked; |
| 2355 | |
Max Reitz | 7d8a9f7 | 2015-10-26 21:39:08 +0100 | [diff] [blame] | 2356 | blk = blk_by_name(device); |
| 2357 | if (!blk) { |
| 2358 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 2359 | "Device '%s' not found", device); |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2360 | return -ENODEV; |
Max Reitz | 7d8a9f7 | 2015-10-26 21:39:08 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | if (!blk_dev_has_removable_media(blk)) { |
| 2364 | error_setg(errp, "Device '%s' is not removable", device); |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2365 | return -ENOTSUP; |
Max Reitz | 7d8a9f7 | 2015-10-26 21:39:08 +0100 | [diff] [blame] | 2366 | } |
| 2367 | |
Max Reitz | 12c7ec8 | 2016-01-29 20:49:11 +0100 | [diff] [blame] | 2368 | if (!blk_dev_has_tray(blk)) { |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 2369 | error_setg(errp, "Device '%s' does not have a tray", device); |
| 2370 | return -ENOSYS; |
Max Reitz | 12c7ec8 | 2016-01-29 20:49:11 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
Max Reitz | 7d8a9f7 | 2015-10-26 21:39:08 +0100 | [diff] [blame] | 2373 | if (blk_dev_is_tray_open(blk)) { |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2374 | return 0; |
Max Reitz | 7d8a9f7 | 2015-10-26 21:39:08 +0100 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | locked = blk_dev_is_medium_locked(blk); |
| 2378 | if (locked) { |
| 2379 | blk_dev_eject_request(blk, force); |
| 2380 | } |
| 2381 | |
| 2382 | if (!locked || force) { |
| 2383 | blk_dev_change_media_cb(blk, false); |
| 2384 | } |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2385 | |
| 2386 | if (locked && !force) { |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 2387 | error_setg(errp, "Device '%s' is locked and force was not specified, " |
| 2388 | "wait for tray to open and try again", device); |
| 2389 | return -EINPROGRESS; |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | return 0; |
| 2393 | } |
| 2394 | |
| 2395 | void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, |
| 2396 | Error **errp) |
| 2397 | { |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 2398 | Error *local_err = NULL; |
| 2399 | int rc; |
| 2400 | |
John Snow | 3a3086b | 2016-05-18 17:53:40 -0400 | [diff] [blame] | 2401 | if (!has_force) { |
| 2402 | force = false; |
| 2403 | } |
Colin Lord | bf18bee | 2016-06-06 14:15:22 -0400 | [diff] [blame] | 2404 | rc = do_open_tray(device, force, &local_err); |
| 2405 | if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { |
| 2406 | error_propagate(errp, local_err); |
| 2407 | return; |
| 2408 | } |
| 2409 | error_free(local_err); |
Max Reitz | 7d8a9f7 | 2015-10-26 21:39:08 +0100 | [diff] [blame] | 2410 | } |
| 2411 | |
Max Reitz | abaaf59 | 2015-10-26 21:39:09 +0100 | [diff] [blame] | 2412 | void qmp_blockdev_close_tray(const char *device, Error **errp) |
| 2413 | { |
| 2414 | BlockBackend *blk; |
| 2415 | |
| 2416 | blk = blk_by_name(device); |
| 2417 | if (!blk) { |
| 2418 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 2419 | "Device '%s' not found", device); |
| 2420 | return; |
| 2421 | } |
| 2422 | |
| 2423 | if (!blk_dev_has_removable_media(blk)) { |
| 2424 | error_setg(errp, "Device '%s' is not removable", device); |
| 2425 | return; |
| 2426 | } |
| 2427 | |
Max Reitz | 12c7ec8 | 2016-01-29 20:49:11 +0100 | [diff] [blame] | 2428 | if (!blk_dev_has_tray(blk)) { |
| 2429 | /* Ignore this command on tray-less devices */ |
| 2430 | return; |
| 2431 | } |
| 2432 | |
Max Reitz | abaaf59 | 2015-10-26 21:39:09 +0100 | [diff] [blame] | 2433 | if (!blk_dev_is_tray_open(blk)) { |
| 2434 | return; |
| 2435 | } |
| 2436 | |
| 2437 | blk_dev_change_media_cb(blk, true); |
| 2438 | } |
| 2439 | |
Max Reitz | 6e0abc2 | 2015-12-11 16:23:05 +0100 | [diff] [blame] | 2440 | void qmp_x_blockdev_remove_medium(const char *device, Error **errp) |
Max Reitz | 2814f67 | 2015-10-26 21:39:10 +0100 | [diff] [blame] | 2441 | { |
| 2442 | BlockBackend *blk; |
| 2443 | BlockDriverState *bs; |
| 2444 | AioContext *aio_context; |
| 2445 | bool has_device; |
| 2446 | |
| 2447 | blk = blk_by_name(device); |
| 2448 | if (!blk) { |
| 2449 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 2450 | "Device '%s' not found", device); |
| 2451 | return; |
| 2452 | } |
| 2453 | |
| 2454 | /* For BBs without a device, we can exchange the BDS tree at will */ |
| 2455 | has_device = blk_get_attached_dev(blk); |
| 2456 | |
| 2457 | if (has_device && !blk_dev_has_removable_media(blk)) { |
| 2458 | error_setg(errp, "Device '%s' is not removable", device); |
| 2459 | return; |
| 2460 | } |
| 2461 | |
Max Reitz | 12c7ec8 | 2016-01-29 20:49:11 +0100 | [diff] [blame] | 2462 | if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { |
Max Reitz | 2814f67 | 2015-10-26 21:39:10 +0100 | [diff] [blame] | 2463 | error_setg(errp, "Tray of device '%s' is not open", device); |
| 2464 | return; |
| 2465 | } |
| 2466 | |
| 2467 | bs = blk_bs(blk); |
| 2468 | if (!bs) { |
| 2469 | return; |
| 2470 | } |
| 2471 | |
| 2472 | aio_context = bdrv_get_aio_context(bs); |
| 2473 | aio_context_acquire(aio_context); |
| 2474 | |
| 2475 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { |
| 2476 | goto out; |
| 2477 | } |
| 2478 | |
Max Reitz | 2814f67 | 2015-10-26 21:39:10 +0100 | [diff] [blame] | 2479 | blk_remove_bs(blk); |
| 2480 | |
Max Reitz | 12c7ec8 | 2016-01-29 20:49:11 +0100 | [diff] [blame] | 2481 | if (!blk_dev_has_tray(blk)) { |
| 2482 | /* For tray-less devices, blockdev-open-tray is a no-op (or may not be |
| 2483 | * called at all); therefore, the medium needs to be ejected here. |
| 2484 | * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load |
| 2485 | * value passed here (i.e. false). */ |
| 2486 | blk_dev_change_media_cb(blk, false); |
| 2487 | } |
| 2488 | |
Max Reitz | 2814f67 | 2015-10-26 21:39:10 +0100 | [diff] [blame] | 2489 | out: |
| 2490 | aio_context_release(aio_context); |
| 2491 | } |
| 2492 | |
Max Reitz | d129988 | 2015-10-26 21:39:11 +0100 | [diff] [blame] | 2493 | static void qmp_blockdev_insert_anon_medium(const char *device, |
| 2494 | BlockDriverState *bs, Error **errp) |
| 2495 | { |
| 2496 | BlockBackend *blk; |
| 2497 | bool has_device; |
| 2498 | |
| 2499 | blk = blk_by_name(device); |
| 2500 | if (!blk) { |
| 2501 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 2502 | "Device '%s' not found", device); |
| 2503 | return; |
| 2504 | } |
| 2505 | |
| 2506 | /* For BBs without a device, we can exchange the BDS tree at will */ |
| 2507 | has_device = blk_get_attached_dev(blk); |
| 2508 | |
| 2509 | if (has_device && !blk_dev_has_removable_media(blk)) { |
| 2510 | error_setg(errp, "Device '%s' is not removable", device); |
| 2511 | return; |
| 2512 | } |
| 2513 | |
Max Reitz | 12c7ec8 | 2016-01-29 20:49:11 +0100 | [diff] [blame] | 2514 | if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { |
Max Reitz | d129988 | 2015-10-26 21:39:11 +0100 | [diff] [blame] | 2515 | error_setg(errp, "Tray of device '%s' is not open", device); |
| 2516 | return; |
| 2517 | } |
| 2518 | |
| 2519 | if (blk_bs(blk)) { |
| 2520 | error_setg(errp, "There already is a medium in device '%s'", device); |
| 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | blk_insert_bs(blk, bs); |
| 2525 | |
Max Reitz | 12c7ec8 | 2016-01-29 20:49:11 +0100 | [diff] [blame] | 2526 | if (!blk_dev_has_tray(blk)) { |
| 2527 | /* For tray-less devices, blockdev-close-tray is a no-op (or may not be |
| 2528 | * called at all); therefore, the medium needs to be pushed into the |
| 2529 | * slot here. |
| 2530 | * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load |
| 2531 | * value passed here (i.e. true). */ |
| 2532 | blk_dev_change_media_cb(blk, true); |
| 2533 | } |
Max Reitz | d129988 | 2015-10-26 21:39:11 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
Max Reitz | 6e0abc2 | 2015-12-11 16:23:05 +0100 | [diff] [blame] | 2536 | void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, |
| 2537 | Error **errp) |
Max Reitz | d129988 | 2015-10-26 21:39:11 +0100 | [diff] [blame] | 2538 | { |
| 2539 | BlockDriverState *bs; |
| 2540 | |
| 2541 | bs = bdrv_find_node(node_name); |
| 2542 | if (!bs) { |
| 2543 | error_setg(errp, "Node '%s' not found", node_name); |
| 2544 | return; |
| 2545 | } |
| 2546 | |
Kevin Wolf | 1f0c461 | 2016-03-22 18:38:44 +0100 | [diff] [blame] | 2547 | if (bdrv_has_blk(bs)) { |
Max Reitz | d129988 | 2015-10-26 21:39:11 +0100 | [diff] [blame] | 2548 | error_setg(errp, "Node '%s' is already in use by '%s'", node_name, |
Kevin Wolf | 1f0c461 | 2016-03-22 18:38:44 +0100 | [diff] [blame] | 2549 | bdrv_get_parent_name(bs)); |
Max Reitz | d129988 | 2015-10-26 21:39:11 +0100 | [diff] [blame] | 2550 | return; |
| 2551 | } |
| 2552 | |
| 2553 | qmp_blockdev_insert_anon_medium(device, bs, errp); |
| 2554 | } |
| 2555 | |
Max Reitz | 24fb413 | 2015-11-06 16:27:06 +0100 | [diff] [blame] | 2556 | void qmp_blockdev_change_medium(const char *device, const char *filename, |
| 2557 | bool has_format, const char *format, |
Max Reitz | 39ff43d | 2015-11-11 04:49:44 +0100 | [diff] [blame] | 2558 | bool has_read_only, |
| 2559 | BlockdevChangeReadOnlyMode read_only, |
Max Reitz | 24fb413 | 2015-11-06 16:27:06 +0100 | [diff] [blame] | 2560 | Error **errp) |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2561 | { |
| 2562 | BlockBackend *blk; |
| 2563 | BlockDriverState *medium_bs = NULL; |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 2564 | int bdrv_flags; |
Colin Lord | 38a53d5 | 2016-06-08 13:56:28 -0400 | [diff] [blame] | 2565 | int rc; |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2566 | QDict *options = NULL; |
| 2567 | Error *err = NULL; |
| 2568 | |
| 2569 | blk = blk_by_name(device); |
| 2570 | if (!blk) { |
| 2571 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 2572 | "Device '%s' not found", device); |
| 2573 | goto fail; |
| 2574 | } |
| 2575 | |
| 2576 | if (blk_bs(blk)) { |
| 2577 | blk_update_root_state(blk); |
| 2578 | } |
| 2579 | |
| 2580 | bdrv_flags = blk_get_open_flags_from_root_state(blk); |
Alyssa Milburn | 156abc2 | 2016-02-06 13:36:18 +0000 | [diff] [blame] | 2581 | bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | |
| 2582 | BDRV_O_PROTOCOL); |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2583 | |
Max Reitz | 39ff43d | 2015-11-11 04:49:44 +0100 | [diff] [blame] | 2584 | if (!has_read_only) { |
| 2585 | read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; |
| 2586 | } |
| 2587 | |
| 2588 | switch (read_only) { |
| 2589 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: |
| 2590 | break; |
| 2591 | |
| 2592 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: |
| 2593 | bdrv_flags &= ~BDRV_O_RDWR; |
| 2594 | break; |
| 2595 | |
| 2596 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: |
| 2597 | bdrv_flags |= BDRV_O_RDWR; |
| 2598 | break; |
| 2599 | |
| 2600 | default: |
| 2601 | abort(); |
| 2602 | } |
| 2603 | |
Max Reitz | 24fb413 | 2015-11-06 16:27:06 +0100 | [diff] [blame] | 2604 | if (has_format) { |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2605 | options = qdict_new(); |
| 2606 | qdict_put(options, "driver", qstring_from_str(format)); |
| 2607 | } |
| 2608 | |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 2609 | medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); |
| 2610 | if (!medium_bs) { |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2611 | goto fail; |
| 2612 | } |
| 2613 | |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2614 | bdrv_add_key(medium_bs, NULL, &err); |
| 2615 | if (err) { |
| 2616 | error_propagate(errp, err); |
| 2617 | goto fail; |
| 2618 | } |
| 2619 | |
Colin Lord | 38a53d5 | 2016-06-08 13:56:28 -0400 | [diff] [blame] | 2620 | rc = do_open_tray(device, false, &err); |
| 2621 | if (rc && rc != -ENOSYS) { |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2622 | error_propagate(errp, err); |
| 2623 | goto fail; |
| 2624 | } |
Colin Lord | 38a53d5 | 2016-06-08 13:56:28 -0400 | [diff] [blame] | 2625 | error_free(err); |
| 2626 | err = NULL; |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2627 | |
Max Reitz | 6e0abc2 | 2015-12-11 16:23:05 +0100 | [diff] [blame] | 2628 | qmp_x_blockdev_remove_medium(device, &err); |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2629 | if (err) { |
| 2630 | error_propagate(errp, err); |
| 2631 | goto fail; |
| 2632 | } |
| 2633 | |
| 2634 | qmp_blockdev_insert_anon_medium(device, medium_bs, &err); |
| 2635 | if (err) { |
| 2636 | error_propagate(errp, err); |
| 2637 | goto fail; |
| 2638 | } |
| 2639 | |
Kevin Wolf | a561499 | 2016-03-21 10:49:51 +0100 | [diff] [blame] | 2640 | blk_apply_root_state(blk, medium_bs); |
| 2641 | |
Max Reitz | de2c6c0 | 2015-10-26 21:39:13 +0100 | [diff] [blame] | 2642 | qmp_blockdev_close_tray(device, errp); |
| 2643 | |
| 2644 | fail: |
| 2645 | /* If the medium has been inserted, the device has its own reference, so |
| 2646 | * ours must be relinquished; and if it has not been inserted successfully, |
| 2647 | * the reference must be relinquished anyway */ |
| 2648 | bdrv_unref(medium_bs); |
| 2649 | } |
| 2650 | |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2651 | /* throttling disk I/O limits */ |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2652 | void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2653 | { |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 2654 | ThrottleConfig cfg; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2655 | BlockDriverState *bs; |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 2656 | BlockBackend *blk; |
Stefan Hajnoczi | b15446f | 2014-05-14 16:22:47 +0200 | [diff] [blame] | 2657 | AioContext *aio_context; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2658 | |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2659 | blk = blk_by_name(arg->device); |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 2660 | if (!blk) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 2661 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2662 | "Device '%s' not found", arg->device); |
Luiz Capitulino | 80047da | 2011-12-14 16:49:14 -0200 | [diff] [blame] | 2663 | return; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2664 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2665 | |
| 2666 | aio_context = blk_get_aio_context(blk); |
| 2667 | aio_context_acquire(aio_context); |
| 2668 | |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 2669 | bs = blk_bs(blk); |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2670 | if (!bs) { |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2671 | error_setg(errp, "Device '%s' has no medium", arg->device); |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2672 | goto out; |
| 2673 | } |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2674 | |
Alberto Garcia | 1588ab5 | 2016-02-18 12:27:00 +0200 | [diff] [blame] | 2675 | throttle_config_init(&cfg); |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2676 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; |
| 2677 | cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; |
| 2678 | cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2679 | |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2680 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; |
| 2681 | cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; |
| 2682 | cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 2683 | |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2684 | if (arg->has_bps_max) { |
| 2685 | cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 2686 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2687 | if (arg->has_bps_rd_max) { |
| 2688 | cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 2689 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2690 | if (arg->has_bps_wr_max) { |
| 2691 | cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 2692 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2693 | if (arg->has_iops_max) { |
| 2694 | cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 2695 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2696 | if (arg->has_iops_rd_max) { |
| 2697 | cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 2698 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2699 | if (arg->has_iops_wr_max) { |
| 2700 | cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 2701 | } |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 2702 | |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2703 | if (arg->has_bps_max_length) { |
| 2704 | cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; |
Alberto Garcia | dce1320 | 2016-02-18 12:27:03 +0200 | [diff] [blame] | 2705 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2706 | if (arg->has_bps_rd_max_length) { |
| 2707 | cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; |
Alberto Garcia | dce1320 | 2016-02-18 12:27:03 +0200 | [diff] [blame] | 2708 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2709 | if (arg->has_bps_wr_max_length) { |
| 2710 | cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; |
Alberto Garcia | dce1320 | 2016-02-18 12:27:03 +0200 | [diff] [blame] | 2711 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2712 | if (arg->has_iops_max_length) { |
| 2713 | cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; |
Alberto Garcia | dce1320 | 2016-02-18 12:27:03 +0200 | [diff] [blame] | 2714 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2715 | if (arg->has_iops_rd_max_length) { |
| 2716 | cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; |
Alberto Garcia | dce1320 | 2016-02-18 12:27:03 +0200 | [diff] [blame] | 2717 | } |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2718 | if (arg->has_iops_wr_max_length) { |
| 2719 | cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; |
Alberto Garcia | dce1320 | 2016-02-18 12:27:03 +0200 | [diff] [blame] | 2720 | } |
| 2721 | |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2722 | if (arg->has_iops_size) { |
| 2723 | cfg.op_size = arg->iops_size; |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 2724 | } |
Benoît Canet | cc0681c | 2013-09-02 14:14:39 +0200 | [diff] [blame] | 2725 | |
Alberto Garcia | d585108 | 2016-02-18 12:26:59 +0200 | [diff] [blame] | 2726 | if (!throttle_is_valid(&cfg, errp)) { |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2727 | goto out; |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2728 | } |
| 2729 | |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 2730 | if (throttle_enabled(&cfg)) { |
| 2731 | /* Enable I/O limits if they're not enabled yet, otherwise |
| 2732 | * just update the throttling group. */ |
Kevin Wolf | 9714807 | 2016-03-21 13:53:52 +0100 | [diff] [blame] | 2733 | if (!blk_get_public(blk)->throttle_state) { |
Eric Blake | 4dc9397 | 2016-07-13 21:50:21 -0600 | [diff] [blame] | 2734 | blk_io_limits_enable(blk, |
| 2735 | arg->has_group ? arg->group : arg->device); |
| 2736 | } else if (arg->has_group) { |
| 2737 | blk_io_limits_update_group(blk, arg->group); |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 2738 | } |
| 2739 | /* Set the new throttling configuration */ |
Kevin Wolf | 9714807 | 2016-03-21 13:53:52 +0100 | [diff] [blame] | 2740 | blk_set_io_limits(blk, &cfg); |
| 2741 | } else if (blk_get_public(blk)->throttle_state) { |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 2742 | /* If all throttling settings are set to 0, disable I/O limits */ |
Kevin Wolf | 9714807 | 2016-03-21 13:53:52 +0100 | [diff] [blame] | 2743 | blk_io_limits_disable(blk); |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2744 | } |
Stefan Hajnoczi | b15446f | 2014-05-14 16:22:47 +0200 | [diff] [blame] | 2745 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2746 | out: |
Stefan Hajnoczi | b15446f | 2014-05-14 16:22:47 +0200 | [diff] [blame] | 2747 | aio_context_release(aio_context); |
Zhi Yong Wu | 727f005 | 2011-11-08 13:00:31 +0800 | [diff] [blame] | 2748 | } |
| 2749 | |
John Snow | 341ebc2 | 2015-04-17 19:49:52 -0400 | [diff] [blame] | 2750 | void qmp_block_dirty_bitmap_add(const char *node, const char *name, |
| 2751 | bool has_granularity, uint32_t granularity, |
| 2752 | Error **errp) |
| 2753 | { |
| 2754 | AioContext *aio_context; |
| 2755 | BlockDriverState *bs; |
| 2756 | |
| 2757 | if (!name || name[0] == '\0') { |
| 2758 | error_setg(errp, "Bitmap name cannot be empty"); |
| 2759 | return; |
| 2760 | } |
| 2761 | |
| 2762 | bs = bdrv_lookup_bs(node, node, errp); |
| 2763 | if (!bs) { |
| 2764 | return; |
| 2765 | } |
| 2766 | |
| 2767 | aio_context = bdrv_get_aio_context(bs); |
| 2768 | aio_context_acquire(aio_context); |
| 2769 | |
| 2770 | if (has_granularity) { |
| 2771 | if (granularity < 512 || !is_power_of_2(granularity)) { |
| 2772 | error_setg(errp, "Granularity must be power of 2 " |
| 2773 | "and at least 512"); |
| 2774 | goto out; |
| 2775 | } |
| 2776 | } else { |
| 2777 | /* Default to cluster size, if available: */ |
| 2778 | granularity = bdrv_get_default_bitmap_granularity(bs); |
| 2779 | } |
| 2780 | |
| 2781 | bdrv_create_dirty_bitmap(bs, granularity, name, errp); |
| 2782 | |
| 2783 | out: |
| 2784 | aio_context_release(aio_context); |
| 2785 | } |
| 2786 | |
| 2787 | void qmp_block_dirty_bitmap_remove(const char *node, const char *name, |
| 2788 | Error **errp) |
| 2789 | { |
| 2790 | AioContext *aio_context; |
| 2791 | BlockDriverState *bs; |
| 2792 | BdrvDirtyBitmap *bitmap; |
| 2793 | |
| 2794 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); |
| 2795 | if (!bitmap || !bs) { |
| 2796 | return; |
| 2797 | } |
| 2798 | |
John Snow | 9bd2b08 | 2015-04-17 19:49:57 -0400 | [diff] [blame] | 2799 | if (bdrv_dirty_bitmap_frozen(bitmap)) { |
| 2800 | error_setg(errp, |
| 2801 | "Bitmap '%s' is currently frozen and cannot be removed", |
| 2802 | name); |
| 2803 | goto out; |
| 2804 | } |
John Snow | 20dca81 | 2015-04-17 19:50:02 -0400 | [diff] [blame] | 2805 | bdrv_dirty_bitmap_make_anon(bitmap); |
John Snow | 341ebc2 | 2015-04-17 19:49:52 -0400 | [diff] [blame] | 2806 | bdrv_release_dirty_bitmap(bs, bitmap); |
| 2807 | |
John Snow | 9bd2b08 | 2015-04-17 19:49:57 -0400 | [diff] [blame] | 2808 | out: |
John Snow | 341ebc2 | 2015-04-17 19:49:52 -0400 | [diff] [blame] | 2809 | aio_context_release(aio_context); |
| 2810 | } |
| 2811 | |
John Snow | e74e6b7 | 2015-04-17 19:49:59 -0400 | [diff] [blame] | 2812 | /** |
| 2813 | * Completely clear a bitmap, for the purposes of synchronizing a bitmap |
| 2814 | * immediately after a full backup operation. |
| 2815 | */ |
| 2816 | void qmp_block_dirty_bitmap_clear(const char *node, const char *name, |
| 2817 | Error **errp) |
| 2818 | { |
| 2819 | AioContext *aio_context; |
| 2820 | BdrvDirtyBitmap *bitmap; |
| 2821 | BlockDriverState *bs; |
| 2822 | |
| 2823 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); |
| 2824 | if (!bitmap || !bs) { |
| 2825 | return; |
| 2826 | } |
| 2827 | |
| 2828 | if (bdrv_dirty_bitmap_frozen(bitmap)) { |
| 2829 | error_setg(errp, |
| 2830 | "Bitmap '%s' is currently frozen and cannot be modified", |
| 2831 | name); |
| 2832 | goto out; |
| 2833 | } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { |
| 2834 | error_setg(errp, |
| 2835 | "Bitmap '%s' is currently disabled and cannot be cleared", |
| 2836 | name); |
| 2837 | goto out; |
| 2838 | } |
| 2839 | |
Fam Zheng | df9a681 | 2015-11-09 18:16:54 +0800 | [diff] [blame] | 2840 | bdrv_clear_dirty_bitmap(bitmap, NULL); |
John Snow | e74e6b7 | 2015-04-17 19:49:59 -0400 | [diff] [blame] | 2841 | |
| 2842 | out: |
| 2843 | aio_context_release(aio_context); |
| 2844 | } |
| 2845 | |
Markus Armbruster | 072ebe6 | 2015-03-05 17:00:56 +0100 | [diff] [blame] | 2846 | void hmp_drive_del(Monitor *mon, const QDict *qdict) |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2847 | { |
| 2848 | const char *id = qdict_get_str(qdict, "id"); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2849 | BlockBackend *blk; |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2850 | BlockDriverState *bs; |
Stefan Hajnoczi | 8ad4202 | 2014-08-18 16:07:12 +0100 | [diff] [blame] | 2851 | AioContext *aio_context; |
Fam Zheng | 3718d8a | 2014-05-23 21:29:43 +0800 | [diff] [blame] | 2852 | Error *local_err = NULL; |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2853 | |
Kevin Wolf | 2073d41 | 2016-02-23 17:50:37 +0100 | [diff] [blame] | 2854 | bs = bdrv_find_node(id); |
| 2855 | if (bs) { |
| 2856 | qmp_x_blockdev_del(false, NULL, true, id, &local_err); |
| 2857 | if (local_err) { |
| 2858 | error_report_err(local_err); |
| 2859 | } |
| 2860 | return; |
| 2861 | } |
| 2862 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2863 | blk = blk_by_name(id); |
| 2864 | if (!blk) { |
Markus Armbruster | b1422f2 | 2014-05-16 11:00:09 +0200 | [diff] [blame] | 2865 | error_report("Device '%s' not found", id); |
Markus Armbruster | 072ebe6 | 2015-03-05 17:00:56 +0100 | [diff] [blame] | 2866 | return; |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2867 | } |
Stefan Hajnoczi | 8ad4202 | 2014-08-18 16:07:12 +0100 | [diff] [blame] | 2868 | |
Markus Armbruster | 26f8b3a | 2014-10-07 13:59:22 +0200 | [diff] [blame] | 2869 | if (!blk_legacy_dinfo(blk)) { |
Markus Armbruster | 48f364d | 2014-09-11 16:45:39 +0200 | [diff] [blame] | 2870 | error_report("Deleting device added with blockdev-add" |
| 2871 | " is not supported"); |
Markus Armbruster | 072ebe6 | 2015-03-05 17:00:56 +0100 | [diff] [blame] | 2872 | return; |
Markus Armbruster | 48f364d | 2014-09-11 16:45:39 +0200 | [diff] [blame] | 2873 | } |
| 2874 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2875 | aio_context = blk_get_aio_context(blk); |
Stefan Hajnoczi | 8ad4202 | 2014-08-18 16:07:12 +0100 | [diff] [blame] | 2876 | aio_context_acquire(aio_context); |
| 2877 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2878 | bs = blk_bs(blk); |
| 2879 | if (bs) { |
| 2880 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { |
| 2881 | error_report_err(local_err); |
| 2882 | aio_context_release(aio_context); |
| 2883 | return; |
| 2884 | } |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2885 | |
Max Reitz | 938abd4 | 2016-01-29 16:36:09 +0100 | [diff] [blame] | 2886 | blk_remove_bs(blk); |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 2887 | } |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2888 | |
Max Reitz | 7c73587 | 2016-03-16 19:54:39 +0100 | [diff] [blame] | 2889 | /* Make the BlockBackend and the attached BlockDriverState anonymous */ |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2890 | monitor_remove_blk(blk); |
| 2891 | |
Max Reitz | 7c73587 | 2016-03-16 19:54:39 +0100 | [diff] [blame] | 2892 | /* If this BlockBackend has a device attached to it, its refcount will be |
| 2893 | * decremented when the device is removed; otherwise we have to do so here. |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 2894 | */ |
Markus Armbruster | a7f53e2 | 2014-10-07 13:59:25 +0200 | [diff] [blame] | 2895 | if (blk_get_attached_dev(blk)) { |
Stefan Hajnoczi | 293c51a | 2013-06-05 10:33:14 +0200 | [diff] [blame] | 2896 | /* Further I/O must not pause the guest */ |
Max Reitz | 373340b | 2015-10-19 17:53:22 +0200 | [diff] [blame] | 2897 | blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, |
| 2898 | BLOCKDEV_ON_ERROR_REPORT); |
Ryan Harper | d22b2f4 | 2011-03-29 20:51:47 -0500 | [diff] [blame] | 2899 | } else { |
Markus Armbruster | b9fe8a7 | 2014-10-07 13:59:09 +0200 | [diff] [blame] | 2900 | blk_unref(blk); |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2901 | } |
| 2902 | |
Stefan Hajnoczi | 8ad4202 | 2014-08-18 16:07:12 +0100 | [diff] [blame] | 2903 | aio_context_release(aio_context); |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 2904 | } |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2905 | |
Benoît Canet | 3b1dbd1 | 2014-01-23 21:31:37 +0100 | [diff] [blame] | 2906 | void qmp_block_resize(bool has_device, const char *device, |
| 2907 | bool has_node_name, const char *node_name, |
| 2908 | int64_t size, Error **errp) |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2909 | { |
Benoît Canet | 3b1dbd1 | 2014-01-23 21:31:37 +0100 | [diff] [blame] | 2910 | Error *local_err = NULL; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2911 | BlockDriverState *bs; |
Stefan Hajnoczi | 927e0e7 | 2014-08-18 14:52:28 +0100 | [diff] [blame] | 2912 | AioContext *aio_context; |
Kevin Wolf | 8732901 | 2013-05-02 15:32:55 +0200 | [diff] [blame] | 2913 | int ret; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2914 | |
Benoît Canet | 3b1dbd1 | 2014-01-23 21:31:37 +0100 | [diff] [blame] | 2915 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
| 2916 | has_node_name ? node_name : NULL, |
| 2917 | &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2918 | if (local_err) { |
Benoît Canet | 3b1dbd1 | 2014-01-23 21:31:37 +0100 | [diff] [blame] | 2919 | error_propagate(errp, local_err); |
| 2920 | return; |
| 2921 | } |
| 2922 | |
Stefan Hajnoczi | 927e0e7 | 2014-08-18 14:52:28 +0100 | [diff] [blame] | 2923 | aio_context = bdrv_get_aio_context(bs); |
| 2924 | aio_context_acquire(aio_context); |
| 2925 | |
Benoît Canet | 3b1dbd1 | 2014-01-23 21:31:37 +0100 | [diff] [blame] | 2926 | if (!bdrv_is_first_non_filter(bs)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2927 | error_setg(errp, QERR_FEATURE_DISABLED, "resize"); |
Stefan Hajnoczi | 927e0e7 | 2014-08-18 14:52:28 +0100 | [diff] [blame] | 2928 | goto out; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | if (size < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2932 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
Stefan Hajnoczi | 927e0e7 | 2014-08-18 14:52:28 +0100 | [diff] [blame] | 2933 | goto out; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2934 | } |
| 2935 | |
Jeff Cody | 9c75e16 | 2014-06-25 16:55:30 -0400 | [diff] [blame] | 2936 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2937 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
Stefan Hajnoczi | 927e0e7 | 2014-08-18 14:52:28 +0100 | [diff] [blame] | 2938 | goto out; |
Jeff Cody | 9c75e16 | 2014-06-25 16:55:30 -0400 | [diff] [blame] | 2939 | } |
| 2940 | |
Peter Lieven | 92b7a08 | 2013-03-11 11:04:24 +0100 | [diff] [blame] | 2941 | /* complete all in-flight operations before resizing the device */ |
| 2942 | bdrv_drain_all(); |
| 2943 | |
Kevin Wolf | 8732901 | 2013-05-02 15:32:55 +0200 | [diff] [blame] | 2944 | ret = bdrv_truncate(bs, size); |
| 2945 | switch (ret) { |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 2946 | case 0: |
| 2947 | break; |
| 2948 | case -ENOMEDIUM: |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2949 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 2950 | break; |
| 2951 | case -ENOTSUP: |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2952 | error_setg(errp, QERR_UNSUPPORTED); |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 2953 | break; |
| 2954 | case -EACCES: |
Alberto Garcia | 81e5f78 | 2015-04-08 12:29:19 +0300 | [diff] [blame] | 2955 | error_setg(errp, "Device '%s' is read only", device); |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 2956 | break; |
| 2957 | case -EBUSY: |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2958 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 2959 | break; |
| 2960 | default: |
Kevin Wolf | 8732901 | 2013-05-02 15:32:55 +0200 | [diff] [blame] | 2961 | error_setg_errno(errp, -ret, "Could not resize"); |
Stefan Hajnoczi | 939a1cc | 2012-01-04 22:23:34 +0000 | [diff] [blame] | 2962 | break; |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2963 | } |
Stefan Hajnoczi | 927e0e7 | 2014-08-18 14:52:28 +0100 | [diff] [blame] | 2964 | |
| 2965 | out: |
| 2966 | aio_context_release(aio_context); |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame] | 2967 | } |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2968 | |
Jeff Cody | 9abf2db | 2012-09-27 13:29:14 -0400 | [diff] [blame] | 2969 | static void block_job_cb(void *opaque, int ret) |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2970 | { |
Stefan Hajnoczi | 723c5d9 | 2014-10-21 12:03:53 +0100 | [diff] [blame] | 2971 | /* Note that this function may be executed from another AioContext besides |
| 2972 | * the QEMU main loop. If you need to access anything that assumes the |
| 2973 | * QEMU global mutex, use a BH or introduce a mutex. |
| 2974 | */ |
| 2975 | |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2976 | BlockDriverState *bs = opaque; |
Wenchao Xia | bcada37b | 2014-06-18 08:43:47 +0200 | [diff] [blame] | 2977 | const char *msg = NULL; |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2978 | |
Jeff Cody | 9abf2db | 2012-09-27 13:29:14 -0400 | [diff] [blame] | 2979 | trace_block_job_cb(bs, bs->job, ret); |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2980 | |
| 2981 | assert(bs->job); |
Wenchao Xia | bcada37b | 2014-06-18 08:43:47 +0200 | [diff] [blame] | 2982 | |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2983 | if (ret < 0) { |
Wenchao Xia | bcada37b | 2014-06-18 08:43:47 +0200 | [diff] [blame] | 2984 | msg = strerror(-ret); |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 2987 | if (block_job_is_cancelled(bs->job)) { |
Wenchao Xia | bcada37b | 2014-06-18 08:43:47 +0200 | [diff] [blame] | 2988 | block_job_event_cancelled(bs->job); |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 2989 | } else { |
Wenchao Xia | bcada37b | 2014-06-18 08:43:47 +0200 | [diff] [blame] | 2990 | block_job_event_completed(bs->job, msg); |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 2991 | } |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Alberto Garcia | 2323322 | 2016-07-05 17:28:59 +0300 | [diff] [blame] | 2994 | void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 2995 | bool has_base, const char *base, |
| 2996 | bool has_backing_file, const char *backing_file, |
| 2997 | bool has_speed, int64_t speed, |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 2998 | bool has_on_error, BlockdevOnError on_error, |
| 2999 | Error **errp) |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 3000 | { |
| 3001 | BlockDriverState *bs; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 3002 | BlockDriverState *base_bs = NULL; |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3003 | AioContext *aio_context; |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 3004 | Error *local_err = NULL; |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 3005 | const char *base_name = NULL; |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 3006 | |
Paolo Bonzini | 1d80909 | 2012-09-28 17:22:59 +0200 | [diff] [blame] | 3007 | if (!has_on_error) { |
| 3008 | on_error = BLOCKDEV_ON_ERROR_REPORT; |
| 3009 | } |
| 3010 | |
Kevin Wolf | b6c1bae | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 3011 | bs = qmp_get_root_bs(device, errp); |
| 3012 | if (!bs) { |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 3013 | return; |
| 3014 | } |
| 3015 | |
Kevin Wolf | b6c1bae | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 3016 | aio_context = bdrv_get_aio_context(bs); |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3017 | aio_context_acquire(aio_context); |
| 3018 | |
Fam Zheng | 628ff68 | 2014-05-23 21:29:44 +0800 | [diff] [blame] | 3019 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3020 | goto out; |
Fam Zheng | 628ff68 | 2014-05-23 21:29:44 +0800 | [diff] [blame] | 3021 | } |
| 3022 | |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 3023 | if (has_base) { |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 3024 | base_bs = bdrv_find_backing_image(bs, base); |
| 3025 | if (base_bs == NULL) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3026 | error_setg(errp, QERR_BASE_NOT_FOUND, base); |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3027 | goto out; |
Marcelo Tosatti | c8c3080 | 2012-01-18 14:40:53 +0000 | [diff] [blame] | 3028 | } |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3029 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 3030 | base_name = base; |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 3033 | /* if we are streaming the entire chain, the result will have no backing |
| 3034 | * file, and specifying one is therefore an error */ |
| 3035 | if (base_bs == NULL && has_backing_file) { |
| 3036 | error_setg(errp, "backing file specified, but streaming the " |
| 3037 | "entire chain"); |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3038 | goto out; |
Jeff Cody | 13d8cc5 | 2014-06-25 15:40:11 -0400 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | /* backing_file string overrides base bs filename */ |
| 3042 | base_name = has_backing_file ? backing_file : base_name; |
| 3043 | |
Alberto Garcia | 2323322 | 2016-07-05 17:28:59 +0300 | [diff] [blame] | 3044 | stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, |
| 3045 | has_speed ? speed : 0, on_error, block_job_cb, bs, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3046 | if (local_err) { |
Stefan Hajnoczi | fd7f8c6 | 2012-04-25 16:51:00 +0100 | [diff] [blame] | 3047 | error_propagate(errp, local_err); |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3048 | goto out; |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | trace_qmp_block_stream(bs, bs->job); |
Stefan Hajnoczi | f3e69be | 2014-10-21 12:03:57 +0100 | [diff] [blame] | 3052 | |
| 3053 | out: |
| 3054 | aio_context_release(aio_context); |
Stefan Hajnoczi | 12bd451 | 2012-01-18 14:40:46 +0000 | [diff] [blame] | 3055 | } |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3056 | |
Alberto Garcia | fd62c60 | 2016-07-05 17:29:00 +0300 | [diff] [blame] | 3057 | void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 3058 | bool has_base, const char *base, |
| 3059 | bool has_top, const char *top, |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 3060 | bool has_backing_file, const char *backing_file, |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3061 | bool has_speed, int64_t speed, |
| 3062 | Error **errp) |
| 3063 | { |
| 3064 | BlockDriverState *bs; |
| 3065 | BlockDriverState *base_bs, *top_bs; |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3066 | AioContext *aio_context; |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3067 | Error *local_err = NULL; |
| 3068 | /* This will be part of the QMP command, if/when the |
| 3069 | * BlockdevOnError change for blkmirror makes it in |
| 3070 | */ |
Paolo Bonzini | 92aa5c6 | 2012-09-28 17:22:55 +0200 | [diff] [blame] | 3071 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3072 | |
Max Reitz | 5450466 | 2014-04-10 19:36:25 +0200 | [diff] [blame] | 3073 | if (!has_speed) { |
| 3074 | speed = 0; |
| 3075 | } |
| 3076 | |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 3077 | /* Important Note: |
| 3078 | * libvirt relies on the DeviceNotFound error class in order to probe for |
| 3079 | * live commit feature versions; for this to work, we must make sure to |
| 3080 | * perform the device lookup before any generic errors that may occur in a |
| 3081 | * scenario in which all optional arguments are omitted. */ |
Kevin Wolf | 1d13b16 | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 3082 | bs = qmp_get_root_bs(device, &local_err); |
| 3083 | if (!bs) { |
| 3084 | bs = bdrv_lookup_bs(device, device, NULL); |
| 3085 | if (!bs) { |
| 3086 | error_free(local_err); |
| 3087 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 3088 | "Device '%s' not found", device); |
| 3089 | } else { |
| 3090 | error_propagate(errp, local_err); |
| 3091 | } |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3092 | return; |
| 3093 | } |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3094 | |
Kevin Wolf | 1d13b16 | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 3095 | aio_context = bdrv_get_aio_context(bs); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3096 | aio_context_acquire(aio_context); |
| 3097 | |
Fam Zheng | bb00021 | 2014-09-11 13:14:00 +0800 | [diff] [blame] | 3098 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3099 | goto out; |
Fam Zheng | 628ff68 | 2014-05-23 21:29:44 +0800 | [diff] [blame] | 3100 | } |
| 3101 | |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3102 | /* default top_bs is the active layer */ |
| 3103 | top_bs = bs; |
| 3104 | |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 3105 | if (has_top && top) { |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3106 | if (strcmp(bs->filename, top) != 0) { |
| 3107 | top_bs = bdrv_find_backing_image(bs, top); |
| 3108 | } |
| 3109 | } |
| 3110 | |
| 3111 | if (top_bs == NULL) { |
| 3112 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3113 | goto out; |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3114 | } |
| 3115 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3116 | assert(bdrv_get_aio_context(top_bs) == aio_context); |
| 3117 | |
Jeff Cody | d5208c4 | 2012-10-16 15:49:10 -0400 | [diff] [blame] | 3118 | if (has_base && base) { |
| 3119 | base_bs = bdrv_find_backing_image(top_bs, base); |
| 3120 | } else { |
| 3121 | base_bs = bdrv_find_base(top_bs); |
| 3122 | } |
| 3123 | |
| 3124 | if (base_bs == NULL) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3125 | error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3126 | goto out; |
Jeff Cody | d5208c4 | 2012-10-16 15:49:10 -0400 | [diff] [blame] | 3127 | } |
| 3128 | |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3129 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
| 3130 | |
Fam Zheng | bb00021 | 2014-09-11 13:14:00 +0800 | [diff] [blame] | 3131 | if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { |
| 3132 | goto out; |
| 3133 | } |
| 3134 | |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 3135 | /* Do not allow attempts to commit an image into itself */ |
| 3136 | if (top_bs == base_bs) { |
| 3137 | error_setg(errp, "cannot commit an image into itself"); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3138 | goto out; |
Jeff Cody | 7676e2c | 2014-06-30 15:14:15 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
Fam Zheng | 20a63d2 | 2013-12-16 14:45:31 +0800 | [diff] [blame] | 3141 | if (top_bs == bs) { |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 3142 | if (has_backing_file) { |
| 3143 | error_setg(errp, "'backing-file' specified," |
| 3144 | " but 'top' is the active layer"); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3145 | goto out; |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 3146 | } |
Alberto Garcia | fd62c60 | 2016-07-05 17:29:00 +0300 | [diff] [blame] | 3147 | commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, speed, |
| 3148 | on_error, block_job_cb, bs, &local_err); |
Fam Zheng | 20a63d2 | 2013-12-16 14:45:31 +0800 | [diff] [blame] | 3149 | } else { |
Alberto Garcia | fd62c60 | 2016-07-05 17:29:00 +0300 | [diff] [blame] | 3150 | commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, |
| 3151 | on_error, block_job_cb, bs, |
Jeff Cody | 54e2690 | 2014-06-25 15:40:10 -0400 | [diff] [blame] | 3152 | has_backing_file ? backing_file : NULL, &local_err); |
Fam Zheng | 20a63d2 | 2013-12-16 14:45:31 +0800 | [diff] [blame] | 3153 | } |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3154 | if (local_err != NULL) { |
| 3155 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3156 | goto out; |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3157 | } |
Stefan Hajnoczi | 9e85cd5 | 2014-10-21 12:03:59 +0100 | [diff] [blame] | 3158 | |
| 3159 | out: |
| 3160 | aio_context_release(aio_context); |
Jeff Cody | ed61fc1 | 2012-09-27 13:29:16 -0400 | [diff] [blame] | 3161 | } |
| 3162 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3163 | static void do_drive_backup(const char *job_id, const char *device, |
| 3164 | const char *target, bool has_format, |
| 3165 | const char *format, enum MirrorSyncMode sync, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3166 | bool has_mode, enum NewImageMode mode, |
| 3167 | bool has_speed, int64_t speed, |
| 3168 | bool has_bitmap, const char *bitmap, |
| 3169 | bool has_on_source_error, |
| 3170 | BlockdevOnError on_source_error, |
| 3171 | bool has_on_target_error, |
| 3172 | BlockdevOnError on_target_error, |
| 3173 | BlockJobTxn *txn, Error **errp) |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3174 | { |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 3175 | BlockBackend *blk; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3176 | BlockDriverState *bs; |
| 3177 | BlockDriverState *target_bs; |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 3178 | BlockDriverState *source = NULL; |
John Snow | d58d845 | 2015-04-17 19:49:58 -0400 | [diff] [blame] | 3179 | BdrvDirtyBitmap *bmap = NULL; |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3180 | AioContext *aio_context; |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3181 | QDict *options = NULL; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3182 | Error *local_err = NULL; |
| 3183 | int flags; |
| 3184 | int64_t size; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3185 | |
| 3186 | if (!has_speed) { |
| 3187 | speed = 0; |
| 3188 | } |
| 3189 | if (!has_on_source_error) { |
| 3190 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; |
| 3191 | } |
| 3192 | if (!has_on_target_error) { |
| 3193 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; |
| 3194 | } |
| 3195 | if (!has_mode) { |
| 3196 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; |
| 3197 | } |
| 3198 | |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 3199 | blk = blk_by_name(device); |
| 3200 | if (!blk) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 3201 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 3202 | "Device '%s' not found", device); |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3203 | return; |
| 3204 | } |
| 3205 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3206 | aio_context = blk_get_aio_context(blk); |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3207 | aio_context_acquire(aio_context); |
| 3208 | |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3209 | /* Although backup_run has this check too, we need to use bs->drv below, so |
| 3210 | * do an early check redundantly. */ |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3211 | if (!blk_is_available(blk)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3212 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3213 | goto out; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3214 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3215 | bs = blk_bs(blk); |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3216 | |
| 3217 | if (!has_format) { |
| 3218 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; |
| 3219 | } |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3220 | |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3221 | /* Early check to avoid creating target */ |
Fam Zheng | 3718d8a | 2014-05-23 21:29:43 +0800 | [diff] [blame] | 3222 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3223 | goto out; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3224 | } |
| 3225 | |
Kevin Wolf | 61de4c6 | 2016-03-18 17:46:45 +0100 | [diff] [blame] | 3226 | flags = bs->open_flags | BDRV_O_RDWR; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3227 | |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 3228 | /* See if we have a backing HD we can use to create our new image |
| 3229 | * on top of. */ |
| 3230 | if (sync == MIRROR_SYNC_MODE_TOP) { |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 3231 | source = backing_bs(bs); |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 3232 | if (!source) { |
| 3233 | sync = MIRROR_SYNC_MODE_FULL; |
| 3234 | } |
| 3235 | } |
| 3236 | if (sync == MIRROR_SYNC_MODE_NONE) { |
| 3237 | source = bs; |
| 3238 | } |
| 3239 | |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3240 | size = bdrv_getlength(bs); |
| 3241 | if (size < 0) { |
| 3242 | error_setg_errno(errp, -size, "bdrv_getlength failed"); |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3243 | goto out; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | if (mode != NEW_IMAGE_MODE_EXISTING) { |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3247 | assert(format); |
Ian Main | fc5d3f8 | 2013-07-26 11:39:04 -0700 | [diff] [blame] | 3248 | if (source) { |
| 3249 | bdrv_img_create(target, format, source->filename, |
| 3250 | source->drv->format_name, NULL, |
| 3251 | size, flags, &local_err, false); |
| 3252 | } else { |
| 3253 | bdrv_img_create(target, format, NULL, NULL, NULL, |
| 3254 | size, flags, &local_err, false); |
| 3255 | } |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3256 | } |
| 3257 | |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3258 | if (local_err) { |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3259 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3260 | goto out; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3261 | } |
| 3262 | |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3263 | if (format) { |
| 3264 | options = qdict_new(); |
| 3265 | qdict_put(options, "driver", qstring_from_str(format)); |
| 3266 | } |
| 3267 | |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 3268 | target_bs = bdrv_open(target, NULL, options, flags, errp); |
| 3269 | if (!target_bs) { |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3270 | goto out; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3271 | } |
| 3272 | |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3273 | bdrv_set_aio_context(target_bs, aio_context); |
| 3274 | |
John Snow | d58d845 | 2015-04-17 19:49:58 -0400 | [diff] [blame] | 3275 | if (has_bitmap) { |
| 3276 | bmap = bdrv_find_dirty_bitmap(bs, bitmap); |
| 3277 | if (!bmap) { |
| 3278 | error_setg(errp, "Bitmap '%s' could not be found", bitmap); |
Max Reitz | 0702d3d | 2015-11-09 23:39:10 +0100 | [diff] [blame] | 3279 | bdrv_unref(target_bs); |
John Snow | d58d845 | 2015-04-17 19:49:58 -0400 | [diff] [blame] | 3280 | goto out; |
| 3281 | } |
| 3282 | } |
| 3283 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3284 | backup_start(job_id, bs, target_bs, speed, sync, bmap, |
John Snow | d58d845 | 2015-04-17 19:49:58 -0400 | [diff] [blame] | 3285 | on_source_error, on_target_error, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3286 | block_job_cb, bs, txn, &local_err); |
Kevin Wolf | 5c438bc | 2016-04-14 13:09:53 +0200 | [diff] [blame] | 3287 | bdrv_unref(target_bs); |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3288 | if (local_err != NULL) { |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3289 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3290 | goto out; |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3291 | } |
Stefan Hajnoczi | 761731b | 2014-10-21 12:03:56 +0100 | [diff] [blame] | 3292 | |
| 3293 | out: |
| 3294 | aio_context_release(aio_context); |
Stefan Hajnoczi | 99a9add | 2013-06-24 17:13:14 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3297 | void qmp_drive_backup(bool has_job_id, const char *job_id, |
| 3298 | const char *device, const char *target, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3299 | bool has_format, const char *format, |
| 3300 | enum MirrorSyncMode sync, |
| 3301 | bool has_mode, enum NewImageMode mode, |
| 3302 | bool has_speed, int64_t speed, |
| 3303 | bool has_bitmap, const char *bitmap, |
| 3304 | bool has_on_source_error, BlockdevOnError on_source_error, |
| 3305 | bool has_on_target_error, BlockdevOnError on_target_error, |
| 3306 | Error **errp) |
| 3307 | { |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3308 | return do_drive_backup(has_job_id ? job_id : NULL, device, target, |
| 3309 | has_format, format, sync, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3310 | has_mode, mode, has_speed, speed, |
| 3311 | has_bitmap, bitmap, |
| 3312 | has_on_source_error, on_source_error, |
| 3313 | has_on_target_error, on_target_error, |
| 3314 | NULL, errp); |
| 3315 | } |
| 3316 | |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 3317 | BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) |
| 3318 | { |
Alberto Garcia | d5a8ee6 | 2015-04-17 14:52:43 +0300 | [diff] [blame] | 3319 | return bdrv_named_nodes_list(errp); |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 3320 | } |
| 3321 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3322 | void do_blockdev_backup(const char *job_id, const char *device, |
| 3323 | const char *target, enum MirrorSyncMode sync, |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3324 | bool has_speed, int64_t speed, |
| 3325 | bool has_on_source_error, |
| 3326 | BlockdevOnError on_source_error, |
| 3327 | bool has_on_target_error, |
| 3328 | BlockdevOnError on_target_error, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3329 | BlockJobTxn *txn, Error **errp) |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3330 | { |
| 3331 | BlockDriverState *bs; |
| 3332 | BlockDriverState *target_bs; |
| 3333 | Error *local_err = NULL; |
| 3334 | AioContext *aio_context; |
| 3335 | |
| 3336 | if (!has_speed) { |
| 3337 | speed = 0; |
| 3338 | } |
| 3339 | if (!has_on_source_error) { |
| 3340 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; |
| 3341 | } |
| 3342 | if (!has_on_target_error) { |
| 3343 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; |
| 3344 | } |
| 3345 | |
Kevin Wolf | cef34ee | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 3346 | bs = qmp_get_root_bs(device, errp); |
| 3347 | if (!bs) { |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3348 | return; |
| 3349 | } |
| 3350 | |
Kevin Wolf | cef34ee | 2016-06-23 14:20:24 +0200 | [diff] [blame] | 3351 | aio_context = bdrv_get_aio_context(bs); |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3352 | aio_context_acquire(aio_context); |
| 3353 | |
Fam Zheng | 0d97891 | 2016-05-23 10:19:35 +0800 | [diff] [blame] | 3354 | target_bs = bdrv_lookup_bs(target, target, errp); |
| 3355 | if (!target_bs) { |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3356 | goto out; |
| 3357 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3358 | |
Fam Zheng | efd7556 | 2016-05-23 10:19:36 +0800 | [diff] [blame] | 3359 | if (bdrv_get_aio_context(target_bs) != aio_context) { |
| 3360 | if (!bdrv_has_blk(target_bs)) { |
| 3361 | /* The target BDS is not attached, we can safely move it to another |
| 3362 | * AioContext. */ |
| 3363 | bdrv_set_aio_context(target_bs, aio_context); |
| 3364 | } else { |
| 3365 | error_setg(errp, "Target is attached to a different thread from " |
| 3366 | "source."); |
| 3367 | goto out; |
| 3368 | } |
| 3369 | } |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3370 | backup_start(job_id, bs, target_bs, speed, sync, NULL, on_source_error, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3371 | on_target_error, block_job_cb, bs, txn, &local_err); |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3372 | if (local_err != NULL) { |
Fam Zheng | c29c1dd | 2014-12-18 18:37:05 +0800 | [diff] [blame] | 3373 | error_propagate(errp, local_err); |
| 3374 | } |
| 3375 | out: |
| 3376 | aio_context_release(aio_context); |
| 3377 | } |
| 3378 | |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3379 | void qmp_blockdev_backup(bool has_job_id, const char *job_id, |
| 3380 | const char *device, const char *target, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3381 | enum MirrorSyncMode sync, |
| 3382 | bool has_speed, int64_t speed, |
| 3383 | bool has_on_source_error, |
| 3384 | BlockdevOnError on_source_error, |
| 3385 | bool has_on_target_error, |
| 3386 | BlockdevOnError on_target_error, |
| 3387 | Error **errp) |
| 3388 | { |
Alberto Garcia | 70559d4 | 2016-07-05 17:28:58 +0300 | [diff] [blame] | 3389 | do_blockdev_backup(has_job_id ? job_id : NULL, device, target, |
| 3390 | sync, has_speed, speed, |
John Snow | 78f51fd | 2015-11-05 18:13:17 -0500 | [diff] [blame] | 3391 | has_on_source_error, on_source_error, |
| 3392 | has_on_target_error, on_target_error, |
| 3393 | NULL, errp); |
| 3394 | } |
| 3395 | |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3396 | /* Parameter check and block job starting for drive mirroring. |
| 3397 | * Caller should hold @device and @target's aio context (must be the same). |
| 3398 | **/ |
Alberto Garcia | 71aa986 | 2016-07-05 17:28:57 +0300 | [diff] [blame] | 3399 | static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3400 | BlockDriverState *target, |
| 3401 | bool has_replaces, const char *replaces, |
| 3402 | enum MirrorSyncMode sync, |
Max Reitz | 274fcce | 2016-06-10 20:57:47 +0200 | [diff] [blame] | 3403 | BlockMirrorBackingMode backing_mode, |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3404 | bool has_speed, int64_t speed, |
| 3405 | bool has_granularity, uint32_t granularity, |
| 3406 | bool has_buf_size, int64_t buf_size, |
| 3407 | bool has_on_source_error, |
| 3408 | BlockdevOnError on_source_error, |
| 3409 | bool has_on_target_error, |
| 3410 | BlockdevOnError on_target_error, |
| 3411 | bool has_unmap, bool unmap, |
| 3412 | Error **errp) |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3413 | { |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3414 | |
| 3415 | if (!has_speed) { |
| 3416 | speed = 0; |
| 3417 | } |
Paolo Bonzini | b952b55 | 2012-10-18 16:49:28 +0200 | [diff] [blame] | 3418 | if (!has_on_source_error) { |
| 3419 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; |
| 3420 | } |
| 3421 | if (!has_on_target_error) { |
| 3422 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; |
| 3423 | } |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 3424 | if (!has_granularity) { |
| 3425 | granularity = 0; |
| 3426 | } |
Paolo Bonzini | 08e4ed6 | 2013-01-22 09:03:13 +0100 | [diff] [blame] | 3427 | if (!has_buf_size) { |
Wen Congyang | 48ac0a4 | 2015-05-15 15:51:36 +0800 | [diff] [blame] | 3428 | buf_size = 0; |
Paolo Bonzini | 08e4ed6 | 2013-01-22 09:03:13 +0100 | [diff] [blame] | 3429 | } |
Fam Zheng | 0fc9f8e | 2015-06-08 13:56:08 +0800 | [diff] [blame] | 3430 | if (!has_unmap) { |
| 3431 | unmap = true; |
| 3432 | } |
Paolo Bonzini | 08e4ed6 | 2013-01-22 09:03:13 +0100 | [diff] [blame] | 3433 | |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 3434 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3435 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
| 3436 | "a value in range [512B, 64MB]"); |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 3437 | return; |
| 3438 | } |
| 3439 | if (granularity & (granularity - 1)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3440 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
| 3441 | "power of 2"); |
Paolo Bonzini | eee13df | 2013-01-21 17:09:46 +0100 | [diff] [blame] | 3442 | return; |
| 3443 | } |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3444 | |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3445 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { |
| 3446 | return; |
| 3447 | } |
Fam Zheng | e40e502 | 2015-12-24 12:45:04 +0800 | [diff] [blame] | 3448 | if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { |
| 3449 | return; |
| 3450 | } |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3451 | |
| 3452 | if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { |
| 3453 | sync = MIRROR_SYNC_MODE_FULL; |
| 3454 | } |
| 3455 | |
| 3456 | /* pass the node name to replace to mirror start since it's loose coupling |
| 3457 | * and will allow to check whether the node still exist at mirror completion |
| 3458 | */ |
Alberto Garcia | 71aa986 | 2016-07-05 17:28:57 +0300 | [diff] [blame] | 3459 | mirror_start(job_id, bs, target, |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3460 | has_replaces ? replaces : NULL, |
Max Reitz | 274fcce | 2016-06-10 20:57:47 +0200 | [diff] [blame] | 3461 | speed, granularity, buf_size, sync, backing_mode, |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3462 | on_source_error, on_target_error, unmap, |
| 3463 | block_job_cb, bs, errp); |
| 3464 | } |
| 3465 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3466 | void qmp_drive_mirror(DriveMirror *arg, Error **errp) |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3467 | { |
| 3468 | BlockDriverState *bs; |
| 3469 | BlockBackend *blk; |
| 3470 | BlockDriverState *source, *target_bs; |
| 3471 | AioContext *aio_context; |
Max Reitz | 274fcce | 2016-06-10 20:57:47 +0200 | [diff] [blame] | 3472 | BlockMirrorBackingMode backing_mode; |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3473 | Error *local_err = NULL; |
| 3474 | QDict *options = NULL; |
| 3475 | int flags; |
| 3476 | int64_t size; |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3477 | const char *format = arg->format; |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3478 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3479 | blk = blk_by_name(arg->device); |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 3480 | if (!blk) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 3481 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3482 | "Device '%s' not found", arg->device); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3483 | return; |
| 3484 | } |
| 3485 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3486 | aio_context = blk_get_aio_context(blk); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3487 | aio_context_acquire(aio_context); |
| 3488 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3489 | if (!blk_is_available(blk)) { |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3490 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, arg->device); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3491 | goto out; |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3492 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3493 | bs = blk_bs(blk); |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3494 | if (!arg->has_mode) { |
| 3495 | arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3496 | } |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3497 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3498 | if (!arg->has_format) { |
| 3499 | format = (arg->mode == NEW_IMAGE_MODE_EXISTING |
| 3500 | ? NULL : bs->drv->format_name); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3501 | } |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3502 | |
Kevin Wolf | 61de4c6 | 2016-03-18 17:46:45 +0100 | [diff] [blame] | 3503 | flags = bs->open_flags | BDRV_O_RDWR; |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 3504 | source = backing_bs(bs); |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3505 | if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { |
| 3506 | arg->sync = MIRROR_SYNC_MODE_FULL; |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3507 | } |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3508 | if (arg->sync == MIRROR_SYNC_MODE_NONE) { |
Max Reitz | 117e0c8 | 2013-11-25 20:28:55 +0100 | [diff] [blame] | 3509 | source = bs; |
| 3510 | } |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3511 | |
Stefan Hajnoczi | ac3c5d8 | 2013-06-24 17:13:13 +0200 | [diff] [blame] | 3512 | size = bdrv_getlength(bs); |
| 3513 | if (size < 0) { |
| 3514 | error_setg_errno(errp, -size, "bdrv_getlength failed"); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3515 | goto out; |
Stefan Hajnoczi | ac3c5d8 | 2013-06-24 17:13:13 +0200 | [diff] [blame] | 3516 | } |
| 3517 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3518 | if (arg->has_replaces) { |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3519 | BlockDriverState *to_replace_bs; |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3520 | AioContext *replace_aio_context; |
| 3521 | int64_t replace_size; |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3522 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3523 | if (!arg->has_node_name) { |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3524 | error_setg(errp, "a node-name must be provided when replacing a" |
| 3525 | " named node of the graph"); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3526 | goto out; |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3527 | } |
| 3528 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3529 | to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3530 | |
| 3531 | if (!to_replace_bs) { |
| 3532 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3533 | goto out; |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3536 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); |
| 3537 | aio_context_acquire(replace_aio_context); |
| 3538 | replace_size = bdrv_getlength(to_replace_bs); |
| 3539 | aio_context_release(replace_aio_context); |
| 3540 | |
| 3541 | if (size != replace_size) { |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3542 | error_setg(errp, "cannot replace image with a mirror image of " |
| 3543 | "different size"); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3544 | goto out; |
Benoît Canet | 09158f0 | 2014-06-27 18:25:25 +0200 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3548 | if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { |
Max Reitz | 274fcce | 2016-06-10 20:57:47 +0200 | [diff] [blame] | 3549 | backing_mode = MIRROR_SOURCE_BACKING_CHAIN; |
| 3550 | } else { |
| 3551 | backing_mode = MIRROR_OPEN_BACKING_CHAIN; |
| 3552 | } |
| 3553 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3554 | if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) |
| 3555 | && arg->mode != NEW_IMAGE_MODE_EXISTING) |
Max Reitz | 1452686 | 2013-11-06 19:50:44 +0100 | [diff] [blame] | 3556 | { |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3557 | /* create new image w/o backing file */ |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3558 | assert(format); |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3559 | bdrv_img_create(arg->target, format, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3560 | NULL, NULL, NULL, size, flags, &local_err, false); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3561 | } else { |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3562 | switch (arg->mode) { |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3563 | case NEW_IMAGE_MODE_EXISTING: |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3564 | break; |
| 3565 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: |
| 3566 | /* create new image with backing file */ |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3567 | bdrv_img_create(arg->target, format, |
Luiz Capitulino | cf8f242 | 2012-11-30 10:52:08 -0200 | [diff] [blame] | 3568 | source->filename, |
| 3569 | source->drv->format_name, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3570 | NULL, size, flags, &local_err, false); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3571 | break; |
| 3572 | default: |
| 3573 | abort(); |
| 3574 | } |
| 3575 | } |
| 3576 | |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3577 | if (local_err) { |
Luiz Capitulino | cf8f242 | 2012-11-30 10:52:08 -0200 | [diff] [blame] | 3578 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3579 | goto out; |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3580 | } |
| 3581 | |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3582 | options = qdict_new(); |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3583 | if (arg->has_node_name) { |
| 3584 | qdict_put(options, "node-name", qstring_from_str(arg->node_name)); |
Benoît Canet | 4c828dc | 2014-06-16 12:00:55 +0200 | [diff] [blame] | 3585 | } |
Max Reitz | e664171 | 2015-08-26 19:47:48 +0200 | [diff] [blame] | 3586 | if (format) { |
| 3587 | qdict_put(options, "driver", qstring_from_str(format)); |
| 3588 | } |
Benoît Canet | 4c828dc | 2014-06-16 12:00:55 +0200 | [diff] [blame] | 3589 | |
Paolo Bonzini | b812f67 | 2013-01-21 17:09:43 +0100 | [diff] [blame] | 3590 | /* Mirroring takes care of copy-on-write using the source's backing |
| 3591 | * file. |
| 3592 | */ |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3593 | target_bs = bdrv_open(arg->target, NULL, options, |
| 3594 | flags | BDRV_O_NO_BACKING, errp); |
Max Reitz | 5b36393 | 2016-05-17 16:41:31 +0200 | [diff] [blame] | 3595 | if (!target_bs) { |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3596 | goto out; |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3597 | } |
| 3598 | |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3599 | bdrv_set_aio_context(target_bs, aio_context); |
| 3600 | |
Eric Blake | faecd40 | 2016-07-14 16:37:58 -0600 | [diff] [blame] | 3601 | blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, |
| 3602 | arg->has_replaces, arg->replaces, arg->sync, |
| 3603 | backing_mode, arg->has_speed, arg->speed, |
| 3604 | arg->has_granularity, arg->granularity, |
| 3605 | arg->has_buf_size, arg->buf_size, |
| 3606 | arg->has_on_source_error, arg->on_source_error, |
| 3607 | arg->has_on_target_error, arg->on_target_error, |
| 3608 | arg->has_unmap, arg->unmap, |
Fam Zheng | 4193cdd | 2015-12-24 12:45:03 +0800 | [diff] [blame] | 3609 | &local_err); |
Kevin Wolf | e253f4b | 2016-04-12 16:17:41 +0200 | [diff] [blame] | 3610 | bdrv_unref(target_bs); |
Eduardo Habkost | 621ff94 | 2016-06-13 18:57:56 -0300 | [diff] [blame] | 3611 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 5a7e7a0 | 2014-10-21 12:03:58 +0100 | [diff] [blame] | 3612 | out: |
| 3613 | aio_context_release(aio_context); |
Paolo Bonzini | d9b902d | 2012-10-18 16:49:24 +0200 | [diff] [blame] | 3614 | } |
| 3615 | |
Alberto Garcia | 71aa986 | 2016-07-05 17:28:57 +0300 | [diff] [blame] | 3616 | void qmp_blockdev_mirror(bool has_job_id, const char *job_id, |
| 3617 | const char *device, const char *target, |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3618 | bool has_replaces, const char *replaces, |
| 3619 | MirrorSyncMode sync, |
| 3620 | bool has_speed, int64_t speed, |
| 3621 | bool has_granularity, uint32_t granularity, |
| 3622 | bool has_buf_size, int64_t buf_size, |
| 3623 | bool has_on_source_error, |
| 3624 | BlockdevOnError on_source_error, |
| 3625 | bool has_on_target_error, |
| 3626 | BlockdevOnError on_target_error, |
| 3627 | Error **errp) |
| 3628 | { |
| 3629 | BlockDriverState *bs; |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3630 | BlockDriverState *target_bs; |
| 3631 | AioContext *aio_context; |
Max Reitz | 274fcce | 2016-06-10 20:57:47 +0200 | [diff] [blame] | 3632 | BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3633 | Error *local_err = NULL; |
| 3634 | |
Kevin Wolf | 07eec65 | 2016-06-23 14:20:24 +0200 | [diff] [blame^] | 3635 | bs = qmp_get_root_bs(device, errp); |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3636 | if (!bs) { |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3637 | return; |
| 3638 | } |
| 3639 | |
| 3640 | target_bs = bdrv_lookup_bs(target, target, errp); |
| 3641 | if (!target_bs) { |
| 3642 | return; |
| 3643 | } |
| 3644 | |
| 3645 | aio_context = bdrv_get_aio_context(bs); |
| 3646 | aio_context_acquire(aio_context); |
| 3647 | |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3648 | bdrv_set_aio_context(target_bs, aio_context); |
| 3649 | |
Alberto Garcia | 71aa986 | 2016-07-05 17:28:57 +0300 | [diff] [blame] | 3650 | blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, |
Max Reitz | 274fcce | 2016-06-10 20:57:47 +0200 | [diff] [blame] | 3651 | has_replaces, replaces, sync, backing_mode, |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3652 | has_speed, speed, |
| 3653 | has_granularity, granularity, |
| 3654 | has_buf_size, buf_size, |
| 3655 | has_on_source_error, on_source_error, |
| 3656 | has_on_target_error, on_target_error, |
| 3657 | true, true, |
| 3658 | &local_err); |
Eduardo Habkost | 621ff94 | 2016-06-13 18:57:56 -0300 | [diff] [blame] | 3659 | error_propagate(errp, local_err); |
Fam Zheng | df92562 | 2015-12-24 12:45:05 +0800 | [diff] [blame] | 3660 | |
| 3661 | aio_context_release(aio_context); |
| 3662 | } |
| 3663 | |
Alberto Garcia | 3ddf3ef | 2016-07-05 17:28:55 +0300 | [diff] [blame] | 3664 | /* Get a block job using its ID and acquire its AioContext */ |
| 3665 | static BlockJob *find_block_job(const char *id, AioContext **aio_context, |
Markus Armbruster | 24d6bff | 2015-01-29 10:36:58 +0100 | [diff] [blame] | 3666 | Error **errp) |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3667 | { |
Alberto Garcia | 3ddf3ef | 2016-07-05 17:28:55 +0300 | [diff] [blame] | 3668 | BlockJob *job; |
| 3669 | |
| 3670 | assert(id != NULL); |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3671 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3672 | *aio_context = NULL; |
| 3673 | |
Alberto Garcia | 3ddf3ef | 2016-07-05 17:28:55 +0300 | [diff] [blame] | 3674 | job = block_job_get(id); |
| 3675 | |
| 3676 | if (!job) { |
| 3677 | error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, |
| 3678 | "Block job '%s' not found", id); |
| 3679 | return NULL; |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3680 | } |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3681 | |
Alberto Garcia | 3ddf3ef | 2016-07-05 17:28:55 +0300 | [diff] [blame] | 3682 | *aio_context = blk_get_aio_context(job->blk); |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3683 | aio_context_acquire(*aio_context); |
| 3684 | |
Alberto Garcia | 3ddf3ef | 2016-07-05 17:28:55 +0300 | [diff] [blame] | 3685 | return job; |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 3688 | 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] | 3689 | { |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3690 | AioContext *aio_context; |
Markus Armbruster | 24d6bff | 2015-01-29 10:36:58 +0100 | [diff] [blame] | 3691 | BlockJob *job = find_block_job(device, &aio_context, errp); |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3692 | |
| 3693 | if (!job) { |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3694 | return; |
| 3695 | } |
| 3696 | |
Stefan Hajnoczi | 882ec7c | 2012-04-25 16:51:02 +0100 | [diff] [blame] | 3697 | block_job_set_speed(job, speed, errp); |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3698 | aio_context_release(aio_context); |
Stefan Hajnoczi | 2d47c6e | 2012-01-18 14:40:47 +0000 | [diff] [blame] | 3699 | } |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 3700 | |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3701 | void qmp_block_job_cancel(const char *device, |
| 3702 | bool has_force, bool force, Error **errp) |
| 3703 | { |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3704 | AioContext *aio_context; |
Markus Armbruster | 24d6bff | 2015-01-29 10:36:58 +0100 | [diff] [blame] | 3705 | BlockJob *job = find_block_job(device, &aio_context, errp); |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3706 | |
| 3707 | if (!job) { |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3708 | return; |
| 3709 | } |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3710 | |
| 3711 | if (!has_force) { |
| 3712 | force = false; |
| 3713 | } |
| 3714 | |
Fam Zheng | 751ebd7 | 2015-04-03 22:05:18 +0800 | [diff] [blame] | 3715 | if (job->user_paused && !force) { |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 3716 | error_setg(errp, "The block job for device '%s' is currently paused", |
| 3717 | device); |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3718 | goto out; |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | trace_qmp_block_job_cancel(job); |
| 3722 | block_job_cancel(job); |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3723 | out: |
| 3724 | aio_context_release(aio_context); |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | void qmp_block_job_pause(const char *device, Error **errp) |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 3728 | { |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3729 | AioContext *aio_context; |
Markus Armbruster | 24d6bff | 2015-01-29 10:36:58 +0100 | [diff] [blame] | 3730 | BlockJob *job = find_block_job(device, &aio_context, errp); |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 3731 | |
Fam Zheng | 751ebd7 | 2015-04-03 22:05:18 +0800 | [diff] [blame] | 3732 | if (!job || job->user_paused) { |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 3733 | return; |
| 3734 | } |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3735 | |
Fam Zheng | 751ebd7 | 2015-04-03 22:05:18 +0800 | [diff] [blame] | 3736 | job->user_paused = true; |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3737 | trace_qmp_block_job_pause(job); |
| 3738 | block_job_pause(job); |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3739 | aio_context_release(aio_context); |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | void qmp_block_job_resume(const char *device, Error **errp) |
| 3743 | { |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3744 | AioContext *aio_context; |
Markus Armbruster | 24d6bff | 2015-01-29 10:36:58 +0100 | [diff] [blame] | 3745 | BlockJob *job = find_block_job(device, &aio_context, errp); |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3746 | |
Fam Zheng | 751ebd7 | 2015-04-03 22:05:18 +0800 | [diff] [blame] | 3747 | if (!job || !job->user_paused) { |
Paolo Bonzini | 8acc72a | 2012-09-28 17:22:50 +0200 | [diff] [blame] | 3748 | return; |
| 3749 | } |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 3750 | |
Fam Zheng | 751ebd7 | 2015-04-03 22:05:18 +0800 | [diff] [blame] | 3751 | job->user_paused = false; |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3752 | trace_qmp_block_job_resume(job); |
Stefan Hajnoczi | 17bd51f | 2016-06-16 17:56:22 +0100 | [diff] [blame] | 3753 | block_job_iostatus_reset(job); |
Paolo Bonzini | 6e37fb8 | 2012-09-28 17:22:51 +0200 | [diff] [blame] | 3754 | block_job_resume(job); |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3755 | aio_context_release(aio_context); |
Stefan Hajnoczi | 370521a | 2012-01-18 14:40:48 +0000 | [diff] [blame] | 3756 | } |
Stefan Hajnoczi | fb5458c | 2012-01-18 14:40:49 +0000 | [diff] [blame] | 3757 | |
Paolo Bonzini | aeae883 | 2012-10-18 16:49:21 +0200 | [diff] [blame] | 3758 | void qmp_block_job_complete(const char *device, Error **errp) |
| 3759 | { |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3760 | AioContext *aio_context; |
Markus Armbruster | 24d6bff | 2015-01-29 10:36:58 +0100 | [diff] [blame] | 3761 | BlockJob *job = find_block_job(device, &aio_context, errp); |
Paolo Bonzini | aeae883 | 2012-10-18 16:49:21 +0200 | [diff] [blame] | 3762 | |
| 3763 | if (!job) { |
Paolo Bonzini | aeae883 | 2012-10-18 16:49:21 +0200 | [diff] [blame] | 3764 | return; |
| 3765 | } |
| 3766 | |
| 3767 | trace_qmp_block_job_complete(job); |
| 3768 | block_job_complete(job, errp); |
Stefan Hajnoczi | 3d948cd | 2014-10-21 12:03:50 +0100 | [diff] [blame] | 3769 | aio_context_release(aio_context); |
Paolo Bonzini | aeae883 | 2012-10-18 16:49:21 +0200 | [diff] [blame] | 3770 | } |
| 3771 | |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3772 | void qmp_change_backing_file(const char *device, |
| 3773 | const char *image_node_name, |
| 3774 | const char *backing_file, |
| 3775 | Error **errp) |
| 3776 | { |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 3777 | BlockBackend *blk; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3778 | BlockDriverState *bs = NULL; |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3779 | AioContext *aio_context; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3780 | BlockDriverState *image_bs = NULL; |
| 3781 | Error *local_err = NULL; |
| 3782 | bool ro; |
| 3783 | int open_flags; |
| 3784 | int ret; |
| 3785 | |
Fam Zheng | a0e8544 | 2015-03-02 19:36:48 +0800 | [diff] [blame] | 3786 | blk = blk_by_name(device); |
| 3787 | if (!blk) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 3788 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 3789 | "Device '%s' not found", device); |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3790 | return; |
| 3791 | } |
| 3792 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3793 | aio_context = blk_get_aio_context(blk); |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3794 | aio_context_acquire(aio_context); |
| 3795 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 3796 | if (!blk_is_available(blk)) { |
| 3797 | error_setg(errp, "Device '%s' has no medium", device); |
| 3798 | goto out; |
| 3799 | } |
| 3800 | bs = blk_bs(blk); |
| 3801 | |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3802 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); |
| 3803 | if (local_err) { |
| 3804 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3805 | goto out; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | if (!image_bs) { |
| 3809 | error_setg(errp, "image file not found"); |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3810 | goto out; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | if (bdrv_find_base(image_bs) == image_bs) { |
| 3814 | error_setg(errp, "not allowing backing file change on an image " |
| 3815 | "without a backing file"); |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3816 | goto out; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | /* even though we are not necessarily operating on bs, we need it to |
| 3820 | * determine if block ops are currently prohibited on the chain */ |
| 3821 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3822 | goto out; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | /* final sanity check */ |
| 3826 | if (!bdrv_chain_contains(bs, image_bs)) { |
| 3827 | error_setg(errp, "'%s' and image file are not in the same chain", |
| 3828 | device); |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3829 | goto out; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | /* if not r/w, reopen to make r/w */ |
| 3833 | open_flags = image_bs->open_flags; |
| 3834 | ro = bdrv_is_read_only(image_bs); |
| 3835 | |
| 3836 | if (ro) { |
| 3837 | bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); |
| 3838 | if (local_err) { |
| 3839 | error_propagate(errp, local_err); |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3840 | goto out; |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | ret = bdrv_change_backing_file(image_bs, backing_file, |
| 3845 | image_bs->drv ? image_bs->drv->format_name : ""); |
| 3846 | |
| 3847 | if (ret < 0) { |
| 3848 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", |
| 3849 | backing_file); |
| 3850 | /* don't exit here, so we can try to restore open flags if |
| 3851 | * appropriate */ |
| 3852 | } |
| 3853 | |
| 3854 | if (ro) { |
| 3855 | bdrv_reopen(image_bs, open_flags, &local_err); |
Eduardo Habkost | 621ff94 | 2016-06-13 18:57:56 -0300 | [diff] [blame] | 3856 | error_propagate(errp, local_err); |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3857 | } |
Stefan Hajnoczi | 729962f | 2014-11-19 14:19:45 +0000 | [diff] [blame] | 3858 | |
| 3859 | out: |
| 3860 | aio_context_release(aio_context); |
Jeff Cody | fa40e65 | 2014-07-01 09:52:16 +0200 | [diff] [blame] | 3861 | } |
| 3862 | |
Kevin Wolf | abb21ac | 2016-02-23 17:33:24 +0100 | [diff] [blame] | 3863 | void hmp_drive_add_node(Monitor *mon, const char *optstr) |
| 3864 | { |
| 3865 | QemuOpts *opts; |
| 3866 | QDict *qdict; |
| 3867 | Error *local_err = NULL; |
| 3868 | |
| 3869 | opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); |
| 3870 | if (!opts) { |
| 3871 | return; |
| 3872 | } |
| 3873 | |
| 3874 | qdict = qemu_opts_to_qdict(opts, NULL); |
| 3875 | |
| 3876 | if (!qdict_get_try_str(qdict, "node-name")) { |
Kevin Wolf | f8746fb | 2016-03-16 11:14:31 +0100 | [diff] [blame] | 3877 | QDECREF(qdict); |
Kevin Wolf | abb21ac | 2016-02-23 17:33:24 +0100 | [diff] [blame] | 3878 | error_report("'node-name' needs to be specified"); |
| 3879 | goto out; |
| 3880 | } |
| 3881 | |
| 3882 | BlockDriverState *bs = bds_tree_init(qdict, &local_err); |
| 3883 | if (!bs) { |
| 3884 | error_report_err(local_err); |
| 3885 | goto out; |
| 3886 | } |
| 3887 | |
| 3888 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); |
| 3889 | |
| 3890 | out: |
| 3891 | qemu_opts_del(opts); |
| 3892 | } |
| 3893 | |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3894 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
| 3895 | { |
Max Reitz | be4b67b | 2015-10-19 17:53:09 +0200 | [diff] [blame] | 3896 | BlockDriverState *bs; |
| 3897 | BlockBackend *blk = NULL; |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3898 | QObject *obj; |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 3899 | Visitor *v = qmp_output_visitor_new(&obj); |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3900 | QDict *qdict; |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3901 | Error *local_err = NULL; |
| 3902 | |
Markus Armbruster | 60e19e0 | 2014-06-06 14:50:58 +0200 | [diff] [blame] | 3903 | /* 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] | 3904 | * cache.direct=false instead of silently switching to aio=threads, except |
Markus Armbruster | 60e19e0 | 2014-06-06 14:50:58 +0200 | [diff] [blame] | 3905 | * when called from drive_new(). |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3906 | * |
| 3907 | * For now, simply forbidding the combination for all drivers will do. */ |
| 3908 | if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { |
Kevin Wolf | c6e0bd9 | 2014-03-06 15:47:32 +0100 | [diff] [blame] | 3909 | bool direct = options->has_cache && |
| 3910 | options->cache->has_direct && |
| 3911 | options->cache->direct; |
| 3912 | if (!direct) { |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3913 | error_setg(errp, "aio=native requires cache.direct=true"); |
| 3914 | goto fail; |
| 3915 | } |
| 3916 | } |
| 3917 | |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 3918 | visit_type_BlockdevOptions(v, NULL, &options, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3919 | if (local_err) { |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3920 | error_propagate(errp, local_err); |
| 3921 | goto fail; |
| 3922 | } |
| 3923 | |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 3924 | visit_complete(v, &obj); |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3925 | qdict = qobject_to_qdict(obj); |
| 3926 | |
| 3927 | qdict_flatten(qdict); |
| 3928 | |
Max Reitz | be4b67b | 2015-10-19 17:53:09 +0200 | [diff] [blame] | 3929 | if (options->has_id) { |
| 3930 | blk = blockdev_init(NULL, qdict, &local_err); |
| 3931 | if (local_err) { |
| 3932 | error_propagate(errp, local_err); |
| 3933 | goto fail; |
| 3934 | } |
| 3935 | |
| 3936 | bs = blk_bs(blk); |
| 3937 | } else { |
Max Reitz | be4b67b | 2015-10-19 17:53:09 +0200 | [diff] [blame] | 3938 | if (!qdict_get_try_str(qdict, "node-name")) { |
| 3939 | error_setg(errp, "'id' and/or 'node-name' need to be specified for " |
| 3940 | "the root node"); |
| 3941 | goto fail; |
| 3942 | } |
| 3943 | |
Max Reitz | bd745e2 | 2015-10-19 17:53:32 +0200 | [diff] [blame] | 3944 | bs = bds_tree_init(qdict, errp); |
| 3945 | if (!bs) { |
Max Reitz | be4b67b | 2015-10-19 17:53:09 +0200 | [diff] [blame] | 3946 | goto fail; |
| 3947 | } |
Max Reitz | 9c4218e | 2016-01-29 16:36:12 +0100 | [diff] [blame] | 3948 | |
| 3949 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
Max Reitz | be4b67b | 2015-10-19 17:53:09 +0200 | [diff] [blame] | 3952 | if (bs && bdrv_key_required(bs)) { |
| 3953 | if (blk) { |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3954 | monitor_remove_blk(blk); |
Max Reitz | be4b67b | 2015-10-19 17:53:09 +0200 | [diff] [blame] | 3955 | blk_unref(blk); |
| 3956 | } else { |
Max Reitz | 9c4218e | 2016-01-29 16:36:12 +0100 | [diff] [blame] | 3957 | QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); |
Max Reitz | be4b67b | 2015-10-19 17:53:09 +0200 | [diff] [blame] | 3958 | bdrv_unref(bs); |
| 3959 | } |
Kevin Wolf | 8ae8e90 | 2014-03-06 15:43:42 +0100 | [diff] [blame] | 3960 | error_setg(errp, "blockdev-add doesn't support encrypted devices"); |
| 3961 | goto fail; |
| 3962 | } |
| 3963 | |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3964 | fail: |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 3965 | visit_free(v); |
Kevin Wolf | d26c9a1 | 2013-09-23 15:26:03 +0200 | [diff] [blame] | 3966 | } |
| 3967 | |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 3968 | void qmp_x_blockdev_del(bool has_id, const char *id, |
| 3969 | bool has_node_name, const char *node_name, Error **errp) |
| 3970 | { |
| 3971 | AioContext *aio_context; |
| 3972 | BlockBackend *blk; |
| 3973 | BlockDriverState *bs; |
| 3974 | |
| 3975 | if (has_id && has_node_name) { |
| 3976 | error_setg(errp, "Only one of id and node-name must be specified"); |
| 3977 | return; |
| 3978 | } else if (!has_id && !has_node_name) { |
| 3979 | error_setg(errp, "No block device specified"); |
| 3980 | return; |
| 3981 | } |
| 3982 | |
| 3983 | if (has_id) { |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3984 | /* blk_by_name() never returns a BB that is not owned by the monitor */ |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 3985 | blk = blk_by_name(id); |
| 3986 | if (!blk) { |
| 3987 | error_setg(errp, "Cannot find block backend %s", id); |
| 3988 | return; |
| 3989 | } |
Paolo Bonzini | 5cf87fd | 2016-03-31 15:07:43 +0200 | [diff] [blame] | 3990 | if (blk_legacy_dinfo(blk)) { |
| 3991 | error_setg(errp, "Deleting block backend added with drive-add" |
| 3992 | " is not supported"); |
| 3993 | return; |
| 3994 | } |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 3995 | if (blk_get_refcnt(blk) > 1) { |
| 3996 | error_setg(errp, "Block backend %s is in use", id); |
| 3997 | return; |
| 3998 | } |
| 3999 | bs = blk_bs(blk); |
| 4000 | aio_context = blk_get_aio_context(blk); |
| 4001 | } else { |
Kevin Wolf | 1f0c461 | 2016-03-22 18:38:44 +0100 | [diff] [blame] | 4002 | blk = NULL; |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 4003 | bs = bdrv_find_node(node_name); |
| 4004 | if (!bs) { |
| 4005 | error_setg(errp, "Cannot find node %s", node_name); |
| 4006 | return; |
| 4007 | } |
Kevin Wolf | 1f0c461 | 2016-03-22 18:38:44 +0100 | [diff] [blame] | 4008 | if (bdrv_has_blk(bs)) { |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 4009 | error_setg(errp, "Node %s is in use by %s", |
Kevin Wolf | 1f0c461 | 2016-03-22 18:38:44 +0100 | [diff] [blame] | 4010 | node_name, bdrv_get_parent_name(bs)); |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 4011 | return; |
| 4012 | } |
| 4013 | aio_context = bdrv_get_aio_context(bs); |
| 4014 | } |
| 4015 | |
| 4016 | aio_context_acquire(aio_context); |
| 4017 | |
| 4018 | if (bs) { |
| 4019 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { |
| 4020 | goto out; |
| 4021 | } |
| 4022 | |
Max Reitz | 9c4218e | 2016-01-29 16:36:12 +0100 | [diff] [blame] | 4023 | if (!blk && !bs->monitor_list.tqe_prev) { |
| 4024 | error_setg(errp, "Node %s is not owned by the monitor", |
| 4025 | bs->node_name); |
| 4026 | goto out; |
| 4027 | } |
| 4028 | |
| 4029 | if (bs->refcnt > 1) { |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 4030 | error_setg(errp, "Block device %s is in use", |
| 4031 | bdrv_get_device_or_node_name(bs)); |
| 4032 | goto out; |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | if (blk) { |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 4037 | monitor_remove_blk(blk); |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 4038 | blk_unref(blk); |
| 4039 | } else { |
Max Reitz | 9c4218e | 2016-01-29 16:36:12 +0100 | [diff] [blame] | 4040 | QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); |
Alberto Garcia | 81b936a | 2015-11-02 16:51:55 +0200 | [diff] [blame] | 4041 | bdrv_unref(bs); |
| 4042 | } |
| 4043 | |
| 4044 | out: |
| 4045 | aio_context_release(aio_context); |
| 4046 | } |
| 4047 | |
Wen Congyang | 7f82159 | 2016-05-10 15:36:39 +0800 | [diff] [blame] | 4048 | static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, |
| 4049 | const char *child_name) |
| 4050 | { |
| 4051 | BdrvChild *child; |
| 4052 | |
| 4053 | QLIST_FOREACH(child, &parent_bs->children, next) { |
| 4054 | if (strcmp(child->name, child_name) == 0) { |
| 4055 | return child; |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | return NULL; |
| 4060 | } |
| 4061 | |
| 4062 | void qmp_x_blockdev_change(const char *parent, bool has_child, |
| 4063 | const char *child, bool has_node, |
| 4064 | const char *node, Error **errp) |
| 4065 | { |
| 4066 | BlockDriverState *parent_bs, *new_bs = NULL; |
| 4067 | BdrvChild *p_child; |
| 4068 | |
| 4069 | parent_bs = bdrv_lookup_bs(parent, parent, errp); |
| 4070 | if (!parent_bs) { |
| 4071 | return; |
| 4072 | } |
| 4073 | |
| 4074 | if (has_child == has_node) { |
| 4075 | if (has_child) { |
| 4076 | error_setg(errp, "The parameters child and node are in conflict"); |
| 4077 | } else { |
| 4078 | error_setg(errp, "Either child or node must be specified"); |
| 4079 | } |
| 4080 | return; |
| 4081 | } |
| 4082 | |
| 4083 | if (has_child) { |
| 4084 | p_child = bdrv_find_child(parent_bs, child); |
| 4085 | if (!p_child) { |
| 4086 | error_setg(errp, "Node '%s' does not have child '%s'", |
| 4087 | parent, child); |
| 4088 | return; |
| 4089 | } |
| 4090 | bdrv_del_child(parent_bs, p_child, errp); |
| 4091 | } |
| 4092 | |
| 4093 | if (has_node) { |
| 4094 | new_bs = bdrv_find_node(node); |
| 4095 | if (!new_bs) { |
| 4096 | error_setg(errp, "Node '%s' not found", node); |
| 4097 | return; |
| 4098 | } |
| 4099 | bdrv_add_child(parent_bs, new_bs, errp); |
| 4100 | } |
| 4101 | } |
| 4102 | |
Stefan Hajnoczi | fb5458c | 2012-01-18 14:40:49 +0000 | [diff] [blame] | 4103 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
| 4104 | { |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 4105 | BlockJobInfoList *head = NULL, **p_next = &head; |
Alberto Garcia | f0f55de | 2016-05-27 12:53:37 +0200 | [diff] [blame] | 4106 | BlockJob *job; |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 4107 | |
Alberto Garcia | f0f55de | 2016-05-27 12:53:37 +0200 | [diff] [blame] | 4108 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { |
| 4109 | BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); |
| 4110 | AioContext *aio_context = blk_get_aio_context(job->blk); |
Stefan Hajnoczi | 69691e7 | 2014-10-21 12:03:51 +0100 | [diff] [blame] | 4111 | |
| 4112 | aio_context_acquire(aio_context); |
Alberto Garcia | f0f55de | 2016-05-27 12:53:37 +0200 | [diff] [blame] | 4113 | elem->value = block_job_query(job); |
Stefan Hajnoczi | 69691e7 | 2014-10-21 12:03:51 +0100 | [diff] [blame] | 4114 | aio_context_release(aio_context); |
Alberto Garcia | f0f55de | 2016-05-27 12:53:37 +0200 | [diff] [blame] | 4115 | |
| 4116 | *p_next = elem; |
| 4117 | p_next = &elem->next; |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | return head; |
Stefan Hajnoczi | fb5458c | 2012-01-18 14:40:49 +0000 | [diff] [blame] | 4121 | } |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4122 | |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 4123 | QemuOptsList qemu_common_drive_opts = { |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4124 | .name = "drive", |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 4125 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4126 | .desc = { |
| 4127 | { |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4128 | .name = "snapshot", |
| 4129 | .type = QEMU_OPT_BOOL, |
| 4130 | .help = "enable/disable snapshot mode", |
| 4131 | },{ |
Paolo Bonzini | a9384af | 2013-02-08 14:06:12 +0100 | [diff] [blame] | 4132 | .name = "discard", |
| 4133 | .type = QEMU_OPT_STRING, |
| 4134 | .help = "discard operation (ignore/off, unmap/on)", |
| 4135 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4136 | .name = "aio", |
| 4137 | .type = QEMU_OPT_STRING, |
| 4138 | .help = "host AIO implementation (threads, native)", |
| 4139 | },{ |
Kevin Wolf | e4b24b4 | 2016-03-15 15:39:42 +0100 | [diff] [blame] | 4140 | .name = BDRV_OPT_CACHE_WB, |
| 4141 | .type = QEMU_OPT_BOOL, |
| 4142 | .help = "Enable writeback mode", |
| 4143 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4144 | .name = "format", |
| 4145 | .type = QEMU_OPT_STRING, |
| 4146 | .help = "disk format (raw, qcow2, ...)", |
| 4147 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4148 | .name = "rerror", |
| 4149 | .type = QEMU_OPT_STRING, |
| 4150 | .help = "read error action", |
| 4151 | },{ |
| 4152 | .name = "werror", |
| 4153 | .type = QEMU_OPT_STRING, |
| 4154 | .help = "write error action", |
| 4155 | },{ |
Kevin Wolf | 0f227a9 | 2013-07-19 20:07:29 +0200 | [diff] [blame] | 4156 | .name = "read-only", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4157 | .type = QEMU_OPT_BOOL, |
| 4158 | .help = "open drive file as read-only", |
| 4159 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 4160 | .name = "throttling.iops-total", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4161 | .type = QEMU_OPT_NUMBER, |
| 4162 | .help = "limit total I/O operations per second", |
| 4163 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 4164 | .name = "throttling.iops-read", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4165 | .type = QEMU_OPT_NUMBER, |
| 4166 | .help = "limit read operations per second", |
| 4167 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 4168 | .name = "throttling.iops-write", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4169 | .type = QEMU_OPT_NUMBER, |
| 4170 | .help = "limit write operations per second", |
| 4171 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 4172 | .name = "throttling.bps-total", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4173 | .type = QEMU_OPT_NUMBER, |
| 4174 | .help = "limit total bytes per second", |
| 4175 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 4176 | .name = "throttling.bps-read", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4177 | .type = QEMU_OPT_NUMBER, |
| 4178 | .help = "limit read bytes per second", |
| 4179 | },{ |
Kevin Wolf | 5797522 | 2013-07-17 14:41:54 +0200 | [diff] [blame] | 4180 | .name = "throttling.bps-write", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4181 | .type = QEMU_OPT_NUMBER, |
| 4182 | .help = "limit write bytes per second", |
| 4183 | },{ |
Benoît Canet | 3e9fab6 | 2013-09-02 14:14:40 +0200 | [diff] [blame] | 4184 | .name = "throttling.iops-total-max", |
| 4185 | .type = QEMU_OPT_NUMBER, |
| 4186 | .help = "I/O operations burst", |
| 4187 | },{ |
| 4188 | .name = "throttling.iops-read-max", |
| 4189 | .type = QEMU_OPT_NUMBER, |
| 4190 | .help = "I/O operations read burst", |
| 4191 | },{ |
| 4192 | .name = "throttling.iops-write-max", |
| 4193 | .type = QEMU_OPT_NUMBER, |
| 4194 | .help = "I/O operations write burst", |
| 4195 | },{ |
| 4196 | .name = "throttling.bps-total-max", |
| 4197 | .type = QEMU_OPT_NUMBER, |
| 4198 | .help = "total bytes burst", |
| 4199 | },{ |
| 4200 | .name = "throttling.bps-read-max", |
| 4201 | .type = QEMU_OPT_NUMBER, |
| 4202 | .help = "total bytes read burst", |
| 4203 | },{ |
| 4204 | .name = "throttling.bps-write-max", |
| 4205 | .type = QEMU_OPT_NUMBER, |
| 4206 | .help = "total bytes write burst", |
| 4207 | },{ |
Alberto Garcia | 8a0fc18 | 2016-02-18 12:27:02 +0200 | [diff] [blame] | 4208 | .name = "throttling.iops-total-max-length", |
| 4209 | .type = QEMU_OPT_NUMBER, |
| 4210 | .help = "length of the iops-total-max burst period, in seconds", |
| 4211 | },{ |
| 4212 | .name = "throttling.iops-read-max-length", |
| 4213 | .type = QEMU_OPT_NUMBER, |
| 4214 | .help = "length of the iops-read-max burst period, in seconds", |
| 4215 | },{ |
| 4216 | .name = "throttling.iops-write-max-length", |
| 4217 | .type = QEMU_OPT_NUMBER, |
| 4218 | .help = "length of the iops-write-max burst period, in seconds", |
| 4219 | },{ |
| 4220 | .name = "throttling.bps-total-max-length", |
| 4221 | .type = QEMU_OPT_NUMBER, |
| 4222 | .help = "length of the bps-total-max burst period, in seconds", |
| 4223 | },{ |
| 4224 | .name = "throttling.bps-read-max-length", |
| 4225 | .type = QEMU_OPT_NUMBER, |
| 4226 | .help = "length of the bps-read-max burst period, in seconds", |
| 4227 | },{ |
| 4228 | .name = "throttling.bps-write-max-length", |
| 4229 | .type = QEMU_OPT_NUMBER, |
| 4230 | .help = "length of the bps-write-max burst period, in seconds", |
| 4231 | },{ |
Benoît Canet | 2024c1d | 2013-09-02 14:14:41 +0200 | [diff] [blame] | 4232 | .name = "throttling.iops-size", |
| 4233 | .type = QEMU_OPT_NUMBER, |
| 4234 | .help = "when limiting by iops max size of an I/O in bytes", |
| 4235 | },{ |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 4236 | .name = "throttling.group", |
| 4237 | .type = QEMU_OPT_STRING, |
| 4238 | .help = "name of the block throttling group", |
| 4239 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4240 | .name = "copy-on-read", |
| 4241 | .type = QEMU_OPT_BOOL, |
| 4242 | .help = "copy read data from backing file into image file", |
Peter Lieven | 465bee1 | 2014-05-18 00:58:19 +0200 | [diff] [blame] | 4243 | },{ |
| 4244 | .name = "detect-zeroes", |
| 4245 | .type = QEMU_OPT_STRING, |
| 4246 | .help = "try to optimize zero writes (off, on, unmap)", |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 4247 | },{ |
| 4248 | .name = "stats-account-invalid", |
| 4249 | .type = QEMU_OPT_BOOL, |
| 4250 | .help = "whether to account for invalid I/O operations " |
| 4251 | "in the statistics", |
| 4252 | },{ |
| 4253 | .name = "stats-account-failed", |
| 4254 | .type = QEMU_OPT_BOOL, |
| 4255 | .help = "whether to account for failed I/O operations " |
| 4256 | "in the statistics", |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4257 | }, |
| 4258 | { /* end of list */ } |
| 4259 | }, |
| 4260 | }; |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 4261 | |
Max Reitz | bd745e2 | 2015-10-19 17:53:32 +0200 | [diff] [blame] | 4262 | static QemuOptsList qemu_root_bds_opts = { |
| 4263 | .name = "root-bds", |
Kevin Wolf | 23f7fcb | 2016-03-15 14:43:14 +0100 | [diff] [blame] | 4264 | .head = QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts.head), |
Max Reitz | bd745e2 | 2015-10-19 17:53:32 +0200 | [diff] [blame] | 4265 | .desc = { |
| 4266 | { |
| 4267 | .name = "discard", |
| 4268 | .type = QEMU_OPT_STRING, |
| 4269 | .help = "discard operation (ignore/off, unmap/on)", |
| 4270 | },{ |
Max Reitz | bd745e2 | 2015-10-19 17:53:32 +0200 | [diff] [blame] | 4271 | .name = "aio", |
| 4272 | .type = QEMU_OPT_STRING, |
| 4273 | .help = "host AIO implementation (threads, native)", |
| 4274 | },{ |
| 4275 | .name = "read-only", |
| 4276 | .type = QEMU_OPT_BOOL, |
| 4277 | .help = "open drive file as read-only", |
| 4278 | },{ |
| 4279 | .name = "copy-on-read", |
| 4280 | .type = QEMU_OPT_BOOL, |
| 4281 | .help = "copy read data from backing file into image file", |
| 4282 | },{ |
| 4283 | .name = "detect-zeroes", |
| 4284 | .type = QEMU_OPT_STRING, |
| 4285 | .help = "try to optimize zero writes (off, on, unmap)", |
| 4286 | }, |
| 4287 | { /* end of list */ } |
| 4288 | }, |
| 4289 | }; |
| 4290 | |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 4291 | QemuOptsList qemu_drive_opts = { |
| 4292 | .name = "drive", |
| 4293 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), |
| 4294 | .desc = { |
Kevin Wolf | 492fdc6 | 2013-06-19 13:44:17 +0200 | [diff] [blame] | 4295 | /* |
| 4296 | * no elements => accept any params |
| 4297 | * validation will happen later |
| 4298 | */ |
Kevin Wolf | 0006383 | 2013-03-15 10:35:07 +0100 | [diff] [blame] | 4299 | { /* end of list */ } |
| 4300 | }, |
| 4301 | }; |