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