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