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