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