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