]> git.ipfire.org Git - thirdparty/qemu.git/blob - blockdev.c
block: New BlockBackend
[thirdparty/qemu.git] / blockdev.c
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.
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.
31 */
32
33 #include "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "monitor/monitor.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qapi/qmp/types.h"
41 #include "qapi-visit.h"
42 #include "qapi/qmp-output-visitor.h"
43 #include "qapi/util.h"
44 #include "sysemu/sysemu.h"
45 #include "block/block_int.h"
46 #include "qmp-commands.h"
47 #include "trace.h"
48 #include "sysemu/arch_init.h"
49
50 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
51
52 static 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
64 static int if_max_devs[IF_COUNT] = {
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,
81 };
82
83 /**
84 * Boards may call this to offer board-by-board overrides
85 * of the default, global values.
86 */
87 void 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
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 */
114 void blockdev_mark_auto_del(BlockDriverState *bs)
115 {
116 DriveInfo *dinfo = drive_get_by_blockdev(bs);
117
118 if (dinfo && !dinfo->enable_auto_del) {
119 return;
120 }
121
122 if (bs->job) {
123 block_job_cancel(bs->job);
124 }
125 if (dinfo) {
126 dinfo->auto_del = 1;
127 }
128 }
129
130 void blockdev_auto_del(BlockDriverState *bs)
131 {
132 DriveInfo *dinfo = drive_get_by_blockdev(bs);
133
134 if (dinfo && dinfo->auto_del) {
135 drive_del(dinfo);
136 }
137 }
138
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 */
147 int 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
156 static 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
162 static 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
168 QemuOpts *drive_def(const char *optstr)
169 {
170 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
171 }
172
173 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
174 const char *optstr)
175 {
176 QemuOpts *opts;
177 char buf[32];
178
179 opts = drive_def(optstr);
180 if (!opts) {
181 return NULL;
182 }
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 }
190 if (file)
191 qemu_opt_set(opts, "file", file);
192 return opts;
193 }
194
195 DriveInfo *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
211 bool 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
232 DriveInfo *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
239 int 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
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. */
256 DriveInfo *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
263 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
264 {
265 DriveInfo *dinfo;
266
267 QTAILQ_FOREACH(dinfo, &drives, next) {
268 if (dinfo->bdrv == bs) {
269 return dinfo;
270 }
271 }
272 return NULL;
273 }
274
275 static void bdrv_format_print(void *opaque, const char *name)
276 {
277 error_printf(" %s", name);
278 }
279
280 void drive_del(DriveInfo *dinfo)
281 {
282 BlockBackend *blk = blk_by_name(dinfo->id);
283
284 bdrv_unref(dinfo->bdrv);
285 blk_unref(blk);
286 }
287
288 void drive_info_del(DriveInfo *dinfo)
289 {
290 if (!dinfo) {
291 return;
292 }
293 qemu_opts_del(dinfo->opts);
294 g_free(dinfo->id);
295 QTAILQ_REMOVE(&drives, dinfo, next);
296 g_free(dinfo->serial);
297 g_free(dinfo);
298 }
299
300 typedef struct {
301 QEMUBH *bh;
302 BlockDriverState *bs;
303 } BDRVPutRefBH;
304
305 static void bdrv_put_ref_bh(void *opaque)
306 {
307 BDRVPutRefBH *s = opaque;
308
309 bdrv_unref(s->bs);
310 qemu_bh_delete(s->bh);
311 g_free(s);
312 }
313
314 /*
315 * Release a BDS reference in a BH
316 *
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.
320 */
321 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
322 {
323 BDRVPutRefBH *s;
324
325 s = g_new(BDRVPutRefBH, 1);
326 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
327 s->bs = bs;
328 qemu_bh_schedule(s->bh);
329 }
330
331 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
332 {
333 if (!strcmp(buf, "ignore")) {
334 return BLOCKDEV_ON_ERROR_IGNORE;
335 } else if (!is_read && !strcmp(buf, "enospc")) {
336 return BLOCKDEV_ON_ERROR_ENOSPC;
337 } else if (!strcmp(buf, "stop")) {
338 return BLOCKDEV_ON_ERROR_STOP;
339 } else if (!strcmp(buf, "report")) {
340 return BLOCKDEV_ON_ERROR_REPORT;
341 } else {
342 error_setg(errp, "'%s' invalid %s error action",
343 buf, is_read ? "read" : "write");
344 return -1;
345 }
346 }
347
348 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
349 {
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");
353 return false;
354 }
355
356 if (!throttle_is_valid(cfg)) {
357 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
358 return false;
359 }
360
361 return true;
362 }
363
364 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
365
366 /* Takes the ownership of bs_opts */
367 static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
368 Error **errp)
369 {
370 const char *buf;
371 int ro = 0;
372 int bdrv_flags = 0;
373 int on_read_error, on_write_error;
374 BlockBackend *blk;
375 BlockDriverState *bs;
376 DriveInfo *dinfo;
377 ThrottleConfig cfg;
378 int snapshot = 0;
379 bool copy_on_read;
380 int ret;
381 Error *error = NULL;
382 QemuOpts *opts;
383 const char *id;
384 bool has_driver_specific_opts;
385 BlockdevDetectZeroesOptions detect_zeroes;
386 BlockDriver *drv = NULL;
387
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");
391 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
392 if (error) {
393 error_propagate(errp, error);
394 goto err_no_opts;
395 }
396
397 qemu_opts_absorb_qdict(opts, bs_opts, &error);
398 if (error) {
399 error_propagate(errp, error);
400 goto early_err;
401 }
402
403 if (id) {
404 qdict_del(bs_opts, "id");
405 }
406
407 has_driver_specific_opts = !!qdict_size(bs_opts);
408
409 /* extract parameters */
410 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
411 ro = qemu_opt_get_bool(opts, "read-only", 0);
412 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
413
414 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
415 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
416 error_setg(errp, "invalid discard option");
417 goto early_err;
418 }
419 }
420
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 }
427 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
428 bdrv_flags |= BDRV_O_NO_FLUSH;
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 {
438 error_setg(errp, "invalid aio option");
439 goto early_err;
440 }
441 }
442 #endif
443
444 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
445 if (is_help_option(buf)) {
446 error_printf("Supported formats:");
447 bdrv_iterate_format(bdrv_format_print, NULL);
448 error_printf("\n");
449 goto early_err;
450 }
451
452 drv = bdrv_find_format(buf);
453 if (!drv) {
454 error_setg(errp, "'%s' invalid format", buf);
455 goto early_err;
456 }
457 }
458
459 /* disk I/O throttling */
460 memset(&cfg, 0, sizeof(cfg));
461 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
462 qemu_opt_get_number(opts, "throttling.bps-total", 0);
463 cfg.buckets[THROTTLE_BPS_READ].avg =
464 qemu_opt_get_number(opts, "throttling.bps-read", 0);
465 cfg.buckets[THROTTLE_BPS_WRITE].avg =
466 qemu_opt_get_number(opts, "throttling.bps-write", 0);
467 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
468 qemu_opt_get_number(opts, "throttling.iops-total", 0);
469 cfg.buckets[THROTTLE_OPS_READ].avg =
470 qemu_opt_get_number(opts, "throttling.iops-read", 0);
471 cfg.buckets[THROTTLE_OPS_WRITE].avg =
472 qemu_opt_get_number(opts, "throttling.iops-write", 0);
473
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);
486
487 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
488
489 if (!check_throttle_config(&cfg, &error)) {
490 error_propagate(errp, error);
491 goto early_err;
492 }
493
494 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
495 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
496 on_write_error = parse_block_error_action(buf, 0, &error);
497 if (error) {
498 error_propagate(errp, error);
499 goto early_err;
500 }
501 }
502
503 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
504 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
505 on_read_error = parse_block_error_action(buf, 1, &error);
506 if (error) {
507 error_propagate(errp, error);
508 goto early_err;
509 }
510 }
511
512 detect_zeroes =
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);
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
530 /* init */
531 blk = blk_new(qemu_opts_id(opts), errp);
532 if (!blk) {
533 goto early_err;
534 }
535 bs = bdrv_new_root(qemu_opts_id(opts), errp);
536 if (!bs) {
537 goto bdrv_new_err;
538 }
539 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
540 bs->read_only = ro;
541 bs->detect_zeroes = detect_zeroes;
542
543 bdrv_set_on_error(bs, on_read_error, on_write_error);
544
545 /* disk I/O throttling */
546 if (throttle_enabled(&cfg)) {
547 bdrv_io_limits_enable(bs);
548 bdrv_set_io_limits(bs, &cfg);
549 }
550
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
556 if (!file || !*file) {
557 if (has_driver_specific_opts) {
558 file = NULL;
559 } else {
560 QDECREF(bs_opts);
561 qemu_opts_del(opts);
562 return dinfo;
563 }
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
571 if (copy_on_read) {
572 bdrv_flags |= BDRV_O_COPY_ON_READ;
573 }
574
575 if (runstate_check(RUN_STATE_INMIGRATE)) {
576 bdrv_flags |= BDRV_O_INCOMING;
577 }
578
579 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
580
581 QINCREF(bs_opts);
582 ret = bdrv_open(&bs, file, NULL, bs_opts, bdrv_flags, drv, &error);
583 assert(bs == dinfo->bdrv);
584
585 if (ret < 0) {
586 error_setg(errp, "could not open disk image %s: %s",
587 file ?: dinfo->id, error_get_pretty(error));
588 error_free(error);
589 goto err;
590 }
591
592 if (bdrv_key_required(bs)) {
593 autostart = 0;
594 }
595
596 QDECREF(bs_opts);
597 qemu_opts_del(opts);
598
599 return dinfo;
600
601 err:
602 bdrv_unref(bs);
603 bdrv_new_err:
604 blk_unref(blk);
605 early_err:
606 qemu_opts_del(opts);
607 err_no_opts:
608 QDECREF(bs_opts);
609 return NULL;
610 }
611
612 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
613 Error **errp)
614 {
615 const char *value;
616
617 value = qemu_opt_get(opts, from);
618 if (value) {
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 }
624 }
625
626 /* rename all items in opts */
627 while ((value = qemu_opt_get(opts, from))) {
628 qemu_opt_set(opts, to, value);
629 qemu_opt_unset(opts, from);
630 }
631 }
632
633 QemuOptsList qemu_legacy_drive_opts = {
634 .name = "drive",
635 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
636 .desc = {
637 {
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 },{
650 .name = "media",
651 .type = QEMU_OPT_STRING,
652 .help = "media type (disk, cdrom)",
653 },{
654 .name = "if",
655 .type = QEMU_OPT_STRING,
656 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
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)",
673 },{
674 .name = "boot",
675 .type = QEMU_OPT_BOOL,
676 .help = "(deprecated, ignored)",
677 },{
678 .name = "addr",
679 .type = QEMU_OPT_STRING,
680 .help = "pci address (virtio only)",
681 },{
682 .name = "serial",
683 .type = QEMU_OPT_STRING,
684 .help = "disk serial number",
685 },{
686 .name = "file",
687 .type = QEMU_OPT_STRING,
688 .help = "file name",
689 },
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",
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",
704 },{
705 .name = "copy-on-read",
706 .type = QEMU_OPT_BOOL,
707 .help = "copy read data from backing file into image file",
708 },
709
710 { /* end of list */ }
711 },
712 };
713
714 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
715 {
716 const char *value;
717 DriveInfo *dinfo = NULL;
718 QDict *bs_opts;
719 QemuOpts *legacy_opts;
720 DriveMediaType media = MEDIA_DISK;
721 BlockInterfaceType type;
722 int cyls, heads, secs, translation;
723 int max_devs, bus_id, unit_id, index;
724 const char *devaddr;
725 const char *werror, *rerror;
726 bool read_only = false;
727 bool copy_on_read;
728 const char *serial;
729 const char *filename;
730 Error *local_err = NULL;
731 int i;
732
733 /* Change legacy command line options into QMP ones */
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" },
741
742 { "bps", "throttling.bps-total" },
743 { "bps_rd", "throttling.bps-read" },
744 { "bps_wr", "throttling.bps-write" },
745
746 { "iops_max", "throttling.iops-total-max" },
747 { "iops_rd_max", "throttling.iops-read-max" },
748 { "iops_wr_max", "throttling.iops-write-max" },
749
750 { "bps_max", "throttling.bps-total-max" },
751 { "bps_rd_max", "throttling.bps-read-max" },
752 { "bps_wr_max", "throttling.bps-write-max" },
753
754 { "iops_size", "throttling.iops-size" },
755
756 { "readonly", "read-only" },
757 };
758
759 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
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 }
767 }
768
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
794 /* Get a QDict for processing the options */
795 bs_opts = qdict_new();
796 qemu_opts_to_qdict(all_opts, bs_opts);
797
798 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
799 &error_abort);
800 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
801 if (local_err) {
802 error_report("%s", error_get_pretty(local_err));
803 error_free(local_err);
804 goto fail;
805 }
806
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
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;
821 read_only = true;
822 } else {
823 error_report("'%s' invalid media", value);
824 goto fail;
825 }
826 }
827
828 /* copy-on-read is disabled with a warning for read-only devices */
829 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
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
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
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;
889 } else if (!strcmp(value, "large")) {
890 translation = BIOS_ATA_TRANSLATION_LARGE;
891 } else if (!strcmp(value, "rechs")) {
892 translation = BIOS_ATA_TRANSLATION_RECHS;
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
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
947 /* Serial number */
948 serial = qemu_opt_get(legacy_opts, "serial");
949
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
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;
977 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
978 &error_abort);
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
990 filename = qemu_opt_get(legacy_opts, "file");
991
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
1013 /* Actual block device init: Functionality shared with blockdev-add */
1014 dinfo = blockdev_init(filename, bs_opts, &local_err);
1015 bs_opts = NULL;
1016 if (dinfo == NULL) {
1017 if (local_err) {
1018 error_report("%s", error_get_pretty(local_err));
1019 error_free(local_err);
1020 }
1021 goto fail;
1022 } else {
1023 assert(!local_err);
1024 }
1025
1026 /* Set legacy DriveInfo fields */
1027 dinfo->enable_auto_del = true;
1028 dinfo->opts = all_opts;
1029
1030 dinfo->cyls = cyls;
1031 dinfo->heads = heads;
1032 dinfo->secs = secs;
1033 dinfo->trans = translation;
1034
1035 dinfo->type = type;
1036 dinfo->bus = bus_id;
1037 dinfo->unit = unit_id;
1038 dinfo->devaddr = devaddr;
1039
1040 dinfo->serial = g_strdup(serial);
1041
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
1053 fail:
1054 qemu_opts_del(legacy_opts);
1055 QDECREF(bs_opts);
1056 return dinfo;
1057 }
1058
1059 void do_commit(Monitor *mon, const QDict *qdict)
1060 {
1061 const char *device = qdict_get_str(qdict, "device");
1062 BlockDriverState *bs;
1063 int ret;
1064
1065 if (!strcmp(device, "all")) {
1066 ret = bdrv_commit_all();
1067 } else {
1068 bs = bdrv_find(device);
1069 if (!bs) {
1070 monitor_printf(mon, "Device '%s' not found\n", device);
1071 return;
1072 }
1073 ret = bdrv_commit(bs);
1074 }
1075 if (ret < 0) {
1076 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1077 strerror(-ret));
1078 }
1079 }
1080
1081 static void blockdev_do_action(int kind, void *data, Error **errp)
1082 {
1083 TransactionAction action;
1084 TransactionActionList list;
1085
1086 action.kind = kind;
1087 action.data = data;
1088 list.value = &action;
1089 list.next = NULL;
1090 qmp_transaction(&list, errp);
1091 }
1092
1093 void 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,
1098 bool has_format, const char *format,
1099 bool has_mode, NewImageMode mode, Error **errp)
1100 {
1101 BlockdevSnapshot snapshot = {
1102 .has_device = has_device,
1103 .device = (char *) device,
1104 .has_node_name = has_node_name,
1105 .node_name = (char *) node_name,
1106 .snapshot_file = (char *) snapshot_file,
1107 .has_snapshot_node_name = has_snapshot_node_name,
1108 .snapshot_node_name = (char *) snapshot_node_name,
1109 .has_format = has_format,
1110 .format = (char *) format,
1111 .has_mode = has_mode,
1112 .mode = mode,
1113 };
1114 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1115 &snapshot, errp);
1116 }
1117
1118 void 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
1131 SnapshotInfo *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);
1163 if (local_err) {
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);
1176 if (local_err) {
1177 error_propagate(errp, local_err);
1178 return NULL;
1179 }
1180
1181 info = g_new0(SnapshotInfo, 1);
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 }
1192
1193 /* New and old BlockDriverState structs for group snapshots */
1194
1195 typedef struct BlkTransactionState BlkTransactionState;
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. */
1199 typedef struct BdrvActionOps {
1200 /* Size of state struct, in bytes. */
1201 size_t instance_size;
1202 /* Prepare the work, must NOT be NULL. */
1203 void (*prepare)(BlkTransactionState *common, Error **errp);
1204 /* Commit the changes, can be NULL. */
1205 void (*commit)(BlkTransactionState *common);
1206 /* Abort the changes on fail, can be NULL. */
1207 void (*abort)(BlkTransactionState *common);
1208 /* Clean up resource in the end, can be NULL. */
1209 void (*clean)(BlkTransactionState *common);
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 */
1217 struct BlkTransactionState {
1218 TransactionAction *action;
1219 const BdrvActionOps *ops;
1220 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1221 };
1222
1223 /* internal snapshot private data */
1224 typedef struct InternalSnapshotState {
1225 BlkTransactionState common;
1226 BlockDriverState *bs;
1227 QEMUSnapshotInfo sn;
1228 } InternalSnapshotState;
1229
1230 static void internal_snapshot_prepare(BlkTransactionState *common,
1231 Error **errp)
1232 {
1233 Error *local_err = NULL;
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 */
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);
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
1314 static 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
1337 /* external snapshot private data */
1338 typedef struct ExternalSnapshotState {
1339 BlkTransactionState common;
1340 BlockDriverState *old_bs;
1341 BlockDriverState *new_bs;
1342 } ExternalSnapshotState;
1343
1344 static void external_snapshot_prepare(BlkTransactionState *common,
1345 Error **errp)
1346 {
1347 BlockDriver *drv;
1348 int flags, ret;
1349 QDict *options = NULL;
1350 Error *local_err = NULL;
1351 bool has_device = false;
1352 const char *device;
1353 bool has_node_name = false;
1354 const char *node_name;
1355 bool has_snapshot_node_name = false;
1356 const char *snapshot_node_name;
1357 const char *new_image_file;
1358 const char *format = "qcow2";
1359 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1360 ExternalSnapshotState *state =
1361 DO_UPCAST(ExternalSnapshotState, common, common);
1362 TransactionAction *action = common->action;
1363
1364 /* get parameters */
1365 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1366
1367 has_device = action->blockdev_snapshot_sync->has_device;
1368 device = action->blockdev_snapshot_sync->device;
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
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 */
1384 drv = bdrv_find_format(format);
1385 if (!drv) {
1386 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1387 return;
1388 }
1389
1390 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1391 has_node_name ? node_name : NULL,
1392 &local_err);
1393 if (local_err) {
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");
1405 return;
1406 }
1407
1408 if (!bdrv_is_inserted(state->old_bs)) {
1409 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1410 return;
1411 }
1412
1413 if (bdrv_op_is_blocked(state->old_bs,
1414 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1415 return;
1416 }
1417
1418 if (!bdrv_is_read_only(state->old_bs)) {
1419 if (bdrv_flush(state->old_bs)) {
1420 error_set(errp, QERR_IO_ERROR);
1421 return;
1422 }
1423 }
1424
1425 if (!bdrv_is_first_non_filter(state->old_bs)) {
1426 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1427 return;
1428 }
1429
1430 flags = state->old_bs->open_flags;
1431
1432 /* create new image w/backing file */
1433 if (mode != NEW_IMAGE_MODE_EXISTING) {
1434 bdrv_img_create(new_image_file, format,
1435 state->old_bs->filename,
1436 state->old_bs->drv->format_name,
1437 NULL, -1, flags, &local_err, false);
1438 if (local_err) {
1439 error_propagate(errp, local_err);
1440 return;
1441 }
1442 }
1443
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
1450 /* TODO Inherit bs->options or only take explicit options with an
1451 * extended QMP command? */
1452 assert(state->new_bs == NULL);
1453 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1454 flags | BDRV_O_NO_BACKING, drv, &local_err);
1455 /* We will manually add the backing_hd field to the bs later */
1456 if (ret != 0) {
1457 error_propagate(errp, local_err);
1458 }
1459 }
1460
1461 static void external_snapshot_commit(BlkTransactionState *common)
1462 {
1463 ExternalSnapshotState *state =
1464 DO_UPCAST(ExternalSnapshotState, common, common);
1465
1466 /* This removes our old bs and adds the new bs */
1467 bdrv_append(state->new_bs, state->old_bs);
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 */
1471 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1472 NULL);
1473 }
1474
1475 static void external_snapshot_abort(BlkTransactionState *common)
1476 {
1477 ExternalSnapshotState *state =
1478 DO_UPCAST(ExternalSnapshotState, common, common);
1479 if (state->new_bs) {
1480 bdrv_unref(state->new_bs);
1481 }
1482 }
1483
1484 typedef struct DriveBackupState {
1485 BlkTransactionState common;
1486 BlockDriverState *bs;
1487 BlockJob *job;
1488 } DriveBackupState;
1489
1490 static 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,
1501 backup->sync,
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);
1507 if (local_err) {
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
1518 static 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
1529 static void abort_prepare(BlkTransactionState *common, Error **errp)
1530 {
1531 error_setg(errp, "Transaction aborted using Abort action");
1532 }
1533
1534 static void abort_commit(BlkTransactionState *common)
1535 {
1536 g_assert_not_reached(); /* this action never succeeds */
1537 }
1538
1539 static const BdrvActionOps actions[] = {
1540 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1541 .instance_size = sizeof(ExternalSnapshotState),
1542 .prepare = external_snapshot_prepare,
1543 .commit = external_snapshot_commit,
1544 .abort = external_snapshot_abort,
1545 },
1546 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1547 .instance_size = sizeof(DriveBackupState),
1548 .prepare = drive_backup_prepare,
1549 .abort = drive_backup_abort,
1550 },
1551 [TRANSACTION_ACTION_KIND_ABORT] = {
1552 .instance_size = sizeof(BlkTransactionState),
1553 .prepare = abort_prepare,
1554 .commit = abort_commit,
1555 },
1556 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1557 .instance_size = sizeof(InternalSnapshotState),
1558 .prepare = internal_snapshot_prepare,
1559 .abort = internal_snapshot_abort,
1560 },
1561 };
1562
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 */
1568 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1569 {
1570 TransactionActionList *dev_entry = dev_list;
1571 BlkTransactionState *state, *next;
1572 Error *local_err = NULL;
1573
1574 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
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) {
1582 TransactionAction *dev_info = NULL;
1583 const BdrvActionOps *ops;
1584
1585 dev_info = dev_entry->value;
1586 dev_entry = dev_entry->next;
1587
1588 assert(dev_info->kind < ARRAY_SIZE(actions));
1589
1590 ops = &actions[dev_info->kind];
1591 assert(ops->instance_size > 0);
1592
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);
1597
1598 state->ops->prepare(state, &local_err);
1599 if (local_err) {
1600 error_propagate(errp, local_err);
1601 goto delete_and_fail;
1602 }
1603 }
1604
1605 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1606 if (state->ops->commit) {
1607 state->ops->commit(state);
1608 }
1609 }
1610
1611 /* success */
1612 goto exit;
1613
1614 delete_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 */
1619 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1620 if (state->ops->abort) {
1621 state->ops->abort(state);
1622 }
1623 }
1624 exit:
1625 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1626 if (state->ops->clean) {
1627 state->ops->clean(state);
1628 }
1629 g_free(state);
1630 }
1631 }
1632
1633
1634 static void eject_device(BlockDriverState *bs, int force, Error **errp)
1635 {
1636 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
1637 return;
1638 }
1639 if (!bdrv_dev_has_removable_media(bs)) {
1640 error_setg(errp, "Device '%s' is not removable",
1641 bdrv_get_device_name(bs));
1642 return;
1643 }
1644
1645 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1646 bdrv_dev_eject_request(bs, force);
1647 if (!force) {
1648 error_setg(errp, "Device '%s' is locked",
1649 bdrv_get_device_name(bs));
1650 return;
1651 }
1652 }
1653
1654 bdrv_close(bs);
1655 }
1656
1657 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1658 {
1659 BlockDriverState *bs;
1660
1661 bs = bdrv_find(device);
1662 if (!bs) {
1663 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1664 return;
1665 }
1666
1667 eject_device(bs, force, errp);
1668 }
1669
1670 void qmp_block_passwd(bool has_device, const char *device,
1671 bool has_node_name, const char *node_name,
1672 const char *password, Error **errp)
1673 {
1674 Error *local_err = NULL;
1675 BlockDriverState *bs;
1676 int err;
1677
1678 bs = bdrv_lookup_bs(has_device ? device : NULL,
1679 has_node_name ? node_name : NULL,
1680 &local_err);
1681 if (local_err) {
1682 error_propagate(errp, local_err);
1683 return;
1684 }
1685
1686 err = bdrv_set_key(bs, password);
1687 if (err == -EINVAL) {
1688 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1689 return;
1690 } else if (err < 0) {
1691 error_set(errp, QERR_INVALID_PASSWORD);
1692 return;
1693 }
1694 }
1695
1696 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1697 int bdrv_flags, BlockDriver *drv,
1698 const char *password, Error **errp)
1699 {
1700 Error *local_err = NULL;
1701 int ret;
1702
1703 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
1704 if (ret < 0) {
1705 error_propagate(errp, local_err);
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
1723 void qmp_change_blockdev(const char *device, const char *filename,
1724 const char *format, Error **errp)
1725 {
1726 BlockDriverState *bs;
1727 BlockDriver *drv = NULL;
1728 int bdrv_flags;
1729 Error *err = NULL;
1730
1731 bs = bdrv_find(device);
1732 if (!bs) {
1733 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1734 return;
1735 }
1736
1737 if (format) {
1738 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1739 if (!drv) {
1740 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1741 return;
1742 }
1743 }
1744
1745 eject_device(bs, 0, &err);
1746 if (err) {
1747 error_propagate(errp, err);
1748 return;
1749 }
1750
1751 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1752 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1753
1754 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1755 }
1756
1757 /* throttling disk I/O limits */
1758 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
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,
1774 int64_t iops_wr_max,
1775 bool has_iops_size,
1776 int64_t iops_size, Error **errp)
1777 {
1778 ThrottleConfig cfg;
1779 BlockDriverState *bs;
1780 AioContext *aio_context;
1781
1782 bs = bdrv_find(device);
1783 if (!bs) {
1784 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1785 return;
1786 }
1787
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
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 }
1815
1816 if (has_iops_size) {
1817 cfg.op_size = iops_size;
1818 }
1819
1820 if (!check_throttle_config(&cfg, errp)) {
1821 return;
1822 }
1823
1824 aio_context = bdrv_get_aio_context(bs);
1825 aio_context_acquire(aio_context);
1826
1827 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
1828 bdrv_io_limits_enable(bs);
1829 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
1830 bdrv_io_limits_disable(bs);
1831 }
1832
1833 if (bs->io_limits_enabled) {
1834 bdrv_set_io_limits(bs, &cfg);
1835 }
1836
1837 aio_context_release(aio_context);
1838 }
1839
1840 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1841 {
1842 const char *id = qdict_get_str(qdict, "id");
1843 BlockDriverState *bs;
1844 DriveInfo *dinfo;
1845 AioContext *aio_context;
1846 Error *local_err = NULL;
1847
1848 bs = bdrv_find(id);
1849 if (!bs) {
1850 error_report("Device '%s' not found", id);
1851 return -1;
1852 }
1853
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
1861 aio_context = bdrv_get_aio_context(bs);
1862 aio_context_acquire(aio_context);
1863
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);
1867 aio_context_release(aio_context);
1868 return -1;
1869 }
1870
1871 /* quiesce block driver; prevent further io */
1872 bdrv_drain_all();
1873 bdrv_flush(bs);
1874 bdrv_close(bs);
1875
1876 /* if we have a device attached to this BlockDriverState
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 */
1881 if (bdrv_get_attached_dev(bs)) {
1882 bdrv_make_anon(bs);
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);
1887 } else {
1888 drive_del(dinfo);
1889 }
1890
1891 aio_context_release(aio_context);
1892 return 0;
1893 }
1894
1895 void qmp_block_resize(bool has_device, const char *device,
1896 bool has_node_name, const char *node_name,
1897 int64_t size, Error **errp)
1898 {
1899 Error *local_err = NULL;
1900 BlockDriverState *bs;
1901 AioContext *aio_context;
1902 int ret;
1903
1904 bs = bdrv_lookup_bs(has_device ? device : NULL,
1905 has_node_name ? node_name : NULL,
1906 &local_err);
1907 if (local_err) {
1908 error_propagate(errp, local_err);
1909 return;
1910 }
1911
1912 aio_context = bdrv_get_aio_context(bs);
1913 aio_context_acquire(aio_context);
1914
1915 if (!bdrv_is_first_non_filter(bs)) {
1916 error_set(errp, QERR_FEATURE_DISABLED, "resize");
1917 goto out;
1918 }
1919
1920 if (size < 0) {
1921 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1922 goto out;
1923 }
1924
1925 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
1926 error_set(errp, QERR_DEVICE_IN_USE, device);
1927 goto out;
1928 }
1929
1930 /* complete all in-flight operations before resizing the device */
1931 bdrv_drain_all();
1932
1933 ret = bdrv_truncate(bs, size);
1934 switch (ret) {
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:
1950 error_setg_errno(errp, -ret, "Could not resize");
1951 break;
1952 }
1953
1954 out:
1955 aio_context_release(aio_context);
1956 }
1957
1958 static void block_job_cb(void *opaque, int ret)
1959 {
1960 BlockDriverState *bs = opaque;
1961 const char *msg = NULL;
1962
1963 trace_block_job_cb(bs, bs->job, ret);
1964
1965 assert(bs->job);
1966
1967 if (ret < 0) {
1968 msg = strerror(-ret);
1969 }
1970
1971 if (block_job_is_cancelled(bs->job)) {
1972 block_job_event_cancelled(bs->job);
1973 } else {
1974 block_job_event_completed(bs->job, msg);
1975 }
1976
1977 bdrv_put_ref_bh_schedule(bs);
1978 }
1979
1980 void 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,
1984 bool has_on_error, BlockdevOnError on_error,
1985 Error **errp)
1986 {
1987 BlockDriverState *bs;
1988 BlockDriverState *base_bs = NULL;
1989 Error *local_err = NULL;
1990 const char *base_name = NULL;
1991
1992 if (!has_on_error) {
1993 on_error = BLOCKDEV_ON_ERROR_REPORT;
1994 }
1995
1996 bs = bdrv_find(device);
1997 if (!bs) {
1998 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1999 return;
2000 }
2001
2002 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2003 return;
2004 }
2005
2006 if (has_base) {
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 }
2012 base_name = base;
2013 }
2014
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,
2027 on_error, block_job_cb, bs, &local_err);
2028 if (local_err) {
2029 error_propagate(errp, local_err);
2030 return;
2031 }
2032
2033 trace_qmp_block_stream(bs, bs->job);
2034 }
2035
2036 void qmp_block_commit(const char *device,
2037 bool has_base, const char *base,
2038 bool has_top, const char *top,
2039 bool has_backing_file, const char *backing_file,
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 */
2049 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2050
2051 if (!has_speed) {
2052 speed = 0;
2053 }
2054
2055 /* drain all i/o before commits */
2056 bdrv_drain_all();
2057
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. */
2063 bs = bdrv_find(device);
2064 if (!bs) {
2065 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
2066 return;
2067 }
2068
2069 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, errp)) {
2070 return;
2071 }
2072
2073 /* default top_bs is the active layer */
2074 top_bs = bs;
2075
2076 if (has_top && top) {
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
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
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
2104 if (top_bs == bs) {
2105 if (has_backing_file) {
2106 error_setg(errp, "'backing-file' specified,"
2107 " but 'top' is the active layer");
2108 return;
2109 }
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,
2114 has_backing_file ? backing_file : NULL, &local_err);
2115 }
2116 if (local_err != NULL) {
2117 error_propagate(errp, local_err);
2118 return;
2119 }
2120 }
2121
2122 void qmp_drive_backup(const char *device, const char *target,
2123 bool has_format, const char *format,
2124 enum MirrorSyncMode sync,
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;
2133 BlockDriverState *source = NULL;
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
2175 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2176 return;
2177 }
2178
2179 flags = bs->open_flags | BDRV_O_RDWR;
2180
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
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);
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 }
2209 }
2210
2211 if (local_err) {
2212 error_propagate(errp, local_err);
2213 return;
2214 }
2215
2216 target_bs = NULL;
2217 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
2218 if (ret < 0) {
2219 error_propagate(errp, local_err);
2220 return;
2221 }
2222
2223 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
2224 block_job_cb, bs, &local_err);
2225 if (local_err != NULL) {
2226 bdrv_unref(target_bs);
2227 error_propagate(errp, local_err);
2228 return;
2229 }
2230 }
2231
2232 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2233 {
2234 return bdrv_named_nodes_list();
2235 }
2236
2237 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2238
2239 void qmp_drive_mirror(const char *device, const char *target,
2240 bool has_format, const char *format,
2241 bool has_node_name, const char *node_name,
2242 bool has_replaces, const char *replaces,
2243 enum MirrorSyncMode sync,
2244 bool has_mode, enum NewImageMode mode,
2245 bool has_speed, int64_t speed,
2246 bool has_granularity, uint32_t granularity,
2247 bool has_buf_size, int64_t buf_size,
2248 bool has_on_source_error, BlockdevOnError on_source_error,
2249 bool has_on_target_error, BlockdevOnError on_target_error,
2250 Error **errp)
2251 {
2252 BlockDriverState *bs;
2253 BlockDriverState *source, *target_bs;
2254 BlockDriver *drv = NULL;
2255 Error *local_err = NULL;
2256 QDict *options = NULL;
2257 int flags;
2258 int64_t size;
2259 int ret;
2260
2261 if (!has_speed) {
2262 speed = 0;
2263 }
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 }
2270 if (!has_mode) {
2271 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2272 }
2273 if (!has_granularity) {
2274 granularity = 0;
2275 }
2276 if (!has_buf_size) {
2277 buf_size = DEFAULT_MIRROR_BUF_SIZE;
2278 }
2279
2280 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
2281 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
2282 "a value in range [512B, 64MB]");
2283 return;
2284 }
2285 if (granularity & (granularity - 1)) {
2286 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
2287 return;
2288 }
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
2312 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
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 }
2321 if (sync == MIRROR_SYNC_MODE_NONE) {
2322 source = bs;
2323 }
2324
2325 size = bdrv_getlength(bs);
2326 if (size < 0) {
2327 error_setg_errno(errp, -size, "bdrv_getlength failed");
2328 return;
2329 }
2330
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
2354 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
2355 && mode != NEW_IMAGE_MODE_EXISTING)
2356 {
2357 /* create new image w/o backing file */
2358 assert(format && drv);
2359 bdrv_img_create(target, format,
2360 NULL, NULL, NULL, size, flags, &local_err, false);
2361 } else {
2362 switch (mode) {
2363 case NEW_IMAGE_MODE_EXISTING:
2364 break;
2365 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
2366 /* create new image with backing file */
2367 bdrv_img_create(target, format,
2368 source->filename,
2369 source->drv->format_name,
2370 NULL, size, flags, &local_err, false);
2371 break;
2372 default:
2373 abort();
2374 }
2375 }
2376
2377 if (local_err) {
2378 error_propagate(errp, local_err);
2379 return;
2380 }
2381
2382 if (has_node_name) {
2383 options = qdict_new();
2384 qdict_put(options, "node-name", qstring_from_str(node_name));
2385 }
2386
2387 /* Mirroring takes care of copy-on-write using the source's backing
2388 * file.
2389 */
2390 target_bs = NULL;
2391 ret = bdrv_open(&target_bs, target, NULL, options,
2392 flags | BDRV_O_NO_BACKING, drv, &local_err);
2393 if (ret < 0) {
2394 error_propagate(errp, local_err);
2395 return;
2396 }
2397
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,
2404 on_source_error, on_target_error,
2405 block_job_cb, bs, &local_err);
2406 if (local_err != NULL) {
2407 bdrv_unref(target_bs);
2408 error_propagate(errp, local_err);
2409 return;
2410 }
2411 }
2412
2413 static 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
2424 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2425 {
2426 BlockJob *job = find_block_job(device);
2427
2428 if (!job) {
2429 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2430 return;
2431 }
2432
2433 block_job_set_speed(job, speed, errp);
2434 }
2435
2436 void qmp_block_job_cancel(const char *device,
2437 bool has_force, bool force, Error **errp)
2438 {
2439 BlockJob *job = find_block_job(device);
2440
2441 if (!has_force) {
2442 force = false;
2443 }
2444
2445 if (!job) {
2446 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2447 return;
2448 }
2449 if (job->paused && !force) {
2450 error_setg(errp, "The block job for device '%s' is currently paused",
2451 device);
2452 return;
2453 }
2454
2455 trace_qmp_block_job_cancel(job);
2456 block_job_cancel(job);
2457 }
2458
2459 void 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
2472 void 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
2485 void 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
2498 void 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
2577 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
2578 {
2579 QmpOutputVisitor *ov = qmp_output_visitor_new();
2580 DriveInfo *dinfo;
2581 QObject *obj;
2582 QDict *qdict;
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
2591 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2592 * cache.direct=false instead of silently switching to aio=threads, except
2593 * when called from drive_new().
2594 *
2595 * For now, simply forbidding the combination for all drivers will do. */
2596 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
2597 bool direct = options->has_cache &&
2598 options->cache->has_direct &&
2599 options->cache->direct;
2600 if (!direct) {
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);
2608 if (local_err) {
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
2618 dinfo = blockdev_init(NULL, qdict, &local_err);
2619 if (local_err) {
2620 error_propagate(errp, local_err);
2621 goto fail;
2622 }
2623
2624 if (bdrv_key_required(dinfo->bdrv)) {
2625 drive_del(dinfo);
2626 error_setg(errp, "blockdev-add doesn't support encrypted devices");
2627 goto fail;
2628 }
2629
2630 fail:
2631 qmp_output_visitor_cleanup(ov);
2632 }
2633
2634 static 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) {
2640 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2641 elem->value = block_job_query(bs->job);
2642 (*prev)->next = elem;
2643 *prev = elem;
2644 }
2645 }
2646
2647 BlockJobInfoList *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 }
2655
2656 QemuOptsList qemu_common_drive_opts = {
2657 .name = "drive",
2658 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
2659 .desc = {
2660 {
2661 .name = "snapshot",
2662 .type = QEMU_OPT_BOOL,
2663 .help = "enable/disable snapshot mode",
2664 },{
2665 .name = "discard",
2666 .type = QEMU_OPT_STRING,
2667 .help = "discard operation (ignore/off, unmap/on)",
2668 },{
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",
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, ...)",
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",
2696 },{
2697 .name = "read-only",
2698 .type = QEMU_OPT_BOOL,
2699 .help = "open drive file as read-only",
2700 },{
2701 .name = "throttling.iops-total",
2702 .type = QEMU_OPT_NUMBER,
2703 .help = "limit total I/O operations per second",
2704 },{
2705 .name = "throttling.iops-read",
2706 .type = QEMU_OPT_NUMBER,
2707 .help = "limit read operations per second",
2708 },{
2709 .name = "throttling.iops-write",
2710 .type = QEMU_OPT_NUMBER,
2711 .help = "limit write operations per second",
2712 },{
2713 .name = "throttling.bps-total",
2714 .type = QEMU_OPT_NUMBER,
2715 .help = "limit total bytes per second",
2716 },{
2717 .name = "throttling.bps-read",
2718 .type = QEMU_OPT_NUMBER,
2719 .help = "limit read bytes per second",
2720 },{
2721 .name = "throttling.bps-write",
2722 .type = QEMU_OPT_NUMBER,
2723 .help = "limit write bytes per second",
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",
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",
2752 },{
2753 .name = "copy-on-read",
2754 .type = QEMU_OPT_BOOL,
2755 .help = "copy read data from backing file into image file",
2756 },{
2757 .name = "detect-zeroes",
2758 .type = QEMU_OPT_STRING,
2759 .help = "try to optimize zero writes (off, on, unmap)",
2760 },
2761 { /* end of list */ }
2762 },
2763 };
2764
2765 QemuOptsList qemu_drive_opts = {
2766 .name = "drive",
2767 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
2768 .desc = {
2769 /*
2770 * no elements => accept any params
2771 * validation will happen later
2772 */
2773 { /* end of list */ }
2774 },
2775 };