blob: a91d5a87df029816282a277c08b7536ba802d1a0 [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
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/blockdev.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/block/block.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010035#include "block/blockjob.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010036#include "monitor/monitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010037#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/option.h"
39#include "qemu/config-file.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010040#include "qapi/qmp/types.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010042#include "block/block_int.h"
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -020043#include "qmp-commands.h"
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +000044#include "trace.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010045#include "sysemu/arch_init.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020046
Markus Armbrusterc9b62a72010-06-02 18:55:22 +020047static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
Kevin Wolf00063832013-03-15 10:35:07 +010048extern QemuOptsList qemu_common_drive_opts;
Markus Armbruster666daa62010-06-02 18:48:27 +020049
Markus Armbruster19609662011-01-28 11:21:39 +010050static const char *const if_name[IF_COUNT] = {
51 [IF_NONE] = "none",
52 [IF_IDE] = "ide",
53 [IF_SCSI] = "scsi",
54 [IF_FLOPPY] = "floppy",
55 [IF_PFLASH] = "pflash",
56 [IF_MTD] = "mtd",
57 [IF_SD] = "sd",
58 [IF_VIRTIO] = "virtio",
59 [IF_XEN] = "xen",
60};
61
62static const int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010063 /*
64 * Do not change these numbers! They govern how drive option
65 * index maps to unit and bus. That mapping is ABI.
66 *
67 * All controllers used to imlement if=T drives need to support
68 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
69 * Otherwise, some index values map to "impossible" bus, unit
70 * values.
71 *
72 * For instance, if you change [IF_SCSI] to 255, -drive
73 * if=scsi,index=12 no longer means bus=1,unit=5, but
74 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
75 * the drive can't be set up. Regression.
76 */
77 [IF_IDE] = 2,
78 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010079};
80
Markus Armbruster14bafc52010-06-25 08:09:10 +020081/*
82 * We automatically delete the drive when a device using it gets
83 * unplugged. Questionable feature, but we can't just drop it.
84 * Device models call blockdev_mark_auto_del() to schedule the
85 * automatic deletion, and generic qdev code calls blockdev_auto_del()
86 * when deletion is actually safe.
87 */
88void blockdev_mark_auto_del(BlockDriverState *bs)
89{
90 DriveInfo *dinfo = drive_get_by_blockdev(bs);
91
Paolo Bonzini12bde0e2012-03-30 13:17:10 +020092 if (bs->job) {
93 block_job_cancel(bs->job);
94 }
Ryan Harper0fc0f1f2010-12-08 10:05:00 -060095 if (dinfo) {
96 dinfo->auto_del = 1;
97 }
Markus Armbruster14bafc52010-06-25 08:09:10 +020098}
99
100void blockdev_auto_del(BlockDriverState *bs)
101{
102 DriveInfo *dinfo = drive_get_by_blockdev(bs);
103
Ryan Harper0fc0f1f2010-12-08 10:05:00 -0600104 if (dinfo && dinfo->auto_del) {
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200105 drive_put_ref(dinfo);
Markus Armbruster14bafc52010-06-25 08:09:10 +0200106 }
107}
108
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100109static int drive_index_to_bus_id(BlockInterfaceType type, int index)
110{
111 int max_devs = if_max_devs[type];
112 return max_devs ? index / max_devs : 0;
113}
114
115static int drive_index_to_unit_id(BlockInterfaceType type, int index)
116{
117 int max_devs = if_max_devs[type];
118 return max_devs ? index % max_devs : index;
119}
120
Markus Armbruster2292dda2011-01-28 11:21:41 +0100121QemuOpts *drive_def(const char *optstr)
122{
123 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
124}
125
126QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +0100127 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +0200128{
Markus Armbruster666daa62010-06-02 18:48:27 +0200129 QemuOpts *opts;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100130 char buf[32];
Markus Armbruster666daa62010-06-02 18:48:27 +0200131
Markus Armbruster2292dda2011-01-28 11:21:41 +0100132 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200133 if (!opts) {
134 return NULL;
135 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100136 if (type != IF_DEFAULT) {
137 qemu_opt_set(opts, "if", if_name[type]);
138 }
139 if (index >= 0) {
140 snprintf(buf, sizeof(buf), "%d", index);
141 qemu_opt_set(opts, "index", buf);
142 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200143 if (file)
144 qemu_opt_set(opts, "file", file);
145 return opts;
146}
147
148DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
149{
150 DriveInfo *dinfo;
151
152 /* seek interface, bus and unit */
153
154 QTAILQ_FOREACH(dinfo, &drives, next) {
155 if (dinfo->type == type &&
156 dinfo->bus == bus &&
157 dinfo->unit == unit)
158 return dinfo;
159 }
160
161 return NULL;
162}
163
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100164DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
165{
166 return drive_get(type,
167 drive_index_to_bus_id(type, index),
168 drive_index_to_unit_id(type, index));
169}
170
Markus Armbruster666daa62010-06-02 18:48:27 +0200171int drive_get_max_bus(BlockInterfaceType type)
172{
173 int max_bus;
174 DriveInfo *dinfo;
175
176 max_bus = -1;
177 QTAILQ_FOREACH(dinfo, &drives, next) {
178 if(dinfo->type == type &&
179 dinfo->bus > max_bus)
180 max_bus = dinfo->bus;
181 }
182 return max_bus;
183}
184
Markus Armbruster13839972011-01-28 11:21:37 +0100185/* Get a block device. This should only be used for single-drive devices
186 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
187 appropriate bus. */
188DriveInfo *drive_get_next(BlockInterfaceType type)
189{
190 static int next_block_unit[IF_COUNT];
191
192 return drive_get(type, 0, next_block_unit[type]++);
193}
194
Markus Armbrustere4700e52010-06-24 17:25:32 +0200195DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
Markus Armbruster666daa62010-06-02 18:48:27 +0200196{
197 DriveInfo *dinfo;
198
199 QTAILQ_FOREACH(dinfo, &drives, next) {
Markus Armbrustere4700e52010-06-24 17:25:32 +0200200 if (dinfo->bdrv == bs) {
201 return dinfo;
202 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200203 }
Markus Armbrustere4700e52010-06-24 17:25:32 +0200204 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200205}
206
Markus Armbruster666daa62010-06-02 18:48:27 +0200207static void bdrv_format_print(void *opaque, const char *name)
208{
Markus Armbruster807105a2011-01-17 19:31:27 +0100209 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200210}
211
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200212static void drive_uninit(DriveInfo *dinfo)
Markus Armbruster666daa62010-06-02 18:48:27 +0200213{
214 qemu_opts_del(dinfo->opts);
Fam Zheng4f6fd342013-08-23 09:14:47 +0800215 bdrv_unref(dinfo->bdrv);
Anthony Liguori7267c092011-08-20 22:09:37 -0500216 g_free(dinfo->id);
Markus Armbruster666daa62010-06-02 18:48:27 +0200217 QTAILQ_REMOVE(&drives, dinfo, next);
Kevin Wolfbb446192013-03-15 10:35:06 +0100218 g_free(dinfo->serial);
Anthony Liguori7267c092011-08-20 22:09:37 -0500219 g_free(dinfo);
Markus Armbruster666daa62010-06-02 18:48:27 +0200220}
221
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200222void drive_put_ref(DriveInfo *dinfo)
223{
224 assert(dinfo->refcount);
225 if (--dinfo->refcount == 0) {
226 drive_uninit(dinfo);
227 }
228}
229
230void drive_get_ref(DriveInfo *dinfo)
231{
232 dinfo->refcount++;
233}
234
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000235typedef struct {
236 QEMUBH *bh;
Fam Zhengfa510eb2013-08-23 09:14:51 +0800237 BlockDriverState *bs;
238} BDRVPutRefBH;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000239
Fam Zhengfa510eb2013-08-23 09:14:51 +0800240static void bdrv_put_ref_bh(void *opaque)
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000241{
Fam Zhengfa510eb2013-08-23 09:14:51 +0800242 BDRVPutRefBH *s = opaque;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000243
Fam Zhengfa510eb2013-08-23 09:14:51 +0800244 bdrv_unref(s->bs);
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000245 qemu_bh_delete(s->bh);
246 g_free(s);
247}
248
249/*
Fam Zhengfa510eb2013-08-23 09:14:51 +0800250 * Release a BDS reference in a BH
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000251 *
Fam Zhengfa510eb2013-08-23 09:14:51 +0800252 * It is not safe to use bdrv_unref() from a callback function when the callers
253 * still need the BlockDriverState. In such cases we schedule a BH to release
254 * the reference.
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000255 */
Fam Zhengfa510eb2013-08-23 09:14:51 +0800256static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000257{
Fam Zhengfa510eb2013-08-23 09:14:51 +0800258 BDRVPutRefBH *s;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000259
Fam Zhengfa510eb2013-08-23 09:14:51 +0800260 s = g_new(BDRVPutRefBH, 1);
261 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
262 s->bs = bs;
Stefan Hajnocziaa398a52012-01-18 14:40:50 +0000263 qemu_bh_schedule(s->bh);
264}
265
Paolo Bonzini1ceee0d2012-09-28 17:22:56 +0200266static int parse_block_error_action(const char *buf, bool is_read)
Markus Armbruster666daa62010-06-02 18:48:27 +0200267{
268 if (!strcmp(buf, "ignore")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200269 return BLOCKDEV_ON_ERROR_IGNORE;
Markus Armbruster666daa62010-06-02 18:48:27 +0200270 } else if (!is_read && !strcmp(buf, "enospc")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200271 return BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200272 } else if (!strcmp(buf, "stop")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200273 return BLOCKDEV_ON_ERROR_STOP;
Markus Armbruster666daa62010-06-02 18:48:27 +0200274 } else if (!strcmp(buf, "report")) {
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200275 return BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200276 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100277 error_report("'%s' invalid %s error action",
278 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200279 return -1;
280 }
281}
282
Benoît Canetcc0681c2013-09-02 14:14:39 +0200283static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800284{
Benoît Canetcc0681c2013-09-02 14:14:39 +0200285 if (throttle_conflicting(cfg)) {
286 error_setg(errp, "bps/iops/max total values and read/write values"
287 " cannot be used at the same time");
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800288 return false;
289 }
290
Benoît Canetcc0681c2013-09-02 14:14:39 +0200291 if (!throttle_is_valid(cfg)) {
292 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
Stefan Hajnoczi7d81c142013-02-13 16:53:43 +0100293 return false;
294 }
295
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800296 return true;
297}
298
Kevin Wolf57975222013-07-17 14:41:54 +0200299static DriveInfo *blockdev_init(QemuOpts *all_opts,
300 BlockInterfaceType block_default_type)
Markus Armbruster666daa62010-06-02 18:48:27 +0200301{
302 const char *buf;
303 const char *file = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200304 const char *serial;
305 const char *mediastr = "";
306 BlockInterfaceType type;
307 enum { MEDIA_DISK, MEDIA_CDROM } media;
308 int bus_id, unit_id;
309 int cyls, heads, secs, translation;
Markus Armbruster666daa62010-06-02 18:48:27 +0200310 int max_devs;
311 int index;
312 int ro = 0;
313 int bdrv_flags = 0;
314 int on_read_error, on_write_error;
315 const char *devaddr;
316 DriveInfo *dinfo;
Benoît Canetcc0681c2013-09-02 14:14:39 +0200317 ThrottleConfig cfg;
Markus Armbruster666daa62010-06-02 18:48:27 +0200318 int snapshot = 0;
Stefan Hajnoczifb0490f2011-11-17 13:40:32 +0000319 bool copy_on_read;
Markus Armbruster666daa62010-06-02 18:48:27 +0200320 int ret;
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100321 Error *error = NULL;
Kevin Wolf00063832013-03-15 10:35:07 +0100322 QemuOpts *opts;
323 QDict *bs_opts;
324 const char *id;
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200325 bool has_driver_specific_opts;
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400326 BlockDriver *drv = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200327
Markus Armbruster666daa62010-06-02 18:48:27 +0200328 translation = BIOS_ATA_TRANSLATION_AUTO;
Markus Armbruster666daa62010-06-02 18:48:27 +0200329 media = MEDIA_DISK;
330
Kevin Wolf00063832013-03-15 10:35:07 +0100331 /* Check common options by copying from all_opts to opts, all other options
332 * are stored in bs_opts. */
333 id = qemu_opts_id(all_opts);
334 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
335 if (error_is_set(&error)) {
336 qerror_report_err(error);
337 error_free(error);
338 return NULL;
339 }
340
341 bs_opts = qdict_new();
342 qemu_opts_to_qdict(all_opts, bs_opts);
343 qemu_opts_absorb_qdict(opts, bs_opts, &error);
344 if (error_is_set(&error)) {
345 qerror_report_err(error);
346 error_free(error);
347 return NULL;
348 }
349
350 if (id) {
351 qdict_del(bs_opts, "id");
352 }
353
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200354 has_driver_specific_opts = !!qdict_size(bs_opts);
355
Markus Armbruster666daa62010-06-02 18:48:27 +0200356 /* extract parameters */
357 bus_id = qemu_opt_get_number(opts, "bus", 0);
358 unit_id = qemu_opt_get_number(opts, "unit", -1);
359 index = qemu_opt_get_number(opts, "index", -1);
360
361 cyls = qemu_opt_get_number(opts, "cyls", 0);
362 heads = qemu_opt_get_number(opts, "heads", 0);
363 secs = qemu_opt_get_number(opts, "secs", 0);
364
365 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Kevin Wolf0f227a92013-07-19 20:07:29 +0200366 ro = qemu_opt_get_bool(opts, "read-only", 0);
Stefan Hajnoczifb0490f2011-11-17 13:40:32 +0000367 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
Markus Armbruster666daa62010-06-02 18:48:27 +0200368
369 file = qemu_opt_get(opts, "file");
370 serial = qemu_opt_get(opts, "serial");
371
372 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
Markus Armbruster19609662011-01-28 11:21:39 +0100373 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
374 ;
375 if (type == IF_COUNT) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100376 error_report("unsupported bus type '%s'", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200377 return NULL;
378 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300379 } else {
Christian Borntraeger2d0d2832012-11-20 15:30:34 +0100380 type = block_default_type;
Markus Armbruster666daa62010-06-02 18:48:27 +0200381 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300382
Markus Armbruster19609662011-01-28 11:21:39 +0100383 max_devs = if_max_devs[type];
Markus Armbruster666daa62010-06-02 18:48:27 +0200384
385 if (cyls || heads || secs) {
Markus Armbrusteraaea3f32012-07-10 11:12:54 +0200386 if (cyls < 1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100387 error_report("invalid physical cyls number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200388 return NULL;
389 }
Markus Armbrusteraaea3f32012-07-10 11:12:54 +0200390 if (heads < 1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100391 error_report("invalid physical heads number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200392 return NULL;
393 }
Markus Armbrusteraaea3f32012-07-10 11:12:54 +0200394 if (secs < 1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100395 error_report("invalid physical secs number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200396 return NULL;
397 }
398 }
399
400 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
401 if (!cyls) {
Markus Armbrustere4080f92011-06-22 14:03:57 +0200402 error_report("'%s' trans must be used with cyls, heads and secs",
Markus Armbruster807105a2011-01-17 19:31:27 +0100403 buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200404 return NULL;
405 }
406 if (!strcmp(buf, "none"))
407 translation = BIOS_ATA_TRANSLATION_NONE;
408 else if (!strcmp(buf, "lba"))
409 translation = BIOS_ATA_TRANSLATION_LBA;
410 else if (!strcmp(buf, "auto"))
411 translation = BIOS_ATA_TRANSLATION_AUTO;
412 else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100413 error_report("'%s' invalid translation type", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200414 return NULL;
415 }
416 }
417
418 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
419 if (!strcmp(buf, "disk")) {
420 media = MEDIA_DISK;
421 } else if (!strcmp(buf, "cdrom")) {
422 if (cyls || secs || heads) {
Luiz Capitulinoe7ff8f02011-07-01 10:46:13 -0300423 error_report("CHS can't be set with media=%s", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200424 return NULL;
425 }
426 media = MEDIA_CDROM;
427 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100428 error_report("'%s' invalid media", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200429 return NULL;
430 }
431 }
432
Paolo Bonzinia9384af2013-02-08 14:06:12 +0100433 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
434 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
435 error_report("invalid discard option");
436 return NULL;
437 }
438 }
439
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200440 if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
441 bdrv_flags |= BDRV_O_CACHE_WB;
442 }
443 if (qemu_opt_get_bool(opts, "cache.direct", false)) {
444 bdrv_flags |= BDRV_O_NOCACHE;
445 }
Paolo Bonzini1df6fa42013-09-19 18:48:53 +0200446 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200447 bdrv_flags |= BDRV_O_NO_FLUSH;
Markus Armbruster666daa62010-06-02 18:48:27 +0200448 }
449
450#ifdef CONFIG_LINUX_AIO
451 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
452 if (!strcmp(buf, "native")) {
453 bdrv_flags |= BDRV_O_NATIVE_AIO;
454 } else if (!strcmp(buf, "threads")) {
455 /* this is the default */
456 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100457 error_report("invalid aio option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200458 return NULL;
459 }
460 }
461#endif
462
463 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
Peter Maydellc8057f92012-08-02 13:45:54 +0100464 if (is_help_option(buf)) {
465 error_printf("Supported formats:");
466 bdrv_iterate_format(bdrv_format_print, NULL);
467 error_printf("\n");
468 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200469 }
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200470
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400471 drv = bdrv_find_whitelisted_format(buf, ro);
472 if (!drv) {
Fam Zheng7780d472013-08-22 15:36:59 +0800473 if (!ro && bdrv_find_whitelisted_format(buf, !ro)) {
474 error_report("'%s' can be only used as read-only device.", buf);
475 } else {
476 error_report("'%s' invalid format", buf);
477 }
Mike Qiu6db5f5d2013-08-08 10:45:16 -0400478 return NULL;
479 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200480 }
481
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800482 /* disk I/O throttling */
Benoît Canetcc0681c2013-09-02 14:14:39 +0200483 memset(&cfg, 0, sizeof(cfg));
484 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
Kevin Wolf57975222013-07-17 14:41:54 +0200485 qemu_opt_get_number(opts, "throttling.bps-total", 0);
Benoît Canetcc0681c2013-09-02 14:14:39 +0200486 cfg.buckets[THROTTLE_BPS_READ].avg =
Kevin Wolf57975222013-07-17 14:41:54 +0200487 qemu_opt_get_number(opts, "throttling.bps-read", 0);
Benoît Canetcc0681c2013-09-02 14:14:39 +0200488 cfg.buckets[THROTTLE_BPS_WRITE].avg =
Kevin Wolf57975222013-07-17 14:41:54 +0200489 qemu_opt_get_number(opts, "throttling.bps-write", 0);
Benoît Canetcc0681c2013-09-02 14:14:39 +0200490 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
Kevin Wolf57975222013-07-17 14:41:54 +0200491 qemu_opt_get_number(opts, "throttling.iops-total", 0);
Benoît Canetcc0681c2013-09-02 14:14:39 +0200492 cfg.buckets[THROTTLE_OPS_READ].avg =
Kevin Wolf57975222013-07-17 14:41:54 +0200493 qemu_opt_get_number(opts, "throttling.iops-read", 0);
Benoît Canetcc0681c2013-09-02 14:14:39 +0200494 cfg.buckets[THROTTLE_OPS_WRITE].avg =
Kevin Wolf57975222013-07-17 14:41:54 +0200495 qemu_opt_get_number(opts, "throttling.iops-write", 0);
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800496
Benoît Canet3e9fab62013-09-02 14:14:40 +0200497 cfg.buckets[THROTTLE_BPS_TOTAL].max =
498 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
499 cfg.buckets[THROTTLE_BPS_READ].max =
500 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
501 cfg.buckets[THROTTLE_BPS_WRITE].max =
502 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
503 cfg.buckets[THROTTLE_OPS_TOTAL].max =
504 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
505 cfg.buckets[THROTTLE_OPS_READ].max =
506 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
507 cfg.buckets[THROTTLE_OPS_WRITE].max =
508 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
Benoît Canetcc0681c2013-09-02 14:14:39 +0200509
Benoît Canet2024c1d2013-09-02 14:14:41 +0200510 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
Benoît Canetcc0681c2013-09-02 14:14:39 +0200511
512 if (!check_throttle_config(&cfg, &error)) {
Stefan Hajnoczic5461942013-02-13 16:53:42 +0100513 error_report("%s", error_get_pretty(error));
514 error_free(error);
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800515 return NULL;
516 }
517
Jan Kiszka0d92d172012-10-05 14:51:43 -0300518 if (qemu_opt_get(opts, "boot") != NULL) {
519 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
520 "ignored. Future versions will reject this parameter. Please "
521 "update your scripts.\n");
522 }
523
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200524 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
Markus Armbruster666daa62010-06-02 18:48:27 +0200525 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
526 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100527 error_report("werror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200528 return NULL;
529 }
530
531 on_write_error = parse_block_error_action(buf, 0);
532 if (on_write_error < 0) {
533 return NULL;
534 }
535 }
536
Paolo Bonzini92aa5c62012-09-28 17:22:55 +0200537 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
Markus Armbruster666daa62010-06-02 18:48:27 +0200538 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolf5dba48a2010-10-25 14:52:21 +0200539 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100540 error_report("rerror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200541 return NULL;
542 }
543
544 on_read_error = parse_block_error_action(buf, 1);
545 if (on_read_error < 0) {
546 return NULL;
547 }
548 }
549
550 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
551 if (type != IF_VIRTIO) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100552 error_report("addr is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200553 return NULL;
554 }
555 }
556
557 /* compute bus and unit according index */
558
559 if (index != -1) {
560 if (bus_id != 0 || unit_id != -1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100561 error_report("index cannot be used with bus and unit");
Markus Armbruster666daa62010-06-02 18:48:27 +0200562 return NULL;
563 }
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100564 bus_id = drive_index_to_bus_id(type, index);
565 unit_id = drive_index_to_unit_id(type, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200566 }
567
568 /* if user doesn't specify a unit_id,
569 * try to find the first free
570 */
571
572 if (unit_id == -1) {
573 unit_id = 0;
574 while (drive_get(type, bus_id, unit_id) != NULL) {
575 unit_id++;
576 if (max_devs && unit_id >= max_devs) {
577 unit_id -= max_devs;
578 bus_id++;
579 }
580 }
581 }
582
583 /* check unit id */
584
585 if (max_devs && unit_id >= max_devs) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100586 error_report("unit %d too big (max is %d)",
587 unit_id, max_devs - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200588 return NULL;
589 }
590
591 /*
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100592 * catch multiple definitions
Markus Armbruster666daa62010-06-02 18:48:27 +0200593 */
594
595 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100596 error_report("drive with bus=%d, unit=%d (index=%d) exists",
597 bus_id, unit_id, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200598 return NULL;
599 }
600
601 /* init */
602
Anthony Liguori7267c092011-08-20 22:09:37 -0500603 dinfo = g_malloc0(sizeof(*dinfo));
Markus Armbruster666daa62010-06-02 18:48:27 +0200604 if ((buf = qemu_opts_id(opts)) != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500605 dinfo->id = g_strdup(buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200606 } else {
607 /* no id supplied -> create one */
Anthony Liguori7267c092011-08-20 22:09:37 -0500608 dinfo->id = g_malloc0(32);
Markus Armbruster666daa62010-06-02 18:48:27 +0200609 if (type == IF_IDE || type == IF_SCSI)
610 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
611 if (max_devs)
612 snprintf(dinfo->id, 32, "%s%i%s%i",
Markus Armbruster79d21d52012-06-05 14:52:53 +0200613 if_name[type], bus_id, mediastr, unit_id);
Markus Armbruster666daa62010-06-02 18:48:27 +0200614 else
615 snprintf(dinfo->id, 32, "%s%s%i",
Markus Armbruster79d21d52012-06-05 14:52:53 +0200616 if_name[type], mediastr, unit_id);
Markus Armbruster666daa62010-06-02 18:48:27 +0200617 }
618 dinfo->bdrv = bdrv_new(dinfo->id);
Kevin Shanahan80dd1aa2012-09-21 08:50:22 +0930619 dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
620 dinfo->bdrv->read_only = ro;
Markus Armbruster666daa62010-06-02 18:48:27 +0200621 dinfo->devaddr = devaddr;
622 dinfo->type = type;
623 dinfo->bus = bus_id;
624 dinfo->unit = unit_id;
Markus Armbruster317bb412012-07-10 11:12:39 +0200625 dinfo->cyls = cyls;
626 dinfo->heads = heads;
627 dinfo->secs = secs;
628 dinfo->trans = translation;
Kevin Wolf00063832013-03-15 10:35:07 +0100629 dinfo->opts = all_opts;
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200630 dinfo->refcount = 1;
Kevin Wolfbb446192013-03-15 10:35:06 +0100631 if (serial != NULL) {
632 dinfo->serial = g_strdup(serial);
633 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200634 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
635
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200636 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
637
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800638 /* disk I/O throttling */
Benoît Canetcc0681c2013-09-02 14:14:39 +0200639 if (throttle_enabled(&cfg)) {
640 bdrv_io_limits_enable(dinfo->bdrv);
641 bdrv_set_io_limits(dinfo->bdrv, &cfg);
642 }
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800643
Markus Armbruster666daa62010-06-02 18:48:27 +0200644 switch(type) {
645 case IF_IDE:
646 case IF_SCSI:
647 case IF_XEN:
648 case IF_NONE:
Markus Armbruster2b584952012-07-10 11:12:50 +0200649 dinfo->media_cd = media == MEDIA_CDROM;
Markus Armbruster666daa62010-06-02 18:48:27 +0200650 break;
651 case IF_SD:
Markus Armbruster666daa62010-06-02 18:48:27 +0200652 case IF_FLOPPY:
Markus Armbruster666daa62010-06-02 18:48:27 +0200653 case IF_PFLASH:
654 case IF_MTD:
655 break;
656 case IF_VIRTIO:
Kevin Wolf00063832013-03-15 10:35:07 +0100657 {
Markus Armbruster666daa62010-06-02 18:48:27 +0200658 /* add virtio block device */
Kevin Wolf00063832013-03-15 10:35:07 +0100659 QemuOpts *devopts;
660 devopts = qemu_opts_create_nofail(qemu_find_opts("device"));
Anthony Liguorieeb9c1b2012-02-03 11:13:30 -0600661 if (arch_type == QEMU_ARCH_S390X) {
Kevin Wolf00063832013-03-15 10:35:07 +0100662 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
Anthony Liguorieeb9c1b2012-02-03 11:13:30 -0600663 } else {
Kevin Wolf00063832013-03-15 10:35:07 +0100664 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
Anthony Liguorieeb9c1b2012-02-03 11:13:30 -0600665 }
Kevin Wolf00063832013-03-15 10:35:07 +0100666 qemu_opt_set(devopts, "drive", dinfo->id);
Markus Armbruster666daa62010-06-02 18:48:27 +0200667 if (devaddr)
Kevin Wolf00063832013-03-15 10:35:07 +0100668 qemu_opt_set(devopts, "addr", devaddr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200669 break;
Kevin Wolf00063832013-03-15 10:35:07 +0100670 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100671 default:
Markus Armbruster666daa62010-06-02 18:48:27 +0200672 abort();
673 }
Markus Armbrusterdd5b0d72010-05-11 15:36:46 +0200674 if (!file || !*file) {
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200675 if (has_driver_specific_opts) {
Kevin Wolfc2ad1b02013-03-18 16:40:51 +0100676 file = NULL;
677 } else {
678 return dinfo;
679 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200680 }
681 if (snapshot) {
682 /* always use cache=unsafe with snapshot */
683 bdrv_flags &= ~BDRV_O_CACHE_MASK;
684 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
685 }
686
Stefan Hajnoczifb0490f2011-11-17 13:40:32 +0000687 if (copy_on_read) {
688 bdrv_flags |= BDRV_O_COPY_ON_READ;
689 }
690
Benoît Caneted9d4202012-03-23 08:36:50 +0100691 if (runstate_check(RUN_STATE_INMIGRATE)) {
692 bdrv_flags |= BDRV_O_INCOMING;
693 }
694
Markus Armbruster666daa62010-06-02 18:48:27 +0200695 if (media == MEDIA_CDROM) {
696 /* CDROM is fine for any interface, don't check. */
697 ro = 1;
698 } else if (ro == 1) {
Jordan Justen1e9eb782012-02-21 23:18:48 -0800699 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
700 type != IF_NONE && type != IF_PFLASH) {
Kevin Wolf0f227a92013-07-19 20:07:29 +0200701 error_report("read-only not supported by this bus type");
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100702 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200703 }
704 }
705
706 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
707
Stefan Hajnoczi04d4abe2012-06-27 18:03:13 +0100708 if (ro && copy_on_read) {
Kevin Wolf0f227a92013-07-19 20:07:29 +0200709 error_report("warning: disabling copy_on_read on read-only drive");
Stefan Hajnoczi04d4abe2012-06-27 18:03:13 +0100710 }
711
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200712 QINCREF(bs_opts);
Max Reitz34b5d2c2013-09-05 14:45:29 +0200713 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error);
Kevin Wolf00063832013-03-15 10:35:07 +0100714
Markus Armbruster666daa62010-06-02 18:48:27 +0200715 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200716 error_report("could not open disk image %s: %s",
717 file ?: dinfo->id, error_get_pretty(error));
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100718 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200719 }
720
721 if (bdrv_key_required(dinfo->bdrv))
722 autostart = 0;
Kevin Wolf00063832013-03-15 10:35:07 +0100723
Kevin Wolf74fe54f2013-07-09 11:09:02 +0200724 QDECREF(bs_opts);
Kevin Wolf00063832013-03-15 10:35:07 +0100725 qemu_opts_del(opts);
726
Markus Armbruster666daa62010-06-02 18:48:27 +0200727 return dinfo;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100728
729err:
Kevin Wolf00063832013-03-15 10:35:07 +0100730 qemu_opts_del(opts);
731 QDECREF(bs_opts);
Fam Zheng4f6fd342013-08-23 09:14:47 +0800732 bdrv_unref(dinfo->bdrv);
Anthony Liguori7267c092011-08-20 22:09:37 -0500733 g_free(dinfo->id);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100734 QTAILQ_REMOVE(&drives, dinfo, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500735 g_free(dinfo);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100736 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200737}
738
Kevin Wolf57975222013-07-17 14:41:54 +0200739static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to)
740{
741 const char *value;
742
743 value = qemu_opt_get(opts, from);
744 if (value) {
745 qemu_opt_set(opts, to, value);
746 qemu_opt_unset(opts, from);
747 }
748}
749
750DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
751{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200752 const char *value;
753
Kevin Wolf57975222013-07-17 14:41:54 +0200754 /* Change legacy command line options into QMP ones */
755 qemu_opt_rename(all_opts, "iops", "throttling.iops-total");
756 qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read");
757 qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write");
758
759 qemu_opt_rename(all_opts, "bps", "throttling.bps-total");
760 qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read");
761 qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write");
762
Benoît Canet3e9fab62013-09-02 14:14:40 +0200763 qemu_opt_rename(all_opts, "iops_max", "throttling.iops-total-max");
764 qemu_opt_rename(all_opts, "iops_rd_max", "throttling.iops-read-max");
765 qemu_opt_rename(all_opts, "iops_wr_max", "throttling.iops-write-max");
766
767 qemu_opt_rename(all_opts, "bps_max", "throttling.bps-total-max");
768 qemu_opt_rename(all_opts, "bps_rd_max", "throttling.bps-read-max");
769 qemu_opt_rename(all_opts, "bps_wr_max", "throttling.bps-write-max");
770
Benoît Canet2024c1d2013-09-02 14:14:41 +0200771 qemu_opt_rename(all_opts,
772 "iops_size", "throttling.iops-size");
773
Kevin Wolf0f227a92013-07-19 20:07:29 +0200774 qemu_opt_rename(all_opts, "readonly", "read-only");
775
Kevin Wolf29c4e2b2013-07-18 16:31:25 +0200776 value = qemu_opt_get(all_opts, "cache");
777 if (value) {
778 int flags = 0;
779
780 if (bdrv_parse_cache_flags(value, &flags) != 0) {
781 error_report("invalid cache option");
782 return NULL;
783 }
784
785 /* Specific options take precedence */
786 if (!qemu_opt_get(all_opts, "cache.writeback")) {
787 qemu_opt_set_bool(all_opts, "cache.writeback",
788 !!(flags & BDRV_O_CACHE_WB));
789 }
790 if (!qemu_opt_get(all_opts, "cache.direct")) {
791 qemu_opt_set_bool(all_opts, "cache.direct",
792 !!(flags & BDRV_O_NOCACHE));
793 }
794 if (!qemu_opt_get(all_opts, "cache.no-flush")) {
795 qemu_opt_set_bool(all_opts, "cache.no-flush",
796 !!(flags & BDRV_O_NO_FLUSH));
797 }
798 qemu_opt_unset(all_opts, "cache");
799 }
800
Kevin Wolf57975222013-07-17 14:41:54 +0200801 return blockdev_init(all_opts, block_default_type);
802}
803
Markus Armbruster666daa62010-06-02 18:48:27 +0200804void do_commit(Monitor *mon, const QDict *qdict)
805{
Markus Armbruster666daa62010-06-02 18:48:27 +0200806 const char *device = qdict_get_str(qdict, "device");
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200807 BlockDriverState *bs;
Stefan Hajnoczie8877492012-03-05 18:10:11 +0000808 int ret;
Markus Armbruster666daa62010-06-02 18:48:27 +0200809
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200810 if (!strcmp(device, "all")) {
Stefan Hajnoczie8877492012-03-05 18:10:11 +0000811 ret = bdrv_commit_all();
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200812 } else {
813 bs = bdrv_find(device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200814 if (!bs) {
Jeff Cody58513bd2013-01-18 12:45:35 -0500815 monitor_printf(mon, "Device '%s' not found\n", device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200816 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200817 }
Stefan Hajnoczi2d3735d2012-01-18 14:40:41 +0000818 ret = bdrv_commit(bs);
Jeff Cody58513bd2013-01-18 12:45:35 -0500819 }
820 if (ret < 0) {
821 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
822 strerror(-ret));
Markus Armbruster666daa62010-06-02 18:48:27 +0200823 }
824}
825
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100826static void blockdev_do_action(int kind, void *data, Error **errp)
827{
Kevin Wolfc8a83e82013-05-13 10:43:43 +0200828 TransactionAction action;
829 TransactionActionList list;
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100830
831 action.kind = kind;
832 action.data = data;
833 list.value = &action;
834 list.next = NULL;
835 qmp_transaction(&list, errp);
836}
837
Luiz Capitulino6106e242011-11-25 16:15:19 -0200838void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
839 bool has_format, const char *format,
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100840 bool has_mode, enum NewImageMode mode,
Luiz Capitulino6106e242011-11-25 16:15:19 -0200841 Error **errp)
Jes Sorensenf8882562010-12-16 13:52:16 +0100842{
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100843 BlockdevSnapshot snapshot = {
844 .device = (char *) device,
845 .snapshot_file = (char *) snapshot_file,
846 .has_format = has_format,
847 .format = (char *) format,
848 .has_mode = has_mode,
849 .mode = mode,
850 };
Kevin Wolfc8a83e82013-05-13 10:43:43 +0200851 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
852 &snapshot, errp);
Jes Sorensenf8882562010-12-16 13:52:16 +0100853}
854
Wenchao Xiaf323bc92013-09-11 14:04:35 +0800855void qmp_blockdev_snapshot_internal_sync(const char *device,
856 const char *name,
857 Error **errp)
858{
859 BlockdevSnapshotInternal snapshot = {
860 .device = (char *) device,
861 .name = (char *) name
862 };
863
864 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
865 &snapshot, errp);
866}
867
Wenchao Xia44e3e052013-09-11 14:04:36 +0800868SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
869 bool has_id,
870 const char *id,
871 bool has_name,
872 const char *name,
873 Error **errp)
874{
875 BlockDriverState *bs = bdrv_find(device);
876 QEMUSnapshotInfo sn;
877 Error *local_err = NULL;
878 SnapshotInfo *info = NULL;
879 int ret;
880
881 if (!bs) {
882 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
883 return NULL;
884 }
885
886 if (!has_id) {
887 id = NULL;
888 }
889
890 if (!has_name) {
891 name = NULL;
892 }
893
894 if (!id && !name) {
895 error_setg(errp, "Name or id must be provided");
896 return NULL;
897 }
898
899 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
900 if (error_is_set(&local_err)) {
901 error_propagate(errp, local_err);
902 return NULL;
903 }
904 if (!ret) {
905 error_setg(errp,
906 "Snapshot with id '%s' and name '%s' does not exist on "
907 "device '%s'",
908 STR_OR_NULL(id), STR_OR_NULL(name), device);
909 return NULL;
910 }
911
912 bdrv_snapshot_delete(bs, id, name, &local_err);
913 if (error_is_set(&local_err)) {
914 error_propagate(errp, local_err);
915 return NULL;
916 }
917
918 info = g_malloc0(sizeof(SnapshotInfo));
919 info->id = g_strdup(sn.id_str);
920 info->name = g_strdup(sn.name);
921 info->date_nsec = sn.date_nsec;
922 info->date_sec = sn.date_sec;
923 info->vm_state_size = sn.vm_state_size;
924 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
925 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
926
927 return info;
928}
Jeff Cody8802d1f2012-02-28 15:54:06 -0500929
930/* New and old BlockDriverState structs for group snapshots */
Wenchao Xiaba0c86a2013-05-08 18:25:16 +0800931
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +0200932typedef struct BlkTransactionState BlkTransactionState;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +0800933
934/* Only prepare() may fail. In a single transaction, only one of commit() or
935 abort() will be called, clean() will always be called if it present. */
936typedef struct BdrvActionOps {
937 /* Size of state struct, in bytes. */
938 size_t instance_size;
939 /* Prepare the work, must NOT be NULL. */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +0200940 void (*prepare)(BlkTransactionState *common, Error **errp);
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +0200941 /* Commit the changes, can be NULL. */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +0200942 void (*commit)(BlkTransactionState *common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +0800943 /* Abort the changes on fail, can be NULL. */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +0200944 void (*abort)(BlkTransactionState *common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +0800945 /* Clean up resource in the end, can be NULL. */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +0200946 void (*clean)(BlkTransactionState *common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +0800947} BdrvActionOps;
948
949/*
950 * This structure must be arranged as first member in child type, assuming
951 * that compiler will also arrange it to the same address with parent instance.
952 * Later it will be used in free().
953 */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +0200954struct BlkTransactionState {
Kevin Wolfc8a83e82013-05-13 10:43:43 +0200955 TransactionAction *action;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +0800956 const BdrvActionOps *ops;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +0200957 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +0800958};
959
Wenchao Xiabbe86012013-09-11 14:04:34 +0800960/* internal snapshot private data */
961typedef struct InternalSnapshotState {
962 BlkTransactionState common;
963 BlockDriverState *bs;
964 QEMUSnapshotInfo sn;
965} InternalSnapshotState;
966
967static void internal_snapshot_prepare(BlkTransactionState *common,
968 Error **errp)
969{
970 const char *device;
971 const char *name;
972 BlockDriverState *bs;
973 QEMUSnapshotInfo old_sn, *sn;
974 bool ret;
975 qemu_timeval tv;
976 BlockdevSnapshotInternal *internal;
977 InternalSnapshotState *state;
978 int ret1;
979
980 g_assert(common->action->kind ==
981 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
982 internal = common->action->blockdev_snapshot_internal_sync;
983 state = DO_UPCAST(InternalSnapshotState, common, common);
984
985 /* 1. parse input */
986 device = internal->device;
987 name = internal->name;
988
989 /* 2. check for validation */
990 bs = bdrv_find(device);
991 if (!bs) {
992 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
993 return;
994 }
995
996 if (!bdrv_is_inserted(bs)) {
997 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
998 return;
999 }
1000
1001 if (bdrv_is_read_only(bs)) {
1002 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1003 return;
1004 }
1005
1006 if (!bdrv_can_snapshot(bs)) {
1007 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1008 bs->drv->format_name, device, "internal snapshot");
1009 return;
1010 }
1011
1012 if (!strlen(name)) {
1013 error_setg(errp, "Name is empty");
1014 return;
1015 }
1016
1017 /* check whether a snapshot with name exist */
1018 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, errp);
1019 if (error_is_set(errp)) {
1020 return;
1021 } else if (ret) {
1022 error_setg(errp,
1023 "Snapshot with name '%s' already exists on device '%s'",
1024 name, device);
1025 return;
1026 }
1027
1028 /* 3. take the snapshot */
1029 sn = &state->sn;
1030 pstrcpy(sn->name, sizeof(sn->name), name);
1031 qemu_gettimeofday(&tv);
1032 sn->date_sec = tv.tv_sec;
1033 sn->date_nsec = tv.tv_usec * 1000;
1034 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1035
1036 ret1 = bdrv_snapshot_create(bs, sn);
1037 if (ret1 < 0) {
1038 error_setg_errno(errp, -ret1,
1039 "Failed to create snapshot '%s' on device '%s'",
1040 name, device);
1041 return;
1042 }
1043
1044 /* 4. succeed, mark a snapshot is created */
1045 state->bs = bs;
1046}
1047
1048static void internal_snapshot_abort(BlkTransactionState *common)
1049{
1050 InternalSnapshotState *state =
1051 DO_UPCAST(InternalSnapshotState, common, common);
1052 BlockDriverState *bs = state->bs;
1053 QEMUSnapshotInfo *sn = &state->sn;
1054 Error *local_error = NULL;
1055
1056 if (!bs) {
1057 return;
1058 }
1059
1060 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1061 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1062 "device '%s' in abort: %s",
1063 sn->id_str,
1064 sn->name,
1065 bdrv_get_device_name(bs),
1066 error_get_pretty(local_error));
1067 error_free(local_error);
1068 }
1069}
1070
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001071/* external snapshot private data */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001072typedef struct ExternalSnapshotState {
1073 BlkTransactionState common;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001074 BlockDriverState *old_bs;
1075 BlockDriverState *new_bs;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001076} ExternalSnapshotState;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001077
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001078static void external_snapshot_prepare(BlkTransactionState *common,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001079 Error **errp)
1080{
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001081 BlockDriver *drv;
1082 int flags, ret;
1083 Error *local_err = NULL;
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001084 const char *device;
1085 const char *new_image_file;
1086 const char *format = "qcow2";
1087 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001088 ExternalSnapshotState *state =
1089 DO_UPCAST(ExternalSnapshotState, common, common);
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001090 TransactionAction *action = common->action;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001091
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001092 /* get parameters */
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001093 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
Wenchao Xiae2a31e82013-05-08 18:25:13 +08001094
1095 device = action->blockdev_snapshot_sync->device;
1096 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1097 if (action->blockdev_snapshot_sync->has_format) {
1098 format = action->blockdev_snapshot_sync->format;
1099 }
1100 if (action->blockdev_snapshot_sync->has_mode) {
1101 mode = action->blockdev_snapshot_sync->mode;
1102 }
1103
1104 /* start processing */
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001105 drv = bdrv_find_format(format);
1106 if (!drv) {
1107 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1108 return;
1109 }
1110
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001111 state->old_bs = bdrv_find(device);
1112 if (!state->old_bs) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001113 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1114 return;
1115 }
1116
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001117 if (!bdrv_is_inserted(state->old_bs)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001118 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1119 return;
1120 }
1121
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001122 if (bdrv_in_use(state->old_bs)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001123 error_set(errp, QERR_DEVICE_IN_USE, device);
1124 return;
1125 }
1126
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001127 if (!bdrv_is_read_only(state->old_bs)) {
1128 if (bdrv_flush(state->old_bs)) {
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001129 error_set(errp, QERR_IO_ERROR);
1130 return;
1131 }
1132 }
1133
Benoît Canetf6186f42013-10-02 14:33:48 +02001134 if (bdrv_check_ext_snapshot(state->old_bs) != EXT_SNAPSHOT_ALLOWED) {
1135 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1136 return;
1137 }
1138
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001139 flags = state->old_bs->open_flags;
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001140
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001141 /* create new image w/backing file */
1142 if (mode != NEW_IMAGE_MODE_EXISTING) {
1143 bdrv_img_create(new_image_file, format,
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001144 state->old_bs->filename,
1145 state->old_bs->drv->format_name,
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001146 NULL, -1, flags, &local_err, false);
1147 if (error_is_set(&local_err)) {
1148 error_propagate(errp, local_err);
1149 return;
1150 }
1151 }
1152
1153 /* We will manually add the backing_hd field to the bs later */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001154 state->new_bs = bdrv_new("");
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001155 /* TODO Inherit bs->options or only take explicit options with an
1156 * extended QMP command? */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001157 ret = bdrv_open(state->new_bs, new_image_file, NULL,
Max Reitz34b5d2c2013-09-05 14:45:29 +02001158 flags | BDRV_O_NO_BACKING, drv, &local_err);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001159 if (ret != 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001160 error_propagate(errp, local_err);
Wenchao Xia9b9877e2013-05-08 18:25:12 +08001161 }
1162}
1163
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001164static void external_snapshot_commit(BlkTransactionState *common)
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001165{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001166 ExternalSnapshotState *state =
1167 DO_UPCAST(ExternalSnapshotState, common, common);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001168
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001169 /* This removes our old bs and adds the new bs */
1170 bdrv_append(state->new_bs, state->old_bs);
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001171 /* We don't need (or want) to use the transactional
1172 * bdrv_reopen_multiple() across all the entries at once, because we
1173 * don't want to abort all of them if one of them fails the reopen */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001174 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
Wenchao Xia3b0047e2013-05-08 18:25:14 +08001175 NULL);
1176}
1177
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001178static void external_snapshot_abort(BlkTransactionState *common)
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001179{
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001180 ExternalSnapshotState *state =
1181 DO_UPCAST(ExternalSnapshotState, common, common);
1182 if (state->new_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001183 bdrv_unref(state->new_bs);
Wenchao Xia96b86bf2013-05-08 18:25:15 +08001184 }
1185}
1186
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001187typedef struct DriveBackupState {
1188 BlkTransactionState common;
1189 BlockDriverState *bs;
1190 BlockJob *job;
1191} DriveBackupState;
1192
1193static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1194{
1195 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1196 DriveBackup *backup;
1197 Error *local_err = NULL;
1198
1199 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1200 backup = common->action->drive_backup;
1201
1202 qmp_drive_backup(backup->device, backup->target,
1203 backup->has_format, backup->format,
Stefan Hajnoczib53169e2013-06-26 14:11:57 +02001204 backup->sync,
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001205 backup->has_mode, backup->mode,
1206 backup->has_speed, backup->speed,
1207 backup->has_on_source_error, backup->on_source_error,
1208 backup->has_on_target_error, backup->on_target_error,
1209 &local_err);
1210 if (error_is_set(&local_err)) {
1211 error_propagate(errp, local_err);
1212 state->bs = NULL;
1213 state->job = NULL;
1214 return;
1215 }
1216
1217 state->bs = bdrv_find(backup->device);
1218 state->job = state->bs->job;
1219}
1220
1221static void drive_backup_abort(BlkTransactionState *common)
1222{
1223 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1224 BlockDriverState *bs = state->bs;
1225
1226 /* Only cancel if it's the job we started */
1227 if (bs && bs->job && bs->job == state->job) {
1228 block_job_cancel_sync(bs->job);
1229 }
1230}
1231
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02001232static void abort_prepare(BlkTransactionState *common, Error **errp)
1233{
1234 error_setg(errp, "Transaction aborted using Abort action");
1235}
1236
1237static void abort_commit(BlkTransactionState *common)
1238{
Stefan Weildfc6f862013-07-25 18:21:28 +02001239 g_assert_not_reached(); /* this action never succeeds */
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02001240}
1241
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001242static const BdrvActionOps actions[] = {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001243 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001244 .instance_size = sizeof(ExternalSnapshotState),
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001245 .prepare = external_snapshot_prepare,
1246 .commit = external_snapshot_commit,
1247 .abort = external_snapshot_abort,
1248 },
Stefan Hajnoczi3037f362013-06-24 17:13:17 +02001249 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1250 .instance_size = sizeof(DriveBackupState),
1251 .prepare = drive_backup_prepare,
1252 .abort = drive_backup_abort,
1253 },
Stefan Hajnoczi78b18b72013-06-24 17:13:18 +02001254 [TRANSACTION_ACTION_KIND_ABORT] = {
1255 .instance_size = sizeof(BlkTransactionState),
1256 .prepare = abort_prepare,
1257 .commit = abort_commit,
1258 },
Wenchao Xiabbe86012013-09-11 14:04:34 +08001259 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1260 .instance_size = sizeof(InternalSnapshotState),
1261 .prepare = internal_snapshot_prepare,
1262 .abort = internal_snapshot_abort,
1263 },
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001264};
1265
Jeff Cody8802d1f2012-02-28 15:54:06 -05001266/*
1267 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1268 * then we do not pivot any of the devices in the group, and abandon the
1269 * snapshots
1270 */
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001271void qmp_transaction(TransactionActionList *dev_list, Error **errp)
Jeff Cody8802d1f2012-02-28 15:54:06 -05001272{
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001273 TransactionActionList *dev_entry = dev_list;
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001274 BlkTransactionState *state, *next;
Luiz Capitulino43e17042012-11-30 10:52:07 -02001275 Error *local_err = NULL;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001276
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001277 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
Jeff Cody8802d1f2012-02-28 15:54:06 -05001278 QSIMPLEQ_INIT(&snap_bdrv_states);
1279
1280 /* drain all i/o before any snapshots */
1281 bdrv_drain_all();
1282
1283 /* We don't do anything in this loop that commits us to the snapshot */
1284 while (NULL != dev_entry) {
Kevin Wolfc8a83e82013-05-13 10:43:43 +02001285 TransactionAction *dev_info = NULL;
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001286 const BdrvActionOps *ops;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01001287
Jeff Cody8802d1f2012-02-28 15:54:06 -05001288 dev_info = dev_entry->value;
1289 dev_entry = dev_entry->next;
1290
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001291 assert(dev_info->kind < ARRAY_SIZE(actions));
1292
1293 ops = &actions[dev_info->kind];
Max Reitzaa3fe712013-09-12 14:57:27 +02001294 assert(ops->instance_size > 0);
1295
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001296 state = g_malloc0(ops->instance_size);
1297 state->ops = ops;
1298 state->action = dev_info;
1299 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
Jeff Cody8802d1f2012-02-28 15:54:06 -05001300
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001301 state->ops->prepare(state, &local_err);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001302 if (error_is_set(&local_err)) {
1303 error_propagate(errp, local_err);
1304 goto delete_and_fail;
Paolo Bonzini52e7c242012-03-06 18:55:57 +01001305 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05001306 }
1307
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001308 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
Stefan Hajnoczif9ea81e2013-06-24 17:13:16 +02001309 if (state->ops->commit) {
1310 state->ops->commit(state);
1311 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05001312 }
1313
1314 /* success */
1315 goto exit;
1316
1317delete_and_fail:
1318 /*
1319 * failure, and it is all-or-none; abandon each new bs, and keep using
1320 * the original bs for all images
1321 */
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001322 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1323 if (state->ops->abort) {
1324 state->ops->abort(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001325 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05001326 }
1327exit:
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001328 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1329 if (state->ops->clean) {
1330 state->ops->clean(state);
Wenchao Xiaba0c86a2013-05-08 18:25:16 +08001331 }
Stefan Hajnocziba5d6ab2013-06-24 17:13:15 +02001332 g_free(state);
Jeff Cody8802d1f2012-02-28 15:54:06 -05001333 }
Jeff Cody8802d1f2012-02-28 15:54:06 -05001334}
1335
1336
Luiz Capitulino92d48552011-12-07 15:47:23 -02001337static void eject_device(BlockDriverState *bs, int force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02001338{
Stefan Hajnoczi2d3735d2012-01-18 14:40:41 +00001339 if (bdrv_in_use(bs)) {
1340 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
1341 return;
1342 }
Markus Armbruster2c6942f2011-09-06 18:58:51 +02001343 if (!bdrv_dev_has_removable_media(bs)) {
Luiz Capitulino92d48552011-12-07 15:47:23 -02001344 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
1345 return;
Markus Armbrusterea8f9422011-07-20 18:23:35 +02001346 }
Luiz Capitulino92d48552011-12-07 15:47:23 -02001347
Paolo Bonzini025ccaa2011-11-07 17:50:13 +01001348 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1349 bdrv_dev_eject_request(bs, force);
1350 if (!force) {
Luiz Capitulino92d48552011-12-07 15:47:23 -02001351 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
1352 return;
Paolo Bonzini025ccaa2011-11-07 17:50:13 +01001353 }
Markus Armbruster666daa62010-06-02 18:48:27 +02001354 }
Luiz Capitulino92d48552011-12-07 15:47:23 -02001355
Eduardo Habkost3b5276b2010-06-01 19:12:19 -03001356 bdrv_close(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +02001357}
1358
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02001359void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02001360{
1361 BlockDriverState *bs;
Markus Armbruster666daa62010-06-02 18:48:27 +02001362
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02001363 bs = bdrv_find(device);
Markus Armbruster666daa62010-06-02 18:48:27 +02001364 if (!bs) {
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02001365 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1366 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02001367 }
Luiz Capitulino92d48552011-12-07 15:47:23 -02001368
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02001369 eject_device(bs, force, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02001370}
1371
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02001372void qmp_block_passwd(const char *device, const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02001373{
1374 BlockDriverState *bs;
1375 int err;
1376
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02001377 bs = bdrv_find(device);
Markus Armbruster666daa62010-06-02 18:48:27 +02001378 if (!bs) {
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02001379 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1380 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02001381 }
1382
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02001383 err = bdrv_set_key(bs, password);
Markus Armbruster666daa62010-06-02 18:48:27 +02001384 if (err == -EINVAL) {
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02001385 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1386 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02001387 } else if (err < 0) {
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -02001388 error_set(errp, QERR_INVALID_PASSWORD);
1389 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02001390 }
Markus Armbruster666daa62010-06-02 18:48:27 +02001391}
1392
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001393static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1394 int bdrv_flags, BlockDriver *drv,
1395 const char *password, Error **errp)
1396{
Max Reitz34b5d2c2013-09-05 14:45:29 +02001397 Error *local_err = NULL;
Luiz Capitulino0eef4072013-06-07 14:33:48 -04001398 int ret;
1399
Max Reitz34b5d2c2013-09-05 14:45:29 +02001400 ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv, &local_err);
Luiz Capitulino0eef4072013-06-07 14:33:48 -04001401 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02001402 error_propagate(errp, local_err);
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001403 return;
1404 }
1405
1406 if (bdrv_key_required(bs)) {
1407 if (password) {
1408 if (bdrv_set_key(bs, password) < 0) {
1409 error_set(errp, QERR_INVALID_PASSWORD);
1410 }
1411 } else {
1412 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1413 bdrv_get_encrypted_filename(bs));
1414 }
1415 } else if (password) {
1416 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1417 }
1418}
1419
1420void qmp_change_blockdev(const char *device, const char *filename,
1421 bool has_format, const char *format, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +02001422{
1423 BlockDriverState *bs;
1424 BlockDriver *drv = NULL;
1425 int bdrv_flags;
Luiz Capitulino92d48552011-12-07 15:47:23 -02001426 Error *err = NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +02001427
1428 bs = bdrv_find(device);
1429 if (!bs) {
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001430 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1431 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02001432 }
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001433
1434 if (format) {
Fam Zhengb64ec4e2013-05-29 19:35:40 +08001435 drv = bdrv_find_whitelisted_format(format, bs->read_only);
Markus Armbruster666daa62010-06-02 18:48:27 +02001436 if (!drv) {
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001437 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1438 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02001439 }
1440 }
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001441
Luiz Capitulino92d48552011-12-07 15:47:23 -02001442 eject_device(bs, 0, &err);
1443 if (error_is_set(&err)) {
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001444 error_propagate(errp, err);
1445 return;
Markus Armbruster666daa62010-06-02 18:48:27 +02001446 }
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001447
Markus Armbruster528f7662010-06-25 15:26:48 +02001448 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
Blue Swirl199630b2010-07-25 20:49:34 +00001449 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001450
1451 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
Markus Armbruster666daa62010-06-02 18:48:27 +02001452}
Ryan Harper9063f812010-11-12 11:07:13 -06001453
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001454/* throttling disk I/O limits */
Luiz Capitulino80047da2011-12-14 16:49:14 -02001455void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
Benoît Canet3e9fab62013-09-02 14:14:40 +02001456 int64_t bps_wr,
1457 int64_t iops,
1458 int64_t iops_rd,
1459 int64_t iops_wr,
1460 bool has_bps_max,
1461 int64_t bps_max,
1462 bool has_bps_rd_max,
1463 int64_t bps_rd_max,
1464 bool has_bps_wr_max,
1465 int64_t bps_wr_max,
1466 bool has_iops_max,
1467 int64_t iops_max,
1468 bool has_iops_rd_max,
1469 int64_t iops_rd_max,
1470 bool has_iops_wr_max,
Benoît Canet2024c1d2013-09-02 14:14:41 +02001471 int64_t iops_wr_max,
1472 bool has_iops_size,
1473 int64_t iops_size, Error **errp)
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001474{
Benoît Canetcc0681c2013-09-02 14:14:39 +02001475 ThrottleConfig cfg;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001476 BlockDriverState *bs;
1477
Luiz Capitulino80047da2011-12-14 16:49:14 -02001478 bs = bdrv_find(device);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001479 if (!bs) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02001480 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1481 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001482 }
1483
Benoît Canetcc0681c2013-09-02 14:14:39 +02001484 memset(&cfg, 0, sizeof(cfg));
1485 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1486 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1487 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001488
Benoît Canetcc0681c2013-09-02 14:14:39 +02001489 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1490 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1491 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1492
Benoît Canet3e9fab62013-09-02 14:14:40 +02001493 if (has_bps_max) {
1494 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1495 }
1496 if (has_bps_rd_max) {
1497 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1498 }
1499 if (has_bps_wr_max) {
1500 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
1501 }
1502 if (has_iops_max) {
1503 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
1504 }
1505 if (has_iops_rd_max) {
1506 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
1507 }
1508 if (has_iops_wr_max) {
1509 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
1510 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02001511
Benoît Canet2024c1d2013-09-02 14:14:41 +02001512 if (has_iops_size) {
1513 cfg.op_size = iops_size;
1514 }
Benoît Canetcc0681c2013-09-02 14:14:39 +02001515
1516 if (!check_throttle_config(&cfg, errp)) {
Luiz Capitulino80047da2011-12-14 16:49:14 -02001517 return;
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001518 }
1519
Benoît Canetcc0681c2013-09-02 14:14:39 +02001520 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001521 bdrv_io_limits_enable(bs);
Benoît Canetcc0681c2013-09-02 14:14:39 +02001522 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001523 bdrv_io_limits_disable(bs);
Benoît Canetcc0681c2013-09-02 14:14:39 +02001524 }
1525
1526 if (bs->io_limits_enabled) {
1527 bdrv_set_io_limits(bs, &cfg);
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001528 }
Zhi Yong Wu727f0052011-11-08 13:00:31 +08001529}
1530
Ryan Harper9063f812010-11-12 11:07:13 -06001531int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1532{
1533 const char *id = qdict_get_str(qdict, "id");
1534 BlockDriverState *bs;
Ryan Harper9063f812010-11-12 11:07:13 -06001535
1536 bs = bdrv_find(id);
1537 if (!bs) {
1538 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1539 return -1;
1540 }
Marcelo Tosatti85916752011-01-26 12:12:35 -02001541 if (bdrv_in_use(bs)) {
1542 qerror_report(QERR_DEVICE_IN_USE, id);
1543 return -1;
1544 }
Ryan Harper9063f812010-11-12 11:07:13 -06001545
1546 /* quiesce block driver; prevent further io */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00001547 bdrv_drain_all();
Ryan Harper9063f812010-11-12 11:07:13 -06001548 bdrv_flush(bs);
1549 bdrv_close(bs);
1550
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001551 /* if we have a device attached to this BlockDriverState
Ryan Harperd22b2f42011-03-29 20:51:47 -05001552 * then we need to make the drive anonymous until the device
1553 * can be removed. If this is a drive with no device backing
1554 * then we can just get rid of the block driver state right here.
1555 */
Markus Armbrusterfa879d62011-08-03 15:07:40 +02001556 if (bdrv_get_attached_dev(bs)) {
Ryan Harperd22b2f42011-03-29 20:51:47 -05001557 bdrv_make_anon(bs);
Stefan Hajnoczi293c51a2013-06-05 10:33:14 +02001558
1559 /* Further I/O must not pause the guest */
1560 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1561 BLOCKDEV_ON_ERROR_REPORT);
Ryan Harperd22b2f42011-03-29 20:51:47 -05001562 } else {
1563 drive_uninit(drive_get_by_blockdev(bs));
Ryan Harper9063f812010-11-12 11:07:13 -06001564 }
1565
Ryan Harper9063f812010-11-12 11:07:13 -06001566 return 0;
1567}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001568
Luiz Capitulino5e7caac2011-11-25 14:57:10 -02001569void qmp_block_resize(const char *device, int64_t size, Error **errp)
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001570{
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001571 BlockDriverState *bs;
Kevin Wolf87329012013-05-02 15:32:55 +02001572 int ret;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001573
1574 bs = bdrv_find(device);
1575 if (!bs) {
Luiz Capitulino5e7caac2011-11-25 14:57:10 -02001576 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1577 return;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001578 }
1579
1580 if (size < 0) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00001581 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
Luiz Capitulino5e7caac2011-11-25 14:57:10 -02001582 return;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001583 }
1584
Peter Lieven92b7a082013-03-11 11:04:24 +01001585 /* complete all in-flight operations before resizing the device */
1586 bdrv_drain_all();
1587
Kevin Wolf87329012013-05-02 15:32:55 +02001588 ret = bdrv_truncate(bs, size);
1589 switch (ret) {
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00001590 case 0:
1591 break;
1592 case -ENOMEDIUM:
1593 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1594 break;
1595 case -ENOTSUP:
1596 error_set(errp, QERR_UNSUPPORTED);
1597 break;
1598 case -EACCES:
1599 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1600 break;
1601 case -EBUSY:
1602 error_set(errp, QERR_DEVICE_IN_USE, device);
1603 break;
1604 default:
Kevin Wolf87329012013-05-02 15:32:55 +02001605 error_setg_errno(errp, -ret, "Could not resize");
Stefan Hajnoczi939a1cc2012-01-04 22:23:34 +00001606 break;
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001607 }
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +01001608}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001609
Jeff Cody9abf2db2012-09-27 13:29:14 -04001610static void block_job_cb(void *opaque, int ret)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001611{
1612 BlockDriverState *bs = opaque;
1613 QObject *obj;
1614
Jeff Cody9abf2db2012-09-27 13:29:14 -04001615 trace_block_job_cb(bs, bs->job, ret);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001616
1617 assert(bs->job);
1618 obj = qobject_from_block_job(bs->job);
1619 if (ret < 0) {
1620 QDict *dict = qobject_to_qdict(obj);
1621 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1622 }
1623
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001624 if (block_job_is_cancelled(bs->job)) {
1625 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1626 } else {
1627 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1628 }
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001629 qobject_decref(obj);
Stefan Hajnocziaa398a52012-01-18 14:40:50 +00001630
Fam Zhengfa510eb2013-08-23 09:14:51 +08001631 bdrv_put_ref_bh_schedule(bs);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001632}
1633
1634void qmp_block_stream(const char *device, bool has_base,
Paolo Bonzini1d809092012-09-28 17:22:59 +02001635 const char *base, bool has_speed, int64_t speed,
1636 bool has_on_error, BlockdevOnError on_error,
1637 Error **errp)
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001638{
1639 BlockDriverState *bs;
Marcelo Tosattic8c30802012-01-18 14:40:53 +00001640 BlockDriverState *base_bs = NULL;
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01001641 Error *local_err = NULL;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001642
Paolo Bonzini1d809092012-09-28 17:22:59 +02001643 if (!has_on_error) {
1644 on_error = BLOCKDEV_ON_ERROR_REPORT;
1645 }
1646
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001647 bs = bdrv_find(device);
1648 if (!bs) {
1649 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1650 return;
1651 }
1652
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001653 if (base) {
Marcelo Tosattic8c30802012-01-18 14:40:53 +00001654 base_bs = bdrv_find_backing_image(bs, base);
1655 if (base_bs == NULL) {
1656 error_set(errp, QERR_BASE_NOT_FOUND, base);
1657 return;
1658 }
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001659 }
1660
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +01001661 stream_start(bs, base_bs, base, has_speed ? speed : 0,
Paolo Bonzini1d809092012-09-28 17:22:59 +02001662 on_error, block_job_cb, bs, &local_err);
Stefan Hajnoczifd7f8c62012-04-25 16:51:00 +01001663 if (error_is_set(&local_err)) {
1664 error_propagate(errp, local_err);
1665 return;
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001666 }
1667
1668 trace_qmp_block_stream(bs, bs->job);
1669}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001670
Jeff Codyed61fc12012-09-27 13:29:16 -04001671void qmp_block_commit(const char *device,
1672 bool has_base, const char *base, const char *top,
1673 bool has_speed, int64_t speed,
1674 Error **errp)
1675{
1676 BlockDriverState *bs;
1677 BlockDriverState *base_bs, *top_bs;
1678 Error *local_err = NULL;
1679 /* This will be part of the QMP command, if/when the
1680 * BlockdevOnError change for blkmirror makes it in
1681 */
Paolo Bonzini92aa5c62012-09-28 17:22:55 +02001682 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
Jeff Codyed61fc12012-09-27 13:29:16 -04001683
1684 /* drain all i/o before commits */
1685 bdrv_drain_all();
1686
1687 bs = bdrv_find(device);
1688 if (!bs) {
1689 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1690 return;
1691 }
Jeff Codyed61fc12012-09-27 13:29:16 -04001692
1693 /* default top_bs is the active layer */
1694 top_bs = bs;
1695
1696 if (top) {
1697 if (strcmp(bs->filename, top) != 0) {
1698 top_bs = bdrv_find_backing_image(bs, top);
1699 }
1700 }
1701
1702 if (top_bs == NULL) {
1703 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1704 return;
1705 }
1706
Jeff Codyd5208c42012-10-16 15:49:10 -04001707 if (has_base && base) {
1708 base_bs = bdrv_find_backing_image(top_bs, base);
1709 } else {
1710 base_bs = bdrv_find_base(top_bs);
1711 }
1712
1713 if (base_bs == NULL) {
1714 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1715 return;
1716 }
1717
Jeff Codyed61fc12012-09-27 13:29:16 -04001718 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1719 &local_err);
1720 if (local_err != NULL) {
1721 error_propagate(errp, local_err);
1722 return;
1723 }
Jeff Codyed61fc12012-09-27 13:29:16 -04001724}
1725
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001726void qmp_drive_backup(const char *device, const char *target,
1727 bool has_format, const char *format,
Stefan Hajnoczib53169e2013-06-26 14:11:57 +02001728 enum MirrorSyncMode sync,
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001729 bool has_mode, enum NewImageMode mode,
1730 bool has_speed, int64_t speed,
1731 bool has_on_source_error, BlockdevOnError on_source_error,
1732 bool has_on_target_error, BlockdevOnError on_target_error,
1733 Error **errp)
1734{
1735 BlockDriverState *bs;
1736 BlockDriverState *target_bs;
Ian Mainfc5d3f82013-07-26 11:39:04 -07001737 BlockDriverState *source = NULL;
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001738 BlockDriver *drv = NULL;
1739 Error *local_err = NULL;
1740 int flags;
1741 int64_t size;
1742 int ret;
1743
1744 if (!has_speed) {
1745 speed = 0;
1746 }
1747 if (!has_on_source_error) {
1748 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1749 }
1750 if (!has_on_target_error) {
1751 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1752 }
1753 if (!has_mode) {
1754 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1755 }
1756
1757 bs = bdrv_find(device);
1758 if (!bs) {
1759 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1760 return;
1761 }
1762
1763 if (!bdrv_is_inserted(bs)) {
1764 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1765 return;
1766 }
1767
1768 if (!has_format) {
1769 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1770 }
1771 if (format) {
1772 drv = bdrv_find_format(format);
1773 if (!drv) {
1774 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1775 return;
1776 }
1777 }
1778
1779 if (bdrv_in_use(bs)) {
1780 error_set(errp, QERR_DEVICE_IN_USE, device);
1781 return;
1782 }
1783
1784 flags = bs->open_flags | BDRV_O_RDWR;
1785
Ian Mainfc5d3f82013-07-26 11:39:04 -07001786 /* See if we have a backing HD we can use to create our new image
1787 * on top of. */
1788 if (sync == MIRROR_SYNC_MODE_TOP) {
1789 source = bs->backing_hd;
1790 if (!source) {
1791 sync = MIRROR_SYNC_MODE_FULL;
1792 }
1793 }
1794 if (sync == MIRROR_SYNC_MODE_NONE) {
1795 source = bs;
1796 }
1797
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001798 size = bdrv_getlength(bs);
1799 if (size < 0) {
1800 error_setg_errno(errp, -size, "bdrv_getlength failed");
1801 return;
1802 }
1803
1804 if (mode != NEW_IMAGE_MODE_EXISTING) {
1805 assert(format && drv);
Ian Mainfc5d3f82013-07-26 11:39:04 -07001806 if (source) {
1807 bdrv_img_create(target, format, source->filename,
1808 source->drv->format_name, NULL,
1809 size, flags, &local_err, false);
1810 } else {
1811 bdrv_img_create(target, format, NULL, NULL, NULL,
1812 size, flags, &local_err, false);
1813 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001814 }
1815
1816 if (error_is_set(&local_err)) {
1817 error_propagate(errp, local_err);
1818 return;
1819 }
1820
1821 target_bs = bdrv_new("");
Max Reitz34b5d2c2013-09-05 14:45:29 +02001822 ret = bdrv_open(target_bs, target, NULL, flags, drv, &local_err);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001823 if (ret < 0) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001824 bdrv_unref(target_bs);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001825 error_propagate(errp, local_err);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001826 return;
1827 }
1828
Ian Mainfc5d3f82013-07-26 11:39:04 -07001829 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001830 block_job_cb, bs, &local_err);
1831 if (local_err != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001832 bdrv_unref(target_bs);
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001833 error_propagate(errp, local_err);
1834 return;
1835 }
Stefan Hajnoczi99a9add2013-06-24 17:13:14 +02001836}
1837
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01001838#define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1839
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001840void qmp_drive_mirror(const char *device, const char *target,
1841 bool has_format, const char *format,
1842 enum MirrorSyncMode sync,
1843 bool has_mode, enum NewImageMode mode,
Paolo Bonzinib952b552012-10-18 16:49:28 +02001844 bool has_speed, int64_t speed,
Paolo Bonzinieee13df2013-01-21 17:09:46 +01001845 bool has_granularity, uint32_t granularity,
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01001846 bool has_buf_size, int64_t buf_size,
Paolo Bonzinib952b552012-10-18 16:49:28 +02001847 bool has_on_source_error, BlockdevOnError on_source_error,
1848 bool has_on_target_error, BlockdevOnError on_target_error,
1849 Error **errp)
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001850{
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001851 BlockDriverState *bs;
1852 BlockDriverState *source, *target_bs;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001853 BlockDriver *drv = NULL;
1854 Error *local_err = NULL;
1855 int flags;
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02001856 int64_t size;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001857 int ret;
1858
1859 if (!has_speed) {
1860 speed = 0;
1861 }
Paolo Bonzinib952b552012-10-18 16:49:28 +02001862 if (!has_on_source_error) {
1863 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1864 }
1865 if (!has_on_target_error) {
1866 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1867 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001868 if (!has_mode) {
1869 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1870 }
Paolo Bonzinieee13df2013-01-21 17:09:46 +01001871 if (!has_granularity) {
1872 granularity = 0;
1873 }
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01001874 if (!has_buf_size) {
1875 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1876 }
1877
Paolo Bonzinieee13df2013-01-21 17:09:46 +01001878 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1879 error_set(errp, QERR_INVALID_PARAMETER, device);
1880 return;
1881 }
1882 if (granularity & (granularity - 1)) {
1883 error_set(errp, QERR_INVALID_PARAMETER, device);
1884 return;
1885 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001886
1887 bs = bdrv_find(device);
1888 if (!bs) {
1889 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1890 return;
1891 }
1892
1893 if (!bdrv_is_inserted(bs)) {
1894 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1895 return;
1896 }
1897
1898 if (!has_format) {
1899 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1900 }
1901 if (format) {
1902 drv = bdrv_find_format(format);
1903 if (!drv) {
1904 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1905 return;
1906 }
1907 }
1908
1909 if (bdrv_in_use(bs)) {
1910 error_set(errp, QERR_DEVICE_IN_USE, device);
1911 return;
1912 }
1913
1914 flags = bs->open_flags | BDRV_O_RDWR;
1915 source = bs->backing_hd;
1916 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1917 sync = MIRROR_SYNC_MODE_FULL;
1918 }
1919
Stefan Hajnocziac3c5d82013-06-24 17:13:13 +02001920 size = bdrv_getlength(bs);
1921 if (size < 0) {
1922 error_setg_errno(errp, -size, "bdrv_getlength failed");
1923 return;
1924 }
1925
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001926 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1927 /* create new image w/o backing file */
1928 assert(format && drv);
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02001929 bdrv_img_create(target, format,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001930 NULL, NULL, NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001931 } else {
1932 switch (mode) {
1933 case NEW_IMAGE_MODE_EXISTING:
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001934 break;
1935 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1936 /* create new image with backing file */
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02001937 bdrv_img_create(target, format,
1938 source->filename,
1939 source->drv->format_name,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001940 NULL, size, flags, &local_err, false);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001941 break;
1942 default:
1943 abort();
1944 }
1945 }
1946
Luiz Capitulinocf8f2422012-11-30 10:52:08 -02001947 if (error_is_set(&local_err)) {
1948 error_propagate(errp, local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001949 return;
1950 }
1951
Paolo Bonzinib812f672013-01-21 17:09:43 +01001952 /* Mirroring takes care of copy-on-write using the source's backing
1953 * file.
1954 */
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001955 target_bs = bdrv_new("");
Max Reitz34b5d2c2013-09-05 14:45:29 +02001956 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv,
1957 &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001958 if (ret < 0) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001959 bdrv_unref(target_bs);
Max Reitz34b5d2c2013-09-05 14:45:29 +02001960 error_propagate(errp, local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001961 return;
1962 }
1963
Paolo Bonzini08e4ed62013-01-22 09:03:13 +01001964 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
Paolo Bonzinieee13df2013-01-21 17:09:46 +01001965 on_source_error, on_target_error,
Paolo Bonzinib952b552012-10-18 16:49:28 +02001966 block_job_cb, bs, &local_err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001967 if (local_err != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001968 bdrv_unref(target_bs);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001969 error_propagate(errp, local_err);
1970 return;
1971 }
Paolo Bonzinid9b902d2012-10-18 16:49:24 +02001972}
1973
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001974static BlockJob *find_block_job(const char *device)
1975{
1976 BlockDriverState *bs;
1977
1978 bs = bdrv_find(device);
1979 if (!bs || !bs->job) {
1980 return NULL;
1981 }
1982 return bs->job;
1983}
1984
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01001985void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001986{
1987 BlockJob *job = find_block_job(device);
1988
1989 if (!job) {
Paolo Bonzini7ef15072012-09-28 17:22:44 +02001990 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001991 return;
1992 }
1993
Stefan Hajnoczi882ec7c2012-04-25 16:51:02 +01001994 block_job_set_speed(job, speed, errp);
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001995}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001996
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02001997void qmp_block_job_cancel(const char *device,
1998 bool has_force, bool force, Error **errp)
1999{
2000 BlockJob *job = find_block_job(device);
2001
2002 if (!has_force) {
2003 force = false;
2004 }
2005
2006 if (!job) {
2007 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2008 return;
2009 }
2010 if (job->paused && !force) {
2011 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
2012 return;
2013 }
2014
2015 trace_qmp_block_job_cancel(job);
2016 block_job_cancel(job);
2017}
2018
2019void qmp_block_job_pause(const char *device, Error **errp)
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002020{
2021 BlockJob *job = find_block_job(device);
2022
2023 if (!job) {
Paolo Bonzini7ef15072012-09-28 17:22:44 +02002024 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002025 return;
2026 }
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02002027
2028 trace_qmp_block_job_pause(job);
2029 block_job_pause(job);
2030}
2031
2032void qmp_block_job_resume(const char *device, Error **errp)
2033{
2034 BlockJob *job = find_block_job(device);
2035
2036 if (!job) {
2037 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
Paolo Bonzini8acc72a2012-09-28 17:22:50 +02002038 return;
2039 }
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002040
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02002041 trace_qmp_block_job_resume(job);
2042 block_job_resume(job);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00002043}
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002044
Paolo Bonziniaeae8832012-10-18 16:49:21 +02002045void qmp_block_job_complete(const char *device, Error **errp)
2046{
2047 BlockJob *job = find_block_job(device);
2048
2049 if (!job) {
2050 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2051 return;
2052 }
2053
2054 trace_qmp_block_job_complete(job);
2055 block_job_complete(job, errp);
2056}
2057
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002058static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
2059{
2060 BlockJobInfoList **prev = opaque;
2061 BlockJob *job = bs->job;
2062
2063 if (job) {
Paolo Bonzini30e628b2012-09-28 17:22:48 +02002064 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2065 elem->value = block_job_query(bs->job);
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002066 (*prev)->next = elem;
2067 *prev = elem;
2068 }
2069}
2070
2071BlockJobInfoList *qmp_query_block_jobs(Error **errp)
2072{
2073 /* Dummy is a fake list element for holding the head pointer */
2074 BlockJobInfoList dummy = {};
2075 BlockJobInfoList *prev = &dummy;
2076 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
2077 return dummy.next;
2078}
Paolo Bonzini4d454572012-11-26 16:03:42 +01002079
Kevin Wolf00063832013-03-15 10:35:07 +01002080QemuOptsList qemu_common_drive_opts = {
Paolo Bonzini4d454572012-11-26 16:03:42 +01002081 .name = "drive",
Kevin Wolf00063832013-03-15 10:35:07 +01002082 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
Paolo Bonzini4d454572012-11-26 16:03:42 +01002083 .desc = {
2084 {
2085 .name = "bus",
2086 .type = QEMU_OPT_NUMBER,
2087 .help = "bus number",
2088 },{
2089 .name = "unit",
2090 .type = QEMU_OPT_NUMBER,
2091 .help = "unit number (i.e. lun for scsi)",
2092 },{
2093 .name = "if",
2094 .type = QEMU_OPT_STRING,
2095 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
2096 },{
2097 .name = "index",
2098 .type = QEMU_OPT_NUMBER,
2099 .help = "index number",
2100 },{
2101 .name = "cyls",
2102 .type = QEMU_OPT_NUMBER,
2103 .help = "number of cylinders (ide disk geometry)",
2104 },{
2105 .name = "heads",
2106 .type = QEMU_OPT_NUMBER,
2107 .help = "number of heads (ide disk geometry)",
2108 },{
2109 .name = "secs",
2110 .type = QEMU_OPT_NUMBER,
2111 .help = "number of sectors (ide disk geometry)",
2112 },{
2113 .name = "trans",
2114 .type = QEMU_OPT_STRING,
2115 .help = "chs translation (auto, lba. none)",
2116 },{
2117 .name = "media",
2118 .type = QEMU_OPT_STRING,
2119 .help = "media type (disk, cdrom)",
2120 },{
2121 .name = "snapshot",
2122 .type = QEMU_OPT_BOOL,
2123 .help = "enable/disable snapshot mode",
2124 },{
2125 .name = "file",
2126 .type = QEMU_OPT_STRING,
2127 .help = "disk image",
2128 },{
Paolo Bonzinia9384af2013-02-08 14:06:12 +01002129 .name = "discard",
2130 .type = QEMU_OPT_STRING,
2131 .help = "discard operation (ignore/off, unmap/on)",
2132 },{
Kevin Wolf29c4e2b2013-07-18 16:31:25 +02002133 .name = "cache.writeback",
2134 .type = QEMU_OPT_BOOL,
2135 .help = "enables writeback mode for any caches",
2136 },{
2137 .name = "cache.direct",
2138 .type = QEMU_OPT_BOOL,
2139 .help = "enables use of O_DIRECT (bypass the host page cache)",
2140 },{
2141 .name = "cache.no-flush",
2142 .type = QEMU_OPT_BOOL,
2143 .help = "ignore any flush requests for the device",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002144 },{
2145 .name = "aio",
2146 .type = QEMU_OPT_STRING,
2147 .help = "host AIO implementation (threads, native)",
2148 },{
2149 .name = "format",
2150 .type = QEMU_OPT_STRING,
2151 .help = "disk format (raw, qcow2, ...)",
2152 },{
2153 .name = "serial",
2154 .type = QEMU_OPT_STRING,
2155 .help = "disk serial number",
2156 },{
2157 .name = "rerror",
2158 .type = QEMU_OPT_STRING,
2159 .help = "read error action",
2160 },{
2161 .name = "werror",
2162 .type = QEMU_OPT_STRING,
2163 .help = "write error action",
2164 },{
2165 .name = "addr",
2166 .type = QEMU_OPT_STRING,
2167 .help = "pci address (virtio only)",
2168 },{
Kevin Wolf0f227a92013-07-19 20:07:29 +02002169 .name = "read-only",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002170 .type = QEMU_OPT_BOOL,
2171 .help = "open drive file as read-only",
2172 },{
Kevin Wolf57975222013-07-17 14:41:54 +02002173 .name = "throttling.iops-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002174 .type = QEMU_OPT_NUMBER,
2175 .help = "limit total I/O operations per second",
2176 },{
Kevin Wolf57975222013-07-17 14:41:54 +02002177 .name = "throttling.iops-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002178 .type = QEMU_OPT_NUMBER,
2179 .help = "limit read operations per second",
2180 },{
Kevin Wolf57975222013-07-17 14:41:54 +02002181 .name = "throttling.iops-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002182 .type = QEMU_OPT_NUMBER,
2183 .help = "limit write operations per second",
2184 },{
Kevin Wolf57975222013-07-17 14:41:54 +02002185 .name = "throttling.bps-total",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002186 .type = QEMU_OPT_NUMBER,
2187 .help = "limit total bytes per second",
2188 },{
Kevin Wolf57975222013-07-17 14:41:54 +02002189 .name = "throttling.bps-read",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002190 .type = QEMU_OPT_NUMBER,
2191 .help = "limit read bytes per second",
2192 },{
Kevin Wolf57975222013-07-17 14:41:54 +02002193 .name = "throttling.bps-write",
Paolo Bonzini4d454572012-11-26 16:03:42 +01002194 .type = QEMU_OPT_NUMBER,
2195 .help = "limit write bytes per second",
2196 },{
Benoît Canet3e9fab62013-09-02 14:14:40 +02002197 .name = "throttling.iops-total-max",
2198 .type = QEMU_OPT_NUMBER,
2199 .help = "I/O operations burst",
2200 },{
2201 .name = "throttling.iops-read-max",
2202 .type = QEMU_OPT_NUMBER,
2203 .help = "I/O operations read burst",
2204 },{
2205 .name = "throttling.iops-write-max",
2206 .type = QEMU_OPT_NUMBER,
2207 .help = "I/O operations write burst",
2208 },{
2209 .name = "throttling.bps-total-max",
2210 .type = QEMU_OPT_NUMBER,
2211 .help = "total bytes burst",
2212 },{
2213 .name = "throttling.bps-read-max",
2214 .type = QEMU_OPT_NUMBER,
2215 .help = "total bytes read burst",
2216 },{
2217 .name = "throttling.bps-write-max",
2218 .type = QEMU_OPT_NUMBER,
2219 .help = "total bytes write burst",
2220 },{
Benoît Canet2024c1d2013-09-02 14:14:41 +02002221 .name = "throttling.iops-size",
2222 .type = QEMU_OPT_NUMBER,
2223 .help = "when limiting by iops max size of an I/O in bytes",
2224 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +01002225 .name = "copy-on-read",
2226 .type = QEMU_OPT_BOOL,
2227 .help = "copy read data from backing file into image file",
2228 },{
2229 .name = "boot",
2230 .type = QEMU_OPT_BOOL,
2231 .help = "(deprecated, ignored)",
2232 },
2233 { /* end of list */ }
2234 },
2235};
Kevin Wolf00063832013-03-15 10:35:07 +01002236
2237QemuOptsList qemu_drive_opts = {
2238 .name = "drive",
2239 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
2240 .desc = {
Kevin Wolf492fdc62013-06-19 13:44:17 +02002241 /*
2242 * no elements => accept any params
2243 * validation will happen later
2244 */
Kevin Wolf00063832013-03-15 10:35:07 +01002245 { /* end of list */ }
2246 },
2247};