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