]> git.proxmox.com Git - mirror_qemu.git/blob - blockdev.c
throttle: Add command-line settings to define the burst periods
[mirror_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 "qemu/osdep.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/blockdev.h"
36 #include "hw/block/block.h"
37 #include "block/blockjob.h"
38 #include "block/throttle-groups.h"
39 #include "monitor/monitor.h"
40 #include "qemu/error-report.h"
41 #include "qemu/option.h"
42 #include "qemu/config-file.h"
43 #include "qapi/qmp/types.h"
44 #include "qapi-visit.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qmp-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
51 #include "trace.h"
52 #include "sysemu/arch_init.h"
53
54 static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
55 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
56
57 static const char *const if_name[IF_COUNT] = {
58 [IF_NONE] = "none",
59 [IF_IDE] = "ide",
60 [IF_SCSI] = "scsi",
61 [IF_FLOPPY] = "floppy",
62 [IF_PFLASH] = "pflash",
63 [IF_MTD] = "mtd",
64 [IF_SD] = "sd",
65 [IF_VIRTIO] = "virtio",
66 [IF_XEN] = "xen",
67 };
68
69 static int if_max_devs[IF_COUNT] = {
70 /*
71 * Do not change these numbers! They govern how drive option
72 * index maps to unit and bus. That mapping is ABI.
73 *
74 * All controllers used to imlement if=T drives need to support
75 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
76 * Otherwise, some index values map to "impossible" bus, unit
77 * values.
78 *
79 * For instance, if you change [IF_SCSI] to 255, -drive
80 * if=scsi,index=12 no longer means bus=1,unit=5, but
81 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
82 * the drive can't be set up. Regression.
83 */
84 [IF_IDE] = 2,
85 [IF_SCSI] = 7,
86 };
87
88 /**
89 * Boards may call this to offer board-by-board overrides
90 * of the default, global values.
91 */
92 void override_max_devs(BlockInterfaceType type, int max_devs)
93 {
94 BlockBackend *blk;
95 DriveInfo *dinfo;
96
97 if (max_devs <= 0) {
98 return;
99 }
100
101 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
102 dinfo = blk_legacy_dinfo(blk);
103 if (dinfo->type == type) {
104 fprintf(stderr, "Cannot override units-per-bus property of"
105 " the %s interface, because a drive of that type has"
106 " already been added.\n", if_name[type]);
107 g_assert_not_reached();
108 }
109 }
110
111 if_max_devs[type] = max_devs;
112 }
113
114 /*
115 * We automatically delete the drive when a device using it gets
116 * unplugged. Questionable feature, but we can't just drop it.
117 * Device models call blockdev_mark_auto_del() to schedule the
118 * automatic deletion, and generic qdev code calls blockdev_auto_del()
119 * when deletion is actually safe.
120 */
121 void blockdev_mark_auto_del(BlockBackend *blk)
122 {
123 DriveInfo *dinfo = blk_legacy_dinfo(blk);
124 BlockDriverState *bs = blk_bs(blk);
125 AioContext *aio_context;
126
127 if (!dinfo) {
128 return;
129 }
130
131 if (bs) {
132 aio_context = bdrv_get_aio_context(bs);
133 aio_context_acquire(aio_context);
134
135 if (bs->job) {
136 block_job_cancel(bs->job);
137 }
138
139 aio_context_release(aio_context);
140 }
141
142 dinfo->auto_del = 1;
143 }
144
145 void blockdev_auto_del(BlockBackend *blk)
146 {
147 DriveInfo *dinfo = blk_legacy_dinfo(blk);
148
149 if (dinfo && dinfo->auto_del) {
150 blk_unref(blk);
151 }
152 }
153
154 /**
155 * Returns the current mapping of how many units per bus
156 * a particular interface can support.
157 *
158 * A positive integer indicates n units per bus.
159 * 0 implies the mapping has not been established.
160 * -1 indicates an invalid BlockInterfaceType was given.
161 */
162 int drive_get_max_devs(BlockInterfaceType type)
163 {
164 if (type >= IF_IDE && type < IF_COUNT) {
165 return if_max_devs[type];
166 }
167
168 return -1;
169 }
170
171 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
172 {
173 int max_devs = if_max_devs[type];
174 return max_devs ? index / max_devs : 0;
175 }
176
177 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
178 {
179 int max_devs = if_max_devs[type];
180 return max_devs ? index % max_devs : index;
181 }
182
183 QemuOpts *drive_def(const char *optstr)
184 {
185 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
186 }
187
188 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
189 const char *optstr)
190 {
191 QemuOpts *opts;
192
193 opts = drive_def(optstr);
194 if (!opts) {
195 return NULL;
196 }
197 if (type != IF_DEFAULT) {
198 qemu_opt_set(opts, "if", if_name[type], &error_abort);
199 }
200 if (index >= 0) {
201 qemu_opt_set_number(opts, "index", index, &error_abort);
202 }
203 if (file)
204 qemu_opt_set(opts, "file", file, &error_abort);
205 return opts;
206 }
207
208 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
209 {
210 BlockBackend *blk;
211 DriveInfo *dinfo;
212
213 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
214 dinfo = blk_legacy_dinfo(blk);
215 if (dinfo && dinfo->type == type
216 && dinfo->bus == bus && dinfo->unit == unit) {
217 return dinfo;
218 }
219 }
220
221 return NULL;
222 }
223
224 bool drive_check_orphaned(void)
225 {
226 BlockBackend *blk;
227 DriveInfo *dinfo;
228 bool rs = false;
229
230 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
231 dinfo = blk_legacy_dinfo(blk);
232 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233 /* Unless this is a default drive, this may be an oversight. */
234 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
235 dinfo->type != IF_NONE) {
236 fprintf(stderr, "Warning: Orphaned drive without device: "
237 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
238 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
239 if_name[dinfo->type], dinfo->bus, dinfo->unit);
240 rs = true;
241 }
242 }
243
244 return rs;
245 }
246
247 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
248 {
249 return drive_get(type,
250 drive_index_to_bus_id(type, index),
251 drive_index_to_unit_id(type, index));
252 }
253
254 int drive_get_max_bus(BlockInterfaceType type)
255 {
256 int max_bus;
257 BlockBackend *blk;
258 DriveInfo *dinfo;
259
260 max_bus = -1;
261 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
262 dinfo = blk_legacy_dinfo(blk);
263 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
264 max_bus = dinfo->bus;
265 }
266 }
267 return max_bus;
268 }
269
270 /* Get a block device. This should only be used for single-drive devices
271 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
272 appropriate bus. */
273 DriveInfo *drive_get_next(BlockInterfaceType type)
274 {
275 static int next_block_unit[IF_COUNT];
276
277 return drive_get(type, 0, next_block_unit[type]++);
278 }
279
280 static void bdrv_format_print(void *opaque, const char *name)
281 {
282 error_printf(" %s", name);
283 }
284
285 typedef struct {
286 QEMUBH *bh;
287 BlockDriverState *bs;
288 } BDRVPutRefBH;
289
290 static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
291 {
292 if (!strcmp(buf, "ignore")) {
293 return BLOCKDEV_ON_ERROR_IGNORE;
294 } else if (!is_read && !strcmp(buf, "enospc")) {
295 return BLOCKDEV_ON_ERROR_ENOSPC;
296 } else if (!strcmp(buf, "stop")) {
297 return BLOCKDEV_ON_ERROR_STOP;
298 } else if (!strcmp(buf, "report")) {
299 return BLOCKDEV_ON_ERROR_REPORT;
300 } else {
301 error_setg(errp, "'%s' invalid %s error action",
302 buf, is_read ? "read" : "write");
303 return -1;
304 }
305 }
306
307 static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
308 Error **errp)
309 {
310 const QListEntry *entry;
311 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
312 switch (qobject_type(entry->value)) {
313
314 case QTYPE_QSTRING: {
315 unsigned long long length;
316 const char *str = qstring_get_str(qobject_to_qstring(entry->value));
317 if (parse_uint_full(str, &length, 10) == 0 &&
318 length > 0 && length <= UINT_MAX) {
319 block_acct_add_interval(stats, (unsigned) length);
320 } else {
321 error_setg(errp, "Invalid interval length: %s", str);
322 return false;
323 }
324 break;
325 }
326
327 case QTYPE_QINT: {
328 int64_t length = qint_get_int(qobject_to_qint(entry->value));
329 if (length > 0 && length <= UINT_MAX) {
330 block_acct_add_interval(stats, (unsigned) length);
331 } else {
332 error_setg(errp, "Invalid interval length: %" PRId64, length);
333 return false;
334 }
335 break;
336 }
337
338 default:
339 error_setg(errp, "The specification of stats-intervals is invalid");
340 return false;
341 }
342 }
343 return true;
344 }
345
346 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
347
348 /* All parameters but @opts are optional and may be set to NULL. */
349 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
350 const char **throttling_group, ThrottleConfig *throttle_cfg,
351 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
352 {
353 const char *discard;
354 Error *local_error = NULL;
355 const char *aio;
356
357 if (bdrv_flags) {
358 if (!qemu_opt_get_bool(opts, "read-only", false)) {
359 *bdrv_flags |= BDRV_O_RDWR;
360 }
361 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
362 *bdrv_flags |= BDRV_O_COPY_ON_READ;
363 }
364
365 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
366 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
367 error_setg(errp, "Invalid discard option");
368 return;
369 }
370 }
371
372 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
373 if (!strcmp(aio, "native")) {
374 *bdrv_flags |= BDRV_O_NATIVE_AIO;
375 } else if (!strcmp(aio, "threads")) {
376 /* this is the default */
377 } else {
378 error_setg(errp, "invalid aio option");
379 return;
380 }
381 }
382 }
383
384 /* disk I/O throttling */
385 if (throttling_group) {
386 *throttling_group = qemu_opt_get(opts, "throttling.group");
387 }
388
389 if (throttle_cfg) {
390 throttle_config_init(throttle_cfg);
391 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
392 qemu_opt_get_number(opts, "throttling.bps-total", 0);
393 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
394 qemu_opt_get_number(opts, "throttling.bps-read", 0);
395 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
396 qemu_opt_get_number(opts, "throttling.bps-write", 0);
397 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
398 qemu_opt_get_number(opts, "throttling.iops-total", 0);
399 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
400 qemu_opt_get_number(opts, "throttling.iops-read", 0);
401 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
402 qemu_opt_get_number(opts, "throttling.iops-write", 0);
403
404 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
405 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
406 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
407 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
408 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
409 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
410 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
411 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
412 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
413 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
414 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
415 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
416
417 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
418 qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
419 throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
420 qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
421 throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
422 qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
423 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
424 qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
425 throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
426 qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
427 throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
428 qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
429
430 throttle_cfg->op_size =
431 qemu_opt_get_number(opts, "throttling.iops-size", 0);
432
433 if (!throttle_is_valid(throttle_cfg, errp)) {
434 return;
435 }
436 }
437
438 if (detect_zeroes) {
439 *detect_zeroes =
440 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
441 qemu_opt_get(opts, "detect-zeroes"),
442 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
443 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
444 &local_error);
445 if (local_error) {
446 error_propagate(errp, local_error);
447 return;
448 }
449
450 if (bdrv_flags &&
451 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
452 !(*bdrv_flags & BDRV_O_UNMAP))
453 {
454 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
455 "without setting discard operation to unmap");
456 return;
457 }
458 }
459 }
460
461 /* Takes the ownership of bs_opts */
462 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
463 Error **errp)
464 {
465 const char *buf;
466 int bdrv_flags = 0;
467 int on_read_error, on_write_error;
468 bool account_invalid, account_failed;
469 BlockBackend *blk;
470 BlockDriverState *bs;
471 ThrottleConfig cfg;
472 int snapshot = 0;
473 Error *error = NULL;
474 QemuOpts *opts;
475 QDict *interval_dict = NULL;
476 QList *interval_list = NULL;
477 const char *id;
478 BlockdevDetectZeroesOptions detect_zeroes =
479 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
480 const char *throttling_group = NULL;
481
482 /* Check common options by copying from bs_opts to opts, all other options
483 * stay in bs_opts for processing by bdrv_open(). */
484 id = qdict_get_try_str(bs_opts, "id");
485 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
486 if (error) {
487 error_propagate(errp, error);
488 goto err_no_opts;
489 }
490
491 qemu_opts_absorb_qdict(opts, bs_opts, &error);
492 if (error) {
493 error_propagate(errp, error);
494 goto early_err;
495 }
496
497 if (id) {
498 qdict_del(bs_opts, "id");
499 }
500
501 /* extract parameters */
502 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
503
504 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
505 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
506
507 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
508 qdict_array_split(interval_dict, &interval_list);
509
510 if (qdict_size(interval_dict) != 0) {
511 error_setg(errp, "Invalid option stats-intervals.%s",
512 qdict_first(interval_dict)->key);
513 goto early_err;
514 }
515
516 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
517 &detect_zeroes, &error);
518 if (error) {
519 error_propagate(errp, error);
520 goto early_err;
521 }
522
523 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
524 if (is_help_option(buf)) {
525 error_printf("Supported formats:");
526 bdrv_iterate_format(bdrv_format_print, NULL);
527 error_printf("\n");
528 goto early_err;
529 }
530
531 if (qdict_haskey(bs_opts, "driver")) {
532 error_setg(errp, "Cannot specify both 'driver' and 'format'");
533 goto early_err;
534 }
535 qdict_put(bs_opts, "driver", qstring_from_str(buf));
536 }
537
538 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
539 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
540 on_write_error = parse_block_error_action(buf, 0, &error);
541 if (error) {
542 error_propagate(errp, error);
543 goto early_err;
544 }
545 }
546
547 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
548 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
549 on_read_error = parse_block_error_action(buf, 1, &error);
550 if (error) {
551 error_propagate(errp, error);
552 goto early_err;
553 }
554 }
555
556 if (snapshot) {
557 bdrv_flags |= BDRV_O_SNAPSHOT;
558 }
559
560 /* init */
561 if ((!file || !*file) && !qdict_size(bs_opts)) {
562 BlockBackendRootState *blk_rs;
563
564 blk = blk_new(qemu_opts_id(opts), errp);
565 if (!blk) {
566 goto early_err;
567 }
568
569 blk_rs = blk_get_root_state(blk);
570 blk_rs->open_flags = bdrv_flags;
571 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
572 blk_rs->detect_zeroes = detect_zeroes;
573
574 if (throttle_enabled(&cfg)) {
575 if (!throttling_group) {
576 throttling_group = blk_name(blk);
577 }
578 blk_rs->throttle_group = g_strdup(throttling_group);
579 blk_rs->throttle_state = throttle_group_incref(throttling_group);
580 blk_rs->throttle_state->cfg = cfg;
581 }
582
583 QDECREF(bs_opts);
584 } else {
585 if (file && !*file) {
586 file = NULL;
587 }
588
589 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
590 * with other callers) rather than what we want as the real defaults.
591 * Apply the defaults here instead. */
592 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on");
593 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
594 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
595
596 if (snapshot) {
597 /* always use cache=unsafe with snapshot */
598 qdict_put(bs_opts, BDRV_OPT_CACHE_WB, qstring_from_str("on"));
599 qdict_put(bs_opts, BDRV_OPT_CACHE_DIRECT, qstring_from_str("off"));
600 qdict_put(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, qstring_from_str("on"));
601 }
602
603 if (runstate_check(RUN_STATE_INMIGRATE)) {
604 bdrv_flags |= BDRV_O_INACTIVE;
605 }
606
607 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
608 errp);
609 if (!blk) {
610 goto err_no_bs_opts;
611 }
612 bs = blk_bs(blk);
613
614 bs->detect_zeroes = detect_zeroes;
615
616 /* disk I/O throttling */
617 if (throttle_enabled(&cfg)) {
618 if (!throttling_group) {
619 throttling_group = blk_name(blk);
620 }
621 bdrv_io_limits_enable(bs, throttling_group);
622 bdrv_set_io_limits(bs, &cfg);
623 }
624
625 if (bdrv_key_required(bs)) {
626 autostart = 0;
627 }
628
629 block_acct_init(blk_get_stats(blk), account_invalid, account_failed);
630
631 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
632 blk_unref(blk);
633 blk = NULL;
634 goto err_no_bs_opts;
635 }
636 }
637
638 blk_set_on_error(blk, on_read_error, on_write_error);
639
640 err_no_bs_opts:
641 qemu_opts_del(opts);
642 QDECREF(interval_dict);
643 QDECREF(interval_list);
644 return blk;
645
646 early_err:
647 qemu_opts_del(opts);
648 QDECREF(interval_dict);
649 QDECREF(interval_list);
650 err_no_opts:
651 QDECREF(bs_opts);
652 return NULL;
653 }
654
655 static QemuOptsList qemu_root_bds_opts;
656
657 /* Takes the ownership of bs_opts */
658 static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
659 {
660 BlockDriverState *bs;
661 QemuOpts *opts;
662 Error *local_error = NULL;
663 BlockdevDetectZeroesOptions detect_zeroes;
664 int ret;
665 int bdrv_flags = 0;
666
667 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
668 if (!opts) {
669 goto fail;
670 }
671
672 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
673 if (local_error) {
674 error_propagate(errp, local_error);
675 goto fail;
676 }
677
678 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
679 &detect_zeroes, &local_error);
680 if (local_error) {
681 error_propagate(errp, local_error);
682 goto fail;
683 }
684
685 if (runstate_check(RUN_STATE_INMIGRATE)) {
686 bdrv_flags |= BDRV_O_INACTIVE;
687 }
688
689 bs = NULL;
690 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
691 if (ret < 0) {
692 goto fail_no_bs_opts;
693 }
694
695 bs->detect_zeroes = detect_zeroes;
696
697 fail_no_bs_opts:
698 qemu_opts_del(opts);
699 return bs;
700
701 fail:
702 qemu_opts_del(opts);
703 QDECREF(bs_opts);
704 return NULL;
705 }
706
707 void blockdev_close_all_bdrv_states(void)
708 {
709 BlockDriverState *bs, *next_bs;
710
711 QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
712 AioContext *ctx = bdrv_get_aio_context(bs);
713
714 aio_context_acquire(ctx);
715 bdrv_unref(bs);
716 aio_context_release(ctx);
717 }
718 }
719
720 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
721 Error **errp)
722 {
723 const char *value;
724
725 value = qemu_opt_get(opts, from);
726 if (value) {
727 if (qemu_opt_find(opts, to)) {
728 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
729 "same time", to, from);
730 return;
731 }
732 }
733
734 /* rename all items in opts */
735 while ((value = qemu_opt_get(opts, from))) {
736 qemu_opt_set(opts, to, value, &error_abort);
737 qemu_opt_unset(opts, from);
738 }
739 }
740
741 QemuOptsList qemu_legacy_drive_opts = {
742 .name = "drive",
743 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
744 .desc = {
745 {
746 .name = "bus",
747 .type = QEMU_OPT_NUMBER,
748 .help = "bus number",
749 },{
750 .name = "unit",
751 .type = QEMU_OPT_NUMBER,
752 .help = "unit number (i.e. lun for scsi)",
753 },{
754 .name = "index",
755 .type = QEMU_OPT_NUMBER,
756 .help = "index number",
757 },{
758 .name = "media",
759 .type = QEMU_OPT_STRING,
760 .help = "media type (disk, cdrom)",
761 },{
762 .name = "if",
763 .type = QEMU_OPT_STRING,
764 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
765 },{
766 .name = "cyls",
767 .type = QEMU_OPT_NUMBER,
768 .help = "number of cylinders (ide disk geometry)",
769 },{
770 .name = "heads",
771 .type = QEMU_OPT_NUMBER,
772 .help = "number of heads (ide disk geometry)",
773 },{
774 .name = "secs",
775 .type = QEMU_OPT_NUMBER,
776 .help = "number of sectors (ide disk geometry)",
777 },{
778 .name = "trans",
779 .type = QEMU_OPT_STRING,
780 .help = "chs translation (auto, lba, none)",
781 },{
782 .name = "boot",
783 .type = QEMU_OPT_BOOL,
784 .help = "(deprecated, ignored)",
785 },{
786 .name = "addr",
787 .type = QEMU_OPT_STRING,
788 .help = "pci address (virtio only)",
789 },{
790 .name = "serial",
791 .type = QEMU_OPT_STRING,
792 .help = "disk serial number",
793 },{
794 .name = "file",
795 .type = QEMU_OPT_STRING,
796 .help = "file name",
797 },
798
799 /* Options that are passed on, but have special semantics with -drive */
800 {
801 .name = "read-only",
802 .type = QEMU_OPT_BOOL,
803 .help = "open drive file as read-only",
804 },{
805 .name = "rerror",
806 .type = QEMU_OPT_STRING,
807 .help = "read error action",
808 },{
809 .name = "werror",
810 .type = QEMU_OPT_STRING,
811 .help = "write error action",
812 },{
813 .name = "copy-on-read",
814 .type = QEMU_OPT_BOOL,
815 .help = "copy read data from backing file into image file",
816 },
817
818 { /* end of list */ }
819 },
820 };
821
822 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
823 {
824 const char *value;
825 BlockBackend *blk;
826 DriveInfo *dinfo = NULL;
827 QDict *bs_opts;
828 QemuOpts *legacy_opts;
829 DriveMediaType media = MEDIA_DISK;
830 BlockInterfaceType type;
831 int cyls, heads, secs, translation;
832 int max_devs, bus_id, unit_id, index;
833 const char *devaddr;
834 const char *werror, *rerror;
835 bool read_only = false;
836 bool copy_on_read;
837 const char *serial;
838 const char *filename;
839 Error *local_err = NULL;
840 int i;
841
842 /* Change legacy command line options into QMP ones */
843 static const struct {
844 const char *from;
845 const char *to;
846 } opt_renames[] = {
847 { "iops", "throttling.iops-total" },
848 { "iops_rd", "throttling.iops-read" },
849 { "iops_wr", "throttling.iops-write" },
850
851 { "bps", "throttling.bps-total" },
852 { "bps_rd", "throttling.bps-read" },
853 { "bps_wr", "throttling.bps-write" },
854
855 { "iops_max", "throttling.iops-total-max" },
856 { "iops_rd_max", "throttling.iops-read-max" },
857 { "iops_wr_max", "throttling.iops-write-max" },
858
859 { "bps_max", "throttling.bps-total-max" },
860 { "bps_rd_max", "throttling.bps-read-max" },
861 { "bps_wr_max", "throttling.bps-write-max" },
862
863 { "iops_size", "throttling.iops-size" },
864
865 { "group", "throttling.group" },
866
867 { "readonly", "read-only" },
868 };
869
870 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
871 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
872 &local_err);
873 if (local_err) {
874 error_report_err(local_err);
875 return NULL;
876 }
877 }
878
879 value = qemu_opt_get(all_opts, "cache");
880 if (value) {
881 int flags = 0;
882
883 if (bdrv_parse_cache_flags(value, &flags) != 0) {
884 error_report("invalid cache option");
885 return NULL;
886 }
887
888 /* Specific options take precedence */
889 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
890 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
891 !!(flags & BDRV_O_CACHE_WB), &error_abort);
892 }
893 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
894 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
895 !!(flags & BDRV_O_NOCACHE), &error_abort);
896 }
897 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
898 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
899 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
900 }
901 qemu_opt_unset(all_opts, "cache");
902 }
903
904 /* Get a QDict for processing the options */
905 bs_opts = qdict_new();
906 qemu_opts_to_qdict(all_opts, bs_opts);
907
908 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
909 &error_abort);
910 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
911 if (local_err) {
912 error_report_err(local_err);
913 goto fail;
914 }
915
916 /* Deprecated option boot=[on|off] */
917 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
918 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
919 "ignored. Future versions will reject this parameter. Please "
920 "update your scripts.\n");
921 }
922
923 /* Media type */
924 value = qemu_opt_get(legacy_opts, "media");
925 if (value) {
926 if (!strcmp(value, "disk")) {
927 media = MEDIA_DISK;
928 } else if (!strcmp(value, "cdrom")) {
929 media = MEDIA_CDROM;
930 read_only = true;
931 } else {
932 error_report("'%s' invalid media", value);
933 goto fail;
934 }
935 }
936
937 /* copy-on-read is disabled with a warning for read-only devices */
938 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
939 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
940
941 if (read_only && copy_on_read) {
942 error_report("warning: disabling copy-on-read on read-only drive");
943 copy_on_read = false;
944 }
945
946 qdict_put(bs_opts, "read-only",
947 qstring_from_str(read_only ? "on" : "off"));
948 qdict_put(bs_opts, "copy-on-read",
949 qstring_from_str(copy_on_read ? "on" :"off"));
950
951 /* Controller type */
952 value = qemu_opt_get(legacy_opts, "if");
953 if (value) {
954 for (type = 0;
955 type < IF_COUNT && strcmp(value, if_name[type]);
956 type++) {
957 }
958 if (type == IF_COUNT) {
959 error_report("unsupported bus type '%s'", value);
960 goto fail;
961 }
962 } else {
963 type = block_default_type;
964 }
965
966 /* Geometry */
967 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
968 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
969 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
970
971 if (cyls || heads || secs) {
972 if (cyls < 1) {
973 error_report("invalid physical cyls number");
974 goto fail;
975 }
976 if (heads < 1) {
977 error_report("invalid physical heads number");
978 goto fail;
979 }
980 if (secs < 1) {
981 error_report("invalid physical secs number");
982 goto fail;
983 }
984 }
985
986 translation = BIOS_ATA_TRANSLATION_AUTO;
987 value = qemu_opt_get(legacy_opts, "trans");
988 if (value != NULL) {
989 if (!cyls) {
990 error_report("'%s' trans must be used with cyls, heads and secs",
991 value);
992 goto fail;
993 }
994 if (!strcmp(value, "none")) {
995 translation = BIOS_ATA_TRANSLATION_NONE;
996 } else if (!strcmp(value, "lba")) {
997 translation = BIOS_ATA_TRANSLATION_LBA;
998 } else if (!strcmp(value, "large")) {
999 translation = BIOS_ATA_TRANSLATION_LARGE;
1000 } else if (!strcmp(value, "rechs")) {
1001 translation = BIOS_ATA_TRANSLATION_RECHS;
1002 } else if (!strcmp(value, "auto")) {
1003 translation = BIOS_ATA_TRANSLATION_AUTO;
1004 } else {
1005 error_report("'%s' invalid translation type", value);
1006 goto fail;
1007 }
1008 }
1009
1010 if (media == MEDIA_CDROM) {
1011 if (cyls || secs || heads) {
1012 error_report("CHS can't be set with media=cdrom");
1013 goto fail;
1014 }
1015 }
1016
1017 /* Device address specified by bus/unit or index.
1018 * If none was specified, try to find the first free one. */
1019 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
1020 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
1021 index = qemu_opt_get_number(legacy_opts, "index", -1);
1022
1023 max_devs = if_max_devs[type];
1024
1025 if (index != -1) {
1026 if (bus_id != 0 || unit_id != -1) {
1027 error_report("index cannot be used with bus and unit");
1028 goto fail;
1029 }
1030 bus_id = drive_index_to_bus_id(type, index);
1031 unit_id = drive_index_to_unit_id(type, index);
1032 }
1033
1034 if (unit_id == -1) {
1035 unit_id = 0;
1036 while (drive_get(type, bus_id, unit_id) != NULL) {
1037 unit_id++;
1038 if (max_devs && unit_id >= max_devs) {
1039 unit_id -= max_devs;
1040 bus_id++;
1041 }
1042 }
1043 }
1044
1045 if (max_devs && unit_id >= max_devs) {
1046 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
1047 goto fail;
1048 }
1049
1050 if (drive_get(type, bus_id, unit_id) != NULL) {
1051 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1052 bus_id, unit_id, index);
1053 goto fail;
1054 }
1055
1056 /* Serial number */
1057 serial = qemu_opt_get(legacy_opts, "serial");
1058
1059 /* no id supplied -> create one */
1060 if (qemu_opts_id(all_opts) == NULL) {
1061 char *new_id;
1062 const char *mediastr = "";
1063 if (type == IF_IDE || type == IF_SCSI) {
1064 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1065 }
1066 if (max_devs) {
1067 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1068 mediastr, unit_id);
1069 } else {
1070 new_id = g_strdup_printf("%s%s%i", if_name[type],
1071 mediastr, unit_id);
1072 }
1073 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1074 g_free(new_id);
1075 }
1076
1077 /* Add virtio block device */
1078 devaddr = qemu_opt_get(legacy_opts, "addr");
1079 if (devaddr && type != IF_VIRTIO) {
1080 error_report("addr is not supported by this bus type");
1081 goto fail;
1082 }
1083
1084 if (type == IF_VIRTIO) {
1085 QemuOpts *devopts;
1086 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1087 &error_abort);
1088 if (arch_type == QEMU_ARCH_S390X) {
1089 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1090 } else {
1091 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1092 }
1093 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1094 &error_abort);
1095 if (devaddr) {
1096 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1097 }
1098 }
1099
1100 filename = qemu_opt_get(legacy_opts, "file");
1101
1102 /* Check werror/rerror compatibility with if=... */
1103 werror = qemu_opt_get(legacy_opts, "werror");
1104 if (werror != NULL) {
1105 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1106 type != IF_NONE) {
1107 error_report("werror is not supported by this bus type");
1108 goto fail;
1109 }
1110 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1111 }
1112
1113 rerror = qemu_opt_get(legacy_opts, "rerror");
1114 if (rerror != NULL) {
1115 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1116 type != IF_NONE) {
1117 error_report("rerror is not supported by this bus type");
1118 goto fail;
1119 }
1120 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1121 }
1122
1123 /* Actual block device init: Functionality shared with blockdev-add */
1124 blk = blockdev_init(filename, bs_opts, &local_err);
1125 bs_opts = NULL;
1126 if (!blk) {
1127 if (local_err) {
1128 error_report_err(local_err);
1129 }
1130 goto fail;
1131 } else {
1132 assert(!local_err);
1133 }
1134
1135 /* Create legacy DriveInfo */
1136 dinfo = g_malloc0(sizeof(*dinfo));
1137 dinfo->opts = all_opts;
1138
1139 dinfo->cyls = cyls;
1140 dinfo->heads = heads;
1141 dinfo->secs = secs;
1142 dinfo->trans = translation;
1143
1144 dinfo->type = type;
1145 dinfo->bus = bus_id;
1146 dinfo->unit = unit_id;
1147 dinfo->devaddr = devaddr;
1148 dinfo->serial = g_strdup(serial);
1149
1150 blk_set_legacy_dinfo(blk, dinfo);
1151
1152 switch(type) {
1153 case IF_IDE:
1154 case IF_SCSI:
1155 case IF_XEN:
1156 case IF_NONE:
1157 dinfo->media_cd = media == MEDIA_CDROM;
1158 break;
1159 default:
1160 break;
1161 }
1162
1163 fail:
1164 qemu_opts_del(legacy_opts);
1165 QDECREF(bs_opts);
1166 return dinfo;
1167 }
1168
1169 void hmp_commit(Monitor *mon, const QDict *qdict)
1170 {
1171 const char *device = qdict_get_str(qdict, "device");
1172 BlockBackend *blk;
1173 int ret;
1174
1175 if (!strcmp(device, "all")) {
1176 ret = bdrv_commit_all();
1177 } else {
1178 BlockDriverState *bs;
1179 AioContext *aio_context;
1180
1181 blk = blk_by_name(device);
1182 if (!blk) {
1183 monitor_printf(mon, "Device '%s' not found\n", device);
1184 return;
1185 }
1186 if (!blk_is_available(blk)) {
1187 monitor_printf(mon, "Device '%s' has no medium\n", device);
1188 return;
1189 }
1190
1191 bs = blk_bs(blk);
1192 aio_context = bdrv_get_aio_context(bs);
1193 aio_context_acquire(aio_context);
1194
1195 ret = bdrv_commit(bs);
1196
1197 aio_context_release(aio_context);
1198 }
1199 if (ret < 0) {
1200 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1201 strerror(-ret));
1202 }
1203 }
1204
1205 static void blockdev_do_action(TransactionActionKind type, void *data,
1206 Error **errp)
1207 {
1208 TransactionAction action;
1209 TransactionActionList list;
1210
1211 action.type = type;
1212 action.u.data = data;
1213 list.value = &action;
1214 list.next = NULL;
1215 qmp_transaction(&list, false, NULL, errp);
1216 }
1217
1218 void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1219 bool has_node_name, const char *node_name,
1220 const char *snapshot_file,
1221 bool has_snapshot_node_name,
1222 const char *snapshot_node_name,
1223 bool has_format, const char *format,
1224 bool has_mode, NewImageMode mode, Error **errp)
1225 {
1226 BlockdevSnapshotSync snapshot = {
1227 .has_device = has_device,
1228 .device = (char *) device,
1229 .has_node_name = has_node_name,
1230 .node_name = (char *) node_name,
1231 .snapshot_file = (char *) snapshot_file,
1232 .has_snapshot_node_name = has_snapshot_node_name,
1233 .snapshot_node_name = (char *) snapshot_node_name,
1234 .has_format = has_format,
1235 .format = (char *) format,
1236 .has_mode = has_mode,
1237 .mode = mode,
1238 };
1239 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1240 &snapshot, errp);
1241 }
1242
1243 void qmp_blockdev_snapshot(const char *node, const char *overlay,
1244 Error **errp)
1245 {
1246 BlockdevSnapshot snapshot_data = {
1247 .node = (char *) node,
1248 .overlay = (char *) overlay
1249 };
1250
1251 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
1252 &snapshot_data, errp);
1253 }
1254
1255 void qmp_blockdev_snapshot_internal_sync(const char *device,
1256 const char *name,
1257 Error **errp)
1258 {
1259 BlockdevSnapshotInternal snapshot = {
1260 .device = (char *) device,
1261 .name = (char *) name
1262 };
1263
1264 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1265 &snapshot, errp);
1266 }
1267
1268 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1269 bool has_id,
1270 const char *id,
1271 bool has_name,
1272 const char *name,
1273 Error **errp)
1274 {
1275 BlockDriverState *bs;
1276 BlockBackend *blk;
1277 AioContext *aio_context;
1278 QEMUSnapshotInfo sn;
1279 Error *local_err = NULL;
1280 SnapshotInfo *info = NULL;
1281 int ret;
1282
1283 blk = blk_by_name(device);
1284 if (!blk) {
1285 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1286 "Device '%s' not found", device);
1287 return NULL;
1288 }
1289
1290 aio_context = blk_get_aio_context(blk);
1291 aio_context_acquire(aio_context);
1292
1293 if (!has_id) {
1294 id = NULL;
1295 }
1296
1297 if (!has_name) {
1298 name = NULL;
1299 }
1300
1301 if (!id && !name) {
1302 error_setg(errp, "Name or id must be provided");
1303 goto out_aio_context;
1304 }
1305
1306 if (!blk_is_available(blk)) {
1307 error_setg(errp, "Device '%s' has no medium", device);
1308 goto out_aio_context;
1309 }
1310 bs = blk_bs(blk);
1311
1312 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1313 goto out_aio_context;
1314 }
1315
1316 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1317 if (local_err) {
1318 error_propagate(errp, local_err);
1319 goto out_aio_context;
1320 }
1321 if (!ret) {
1322 error_setg(errp,
1323 "Snapshot with id '%s' and name '%s' does not exist on "
1324 "device '%s'",
1325 STR_OR_NULL(id), STR_OR_NULL(name), device);
1326 goto out_aio_context;
1327 }
1328
1329 bdrv_snapshot_delete(bs, id, name, &local_err);
1330 if (local_err) {
1331 error_propagate(errp, local_err);
1332 goto out_aio_context;
1333 }
1334
1335 aio_context_release(aio_context);
1336
1337 info = g_new0(SnapshotInfo, 1);
1338 info->id = g_strdup(sn.id_str);
1339 info->name = g_strdup(sn.name);
1340 info->date_nsec = sn.date_nsec;
1341 info->date_sec = sn.date_sec;
1342 info->vm_state_size = sn.vm_state_size;
1343 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1344 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1345
1346 return info;
1347
1348 out_aio_context:
1349 aio_context_release(aio_context);
1350 return NULL;
1351 }
1352
1353 /**
1354 * block_dirty_bitmap_lookup:
1355 * Return a dirty bitmap (if present), after validating
1356 * the node reference and bitmap names.
1357 *
1358 * @node: The name of the BDS node to search for bitmaps
1359 * @name: The name of the bitmap to search for
1360 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1361 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1362 * @errp: Output pointer for error information. Can be NULL.
1363 *
1364 * @return: A bitmap object on success, or NULL on failure.
1365 */
1366 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1367 const char *name,
1368 BlockDriverState **pbs,
1369 AioContext **paio,
1370 Error **errp)
1371 {
1372 BlockDriverState *bs;
1373 BdrvDirtyBitmap *bitmap;
1374 AioContext *aio_context;
1375
1376 if (!node) {
1377 error_setg(errp, "Node cannot be NULL");
1378 return NULL;
1379 }
1380 if (!name) {
1381 error_setg(errp, "Bitmap name cannot be NULL");
1382 return NULL;
1383 }
1384 bs = bdrv_lookup_bs(node, node, NULL);
1385 if (!bs) {
1386 error_setg(errp, "Node '%s' not found", node);
1387 return NULL;
1388 }
1389
1390 aio_context = bdrv_get_aio_context(bs);
1391 aio_context_acquire(aio_context);
1392
1393 bitmap = bdrv_find_dirty_bitmap(bs, name);
1394 if (!bitmap) {
1395 error_setg(errp, "Dirty bitmap '%s' not found", name);
1396 goto fail;
1397 }
1398
1399 if (pbs) {
1400 *pbs = bs;
1401 }
1402 if (paio) {
1403 *paio = aio_context;
1404 } else {
1405 aio_context_release(aio_context);
1406 }
1407
1408 return bitmap;
1409
1410 fail:
1411 aio_context_release(aio_context);
1412 return NULL;
1413 }
1414
1415 /* New and old BlockDriverState structs for atomic group operations */
1416
1417 typedef struct BlkActionState BlkActionState;
1418
1419 /**
1420 * BlkActionOps:
1421 * Table of operations that define an Action.
1422 *
1423 * @instance_size: Size of state struct, in bytes.
1424 * @prepare: Prepare the work, must NOT be NULL.
1425 * @commit: Commit the changes, can be NULL.
1426 * @abort: Abort the changes on fail, can be NULL.
1427 * @clean: Clean up resources after all transaction actions have called
1428 * commit() or abort(). Can be NULL.
1429 *
1430 * Only prepare() may fail. In a single transaction, only one of commit() or
1431 * abort() will be called. clean() will always be called if it is present.
1432 */
1433 typedef struct BlkActionOps {
1434 size_t instance_size;
1435 void (*prepare)(BlkActionState *common, Error **errp);
1436 void (*commit)(BlkActionState *common);
1437 void (*abort)(BlkActionState *common);
1438 void (*clean)(BlkActionState *common);
1439 } BlkActionOps;
1440
1441 /**
1442 * BlkActionState:
1443 * Describes one Action's state within a Transaction.
1444 *
1445 * @action: QAPI-defined enum identifying which Action to perform.
1446 * @ops: Table of ActionOps this Action can perform.
1447 * @block_job_txn: Transaction which this action belongs to.
1448 * @entry: List membership for all Actions in this Transaction.
1449 *
1450 * This structure must be arranged as first member in a subclassed type,
1451 * assuming that the compiler will also arrange it to the same offsets as the
1452 * base class.
1453 */
1454 struct BlkActionState {
1455 TransactionAction *action;
1456 const BlkActionOps *ops;
1457 BlockJobTxn *block_job_txn;
1458 TransactionProperties *txn_props;
1459 QSIMPLEQ_ENTRY(BlkActionState) entry;
1460 };
1461
1462 /* internal snapshot private data */
1463 typedef struct InternalSnapshotState {
1464 BlkActionState common;
1465 BlockDriverState *bs;
1466 AioContext *aio_context;
1467 QEMUSnapshotInfo sn;
1468 bool created;
1469 } InternalSnapshotState;
1470
1471
1472 static int action_check_completion_mode(BlkActionState *s, Error **errp)
1473 {
1474 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
1475 error_setg(errp,
1476 "Action '%s' does not support Transaction property "
1477 "completion-mode = %s",
1478 TransactionActionKind_lookup[s->action->type],
1479 ActionCompletionMode_lookup[s->txn_props->completion_mode]);
1480 return -1;
1481 }
1482 return 0;
1483 }
1484
1485 static void internal_snapshot_prepare(BlkActionState *common,
1486 Error **errp)
1487 {
1488 Error *local_err = NULL;
1489 const char *device;
1490 const char *name;
1491 BlockBackend *blk;
1492 BlockDriverState *bs;
1493 QEMUSnapshotInfo old_sn, *sn;
1494 bool ret;
1495 qemu_timeval tv;
1496 BlockdevSnapshotInternal *internal;
1497 InternalSnapshotState *state;
1498 int ret1;
1499
1500 g_assert(common->action->type ==
1501 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1502 internal = common->action->u.blockdev_snapshot_internal_sync;
1503 state = DO_UPCAST(InternalSnapshotState, common, common);
1504
1505 /* 1. parse input */
1506 device = internal->device;
1507 name = internal->name;
1508
1509 /* 2. check for validation */
1510 if (action_check_completion_mode(common, errp) < 0) {
1511 return;
1512 }
1513
1514 blk = blk_by_name(device);
1515 if (!blk) {
1516 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1517 "Device '%s' not found", device);
1518 return;
1519 }
1520
1521 /* AioContext is released in .clean() */
1522 state->aio_context = blk_get_aio_context(blk);
1523 aio_context_acquire(state->aio_context);
1524
1525 if (!blk_is_available(blk)) {
1526 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1527 return;
1528 }
1529 bs = blk_bs(blk);
1530
1531 state->bs = bs;
1532 bdrv_drained_begin(bs);
1533
1534 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1535 return;
1536 }
1537
1538 if (bdrv_is_read_only(bs)) {
1539 error_setg(errp, "Device '%s' is read only", device);
1540 return;
1541 }
1542
1543 if (!bdrv_can_snapshot(bs)) {
1544 error_setg(errp, "Block format '%s' used by device '%s' "
1545 "does not support internal snapshots",
1546 bs->drv->format_name, device);
1547 return;
1548 }
1549
1550 if (!strlen(name)) {
1551 error_setg(errp, "Name is empty");
1552 return;
1553 }
1554
1555 /* check whether a snapshot with name exist */
1556 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1557 &local_err);
1558 if (local_err) {
1559 error_propagate(errp, local_err);
1560 return;
1561 } else if (ret) {
1562 error_setg(errp,
1563 "Snapshot with name '%s' already exists on device '%s'",
1564 name, device);
1565 return;
1566 }
1567
1568 /* 3. take the snapshot */
1569 sn = &state->sn;
1570 pstrcpy(sn->name, sizeof(sn->name), name);
1571 qemu_gettimeofday(&tv);
1572 sn->date_sec = tv.tv_sec;
1573 sn->date_nsec = tv.tv_usec * 1000;
1574 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1575
1576 ret1 = bdrv_snapshot_create(bs, sn);
1577 if (ret1 < 0) {
1578 error_setg_errno(errp, -ret1,
1579 "Failed to create snapshot '%s' on device '%s'",
1580 name, device);
1581 return;
1582 }
1583
1584 /* 4. succeed, mark a snapshot is created */
1585 state->created = true;
1586 }
1587
1588 static void internal_snapshot_abort(BlkActionState *common)
1589 {
1590 InternalSnapshotState *state =
1591 DO_UPCAST(InternalSnapshotState, common, common);
1592 BlockDriverState *bs = state->bs;
1593 QEMUSnapshotInfo *sn = &state->sn;
1594 Error *local_error = NULL;
1595
1596 if (!state->created) {
1597 return;
1598 }
1599
1600 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1601 error_reportf_err(local_error,
1602 "Failed to delete snapshot with id '%s' and "
1603 "name '%s' on device '%s' in abort: ",
1604 sn->id_str, sn->name,
1605 bdrv_get_device_name(bs));
1606 }
1607 }
1608
1609 static void internal_snapshot_clean(BlkActionState *common)
1610 {
1611 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1612 common, common);
1613
1614 if (state->aio_context) {
1615 if (state->bs) {
1616 bdrv_drained_end(state->bs);
1617 }
1618 aio_context_release(state->aio_context);
1619 }
1620 }
1621
1622 /* external snapshot private data */
1623 typedef struct ExternalSnapshotState {
1624 BlkActionState common;
1625 BlockDriverState *old_bs;
1626 BlockDriverState *new_bs;
1627 AioContext *aio_context;
1628 } ExternalSnapshotState;
1629
1630 static void external_snapshot_prepare(BlkActionState *common,
1631 Error **errp)
1632 {
1633 int flags = 0, ret;
1634 QDict *options = NULL;
1635 Error *local_err = NULL;
1636 /* Device and node name of the image to generate the snapshot from */
1637 const char *device;
1638 const char *node_name;
1639 /* Reference to the new image (for 'blockdev-snapshot') */
1640 const char *snapshot_ref;
1641 /* File name of the new image (for 'blockdev-snapshot-sync') */
1642 const char *new_image_file;
1643 ExternalSnapshotState *state =
1644 DO_UPCAST(ExternalSnapshotState, common, common);
1645 TransactionAction *action = common->action;
1646
1647 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1648 * purpose but a different set of parameters */
1649 switch (action->type) {
1650 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
1651 {
1652 BlockdevSnapshot *s = action->u.blockdev_snapshot;
1653 device = s->node;
1654 node_name = s->node;
1655 new_image_file = NULL;
1656 snapshot_ref = s->overlay;
1657 }
1658 break;
1659 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
1660 {
1661 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1662 device = s->has_device ? s->device : NULL;
1663 node_name = s->has_node_name ? s->node_name : NULL;
1664 new_image_file = s->snapshot_file;
1665 snapshot_ref = NULL;
1666 }
1667 break;
1668 default:
1669 g_assert_not_reached();
1670 }
1671
1672 /* start processing */
1673 if (action_check_completion_mode(common, errp) < 0) {
1674 return;
1675 }
1676
1677 state->old_bs = bdrv_lookup_bs(device, node_name, errp);
1678 if (!state->old_bs) {
1679 return;
1680 }
1681
1682 /* Acquire AioContext now so any threads operating on old_bs stop */
1683 state->aio_context = bdrv_get_aio_context(state->old_bs);
1684 aio_context_acquire(state->aio_context);
1685 bdrv_drained_begin(state->old_bs);
1686
1687 if (!bdrv_is_inserted(state->old_bs)) {
1688 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1689 return;
1690 }
1691
1692 if (bdrv_op_is_blocked(state->old_bs,
1693 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1694 return;
1695 }
1696
1697 if (!bdrv_is_read_only(state->old_bs)) {
1698 if (bdrv_flush(state->old_bs)) {
1699 error_setg(errp, QERR_IO_ERROR);
1700 return;
1701 }
1702 }
1703
1704 if (!bdrv_is_first_non_filter(state->old_bs)) {
1705 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1706 return;
1707 }
1708
1709 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
1710 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync;
1711 const char *format = s->has_format ? s->format : "qcow2";
1712 enum NewImageMode mode;
1713 const char *snapshot_node_name =
1714 s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
1715
1716 if (node_name && !snapshot_node_name) {
1717 error_setg(errp, "New snapshot node name missing");
1718 return;
1719 }
1720
1721 if (snapshot_node_name &&
1722 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1723 error_setg(errp, "New snapshot node name already in use");
1724 return;
1725 }
1726
1727 flags = state->old_bs->open_flags;
1728
1729 /* create new image w/backing file */
1730 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1731 if (mode != NEW_IMAGE_MODE_EXISTING) {
1732 bdrv_img_create(new_image_file, format,
1733 state->old_bs->filename,
1734 state->old_bs->drv->format_name,
1735 NULL, -1, flags, &local_err, false);
1736 if (local_err) {
1737 error_propagate(errp, local_err);
1738 return;
1739 }
1740 }
1741
1742 options = qdict_new();
1743 if (s->has_snapshot_node_name) {
1744 qdict_put(options, "node-name",
1745 qstring_from_str(snapshot_node_name));
1746 }
1747 qdict_put(options, "driver", qstring_from_str(format));
1748
1749 flags |= BDRV_O_NO_BACKING;
1750 }
1751
1752 assert(state->new_bs == NULL);
1753 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options,
1754 flags, errp);
1755 /* We will manually add the backing_hd field to the bs later */
1756 if (ret != 0) {
1757 return;
1758 }
1759
1760 if (state->new_bs->blk != NULL) {
1761 error_setg(errp, "The snapshot is already in use by %s",
1762 blk_name(state->new_bs->blk));
1763 return;
1764 }
1765
1766 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT,
1767 errp)) {
1768 return;
1769 }
1770
1771 if (state->new_bs->backing != NULL) {
1772 error_setg(errp, "The snapshot already has a backing image");
1773 return;
1774 }
1775
1776 if (!state->new_bs->drv->supports_backing) {
1777 error_setg(errp, "The snapshot does not support backing images");
1778 }
1779 }
1780
1781 static void external_snapshot_commit(BlkActionState *common)
1782 {
1783 ExternalSnapshotState *state =
1784 DO_UPCAST(ExternalSnapshotState, common, common);
1785
1786 bdrv_set_aio_context(state->new_bs, state->aio_context);
1787
1788 /* This removes our old bs and adds the new bs */
1789 bdrv_append(state->new_bs, state->old_bs);
1790 /* We don't need (or want) to use the transactional
1791 * bdrv_reopen_multiple() across all the entries at once, because we
1792 * don't want to abort all of them if one of them fails the reopen */
1793 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1794 NULL);
1795 }
1796
1797 static void external_snapshot_abort(BlkActionState *common)
1798 {
1799 ExternalSnapshotState *state =
1800 DO_UPCAST(ExternalSnapshotState, common, common);
1801 if (state->new_bs) {
1802 bdrv_unref(state->new_bs);
1803 }
1804 }
1805
1806 static void external_snapshot_clean(BlkActionState *common)
1807 {
1808 ExternalSnapshotState *state =
1809 DO_UPCAST(ExternalSnapshotState, common, common);
1810 if (state->aio_context) {
1811 bdrv_drained_end(state->old_bs);
1812 aio_context_release(state->aio_context);
1813 }
1814 }
1815
1816 typedef struct DriveBackupState {
1817 BlkActionState common;
1818 BlockDriverState *bs;
1819 AioContext *aio_context;
1820 BlockJob *job;
1821 } DriveBackupState;
1822
1823 static void do_drive_backup(const char *device, const char *target,
1824 bool has_format, const char *format,
1825 enum MirrorSyncMode sync,
1826 bool has_mode, enum NewImageMode mode,
1827 bool has_speed, int64_t speed,
1828 bool has_bitmap, const char *bitmap,
1829 bool has_on_source_error,
1830 BlockdevOnError on_source_error,
1831 bool has_on_target_error,
1832 BlockdevOnError on_target_error,
1833 BlockJobTxn *txn, Error **errp);
1834
1835 static void drive_backup_prepare(BlkActionState *common, Error **errp)
1836 {
1837 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1838 BlockBackend *blk;
1839 DriveBackup *backup;
1840 Error *local_err = NULL;
1841
1842 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1843 backup = common->action->u.drive_backup;
1844
1845 blk = blk_by_name(backup->device);
1846 if (!blk) {
1847 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1848 "Device '%s' not found", backup->device);
1849 return;
1850 }
1851
1852 if (!blk_is_available(blk)) {
1853 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1854 return;
1855 }
1856
1857 /* AioContext is released in .clean() */
1858 state->aio_context = blk_get_aio_context(blk);
1859 aio_context_acquire(state->aio_context);
1860 bdrv_drained_begin(blk_bs(blk));
1861 state->bs = blk_bs(blk);
1862
1863 do_drive_backup(backup->device, backup->target,
1864 backup->has_format, backup->format,
1865 backup->sync,
1866 backup->has_mode, backup->mode,
1867 backup->has_speed, backup->speed,
1868 backup->has_bitmap, backup->bitmap,
1869 backup->has_on_source_error, backup->on_source_error,
1870 backup->has_on_target_error, backup->on_target_error,
1871 common->block_job_txn, &local_err);
1872 if (local_err) {
1873 error_propagate(errp, local_err);
1874 return;
1875 }
1876
1877 state->job = state->bs->job;
1878 }
1879
1880 static void drive_backup_abort(BlkActionState *common)
1881 {
1882 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1883 BlockDriverState *bs = state->bs;
1884
1885 /* Only cancel if it's the job we started */
1886 if (bs && bs->job && bs->job == state->job) {
1887 block_job_cancel_sync(bs->job);
1888 }
1889 }
1890
1891 static void drive_backup_clean(BlkActionState *common)
1892 {
1893 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1894
1895 if (state->aio_context) {
1896 bdrv_drained_end(state->bs);
1897 aio_context_release(state->aio_context);
1898 }
1899 }
1900
1901 typedef struct BlockdevBackupState {
1902 BlkActionState common;
1903 BlockDriverState *bs;
1904 BlockJob *job;
1905 AioContext *aio_context;
1906 } BlockdevBackupState;
1907
1908 static void do_blockdev_backup(const char *device, const char *target,
1909 enum MirrorSyncMode sync,
1910 bool has_speed, int64_t speed,
1911 bool has_on_source_error,
1912 BlockdevOnError on_source_error,
1913 bool has_on_target_error,
1914 BlockdevOnError on_target_error,
1915 BlockJobTxn *txn, Error **errp);
1916
1917 static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
1918 {
1919 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1920 BlockdevBackup *backup;
1921 BlockBackend *blk, *target;
1922 Error *local_err = NULL;
1923
1924 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1925 backup = common->action->u.blockdev_backup;
1926
1927 blk = blk_by_name(backup->device);
1928 if (!blk) {
1929 error_setg(errp, "Device '%s' not found", backup->device);
1930 return;
1931 }
1932
1933 if (!blk_is_available(blk)) {
1934 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1935 return;
1936 }
1937
1938 target = blk_by_name(backup->target);
1939 if (!target) {
1940 error_setg(errp, "Device '%s' not found", backup->target);
1941 return;
1942 }
1943
1944 /* AioContext is released in .clean() */
1945 state->aio_context = blk_get_aio_context(blk);
1946 if (state->aio_context != blk_get_aio_context(target)) {
1947 state->aio_context = NULL;
1948 error_setg(errp, "Backup between two IO threads is not implemented");
1949 return;
1950 }
1951 aio_context_acquire(state->aio_context);
1952 state->bs = blk_bs(blk);
1953 bdrv_drained_begin(state->bs);
1954
1955 do_blockdev_backup(backup->device, backup->target,
1956 backup->sync,
1957 backup->has_speed, backup->speed,
1958 backup->has_on_source_error, backup->on_source_error,
1959 backup->has_on_target_error, backup->on_target_error,
1960 common->block_job_txn, &local_err);
1961 if (local_err) {
1962 error_propagate(errp, local_err);
1963 return;
1964 }
1965
1966 state->job = state->bs->job;
1967 }
1968
1969 static void blockdev_backup_abort(BlkActionState *common)
1970 {
1971 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1972 BlockDriverState *bs = state->bs;
1973
1974 /* Only cancel if it's the job we started */
1975 if (bs && bs->job && bs->job == state->job) {
1976 block_job_cancel_sync(bs->job);
1977 }
1978 }
1979
1980 static void blockdev_backup_clean(BlkActionState *common)
1981 {
1982 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1983
1984 if (state->aio_context) {
1985 bdrv_drained_end(state->bs);
1986 aio_context_release(state->aio_context);
1987 }
1988 }
1989
1990 typedef struct BlockDirtyBitmapState {
1991 BlkActionState common;
1992 BdrvDirtyBitmap *bitmap;
1993 BlockDriverState *bs;
1994 AioContext *aio_context;
1995 HBitmap *backup;
1996 bool prepared;
1997 } BlockDirtyBitmapState;
1998
1999 static void block_dirty_bitmap_add_prepare(BlkActionState *common,
2000 Error **errp)
2001 {
2002 Error *local_err = NULL;
2003 BlockDirtyBitmapAdd *action;
2004 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2005 common, common);
2006
2007 if (action_check_completion_mode(common, errp) < 0) {
2008 return;
2009 }
2010
2011 action = common->action->u.block_dirty_bitmap_add;
2012 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2013 qmp_block_dirty_bitmap_add(action->node, action->name,
2014 action->has_granularity, action->granularity,
2015 &local_err);
2016
2017 if (!local_err) {
2018 state->prepared = true;
2019 } else {
2020 error_propagate(errp, local_err);
2021 }
2022 }
2023
2024 static void block_dirty_bitmap_add_abort(BlkActionState *common)
2025 {
2026 BlockDirtyBitmapAdd *action;
2027 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2028 common, common);
2029
2030 action = common->action->u.block_dirty_bitmap_add;
2031 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2032 * then the node reference and bitmap name must have been valid.
2033 */
2034 if (state->prepared) {
2035 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
2036 }
2037 }
2038
2039 static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
2040 Error **errp)
2041 {
2042 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2043 common, common);
2044 BlockDirtyBitmap *action;
2045
2046 if (action_check_completion_mode(common, errp) < 0) {
2047 return;
2048 }
2049
2050 action = common->action->u.block_dirty_bitmap_clear;
2051 state->bitmap = block_dirty_bitmap_lookup(action->node,
2052 action->name,
2053 &state->bs,
2054 &state->aio_context,
2055 errp);
2056 if (!state->bitmap) {
2057 return;
2058 }
2059
2060 if (bdrv_dirty_bitmap_frozen(state->bitmap)) {
2061 error_setg(errp, "Cannot modify a frozen bitmap");
2062 return;
2063 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) {
2064 error_setg(errp, "Cannot clear a disabled bitmap");
2065 return;
2066 }
2067
2068 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
2069 /* AioContext is released in .clean() */
2070 }
2071
2072 static void block_dirty_bitmap_clear_abort(BlkActionState *common)
2073 {
2074 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2075 common, common);
2076
2077 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup);
2078 }
2079
2080 static void block_dirty_bitmap_clear_commit(BlkActionState *common)
2081 {
2082 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2083 common, common);
2084
2085 hbitmap_free(state->backup);
2086 }
2087
2088 static void block_dirty_bitmap_clear_clean(BlkActionState *common)
2089 {
2090 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
2091 common, common);
2092
2093 if (state->aio_context) {
2094 aio_context_release(state->aio_context);
2095 }
2096 }
2097
2098 static void abort_prepare(BlkActionState *common, Error **errp)
2099 {
2100 error_setg(errp, "Transaction aborted using Abort action");
2101 }
2102
2103 static void abort_commit(BlkActionState *common)
2104 {
2105 g_assert_not_reached(); /* this action never succeeds */
2106 }
2107
2108 static const BlkActionOps actions[] = {
2109 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
2110 .instance_size = sizeof(ExternalSnapshotState),
2111 .prepare = external_snapshot_prepare,
2112 .commit = external_snapshot_commit,
2113 .abort = external_snapshot_abort,
2114 .clean = external_snapshot_clean,
2115 },
2116 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
2117 .instance_size = sizeof(ExternalSnapshotState),
2118 .prepare = external_snapshot_prepare,
2119 .commit = external_snapshot_commit,
2120 .abort = external_snapshot_abort,
2121 .clean = external_snapshot_clean,
2122 },
2123 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
2124 .instance_size = sizeof(DriveBackupState),
2125 .prepare = drive_backup_prepare,
2126 .abort = drive_backup_abort,
2127 .clean = drive_backup_clean,
2128 },
2129 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
2130 .instance_size = sizeof(BlockdevBackupState),
2131 .prepare = blockdev_backup_prepare,
2132 .abort = blockdev_backup_abort,
2133 .clean = blockdev_backup_clean,
2134 },
2135 [TRANSACTION_ACTION_KIND_ABORT] = {
2136 .instance_size = sizeof(BlkActionState),
2137 .prepare = abort_prepare,
2138 .commit = abort_commit,
2139 },
2140 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
2141 .instance_size = sizeof(InternalSnapshotState),
2142 .prepare = internal_snapshot_prepare,
2143 .abort = internal_snapshot_abort,
2144 .clean = internal_snapshot_clean,
2145 },
2146 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
2147 .instance_size = sizeof(BlockDirtyBitmapState),
2148 .prepare = block_dirty_bitmap_add_prepare,
2149 .abort = block_dirty_bitmap_add_abort,
2150 },
2151 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
2152 .instance_size = sizeof(BlockDirtyBitmapState),
2153 .prepare = block_dirty_bitmap_clear_prepare,
2154 .commit = block_dirty_bitmap_clear_commit,
2155 .abort = block_dirty_bitmap_clear_abort,
2156 .clean = block_dirty_bitmap_clear_clean,
2157 }
2158 };
2159
2160 /**
2161 * Allocate a TransactionProperties structure if necessary, and fill
2162 * that structure with desired defaults if they are unset.
2163 */
2164 static TransactionProperties *get_transaction_properties(
2165 TransactionProperties *props)
2166 {
2167 if (!props) {
2168 props = g_new0(TransactionProperties, 1);
2169 }
2170
2171 if (!props->has_completion_mode) {
2172 props->has_completion_mode = true;
2173 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
2174 }
2175
2176 return props;
2177 }
2178
2179 /*
2180 * 'Atomic' group operations. The operations are performed as a set, and if
2181 * any fail then we roll back all operations in the group.
2182 */
2183 void qmp_transaction(TransactionActionList *dev_list,
2184 bool has_props,
2185 struct TransactionProperties *props,
2186 Error **errp)
2187 {
2188 TransactionActionList *dev_entry = dev_list;
2189 BlockJobTxn *block_job_txn = NULL;
2190 BlkActionState *state, *next;
2191 Error *local_err = NULL;
2192
2193 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states;
2194 QSIMPLEQ_INIT(&snap_bdrv_states);
2195
2196 /* Does this transaction get canceled as a group on failure?
2197 * If not, we don't really need to make a BlockJobTxn.
2198 */
2199 props = get_transaction_properties(props);
2200 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
2201 block_job_txn = block_job_txn_new();
2202 }
2203
2204 /* drain all i/o before any operations */
2205 bdrv_drain_all();
2206
2207 /* We don't do anything in this loop that commits us to the operations */
2208 while (NULL != dev_entry) {
2209 TransactionAction *dev_info = NULL;
2210 const BlkActionOps *ops;
2211
2212 dev_info = dev_entry->value;
2213 dev_entry = dev_entry->next;
2214
2215 assert(dev_info->type < ARRAY_SIZE(actions));
2216
2217 ops = &actions[dev_info->type];
2218 assert(ops->instance_size > 0);
2219
2220 state = g_malloc0(ops->instance_size);
2221 state->ops = ops;
2222 state->action = dev_info;
2223 state->block_job_txn = block_job_txn;
2224 state->txn_props = props;
2225 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
2226
2227 state->ops->prepare(state, &local_err);
2228 if (local_err) {
2229 error_propagate(errp, local_err);
2230 goto delete_and_fail;
2231 }
2232 }
2233
2234 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2235 if (state->ops->commit) {
2236 state->ops->commit(state);
2237 }
2238 }
2239
2240 /* success */
2241 goto exit;
2242
2243 delete_and_fail:
2244 /* failure, and it is all-or-none; roll back all operations */
2245 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
2246 if (state->ops->abort) {
2247 state->ops->abort(state);
2248 }
2249 }
2250 exit:
2251 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
2252 if (state->ops->clean) {
2253 state->ops->clean(state);
2254 }
2255 g_free(state);
2256 }
2257 if (!has_props) {
2258 qapi_free_TransactionProperties(props);
2259 }
2260 block_job_txn_unref(block_job_txn);
2261 }
2262
2263 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
2264 {
2265 Error *local_err = NULL;
2266
2267 qmp_blockdev_open_tray(device, has_force, force, &local_err);
2268 if (local_err) {
2269 error_propagate(errp, local_err);
2270 return;
2271 }
2272
2273 qmp_x_blockdev_remove_medium(device, errp);
2274 }
2275
2276 void qmp_block_passwd(bool has_device, const char *device,
2277 bool has_node_name, const char *node_name,
2278 const char *password, Error **errp)
2279 {
2280 Error *local_err = NULL;
2281 BlockDriverState *bs;
2282 AioContext *aio_context;
2283
2284 bs = bdrv_lookup_bs(has_device ? device : NULL,
2285 has_node_name ? node_name : NULL,
2286 &local_err);
2287 if (local_err) {
2288 error_propagate(errp, local_err);
2289 return;
2290 }
2291
2292 aio_context = bdrv_get_aio_context(bs);
2293 aio_context_acquire(aio_context);
2294
2295 bdrv_add_key(bs, password, errp);
2296
2297 aio_context_release(aio_context);
2298 }
2299
2300 void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
2301 Error **errp)
2302 {
2303 BlockBackend *blk;
2304 bool locked;
2305
2306 if (!has_force) {
2307 force = false;
2308 }
2309
2310 blk = blk_by_name(device);
2311 if (!blk) {
2312 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2313 "Device '%s' not found", device);
2314 return;
2315 }
2316
2317 if (!blk_dev_has_removable_media(blk)) {
2318 error_setg(errp, "Device '%s' is not removable", device);
2319 return;
2320 }
2321
2322 if (!blk_dev_has_tray(blk)) {
2323 /* Ignore this command on tray-less devices */
2324 return;
2325 }
2326
2327 if (blk_dev_is_tray_open(blk)) {
2328 return;
2329 }
2330
2331 locked = blk_dev_is_medium_locked(blk);
2332 if (locked) {
2333 blk_dev_eject_request(blk, force);
2334 }
2335
2336 if (!locked || force) {
2337 blk_dev_change_media_cb(blk, false);
2338 }
2339 }
2340
2341 void qmp_blockdev_close_tray(const char *device, Error **errp)
2342 {
2343 BlockBackend *blk;
2344
2345 blk = blk_by_name(device);
2346 if (!blk) {
2347 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2348 "Device '%s' not found", device);
2349 return;
2350 }
2351
2352 if (!blk_dev_has_removable_media(blk)) {
2353 error_setg(errp, "Device '%s' is not removable", device);
2354 return;
2355 }
2356
2357 if (!blk_dev_has_tray(blk)) {
2358 /* Ignore this command on tray-less devices */
2359 return;
2360 }
2361
2362 if (!blk_dev_is_tray_open(blk)) {
2363 return;
2364 }
2365
2366 blk_dev_change_media_cb(blk, true);
2367 }
2368
2369 void qmp_x_blockdev_remove_medium(const char *device, Error **errp)
2370 {
2371 BlockBackend *blk;
2372 BlockDriverState *bs;
2373 AioContext *aio_context;
2374 bool has_device;
2375
2376 blk = blk_by_name(device);
2377 if (!blk) {
2378 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2379 "Device '%s' not found", device);
2380 return;
2381 }
2382
2383 /* For BBs without a device, we can exchange the BDS tree at will */
2384 has_device = blk_get_attached_dev(blk);
2385
2386 if (has_device && !blk_dev_has_removable_media(blk)) {
2387 error_setg(errp, "Device '%s' is not removable", device);
2388 return;
2389 }
2390
2391 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2392 error_setg(errp, "Tray of device '%s' is not open", device);
2393 return;
2394 }
2395
2396 bs = blk_bs(blk);
2397 if (!bs) {
2398 return;
2399 }
2400
2401 aio_context = bdrv_get_aio_context(bs);
2402 aio_context_acquire(aio_context);
2403
2404 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2405 goto out;
2406 }
2407
2408 /* This follows the convention established by bdrv_make_anon() */
2409 if (bs->device_list.tqe_prev) {
2410 bdrv_device_remove(bs);
2411 }
2412
2413 blk_remove_bs(blk);
2414
2415 if (!blk_dev_has_tray(blk)) {
2416 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2417 * called at all); therefore, the medium needs to be ejected here.
2418 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2419 * value passed here (i.e. false). */
2420 blk_dev_change_media_cb(blk, false);
2421 }
2422
2423 out:
2424 aio_context_release(aio_context);
2425 }
2426
2427 static void qmp_blockdev_insert_anon_medium(const char *device,
2428 BlockDriverState *bs, Error **errp)
2429 {
2430 BlockBackend *blk;
2431 bool has_device;
2432
2433 blk = blk_by_name(device);
2434 if (!blk) {
2435 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2436 "Device '%s' not found", device);
2437 return;
2438 }
2439
2440 /* For BBs without a device, we can exchange the BDS tree at will */
2441 has_device = blk_get_attached_dev(blk);
2442
2443 if (has_device && !blk_dev_has_removable_media(blk)) {
2444 error_setg(errp, "Device '%s' is not removable", device);
2445 return;
2446 }
2447
2448 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
2449 error_setg(errp, "Tray of device '%s' is not open", device);
2450 return;
2451 }
2452
2453 if (blk_bs(blk)) {
2454 error_setg(errp, "There already is a medium in device '%s'", device);
2455 return;
2456 }
2457
2458 blk_insert_bs(blk, bs);
2459
2460 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
2461
2462 if (!blk_dev_has_tray(blk)) {
2463 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2464 * called at all); therefore, the medium needs to be pushed into the
2465 * slot here.
2466 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2467 * value passed here (i.e. true). */
2468 blk_dev_change_media_cb(blk, true);
2469 }
2470 }
2471
2472 void qmp_x_blockdev_insert_medium(const char *device, const char *node_name,
2473 Error **errp)
2474 {
2475 BlockDriverState *bs;
2476
2477 bs = bdrv_find_node(node_name);
2478 if (!bs) {
2479 error_setg(errp, "Node '%s' not found", node_name);
2480 return;
2481 }
2482
2483 if (bs->blk) {
2484 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2485 blk_name(bs->blk));
2486 return;
2487 }
2488
2489 qmp_blockdev_insert_anon_medium(device, bs, errp);
2490 }
2491
2492 void qmp_blockdev_change_medium(const char *device, const char *filename,
2493 bool has_format, const char *format,
2494 bool has_read_only,
2495 BlockdevChangeReadOnlyMode read_only,
2496 Error **errp)
2497 {
2498 BlockBackend *blk;
2499 BlockDriverState *medium_bs = NULL;
2500 int bdrv_flags, ret;
2501 QDict *options = NULL;
2502 Error *err = NULL;
2503
2504 blk = blk_by_name(device);
2505 if (!blk) {
2506 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2507 "Device '%s' not found", device);
2508 goto fail;
2509 }
2510
2511 if (blk_bs(blk)) {
2512 blk_update_root_state(blk);
2513 }
2514
2515 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2516
2517 if (!has_read_only) {
2518 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2519 }
2520
2521 switch (read_only) {
2522 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2523 break;
2524
2525 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2526 bdrv_flags &= ~BDRV_O_RDWR;
2527 break;
2528
2529 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2530 bdrv_flags |= BDRV_O_RDWR;
2531 break;
2532
2533 default:
2534 abort();
2535 }
2536
2537 if (has_format) {
2538 options = qdict_new();
2539 qdict_put(options, "driver", qstring_from_str(format));
2540 }
2541
2542 assert(!medium_bs);
2543 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2544 if (ret < 0) {
2545 goto fail;
2546 }
2547
2548 blk_apply_root_state(blk, medium_bs);
2549
2550 bdrv_add_key(medium_bs, NULL, &err);
2551 if (err) {
2552 error_propagate(errp, err);
2553 goto fail;
2554 }
2555
2556 qmp_blockdev_open_tray(device, false, false, &err);
2557 if (err) {
2558 error_propagate(errp, err);
2559 goto fail;
2560 }
2561
2562 qmp_x_blockdev_remove_medium(device, &err);
2563 if (err) {
2564 error_propagate(errp, err);
2565 goto fail;
2566 }
2567
2568 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2569 if (err) {
2570 error_propagate(errp, err);
2571 goto fail;
2572 }
2573
2574 qmp_blockdev_close_tray(device, errp);
2575
2576 fail:
2577 /* If the medium has been inserted, the device has its own reference, so
2578 * ours must be relinquished; and if it has not been inserted successfully,
2579 * the reference must be relinquished anyway */
2580 bdrv_unref(medium_bs);
2581 }
2582
2583 /* throttling disk I/O limits */
2584 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
2585 int64_t bps_wr,
2586 int64_t iops,
2587 int64_t iops_rd,
2588 int64_t iops_wr,
2589 bool has_bps_max,
2590 int64_t bps_max,
2591 bool has_bps_rd_max,
2592 int64_t bps_rd_max,
2593 bool has_bps_wr_max,
2594 int64_t bps_wr_max,
2595 bool has_iops_max,
2596 int64_t iops_max,
2597 bool has_iops_rd_max,
2598 int64_t iops_rd_max,
2599 bool has_iops_wr_max,
2600 int64_t iops_wr_max,
2601 bool has_iops_size,
2602 int64_t iops_size,
2603 bool has_group,
2604 const char *group, Error **errp)
2605 {
2606 ThrottleConfig cfg;
2607 BlockDriverState *bs;
2608 BlockBackend *blk;
2609 AioContext *aio_context;
2610
2611 blk = blk_by_name(device);
2612 if (!blk) {
2613 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2614 "Device '%s' not found", device);
2615 return;
2616 }
2617
2618 aio_context = blk_get_aio_context(blk);
2619 aio_context_acquire(aio_context);
2620
2621 bs = blk_bs(blk);
2622 if (!bs) {
2623 error_setg(errp, "Device '%s' has no medium", device);
2624 goto out;
2625 }
2626
2627 throttle_config_init(&cfg);
2628 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2629 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2630 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2631
2632 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2633 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2634 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2635
2636 if (has_bps_max) {
2637 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2638 }
2639 if (has_bps_rd_max) {
2640 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2641 }
2642 if (has_bps_wr_max) {
2643 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2644 }
2645 if (has_iops_max) {
2646 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2647 }
2648 if (has_iops_rd_max) {
2649 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2650 }
2651 if (has_iops_wr_max) {
2652 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2653 }
2654
2655 if (has_iops_size) {
2656 cfg.op_size = iops_size;
2657 }
2658
2659 if (!throttle_is_valid(&cfg, errp)) {
2660 goto out;
2661 }
2662
2663 if (throttle_enabled(&cfg)) {
2664 /* Enable I/O limits if they're not enabled yet, otherwise
2665 * just update the throttling group. */
2666 if (!bs->throttle_state) {
2667 bdrv_io_limits_enable(bs, has_group ? group : device);
2668 } else if (has_group) {
2669 bdrv_io_limits_update_group(bs, group);
2670 }
2671 /* Set the new throttling configuration */
2672 bdrv_set_io_limits(bs, &cfg);
2673 } else if (bs->throttle_state) {
2674 /* If all throttling settings are set to 0, disable I/O limits */
2675 bdrv_io_limits_disable(bs);
2676 }
2677
2678 out:
2679 aio_context_release(aio_context);
2680 }
2681
2682 void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2683 bool has_granularity, uint32_t granularity,
2684 Error **errp)
2685 {
2686 AioContext *aio_context;
2687 BlockDriverState *bs;
2688
2689 if (!name || name[0] == '\0') {
2690 error_setg(errp, "Bitmap name cannot be empty");
2691 return;
2692 }
2693
2694 bs = bdrv_lookup_bs(node, node, errp);
2695 if (!bs) {
2696 return;
2697 }
2698
2699 aio_context = bdrv_get_aio_context(bs);
2700 aio_context_acquire(aio_context);
2701
2702 if (has_granularity) {
2703 if (granularity < 512 || !is_power_of_2(granularity)) {
2704 error_setg(errp, "Granularity must be power of 2 "
2705 "and at least 512");
2706 goto out;
2707 }
2708 } else {
2709 /* Default to cluster size, if available: */
2710 granularity = bdrv_get_default_bitmap_granularity(bs);
2711 }
2712
2713 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2714
2715 out:
2716 aio_context_release(aio_context);
2717 }
2718
2719 void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2720 Error **errp)
2721 {
2722 AioContext *aio_context;
2723 BlockDriverState *bs;
2724 BdrvDirtyBitmap *bitmap;
2725
2726 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2727 if (!bitmap || !bs) {
2728 return;
2729 }
2730
2731 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2732 error_setg(errp,
2733 "Bitmap '%s' is currently frozen and cannot be removed",
2734 name);
2735 goto out;
2736 }
2737 bdrv_dirty_bitmap_make_anon(bitmap);
2738 bdrv_release_dirty_bitmap(bs, bitmap);
2739
2740 out:
2741 aio_context_release(aio_context);
2742 }
2743
2744 /**
2745 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2746 * immediately after a full backup operation.
2747 */
2748 void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2749 Error **errp)
2750 {
2751 AioContext *aio_context;
2752 BdrvDirtyBitmap *bitmap;
2753 BlockDriverState *bs;
2754
2755 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2756 if (!bitmap || !bs) {
2757 return;
2758 }
2759
2760 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2761 error_setg(errp,
2762 "Bitmap '%s' is currently frozen and cannot be modified",
2763 name);
2764 goto out;
2765 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2766 error_setg(errp,
2767 "Bitmap '%s' is currently disabled and cannot be cleared",
2768 name);
2769 goto out;
2770 }
2771
2772 bdrv_clear_dirty_bitmap(bitmap, NULL);
2773
2774 out:
2775 aio_context_release(aio_context);
2776 }
2777
2778 void hmp_drive_del(Monitor *mon, const QDict *qdict)
2779 {
2780 const char *id = qdict_get_str(qdict, "id");
2781 BlockBackend *blk;
2782 BlockDriverState *bs;
2783 AioContext *aio_context;
2784 Error *local_err = NULL;
2785
2786 blk = blk_by_name(id);
2787 if (!blk) {
2788 error_report("Device '%s' not found", id);
2789 return;
2790 }
2791
2792 if (!blk_legacy_dinfo(blk)) {
2793 error_report("Deleting device added with blockdev-add"
2794 " is not supported");
2795 return;
2796 }
2797
2798 aio_context = blk_get_aio_context(blk);
2799 aio_context_acquire(aio_context);
2800
2801 bs = blk_bs(blk);
2802 if (bs) {
2803 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2804 error_report_err(local_err);
2805 aio_context_release(aio_context);
2806 return;
2807 }
2808
2809 blk_remove_bs(blk);
2810 }
2811
2812 /* if we have a device attached to this BlockDriverState
2813 * then we need to make the drive anonymous until the device
2814 * can be removed. If this is a drive with no device backing
2815 * then we can just get rid of the block driver state right here.
2816 */
2817 if (blk_get_attached_dev(blk)) {
2818 blk_hide_on_behalf_of_hmp_drive_del(blk);
2819 /* Further I/O must not pause the guest */
2820 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2821 BLOCKDEV_ON_ERROR_REPORT);
2822 } else {
2823 blk_unref(blk);
2824 }
2825
2826 aio_context_release(aio_context);
2827 }
2828
2829 void qmp_block_resize(bool has_device, const char *device,
2830 bool has_node_name, const char *node_name,
2831 int64_t size, Error **errp)
2832 {
2833 Error *local_err = NULL;
2834 BlockDriverState *bs;
2835 AioContext *aio_context;
2836 int ret;
2837
2838 bs = bdrv_lookup_bs(has_device ? device : NULL,
2839 has_node_name ? node_name : NULL,
2840 &local_err);
2841 if (local_err) {
2842 error_propagate(errp, local_err);
2843 return;
2844 }
2845
2846 aio_context = bdrv_get_aio_context(bs);
2847 aio_context_acquire(aio_context);
2848
2849 if (!bdrv_is_first_non_filter(bs)) {
2850 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2851 goto out;
2852 }
2853
2854 if (size < 0) {
2855 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2856 goto out;
2857 }
2858
2859 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2860 error_setg(errp, QERR_DEVICE_IN_USE, device);
2861 goto out;
2862 }
2863
2864 /* complete all in-flight operations before resizing the device */
2865 bdrv_drain_all();
2866
2867 ret = bdrv_truncate(bs, size);
2868 switch (ret) {
2869 case 0:
2870 break;
2871 case -ENOMEDIUM:
2872 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2873 break;
2874 case -ENOTSUP:
2875 error_setg(errp, QERR_UNSUPPORTED);
2876 break;
2877 case -EACCES:
2878 error_setg(errp, "Device '%s' is read only", device);
2879 break;
2880 case -EBUSY:
2881 error_setg(errp, QERR_DEVICE_IN_USE, device);
2882 break;
2883 default:
2884 error_setg_errno(errp, -ret, "Could not resize");
2885 break;
2886 }
2887
2888 out:
2889 aio_context_release(aio_context);
2890 }
2891
2892 static void block_job_cb(void *opaque, int ret)
2893 {
2894 /* Note that this function may be executed from another AioContext besides
2895 * the QEMU main loop. If you need to access anything that assumes the
2896 * QEMU global mutex, use a BH or introduce a mutex.
2897 */
2898
2899 BlockDriverState *bs = opaque;
2900 const char *msg = NULL;
2901
2902 trace_block_job_cb(bs, bs->job, ret);
2903
2904 assert(bs->job);
2905
2906 if (ret < 0) {
2907 msg = strerror(-ret);
2908 }
2909
2910 if (block_job_is_cancelled(bs->job)) {
2911 block_job_event_cancelled(bs->job);
2912 } else {
2913 block_job_event_completed(bs->job, msg);
2914 }
2915 }
2916
2917 void qmp_block_stream(const char *device,
2918 bool has_base, const char *base,
2919 bool has_backing_file, const char *backing_file,
2920 bool has_speed, int64_t speed,
2921 bool has_on_error, BlockdevOnError on_error,
2922 Error **errp)
2923 {
2924 BlockBackend *blk;
2925 BlockDriverState *bs;
2926 BlockDriverState *base_bs = NULL;
2927 AioContext *aio_context;
2928 Error *local_err = NULL;
2929 const char *base_name = NULL;
2930
2931 if (!has_on_error) {
2932 on_error = BLOCKDEV_ON_ERROR_REPORT;
2933 }
2934
2935 blk = blk_by_name(device);
2936 if (!blk) {
2937 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2938 "Device '%s' not found", device);
2939 return;
2940 }
2941
2942 aio_context = blk_get_aio_context(blk);
2943 aio_context_acquire(aio_context);
2944
2945 if (!blk_is_available(blk)) {
2946 error_setg(errp, "Device '%s' has no medium", device);
2947 goto out;
2948 }
2949 bs = blk_bs(blk);
2950
2951 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2952 goto out;
2953 }
2954
2955 if (has_base) {
2956 base_bs = bdrv_find_backing_image(bs, base);
2957 if (base_bs == NULL) {
2958 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2959 goto out;
2960 }
2961 assert(bdrv_get_aio_context(base_bs) == aio_context);
2962 base_name = base;
2963 }
2964
2965 /* if we are streaming the entire chain, the result will have no backing
2966 * file, and specifying one is therefore an error */
2967 if (base_bs == NULL && has_backing_file) {
2968 error_setg(errp, "backing file specified, but streaming the "
2969 "entire chain");
2970 goto out;
2971 }
2972
2973 /* backing_file string overrides base bs filename */
2974 base_name = has_backing_file ? backing_file : base_name;
2975
2976 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2977 on_error, block_job_cb, bs, &local_err);
2978 if (local_err) {
2979 error_propagate(errp, local_err);
2980 goto out;
2981 }
2982
2983 trace_qmp_block_stream(bs, bs->job);
2984
2985 out:
2986 aio_context_release(aio_context);
2987 }
2988
2989 void qmp_block_commit(const char *device,
2990 bool has_base, const char *base,
2991 bool has_top, const char *top,
2992 bool has_backing_file, const char *backing_file,
2993 bool has_speed, int64_t speed,
2994 Error **errp)
2995 {
2996 BlockBackend *blk;
2997 BlockDriverState *bs;
2998 BlockDriverState *base_bs, *top_bs;
2999 AioContext *aio_context;
3000 Error *local_err = NULL;
3001 /* This will be part of the QMP command, if/when the
3002 * BlockdevOnError change for blkmirror makes it in
3003 */
3004 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
3005
3006 if (!has_speed) {
3007 speed = 0;
3008 }
3009
3010 /* Important Note:
3011 * libvirt relies on the DeviceNotFound error class in order to probe for
3012 * live commit feature versions; for this to work, we must make sure to
3013 * perform the device lookup before any generic errors that may occur in a
3014 * scenario in which all optional arguments are omitted. */
3015 blk = blk_by_name(device);
3016 if (!blk) {
3017 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3018 "Device '%s' not found", device);
3019 return;
3020 }
3021
3022 aio_context = blk_get_aio_context(blk);
3023 aio_context_acquire(aio_context);
3024
3025 if (!blk_is_available(blk)) {
3026 error_setg(errp, "Device '%s' has no medium", device);
3027 goto out;
3028 }
3029 bs = blk_bs(blk);
3030
3031 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
3032 goto out;
3033 }
3034
3035 /* default top_bs is the active layer */
3036 top_bs = bs;
3037
3038 if (has_top && top) {
3039 if (strcmp(bs->filename, top) != 0) {
3040 top_bs = bdrv_find_backing_image(bs, top);
3041 }
3042 }
3043
3044 if (top_bs == NULL) {
3045 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
3046 goto out;
3047 }
3048
3049 assert(bdrv_get_aio_context(top_bs) == aio_context);
3050
3051 if (has_base && base) {
3052 base_bs = bdrv_find_backing_image(top_bs, base);
3053 } else {
3054 base_bs = bdrv_find_base(top_bs);
3055 }
3056
3057 if (base_bs == NULL) {
3058 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
3059 goto out;
3060 }
3061
3062 assert(bdrv_get_aio_context(base_bs) == aio_context);
3063
3064 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
3065 goto out;
3066 }
3067
3068 /* Do not allow attempts to commit an image into itself */
3069 if (top_bs == base_bs) {
3070 error_setg(errp, "cannot commit an image into itself");
3071 goto out;
3072 }
3073
3074 if (top_bs == bs) {
3075 if (has_backing_file) {
3076 error_setg(errp, "'backing-file' specified,"
3077 " but 'top' is the active layer");
3078 goto out;
3079 }
3080 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
3081 bs, &local_err);
3082 } else {
3083 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
3084 has_backing_file ? backing_file : NULL, &local_err);
3085 }
3086 if (local_err != NULL) {
3087 error_propagate(errp, local_err);
3088 goto out;
3089 }
3090
3091 out:
3092 aio_context_release(aio_context);
3093 }
3094
3095 static void do_drive_backup(const char *device, const char *target,
3096 bool has_format, const char *format,
3097 enum MirrorSyncMode sync,
3098 bool has_mode, enum NewImageMode mode,
3099 bool has_speed, int64_t speed,
3100 bool has_bitmap, const char *bitmap,
3101 bool has_on_source_error,
3102 BlockdevOnError on_source_error,
3103 bool has_on_target_error,
3104 BlockdevOnError on_target_error,
3105 BlockJobTxn *txn, Error **errp)
3106 {
3107 BlockBackend *blk;
3108 BlockDriverState *bs;
3109 BlockDriverState *target_bs;
3110 BlockDriverState *source = NULL;
3111 BdrvDirtyBitmap *bmap = NULL;
3112 AioContext *aio_context;
3113 QDict *options = NULL;
3114 Error *local_err = NULL;
3115 int flags;
3116 int64_t size;
3117 int ret;
3118
3119 if (!has_speed) {
3120 speed = 0;
3121 }
3122 if (!has_on_source_error) {
3123 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3124 }
3125 if (!has_on_target_error) {
3126 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3127 }
3128 if (!has_mode) {
3129 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3130 }
3131
3132 blk = blk_by_name(device);
3133 if (!blk) {
3134 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3135 "Device '%s' not found", device);
3136 return;
3137 }
3138
3139 aio_context = blk_get_aio_context(blk);
3140 aio_context_acquire(aio_context);
3141
3142 /* Although backup_run has this check too, we need to use bs->drv below, so
3143 * do an early check redundantly. */
3144 if (!blk_is_available(blk)) {
3145 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3146 goto out;
3147 }
3148 bs = blk_bs(blk);
3149
3150 if (!has_format) {
3151 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3152 }
3153
3154 /* Early check to avoid creating target */
3155 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
3156 goto out;
3157 }
3158
3159 flags = bs->open_flags | BDRV_O_RDWR;
3160
3161 /* See if we have a backing HD we can use to create our new image
3162 * on top of. */
3163 if (sync == MIRROR_SYNC_MODE_TOP) {
3164 source = backing_bs(bs);
3165 if (!source) {
3166 sync = MIRROR_SYNC_MODE_FULL;
3167 }
3168 }
3169 if (sync == MIRROR_SYNC_MODE_NONE) {
3170 source = bs;
3171 }
3172
3173 size = bdrv_getlength(bs);
3174 if (size < 0) {
3175 error_setg_errno(errp, -size, "bdrv_getlength failed");
3176 goto out;
3177 }
3178
3179 if (mode != NEW_IMAGE_MODE_EXISTING) {
3180 assert(format);
3181 if (source) {
3182 bdrv_img_create(target, format, source->filename,
3183 source->drv->format_name, NULL,
3184 size, flags, &local_err, false);
3185 } else {
3186 bdrv_img_create(target, format, NULL, NULL, NULL,
3187 size, flags, &local_err, false);
3188 }
3189 }
3190
3191 if (local_err) {
3192 error_propagate(errp, local_err);
3193 goto out;
3194 }
3195
3196 if (format) {
3197 options = qdict_new();
3198 qdict_put(options, "driver", qstring_from_str(format));
3199 }
3200
3201 target_bs = NULL;
3202 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
3203 if (ret < 0) {
3204 error_propagate(errp, local_err);
3205 goto out;
3206 }
3207
3208 bdrv_set_aio_context(target_bs, aio_context);
3209
3210 if (has_bitmap) {
3211 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
3212 if (!bmap) {
3213 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
3214 bdrv_unref(target_bs);
3215 goto out;
3216 }
3217 }
3218
3219 backup_start(bs, target_bs, speed, sync, bmap,
3220 on_source_error, on_target_error,
3221 block_job_cb, bs, txn, &local_err);
3222 if (local_err != NULL) {
3223 bdrv_unref(target_bs);
3224 error_propagate(errp, local_err);
3225 goto out;
3226 }
3227
3228 out:
3229 aio_context_release(aio_context);
3230 }
3231
3232 void qmp_drive_backup(const char *device, const char *target,
3233 bool has_format, const char *format,
3234 enum MirrorSyncMode sync,
3235 bool has_mode, enum NewImageMode mode,
3236 bool has_speed, int64_t speed,
3237 bool has_bitmap, const char *bitmap,
3238 bool has_on_source_error, BlockdevOnError on_source_error,
3239 bool has_on_target_error, BlockdevOnError on_target_error,
3240 Error **errp)
3241 {
3242 return do_drive_backup(device, target, has_format, format, sync,
3243 has_mode, mode, has_speed, speed,
3244 has_bitmap, bitmap,
3245 has_on_source_error, on_source_error,
3246 has_on_target_error, on_target_error,
3247 NULL, errp);
3248 }
3249
3250 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
3251 {
3252 return bdrv_named_nodes_list(errp);
3253 }
3254
3255 void do_blockdev_backup(const char *device, const char *target,
3256 enum MirrorSyncMode sync,
3257 bool has_speed, int64_t speed,
3258 bool has_on_source_error,
3259 BlockdevOnError on_source_error,
3260 bool has_on_target_error,
3261 BlockdevOnError on_target_error,
3262 BlockJobTxn *txn, Error **errp)
3263 {
3264 BlockBackend *blk, *target_blk;
3265 BlockDriverState *bs;
3266 BlockDriverState *target_bs;
3267 Error *local_err = NULL;
3268 AioContext *aio_context;
3269
3270 if (!has_speed) {
3271 speed = 0;
3272 }
3273 if (!has_on_source_error) {
3274 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3275 }
3276 if (!has_on_target_error) {
3277 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3278 }
3279
3280 blk = blk_by_name(device);
3281 if (!blk) {
3282 error_setg(errp, "Device '%s' not found", device);
3283 return;
3284 }
3285
3286 aio_context = blk_get_aio_context(blk);
3287 aio_context_acquire(aio_context);
3288
3289 if (!blk_is_available(blk)) {
3290 error_setg(errp, "Device '%s' has no medium", device);
3291 goto out;
3292 }
3293 bs = blk_bs(blk);
3294
3295 target_blk = blk_by_name(target);
3296 if (!target_blk) {
3297 error_setg(errp, "Device '%s' not found", target);
3298 goto out;
3299 }
3300
3301 if (!blk_is_available(target_blk)) {
3302 error_setg(errp, "Device '%s' has no medium", target);
3303 goto out;
3304 }
3305 target_bs = blk_bs(target_blk);
3306
3307 bdrv_ref(target_bs);
3308 bdrv_set_aio_context(target_bs, aio_context);
3309 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
3310 on_target_error, block_job_cb, bs, txn, &local_err);
3311 if (local_err != NULL) {
3312 bdrv_unref(target_bs);
3313 error_propagate(errp, local_err);
3314 }
3315 out:
3316 aio_context_release(aio_context);
3317 }
3318
3319 void qmp_blockdev_backup(const char *device, const char *target,
3320 enum MirrorSyncMode sync,
3321 bool has_speed, int64_t speed,
3322 bool has_on_source_error,
3323 BlockdevOnError on_source_error,
3324 bool has_on_target_error,
3325 BlockdevOnError on_target_error,
3326 Error **errp)
3327 {
3328 do_blockdev_backup(device, target, sync, has_speed, speed,
3329 has_on_source_error, on_source_error,
3330 has_on_target_error, on_target_error,
3331 NULL, errp);
3332 }
3333
3334 /* Parameter check and block job starting for drive mirroring.
3335 * Caller should hold @device and @target's aio context (must be the same).
3336 **/
3337 static void blockdev_mirror_common(BlockDriverState *bs,
3338 BlockDriverState *target,
3339 bool has_replaces, const char *replaces,
3340 enum MirrorSyncMode sync,
3341 bool has_speed, int64_t speed,
3342 bool has_granularity, uint32_t granularity,
3343 bool has_buf_size, int64_t buf_size,
3344 bool has_on_source_error,
3345 BlockdevOnError on_source_error,
3346 bool has_on_target_error,
3347 BlockdevOnError on_target_error,
3348 bool has_unmap, bool unmap,
3349 Error **errp)
3350 {
3351
3352 if (!has_speed) {
3353 speed = 0;
3354 }
3355 if (!has_on_source_error) {
3356 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
3357 }
3358 if (!has_on_target_error) {
3359 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
3360 }
3361 if (!has_granularity) {
3362 granularity = 0;
3363 }
3364 if (!has_buf_size) {
3365 buf_size = 0;
3366 }
3367 if (!has_unmap) {
3368 unmap = true;
3369 }
3370
3371 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3372 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3373 "a value in range [512B, 64MB]");
3374 return;
3375 }
3376 if (granularity & (granularity - 1)) {
3377 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3378 "power of 2");
3379 return;
3380 }
3381
3382 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
3383 return;
3384 }
3385 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
3386 return;
3387 }
3388 if (target->blk) {
3389 error_setg(errp, "Cannot mirror to an attached block device");
3390 return;
3391 }
3392
3393 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
3394 sync = MIRROR_SYNC_MODE_FULL;
3395 }
3396
3397 /* pass the node name to replace to mirror start since it's loose coupling
3398 * and will allow to check whether the node still exist at mirror completion
3399 */
3400 mirror_start(bs, target,
3401 has_replaces ? replaces : NULL,
3402 speed, granularity, buf_size, sync,
3403 on_source_error, on_target_error, unmap,
3404 block_job_cb, bs, errp);
3405 }
3406
3407 void qmp_drive_mirror(const char *device, const char *target,
3408 bool has_format, const char *format,
3409 bool has_node_name, const char *node_name,
3410 bool has_replaces, const char *replaces,
3411 enum MirrorSyncMode sync,
3412 bool has_mode, enum NewImageMode mode,
3413 bool has_speed, int64_t speed,
3414 bool has_granularity, uint32_t granularity,
3415 bool has_buf_size, int64_t buf_size,
3416 bool has_on_source_error, BlockdevOnError on_source_error,
3417 bool has_on_target_error, BlockdevOnError on_target_error,
3418 bool has_unmap, bool unmap,
3419 Error **errp)
3420 {
3421 BlockDriverState *bs;
3422 BlockBackend *blk;
3423 BlockDriverState *source, *target_bs;
3424 AioContext *aio_context;
3425 Error *local_err = NULL;
3426 QDict *options = NULL;
3427 int flags;
3428 int64_t size;
3429 int ret;
3430
3431 blk = blk_by_name(device);
3432 if (!blk) {
3433 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3434 "Device '%s' not found", device);
3435 return;
3436 }
3437
3438 aio_context = blk_get_aio_context(blk);
3439 aio_context_acquire(aio_context);
3440
3441 if (!blk_is_available(blk)) {
3442 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3443 goto out;
3444 }
3445 bs = blk_bs(blk);
3446 if (!has_mode) {
3447 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
3448 }
3449
3450 if (!has_format) {
3451 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3452 }
3453
3454 flags = bs->open_flags | BDRV_O_RDWR;
3455 source = backing_bs(bs);
3456 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3457 sync = MIRROR_SYNC_MODE_FULL;
3458 }
3459 if (sync == MIRROR_SYNC_MODE_NONE) {
3460 source = bs;
3461 }
3462
3463 size = bdrv_getlength(bs);
3464 if (size < 0) {
3465 error_setg_errno(errp, -size, "bdrv_getlength failed");
3466 goto out;
3467 }
3468
3469 if (has_replaces) {
3470 BlockDriverState *to_replace_bs;
3471 AioContext *replace_aio_context;
3472 int64_t replace_size;
3473
3474 if (!has_node_name) {
3475 error_setg(errp, "a node-name must be provided when replacing a"
3476 " named node of the graph");
3477 goto out;
3478 }
3479
3480 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
3481
3482 if (!to_replace_bs) {
3483 error_propagate(errp, local_err);
3484 goto out;
3485 }
3486
3487 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3488 aio_context_acquire(replace_aio_context);
3489 replace_size = bdrv_getlength(to_replace_bs);
3490 aio_context_release(replace_aio_context);
3491
3492 if (size != replace_size) {
3493 error_setg(errp, "cannot replace image with a mirror image of "
3494 "different size");
3495 goto out;
3496 }
3497 }
3498
3499 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3500 && mode != NEW_IMAGE_MODE_EXISTING)
3501 {
3502 /* create new image w/o backing file */
3503 assert(format);
3504 bdrv_img_create(target, format,
3505 NULL, NULL, NULL, size, flags, &local_err, false);
3506 } else {
3507 switch (mode) {
3508 case NEW_IMAGE_MODE_EXISTING:
3509 break;
3510 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3511 /* create new image with backing file */
3512 bdrv_img_create(target, format,
3513 source->filename,
3514 source->drv->format_name,
3515 NULL, size, flags, &local_err, false);
3516 break;
3517 default:
3518 abort();
3519 }
3520 }
3521
3522 if (local_err) {
3523 error_propagate(errp, local_err);
3524 goto out;
3525 }
3526
3527 options = qdict_new();
3528 if (has_node_name) {
3529 qdict_put(options, "node-name", qstring_from_str(node_name));
3530 }
3531 if (format) {
3532 qdict_put(options, "driver", qstring_from_str(format));
3533 }
3534
3535 /* Mirroring takes care of copy-on-write using the source's backing
3536 * file.
3537 */
3538 target_bs = NULL;
3539 ret = bdrv_open(&target_bs, target, NULL, options,
3540 flags | BDRV_O_NO_BACKING, &local_err);
3541 if (ret < 0) {
3542 error_propagate(errp, local_err);
3543 goto out;
3544 }
3545
3546 bdrv_set_aio_context(target_bs, aio_context);
3547
3548 blockdev_mirror_common(bs, target_bs,
3549 has_replaces, replaces, sync,
3550 has_speed, speed,
3551 has_granularity, granularity,
3552 has_buf_size, buf_size,
3553 has_on_source_error, on_source_error,
3554 has_on_target_error, on_target_error,
3555 has_unmap, unmap,
3556 &local_err);
3557 if (local_err) {
3558 error_propagate(errp, local_err);
3559 bdrv_unref(target_bs);
3560 }
3561 out:
3562 aio_context_release(aio_context);
3563 }
3564
3565 void qmp_blockdev_mirror(const char *device, const char *target,
3566 bool has_replaces, const char *replaces,
3567 MirrorSyncMode sync,
3568 bool has_speed, int64_t speed,
3569 bool has_granularity, uint32_t granularity,
3570 bool has_buf_size, int64_t buf_size,
3571 bool has_on_source_error,
3572 BlockdevOnError on_source_error,
3573 bool has_on_target_error,
3574 BlockdevOnError on_target_error,
3575 Error **errp)
3576 {
3577 BlockDriverState *bs;
3578 BlockBackend *blk;
3579 BlockDriverState *target_bs;
3580 AioContext *aio_context;
3581 Error *local_err = NULL;
3582
3583 blk = blk_by_name(device);
3584 if (!blk) {
3585 error_setg(errp, "Device '%s' not found", device);
3586 return;
3587 }
3588 bs = blk_bs(blk);
3589
3590 if (!bs) {
3591 error_setg(errp, "Device '%s' has no media", device);
3592 return;
3593 }
3594
3595 target_bs = bdrv_lookup_bs(target, target, errp);
3596 if (!target_bs) {
3597 return;
3598 }
3599
3600 aio_context = bdrv_get_aio_context(bs);
3601 aio_context_acquire(aio_context);
3602
3603 bdrv_ref(target_bs);
3604 bdrv_set_aio_context(target_bs, aio_context);
3605
3606 blockdev_mirror_common(bs, target_bs,
3607 has_replaces, replaces, sync,
3608 has_speed, speed,
3609 has_granularity, granularity,
3610 has_buf_size, buf_size,
3611 has_on_source_error, on_source_error,
3612 has_on_target_error, on_target_error,
3613 true, true,
3614 &local_err);
3615 if (local_err) {
3616 error_propagate(errp, local_err);
3617 bdrv_unref(target_bs);
3618 }
3619
3620 aio_context_release(aio_context);
3621 }
3622
3623 /* Get the block job for a given device name and acquire its AioContext */
3624 static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3625 Error **errp)
3626 {
3627 BlockBackend *blk;
3628 BlockDriverState *bs;
3629
3630 *aio_context = NULL;
3631
3632 blk = blk_by_name(device);
3633 if (!blk) {
3634 goto notfound;
3635 }
3636
3637 *aio_context = blk_get_aio_context(blk);
3638 aio_context_acquire(*aio_context);
3639
3640 if (!blk_is_available(blk)) {
3641 goto notfound;
3642 }
3643 bs = blk_bs(blk);
3644
3645 if (!bs->job) {
3646 goto notfound;
3647 }
3648
3649 return bs->job;
3650
3651 notfound:
3652 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3653 "No active block job on device '%s'", device);
3654 if (*aio_context) {
3655 aio_context_release(*aio_context);
3656 *aio_context = NULL;
3657 }
3658 return NULL;
3659 }
3660
3661 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3662 {
3663 AioContext *aio_context;
3664 BlockJob *job = find_block_job(device, &aio_context, errp);
3665
3666 if (!job) {
3667 return;
3668 }
3669
3670 block_job_set_speed(job, speed, errp);
3671 aio_context_release(aio_context);
3672 }
3673
3674 void qmp_block_job_cancel(const char *device,
3675 bool has_force, bool force, Error **errp)
3676 {
3677 AioContext *aio_context;
3678 BlockJob *job = find_block_job(device, &aio_context, errp);
3679
3680 if (!job) {
3681 return;
3682 }
3683
3684 if (!has_force) {
3685 force = false;
3686 }
3687
3688 if (job->user_paused && !force) {
3689 error_setg(errp, "The block job for device '%s' is currently paused",
3690 device);
3691 goto out;
3692 }
3693
3694 trace_qmp_block_job_cancel(job);
3695 block_job_cancel(job);
3696 out:
3697 aio_context_release(aio_context);
3698 }
3699
3700 void qmp_block_job_pause(const char *device, Error **errp)
3701 {
3702 AioContext *aio_context;
3703 BlockJob *job = find_block_job(device, &aio_context, errp);
3704
3705 if (!job || job->user_paused) {
3706 return;
3707 }
3708
3709 job->user_paused = true;
3710 trace_qmp_block_job_pause(job);
3711 block_job_pause(job);
3712 aio_context_release(aio_context);
3713 }
3714
3715 void qmp_block_job_resume(const char *device, Error **errp)
3716 {
3717 AioContext *aio_context;
3718 BlockJob *job = find_block_job(device, &aio_context, errp);
3719
3720 if (!job || !job->user_paused) {
3721 return;
3722 }
3723
3724 job->user_paused = false;
3725 trace_qmp_block_job_resume(job);
3726 block_job_resume(job);
3727 aio_context_release(aio_context);
3728 }
3729
3730 void qmp_block_job_complete(const char *device, Error **errp)
3731 {
3732 AioContext *aio_context;
3733 BlockJob *job = find_block_job(device, &aio_context, errp);
3734
3735 if (!job) {
3736 return;
3737 }
3738
3739 trace_qmp_block_job_complete(job);
3740 block_job_complete(job, errp);
3741 aio_context_release(aio_context);
3742 }
3743
3744 void qmp_change_backing_file(const char *device,
3745 const char *image_node_name,
3746 const char *backing_file,
3747 Error **errp)
3748 {
3749 BlockBackend *blk;
3750 BlockDriverState *bs = NULL;
3751 AioContext *aio_context;
3752 BlockDriverState *image_bs = NULL;
3753 Error *local_err = NULL;
3754 bool ro;
3755 int open_flags;
3756 int ret;
3757
3758 blk = blk_by_name(device);
3759 if (!blk) {
3760 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3761 "Device '%s' not found", device);
3762 return;
3763 }
3764
3765 aio_context = blk_get_aio_context(blk);
3766 aio_context_acquire(aio_context);
3767
3768 if (!blk_is_available(blk)) {
3769 error_setg(errp, "Device '%s' has no medium", device);
3770 goto out;
3771 }
3772 bs = blk_bs(blk);
3773
3774 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3775 if (local_err) {
3776 error_propagate(errp, local_err);
3777 goto out;
3778 }
3779
3780 if (!image_bs) {
3781 error_setg(errp, "image file not found");
3782 goto out;
3783 }
3784
3785 if (bdrv_find_base(image_bs) == image_bs) {
3786 error_setg(errp, "not allowing backing file change on an image "
3787 "without a backing file");
3788 goto out;
3789 }
3790
3791 /* even though we are not necessarily operating on bs, we need it to
3792 * determine if block ops are currently prohibited on the chain */
3793 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3794 goto out;
3795 }
3796
3797 /* final sanity check */
3798 if (!bdrv_chain_contains(bs, image_bs)) {
3799 error_setg(errp, "'%s' and image file are not in the same chain",
3800 device);
3801 goto out;
3802 }
3803
3804 /* if not r/w, reopen to make r/w */
3805 open_flags = image_bs->open_flags;
3806 ro = bdrv_is_read_only(image_bs);
3807
3808 if (ro) {
3809 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3810 if (local_err) {
3811 error_propagate(errp, local_err);
3812 goto out;
3813 }
3814 }
3815
3816 ret = bdrv_change_backing_file(image_bs, backing_file,
3817 image_bs->drv ? image_bs->drv->format_name : "");
3818
3819 if (ret < 0) {
3820 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3821 backing_file);
3822 /* don't exit here, so we can try to restore open flags if
3823 * appropriate */
3824 }
3825
3826 if (ro) {
3827 bdrv_reopen(image_bs, open_flags, &local_err);
3828 if (local_err) {
3829 error_propagate(errp, local_err); /* will preserve prior errp */
3830 }
3831 }
3832
3833 out:
3834 aio_context_release(aio_context);
3835 }
3836
3837 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3838 {
3839 QmpOutputVisitor *ov = qmp_output_visitor_new();
3840 BlockDriverState *bs;
3841 BlockBackend *blk = NULL;
3842 QObject *obj;
3843 QDict *qdict;
3844 Error *local_err = NULL;
3845
3846 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3847 * cache.direct=false instead of silently switching to aio=threads, except
3848 * when called from drive_new().
3849 *
3850 * For now, simply forbidding the combination for all drivers will do. */
3851 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3852 bool direct = options->has_cache &&
3853 options->cache->has_direct &&
3854 options->cache->direct;
3855 if (!direct) {
3856 error_setg(errp, "aio=native requires cache.direct=true");
3857 goto fail;
3858 }
3859 }
3860
3861 visit_type_BlockdevOptions(qmp_output_get_visitor(ov), NULL, &options,
3862 &local_err);
3863 if (local_err) {
3864 error_propagate(errp, local_err);
3865 goto fail;
3866 }
3867
3868 obj = qmp_output_get_qobject(ov);
3869 qdict = qobject_to_qdict(obj);
3870
3871 qdict_flatten(qdict);
3872
3873 if (options->has_id) {
3874 blk = blockdev_init(NULL, qdict, &local_err);
3875 if (local_err) {
3876 error_propagate(errp, local_err);
3877 goto fail;
3878 }
3879
3880 bs = blk_bs(blk);
3881 } else {
3882 if (!qdict_get_try_str(qdict, "node-name")) {
3883 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3884 "the root node");
3885 goto fail;
3886 }
3887
3888 bs = bds_tree_init(qdict, errp);
3889 if (!bs) {
3890 goto fail;
3891 }
3892
3893 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
3894 }
3895
3896 if (bs && bdrv_key_required(bs)) {
3897 if (blk) {
3898 blk_unref(blk);
3899 } else {
3900 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3901 bdrv_unref(bs);
3902 }
3903 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3904 goto fail;
3905 }
3906
3907 fail:
3908 qmp_output_visitor_cleanup(ov);
3909 }
3910
3911 void qmp_x_blockdev_del(bool has_id, const char *id,
3912 bool has_node_name, const char *node_name, Error **errp)
3913 {
3914 AioContext *aio_context;
3915 BlockBackend *blk;
3916 BlockDriverState *bs;
3917
3918 if (has_id && has_node_name) {
3919 error_setg(errp, "Only one of id and node-name must be specified");
3920 return;
3921 } else if (!has_id && !has_node_name) {
3922 error_setg(errp, "No block device specified");
3923 return;
3924 }
3925
3926 if (has_id) {
3927 blk = blk_by_name(id);
3928 if (!blk) {
3929 error_setg(errp, "Cannot find block backend %s", id);
3930 return;
3931 }
3932 if (blk_get_refcnt(blk) > 1) {
3933 error_setg(errp, "Block backend %s is in use", id);
3934 return;
3935 }
3936 bs = blk_bs(blk);
3937 aio_context = blk_get_aio_context(blk);
3938 } else {
3939 bs = bdrv_find_node(node_name);
3940 if (!bs) {
3941 error_setg(errp, "Cannot find node %s", node_name);
3942 return;
3943 }
3944 blk = bs->blk;
3945 if (blk) {
3946 error_setg(errp, "Node %s is in use by %s",
3947 node_name, blk_name(blk));
3948 return;
3949 }
3950 aio_context = bdrv_get_aio_context(bs);
3951 }
3952
3953 aio_context_acquire(aio_context);
3954
3955 if (bs) {
3956 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
3957 goto out;
3958 }
3959
3960 if (!blk && !bs->monitor_list.tqe_prev) {
3961 error_setg(errp, "Node %s is not owned by the monitor",
3962 bs->node_name);
3963 goto out;
3964 }
3965
3966 if (bs->refcnt > 1) {
3967 error_setg(errp, "Block device %s is in use",
3968 bdrv_get_device_or_node_name(bs));
3969 goto out;
3970 }
3971 }
3972
3973 if (blk) {
3974 blk_unref(blk);
3975 } else {
3976 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
3977 bdrv_unref(bs);
3978 }
3979
3980 out:
3981 aio_context_release(aio_context);
3982 }
3983
3984 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3985 {
3986 BlockJobInfoList *head = NULL, **p_next = &head;
3987 BlockDriverState *bs;
3988
3989 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3990 AioContext *aio_context = bdrv_get_aio_context(bs);
3991
3992 aio_context_acquire(aio_context);
3993
3994 if (bs->job) {
3995 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3996 elem->value = block_job_query(bs->job);
3997 *p_next = elem;
3998 p_next = &elem->next;
3999 }
4000
4001 aio_context_release(aio_context);
4002 }
4003
4004 return head;
4005 }
4006
4007 QemuOptsList qemu_common_drive_opts = {
4008 .name = "drive",
4009 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4010 .desc = {
4011 {
4012 .name = "snapshot",
4013 .type = QEMU_OPT_BOOL,
4014 .help = "enable/disable snapshot mode",
4015 },{
4016 .name = "discard",
4017 .type = QEMU_OPT_STRING,
4018 .help = "discard operation (ignore/off, unmap/on)",
4019 },{
4020 .name = "aio",
4021 .type = QEMU_OPT_STRING,
4022 .help = "host AIO implementation (threads, native)",
4023 },{
4024 .name = "format",
4025 .type = QEMU_OPT_STRING,
4026 .help = "disk format (raw, qcow2, ...)",
4027 },{
4028 .name = "rerror",
4029 .type = QEMU_OPT_STRING,
4030 .help = "read error action",
4031 },{
4032 .name = "werror",
4033 .type = QEMU_OPT_STRING,
4034 .help = "write error action",
4035 },{
4036 .name = "read-only",
4037 .type = QEMU_OPT_BOOL,
4038 .help = "open drive file as read-only",
4039 },{
4040 .name = "throttling.iops-total",
4041 .type = QEMU_OPT_NUMBER,
4042 .help = "limit total I/O operations per second",
4043 },{
4044 .name = "throttling.iops-read",
4045 .type = QEMU_OPT_NUMBER,
4046 .help = "limit read operations per second",
4047 },{
4048 .name = "throttling.iops-write",
4049 .type = QEMU_OPT_NUMBER,
4050 .help = "limit write operations per second",
4051 },{
4052 .name = "throttling.bps-total",
4053 .type = QEMU_OPT_NUMBER,
4054 .help = "limit total bytes per second",
4055 },{
4056 .name = "throttling.bps-read",
4057 .type = QEMU_OPT_NUMBER,
4058 .help = "limit read bytes per second",
4059 },{
4060 .name = "throttling.bps-write",
4061 .type = QEMU_OPT_NUMBER,
4062 .help = "limit write bytes per second",
4063 },{
4064 .name = "throttling.iops-total-max",
4065 .type = QEMU_OPT_NUMBER,
4066 .help = "I/O operations burst",
4067 },{
4068 .name = "throttling.iops-read-max",
4069 .type = QEMU_OPT_NUMBER,
4070 .help = "I/O operations read burst",
4071 },{
4072 .name = "throttling.iops-write-max",
4073 .type = QEMU_OPT_NUMBER,
4074 .help = "I/O operations write burst",
4075 },{
4076 .name = "throttling.bps-total-max",
4077 .type = QEMU_OPT_NUMBER,
4078 .help = "total bytes burst",
4079 },{
4080 .name = "throttling.bps-read-max",
4081 .type = QEMU_OPT_NUMBER,
4082 .help = "total bytes read burst",
4083 },{
4084 .name = "throttling.bps-write-max",
4085 .type = QEMU_OPT_NUMBER,
4086 .help = "total bytes write burst",
4087 },{
4088 .name = "throttling.iops-total-max-length",
4089 .type = QEMU_OPT_NUMBER,
4090 .help = "length of the iops-total-max burst period, in seconds",
4091 },{
4092 .name = "throttling.iops-read-max-length",
4093 .type = QEMU_OPT_NUMBER,
4094 .help = "length of the iops-read-max burst period, in seconds",
4095 },{
4096 .name = "throttling.iops-write-max-length",
4097 .type = QEMU_OPT_NUMBER,
4098 .help = "length of the iops-write-max burst period, in seconds",
4099 },{
4100 .name = "throttling.bps-total-max-length",
4101 .type = QEMU_OPT_NUMBER,
4102 .help = "length of the bps-total-max burst period, in seconds",
4103 },{
4104 .name = "throttling.bps-read-max-length",
4105 .type = QEMU_OPT_NUMBER,
4106 .help = "length of the bps-read-max burst period, in seconds",
4107 },{
4108 .name = "throttling.bps-write-max-length",
4109 .type = QEMU_OPT_NUMBER,
4110 .help = "length of the bps-write-max burst period, in seconds",
4111 },{
4112 .name = "throttling.iops-size",
4113 .type = QEMU_OPT_NUMBER,
4114 .help = "when limiting by iops max size of an I/O in bytes",
4115 },{
4116 .name = "throttling.group",
4117 .type = QEMU_OPT_STRING,
4118 .help = "name of the block throttling group",
4119 },{
4120 .name = "copy-on-read",
4121 .type = QEMU_OPT_BOOL,
4122 .help = "copy read data from backing file into image file",
4123 },{
4124 .name = "detect-zeroes",
4125 .type = QEMU_OPT_STRING,
4126 .help = "try to optimize zero writes (off, on, unmap)",
4127 },{
4128 .name = "stats-account-invalid",
4129 .type = QEMU_OPT_BOOL,
4130 .help = "whether to account for invalid I/O operations "
4131 "in the statistics",
4132 },{
4133 .name = "stats-account-failed",
4134 .type = QEMU_OPT_BOOL,
4135 .help = "whether to account for failed I/O operations "
4136 "in the statistics",
4137 },
4138 { /* end of list */ }
4139 },
4140 };
4141
4142 static QemuOptsList qemu_root_bds_opts = {
4143 .name = "root-bds",
4144 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4145 .desc = {
4146 {
4147 .name = "discard",
4148 .type = QEMU_OPT_STRING,
4149 .help = "discard operation (ignore/off, unmap/on)",
4150 },{
4151 .name = "aio",
4152 .type = QEMU_OPT_STRING,
4153 .help = "host AIO implementation (threads, native)",
4154 },{
4155 .name = "read-only",
4156 .type = QEMU_OPT_BOOL,
4157 .help = "open drive file as read-only",
4158 },{
4159 .name = "copy-on-read",
4160 .type = QEMU_OPT_BOOL,
4161 .help = "copy read data from backing file into image file",
4162 },{
4163 .name = "detect-zeroes",
4164 .type = QEMU_OPT_STRING,
4165 .help = "try to optimize zero writes (off, on, unmap)",
4166 },
4167 { /* end of list */ }
4168 },
4169 };
4170
4171 QemuOptsList qemu_drive_opts = {
4172 .name = "drive",
4173 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
4174 .desc = {
4175 /*
4176 * no elements => accept any params
4177 * validation will happen later
4178 */
4179 { /* end of list */ }
4180 },
4181 };