blob: 18acc5bf01eeee5c1242d64c1cfb87ef89bec809 [file] [log] [blame]
Markus Armbruster666daa62010-06-02 18:48:27 +02001/*
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 Armbruster3618a092013-03-14 13:59:53 +01008 *
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 Armbruster666daa62010-06-02 18:48:27 +020031 */
32
Peter Maydelld38ea872016-01-29 17:50:05 +000033#include "qemu/osdep.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020034#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010036#include "hw/block/block.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010037#include "block/blockjob.h"
Alberto Garcia76f4afb2015-06-08 18:17:44 +020038#include "block/throttle-groups.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010040#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/option.h"
42#include "qemu/config-file.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010043#include "qapi/qmp/types.h"
Kevin Wolfd26c9a12013-09-23 15:26:03 +020044#include "qapi-visit.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010045#include "qapi/qmp/qerror.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010046#include "qapi/qobject-output-visitor.h"
Peter Lieven9e7dac72014-08-13 19:20:17 +020047#include "qapi/util.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010049#include "block/block_int.h"
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -020050#include "qmp-commands.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000051#include "block/trace.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010052#include "sysemu/arch_init.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020053#include "qemu/cutils.h"
54#include "qemu/help_option.h"
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +010055#include "qemu/throttle-options.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020056
Max Reitz9c4218e2016-01-29 16:36:12 +010057static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
58 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
59
Kevin Wolfb33945c2016-09-20 13:38:43 +020060static int do_open_tray(const char *blk_name, const char *qdev_id,
61 bool force, Error **errp);
Colin Lordbf18bee2016-06-06 14:15:22 -040062
Markus Armbruster19609662011-01-28 11:21:39 +010063static const char *const if_name[IF_COUNT] = {
64 [IF_NONE] = "none",
65 [IF_IDE] = "ide",
66 [IF_SCSI] = "scsi",
67 [IF_FLOPPY] = "floppy",
68 [IF_PFLASH] = "pflash",
69 [IF_MTD] = "mtd",
70 [IF_SD] = "sd",
71 [IF_VIRTIO] = "virtio",
72 [IF_XEN] = "xen",
73};
74
John Snow21dff8c2014-10-01 14:19:25 -040075static int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010076 /*
77 * Do not change these numbers! They govern how drive option
78 * index maps to unit and bus. That mapping is ABI.
79 *
Wei Jiangang547cb152016-04-26 18:12:43 +080080 * All controllers used to implement if=T drives need to support
Markus Armbruster27d6bf42011-01-28 11:21:40 +010081 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
82 * Otherwise, some index values map to "impossible" bus, unit
83 * values.
84 *
85 * For instance, if you change [IF_SCSI] to 255, -drive
86 * if=scsi,index=12 no longer means bus=1,unit=5, but
87 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
88 * the drive can't be set up. Regression.
89 */
90 [IF_IDE] = 2,
91 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010092};
93
John Snow21dff8c2014-10-01 14:19:25 -040094/**
95 * Boards may call this to offer board-by-board overrides
96 * of the default, global values.
97 */
98void override_max_devs(BlockInterfaceType type, int max_devs)
99{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200100 BlockBackend *blk;
John Snow21dff8c2014-10-01 14:19:25 -0400101 DriveInfo *dinfo;
102
103 if (max_devs <= 0) {
104 return;
105 }
106
Markus Armbruster18e46a02014-10-07 13:59:06 +0200107 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
108 dinfo = blk_legacy_dinfo(blk);
John Snow21dff8c2014-10-01 14:19:25 -0400109 if (dinfo->type == type) {
110 fprintf(stderr, "Cannot override units-per-bus property of"
111 " the %s interface, because a drive of that type has"
112 " already been added.\n", if_name[type]);
113 g_assert_not_reached();
114 }
115 }
116
117 if_max_devs[type] = max_devs;
118}
119
Markus Armbruster14bafc52010-06-25 08:09:10 +0200120/*
121 * We automatically delete the drive when a device using it gets
122 * unplugged. Questionable feature, but we can't just drop it.
123 * Device models call blockdev_mark_auto_del() to schedule the
124 * automatic deletion, and generic qdev code calls blockdev_auto_del()
125 * when deletion is actually safe.
126 */
Markus Armbruster4be74632014-10-07 13:59:18 +0200127void blockdev_mark_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200128{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200129 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster4be74632014-10-07 13:59:18 +0200130 BlockDriverState *bs = blk_bs(blk);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100131 AioContext *aio_context;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200132
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200133 if (!dinfo) {
Kevin Wolf2d246f02013-09-18 15:14:47 +0200134 return;
135 }
136
Max Reitz5433c242015-10-19 17:53:29 +0200137 if (bs) {
138 aio_context = bdrv_get_aio_context(bs);
139 aio_context_acquire(aio_context);
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100140
Max Reitz5433c242015-10-19 17:53:29 +0200141 if (bs->job) {
142 block_job_cancel(bs->job);
143 }
144
145 aio_context_release(aio_context);
Paolo Bonzini12bde0e2012-03-30 13:17:10 +0200146 }
Stefan Hajnoczi91fddb02014-10-21 12:03:52 +0100147
Markus Armbruster26f8b3a2014-10-07 13:59:22 +0200148 dinfo->auto_del = 1;
Markus Armbruster14bafc52010-06-25 08:09:10 +0200149}
150
Markus Armbruster4be74632014-10-07 13:59:18 +0200151void blockdev_auto_del(BlockBackend *blk)
Markus Armbruster14bafc52010-06-25 08:09:10 +0200152{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200153 DriveInfo *dinfo = blk_legacy_dinfo(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200154
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600155 if (dinfo && dinfo->auto_del) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100156 monitor_remove_blk(blk);
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +0200157 blk_unref(blk);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200158 }
159}
160
John Snowd8f94e12014-10-01 14:19:27 -0400161/**
162 * Returns the current mapping of how many units per bus
163 * a particular interface can support.
164 *
165 * A positive integer indicates n units per bus.
166 * 0 implies the mapping has not been established.
167 * -1 indicates an invalid BlockInterfaceType was given.
168 */
169int drive_get_max_devs(BlockInterfaceType type)
170{
171 if (type >= IF_IDE && type < IF_COUNT) {
172 return if_max_devs[type];
173 }
174
175 return -1;
176}
177
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100178static int drive_index_to_bus_id(BlockInterfaceType type, int index)
179{
180 int max_devs = if_max_devs[type];
181 return max_devs ? index / max_devs : 0;
182}
183
184static int drive_index_to_unit_id(BlockInterfaceType type, int index)
185{
186 int max_devs = if_max_devs[type];
187 return max_devs ? index % max_devs : index;
188}
189
Markus Armbruster2292dda2011-01-28 11:21:41 +0100190QemuOpts *drive_def(const char *optstr)
191{
Markus Armbruster70b94332015-02-13 12:50:26 +0100192 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100193}
194
195QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100196 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200197{
Markus Armbruster666daa62010-06-02 18:48:27 +0200198 QemuOpts *opts;
199
Markus Armbruster2292dda2011-01-28 11:21:41 +0100200 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200201 if (!opts) {
202 return NULL;
203 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100204 if (type != IF_DEFAULT) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100205 qemu_opt_set(opts, "if", if_name[type], &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100206 }
207 if (index >= 0) {
Markus Armbrustera8b18f82015-02-13 15:50:43 +0100208 qemu_opt_set_number(opts, "index", index, &error_abort);
Markus Armbruster2292dda2011-01-28 11:21:41 +0100209 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200210 if (file)
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100211 qemu_opt_set(opts, "file", file, &error_abort);
Markus Armbruster666daa62010-06-02 18:48:27 +0200212 return opts;
213}
214
215DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
216{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200217 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200218 DriveInfo *dinfo;
219
Markus Armbruster18e46a02014-10-07 13:59:06 +0200220 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
221 dinfo = blk_legacy_dinfo(blk);
222 if (dinfo && dinfo->type == type
223 && dinfo->bus == bus && dinfo->unit == unit) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200224 return dinfo;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200225 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200226 }
227
228 return NULL;
229}
230
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100231void drive_check_orphaned(void)
John Snowa66c9dc2014-10-01 14:19:24 -0400232{
Markus Armbruster18e46a02014-10-07 13:59:06 +0200233 BlockBackend *blk;
John Snowa66c9dc2014-10-01 14:19:24 -0400234 DriveInfo *dinfo;
Markus Armbruster664cc622017-02-15 11:05:45 +0100235 Location loc;
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100236 bool orphans = false;
John Snowa66c9dc2014-10-01 14:19:24 -0400237
Markus Armbruster18e46a02014-10-07 13:59:06 +0200238 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
239 dinfo = blk_legacy_dinfo(blk);
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200240 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
John Snowa66c9dc2014-10-01 14:19:24 -0400241 dinfo->type != IF_NONE) {
Markus Armbruster664cc622017-02-15 11:05:45 +0100242 loc_push_none(&loc);
243 qemu_opts_loc_restore(dinfo->opts);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100244 error_report("machine type does not support"
Markus Armbruster664cc622017-02-15 11:05:45 +0100245 " if=%s,bus=%d,unit=%d",
246 if_name[dinfo->type], dinfo->bus, dinfo->unit);
247 loc_pop(&loc);
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100248 orphans = true;
John Snowa66c9dc2014-10-01 14:19:24 -0400249 }
250 }
251
Markus Armbruster720b8dc2017-02-15 11:05:46 +0100252 if (orphans) {
253 exit(1);
254 }
John Snowa66c9dc2014-10-01 14:19:24 -0400255}
256
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100257DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
258{
259 return drive_get(type,
260 drive_index_to_bus_id(type, index),
261 drive_index_to_unit_id(type, index));
262}
263
Markus Armbruster666daa62010-06-02 18:48:27 +0200264int drive_get_max_bus(BlockInterfaceType type)
265{
266 int max_bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200267 BlockBackend *blk;
Markus Armbruster666daa62010-06-02 18:48:27 +0200268 DriveInfo *dinfo;
269
270 max_bus = -1;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200271 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
272 dinfo = blk_legacy_dinfo(blk);
273 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
Markus Armbruster666daa62010-06-02 18:48:27 +0200274 max_bus = dinfo->bus;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200275 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200276 }
277 return max_bus;
278}
279
Markus Armbruster13839972011-01-28 11:21:37 +0100280/* Get a block device. This should only be used for single-drive devices
281 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
282 appropriate bus. */
283DriveInfo *drive_get_next(BlockInterfaceType type)
284{
285 static int next_block_unit[IF_COUNT];
286
287 return drive_get(type, 0, next_block_unit[type]++);
288}
289
Markus Armbruster666daa62010-06-02 18:48:27 +0200290static void bdrv_format_print(void *opaque, const char *name)
291{
Markus Armbruster807105a2011-01-17 19:31:27 +0100292 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200293}
294
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000295typedef struct {
296 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800297 BlockDriverState *bs;
298} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000299
Kevin Wolfb6810722013-09-20 11:33:11 +0200300static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200301{
302 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200303 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200304 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200305 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200306 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200307 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200308 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200309 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200310 } else {
Kevin Wolfb6810722013-09-20 11:33:11 +0200311 error_setg(errp, "'%s' invalid %s error action",
312 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200313 return -1;
314 }
315}
316
Alberto Garcia40119ef2015-11-16 11:28:38 +0200317static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
318 Error **errp)
319{
320 const QListEntry *entry;
321 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
322 switch (qobject_type(entry->value)) {
323
324 case QTYPE_QSTRING: {
325 unsigned long long length;
326 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
327 if (parse_uint_full(str, &length, 10) == 0 &&
328 length > 0 && length <= UINT_MAX) {
329 block_acct_add_interval(stats, (unsigned) length);
330 } else {
331 error_setg(errp, "Invalid interval length: %s", str);
332 return false;
333 }
334 break;
335 }
336
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400337 case QTYPE_QNUM: {
338 int64_t length = qnum_get_int(qobject_to_qnum(entry->value));
339
Alberto Garcia40119ef2015-11-16 11:28:38 +0200340 if (length > 0 && length <= UINT_MAX) {
341 block_acct_add_interval(stats, (unsigned) length);
342 } else {
343 error_setg(errp, "Invalid interval length: %" PRId64, length);
344 return false;
345 }
346 break;
347 }
348
349 default:
350 error_setg(errp, "The specification of stats-intervals is invalid");
351 return false;
352 }
353 }
354 return true;
355}
356
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200357typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
358
Max Reitzfbf81752015-10-19 17:53:31 +0200359/* All parameters but @opts are optional and may be set to NULL. */
360static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
361 const char **throttling_group, ThrottleConfig *throttle_cfg,
362 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
363{
Max Reitzfbf81752015-10-19 17:53:31 +0200364 Error *local_error = NULL;
365 const char *aio;
366
367 if (bdrv_flags) {
Max Reitzfbf81752015-10-19 17:53:31 +0200368 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
369 *bdrv_flags |= BDRV_O_COPY_ON_READ;
370 }
371
Max Reitzfbf81752015-10-19 17:53:31 +0200372 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
373 if (!strcmp(aio, "native")) {
374 *bdrv_flags |= BDRV_O_NATIVE_AIO;
375 } else if (!strcmp(aio, "threads")) {
376 /* this is the default */
377 } else {
378 error_setg(errp, "invalid aio option");
379 return;
380 }
381 }
382 }
383
384 /* disk I/O throttling */
385 if (throttling_group) {
386 *throttling_group = qemu_opt_get(opts, "throttling.group");
387 }
388
389 if (throttle_cfg) {
Alberto Garcia1588ab52016-02-18 12:27:00 +0200390 throttle_config_init(throttle_cfg);
Max Reitzfbf81752015-10-19 17:53:31 +0200391 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
392 qemu_opt_get_number(opts, "throttling.bps-total", 0);
393 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
394 qemu_opt_get_number(opts, "throttling.bps-read", 0);
395 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
396 qemu_opt_get_number(opts, "throttling.bps-write", 0);
397 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
398 qemu_opt_get_number(opts, "throttling.iops-total", 0);
399 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
400 qemu_opt_get_number(opts, "throttling.iops-read", 0);
401 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
402 qemu_opt_get_number(opts, "throttling.iops-write", 0);
403
404 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
405 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
406 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
407 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
408 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
409 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
410 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
411 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
412 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
413 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
414 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
415 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
416
Alberto Garcia8a0fc182016-02-18 12:27:02 +0200417 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
418 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
419 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
420 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
421 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
422 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
423 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
424 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
425 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
426 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
427 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
428 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
429
Max Reitzfbf81752015-10-19 17:53:31 +0200430 throttle_cfg->op_size =
431 qemu_opt_get_number(opts, "throttling.iops-size", 0);
432
Alberto Garciad5851082016-02-18 12:26:59 +0200433 if (!throttle_is_valid(throttle_cfg, errp)) {
Max Reitzfbf81752015-10-19 17:53:31 +0200434 return;
435 }
436 }
437
438 if (detect_zeroes) {
439 *detect_zeroes =
440 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
441 qemu_opt_get(opts, "detect-zeroes"),
Eric Blake7fb1cf12015-11-18 01:52:57 -0700442 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Max Reitzfbf81752015-10-19 17:53:31 +0200443 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
444 &local_error);
445 if (local_error) {
446 error_propagate(errp, local_error);
447 return;
448 }
Max Reitzfbf81752015-10-19 17:53:31 +0200449 }
450}
451
Kevin Wolff298d072013-09-10 12:01:20 +0200452/* Takes the ownership of bs_opts */
Markus Armbruster18e46a02014-10-07 13:59:06 +0200453static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
454 Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200455{
456 const char *buf;
Markus Armbruster666daa62010-06-02 18:48:27 +0200457 int bdrv_flags = 0;
458 int on_read_error, on_write_error;
Alberto Garcia362e9292015-10-28 17:33:04 +0200459 bool account_invalid, account_failed;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300460 bool writethrough, read_only;
Markus Armbruster26f54e92014-10-07 13:59:04 +0200461 BlockBackend *blk;
Markus Armbrustera0f1eab2014-09-12 21:26:21 +0200462 BlockDriverState *bs;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200463 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200464 int snapshot = 0;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100465 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100466 QemuOpts *opts;
Alberto Garcia40119ef2015-11-16 11:28:38 +0200467 QDict *interval_dict = NULL;
468 QList *interval_list = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100469 const char *id;
Max Reitzfbf81752015-10-19 17:53:31 +0200470 BlockdevDetectZeroesOptions detect_zeroes =
471 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
472 const char *throttling_group = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200473
Kevin Wolff298d072013-09-10 12:01:20 +0200474 /* Check common options by copying from bs_opts to opts, all other options
475 * stay in bs_opts for processing by bdrv_open(). */
476 id = qdict_get_try_str(bs_opts, "id");
Kevin Wolf00063832013-03-15 10:35:07 +0100477 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100478 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200479 error_propagate(errp, error);
Markus Armbruster6376f952014-05-28 11:17:01 +0200480 goto err_no_opts;
Kevin Wolf00063832013-03-15 10:35:07 +0100481 }
482
Kevin Wolf00063832013-03-15 10:35:07 +0100483 qemu_opts_absorb_qdict(opts, bs_opts, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100484 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200485 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100486 goto early_err;
Kevin Wolf00063832013-03-15 10:35:07 +0100487 }
488
489 if (id) {
490 qdict_del(bs_opts, "id");
491 }
492
Markus Armbruster666daa62010-06-02 18:48:27 +0200493 /* extract parameters */
Markus Armbruster666daa62010-06-02 18:48:27 +0200494 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200495
Alberto Garcia362e9292015-10-28 17:33:04 +0200496 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
497 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
498
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100499 writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
500
Alberto Garciaff356ee2016-07-08 17:03:00 +0300501 id = qemu_opts_id(opts);
502
Alberto Garcia40119ef2015-11-16 11:28:38 +0200503 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
504 qdict_array_split(interval_dict, &interval_list);
505
506 if (qdict_size(interval_dict) != 0) {
507 error_setg(errp, "Invalid option stats-intervals.%s",
508 qdict_first(interval_dict)->key);
509 goto early_err;
510 }
Alberto Garcia2be55062015-10-28 17:33:07 +0200511
Max Reitzfbf81752015-10-19 17:53:31 +0200512 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
513 &detect_zeroes, &error);
514 if (error) {
515 error_propagate(errp, error);
516 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200517 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200518
519 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100520 if (is_help_option(buf)) {
521 error_printf("Supported formats:");
522 bdrv_iterate_format(bdrv_format_print, NULL);
523 error_printf("\n");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100524 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200525 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200526
Max Reitze4342ce2015-02-05 13:58:14 -0500527 if (qdict_haskey(bs_opts, "driver")) {
528 error_setg(errp, "Cannot specify both 'driver' and 'format'");
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100529 goto early_err;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400530 }
Eric Blake46f5ac22017-04-27 16:58:17 -0500531 qdict_put_str(bs_opts, "driver", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200532 }
533
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200534 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200535 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200536 on_write_error = parse_block_error_action(buf, 0, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100537 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200538 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100539 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200540 }
541 }
542
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200543 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200544 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200545 on_read_error = parse_block_error_action(buf, 1, &error);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100546 if (error) {
Kevin Wolfb6810722013-09-20 11:33:11 +0200547 error_propagate(errp, error);
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100548 goto early_err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200549 }
550 }
551
Max Reitz5ec18f82015-10-19 17:53:30 +0200552 if (snapshot) {
Kevin Wolf91a097e2015-05-08 17:49:53 +0200553 bdrv_flags |= BDRV_O_SNAPSHOT;
Max Reitz5ec18f82015-10-19 17:53:30 +0200554 }
555
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300556 read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
557
Kevin Wolf326642b2013-07-11 12:52:34 +0200558 /* init */
Kevin Wolf39c4ae92015-11-13 14:45:42 +0100559 if ((!file || !*file) && !qdict_size(bs_opts)) {
Max Reitz5ec18f82015-10-19 17:53:30 +0200560 BlockBackendRootState *blk_rs;
561
Kevin Wolf6d0eb642017-01-20 17:07:26 +0100562 blk = blk_new(0, BLK_PERM_ALL);
Max Reitz5ec18f82015-10-19 17:53:30 +0200563 blk_rs = blk_get_root_state(blk);
564 blk_rs->open_flags = bdrv_flags;
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300565 blk_rs->read_only = read_only;
Max Reitz5ec18f82015-10-19 17:53:30 +0200566 blk_rs->detect_zeroes = detect_zeroes;
567
Max Reitze4342ce2015-02-05 13:58:14 -0500568 QDECREF(bs_opts);
569 } else {
570 if (file && !*file) {
571 file = NULL;
572 }
573
Kevin Wolf91a097e2015-05-08 17:49:53 +0200574 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
575 * with other callers) rather than what we want as the real defaults.
576 * Apply the defaults here instead. */
Kevin Wolf91a097e2015-05-08 17:49:53 +0200577 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
578 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300579 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
580 read_only ? "on" : "off");
Kevin Wolf72e775c2016-03-15 14:34:37 +0100581 assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
Kevin Wolf91a097e2015-05-08 17:49:53 +0200582
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100583 if (runstate_check(RUN_STATE_INMIGRATE)) {
584 bdrv_flags |= BDRV_O_INACTIVE;
585 }
586
Max Reitzefaa7c42016-03-16 19:54:38 +0100587 blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
Max Reitze4342ce2015-02-05 13:58:14 -0500588 if (!blk) {
589 goto err_no_bs_opts;
590 }
591 bs = blk_bs(blk);
Max Reitze4342ce2015-02-05 13:58:14 -0500592
Max Reitz5ec18f82015-10-19 17:53:30 +0200593 bs->detect_zeroes = detect_zeroes;
594
Max Reitz5ec18f82015-10-19 17:53:30 +0200595 if (bdrv_key_required(bs)) {
596 autostart = 0;
597 }
Alberto Garcia362e9292015-10-28 17:33:04 +0200598
599 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
Alberto Garcia2be55062015-10-28 17:33:07 +0200600
Alberto Garcia40119ef2015-11-16 11:28:38 +0200601 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
602 blk_unref(blk);
603 blk = NULL;
604 goto err_no_bs_opts;
Alberto Garcia2be55062015-10-28 17:33:07 +0200605 }
Max Reitz5ec18f82015-10-19 17:53:30 +0200606 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200607
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100608 /* disk I/O throttling */
609 if (throttle_enabled(&cfg)) {
610 if (!throttling_group) {
Alberto Garciaff356ee2016-07-08 17:03:00 +0300611 throttling_group = id;
Kevin Wolf7ca7f0f2016-03-22 13:00:08 +0100612 }
613 blk_io_limits_enable(blk, throttling_group);
614 blk_set_io_limits(blk, &cfg);
615 }
616
Kevin Wolfe4b24b42016-03-15 15:39:42 +0100617 blk_set_enable_write_cache(blk, !writethrough);
Max Reitz373340b2015-10-19 17:53:22 +0200618 blk_set_on_error(blk, on_read_error, on_write_error);
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200619
Alberto Garciaff356ee2016-07-08 17:03:00 +0300620 if (!monitor_add_blk(blk, id, errp)) {
Max Reitzefaa7c42016-03-16 19:54:38 +0100621 blk_unref(blk);
622 blk = NULL;
623 goto err_no_bs_opts;
624 }
625
Max Reitze4342ce2015-02-05 13:58:14 -0500626err_no_bs_opts:
Kevin Wolf00063832013-03-15 10:35:07 +0100627 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200628 QDECREF(interval_dict);
629 QDECREF(interval_list);
Markus Armbruster18e46a02014-10-07 13:59:06 +0200630 return blk;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100631
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100632early_err:
Stefan Hajnocziec9c10d2013-10-30 14:54:30 +0100633 qemu_opts_del(opts);
Alberto Garcia40119ef2015-11-16 11:28:38 +0200634 QDECREF(interval_dict);
635 QDECREF(interval_list);
Markus Armbruster6376f952014-05-28 11:17:01 +0200636err_no_opts:
637 QDECREF(bs_opts);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100638 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200639}
640
Max Reitzbd745e22015-10-19 17:53:32 +0200641/* Takes the ownership of bs_opts */
642static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
643{
Max Reitzbd745e22015-10-19 17:53:32 +0200644 int bdrv_flags = 0;
645
Kevin Wolfa81d6162016-03-07 14:23:04 +0100646 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
647 * with other callers) rather than what we want as the real defaults.
648 * Apply the defaults here instead. */
Kevin Wolfa81d6162016-03-07 14:23:04 +0100649 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
650 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
Alberto Garciaf87a0e22016-09-15 17:53:02 +0300651 qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
Kevin Wolfa81d6162016-03-07 14:23:04 +0100652
Kevin Wolf12d5ee32016-02-19 16:48:10 +0100653 if (runstate_check(RUN_STATE_INMIGRATE)) {
654 bdrv_flags |= BDRV_O_INACTIVE;
655 }
656
Kevin Wolf74e1ae72016-09-20 20:48:52 +0200657 return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
Max Reitzbd745e22015-10-19 17:53:32 +0200658}
659
Max Reitz9c4218e2016-01-29 16:36:12 +0100660void blockdev_close_all_bdrv_states(void)
661{
662 BlockDriverState *bs, *next_bs;
663
664 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
665 AioContext *ctx = bdrv_get_aio_context(bs);
666
667 aio_context_acquire(ctx);
668 bdrv_unref(bs);
669 aio_context_release(ctx);
670 }
671}
672
Max Reitz262b4e82016-03-16 19:54:41 +0100673/* Iterates over the list of monitor-owned BlockDriverStates */
674BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
675{
676 return bs ? QTAILQ_NEXT(bs, monitor_list)
677 : QTAILQ_FIRST(&monitor_bdrv_states);
678}
679
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200680static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
681 Error **errp)
Kevin Wolf57975222013-07-17 14:41:54 +0200682{
683 const char *value;
684
685 value = qemu_opt_get(opts, from);
686 if (value) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200687 if (qemu_opt_find(opts, to)) {
688 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
689 "same time", to, from);
690 return;
691 }
Jun Li20d6cd42014-09-24 13:45:27 +0800692 }
693
694 /* rename all items in opts */
695 while ((value = qemu_opt_get(opts, from))) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100696 qemu_opt_set(opts, to, value, &error_abort);
Kevin Wolf57975222013-07-17 14:41:54 +0200697 qemu_opt_unset(opts, from);
698 }
699}
700
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200701QemuOptsList qemu_legacy_drive_opts = {
702 .name = "drive",
703 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
704 .desc = {
705 {
Kevin Wolf87a899c2013-09-10 15:48:13 +0200706 .name = "bus",
707 .type = QEMU_OPT_NUMBER,
708 .help = "bus number",
709 },{
710 .name = "unit",
711 .type = QEMU_OPT_NUMBER,
712 .help = "unit number (i.e. lun for scsi)",
713 },{
714 .name = "index",
715 .type = QEMU_OPT_NUMBER,
716 .help = "index number",
717 },{
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200718 .name = "media",
719 .type = QEMU_OPT_STRING,
720 .help = "media type (disk, cdrom)",
Kevin Wolf593d4642013-08-28 17:24:51 +0200721 },{
722 .name = "if",
723 .type = QEMU_OPT_STRING,
724 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
Kevin Wolfb41a7332013-09-09 16:49:49 +0200725 },{
726 .name = "cyls",
727 .type = QEMU_OPT_NUMBER,
728 .help = "number of cylinders (ide disk geometry)",
729 },{
730 .name = "heads",
731 .type = QEMU_OPT_NUMBER,
732 .help = "number of heads (ide disk geometry)",
733 },{
734 .name = "secs",
735 .type = QEMU_OPT_NUMBER,
736 .help = "number of sectors (ide disk geometry)",
737 },{
738 .name = "trans",
739 .type = QEMU_OPT_STRING,
740 .help = "chs translation (auto, lba, none)",
Kevin Wolf26929292013-09-09 17:01:03 +0200741 },{
742 .name = "boot",
743 .type = QEMU_OPT_BOOL,
744 .help = "(deprecated, ignored)",
Kevin Wolf394c7d42013-09-13 14:09:17 +0200745 },{
746 .name = "addr",
747 .type = QEMU_OPT_STRING,
748 .help = "pci address (virtio only)",
Max Reitzd095b462013-12-20 19:28:14 +0100749 },{
Kevin Wolfbcf83152014-06-06 15:21:48 +0200750 .name = "serial",
751 .type = QEMU_OPT_STRING,
752 .help = "disk serial number",
753 },{
Max Reitzd095b462013-12-20 19:28:14 +0100754 .name = "file",
755 .type = QEMU_OPT_STRING,
756 .help = "file name",
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200757 },
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200758
759 /* Options that are passed on, but have special semantics with -drive */
760 {
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300761 .name = BDRV_OPT_READ_ONLY,
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200762 .type = QEMU_OPT_BOOL,
763 .help = "open drive file as read-only",
764 },{
Kevin Wolfee13ed12014-02-09 09:52:32 +0100765 .name = "rerror",
766 .type = QEMU_OPT_STRING,
767 .help = "read error action",
768 },{
769 .name = "werror",
770 .type = QEMU_OPT_STRING,
771 .help = "write error action",
772 },{
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200773 .name = "copy-on-read",
774 .type = QEMU_OPT_BOOL,
775 .help = "copy read data from backing file into image file",
776 },
777
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200778 { /* end of list */ }
779 },
780};
781
Markus Armbruster60e19e02014-06-06 14:50:58 +0200782DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
Kevin Wolf57975222013-07-17 14:41:54 +0200783{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200784 const char *value;
Markus Armbruster18e46a02014-10-07 13:59:06 +0200785 BlockBackend *blk;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200786 DriveInfo *dinfo = NULL;
Kevin Wolff298d072013-09-10 12:01:20 +0200787 QDict *bs_opts;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200788 QemuOpts *legacy_opts;
789 DriveMediaType media = MEDIA_DISK;
Kevin Wolf593d4642013-08-28 17:24:51 +0200790 BlockInterfaceType type;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200791 int cyls, heads, secs, translation;
Kevin Wolf87a899c2013-09-10 15:48:13 +0200792 int max_devs, bus_id, unit_id, index;
Kevin Wolf394c7d42013-09-13 14:09:17 +0200793 const char *devaddr;
Kevin Wolfee13ed12014-02-09 09:52:32 +0100794 const char *werror, *rerror;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800795 bool read_only = false;
796 bool copy_on_read;
Kevin Wolfbcf83152014-06-06 15:21:48 +0200797 const char *serial;
Max Reitzd095b462013-12-20 19:28:14 +0100798 const char *filename;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200799 Error *local_err = NULL;
Kevin Wolf247147f2014-09-24 16:37:14 +0200800 int i;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200801
Kevin Wolf57975222013-07-17 14:41:54 +0200802 /* Change legacy command line options into QMP ones */
Kevin Wolf247147f2014-09-24 16:37:14 +0200803 static const struct {
804 const char *from;
805 const char *to;
806 } opt_renames[] = {
807 { "iops", "throttling.iops-total" },
808 { "iops_rd", "throttling.iops-read" },
809 { "iops_wr", "throttling.iops-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200810
Kevin Wolf247147f2014-09-24 16:37:14 +0200811 { "bps", "throttling.bps-total" },
812 { "bps_rd", "throttling.bps-read" },
813 { "bps_wr", "throttling.bps-write" },
Kevin Wolf57975222013-07-17 14:41:54 +0200814
Kevin Wolf247147f2014-09-24 16:37:14 +0200815 { "iops_max", "throttling.iops-total-max" },
816 { "iops_rd_max", "throttling.iops-read-max" },
817 { "iops_wr_max", "throttling.iops-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200818
Kevin Wolf247147f2014-09-24 16:37:14 +0200819 { "bps_max", "throttling.bps-total-max" },
820 { "bps_rd_max", "throttling.bps-read-max" },
821 { "bps_wr_max", "throttling.bps-write-max" },
Benoît Canet3e9fab62013-09-02 14:14:40 +0200822
Kevin Wolf247147f2014-09-24 16:37:14 +0200823 { "iops_size", "throttling.iops-size" },
Benoît Canet2024c1d2013-09-02 14:14:41 +0200824
Alberto Garcia76f4afb2015-06-08 18:17:44 +0200825 { "group", "throttling.group" },
826
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300827 { "readonly", BDRV_OPT_READ_ONLY },
Kevin Wolf247147f2014-09-24 16:37:14 +0200828 };
829
830 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200831 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
832 &local_err);
833 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100834 error_report_err(local_err);
Kevin Wolf5abbf0e2014-09-18 11:48:34 +0200835 return NULL;
836 }
Kevin Wolf247147f2014-09-24 16:37:14 +0200837 }
Kevin Wolf0f227a92013-07-19 20:07:29 +0200838
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200839 value = qemu_opt_get(all_opts, "cache");
840 if (value) {
841 int flags = 0;
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100842 bool writethrough;
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200843
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100844 if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200845 error_report("invalid cache option");
846 return NULL;
847 }
848
849 /* Specific options take precedence */
Kevin Wolf54861b92015-04-07 16:55:00 +0200850 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
851 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
Kevin Wolf04feb4a2016-03-18 15:35:51 +0100852 !writethrough, &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200853 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200854 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
855 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100856 !!(flags & BDRV_O_NOCACHE), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200857 }
Kevin Wolf54861b92015-04-07 16:55:00 +0200858 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
859 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
Markus Armbrustercccb7962015-02-12 16:37:44 +0100860 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200861 }
862 qemu_opt_unset(all_opts, "cache");
863 }
864
Kevin Wolff298d072013-09-10 12:01:20 +0200865 /* Get a QDict for processing the options */
866 bs_opts = qdict_new();
867 qemu_opts_to_qdict(all_opts, bs_opts);
868
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800869 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
870 &error_abort);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200871 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100872 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100873 error_report_err(local_err);
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200874 goto fail;
875 }
876
Kevin Wolf26929292013-09-09 17:01:03 +0200877 /* Deprecated option boot=[on|off] */
878 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
879 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
880 "ignored. Future versions will reject this parameter. Please "
881 "update your scripts.\n");
882 }
883
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200884 /* Media type */
885 value = qemu_opt_get(legacy_opts, "media");
886 if (value) {
887 if (!strcmp(value, "disk")) {
888 media = MEDIA_DISK;
889 } else if (!strcmp(value, "cdrom")) {
890 media = MEDIA_CDROM;
Fam Zhenga7fdbcf2013-10-15 17:45:50 +0800891 read_only = true;
Kevin Wolf33cb7dc2013-08-28 17:00:13 +0200892 } else {
893 error_report("'%s' invalid media", value);
894 goto fail;
895 }
896 }
897
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200898 /* copy-on-read is disabled with a warning for read-only devices */
Alberto Garcia4e200cf2016-09-15 17:53:05 +0300899 read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200900 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
901
902 if (read_only && copy_on_read) {
903 error_report("warning: disabling copy-on-read on read-only drive");
904 copy_on_read = false;
905 }
906
Eric Blake46f5ac22017-04-27 16:58:17 -0500907 qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
908 qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
Kevin Wolf0ebd24e2013-09-19 15:12:18 +0200909
Kevin Wolf593d4642013-08-28 17:24:51 +0200910 /* Controller type */
911 value = qemu_opt_get(legacy_opts, "if");
912 if (value) {
913 for (type = 0;
914 type < IF_COUNT && strcmp(value, if_name[type]);
915 type++) {
916 }
917 if (type == IF_COUNT) {
918 error_report("unsupported bus type '%s'", value);
919 goto fail;
920 }
921 } else {
922 type = block_default_type;
923 }
924
Kevin Wolfb41a7332013-09-09 16:49:49 +0200925 /* Geometry */
926 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
927 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
928 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
929
930 if (cyls || heads || secs) {
931 if (cyls < 1) {
932 error_report("invalid physical cyls number");
933 goto fail;
934 }
935 if (heads < 1) {
936 error_report("invalid physical heads number");
937 goto fail;
938 }
939 if (secs < 1) {
940 error_report("invalid physical secs number");
941 goto fail;
942 }
943 }
944
945 translation = BIOS_ATA_TRANSLATION_AUTO;
946 value = qemu_opt_get(legacy_opts, "trans");
947 if (value != NULL) {
948 if (!cyls) {
949 error_report("'%s' trans must be used with cyls, heads and secs",
950 value);
951 goto fail;
952 }
953 if (!strcmp(value, "none")) {
954 translation = BIOS_ATA_TRANSLATION_NONE;
955 } else if (!strcmp(value, "lba")) {
956 translation = BIOS_ATA_TRANSLATION_LBA;
Paolo Bonzinif31c41f2014-02-08 11:01:54 +0100957 } else if (!strcmp(value, "large")) {
958 translation = BIOS_ATA_TRANSLATION_LARGE;
959 } else if (!strcmp(value, "rechs")) {
960 translation = BIOS_ATA_TRANSLATION_RECHS;
Kevin Wolfb41a7332013-09-09 16:49:49 +0200961 } else if (!strcmp(value, "auto")) {
962 translation = BIOS_ATA_TRANSLATION_AUTO;
963 } else {
964 error_report("'%s' invalid translation type", value);
965 goto fail;
966 }
967 }
968
969 if (media == MEDIA_CDROM) {
970 if (cyls || secs || heads) {
971 error_report("CHS can't be set with media=cdrom");
972 goto fail;
973 }
974 }
975
Kevin Wolf87a899c2013-09-10 15:48:13 +0200976 /* Device address specified by bus/unit or index.
977 * If none was specified, try to find the first free one. */
978 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
979 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
980 index = qemu_opt_get_number(legacy_opts, "index", -1);
981
982 max_devs = if_max_devs[type];
983
984 if (index != -1) {
985 if (bus_id != 0 || unit_id != -1) {
986 error_report("index cannot be used with bus and unit");
987 goto fail;
988 }
989 bus_id = drive_index_to_bus_id(type, index);
990 unit_id = drive_index_to_unit_id(type, index);
991 }
992
993 if (unit_id == -1) {
994 unit_id = 0;
995 while (drive_get(type, bus_id, unit_id) != NULL) {
996 unit_id++;
997 if (max_devs && unit_id >= max_devs) {
998 unit_id -= max_devs;
999 bus_id++;
1000 }
1001 }
1002 }
1003
1004 if (max_devs && unit_id >= max_devs) {
1005 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1006 goto fail;
1007 }
1008
1009 if (drive_get(type, bus_id, unit_id) != NULL) {
1010 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1011 bus_id, unit_id, index);
1012 goto fail;
1013 }
1014
Kevin Wolfbcf83152014-06-06 15:21:48 +02001015 /* Serial number */
1016 serial = qemu_opt_get(legacy_opts, "serial");
1017
Kevin Wolf87a899c2013-09-10 15:48:13 +02001018 /* no id supplied -> create one */
1019 if (qemu_opts_id(all_opts) == NULL) {
1020 char *new_id;
1021 const char *mediastr = "";
1022 if (type == IF_IDE || type == IF_SCSI) {
1023 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1024 }
1025 if (max_devs) {
1026 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1027 mediastr, unit_id);
1028 } else {
1029 new_id = g_strdup_printf("%s%s%i", if_name[type],
1030 mediastr, unit_id);
1031 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001032 qdict_put_str(bs_opts, "id", new_id);
Kevin Wolf87a899c2013-09-10 15:48:13 +02001033 g_free(new_id);
1034 }
1035
Kevin Wolf394c7d42013-09-13 14:09:17 +02001036 /* Add virtio block device */
1037 devaddr = qemu_opt_get(legacy_opts, "addr");
1038 if (devaddr && type != IF_VIRTIO) {
1039 error_report("addr is not supported by this bus type");
1040 goto fail;
1041 }
1042
1043 if (type == IF_VIRTIO) {
1044 QemuOpts *devopts;
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08001045 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1046 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001047 if (arch_type == QEMU_ARCH_S390X) {
Alexander Graf1f68f1d2015-06-16 23:06:33 +02001048 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001049 } else {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001050 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001051 }
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001052 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1053 &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001054 if (devaddr) {
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001055 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
Kevin Wolf394c7d42013-09-13 14:09:17 +02001056 }
1057 }
1058
Max Reitzd095b462013-12-20 19:28:14 +01001059 filename = qemu_opt_get(legacy_opts, "file");
1060
Kevin Wolfee13ed12014-02-09 09:52:32 +01001061 /* Check werror/rerror compatibility with if=... */
1062 werror = qemu_opt_get(legacy_opts, "werror");
1063 if (werror != NULL) {
1064 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1065 type != IF_NONE) {
1066 error_report("werror is not supported by this bus type");
1067 goto fail;
1068 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001069 qdict_put_str(bs_opts, "werror", werror);
Kevin Wolfee13ed12014-02-09 09:52:32 +01001070 }
1071
1072 rerror = qemu_opt_get(legacy_opts, "rerror");
1073 if (rerror != NULL) {
1074 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1075 type != IF_NONE) {
1076 error_report("rerror is not supported by this bus type");
1077 goto fail;
1078 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001079 qdict_put_str(bs_opts, "rerror", rerror);
Kevin Wolfee13ed12014-02-09 09:52:32 +01001080 }
1081
Kevin Wolf2d246f02013-09-18 15:14:47 +02001082 /* Actual block device init: Functionality shared with blockdev-add */
Markus Armbruster18e46a02014-10-07 13:59:06 +02001083 blk = blockdev_init(filename, bs_opts, &local_err);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001084 bs_opts = NULL;
Markus Armbruster18e46a02014-10-07 13:59:06 +02001085 if (!blk) {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001086 if (local_err) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001087 error_report_err(local_err);
Kevin Wolfb6810722013-09-20 11:33:11 +02001088 }
Kevin Wolf2d246f02013-09-18 15:14:47 +02001089 goto fail;
Kevin Wolfb6810722013-09-20 11:33:11 +02001090 } else {
Markus Armbruster84d18f02014-01-30 15:07:28 +01001091 assert(!local_err);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001092 }
1093
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001094 /* Create legacy DriveInfo */
1095 dinfo = g_malloc0(sizeof(*dinfo));
Kevin Wolff298d072013-09-10 12:01:20 +02001096 dinfo->opts = all_opts;
Kevin Wolf2d246f02013-09-18 15:14:47 +02001097
Kevin Wolfb41a7332013-09-09 16:49:49 +02001098 dinfo->cyls = cyls;
1099 dinfo->heads = heads;
1100 dinfo->secs = secs;
1101 dinfo->trans = translation;
1102
Kevin Wolfee13ed12014-02-09 09:52:32 +01001103 dinfo->type = type;
Kevin Wolf87a899c2013-09-10 15:48:13 +02001104 dinfo->bus = bus_id;
1105 dinfo->unit = unit_id;
Kevin Wolf394c7d42013-09-13 14:09:17 +02001106 dinfo->devaddr = devaddr;
Kevin Wolfbcf83152014-06-06 15:21:48 +02001107 dinfo->serial = g_strdup(serial);
1108
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02001109 blk_set_legacy_dinfo(blk, dinfo);
1110
Kevin Wolfe34ef042013-09-19 14:24:10 +02001111 switch(type) {
1112 case IF_IDE:
1113 case IF_SCSI:
1114 case IF_XEN:
1115 case IF_NONE:
1116 dinfo->media_cd = media == MEDIA_CDROM;
1117 break;
1118 default:
1119 break;
1120 }
1121
Kevin Wolf2d246f02013-09-18 15:14:47 +02001122fail:
Kevin Wolf33cb7dc2013-08-28 17:00:13 +02001123 qemu_opts_del(legacy_opts);
Markus Armbruster3cb0e252014-05-28 11:17:02 +02001124 QDECREF(bs_opts);
Kevin Wolf2d246f02013-09-18 15:14:47 +02001125 return dinfo;
Kevin Wolf57975222013-07-17 14:41:54 +02001126}
1127
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02001128static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
1129{
1130 BlockDriverState *bs;
1131
1132 bs = bdrv_lookup_bs(name, name, errp);
1133 if (bs == NULL) {
1134 return NULL;
1135 }
1136
1137 if (!bdrv_is_root_node(bs)) {
1138 error_setg(errp, "Need a root block node");
1139 return NULL;
1140 }
1141
1142 if (!bdrv_is_inserted(bs)) {
1143 error_setg(errp, "Device has no medium");
1144 return NULL;
1145 }
1146
1147 return bs;
1148}
1149
Kevin Wolfb33945c2016-09-20 13:38:43 +02001150static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
1151 Error **errp)
1152{
1153 BlockBackend *blk;
1154
1155 if (!blk_name == !qdev_id) {
1156 error_setg(errp, "Need exactly one of 'device' and 'id'");
1157 return NULL;
1158 }
1159
1160 if (qdev_id) {
1161 blk = blk_by_qdev_id(qdev_id, errp);
1162 } else {
1163 blk = blk_by_name(blk_name);
1164 if (blk == NULL) {
1165 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1166 "Device '%s' not found", blk_name);
1167 }
1168 }
1169
1170 return blk;
1171}
1172
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001173void hmp_commit(Monitor *mon, const QDict *qdict)
Markus Armbruster666daa62010-06-02 18:48:27 +02001174{
Markus Armbruster666daa62010-06-02 18:48:27 +02001175 const char *device = qdict_get_str(qdict, "device");
Fam Zhenga0e85442015-03-02 19:36:48 +08001176 BlockBackend *blk;
Stefan Hajnoczie8877492012-03-05 18:10:11 +00001177 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +02001178
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001179 if (!strcmp(device, "all")) {
Max Reitzda31d592016-03-16 19:54:32 +01001180 ret = blk_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001181 } else {
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001182 BlockDriverState *bs;
1183 AioContext *aio_context;
1184
Fam Zhenga0e85442015-03-02 19:36:48 +08001185 blk = blk_by_name(device);
1186 if (!blk) {
Jeff Cody58513bd2013-01-18 12:45:35 -05001187 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +02001188 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +02001189 }
Max Reitz5433c242015-10-19 17:53:29 +02001190 if (!blk_is_available(blk)) {
1191 monitor_printf(mon, "Device '%s' has no medium\n", device);
1192 return;
1193 }
Stefan Hajnoczi84aa0142015-11-04 20:27:23 +03001194
1195 bs = blk_bs(blk);
1196 aio_context = bdrv_get_aio_context(bs);
1197 aio_context_acquire(aio_context);
1198
1199 ret = bdrv_commit(bs);
1200
1201 aio_context_release(aio_context);
Jeff Cody58513bd2013-01-18 12:45:35 -05001202 }
1203 if (ret < 0) {
1204 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1205 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +02001206 }
1207}
1208
Eric Blake10f75902016-03-03 09:16:50 -07001209static void blockdev_do_action(TransactionAction *action, Error **errp)
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001210{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001211 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001212
Eric Blake10f75902016-03-03 09:16:50 -07001213 list.value = action;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001214 list.next = NULL;
John Snow94d16a62015-11-05 18:13:18 -05001215 qmp_transaction(&list, false, NULL, errp);
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001216}
1217
Benoît Canet0901f672014-01-23 21:31:38 +01001218void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1219 bool has_node_name, const char *node_name,
1220 const char *snapshot_file,
1221 bool has_snapshot_node_name,
1222 const char *snapshot_node_name,
Luiz Capitulino6106e242011-11-25 16:15:19 -02001223 bool has_format, const char *format,
Benoît Canet0901f672014-01-23 21:31:38 +01001224 bool has_mode, NewImageMode mode, Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +01001225{
Alberto Garciaa911e6a2015-10-26 14:27:14 +02001226 BlockdevSnapshotSync snapshot = {
Benoît Canet0901f672014-01-23 21:31:38 +01001227 .has_device = has_device,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001228 .device = (char *) device,
Benoît Canet0901f672014-01-23 21:31:38 +01001229 .has_node_name = has_node_name,
1230 .node_name = (char *) node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001231 .snapshot_file = (char *) snapshot_file,
Benoît Canet0901f672014-01-23 21:31:38 +01001232 .has_snapshot_node_name = has_snapshot_node_name,
1233 .snapshot_node_name = (char *) snapshot_node_name,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +01001234 .has_format = has_format,
1235 .format = (char *) format,
1236 .has_mode = has_mode,
1237 .mode = mode,
1238 };
Eric Blake10f75902016-03-03 09:16:50 -07001239 TransactionAction action = {
1240 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001241 .u.blockdev_snapshot_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001242 };
1243 blockdev_do_action(&action, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +01001244}
1245
Alberto Garcia43de7e22015-10-26 14:27:16 +02001246void qmp_blockdev_snapshot(const char *node, const char *overlay,
1247 Error **errp)
1248{
1249 BlockdevSnapshot snapshot_data = {
1250 .node = (char *) node,
1251 .overlay = (char *) overlay
1252 };
Eric Blake10f75902016-03-03 09:16:50 -07001253 TransactionAction action = {
1254 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
Eric Blake32bafa82016-03-17 16:48:37 -06001255 .u.blockdev_snapshot.data = &snapshot_data,
Eric Blake10f75902016-03-03 09:16:50 -07001256 };
1257 blockdev_do_action(&action, errp);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001258}
1259
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001260void qmp_blockdev_snapshot_internal_sync(const char *device,
1261 const char *name,
1262 Error **errp)
1263{
1264 BlockdevSnapshotInternal snapshot = {
1265 .device = (char *) device,
1266 .name = (char *) name
1267 };
Eric Blake10f75902016-03-03 09:16:50 -07001268 TransactionAction action = {
1269 .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
Eric Blake32bafa82016-03-17 16:48:37 -06001270 .u.blockdev_snapshot_internal_sync.data = &snapshot,
Eric Blake10f75902016-03-03 09:16:50 -07001271 };
1272 blockdev_do_action(&action, errp);
Wenchao Xiaf323bc92013-09-11 14:04:35 +08001273}
1274
Wenchao Xia44e3e052013-09-11 14:04:36 +08001275SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1276 bool has_id,
1277 const char *id,
1278 bool has_name,
1279 const char *name,
1280 Error **errp)
1281{
Fam Zhenga0e85442015-03-02 19:36:48 +08001282 BlockDriverState *bs;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001283 AioContext *aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001284 QEMUSnapshotInfo sn;
1285 Error *local_err = NULL;
1286 SnapshotInfo *info = NULL;
1287 int ret;
1288
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001289 bs = qmp_get_root_bs(device, errp);
1290 if (!bs) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001291 return NULL;
1292 }
Kevin Wolf2dfb4c02016-06-23 14:20:24 +02001293 aio_context = bdrv_get_aio_context(bs);
Max Reitz5433c242015-10-19 17:53:29 +02001294 aio_context_acquire(aio_context);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001295
1296 if (!has_id) {
1297 id = NULL;
1298 }
1299
1300 if (!has_name) {
1301 name = NULL;
1302 }
1303
1304 if (!id && !name) {
1305 error_setg(errp, "Name or id must be provided");
Max Reitz5433c242015-10-19 17:53:29 +02001306 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001307 }
1308
Stefan Hajnoczi0b928852014-11-19 14:19:43 +00001309 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1310 goto out_aio_context;
1311 }
1312
Wenchao Xia44e3e052013-09-11 14:04:36 +08001313 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001314 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001315 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001316 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001317 }
1318 if (!ret) {
1319 error_setg(errp,
1320 "Snapshot with id '%s' and name '%s' does not exist on "
1321 "device '%s'",
1322 STR_OR_NULL(id), STR_OR_NULL(name), device);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001323 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001324 }
1325
1326 bdrv_snapshot_delete(bs, id, name, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001327 if (local_err) {
Wenchao Xia44e3e052013-09-11 14:04:36 +08001328 error_propagate(errp, local_err);
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001329 goto out_aio_context;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001330 }
1331
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001332 aio_context_release(aio_context);
1333
Markus Armbruster5839e532014-08-19 10:31:08 +02001334 info = g_new0(SnapshotInfo, 1);
Wenchao Xia44e3e052013-09-11 14:04:36 +08001335 info->id = g_strdup(sn.id_str);
1336 info->name = g_strdup(sn.name);
1337 info->date_nsec = sn.date_nsec;
1338 info->date_sec = sn.date_sec;
1339 info->vm_state_size = sn.vm_state_size;
1340 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1341 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1342
1343 return info;
Stefan Hajnoczi4ef39822014-11-19 14:19:42 +00001344
1345out_aio_context:
1346 aio_context_release(aio_context);
1347 return NULL;
Wenchao Xia44e3e052013-09-11 14:04:36 +08001348}
Jeff Cody8802d1f2012-02-28 15:54:06 -05001349
John Snow341ebc22015-04-17 19:49:52 -04001350/**
1351 * block_dirty_bitmap_lookup:
1352 * Return a dirty bitmap (if present), after validating
1353 * the node reference and bitmap names.
1354 *
1355 * @node: The name of the BDS node to search for bitmaps
1356 * @name: The name of the bitmap to search for
1357 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1358 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1359 * @errp: Output pointer for error information. Can be NULL.
1360 *
1361 * @return: A bitmap object on success, or NULL on failure.
1362 */
1363static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1364 const char *name,
1365 BlockDriverState **pbs,
1366 AioContext **paio,
1367 Error **errp)
1368{
1369 BlockDriverState *bs;
1370 BdrvDirtyBitmap *bitmap;
1371 AioContext *aio_context;
1372
1373 if (!node) {
1374 error_setg(errp, "Node cannot be NULL");
1375 return NULL;
1376 }
1377 if (!name) {
1378 error_setg(errp, "Bitmap name cannot be NULL");
1379 return NULL;
1380 }
1381 bs = bdrv_lookup_bs(node, node, NULL);
1382 if (!bs) {
1383 error_setg(errp, "Node '%s' not found", node);
1384 return NULL;
1385 }
1386
1387 aio_context = bdrv_get_aio_context(bs);
1388 aio_context_acquire(aio_context);
1389
1390 bitmap = bdrv_find_dirty_bitmap(bs, name);
1391 if (!bitmap) {
1392 error_setg(errp, "Dirty bitmap '%s' not found", name);
1393 goto fail;
1394 }
1395
1396 if (pbs) {
1397 *pbs = bs;
1398 }
1399 if (paio) {
1400 *paio = aio_context;
1401 } else {
1402 aio_context_release(aio_context);
1403 }
1404
1405 return bitmap;
1406
1407 fail:
1408 aio_context_release(aio_context);
1409 return NULL;
1410}
1411
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00001412/* New and old BlockDriverState structs for atomic group operations */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001413
John Snow50f43f02015-11-05 18:13:09 -05001414typedef struct BlkActionState BlkActionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001415
John Snow50f43f02015-11-05 18:13:09 -05001416/**
1417 * BlkActionOps:
1418 * Table of operations that define an Action.
1419 *
1420 * @instance_size: Size of state struct, in bytes.
1421 * @prepare: Prepare the work, must NOT be NULL.
1422 * @commit: Commit the changes, can be NULL.
1423 * @abort: Abort the changes on fail, can be NULL.
1424 * @clean: Clean up resources after all transaction actions have called
1425 * commit() or abort(). Can be NULL.
1426 *
1427 * Only prepare() may fail. In a single transaction, only one of commit() or
1428 * abort() will be called. clean() will always be called if it is present.
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001429 */
John Snow50f43f02015-11-05 18:13:09 -05001430typedef struct BlkActionOps {
1431 size_t instance_size;
1432 void (*prepare)(BlkActionState *common, Error **errp);
1433 void (*commit)(BlkActionState *common);
1434 void (*abort)(BlkActionState *common);
1435 void (*clean)(BlkActionState *common);
1436} BlkActionOps;
1437
1438/**
1439 * BlkActionState:
1440 * Describes one Action's state within a Transaction.
1441 *
1442 * @action: QAPI-defined enum identifying which Action to perform.
1443 * @ops: Table of ActionOps this Action can perform.
John Snow94d16a62015-11-05 18:13:18 -05001444 * @block_job_txn: Transaction which this action belongs to.
John Snow50f43f02015-11-05 18:13:09 -05001445 * @entry: List membership for all Actions in this Transaction.
1446 *
1447 * This structure must be arranged as first member in a subclassed type,
1448 * assuming that the compiler will also arrange it to the same offsets as the
1449 * base class.
1450 */
1451struct BlkActionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001452 TransactionAction *action;
John Snow50f43f02015-11-05 18:13:09 -05001453 const BlkActionOps *ops;
John Snow94d16a62015-11-05 18:13:18 -05001454 BlockJobTxn *block_job_txn;
1455 TransactionProperties *txn_props;
John Snow50f43f02015-11-05 18:13:09 -05001456 QSIMPLEQ_ENTRY(BlkActionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001457};
1458
Wenchao Xiabbe86012013-09-11 14:04:34 +08001459/* internal snapshot private data */
1460typedef struct InternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001461 BlkActionState common;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001462 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001463 AioContext *aio_context;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001464 QEMUSnapshotInfo sn;
Fam Zheng507306c2015-10-23 11:08:13 +08001465 bool created;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001466} InternalSnapshotState;
1467
John Snow94d16a62015-11-05 18:13:18 -05001468
1469static int action_check_completion_mode(BlkActionState *s, Error **errp)
1470{
1471 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1472 error_setg(errp,
1473 "Action '%s' does not support Transaction property "
1474 "completion-mode = %s",
1475 TransactionActionKind_lookup[s->action->type],
1476 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1477 return -1;
1478 }
1479 return 0;
1480}
1481
John Snow50f43f02015-11-05 18:13:09 -05001482static void internal_snapshot_prepare(BlkActionState *common,
Wenchao Xiabbe86012013-09-11 14:04:34 +08001483 Error **errp)
1484{
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001485 Error *local_err = NULL;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001486 const char *device;
1487 const char *name;
1488 BlockDriverState *bs;
1489 QEMUSnapshotInfo old_sn, *sn;
1490 bool ret;
1491 qemu_timeval tv;
1492 BlockdevSnapshotInternal *internal;
1493 InternalSnapshotState *state;
1494 int ret1;
1495
Eric Blake6a8f9662015-10-26 16:34:54 -06001496 g_assert(common->action->type ==
Wenchao Xiabbe86012013-09-11 14:04:34 +08001497 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
Eric Blake32bafa82016-03-17 16:48:37 -06001498 internal = common->action->u.blockdev_snapshot_internal_sync.data;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001499 state = DO_UPCAST(InternalSnapshotState, common, common);
1500
1501 /* 1. parse input */
1502 device = internal->device;
1503 name = internal->name;
1504
1505 /* 2. check for validation */
John Snow94d16a62015-11-05 18:13:18 -05001506 if (action_check_completion_mode(common, errp) < 0) {
1507 return;
1508 }
1509
Kevin Wolf75dfd402016-06-23 14:20:24 +02001510 bs = qmp_get_root_bs(device, errp);
1511 if (!bs) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001512 return;
1513 }
1514
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001515 /* AioContext is released in .clean() */
Kevin Wolf75dfd402016-06-23 14:20:24 +02001516 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001517 aio_context_acquire(state->aio_context);
1518
Fam Zheng507306c2015-10-23 11:08:13 +08001519 state->bs = bs;
1520 bdrv_drained_begin(bs);
1521
Stefan Hajnoczi3dc7ca32014-11-21 10:49:00 +00001522 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1523 return;
1524 }
1525
Wenchao Xiabbe86012013-09-11 14:04:34 +08001526 if (bdrv_is_read_only(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001527 error_setg(errp, "Device '%s' is read only", device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001528 return;
1529 }
1530
1531 if (!bdrv_can_snapshot(bs)) {
Alberto Garcia81e5f782015-04-08 12:29:19 +03001532 error_setg(errp, "Block format '%s' used by device '%s' "
1533 "does not support internal snapshots",
1534 bs->drv->format_name, device);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001535 return;
1536 }
1537
1538 if (!strlen(name)) {
1539 error_setg(errp, "Name is empty");
1540 return;
1541 }
1542
1543 /* check whether a snapshot with name exist */
Markus Armbrusterf70edf92014-04-25 16:50:34 +02001544 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1545 &local_err);
1546 if (local_err) {
1547 error_propagate(errp, local_err);
Wenchao Xiabbe86012013-09-11 14:04:34 +08001548 return;
1549 } else if (ret) {
1550 error_setg(errp,
1551 "Snapshot with name '%s' already exists on device '%s'",
1552 name, device);
1553 return;
1554 }
1555
1556 /* 3. take the snapshot */
1557 sn = &state->sn;
1558 pstrcpy(sn->name, sizeof(sn->name), name);
1559 qemu_gettimeofday(&tv);
1560 sn->date_sec = tv.tv_sec;
1561 sn->date_nsec = tv.tv_usec * 1000;
1562 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1563
1564 ret1 = bdrv_snapshot_create(bs, sn);
1565 if (ret1 < 0) {
1566 error_setg_errno(errp, -ret1,
1567 "Failed to create snapshot '%s' on device '%s'",
1568 name, device);
1569 return;
1570 }
1571
1572 /* 4. succeed, mark a snapshot is created */
Fam Zheng507306c2015-10-23 11:08:13 +08001573 state->created = true;
Wenchao Xiabbe86012013-09-11 14:04:34 +08001574}
1575
John Snow50f43f02015-11-05 18:13:09 -05001576static void internal_snapshot_abort(BlkActionState *common)
Wenchao Xiabbe86012013-09-11 14:04:34 +08001577{
1578 InternalSnapshotState *state =
1579 DO_UPCAST(InternalSnapshotState, common, common);
1580 BlockDriverState *bs = state->bs;
1581 QEMUSnapshotInfo *sn = &state->sn;
1582 Error *local_error = NULL;
1583
Fam Zheng507306c2015-10-23 11:08:13 +08001584 if (!state->created) {
Wenchao Xiabbe86012013-09-11 14:04:34 +08001585 return;
1586 }
1587
1588 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001589 error_reportf_err(local_error,
1590 "Failed to delete snapshot with id '%s' and "
1591 "name '%s' on device '%s' in abort: ",
1592 sn->id_str, sn->name,
1593 bdrv_get_device_name(bs));
Wenchao Xiabbe86012013-09-11 14:04:34 +08001594 }
1595}
1596
John Snow50f43f02015-11-05 18:13:09 -05001597static void internal_snapshot_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001598{
1599 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1600 common, common);
1601
1602 if (state->aio_context) {
Fam Zheng507306c2015-10-23 11:08:13 +08001603 if (state->bs) {
1604 bdrv_drained_end(state->bs);
1605 }
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001606 aio_context_release(state->aio_context);
1607 }
1608}
1609
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001610/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001611typedef struct ExternalSnapshotState {
John Snow50f43f02015-11-05 18:13:09 -05001612 BlkActionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001613 BlockDriverState *old_bs;
1614 BlockDriverState *new_bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001615 AioContext *aio_context;
Kevin Wolf067acf22017-03-02 15:26:18 +01001616 bool overlay_appended;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001617} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001618
John Snow50f43f02015-11-05 18:13:09 -05001619static void external_snapshot_prepare(BlkActionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001620 Error **errp)
1621{
Max Reitz5b363932016-05-17 16:41:31 +02001622 int flags = 0;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001623 QDict *options = NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001624 Error *local_err = NULL;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001625 /* Device and node name of the image to generate the snapshot from */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001626 const char *device;
Benoît Canet0901f672014-01-23 21:31:38 +01001627 const char *node_name;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001628 /* Reference to the new image (for 'blockdev-snapshot') */
1629 const char *snapshot_ref;
1630 /* File name of the new image (for 'blockdev-snapshot-sync') */
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001631 const char *new_image_file;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001632 ExternalSnapshotState *state =
1633 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001634 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001635
Alberto Garcia43de7e22015-10-26 14:27:16 +02001636 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1637 * purpose but a different set of parameters */
1638 switch (action->type) {
1639 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1640 {
Eric Blake32bafa82016-03-17 16:48:37 -06001641 BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001642 device = s->node;
1643 node_name = s->node;
1644 new_image_file = NULL;
1645 snapshot_ref = s->overlay;
1646 }
1647 break;
1648 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1649 {
Eric Blake32bafa82016-03-17 16:48:37 -06001650 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001651 device = s->has_device ? s->device : NULL;
1652 node_name = s->has_node_name ? s->node_name : NULL;
1653 new_image_file = s->snapshot_file;
1654 snapshot_ref = NULL;
1655 }
1656 break;
1657 default:
1658 g_assert_not_reached();
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001659 }
1660
1661 /* start processing */
John Snow94d16a62015-11-05 18:13:18 -05001662 if (action_check_completion_mode(common, errp) < 0) {
1663 return;
1664 }
1665
Alberto Garcia43de7e22015-10-26 14:27:16 +02001666 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1667 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001668 return;
1669 }
1670
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001671 /* Acquire AioContext now so any threads operating on old_bs stop */
1672 state->aio_context = bdrv_get_aio_context(state->old_bs);
1673 aio_context_acquire(state->aio_context);
Fam Zhengda763e82015-10-23 11:08:10 +08001674 bdrv_drained_begin(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001675
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001676 if (!bdrv_is_inserted(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001677 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001678 return;
1679 }
1680
Fam Zheng3718d8a2014-05-23 21:29:43 +08001681 if (bdrv_op_is_blocked(state->old_bs,
1682 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001683 return;
1684 }
1685
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001686 if (!bdrv_is_read_only(state->old_bs)) {
1687 if (bdrv_flush(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001688 error_setg(errp, QERR_IO_ERROR);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001689 return;
1690 }
1691 }
1692
Benoît Canet212a5a82014-01-23 21:31:36 +01001693 if (!bdrv_is_first_non_filter(state->old_bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001694 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
Benoît Canetf6186f42013-10-02 14:33:48 +02001695 return;
1696 }
1697
Alberto Garcia43de7e22015-10-26 14:27:16 +02001698 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
Eric Blake32bafa82016-03-17 16:48:37 -06001699 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
Alberto Garcia43de7e22015-10-26 14:27:16 +02001700 const char *format = s->has_format ? s->format : "qcow2";
1701 enum NewImageMode mode;
1702 const char *snapshot_node_name =
1703 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001704
Alberto Garcia43de7e22015-10-26 14:27:16 +02001705 if (node_name && !snapshot_node_name) {
1706 error_setg(errp, "New snapshot node name missing");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001707 return;
1708 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001709
1710 if (snapshot_node_name &&
1711 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1712 error_setg(errp, "New snapshot node name already in use");
1713 return;
1714 }
1715
1716 flags = state->old_bs->open_flags;
Kevin Wolf4c844982016-02-29 13:12:26 +01001717 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001718
1719 /* create new image w/backing file */
1720 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1721 if (mode != NEW_IMAGE_MODE_EXISTING) {
Kevin Wolff86b8b52016-03-02 12:16:44 +01001722 int64_t size = bdrv_getlength(state->old_bs);
1723 if (size < 0) {
1724 error_setg_errno(errp, -size, "bdrv_getlength failed");
1725 return;
1726 }
Alberto Garcia43de7e22015-10-26 14:27:16 +02001727 bdrv_img_create(new_image_file, format,
1728 state->old_bs->filename,
1729 state->old_bs->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08001730 NULL, size, flags, false, &local_err);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001731 if (local_err) {
1732 error_propagate(errp, local_err);
1733 return;
1734 }
1735 }
1736
1737 options = qdict_new();
1738 if (s->has_snapshot_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05001739 qdict_put_str(options, "node-name", snapshot_node_name);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001740 }
Eric Blake46f5ac22017-04-27 16:58:17 -05001741 qdict_put_str(options, "driver", format);
Alberto Garcia43de7e22015-10-26 14:27:16 +02001742
1743 flags |= BDRV_O_NO_BACKING;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001744 }
1745
Max Reitz5b363932016-05-17 16:41:31 +02001746 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
1747 errp);
Max Reitzf67503e2014-02-18 18:33:05 +01001748 /* We will manually add the backing_hd field to the bs later */
Max Reitz5b363932016-05-17 16:41:31 +02001749 if (!state->new_bs) {
Alberto Garcia43de7e22015-10-26 14:27:16 +02001750 return;
1751 }
1752
Kevin Wolf1f0c4612016-03-22 18:38:44 +01001753 if (bdrv_has_blk(state->new_bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02001754 error_setg(errp, "The snapshot is already in use");
Alberto Garcia43de7e22015-10-26 14:27:16 +02001755 return;
1756 }
1757
1758 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1759 errp)) {
1760 return;
1761 }
1762
1763 if (state->new_bs->backing != NULL) {
1764 error_setg(errp, "The snapshot already has a backing image");
Alberto Garcia08b24cf2015-11-03 12:32:35 +02001765 return;
1766 }
1767
1768 if (!state->new_bs->drv->supports_backing) {
1769 error_setg(errp, "The snapshot does not support backing images");
Kevin Wolfb2c28322017-02-20 12:46:42 +01001770 return;
1771 }
1772
Fam Zhengc26a5ab2017-04-07 14:54:09 +08001773 bdrv_set_aio_context(state->new_bs, state->aio_context);
1774
Kevin Wolfb2c28322017-02-20 12:46:42 +01001775 /* This removes our old bs and adds the new bs. This is an operation that
1776 * can fail, so we need to do it in .prepare; undoing it for abort is
1777 * always possible. */
1778 bdrv_ref(state->new_bs);
1779 bdrv_append(state->new_bs, state->old_bs, &local_err);
1780 if (local_err) {
1781 error_propagate(errp, local_err);
1782 return;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001783 }
Kevin Wolf067acf22017-03-02 15:26:18 +01001784 state->overlay_appended = true;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001785}
1786
John Snow50f43f02015-11-05 18:13:09 -05001787static void external_snapshot_commit(BlkActionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001788{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001789 ExternalSnapshotState *state =
1790 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001791
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001792 /* We don't need (or want) to use the transactional
1793 * bdrv_reopen_multiple() across all the entries at once, because we
1794 * don't want to abort all of them if one of them fails the reopen */
Kevin Wolf4c844982016-02-29 13:12:26 +01001795 if (!state->old_bs->copy_on_read) {
1796 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1797 NULL);
1798 }
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001799}
1800
John Snow50f43f02015-11-05 18:13:09 -05001801static void external_snapshot_abort(BlkActionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001802{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001803 ExternalSnapshotState *state =
1804 DO_UPCAST(ExternalSnapshotState, common, common);
1805 if (state->new_bs) {
Kevin Wolf067acf22017-03-02 15:26:18 +01001806 if (state->overlay_appended) {
Jeff Cody719fc282017-06-07 09:55:22 -04001807 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
1808 close state->old_bs; we need it */
1809 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
Kevin Wolf5fe31c22017-03-06 16:20:51 +01001810 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
Jeff Cody719fc282017-06-07 09:55:22 -04001811 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
Kevin Wolfb2c28322017-02-20 12:46:42 +01001812 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001813 }
Fam Zhengda763e82015-10-23 11:08:10 +08001814}
1815
John Snow50f43f02015-11-05 18:13:09 -05001816static void external_snapshot_clean(BlkActionState *common)
Fam Zhengda763e82015-10-23 11:08:10 +08001817{
1818 ExternalSnapshotState *state =
1819 DO_UPCAST(ExternalSnapshotState, common, common);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001820 if (state->aio_context) {
Fam Zhengda763e82015-10-23 11:08:10 +08001821 bdrv_drained_end(state->old_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001822 aio_context_release(state->aio_context);
Kevin Wolfb2c28322017-02-20 12:46:42 +01001823 bdrv_unref(state->new_bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001824 }
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001825}
1826
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001827typedef struct DriveBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001828 BlkActionState common;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001829 BlockDriverState *bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001830 AioContext *aio_context;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001831 BlockJob *job;
1832} DriveBackupState;
1833
John Snow111049a2016-11-08 01:50:38 -05001834static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
Pavel Butsykin81206a82016-07-22 11:17:50 +03001835 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001836
John Snow50f43f02015-11-05 18:13:09 -05001837static void drive_backup_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001838{
1839 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001840 BlockDriverState *bs;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001841 DriveBackup *backup;
1842 Error *local_err = NULL;
1843
Eric Blake6a8f9662015-10-26 16:34:54 -06001844 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001845 backup = common->action->u.drive_backup.data;
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001846
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001847 bs = qmp_get_root_bs(backup->device, errp);
1848 if (!bs) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001849 return;
1850 }
1851
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001852 /* AioContext is released in .clean() */
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001853 state->aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001854 aio_context_acquire(state->aio_context);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02001855 bdrv_drained_begin(bs);
1856 state->bs = bs;
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001857
John Snow111049a2016-11-08 01:50:38 -05001858 state->job = do_drive_backup(backup, common->block_job_txn, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001859 if (local_err) {
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001860 error_propagate(errp, local_err);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001861 return;
1862 }
John Snow111049a2016-11-08 01:50:38 -05001863}
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001864
John Snow111049a2016-11-08 01:50:38 -05001865static void drive_backup_commit(BlkActionState *common)
1866{
1867 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1868 assert(state->job);
1869 block_job_start(state->job);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001870}
1871
John Snow50f43f02015-11-05 18:13:09 -05001872static void drive_backup_abort(BlkActionState *common)
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001873{
1874 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001875
John Snow111049a2016-11-08 01:50:38 -05001876 if (state->job) {
1877 block_job_cancel_sync(state->job);
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001878 }
1879}
1880
John Snow50f43f02015-11-05 18:13:09 -05001881static void drive_backup_clean(BlkActionState *common)
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001882{
1883 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1884
1885 if (state->aio_context) {
Fam Zheng1fdd4b72015-10-23 11:08:11 +08001886 bdrv_drained_end(state->bs);
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00001887 aio_context_release(state->aio_context);
1888 }
1889}
1890
Fam Zhengbd8baec2014-12-18 18:37:06 +08001891typedef struct BlockdevBackupState {
John Snow50f43f02015-11-05 18:13:09 -05001892 BlkActionState common;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001893 BlockDriverState *bs;
1894 BlockJob *job;
1895 AioContext *aio_context;
1896} BlockdevBackupState;
1897
John Snow111049a2016-11-08 01:50:38 -05001898static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
1899 Error **errp);
John Snow78f51fd2015-11-05 18:13:17 -05001900
John Snow50f43f02015-11-05 18:13:09 -05001901static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001902{
1903 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1904 BlockdevBackup *backup;
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001905 BlockDriverState *bs, *target;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001906 Error *local_err = NULL;
1907
Eric Blake6a8f9662015-10-26 16:34:54 -06001908 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
Eric Blake32bafa82016-03-17 16:48:37 -06001909 backup = common->action->u.blockdev_backup.data;
Fam Zhengbd8baec2014-12-18 18:37:06 +08001910
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001911 bs = qmp_get_root_bs(backup->device, errp);
1912 if (!bs) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001913 return;
1914 }
1915
Kevin Wolf39d990a2016-08-02 19:22:04 +02001916 target = bdrv_lookup_bs(backup->target, backup->target, errp);
Max Reitz5433c242015-10-19 17:53:29 +02001917 if (!target) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001918 return;
1919 }
1920
1921 /* AioContext is released in .clean() */
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001922 state->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf39d990a2016-08-02 19:22:04 +02001923 if (state->aio_context != bdrv_get_aio_context(target)) {
Fam Zhengbd8baec2014-12-18 18:37:06 +08001924 state->aio_context = NULL;
1925 error_setg(errp, "Backup between two IO threads is not implemented");
1926 return;
1927 }
1928 aio_context_acquire(state->aio_context);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02001929 state->bs = bs;
Fam Zhengff52bf32015-10-23 11:08:12 +08001930 bdrv_drained_begin(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001931
John Snow111049a2016-11-08 01:50:38 -05001932 state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001933 if (local_err) {
1934 error_propagate(errp, local_err);
1935 return;
1936 }
John Snow111049a2016-11-08 01:50:38 -05001937}
Fam Zhengbd8baec2014-12-18 18:37:06 +08001938
John Snow111049a2016-11-08 01:50:38 -05001939static void blockdev_backup_commit(BlkActionState *common)
1940{
1941 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1942 assert(state->job);
1943 block_job_start(state->job);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001944}
1945
John Snow50f43f02015-11-05 18:13:09 -05001946static void blockdev_backup_abort(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001947{
1948 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001949
John Snow111049a2016-11-08 01:50:38 -05001950 if (state->job) {
1951 block_job_cancel_sync(state->job);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001952 }
1953}
1954
John Snow50f43f02015-11-05 18:13:09 -05001955static void blockdev_backup_clean(BlkActionState *common)
Fam Zhengbd8baec2014-12-18 18:37:06 +08001956{
1957 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1958
1959 if (state->aio_context) {
Fam Zhengff52bf32015-10-23 11:08:12 +08001960 bdrv_drained_end(state->bs);
Fam Zhengbd8baec2014-12-18 18:37:06 +08001961 aio_context_release(state->aio_context);
1962 }
1963}
1964
Fam Zhengdf9a6812015-11-09 18:16:54 +08001965typedef struct BlockDirtyBitmapState {
John Snow50f43f02015-11-05 18:13:09 -05001966 BlkActionState common;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001967 BdrvDirtyBitmap *bitmap;
1968 BlockDriverState *bs;
1969 AioContext *aio_context;
1970 HBitmap *backup;
1971 bool prepared;
1972} BlockDirtyBitmapState;
1973
John Snow50f43f02015-11-05 18:13:09 -05001974static void block_dirty_bitmap_add_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08001975 Error **errp)
1976{
1977 Error *local_err = NULL;
1978 BlockDirtyBitmapAdd *action;
1979 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
1980 common, common);
1981
John Snow94d16a62015-11-05 18:13:18 -05001982 if (action_check_completion_mode(common, errp) < 0) {
1983 return;
1984 }
1985
Eric Blake32bafa82016-03-17 16:48:37 -06001986 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08001987 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1988 qmp_block_dirty_bitmap_add(action->node, action->name,
1989 action->has_granularity, action->granularity,
1990 &local_err);
1991
1992 if (!local_err) {
1993 state->prepared = true;
1994 } else {
1995 error_propagate(errp, local_err);
1996 }
1997}
1998
John Snow50f43f02015-11-05 18:13:09 -05001999static void block_dirty_bitmap_add_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002000{
2001 BlockDirtyBitmapAdd *action;
2002 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2003 common, common);
2004
Eric Blake32bafa82016-03-17 16:48:37 -06002005 action = common->action->u.block_dirty_bitmap_add.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002006 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2007 * then the node reference and bitmap name must have been valid.
2008 */
2009 if (state->prepared) {
2010 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2011 }
2012}
2013
John Snow50f43f02015-11-05 18:13:09 -05002014static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
Fam Zhengdf9a6812015-11-09 18:16:54 +08002015 Error **errp)
2016{
2017 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2018 common, common);
2019 BlockDirtyBitmap *action;
2020
John Snow94d16a62015-11-05 18:13:18 -05002021 if (action_check_completion_mode(common, errp) < 0) {
2022 return;
2023 }
2024
Eric Blake32bafa82016-03-17 16:48:37 -06002025 action = common->action->u.block_dirty_bitmap_clear.data;
Fam Zhengdf9a6812015-11-09 18:16:54 +08002026 state->bitmap = block_dirty_bitmap_lookup(action->node,
2027 action->name,
2028 &state->bs,
2029 &state->aio_context,
2030 errp);
2031 if (!state->bitmap) {
2032 return;
2033 }
2034
2035 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2036 error_setg(errp, "Cannot modify a frozen bitmap");
2037 return;
2038 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2039 error_setg(errp, "Cannot clear a disabled bitmap");
2040 return;
2041 }
2042
2043 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2044 /* AioContext is released in .clean() */
2045}
2046
John Snow50f43f02015-11-05 18:13:09 -05002047static void block_dirty_bitmap_clear_abort(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002048{
2049 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2050 common, common);
2051
John Snow184dd9c2017-03-15 17:28:11 -04002052 if (state->backup) {
2053 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2054 }
Fam Zhengdf9a6812015-11-09 18:16:54 +08002055}
2056
John Snow50f43f02015-11-05 18:13:09 -05002057static void block_dirty_bitmap_clear_commit(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002058{
2059 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2060 common, common);
2061
2062 hbitmap_free(state->backup);
2063}
2064
John Snow50f43f02015-11-05 18:13:09 -05002065static void block_dirty_bitmap_clear_clean(BlkActionState *common)
Fam Zhengdf9a6812015-11-09 18:16:54 +08002066{
2067 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2068 common, common);
2069
2070 if (state->aio_context) {
2071 aio_context_release(state->aio_context);
2072 }
2073}
2074
John Snow50f43f02015-11-05 18:13:09 -05002075static void abort_prepare(BlkActionState *common, Error **errp)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002076{
2077 error_setg(errp, "Transaction aborted using Abort action");
2078}
2079
John Snow50f43f02015-11-05 18:13:09 -05002080static void abort_commit(BlkActionState *common)
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002081{
Stefan Weildfc6f862013-07-25 18:21:28 +02002082 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002083}
2084
John Snow50f43f02015-11-05 18:13:09 -05002085static const BlkActionOps actions[] = {
Alberto Garcia43de7e22015-10-26 14:27:16 +02002086 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2087 .instance_size = sizeof(ExternalSnapshotState),
2088 .prepare = external_snapshot_prepare,
2089 .commit = external_snapshot_commit,
2090 .abort = external_snapshot_abort,
Alberto Garcia4ad6f3d2015-11-13 15:00:24 +02002091 .clean = external_snapshot_clean,
Alberto Garcia43de7e22015-10-26 14:27:16 +02002092 },
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002093 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002094 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002095 .prepare = external_snapshot_prepare,
2096 .commit = external_snapshot_commit,
2097 .abort = external_snapshot_abort,
Fam Zhengda763e82015-10-23 11:08:10 +08002098 .clean = external_snapshot_clean,
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002099 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002100 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2101 .instance_size = sizeof(DriveBackupState),
2102 .prepare = drive_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002103 .commit = drive_backup_commit,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002104 .abort = drive_backup_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002105 .clean = drive_backup_clean,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02002106 },
Fam Zhengbd8baec2014-12-18 18:37:06 +08002107 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2108 .instance_size = sizeof(BlockdevBackupState),
2109 .prepare = blockdev_backup_prepare,
John Snow111049a2016-11-08 01:50:38 -05002110 .commit = blockdev_backup_commit,
Fam Zhengbd8baec2014-12-18 18:37:06 +08002111 .abort = blockdev_backup_abort,
2112 .clean = blockdev_backup_clean,
2113 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002114 [TRANSACTION_ACTION_KIND_ABORT] = {
John Snow50f43f02015-11-05 18:13:09 -05002115 .instance_size = sizeof(BlkActionState),
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02002116 .prepare = abort_prepare,
2117 .commit = abort_commit,
2118 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08002119 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2120 .instance_size = sizeof(InternalSnapshotState),
2121 .prepare = internal_snapshot_prepare,
2122 .abort = internal_snapshot_abort,
Stefan Hajnoczi5d6e96e2014-11-21 10:48:59 +00002123 .clean = internal_snapshot_clean,
Wenchao Xiabbe86012013-09-11 14:04:34 +08002124 },
Fam Zhengdf9a6812015-11-09 18:16:54 +08002125 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2126 .instance_size = sizeof(BlockDirtyBitmapState),
2127 .prepare = block_dirty_bitmap_add_prepare,
2128 .abort = block_dirty_bitmap_add_abort,
2129 },
2130 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2131 .instance_size = sizeof(BlockDirtyBitmapState),
2132 .prepare = block_dirty_bitmap_clear_prepare,
2133 .commit = block_dirty_bitmap_clear_commit,
2134 .abort = block_dirty_bitmap_clear_abort,
2135 .clean = block_dirty_bitmap_clear_clean,
2136 }
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002137};
2138
John Snow94d16a62015-11-05 18:13:18 -05002139/**
2140 * Allocate a TransactionProperties structure if necessary, and fill
2141 * that structure with desired defaults if they are unset.
2142 */
2143static TransactionProperties *get_transaction_properties(
2144 TransactionProperties *props)
2145{
2146 if (!props) {
2147 props = g_new0(TransactionProperties, 1);
2148 }
2149
2150 if (!props->has_completion_mode) {
2151 props->has_completion_mode = true;
2152 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2153 }
2154
2155 return props;
2156}
2157
Jeff Cody8802d1f2012-02-28 15:54:06 -05002158/*
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002159 * 'Atomic' group operations. The operations are performed as a set, and if
2160 * any fail then we roll back all operations in the group.
Jeff Cody8802d1f2012-02-28 15:54:06 -05002161 */
John Snow94d16a62015-11-05 18:13:18 -05002162void qmp_transaction(TransactionActionList *dev_list,
2163 bool has_props,
2164 struct TransactionProperties *props,
2165 Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05002166{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002167 TransactionActionList *dev_entry = dev_list;
John Snow94d16a62015-11-05 18:13:18 -05002168 BlockJobTxn *block_job_txn = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002169 BlkActionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02002170 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002171
John Snow50f43f02015-11-05 18:13:09 -05002172 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05002173 QSIMPLEQ_INIT(&snap_bdrv_states);
2174
John Snow94d16a62015-11-05 18:13:18 -05002175 /* Does this transaction get canceled as a group on failure?
2176 * If not, we don't really need to make a BlockJobTxn.
2177 */
2178 props = get_transaction_properties(props);
2179 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2180 block_job_txn = block_job_txn_new();
2181 }
2182
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002183 /* drain all i/o before any operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002184 bdrv_drain_all();
2185
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002186 /* We don't do anything in this loop that commits us to the operations */
Jeff Cody8802d1f2012-02-28 15:54:06 -05002187 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02002188 TransactionAction *dev_info = NULL;
John Snow50f43f02015-11-05 18:13:09 -05002189 const BlkActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002190
Jeff Cody8802d1f2012-02-28 15:54:06 -05002191 dev_info = dev_entry->value;
2192 dev_entry = dev_entry->next;
2193
Eric Blake6a8f9662015-10-26 16:34:54 -06002194 assert(dev_info->type < ARRAY_SIZE(actions));
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002195
Eric Blake6a8f9662015-10-26 16:34:54 -06002196 ops = &actions[dev_info->type];
Max Reitzaa3fe712013-09-12 14:57:27 +02002197 assert(ops->instance_size > 0);
2198
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002199 state = g_malloc0(ops->instance_size);
2200 state->ops = ops;
2201 state->action = dev_info;
John Snow94d16a62015-11-05 18:13:18 -05002202 state->block_job_txn = block_job_txn;
2203 state->txn_props = props;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002204 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002205
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002206 state->ops->prepare(state, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002207 if (local_err) {
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002208 error_propagate(errp, local_err);
2209 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01002210 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002211 }
2212
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002213 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02002214 if (state->ops->commit) {
2215 state->ops->commit(state);
2216 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002217 }
2218
2219 /* success */
2220 goto exit;
2221
2222delete_and_fail:
Stefan Hajnoczib756b9c2014-11-21 10:48:57 +00002223 /* failure, and it is all-or-none; roll back all operations */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002224 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2225 if (state->ops->abort) {
2226 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002227 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05002228 }
2229exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002230 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2231 if (state->ops->clean) {
2232 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08002233 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02002234 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002235 }
John Snow94d16a62015-11-05 18:13:18 -05002236 if (!has_props) {
2237 qapi_free_TransactionProperties(props);
2238 }
2239 block_job_txn_unref(block_job_txn);
Jeff Cody8802d1f2012-02-28 15:54:06 -05002240}
2241
Kevin Wolffbe2d812016-09-20 13:38:46 +02002242void qmp_eject(bool has_device, const char *device,
2243 bool has_id, const char *id,
2244 bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002245{
Max Reitz38f54bd2015-10-26 21:39:12 +01002246 Error *local_err = NULL;
John Snow3a3086b2016-05-18 17:53:40 -04002247 int rc;
Markus Armbruster666daa62010-06-02 18:48:27 +02002248
John Snow3a3086b2016-05-18 17:53:40 -04002249 if (!has_force) {
2250 force = false;
2251 }
2252
Kevin Wolffbe2d812016-09-20 13:38:46 +02002253 rc = do_open_tray(has_device ? device : NULL,
2254 has_id ? id : NULL,
2255 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002256 if (rc && rc != -ENOSYS) {
Max Reitz38f54bd2015-10-26 21:39:12 +01002257 error_propagate(errp, local_err);
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02002258 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002259 }
Colin Lordbf18bee2016-06-06 14:15:22 -04002260 error_free(local_err);
John Snow3a3086b2016-05-18 17:53:40 -04002261
Kevin Wolffbe2d812016-09-20 13:38:46 +02002262 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02002263}
2264
Benoît Canet12d3ba82014-01-23 21:31:35 +01002265void qmp_block_passwd(bool has_device, const char *device,
2266 bool has_node_name, const char *node_name,
2267 const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02002268{
Benoît Canet12d3ba82014-01-23 21:31:35 +01002269 Error *local_err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02002270 BlockDriverState *bs;
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002271 AioContext *aio_context;
Markus Armbruster666daa62010-06-02 18:48:27 +02002272
Benoît Canet12d3ba82014-01-23 21:31:35 +01002273 bs = bdrv_lookup_bs(has_device ? device : NULL,
2274 has_node_name ? node_name : NULL,
2275 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002276 if (local_err) {
Benoît Canet12d3ba82014-01-23 21:31:35 +01002277 error_propagate(errp, local_err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02002278 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02002279 }
2280
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002281 aio_context = bdrv_get_aio_context(bs);
2282 aio_context_acquire(aio_context);
2283
Markus Armbruster4d2855a2015-01-29 10:37:00 +01002284 bdrv_add_key(bs, password, errp);
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002285
Stefan Hajnoczie3442092014-11-19 14:19:44 +00002286 aio_context_release(aio_context);
Markus Armbruster666daa62010-06-02 18:48:27 +02002287}
2288
Colin Lordbf18bee2016-06-06 14:15:22 -04002289/*
2290 * Attempt to open the tray of @device.
2291 * If @force, ignore its tray lock.
2292 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2293 * On error, store an error through @errp and return -errno.
2294 * If @device does not exist, return -ENODEV.
2295 * If it has no removable media, return -ENOTSUP.
2296 * If it has no tray, return -ENOSYS.
2297 * If the guest was asked to open the tray, return -EINPROGRESS.
2298 * Else, return 0.
John Snow3a3086b2016-05-18 17:53:40 -04002299 */
Kevin Wolfb33945c2016-09-20 13:38:43 +02002300static int do_open_tray(const char *blk_name, const char *qdev_id,
2301 bool force, Error **errp)
Max Reitz7d8a9f72015-10-26 21:39:08 +01002302{
2303 BlockBackend *blk;
Kevin Wolfb33945c2016-09-20 13:38:43 +02002304 const char *device = qdev_id ?: blk_name;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002305 bool locked;
2306
Kevin Wolfb33945c2016-09-20 13:38:43 +02002307 blk = qmp_get_blk(blk_name, qdev_id, errp);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002308 if (!blk) {
John Snow3a3086b2016-05-18 17:53:40 -04002309 return -ENODEV;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002310 }
2311
2312 if (!blk_dev_has_removable_media(blk)) {
2313 error_setg(errp, "Device '%s' is not removable", device);
John Snow3a3086b2016-05-18 17:53:40 -04002314 return -ENOTSUP;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002315 }
2316
Max Reitz12c7ec82016-01-29 20:49:11 +01002317 if (!blk_dev_has_tray(blk)) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002318 error_setg(errp, "Device '%s' does not have a tray", device);
2319 return -ENOSYS;
Max Reitz12c7ec82016-01-29 20:49:11 +01002320 }
2321
Max Reitz7d8a9f72015-10-26 21:39:08 +01002322 if (blk_dev_is_tray_open(blk)) {
John Snow3a3086b2016-05-18 17:53:40 -04002323 return 0;
Max Reitz7d8a9f72015-10-26 21:39:08 +01002324 }
2325
2326 locked = blk_dev_is_medium_locked(blk);
2327 if (locked) {
2328 blk_dev_eject_request(blk, force);
2329 }
2330
2331 if (!locked || force) {
Kevin Wolf39829a02017-01-24 14:21:41 +01002332 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002333 }
John Snow3a3086b2016-05-18 17:53:40 -04002334
2335 if (locked && !force) {
Colin Lordbf18bee2016-06-06 14:15:22 -04002336 error_setg(errp, "Device '%s' is locked and force was not specified, "
2337 "wait for tray to open and try again", device);
2338 return -EINPROGRESS;
John Snow3a3086b2016-05-18 17:53:40 -04002339 }
2340
2341 return 0;
2342}
2343
Kevin Wolfb33945c2016-09-20 13:38:43 +02002344void qmp_blockdev_open_tray(bool has_device, const char *device,
2345 bool has_id, const char *id,
2346 bool has_force, bool force,
John Snow3a3086b2016-05-18 17:53:40 -04002347 Error **errp)
2348{
Colin Lordbf18bee2016-06-06 14:15:22 -04002349 Error *local_err = NULL;
2350 int rc;
2351
John Snow3a3086b2016-05-18 17:53:40 -04002352 if (!has_force) {
2353 force = false;
2354 }
Kevin Wolfb33945c2016-09-20 13:38:43 +02002355 rc = do_open_tray(has_device ? device : NULL,
2356 has_id ? id : NULL,
2357 force, &local_err);
Colin Lordbf18bee2016-06-06 14:15:22 -04002358 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
2359 error_propagate(errp, local_err);
2360 return;
2361 }
2362 error_free(local_err);
Max Reitz7d8a9f72015-10-26 21:39:08 +01002363}
2364
Kevin Wolfb33945c2016-09-20 13:38:43 +02002365void qmp_blockdev_close_tray(bool has_device, const char *device,
2366 bool has_id, const char *id,
2367 Error **errp)
Max Reitzabaaf592015-10-26 21:39:09 +01002368{
2369 BlockBackend *blk;
Kevin Wolf39829a02017-01-24 14:21:41 +01002370 Error *local_err = NULL;
Max Reitzabaaf592015-10-26 21:39:09 +01002371
Kevin Wolfb33945c2016-09-20 13:38:43 +02002372 device = has_device ? device : NULL;
2373 id = has_id ? id : NULL;
2374
2375 blk = qmp_get_blk(device, id, errp);
Max Reitzabaaf592015-10-26 21:39:09 +01002376 if (!blk) {
Max Reitzabaaf592015-10-26 21:39:09 +01002377 return;
2378 }
2379
2380 if (!blk_dev_has_removable_media(blk)) {
Kevin Wolfb33945c2016-09-20 13:38:43 +02002381 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitzabaaf592015-10-26 21:39:09 +01002382 return;
2383 }
2384
Max Reitz12c7ec82016-01-29 20:49:11 +01002385 if (!blk_dev_has_tray(blk)) {
2386 /* Ignore this command on tray-less devices */
2387 return;
2388 }
2389
Max Reitzabaaf592015-10-26 21:39:09 +01002390 if (!blk_dev_is_tray_open(blk)) {
2391 return;
2392 }
2393
Kevin Wolf39829a02017-01-24 14:21:41 +01002394 blk_dev_change_media_cb(blk, true, &local_err);
2395 if (local_err) {
2396 error_propagate(errp, local_err);
2397 return;
2398 }
Max Reitzabaaf592015-10-26 21:39:09 +01002399}
2400
Kevin Wolf00949ba2016-09-20 13:38:45 +02002401void qmp_x_blockdev_remove_medium(bool has_device, const char *device,
2402 bool has_id, const char *id, Error **errp)
Max Reitz2814f672015-10-26 21:39:10 +01002403{
2404 BlockBackend *blk;
2405 BlockDriverState *bs;
2406 AioContext *aio_context;
Kevin Wolf00949ba2016-09-20 13:38:45 +02002407 bool has_attached_device;
Max Reitz2814f672015-10-26 21:39:10 +01002408
Kevin Wolf00949ba2016-09-20 13:38:45 +02002409 device = has_device ? device : NULL;
2410 id = has_id ? id : NULL;
2411
2412 blk = qmp_get_blk(device, id, errp);
Max Reitz2814f672015-10-26 21:39:10 +01002413 if (!blk) {
Max Reitz2814f672015-10-26 21:39:10 +01002414 return;
2415 }
2416
2417 /* For BBs without a device, we can exchange the BDS tree at will */
Kevin Wolf00949ba2016-09-20 13:38:45 +02002418 has_attached_device = blk_get_attached_dev(blk);
Max Reitz2814f672015-10-26 21:39:10 +01002419
Kevin Wolf00949ba2016-09-20 13:38:45 +02002420 if (has_attached_device && !blk_dev_has_removable_media(blk)) {
2421 error_setg(errp, "Device '%s' is not removable", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002422 return;
2423 }
2424
Kevin Wolf00949ba2016-09-20 13:38:45 +02002425 if (has_attached_device && blk_dev_has_tray(blk) &&
2426 !blk_dev_is_tray_open(blk))
2427 {
2428 error_setg(errp, "Tray of device '%s' is not open", device ?: id);
Max Reitz2814f672015-10-26 21:39:10 +01002429 return;
2430 }
2431
2432 bs = blk_bs(blk);
2433 if (!bs) {
2434 return;
2435 }
2436
2437 aio_context = bdrv_get_aio_context(bs);
2438 aio_context_acquire(aio_context);
2439
2440 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2441 goto out;
2442 }
2443
Max Reitz2814f672015-10-26 21:39:10 +01002444 blk_remove_bs(blk);
2445
Max Reitz12c7ec82016-01-29 20:49:11 +01002446 if (!blk_dev_has_tray(blk)) {
2447 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2448 * called at all); therefore, the medium needs to be ejected here.
2449 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2450 * value passed here (i.e. false). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002451 blk_dev_change_media_cb(blk, false, &error_abort);
Max Reitz12c7ec82016-01-29 20:49:11 +01002452 }
2453
Max Reitz2814f672015-10-26 21:39:10 +01002454out:
2455 aio_context_release(aio_context);
2456}
2457
Kevin Wolf716df212016-09-20 13:38:44 +02002458static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
Max Reitzd1299882015-10-26 21:39:11 +01002459 BlockDriverState *bs, Error **errp)
2460{
Kevin Wolf39829a02017-01-24 14:21:41 +01002461 Error *local_err = NULL;
Max Reitzd1299882015-10-26 21:39:11 +01002462 bool has_device;
Kevin Wolfd7086422017-01-13 19:02:32 +01002463 int ret;
Max Reitzd1299882015-10-26 21:39:11 +01002464
Max Reitzd1299882015-10-26 21:39:11 +01002465 /* For BBs without a device, we can exchange the BDS tree at will */
2466 has_device = blk_get_attached_dev(blk);
2467
2468 if (has_device && !blk_dev_has_removable_media(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002469 error_setg(errp, "Device is not removable");
Max Reitzd1299882015-10-26 21:39:11 +01002470 return;
2471 }
2472
Max Reitz12c7ec82016-01-29 20:49:11 +01002473 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002474 error_setg(errp, "Tray of the device is not open");
Max Reitzd1299882015-10-26 21:39:11 +01002475 return;
2476 }
2477
2478 if (blk_bs(blk)) {
Kevin Wolf716df212016-09-20 13:38:44 +02002479 error_setg(errp, "There already is a medium in the device");
Max Reitzd1299882015-10-26 21:39:11 +01002480 return;
2481 }
2482
Kevin Wolfd7086422017-01-13 19:02:32 +01002483 ret = blk_insert_bs(blk, bs, errp);
2484 if (ret < 0) {
2485 return;
2486 }
Max Reitzd1299882015-10-26 21:39:11 +01002487
Max Reitz12c7ec82016-01-29 20:49:11 +01002488 if (!blk_dev_has_tray(blk)) {
2489 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2490 * called at all); therefore, the medium needs to be pushed into the
2491 * slot here.
2492 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2493 * value passed here (i.e. true). */
Kevin Wolf39829a02017-01-24 14:21:41 +01002494 blk_dev_change_media_cb(blk, true, &local_err);
2495 if (local_err) {
2496 error_propagate(errp, local_err);
2497 blk_remove_bs(blk);
2498 return;
2499 }
Max Reitz12c7ec82016-01-29 20:49:11 +01002500 }
Max Reitzd1299882015-10-26 21:39:11 +01002501}
2502
Kevin Wolf716df212016-09-20 13:38:44 +02002503void qmp_x_blockdev_insert_medium(bool has_device, const char *device,
2504 bool has_id, const char *id,
2505 const char *node_name, Error **errp)
Max Reitzd1299882015-10-26 21:39:11 +01002506{
Kevin Wolf716df212016-09-20 13:38:44 +02002507 BlockBackend *blk;
Max Reitzd1299882015-10-26 21:39:11 +01002508 BlockDriverState *bs;
2509
Kevin Wolf716df212016-09-20 13:38:44 +02002510 blk = qmp_get_blk(has_device ? device : NULL,
2511 has_id ? id : NULL,
2512 errp);
2513 if (!blk) {
2514 return;
2515 }
2516
Max Reitzd1299882015-10-26 21:39:11 +01002517 bs = bdrv_find_node(node_name);
2518 if (!bs) {
2519 error_setg(errp, "Node '%s' not found", node_name);
2520 return;
2521 }
2522
Kevin Wolf1f0c4612016-03-22 18:38:44 +01002523 if (bdrv_has_blk(bs)) {
Kevin Wolfe467da72016-09-21 14:56:09 +02002524 error_setg(errp, "Node '%s' is already in use", node_name);
Max Reitzd1299882015-10-26 21:39:11 +01002525 return;
2526 }
2527
Kevin Wolf716df212016-09-20 13:38:44 +02002528 qmp_blockdev_insert_anon_medium(blk, bs, errp);
Max Reitzd1299882015-10-26 21:39:11 +01002529}
2530
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002531void qmp_blockdev_change_medium(bool has_device, const char *device,
2532 bool has_id, const char *id,
2533 const char *filename,
Max Reitz24fb4132015-11-06 16:27:06 +01002534 bool has_format, const char *format,
Max Reitz39ff43d2015-11-11 04:49:44 +01002535 bool has_read_only,
2536 BlockdevChangeReadOnlyMode read_only,
Max Reitz24fb4132015-11-06 16:27:06 +01002537 Error **errp)
Max Reitzde2c6c02015-10-26 21:39:13 +01002538{
2539 BlockBackend *blk;
2540 BlockDriverState *medium_bs = NULL;
Max Reitz5b363932016-05-17 16:41:31 +02002541 int bdrv_flags;
Kevin Wolfb85114f2016-09-12 19:08:31 +02002542 bool detect_zeroes;
Colin Lord38a53d52016-06-08 13:56:28 -04002543 int rc;
Max Reitzde2c6c02015-10-26 21:39:13 +01002544 QDict *options = NULL;
2545 Error *err = NULL;
2546
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002547 blk = qmp_get_blk(has_device ? device : NULL,
2548 has_id ? id : NULL,
2549 errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002550 if (!blk) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002551 goto fail;
2552 }
2553
2554 if (blk_bs(blk)) {
2555 blk_update_root_state(blk);
2556 }
2557
2558 bdrv_flags = blk_get_open_flags_from_root_state(blk);
Alyssa Milburn156abc22016-02-06 13:36:18 +00002559 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
2560 BDRV_O_PROTOCOL);
Max Reitzde2c6c02015-10-26 21:39:13 +01002561
Max Reitz39ff43d2015-11-11 04:49:44 +01002562 if (!has_read_only) {
2563 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2564 }
2565
2566 switch (read_only) {
2567 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2568 break;
2569
2570 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2571 bdrv_flags &= ~BDRV_O_RDWR;
2572 break;
2573
2574 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2575 bdrv_flags |= BDRV_O_RDWR;
2576 break;
2577
2578 default:
2579 abort();
2580 }
2581
Kevin Wolfb85114f2016-09-12 19:08:31 +02002582 options = qdict_new();
2583 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
Eric Blake46f5ac22017-04-27 16:58:17 -05002584 qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off");
Kevin Wolfb85114f2016-09-12 19:08:31 +02002585
Max Reitz24fb4132015-11-06 16:27:06 +01002586 if (has_format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05002587 qdict_put_str(options, "driver", format);
Max Reitzde2c6c02015-10-26 21:39:13 +01002588 }
2589
Max Reitz5b363932016-05-17 16:41:31 +02002590 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
2591 if (!medium_bs) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002592 goto fail;
2593 }
2594
Max Reitzde2c6c02015-10-26 21:39:13 +01002595 bdrv_add_key(medium_bs, NULL, &err);
2596 if (err) {
2597 error_propagate(errp, err);
2598 goto fail;
2599 }
2600
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002601 rc = do_open_tray(has_device ? device : NULL,
2602 has_id ? id : NULL,
2603 false, &err);
Colin Lord38a53d52016-06-08 13:56:28 -04002604 if (rc && rc != -ENOSYS) {
Max Reitzde2c6c02015-10-26 21:39:13 +01002605 error_propagate(errp, err);
2606 goto fail;
2607 }
Colin Lord38a53d52016-06-08 13:56:28 -04002608 error_free(err);
2609 err = NULL;
Max Reitzde2c6c02015-10-26 21:39:13 +01002610
Kevin Wolf24df38b2016-09-26 15:03:00 +02002611 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002612 if (err) {
2613 error_propagate(errp, err);
2614 goto fail;
2615 }
2616
Kevin Wolf716df212016-09-20 13:38:44 +02002617 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
Max Reitzde2c6c02015-10-26 21:39:13 +01002618 if (err) {
2619 error_propagate(errp, err);
2620 goto fail;
2621 }
2622
Kevin Wolf70e2cb32016-09-20 13:38:47 +02002623 qmp_blockdev_close_tray(has_device, device, has_id, id, errp);
Max Reitzde2c6c02015-10-26 21:39:13 +01002624
2625fail:
2626 /* If the medium has been inserted, the device has its own reference, so
2627 * ours must be relinquished; and if it has not been inserted successfully,
2628 * the reference must be relinquished anyway */
2629 bdrv_unref(medium_bs);
2630}
2631
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002632/* throttling disk I/O limits */
Eric Blake4dc93972016-07-13 21:50:21 -06002633void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002634{
Benoît Canetcc0681c2013-09-02 14:14:39 +02002635 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002636 BlockDriverState *bs;
Fam Zhenga0e85442015-03-02 19:36:48 +08002637 BlockBackend *blk;
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002638 AioContext *aio_context;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002639
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002640 blk = qmp_get_blk(arg->has_device ? arg->device : NULL,
2641 arg->has_id ? arg->id : NULL,
2642 errp);
Fam Zhenga0e85442015-03-02 19:36:48 +08002643 if (!blk) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02002644 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002645 }
Max Reitz5433c242015-10-19 17:53:29 +02002646
2647 aio_context = blk_get_aio_context(blk);
2648 aio_context_acquire(aio_context);
2649
Fam Zhenga0e85442015-03-02 19:36:48 +08002650 bs = blk_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002651 if (!bs) {
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002652 error_setg(errp, "Device has no medium");
Max Reitz5433c242015-10-19 17:53:29 +02002653 goto out;
2654 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002655
Alberto Garcia1588ab52016-02-18 12:27:00 +02002656 throttle_config_init(&cfg);
Eric Blake4dc93972016-07-13 21:50:21 -06002657 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
2658 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
2659 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002660
Eric Blake4dc93972016-07-13 21:50:21 -06002661 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
2662 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
2663 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
Benoît Canetcc0681c2013-09-02 14:14:39 +02002664
Eric Blake4dc93972016-07-13 21:50:21 -06002665 if (arg->has_bps_max) {
2666 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002667 }
Eric Blake4dc93972016-07-13 21:50:21 -06002668 if (arg->has_bps_rd_max) {
2669 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002670 }
Eric Blake4dc93972016-07-13 21:50:21 -06002671 if (arg->has_bps_wr_max) {
2672 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002673 }
Eric Blake4dc93972016-07-13 21:50:21 -06002674 if (arg->has_iops_max) {
2675 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002676 }
Eric Blake4dc93972016-07-13 21:50:21 -06002677 if (arg->has_iops_rd_max) {
2678 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002679 }
Eric Blake4dc93972016-07-13 21:50:21 -06002680 if (arg->has_iops_wr_max) {
2681 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
Benoît Canet3e9fab62013-09-02 14:14:40 +02002682 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002683
Eric Blake4dc93972016-07-13 21:50:21 -06002684 if (arg->has_bps_max_length) {
2685 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002686 }
Eric Blake4dc93972016-07-13 21:50:21 -06002687 if (arg->has_bps_rd_max_length) {
2688 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002689 }
Eric Blake4dc93972016-07-13 21:50:21 -06002690 if (arg->has_bps_wr_max_length) {
2691 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002692 }
Eric Blake4dc93972016-07-13 21:50:21 -06002693 if (arg->has_iops_max_length) {
2694 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002695 }
Eric Blake4dc93972016-07-13 21:50:21 -06002696 if (arg->has_iops_rd_max_length) {
2697 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002698 }
Eric Blake4dc93972016-07-13 21:50:21 -06002699 if (arg->has_iops_wr_max_length) {
2700 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
Alberto Garciadce13202016-02-18 12:27:03 +02002701 }
2702
Eric Blake4dc93972016-07-13 21:50:21 -06002703 if (arg->has_iops_size) {
2704 cfg.op_size = arg->iops_size;
Benoît Canet2024c1d2013-09-02 14:14:41 +02002705 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02002706
Alberto Garciad5851082016-02-18 12:26:59 +02002707 if (!throttle_is_valid(&cfg, errp)) {
Max Reitz5433c242015-10-19 17:53:29 +02002708 goto out;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002709 }
2710
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002711 if (throttle_enabled(&cfg)) {
2712 /* Enable I/O limits if they're not enabled yet, otherwise
2713 * just update the throttling group. */
Kevin Wolf97148072016-03-21 13:53:52 +01002714 if (!blk_get_public(blk)->throttle_state) {
Eric Blake4dc93972016-07-13 21:50:21 -06002715 blk_io_limits_enable(blk,
Kevin Wolf7a9877a2016-09-20 13:38:48 +02002716 arg->has_group ? arg->group :
2717 arg->has_device ? arg->device :
2718 arg->id);
Eric Blake4dc93972016-07-13 21:50:21 -06002719 } else if (arg->has_group) {
2720 blk_io_limits_update_group(blk, arg->group);
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002721 }
2722 /* Set the new throttling configuration */
Kevin Wolf97148072016-03-21 13:53:52 +01002723 blk_set_io_limits(blk, &cfg);
2724 } else if (blk_get_public(blk)->throttle_state) {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02002725 /* If all throttling settings are set to 0, disable I/O limits */
Kevin Wolf97148072016-03-21 13:53:52 +01002726 blk_io_limits_disable(blk);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002727 }
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002728
Max Reitz5433c242015-10-19 17:53:29 +02002729out:
Stefan Hajnoczib15446f2014-05-14 16:22:47 +02002730 aio_context_release(aio_context);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08002731}
2732
John Snow341ebc22015-04-17 19:49:52 -04002733void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2734 bool has_granularity, uint32_t granularity,
2735 Error **errp)
2736{
2737 AioContext *aio_context;
2738 BlockDriverState *bs;
2739
2740 if (!name || name[0] == '\0') {
2741 error_setg(errp, "Bitmap name cannot be empty");
2742 return;
2743 }
2744
2745 bs = bdrv_lookup_bs(node, node, errp);
2746 if (!bs) {
2747 return;
2748 }
2749
2750 aio_context = bdrv_get_aio_context(bs);
2751 aio_context_acquire(aio_context);
2752
2753 if (has_granularity) {
2754 if (granularity < 512 || !is_power_of_2(granularity)) {
2755 error_setg(errp, "Granularity must be power of 2 "
2756 "and at least 512");
2757 goto out;
2758 }
2759 } else {
2760 /* Default to cluster size, if available: */
2761 granularity = bdrv_get_default_bitmap_granularity(bs);
2762 }
2763
2764 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2765
2766 out:
2767 aio_context_release(aio_context);
2768}
2769
2770void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2771 Error **errp)
2772{
2773 AioContext *aio_context;
2774 BlockDriverState *bs;
2775 BdrvDirtyBitmap *bitmap;
2776
2777 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2778 if (!bitmap || !bs) {
2779 return;
2780 }
2781
John Snow9bd2b082015-04-17 19:49:57 -04002782 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2783 error_setg(errp,
2784 "Bitmap '%s' is currently frozen and cannot be removed",
2785 name);
2786 goto out;
2787 }
John Snow20dca812015-04-17 19:50:02 -04002788 bdrv_dirty_bitmap_make_anon(bitmap);
John Snow341ebc22015-04-17 19:49:52 -04002789 bdrv_release_dirty_bitmap(bs, bitmap);
2790
John Snow9bd2b082015-04-17 19:49:57 -04002791 out:
John Snow341ebc22015-04-17 19:49:52 -04002792 aio_context_release(aio_context);
2793}
2794
John Snowe74e6b72015-04-17 19:49:59 -04002795/**
2796 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2797 * immediately after a full backup operation.
2798 */
2799void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2800 Error **errp)
2801{
2802 AioContext *aio_context;
2803 BdrvDirtyBitmap *bitmap;
2804 BlockDriverState *bs;
2805
2806 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2807 if (!bitmap || !bs) {
2808 return;
2809 }
2810
2811 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2812 error_setg(errp,
2813 "Bitmap '%s' is currently frozen and cannot be modified",
2814 name);
2815 goto out;
2816 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2817 error_setg(errp,
2818 "Bitmap '%s' is currently disabled and cannot be cleared",
2819 name);
2820 goto out;
2821 }
2822
Fam Zhengdf9a6812015-11-09 18:16:54 +08002823 bdrv_clear_dirty_bitmap(bitmap, NULL);
John Snowe74e6b72015-04-17 19:49:59 -04002824
2825 out:
2826 aio_context_release(aio_context);
2827}
2828
Markus Armbruster072ebe62015-03-05 17:00:56 +01002829void hmp_drive_del(Monitor *mon, const QDict *qdict)
Ryan Harper9063f812010-11-12 11:07:13 -06002830{
2831 const char *id = qdict_get_str(qdict, "id");
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002832 BlockBackend *blk;
Ryan Harper9063f812010-11-12 11:07:13 -06002833 BlockDriverState *bs;
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002834 AioContext *aio_context;
Fam Zheng3718d8a2014-05-23 21:29:43 +08002835 Error *local_err = NULL;
Ryan Harper9063f812010-11-12 11:07:13 -06002836
Kevin Wolf2073d412016-02-23 17:50:37 +01002837 bs = bdrv_find_node(id);
2838 if (bs) {
Markus Armbruster79b7a772017-03-21 17:53:28 +01002839 qmp_blockdev_del(id, &local_err);
Kevin Wolf2073d412016-02-23 17:50:37 +01002840 if (local_err) {
2841 error_report_err(local_err);
2842 }
2843 return;
2844 }
2845
Markus Armbruster7e7d56d2014-10-07 13:59:05 +02002846 blk = blk_by_name(id);
2847 if (!blk) {
Markus Armbrusterb1422f22014-05-16 11:00:09 +02002848 error_report("Device '%s' not found", id);
Markus Armbruster072ebe62015-03-05 17:00:56 +01002849 return;
Ryan Harper9063f812010-11-12 11:07:13 -06002850 }
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002851
Markus Armbruster26f8b3a2014-10-07 13:59:22 +02002852 if (!blk_legacy_dinfo(blk)) {
Markus Armbruster48f364d2014-09-11 16:45:39 +02002853 error_report("Deleting device added with blockdev-add"
2854 " is not supported");
Markus Armbruster072ebe62015-03-05 17:00:56 +01002855 return;
Markus Armbruster48f364d2014-09-11 16:45:39 +02002856 }
2857
Max Reitz5433c242015-10-19 17:53:29 +02002858 aio_context = blk_get_aio_context(blk);
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002859 aio_context_acquire(aio_context);
2860
Max Reitz5433c242015-10-19 17:53:29 +02002861 bs = blk_bs(blk);
2862 if (bs) {
2863 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2864 error_report_err(local_err);
2865 aio_context_release(aio_context);
2866 return;
2867 }
Ryan Harper9063f812010-11-12 11:07:13 -06002868
Max Reitz938abd42016-01-29 16:36:09 +01002869 blk_remove_bs(blk);
Max Reitz5433c242015-10-19 17:53:29 +02002870 }
Ryan Harper9063f812010-11-12 11:07:13 -06002871
Max Reitz7c735872016-03-16 19:54:39 +01002872 /* Make the BlockBackend and the attached BlockDriverState anonymous */
Max Reitzefaa7c42016-03-16 19:54:38 +01002873 monitor_remove_blk(blk);
2874
Max Reitz7c735872016-03-16 19:54:39 +01002875 /* If this BlockBackend has a device attached to it, its refcount will be
2876 * decremented when the device is removed; otherwise we have to do so here.
Ryan Harperd22b2f42011-03-29 20:51:47 -05002877 */
Markus Armbrustera7f53e22014-10-07 13:59:25 +02002878 if (blk_get_attached_dev(blk)) {
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02002879 /* Further I/O must not pause the guest */
Max Reitz373340b2015-10-19 17:53:22 +02002880 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2881 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05002882 } else {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +02002883 blk_unref(blk);
Ryan Harper9063f812010-11-12 11:07:13 -06002884 }
2885
Stefan Hajnoczi8ad42022014-08-18 16:07:12 +01002886 aio_context_release(aio_context);
Ryan Harper9063f812010-11-12 11:07:13 -06002887}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002888
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002889void qmp_block_resize(bool has_device, const char *device,
2890 bool has_node_name, const char *node_name,
2891 int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002892{
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002893 Error *local_err = NULL;
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002894 BlockBackend *blk = NULL;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002895 BlockDriverState *bs;
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002896 AioContext *aio_context;
Kevin Wolf87329012013-05-02 15:32:55 +02002897 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002898
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002899 bs = bdrv_lookup_bs(has_device ? device : NULL,
2900 has_node_name ? node_name : NULL,
2901 &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002902 if (local_err) {
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002903 error_propagate(errp, local_err);
2904 return;
2905 }
2906
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002907 aio_context = bdrv_get_aio_context(bs);
2908 aio_context_acquire(aio_context);
2909
Benoît Canet3b1dbd12014-01-23 21:31:37 +01002910 if (!bdrv_is_first_non_filter(bs)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002911 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002912 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002913 }
2914
2915 if (size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002916 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002917 goto out;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002918 }
2919
Jeff Cody9c75e162014-06-25 16:55:30 -04002920 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002921 error_setg(errp, QERR_DEVICE_IN_USE, device);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002922 goto out;
Jeff Cody9c75e162014-06-25 16:55:30 -04002923 }
2924
Kevin Wolf6d0eb642017-01-20 17:07:26 +01002925 blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
Kevin Wolfd7086422017-01-13 19:02:32 +01002926 ret = blk_insert_bs(blk, bs, errp);
2927 if (ret < 0) {
2928 goto out;
2929 }
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002930
John Snow698bdfa2017-05-10 13:39:45 -04002931 bdrv_drained_begin(bs);
Max Reitzed3d2ec2017-03-28 22:51:27 +02002932 ret = blk_truncate(blk, size, errp);
John Snow698bdfa2017-05-10 13:39:45 -04002933 bdrv_drained_end(bs);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002934
2935out:
Kevin Wolf7dad9ee2017-02-17 11:23:33 +01002936 blk_unref(blk);
Stefan Hajnoczi927e0e72014-08-18 14:52:28 +01002937 aio_context_release(aio_context);
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01002938}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002939
Alberto Garcia23233222016-07-05 17:28:59 +03002940void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002941 bool has_base, const char *base,
Alberto Garcia312fe092016-10-28 10:08:19 +03002942 bool has_base_node, const char *base_node,
Jeff Cody13d8cc52014-06-25 15:40:11 -04002943 bool has_backing_file, const char *backing_file,
2944 bool has_speed, int64_t speed,
Paolo Bonzini1d809092012-09-28 17:22:59 +02002945 bool has_on_error, BlockdevOnError on_error,
2946 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002947{
Alberto Garcia554b6142016-10-28 10:08:11 +03002948 BlockDriverState *bs, *iter;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002949 BlockDriverState *base_bs = NULL;
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002950 AioContext *aio_context;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01002951 Error *local_err = NULL;
Jeff Cody13d8cc52014-06-25 15:40:11 -04002952 const char *base_name = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002953
Paolo Bonzini1d809092012-09-28 17:22:59 +02002954 if (!has_on_error) {
2955 on_error = BLOCKDEV_ON_ERROR_REPORT;
2956 }
2957
Alberto Garcia554b6142016-10-28 10:08:11 +03002958 bs = bdrv_lookup_bs(device, device, errp);
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002959 if (!bs) {
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002960 return;
2961 }
2962
Kevin Wolfb6c1bae2016-06-23 14:20:24 +02002963 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002964 aio_context_acquire(aio_context);
2965
Alberto Garcia312fe092016-10-28 10:08:19 +03002966 if (has_base && has_base_node) {
2967 error_setg(errp, "'base' and 'base-node' cannot be specified "
2968 "at the same time");
2969 goto out;
2970 }
2971
Jeff Cody13d8cc52014-06-25 15:40:11 -04002972 if (has_base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002973 base_bs = bdrv_find_backing_image(bs, base);
2974 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002975 error_setg(errp, QERR_BASE_NOT_FOUND, base);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002976 goto out;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00002977 }
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01002978 assert(bdrv_get_aio_context(base_bs) == aio_context);
Jeff Cody13d8cc52014-06-25 15:40:11 -04002979 base_name = base;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00002980 }
2981
Alberto Garcia312fe092016-10-28 10:08:19 +03002982 if (has_base_node) {
2983 base_bs = bdrv_lookup_bs(NULL, base_node, errp);
2984 if (!base_bs) {
2985 goto out;
2986 }
2987 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
2988 error_setg(errp, "Node '%s' is not a backing image of '%s'",
2989 base_node, device);
2990 goto out;
2991 }
2992 assert(bdrv_get_aio_context(base_bs) == aio_context);
2993 base_name = base_bs->filename;
2994 }
2995
Alberto Garcia554b6142016-10-28 10:08:11 +03002996 /* Check for op blockers in the whole chain between bs and base */
2997 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
2998 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
2999 goto out;
3000 }
3001 }
3002
Jeff Cody13d8cc52014-06-25 15:40:11 -04003003 /* if we are streaming the entire chain, the result will have no backing
3004 * file, and specifying one is therefore an error */
3005 if (base_bs == NULL && has_backing_file) {
3006 error_setg(errp, "backing file specified, but streaming the "
3007 "entire chain");
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003008 goto out;
Jeff Cody13d8cc52014-06-25 15:40:11 -04003009 }
3010
3011 /* backing_file string overrides base bs filename */
3012 base_name = has_backing_file ? backing_file : base_name;
3013
Alberto Garcia23233222016-07-05 17:28:59 +03003014 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
John Snow8254b6d2016-10-27 12:06:58 -04003015 has_speed ? speed : 0, on_error, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003016 if (local_err) {
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01003017 error_propagate(errp, local_err);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003018 goto out;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003019 }
3020
3021 trace_qmp_block_stream(bs, bs->job);
Stefan Hajnoczif3e69be2014-10-21 12:03:57 +01003022
3023out:
3024 aio_context_release(aio_context);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00003025}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003026
Alberto Garciafd62c602016-07-05 17:29:00 +03003027void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
Jeff Cody7676e2c2014-06-30 15:14:15 +02003028 bool has_base, const char *base,
3029 bool has_top, const char *top,
Jeff Cody54e26902014-06-25 15:40:10 -04003030 bool has_backing_file, const char *backing_file,
Jeff Codyed61fc12012-09-27 13:29:16 -04003031 bool has_speed, int64_t speed,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003032 bool has_filter_node_name, const char *filter_node_name,
Jeff Codyed61fc12012-09-27 13:29:16 -04003033 Error **errp)
3034{
3035 BlockDriverState *bs;
Alberto Garcia058223a2016-10-28 10:08:07 +03003036 BlockDriverState *iter;
Jeff Codyed61fc12012-09-27 13:29:16 -04003037 BlockDriverState *base_bs, *top_bs;
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003038 AioContext *aio_context;
Jeff Codyed61fc12012-09-27 13:29:16 -04003039 Error *local_err = NULL;
3040 /* This will be part of the QMP command, if/when the
3041 * BlockdevOnError change for blkmirror makes it in
3042 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02003043 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04003044
Max Reitz54504662014-04-10 19:36:25 +02003045 if (!has_speed) {
3046 speed = 0;
3047 }
Kevin Wolf0db832f2017-02-20 18:10:05 +01003048 if (!has_filter_node_name) {
3049 filter_node_name = NULL;
3050 }
Max Reitz54504662014-04-10 19:36:25 +02003051
Jeff Cody7676e2c2014-06-30 15:14:15 +02003052 /* Important Note:
3053 * libvirt relies on the DeviceNotFound error class in order to probe for
3054 * live commit feature versions; for this to work, we must make sure to
3055 * perform the device lookup before any generic errors that may occur in a
3056 * scenario in which all optional arguments are omitted. */
Kevin Wolf1d13b162016-06-23 14:20:24 +02003057 bs = qmp_get_root_bs(device, &local_err);
3058 if (!bs) {
3059 bs = bdrv_lookup_bs(device, device, NULL);
3060 if (!bs) {
3061 error_free(local_err);
3062 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3063 "Device '%s' not found", device);
3064 } else {
3065 error_propagate(errp, local_err);
3066 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003067 return;
3068 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003069
Kevin Wolf1d13b162016-06-23 14:20:24 +02003070 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003071 aio_context_acquire(aio_context);
3072
Fam Zhengbb000212014-09-11 13:14:00 +08003073 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003074 goto out;
Fam Zheng628ff682014-05-23 21:29:44 +08003075 }
3076
Jeff Codyed61fc12012-09-27 13:29:16 -04003077 /* default top_bs is the active layer */
3078 top_bs = bs;
3079
Jeff Cody7676e2c2014-06-30 15:14:15 +02003080 if (has_top && top) {
Jeff Codyed61fc12012-09-27 13:29:16 -04003081 if (strcmp(bs->filename, top) != 0) {
3082 top_bs = bdrv_find_backing_image(bs, top);
3083 }
3084 }
3085
3086 if (top_bs == NULL) {
3087 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003088 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003089 }
3090
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003091 assert(bdrv_get_aio_context(top_bs) == aio_context);
3092
Jeff Codyd5208c42012-10-16 15:49:10 -04003093 if (has_base && base) {
3094 base_bs = bdrv_find_backing_image(top_bs, base);
3095 } else {
3096 base_bs = bdrv_find_base(top_bs);
3097 }
3098
3099 if (base_bs == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003100 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003101 goto out;
Jeff Codyd5208c42012-10-16 15:49:10 -04003102 }
3103
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003104 assert(bdrv_get_aio_context(base_bs) == aio_context);
3105
Alberto Garcia058223a2016-10-28 10:08:07 +03003106 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
3107 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3108 goto out;
3109 }
Fam Zhengbb000212014-09-11 13:14:00 +08003110 }
3111
Jeff Cody7676e2c2014-06-30 15:14:15 +02003112 /* Do not allow attempts to commit an image into itself */
3113 if (top_bs == base_bs) {
3114 error_setg(errp, "cannot commit an image into itself");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003115 goto out;
Jeff Cody7676e2c2014-06-30 15:14:15 +02003116 }
3117
Fam Zheng20a63d22013-12-16 14:45:31 +08003118 if (top_bs == bs) {
Jeff Cody54e26902014-06-25 15:40:10 -04003119 if (has_backing_file) {
3120 error_setg(errp, "'backing-file' specified,"
3121 " but 'top' is the active layer");
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003122 goto out;
Jeff Cody54e26902014-06-25 15:40:10 -04003123 }
John Snow47970df2016-10-27 12:06:57 -04003124 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003125 BLOCK_JOB_DEFAULT, speed, on_error,
Fam Zheng78bbd912017-04-21 20:27:04 +08003126 filter_node_name, NULL, NULL, false, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003127 } else {
Alberto Garcia058223a2016-10-28 10:08:07 +03003128 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
3129 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3130 goto out;
3131 }
Alberto Garciafd62c602016-07-05 17:29:00 +03003132 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed,
John Snow8254b6d2016-10-27 12:06:58 -04003133 on_error, has_backing_file ? backing_file : NULL,
Kevin Wolf0db832f2017-02-20 18:10:05 +01003134 filter_node_name, &local_err);
Fam Zheng20a63d22013-12-16 14:45:31 +08003135 }
Jeff Codyed61fc12012-09-27 13:29:16 -04003136 if (local_err != NULL) {
3137 error_propagate(errp, local_err);
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003138 goto out;
Jeff Codyed61fc12012-09-27 13:29:16 -04003139 }
Stefan Hajnoczi9e85cd52014-10-21 12:03:59 +01003140
3141out:
3142 aio_context_release(aio_context);
Jeff Codyed61fc12012-09-27 13:29:16 -04003143}
3144
John Snow111049a2016-11-08 01:50:38 -05003145static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn,
3146 Error **errp)
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003147{
3148 BlockDriverState *bs;
3149 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003150 BlockDriverState *source = NULL;
John Snow111049a2016-11-08 01:50:38 -05003151 BlockJob *job = NULL;
John Snowd58d8452015-04-17 19:49:58 -04003152 BdrvDirtyBitmap *bmap = NULL;
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003153 AioContext *aio_context;
Max Reitze6641712015-08-26 19:47:48 +02003154 QDict *options = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003155 Error *local_err = NULL;
3156 int flags;
3157 int64_t size;
Fam Zhengfc0932f2017-05-03 00:35:53 +08003158 bool set_backing_hd = false;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003159
Pavel Butsykin81206a82016-07-22 11:17:50 +03003160 if (!backup->has_speed) {
3161 backup->speed = 0;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003162 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003163 if (!backup->has_on_source_error) {
3164 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003165 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003166 if (!backup->has_on_target_error) {
3167 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003168 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003169 if (!backup->has_mode) {
3170 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3171 }
3172 if (!backup->has_job_id) {
3173 backup->job_id = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003174 }
Pavel Butsykin13b94142016-07-22 11:17:52 +03003175 if (!backup->has_compress) {
3176 backup->compress = false;
3177 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003178
Pavel Butsykin81206a82016-07-22 11:17:50 +03003179 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003180 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003181 return NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003182 }
3183
Kevin Wolfb7e4fa22016-06-23 14:20:24 +02003184 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003185 aio_context_acquire(aio_context);
3186
Pavel Butsykin81206a82016-07-22 11:17:50 +03003187 if (!backup->has_format) {
3188 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
3189 NULL : (char*) bs->drv->format_name;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003190 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003191
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003192 /* Early check to avoid creating target */
Fam Zheng3718d8a2014-05-23 21:29:43 +08003193 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003194 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003195 }
3196
Kevin Wolf61de4c62016-03-18 17:46:45 +01003197 flags = bs->open_flags | BDRV_O_RDWR;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003198
Ian Mainfc5d3f82013-07-26 11:39:04 -07003199 /* See if we have a backing HD we can use to create our new image
3200 * on top of. */
Pavel Butsykin81206a82016-07-22 11:17:50 +03003201 if (backup->sync == MIRROR_SYNC_MODE_TOP) {
Kevin Wolf760e0062015-06-17 14:55:21 +02003202 source = backing_bs(bs);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003203 if (!source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003204 backup->sync = MIRROR_SYNC_MODE_FULL;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003205 }
3206 }
Pavel Butsykin81206a82016-07-22 11:17:50 +03003207 if (backup->sync == MIRROR_SYNC_MODE_NONE) {
Ian Mainfc5d3f82013-07-26 11:39:04 -07003208 source = bs;
Fam Zhengfc0932f2017-05-03 00:35:53 +08003209 flags |= BDRV_O_NO_BACKING;
3210 set_backing_hd = true;
Ian Mainfc5d3f82013-07-26 11:39:04 -07003211 }
3212
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003213 size = bdrv_getlength(bs);
3214 if (size < 0) {
3215 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003216 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003217 }
3218
Pavel Butsykin81206a82016-07-22 11:17:50 +03003219 if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
3220 assert(backup->format);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003221 if (source) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003222 bdrv_img_create(backup->target, backup->format, source->filename,
Ian Mainfc5d3f82013-07-26 11:39:04 -07003223 source->drv->format_name, NULL,
Fam Zheng92172832017-04-21 20:27:01 +08003224 size, flags, false, &local_err);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003225 } else {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003226 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
Fam Zheng92172832017-04-21 20:27:01 +08003227 size, flags, false, &local_err);
Ian Mainfc5d3f82013-07-26 11:39:04 -07003228 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003229 }
3230
Markus Armbruster84d18f02014-01-30 15:07:28 +01003231 if (local_err) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003232 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003233 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003234 }
3235
Pavel Butsykin81206a82016-07-22 11:17:50 +03003236 if (backup->format) {
Fam Zhengfc0932f2017-05-03 00:35:53 +08003237 if (!options) {
3238 options = qdict_new();
3239 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003240 qdict_put_str(options, "driver", backup->format);
Max Reitze6641712015-08-26 19:47:48 +02003241 }
3242
Pavel Butsykin81206a82016-07-22 11:17:50 +03003243 target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003244 if (!target_bs) {
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003245 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003246 }
3247
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003248 bdrv_set_aio_context(target_bs, aio_context);
3249
Fam Zhengfc0932f2017-05-03 00:35:53 +08003250 if (set_backing_hd) {
3251 bdrv_set_backing_hd(target_bs, source, &local_err);
3252 if (local_err) {
3253 bdrv_unref(target_bs);
3254 goto out;
3255 }
3256 }
3257
Pavel Butsykin81206a82016-07-22 11:17:50 +03003258 if (backup->has_bitmap) {
3259 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
John Snowd58d8452015-04-17 19:49:58 -04003260 if (!bmap) {
Pavel Butsykin81206a82016-07-22 11:17:50 +03003261 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
Max Reitz0702d3d2015-11-09 23:39:10 +01003262 bdrv_unref(target_bs);
John Snowd58d8452015-04-17 19:49:58 -04003263 goto out;
3264 }
3265 }
3266
John Snow111049a2016-11-08 01:50:38 -05003267 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3268 backup->sync, bmap, backup->compress,
3269 backup->on_source_error, backup->on_target_error,
3270 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
Kevin Wolf5c438bc2016-04-14 13:09:53 +02003271 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003272 if (local_err != NULL) {
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003273 error_propagate(errp, local_err);
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003274 goto out;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003275 }
Stefan Hajnoczi761731b2014-10-21 12:03:56 +01003276
3277out:
3278 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003279 return job;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02003280}
3281
Pavel Butsykin81206a82016-07-22 11:17:50 +03003282void qmp_drive_backup(DriveBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003283{
John Snow111049a2016-11-08 01:50:38 -05003284
3285 BlockJob *job;
3286 job = do_drive_backup(arg, NULL, errp);
3287 if (job) {
3288 block_job_start(job);
3289 }
John Snow78f51fd2015-11-05 18:13:17 -05003290}
3291
Benoît Canetc13163f2014-01-23 21:31:34 +01003292BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3293{
Alberto Garciad5a8ee62015-04-17 14:52:43 +03003294 return bdrv_named_nodes_list(errp);
Benoît Canetc13163f2014-01-23 21:31:34 +01003295}
3296
John Snow111049a2016-11-08 01:50:38 -05003297BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn,
3298 Error **errp)
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003299{
3300 BlockDriverState *bs;
3301 BlockDriverState *target_bs;
3302 Error *local_err = NULL;
3303 AioContext *aio_context;
John Snow111049a2016-11-08 01:50:38 -05003304 BlockJob *job = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003305
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003306 if (!backup->has_speed) {
3307 backup->speed = 0;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003308 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003309 if (!backup->has_on_source_error) {
3310 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003311 }
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003312 if (!backup->has_on_target_error) {
3313 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3314 }
3315 if (!backup->has_job_id) {
3316 backup->job_id = NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003317 }
Pavel Butsykin3b7b1232016-07-22 11:17:53 +03003318 if (!backup->has_compress) {
3319 backup->compress = false;
3320 }
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003321
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003322 bs = qmp_get_root_bs(backup->device, errp);
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003323 if (!bs) {
John Snow111049a2016-11-08 01:50:38 -05003324 return NULL;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003325 }
3326
Kevin Wolfcef34ee2016-06-23 14:20:24 +02003327 aio_context = bdrv_get_aio_context(bs);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003328 aio_context_acquire(aio_context);
3329
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003330 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
Fam Zheng0d978912016-05-23 10:19:35 +08003331 if (!target_bs) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003332 goto out;
3333 }
Max Reitz5433c242015-10-19 17:53:29 +02003334
Fam Zhengefd75562016-05-23 10:19:36 +08003335 if (bdrv_get_aio_context(target_bs) != aio_context) {
3336 if (!bdrv_has_blk(target_bs)) {
3337 /* The target BDS is not attached, we can safely move it to another
3338 * AioContext. */
3339 bdrv_set_aio_context(target_bs, aio_context);
3340 } else {
3341 error_setg(errp, "Target is attached to a different thread from "
3342 "source.");
3343 goto out;
3344 }
3345 }
John Snow111049a2016-11-08 01:50:38 -05003346 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
3347 backup->sync, NULL, backup->compress,
3348 backup->on_source_error, backup->on_target_error,
3349 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err);
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003350 if (local_err != NULL) {
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003351 error_propagate(errp, local_err);
3352 }
3353out:
3354 aio_context_release(aio_context);
John Snow111049a2016-11-08 01:50:38 -05003355 return job;
Fam Zhengc29c1dd2014-12-18 18:37:05 +08003356}
3357
Pavel Butsykindc7a4a92016-07-22 11:17:51 +03003358void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp)
John Snow78f51fd2015-11-05 18:13:17 -05003359{
John Snow111049a2016-11-08 01:50:38 -05003360 BlockJob *job;
3361 job = do_blockdev_backup(arg, NULL, errp);
3362 if (job) {
3363 block_job_start(job);
3364 }
John Snow78f51fd2015-11-05 18:13:17 -05003365}
3366
Fam Zheng4193cdd2015-12-24 12:45:03 +08003367/* Parameter check and block job starting for drive mirroring.
3368 * Caller should hold @device and @target's aio context (must be the same).
3369 **/
Alberto Garcia71aa9862016-07-05 17:28:57 +03003370static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003371 BlockDriverState *target,
3372 bool has_replaces, const char *replaces,
3373 enum MirrorSyncMode sync,
Max Reitz274fcce2016-06-10 20:57:47 +02003374 BlockMirrorBackingMode backing_mode,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003375 bool has_speed, int64_t speed,
3376 bool has_granularity, uint32_t granularity,
3377 bool has_buf_size, int64_t buf_size,
3378 bool has_on_source_error,
3379 BlockdevOnError on_source_error,
3380 bool has_on_target_error,
3381 BlockdevOnError on_target_error,
3382 bool has_unmap, bool unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003383 bool has_filter_node_name,
3384 const char *filter_node_name,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003385 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003386{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003387
3388 if (!has_speed) {
3389 speed = 0;
3390 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02003391 if (!has_on_source_error) {
3392 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3393 }
3394 if (!has_on_target_error) {
3395 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3396 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003397 if (!has_granularity) {
3398 granularity = 0;
3399 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003400 if (!has_buf_size) {
Wen Congyang48ac0a42015-05-15 15:51:36 +08003401 buf_size = 0;
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003402 }
Fam Zheng0fc9f8e2015-06-08 13:56:08 +08003403 if (!has_unmap) {
3404 unmap = true;
3405 }
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003406 if (!has_filter_node_name) {
3407 filter_node_name = NULL;
3408 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01003409
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003410 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003411 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3412 "a value in range [512B, 64MB]");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003413 return;
3414 }
3415 if (granularity & (granularity - 1)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003416 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3417 "power of 2");
Paolo Bonzinieee13df2013-01-21 17:09:46 +01003418 return;
3419 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003420
Fam Zheng4193cdd2015-12-24 12:45:03 +08003421 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3422 return;
3423 }
Fam Zhenge40e5022015-12-24 12:45:04 +08003424 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3425 return;
3426 }
Fam Zheng4193cdd2015-12-24 12:45:03 +08003427
3428 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3429 sync = MIRROR_SYNC_MODE_FULL;
3430 }
3431
3432 /* pass the node name to replace to mirror start since it's loose coupling
3433 * and will allow to check whether the node still exist at mirror completion
3434 */
Alberto Garcia71aa9862016-07-05 17:28:57 +03003435 mirror_start(job_id, bs, target,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003436 has_replaces ? replaces : NULL,
Max Reitz274fcce2016-06-10 20:57:47 +02003437 speed, granularity, buf_size, sync, backing_mode,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003438 on_source_error, on_target_error, unmap, filter_node_name,
3439 errp);
Fam Zheng4193cdd2015-12-24 12:45:03 +08003440}
3441
Eric Blakefaecd402016-07-14 16:37:58 -06003442void qmp_drive_mirror(DriveMirror *arg, Error **errp)
Fam Zheng4193cdd2015-12-24 12:45:03 +08003443{
3444 BlockDriverState *bs;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003445 BlockDriverState *source, *target_bs;
3446 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003447 BlockMirrorBackingMode backing_mode;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003448 Error *local_err = NULL;
3449 QDict *options = NULL;
3450 int flags;
3451 int64_t size;
Eric Blakefaecd402016-07-14 16:37:58 -06003452 const char *format = arg->format;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003453
Kevin Wolf0524e932016-06-23 14:20:24 +02003454 bs = qmp_get_root_bs(arg->device, errp);
3455 if (!bs) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003456 return;
3457 }
3458
Kevin Wolf0524e932016-06-23 14:20:24 +02003459 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003460 aio_context_acquire(aio_context);
3461
Eric Blakefaecd402016-07-14 16:37:58 -06003462 if (!arg->has_mode) {
3463 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Fam Zheng4193cdd2015-12-24 12:45:03 +08003464 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003465
Eric Blakefaecd402016-07-14 16:37:58 -06003466 if (!arg->has_format) {
3467 format = (arg->mode == NEW_IMAGE_MODE_EXISTING
3468 ? NULL : bs->drv->format_name);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003469 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003470
Kevin Wolf61de4c62016-03-18 17:46:45 +01003471 flags = bs->open_flags | BDRV_O_RDWR;
Kevin Wolf760e0062015-06-17 14:55:21 +02003472 source = backing_bs(bs);
Eric Blakefaecd402016-07-14 16:37:58 -06003473 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
3474 arg->sync = MIRROR_SYNC_MODE_FULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003475 }
Eric Blakefaecd402016-07-14 16:37:58 -06003476 if (arg->sync == MIRROR_SYNC_MODE_NONE) {
Max Reitz117e0c82013-11-25 20:28:55 +01003477 source = bs;
3478 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003479
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003480 size = bdrv_getlength(bs);
3481 if (size < 0) {
3482 error_setg_errno(errp, -size, "bdrv_getlength failed");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003483 goto out;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02003484 }
3485
Eric Blakefaecd402016-07-14 16:37:58 -06003486 if (arg->has_replaces) {
Benoît Canet09158f02014-06-27 18:25:25 +02003487 BlockDriverState *to_replace_bs;
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003488 AioContext *replace_aio_context;
3489 int64_t replace_size;
Benoît Canet09158f02014-06-27 18:25:25 +02003490
Eric Blakefaecd402016-07-14 16:37:58 -06003491 if (!arg->has_node_name) {
Benoît Canet09158f02014-06-27 18:25:25 +02003492 error_setg(errp, "a node-name must be provided when replacing a"
3493 " named node of the graph");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003494 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003495 }
3496
Eric Blakefaecd402016-07-14 16:37:58 -06003497 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err);
Benoît Canet09158f02014-06-27 18:25:25 +02003498
3499 if (!to_replace_bs) {
3500 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003501 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003502 }
3503
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003504 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3505 aio_context_acquire(replace_aio_context);
3506 replace_size = bdrv_getlength(to_replace_bs);
3507 aio_context_release(replace_aio_context);
3508
3509 if (size != replace_size) {
Benoît Canet09158f02014-06-27 18:25:25 +02003510 error_setg(errp, "cannot replace image with a mirror image of "
3511 "different size");
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003512 goto out;
Benoît Canet09158f02014-06-27 18:25:25 +02003513 }
3514 }
3515
Eric Blakefaecd402016-07-14 16:37:58 -06003516 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
Max Reitz274fcce2016-06-10 20:57:47 +02003517 backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
3518 } else {
3519 backing_mode = MIRROR_OPEN_BACKING_CHAIN;
3520 }
3521
Eric Blakefaecd402016-07-14 16:37:58 -06003522 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
3523 && arg->mode != NEW_IMAGE_MODE_EXISTING)
Max Reitz14526862013-11-06 19:50:44 +01003524 {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003525 /* create new image w/o backing file */
Max Reitze6641712015-08-26 19:47:48 +02003526 assert(format);
Eric Blakefaecd402016-07-14 16:37:58 -06003527 bdrv_img_create(arg->target, format,
Fam Zheng92172832017-04-21 20:27:01 +08003528 NULL, NULL, NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003529 } else {
Eric Blakefaecd402016-07-14 16:37:58 -06003530 switch (arg->mode) {
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003531 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003532 break;
3533 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3534 /* create new image with backing file */
Eric Blakefaecd402016-07-14 16:37:58 -06003535 bdrv_img_create(arg->target, format,
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003536 source->filename,
3537 source->drv->format_name,
Fam Zheng92172832017-04-21 20:27:01 +08003538 NULL, size, flags, false, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003539 break;
3540 default:
3541 abort();
3542 }
3543 }
3544
Markus Armbruster84d18f02014-01-30 15:07:28 +01003545 if (local_err) {
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02003546 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003547 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003548 }
3549
Max Reitze6641712015-08-26 19:47:48 +02003550 options = qdict_new();
Eric Blakefaecd402016-07-14 16:37:58 -06003551 if (arg->has_node_name) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003552 qdict_put_str(options, "node-name", arg->node_name);
Benoît Canet4c828dc2014-06-16 12:00:55 +02003553 }
Max Reitze6641712015-08-26 19:47:48 +02003554 if (format) {
Eric Blake46f5ac22017-04-27 16:58:17 -05003555 qdict_put_str(options, "driver", format);
Max Reitze6641712015-08-26 19:47:48 +02003556 }
Benoît Canet4c828dc2014-06-16 12:00:55 +02003557
Paolo Bonzinib812f672013-01-21 17:09:43 +01003558 /* Mirroring takes care of copy-on-write using the source's backing
3559 * file.
3560 */
Eric Blakefaecd402016-07-14 16:37:58 -06003561 target_bs = bdrv_open(arg->target, NULL, options,
3562 flags | BDRV_O_NO_BACKING, errp);
Max Reitz5b363932016-05-17 16:41:31 +02003563 if (!target_bs) {
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003564 goto out;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003565 }
3566
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003567 bdrv_set_aio_context(target_bs, aio_context);
3568
Eric Blakefaecd402016-07-14 16:37:58 -06003569 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
3570 arg->has_replaces, arg->replaces, arg->sync,
3571 backing_mode, arg->has_speed, arg->speed,
3572 arg->has_granularity, arg->granularity,
3573 arg->has_buf_size, arg->buf_size,
3574 arg->has_on_source_error, arg->on_source_error,
3575 arg->has_on_target_error, arg->on_target_error,
3576 arg->has_unmap, arg->unmap,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003577 false, NULL,
Fam Zheng4193cdd2015-12-24 12:45:03 +08003578 &local_err);
Kevin Wolfe253f4b2016-04-12 16:17:41 +02003579 bdrv_unref(target_bs);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003580 error_propagate(errp, local_err);
Stefan Hajnoczi5a7e7a02014-10-21 12:03:58 +01003581out:
3582 aio_context_release(aio_context);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02003583}
3584
Alberto Garcia71aa9862016-07-05 17:28:57 +03003585void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
3586 const char *device, const char *target,
Fam Zhengdf925622015-12-24 12:45:05 +08003587 bool has_replaces, const char *replaces,
3588 MirrorSyncMode sync,
3589 bool has_speed, int64_t speed,
3590 bool has_granularity, uint32_t granularity,
3591 bool has_buf_size, int64_t buf_size,
3592 bool has_on_source_error,
3593 BlockdevOnError on_source_error,
3594 bool has_on_target_error,
3595 BlockdevOnError on_target_error,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003596 bool has_filter_node_name,
3597 const char *filter_node_name,
Fam Zhengdf925622015-12-24 12:45:05 +08003598 Error **errp)
3599{
3600 BlockDriverState *bs;
Fam Zhengdf925622015-12-24 12:45:05 +08003601 BlockDriverState *target_bs;
3602 AioContext *aio_context;
Max Reitz274fcce2016-06-10 20:57:47 +02003603 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
Fam Zhengdf925622015-12-24 12:45:05 +08003604 Error *local_err = NULL;
3605
Kevin Wolf07eec652016-06-23 14:20:24 +02003606 bs = qmp_get_root_bs(device, errp);
Fam Zhengdf925622015-12-24 12:45:05 +08003607 if (!bs) {
Fam Zhengdf925622015-12-24 12:45:05 +08003608 return;
3609 }
3610
3611 target_bs = bdrv_lookup_bs(target, target, errp);
3612 if (!target_bs) {
3613 return;
3614 }
3615
3616 aio_context = bdrv_get_aio_context(bs);
3617 aio_context_acquire(aio_context);
3618
Fam Zhengdf925622015-12-24 12:45:05 +08003619 bdrv_set_aio_context(target_bs, aio_context);
3620
Alberto Garcia71aa9862016-07-05 17:28:57 +03003621 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
Max Reitz274fcce2016-06-10 20:57:47 +02003622 has_replaces, replaces, sync, backing_mode,
Fam Zhengdf925622015-12-24 12:45:05 +08003623 has_speed, speed,
3624 has_granularity, granularity,
3625 has_buf_size, buf_size,
3626 has_on_source_error, on_source_error,
3627 has_on_target_error, on_target_error,
3628 true, true,
Kevin Wolf6cdbceb2017-02-20 18:10:05 +01003629 has_filter_node_name, filter_node_name,
Fam Zhengdf925622015-12-24 12:45:05 +08003630 &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003631 error_propagate(errp, local_err);
Fam Zhengdf925622015-12-24 12:45:05 +08003632
3633 aio_context_release(aio_context);
3634}
3635
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003636/* Get a block job using its ID and acquire its AioContext */
3637static BlockJob *find_block_job(const char *id, AioContext **aio_context,
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003638 Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003639{
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003640 BlockJob *job;
3641
3642 assert(id != NULL);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003643
Max Reitz5433c242015-10-19 17:53:29 +02003644 *aio_context = NULL;
3645
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003646 job = block_job_get(id);
3647
3648 if (!job) {
3649 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3650 "Block job '%s' not found", id);
3651 return NULL;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003652 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003653
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003654 *aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003655 aio_context_acquire(*aio_context);
3656
Alberto Garcia3ddf3ef2016-07-05 17:28:55 +03003657 return job;
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003658}
3659
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003660void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003661{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003662 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003663 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003664
3665 if (!job) {
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003666 return;
3667 }
3668
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01003669 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003670 aio_context_release(aio_context);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00003671}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003672
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003673void qmp_block_job_cancel(const char *device,
3674 bool has_force, bool force, Error **errp)
3675{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003676 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003677 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003678
3679 if (!job) {
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003680 return;
3681 }
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003682
3683 if (!has_force) {
3684 force = false;
3685 }
3686
John Snow0df4ba52016-10-27 12:06:59 -04003687 if (block_job_user_paused(job) && !force) {
Cole Robinsonf231b882014-03-21 19:42:26 -04003688 error_setg(errp, "The block job for device '%s' is currently paused",
3689 device);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003690 goto out;
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003691 }
3692
3693 trace_qmp_block_job_cancel(job);
3694 block_job_cancel(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003695out:
3696 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003697}
3698
3699void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003700{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003701 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003702 BlockJob *job = find_block_job(device, &aio_context, errp);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003703
John Snow0df4ba52016-10-27 12:06:59 -04003704 if (!job || block_job_user_paused(job)) {
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003705 return;
3706 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003707
3708 trace_qmp_block_job_pause(job);
John Snow0df4ba52016-10-27 12:06:59 -04003709 block_job_user_pause(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003710 aio_context_release(aio_context);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003711}
3712
3713void qmp_block_job_resume(const char *device, Error **errp)
3714{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003715 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003716 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003717
John Snow0df4ba52016-10-27 12:06:59 -04003718 if (!job || !block_job_user_paused(job)) {
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02003719 return;
3720 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003721
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02003722 trace_qmp_block_job_resume(job);
John Snow0df4ba52016-10-27 12:06:59 -04003723 block_job_user_resume(job);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003724 aio_context_release(aio_context);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00003725}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003726
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003727void qmp_block_job_complete(const char *device, Error **errp)
3728{
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003729 AioContext *aio_context;
Markus Armbruster24d6bff2015-01-29 10:36:58 +01003730 BlockJob *job = find_block_job(device, &aio_context, errp);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003731
3732 if (!job) {
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003733 return;
3734 }
3735
3736 trace_qmp_block_job_complete(job);
3737 block_job_complete(job, errp);
Stefan Hajnoczi3d948cd2014-10-21 12:03:50 +01003738 aio_context_release(aio_context);
Paolo Bonziniaeae8832012-10-18 16:49:21 +02003739}
3740
Jeff Codyfa40e652014-07-01 09:52:16 +02003741void qmp_change_backing_file(const char *device,
3742 const char *image_node_name,
3743 const char *backing_file,
3744 Error **errp)
3745{
3746 BlockDriverState *bs = NULL;
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003747 AioContext *aio_context;
Jeff Codyfa40e652014-07-01 09:52:16 +02003748 BlockDriverState *image_bs = NULL;
3749 Error *local_err = NULL;
3750 bool ro;
3751 int open_flags;
3752 int ret;
3753
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003754 bs = qmp_get_root_bs(device, errp);
3755 if (!bs) {
Jeff Codyfa40e652014-07-01 09:52:16 +02003756 return;
3757 }
3758
Kevin Wolf7b5dca32016-06-23 14:20:24 +02003759 aio_context = bdrv_get_aio_context(bs);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003760 aio_context_acquire(aio_context);
3761
Jeff Codyfa40e652014-07-01 09:52:16 +02003762 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3763 if (local_err) {
3764 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003765 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003766 }
3767
3768 if (!image_bs) {
3769 error_setg(errp, "image file not found");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003770 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003771 }
3772
3773 if (bdrv_find_base(image_bs) == image_bs) {
3774 error_setg(errp, "not allowing backing file change on an image "
3775 "without a backing file");
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003776 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003777 }
3778
3779 /* even though we are not necessarily operating on bs, we need it to
3780 * determine if block ops are currently prohibited on the chain */
3781 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003782 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003783 }
3784
3785 /* final sanity check */
3786 if (!bdrv_chain_contains(bs, image_bs)) {
3787 error_setg(errp, "'%s' and image file are not in the same chain",
3788 device);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003789 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003790 }
3791
3792 /* if not r/w, reopen to make r/w */
3793 open_flags = image_bs->open_flags;
3794 ro = bdrv_is_read_only(image_bs);
3795
3796 if (ro) {
3797 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3798 if (local_err) {
3799 error_propagate(errp, local_err);
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003800 goto out;
Jeff Codyfa40e652014-07-01 09:52:16 +02003801 }
3802 }
3803
3804 ret = bdrv_change_backing_file(image_bs, backing_file,
3805 image_bs->drv ? image_bs->drv->format_name : "");
3806
3807 if (ret < 0) {
3808 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3809 backing_file);
3810 /* don't exit here, so we can try to restore open flags if
3811 * appropriate */
3812 }
3813
3814 if (ro) {
3815 bdrv_reopen(image_bs, open_flags, &local_err);
Eduardo Habkost621ff942016-06-13 18:57:56 -03003816 error_propagate(errp, local_err);
Jeff Codyfa40e652014-07-01 09:52:16 +02003817 }
Stefan Hajnoczi729962f2014-11-19 14:19:45 +00003818
3819out:
3820 aio_context_release(aio_context);
Jeff Codyfa40e652014-07-01 09:52:16 +02003821}
3822
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003823void hmp_drive_add_node(Monitor *mon, const char *optstr)
3824{
3825 QemuOpts *opts;
3826 QDict *qdict;
3827 Error *local_err = NULL;
3828
3829 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
3830 if (!opts) {
3831 return;
3832 }
3833
3834 qdict = qemu_opts_to_qdict(opts, NULL);
3835
3836 if (!qdict_get_try_str(qdict, "node-name")) {
Kevin Wolff8746fb2016-03-16 11:14:31 +01003837 QDECREF(qdict);
Kevin Wolfabb21ac2016-02-23 17:33:24 +01003838 error_report("'node-name' needs to be specified");
3839 goto out;
3840 }
3841
3842 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
3843 if (!bs) {
3844 error_report_err(local_err);
3845 goto out;
3846 }
3847
3848 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3849
3850out:
3851 qemu_opts_del(opts);
3852}
3853
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003854void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3855{
Max Reitzbe4b67b2015-10-19 17:53:09 +02003856 BlockDriverState *bs;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003857 QObject *obj;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +01003858 Visitor *v = qobject_output_visitor_new(&obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003859 QDict *qdict;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003860 Error *local_err = NULL;
3861
Eric Blake3b098d52016-06-09 10:48:43 -06003862 visit_type_BlockdevOptions(v, NULL, &options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01003863 if (local_err) {
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003864 error_propagate(errp, local_err);
3865 goto fail;
3866 }
3867
Eric Blake3b098d52016-06-09 10:48:43 -06003868 visit_complete(v, &obj);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003869 qdict = qobject_to_qdict(obj);
3870
3871 qdict_flatten(qdict);
3872
Kevin Wolf9ec88732016-09-21 14:56:11 +02003873 if (!qdict_get_try_str(qdict, "node-name")) {
3874 error_setg(errp, "'node-name' must be specified for the root node");
3875 goto fail;
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003876 }
3877
Kevin Wolf9ec88732016-09-21 14:56:11 +02003878 bs = bds_tree_init(qdict, errp);
3879 if (!bs) {
3880 goto fail;
3881 }
3882
3883 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3884
Max Reitzbe4b67b2015-10-19 17:53:09 +02003885 if (bs && bdrv_key_required(bs)) {
Kevin Wolf9ec88732016-09-21 14:56:11 +02003886 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3887 bdrv_unref(bs);
Kevin Wolf8ae8e902014-03-06 15:43:42 +01003888 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3889 goto fail;
3890 }
3891
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003892fail:
Eric Blake3b098d52016-06-09 10:48:43 -06003893 visit_free(v);
Kevin Wolfd26c9a12013-09-23 15:26:03 +02003894}
3895
Markus Armbruster79b7a772017-03-21 17:53:28 +01003896void qmp_blockdev_del(const char *node_name, Error **errp)
Alberto Garcia81b936a2015-11-02 16:51:55 +02003897{
3898 AioContext *aio_context;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003899 BlockDriverState *bs;
3900
Kevin Wolf9ec88732016-09-21 14:56:11 +02003901 bs = bdrv_find_node(node_name);
3902 if (!bs) {
3903 error_setg(errp, "Cannot find node %s", node_name);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003904 return;
3905 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003906 if (bdrv_has_blk(bs)) {
3907 error_setg(errp, "Node %s is in use", node_name);
3908 return;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003909 }
Kevin Wolf9ec88732016-09-21 14:56:11 +02003910 aio_context = bdrv_get_aio_context(bs);
Alberto Garcia81b936a2015-11-02 16:51:55 +02003911 aio_context_acquire(aio_context);
3912
Kevin Wolf9ec88732016-09-21 14:56:11 +02003913 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3914 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003915 }
3916
Kevin Wolf9ec88732016-09-21 14:56:11 +02003917 if (!bs->monitor_list.tqe_prev) {
3918 error_setg(errp, "Node %s is not owned by the monitor",
3919 bs->node_name);
3920 goto out;
Alberto Garcia81b936a2015-11-02 16:51:55 +02003921 }
3922
Kevin Wolf9ec88732016-09-21 14:56:11 +02003923 if (bs->refcnt > 1) {
3924 error_setg(errp, "Block device %s is in use",
3925 bdrv_get_device_or_node_name(bs));
3926 goto out;
3927 }
3928
3929 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3930 bdrv_unref(bs);
3931
Alberto Garcia81b936a2015-11-02 16:51:55 +02003932out:
3933 aio_context_release(aio_context);
3934}
3935
Wen Congyang7f821592016-05-10 15:36:39 +08003936static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
3937 const char *child_name)
3938{
3939 BdrvChild *child;
3940
3941 QLIST_FOREACH(child, &parent_bs->children, next) {
3942 if (strcmp(child->name, child_name) == 0) {
3943 return child;
3944 }
3945 }
3946
3947 return NULL;
3948}
3949
3950void qmp_x_blockdev_change(const char *parent, bool has_child,
3951 const char *child, bool has_node,
3952 const char *node, Error **errp)
3953{
3954 BlockDriverState *parent_bs, *new_bs = NULL;
3955 BdrvChild *p_child;
3956
3957 parent_bs = bdrv_lookup_bs(parent, parent, errp);
3958 if (!parent_bs) {
3959 return;
3960 }
3961
3962 if (has_child == has_node) {
3963 if (has_child) {
3964 error_setg(errp, "The parameters child and node are in conflict");
3965 } else {
3966 error_setg(errp, "Either child or node must be specified");
3967 }
3968 return;
3969 }
3970
3971 if (has_child) {
3972 p_child = bdrv_find_child(parent_bs, child);
3973 if (!p_child) {
3974 error_setg(errp, "Node '%s' does not have child '%s'",
3975 parent, child);
3976 return;
3977 }
3978 bdrv_del_child(parent_bs, p_child, errp);
3979 }
3980
3981 if (has_node) {
3982 new_bs = bdrv_find_node(node);
3983 if (!new_bs) {
3984 error_setg(errp, "Node '%s' not found", node);
3985 return;
3986 }
3987 bdrv_add_child(parent_bs, new_bs, errp);
3988 }
3989}
3990
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00003991BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3992{
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003993 BlockJobInfoList *head = NULL, **p_next = &head;
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003994 BlockJob *job;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003995
Alberto Garciaf0f55de2016-05-27 12:53:37 +02003996 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
John Snow559b9352016-10-27 12:06:55 -04003997 BlockJobInfoList *elem;
3998 AioContext *aio_context;
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01003999
John Snow559b9352016-10-27 12:06:55 -04004000 if (block_job_is_internal(job)) {
4001 continue;
4002 }
4003 elem = g_new0(BlockJobInfoList, 1);
4004 aio_context = blk_get_aio_context(job->blk);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004005 aio_context_acquire(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004006 elem->value = block_job_query(job, errp);
Stefan Hajnoczi69691e72014-10-21 12:03:51 +01004007 aio_context_release(aio_context);
John Snow559b9352016-10-27 12:06:55 -04004008 if (!elem->value) {
4009 g_free(elem);
4010 qapi_free_BlockJobInfoList(head);
4011 return NULL;
4012 }
Alberto Garciaf0f55de2016-05-27 12:53:37 +02004013 *p_next = elem;
4014 p_next = &elem->next;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02004015 }
4016
4017 return head;
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00004018}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004019
Kevin Wolf00063832013-03-15 10:35:07 +01004020QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004021 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01004022 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01004023 .desc = {
4024 {
Paolo Bonzini4d454572012-11-26 16:03:42 +01004025 .name = "snapshot",
4026 .type = QEMU_OPT_BOOL,
4027 .help = "enable/disable snapshot mode",
4028 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004029 .name = "aio",
4030 .type = QEMU_OPT_STRING,
4031 .help = "host AIO implementation (threads, native)",
4032 },{
Kevin Wolfe4b24b42016-03-15 15:39:42 +01004033 .name = BDRV_OPT_CACHE_WB,
4034 .type = QEMU_OPT_BOOL,
4035 .help = "Enable writeback mode",
4036 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004037 .name = "format",
4038 .type = QEMU_OPT_STRING,
4039 .help = "disk format (raw, qcow2, ...)",
4040 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004041 .name = "rerror",
4042 .type = QEMU_OPT_STRING,
4043 .help = "read error action",
4044 },{
4045 .name = "werror",
4046 .type = QEMU_OPT_STRING,
4047 .help = "write error action",
4048 },{
Alberto Garcia4e200cf2016-09-15 17:53:05 +03004049 .name = BDRV_OPT_READ_ONLY,
Paolo Bonzini4d454572012-11-26 16:03:42 +01004050 .type = QEMU_OPT_BOOL,
4051 .help = "open drive file as read-only",
Pradeep Jagadeesha2a78622017-02-28 10:31:46 +01004052 },
4053
4054 THROTTLE_OPTS,
4055
4056 {
Alberto Garcia76f4afb2015-06-08 18:17:44 +02004057 .name = "throttling.group",
4058 .type = QEMU_OPT_STRING,
4059 .help = "name of the block throttling group",
4060 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01004061 .name = "copy-on-read",
4062 .type = QEMU_OPT_BOOL,
4063 .help = "copy read data from backing file into image file",
Peter Lieven465bee12014-05-18 00:58:19 +02004064 },{
4065 .name = "detect-zeroes",
4066 .type = QEMU_OPT_STRING,
4067 .help = "try to optimize zero writes (off, on, unmap)",
Alberto Garcia362e9292015-10-28 17:33:04 +02004068 },{
4069 .name = "stats-account-invalid",
4070 .type = QEMU_OPT_BOOL,
4071 .help = "whether to account for invalid I/O operations "
4072 "in the statistics",
4073 },{
4074 .name = "stats-account-failed",
4075 .type = QEMU_OPT_BOOL,
4076 .help = "whether to account for failed I/O operations "
4077 "in the statistics",
Paolo Bonzini4d454572012-11-26 16:03:42 +01004078 },
4079 { /* end of list */ }
4080 },
4081};
Kevin Wolf00063832013-03-15 10:35:07 +01004082
4083QemuOptsList qemu_drive_opts = {
4084 .name = "drive",
4085 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4086 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02004087 /*
4088 * no elements => accept any params
4089 * validation will happen later
4090 */
Kevin Wolf00063832013-03-15 10:35:07 +01004091 { /* end of list */ }
4092 },
4093};