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