qemu/qemu-img.c
<<
>>
Prefs
   1/*
   2 * QEMU disk image utility
   3 *
   4 * Copyright (c) 2003-2008 Fabrice Bellard
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24#include "qemu/osdep.h"
  25#include <getopt.h>
  26
  27#include "qemu-version.h"
  28#include "qapi/error.h"
  29#include "qapi-visit.h"
  30#include "qapi/qobject-output-visitor.h"
  31#include "qapi/qmp/qerror.h"
  32#include "qapi/qmp/qjson.h"
  33#include "qapi/qmp/qbool.h"
  34#include "qemu/cutils.h"
  35#include "qemu/config-file.h"
  36#include "qemu/option.h"
  37#include "qemu/error-report.h"
  38#include "qemu/log.h"
  39#include "qom/object_interfaces.h"
  40#include "sysemu/sysemu.h"
  41#include "sysemu/block-backend.h"
  42#include "block/block_int.h"
  43#include "block/blockjob.h"
  44#include "block/qapi.h"
  45#include "crypto/init.h"
  46#include "trace/control.h"
  47
  48#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
  49                          "\n" QEMU_COPYRIGHT "\n"
  50
  51typedef struct img_cmd_t {
  52    const char *name;
  53    int (*handler)(int argc, char **argv);
  54} img_cmd_t;
  55
  56enum {
  57    OPTION_OUTPUT = 256,
  58    OPTION_BACKING_CHAIN = 257,
  59    OPTION_OBJECT = 258,
  60    OPTION_IMAGE_OPTS = 259,
  61    OPTION_PATTERN = 260,
  62    OPTION_FLUSH_INTERVAL = 261,
  63    OPTION_NO_DRAIN = 262,
  64    OPTION_TARGET_IMAGE_OPTS = 263,
  65    OPTION_SIZE = 264,
  66    OPTION_PREALLOCATION = 265,
  67    OPTION_SHRINK = 266,
  68};
  69
  70typedef enum OutputFormat {
  71    OFORMAT_JSON,
  72    OFORMAT_HUMAN,
  73} OutputFormat;
  74
  75/* Default to cache=writeback as data integrity is not important for qemu-img */
  76#define BDRV_DEFAULT_CACHE "writeback"
  77
  78static void format_print(void *opaque, const char *name)
  79{
  80    printf(" %s", name);
  81}
  82
  83static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
  84{
  85    va_list ap;
  86
  87    error_printf("qemu-img: ");
  88
  89    va_start(ap, fmt);
  90    error_vprintf(fmt, ap);
  91    va_end(ap);
  92
  93    error_printf("\nTry 'qemu-img --help' for more information\n");
  94    exit(EXIT_FAILURE);
  95}
  96
  97static void QEMU_NORETURN missing_argument(const char *option)
  98{
  99    error_exit("missing argument for option '%s'", option);
 100}
 101
 102static void QEMU_NORETURN unrecognized_option(const char *option)
 103{
 104    error_exit("unrecognized option '%s'", option);
 105}
 106
 107/* Please keep in synch with qemu-img.texi */
 108static void QEMU_NORETURN help(void)
 109{
 110    const char *help_msg =
 111           QEMU_IMG_VERSION
 112           "usage: qemu-img [standard options] command [command options]\n"
 113           "QEMU disk image utility\n"
 114           "\n"
 115           "    '-h', '--help'       display this help and exit\n"
 116           "    '-V', '--version'    output version information and exit\n"
 117           "    '-T', '--trace'      [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
 118           "                         specify tracing options\n"
 119           "\n"
 120           "Command syntax:\n"
 121#define DEF(option, callback, arg_string)        \
 122           "  " arg_string "\n"
 123#include "qemu-img-cmds.h"
 124#undef DEF
 125#undef GEN_DOCS
 126           "\n"
 127           "Command parameters:\n"
 128           "  'filename' is a disk image filename\n"
 129           "  'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n"
 130           "    manual page for a description of the object properties. The most common\n"
 131           "    object type is a 'secret', which is used to supply passwords and/or\n"
 132           "    encryption keys.\n"
 133           "  'fmt' is the disk image format. It is guessed automatically in most cases\n"
 134           "  'cache' is the cache mode used to write the output disk image, the valid\n"
 135           "    options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
 136           "    'directsync' and 'unsafe' (default for convert)\n"
 137           "  'src_cache' is the cache mode used to read input disk images, the valid\n"
 138           "    options are the same as for the 'cache' option\n"
 139           "  'size' is the disk image size in bytes. Optional suffixes\n"
 140           "    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
 141           "    'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are\n"
 142           "    supported. 'b' is ignored.\n"
 143           "  'output_filename' is the destination disk image filename\n"
 144           "  'output_fmt' is the destination format\n"
 145           "  'options' is a comma separated list of format specific options in a\n"
 146           "    name=value format. Use -o ? for an overview of the options supported by the\n"
 147           "    used format\n"
 148           "  'snapshot_param' is param used for internal snapshot, format\n"
 149           "    is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
 150           "    '[ID_OR_NAME]'\n"
 151           "  'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
 152           "    instead\n"
 153           "  '-c' indicates that target image must be compressed (qcow format only)\n"
 154           "  '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n"
 155           "       new backing file match exactly. The image doesn't need a working\n"
 156           "       backing file before rebasing in this case (useful for renaming the\n"
 157           "       backing file). For image creation, allow creating without attempting\n"
 158           "       to open the backing file.\n"
 159           "  '-h' with or without a command shows this help and lists the supported formats\n"
 160           "  '-p' show progress of command (only certain commands)\n"
 161           "  '-q' use Quiet mode - do not print any output (except errors)\n"
 162           "  '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
 163           "       contain only zeros for qemu-img to create a sparse image during\n"
 164           "       conversion. If the number of bytes is 0, the source will not be scanned for\n"
 165           "       unallocated or zero sectors, and the destination image will always be\n"
 166           "       fully allocated\n"
 167           "  '--output' takes the format in which the output must be done (human or json)\n"
 168           "  '-n' skips the target volume creation (useful if the volume is created\n"
 169           "       prior to running qemu-img)\n"
 170           "\n"
 171           "Parameters to check subcommand:\n"
 172           "  '-r' tries to repair any inconsistencies that are found during the check.\n"
 173           "       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
 174           "       kinds of errors, with a higher risk of choosing the wrong fix or\n"
 175           "       hiding corruption that has already occurred.\n"
 176           "\n"
 177           "Parameters to convert subcommand:\n"
 178           "  '-m' specifies how many coroutines work in parallel during the convert\n"
 179           "       process (defaults to 8)\n"
 180           "  '-W' allow to write to the target out of order rather than sequential\n"
 181           "\n"
 182           "Parameters to snapshot subcommand:\n"
 183           "  'snapshot' is the name of the snapshot to create, apply or delete\n"
 184           "  '-a' applies a snapshot (revert disk to saved state)\n"
 185           "  '-c' creates a snapshot\n"
 186           "  '-d' deletes a snapshot\n"
 187           "  '-l' lists all snapshots in the given image\n"
 188           "\n"
 189           "Parameters to compare subcommand:\n"
 190           "  '-f' first image format\n"
 191           "  '-F' second image format\n"
 192           "  '-s' run in Strict mode - fail on different image size or sector allocation\n"
 193           "\n"
 194           "Parameters to dd subcommand:\n"
 195           "  'bs=BYTES' read and write up to BYTES bytes at a time "
 196           "(default: 512)\n"
 197           "  'count=N' copy only N input blocks\n"
 198           "  'if=FILE' read from FILE\n"
 199           "  'of=FILE' write to FILE\n"
 200           "  'skip=N' skip N bs-sized blocks at the start of input\n";
 201
 202    printf("%s\nSupported formats:", help_msg);
 203    bdrv_iterate_format(format_print, NULL);
 204    printf("\n\n" QEMU_HELP_BOTTOM "\n");
 205    exit(EXIT_SUCCESS);
 206}
 207
 208static QemuOptsList qemu_object_opts = {
 209    .name = "object",
 210    .implied_opt_name = "qom-type",
 211    .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
 212    .desc = {
 213        { }
 214    },
 215};
 216
 217static QemuOptsList qemu_source_opts = {
 218    .name = "source",
 219    .implied_opt_name = "file",
 220    .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head),
 221    .desc = {
 222        { }
 223    },
 224};
 225
 226static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
 227{
 228    int ret = 0;
 229    if (!quiet) {
 230        va_list args;
 231        va_start(args, fmt);
 232        ret = vprintf(fmt, args);
 233        va_end(args);
 234    }
 235    return ret;
 236}
 237
 238
 239static int print_block_option_help(const char *filename, const char *fmt)
 240{
 241    BlockDriver *drv, *proto_drv;
 242    QemuOptsList *create_opts = NULL;
 243    Error *local_err = NULL;
 244
 245    /* Find driver and parse its options */
 246    drv = bdrv_find_format(fmt);
 247    if (!drv) {
 248        error_report("Unknown file format '%s'", fmt);
 249        return 1;
 250    }
 251
 252    create_opts = qemu_opts_append(create_opts, drv->create_opts);
 253    if (filename) {
 254        proto_drv = bdrv_find_protocol(filename, true, &local_err);
 255        if (!proto_drv) {
 256            error_report_err(local_err);
 257            qemu_opts_free(create_opts);
 258            return 1;
 259        }
 260        create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
 261    }
 262
 263    qemu_opts_print_help(create_opts);
 264    qemu_opts_free(create_opts);
 265    return 0;
 266}
 267
 268
 269static BlockBackend *img_open_opts(const char *optstr,
 270                                   QemuOpts *opts, int flags, bool writethrough,
 271                                   bool quiet, bool force_share)
 272{
 273    QDict *options;
 274    Error *local_err = NULL;
 275    BlockBackend *blk;
 276    options = qemu_opts_to_qdict(opts, NULL);
 277    if (force_share) {
 278        if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE)
 279            && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) {
 280            error_report("--force-share/-U conflicts with image options");
 281            QDECREF(options);
 282            return NULL;
 283        }
 284        qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on");
 285    }
 286    blk = blk_new_open(NULL, NULL, options, flags, &local_err);
 287    if (!blk) {
 288        error_reportf_err(local_err, "Could not open '%s': ", optstr);
 289        return NULL;
 290    }
 291    blk_set_enable_write_cache(blk, !writethrough);
 292
 293    return blk;
 294}
 295
 296static BlockBackend *img_open_file(const char *filename,
 297                                   QDict *options,
 298                                   const char *fmt, int flags,
 299                                   bool writethrough, bool quiet,
 300                                   bool force_share)
 301{
 302    BlockBackend *blk;
 303    Error *local_err = NULL;
 304
 305    if (!options) {
 306        options = qdict_new();
 307    }
 308    if (fmt) {
 309        qdict_put_str(options, "driver", fmt);
 310    }
 311
 312    if (force_share) {
 313        qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
 314    }
 315    blk = blk_new_open(filename, NULL, options, flags, &local_err);
 316    if (!blk) {
 317        error_reportf_err(local_err, "Could not open '%s': ", filename);
 318        return NULL;
 319    }
 320    blk_set_enable_write_cache(blk, !writethrough);
 321
 322    return blk;
 323}
 324
 325
 326static int img_add_key_secrets(void *opaque,
 327                               const char *name, const char *value,
 328                               Error **errp)
 329{
 330    QDict *options = opaque;
 331
 332    if (g_str_has_suffix(name, "key-secret")) {
 333        qdict_put_str(options, name, value);
 334    }
 335
 336    return 0;
 337}
 338
 339static BlockBackend *img_open_new_file(const char *filename,
 340                                       QemuOpts *create_opts,
 341                                       const char *fmt, int flags,
 342                                       bool writethrough, bool quiet,
 343                                       bool force_share)
 344{
 345    QDict *options = NULL;
 346
 347    options = qdict_new();
 348    qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort);
 349
 350    return img_open_file(filename, options, fmt, flags, writethrough, quiet,
 351                         force_share);
 352}
 353
 354
 355static BlockBackend *img_open(bool image_opts,
 356                              const char *filename,
 357                              const char *fmt, int flags, bool writethrough,
 358                              bool quiet, bool force_share)
 359{
 360    BlockBackend *blk;
 361    if (image_opts) {
 362        QemuOpts *opts;
 363        if (fmt) {
 364            error_report("--image-opts and --format are mutually exclusive");
 365            return NULL;
 366        }
 367        opts = qemu_opts_parse_noisily(qemu_find_opts("source"),
 368                                       filename, true);
 369        if (!opts) {
 370            return NULL;
 371        }
 372        blk = img_open_opts(filename, opts, flags, writethrough, quiet,
 373                            force_share);
 374    } else {
 375        blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet,
 376                            force_share);
 377    }
 378    return blk;
 379}
 380
 381
 382static int add_old_style_options(const char *fmt, QemuOpts *opts,
 383                                 const char *base_filename,
 384                                 const char *base_fmt)
 385{
 386    Error *err = NULL;
 387
 388    if (base_filename) {
 389        qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err);
 390        if (err) {
 391            error_report("Backing file not supported for file format '%s'",
 392                         fmt);
 393            error_free(err);
 394            return -1;
 395        }
 396    }
 397    if (base_fmt) {
 398        qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err);
 399        if (err) {
 400            error_report("Backing file format not supported for file "
 401                         "format '%s'", fmt);
 402            error_free(err);
 403            return -1;
 404        }
 405    }
 406    return 0;
 407}
 408
 409static int64_t cvtnum(const char *s)
 410{
 411    int err;
 412    uint64_t value;
 413
 414    err = qemu_strtosz(s, NULL, &value);
 415    if (err < 0) {
 416        return err;
 417    }
 418    if (value > INT64_MAX) {
 419        return -ERANGE;
 420    }
 421    return value;
 422}
 423
 424static int img_create(int argc, char **argv)
 425{
 426    int c;
 427    uint64_t img_size = -1;
 428    const char *fmt = "raw";
 429    const char *base_fmt = NULL;
 430    const char *filename;
 431    const char *base_filename = NULL;
 432    char *options = NULL;
 433    Error *local_err = NULL;
 434    bool quiet = false;
 435    int flags = 0;
 436
 437    for(;;) {
 438        static const struct option long_options[] = {
 439            {"help", no_argument, 0, 'h'},
 440            {"object", required_argument, 0, OPTION_OBJECT},
 441            {0, 0, 0, 0}
 442        };
 443        c = getopt_long(argc, argv, ":F:b:f:ho:qu",
 444                        long_options, NULL);
 445        if (c == -1) {
 446            break;
 447        }
 448        switch(c) {
 449        case ':':
 450            missing_argument(argv[optind - 1]);
 451            break;
 452        case '?':
 453            unrecognized_option(argv[optind - 1]);
 454            break;
 455        case 'h':
 456            help();
 457            break;
 458        case 'F':
 459            base_fmt = optarg;
 460            break;
 461        case 'b':
 462            base_filename = optarg;
 463            break;
 464        case 'f':
 465            fmt = optarg;
 466            break;
 467        case 'o':
 468            if (!is_valid_option_list(optarg)) {
 469                error_report("Invalid option list: %s", optarg);
 470                goto fail;
 471            }
 472            if (!options) {
 473                options = g_strdup(optarg);
 474            } else {
 475                char *old_options = options;
 476                options = g_strdup_printf("%s,%s", options, optarg);
 477                g_free(old_options);
 478            }
 479            break;
 480        case 'q':
 481            quiet = true;
 482            break;
 483        case 'u':
 484            flags |= BDRV_O_NO_BACKING;
 485            break;
 486        case OPTION_OBJECT: {
 487            QemuOpts *opts;
 488            opts = qemu_opts_parse_noisily(&qemu_object_opts,
 489                                           optarg, true);
 490            if (!opts) {
 491                goto fail;
 492            }
 493        }   break;
 494        }
 495    }
 496
 497    /* Get the filename */
 498    filename = (optind < argc) ? argv[optind] : NULL;
 499    if (options && has_help_option(options)) {
 500        g_free(options);
 501        return print_block_option_help(filename, fmt);
 502    }
 503
 504    if (optind >= argc) {
 505        error_exit("Expecting image file name");
 506    }
 507    optind++;
 508
 509    if (qemu_opts_foreach(&qemu_object_opts,
 510                          user_creatable_add_opts_foreach,
 511                          NULL, NULL)) {
 512        goto fail;
 513    }
 514
 515    /* Get image size, if specified */
 516    if (optind < argc) {
 517        int64_t sval;
 518
 519        sval = cvtnum(argv[optind++]);
 520        if (sval < 0) {
 521            if (sval == -ERANGE) {
 522                error_report("Image size must be less than 8 EiB!");
 523            } else {
 524                error_report("Invalid image size specified! You may use k, M, "
 525                      "G, T, P or E suffixes for ");
 526                error_report("kilobytes, megabytes, gigabytes, terabytes, "
 527                             "petabytes and exabytes.");
 528            }
 529            goto fail;
 530        }
 531        img_size = (uint64_t)sval;
 532    }
 533    if (optind != argc) {
 534        error_exit("Unexpected argument: %s", argv[optind]);
 535    }
 536
 537    bdrv_img_create(filename, fmt, base_filename, base_fmt,
 538                    options, img_size, flags, quiet, &local_err);
 539    if (local_err) {
 540        error_reportf_err(local_err, "%s: ", filename);
 541        goto fail;
 542    }
 543
 544    g_free(options);
 545    return 0;
 546
 547fail:
 548    g_free(options);
 549    return 1;
 550}
 551
 552static void dump_json_image_check(ImageCheck *check, bool quiet)
 553{
 554    QString *str;
 555    QObject *obj;
 556    Visitor *v = qobject_output_visitor_new(&obj);
 557
 558    visit_type_ImageCheck(v, NULL, &check, &error_abort);
 559    visit_complete(v, &obj);
 560    str = qobject_to_json_pretty(obj);
 561    assert(str != NULL);
 562    qprintf(quiet, "%s\n", qstring_get_str(str));
 563    qobject_decref(obj);
 564    visit_free(v);
 565    QDECREF(str);
 566}
 567
 568static void dump_human_image_check(ImageCheck *check, bool quiet)
 569{
 570    if (!(check->corruptions || check->leaks || check->check_errors)) {
 571        qprintf(quiet, "No errors were found on the image.\n");
 572    } else {
 573        if (check->corruptions) {
 574            qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
 575                    "Data may be corrupted, or further writes to the image "
 576                    "may corrupt it.\n",
 577                    check->corruptions);
 578        }
 579
 580        if (check->leaks) {
 581            qprintf(quiet,
 582                    "\n%" PRId64 " leaked clusters were found on the image.\n"
 583                    "This means waste of disk space, but no harm to data.\n",
 584                    check->leaks);
 585        }
 586
 587        if (check->check_errors) {
 588            qprintf(quiet,
 589                    "\n%" PRId64
 590                    " internal errors have occurred during the check.\n",
 591                    check->check_errors);
 592        }
 593    }
 594
 595    if (check->total_clusters != 0 && check->allocated_clusters != 0) {
 596        qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
 597                "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
 598                check->allocated_clusters, check->total_clusters,
 599                check->allocated_clusters * 100.0 / check->total_clusters,
 600                check->fragmented_clusters * 100.0 / check->allocated_clusters,
 601                check->compressed_clusters * 100.0 /
 602                check->allocated_clusters);
 603    }
 604
 605    if (check->image_end_offset) {
 606        qprintf(quiet,
 607                "Image end offset: %" PRId64 "\n", check->image_end_offset);
 608    }
 609}
 610
 611static int collect_image_check(BlockDriverState *bs,
 612                   ImageCheck *check,
 613                   const char *filename,
 614                   const char *fmt,
 615                   int fix)
 616{
 617    int ret;
 618    BdrvCheckResult result;
 619
 620    ret = bdrv_check(bs, &result, fix);
 621    if (ret < 0) {
 622        return ret;
 623    }
 624
 625    check->filename                 = g_strdup(filename);
 626    check->format                   = g_strdup(bdrv_get_format_name(bs));
 627    check->check_errors             = result.check_errors;
 628    check->corruptions              = result.corruptions;
 629    check->has_corruptions          = result.corruptions != 0;
 630    check->leaks                    = result.leaks;
 631    check->has_leaks                = result.leaks != 0;
 632    check->corruptions_fixed        = result.corruptions_fixed;
 633    check->has_corruptions_fixed    = result.corruptions != 0;
 634    check->leaks_fixed              = result.leaks_fixed;
 635    check->has_leaks_fixed          = result.leaks != 0;
 636    check->image_end_offset         = result.image_end_offset;
 637    check->has_image_end_offset     = result.image_end_offset != 0;
 638    check->total_clusters           = result.bfi.total_clusters;
 639    check->has_total_clusters       = result.bfi.total_clusters != 0;
 640    check->allocated_clusters       = result.bfi.allocated_clusters;
 641    check->has_allocated_clusters   = result.bfi.allocated_clusters != 0;
 642    check->fragmented_clusters      = result.bfi.fragmented_clusters;
 643    check->has_fragmented_clusters  = result.bfi.fragmented_clusters != 0;
 644    check->compressed_clusters      = result.bfi.compressed_clusters;
 645    check->has_compressed_clusters  = result.bfi.compressed_clusters != 0;
 646
 647    return 0;
 648}
 649
 650/*
 651 * Checks an image for consistency. Exit codes:
 652 *
 653 *  0 - Check completed, image is good
 654 *  1 - Check not completed because of internal errors
 655 *  2 - Check completed, image is corrupted
 656 *  3 - Check completed, image has leaked clusters, but is good otherwise
 657 * 63 - Checks are not supported by the image format
 658 */
 659static int img_check(int argc, char **argv)
 660{
 661    int c, ret;
 662    OutputFormat output_format = OFORMAT_HUMAN;
 663    const char *filename, *fmt, *output, *cache;
 664    BlockBackend *blk;
 665    BlockDriverState *bs;
 666    int fix = 0;
 667    int flags = BDRV_O_CHECK;
 668    bool writethrough;
 669    ImageCheck *check;
 670    bool quiet = false;
 671    bool image_opts = false;
 672    bool force_share = false;
 673
 674    fmt = NULL;
 675    output = NULL;
 676    cache = BDRV_DEFAULT_CACHE;
 677
 678    for(;;) {
 679        int option_index = 0;
 680        static const struct option long_options[] = {
 681            {"help", no_argument, 0, 'h'},
 682            {"format", required_argument, 0, 'f'},
 683            {"repair", required_argument, 0, 'r'},
 684            {"output", required_argument, 0, OPTION_OUTPUT},
 685            {"object", required_argument, 0, OPTION_OBJECT},
 686            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
 687            {"force-share", no_argument, 0, 'U'},
 688            {0, 0, 0, 0}
 689        };
 690        c = getopt_long(argc, argv, ":hf:r:T:qU",
 691                        long_options, &option_index);
 692        if (c == -1) {
 693            break;
 694        }
 695        switch(c) {
 696        case ':':
 697            missing_argument(argv[optind - 1]);
 698            break;
 699        case '?':
 700            unrecognized_option(argv[optind - 1]);
 701            break;
 702        case 'h':
 703            help();
 704            break;
 705        case 'f':
 706            fmt = optarg;
 707            break;
 708        case 'r':
 709            flags |= BDRV_O_RDWR;
 710
 711            if (!strcmp(optarg, "leaks")) {
 712                fix = BDRV_FIX_LEAKS;
 713            } else if (!strcmp(optarg, "all")) {
 714                fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
 715            } else {
 716                error_exit("Unknown option value for -r "
 717                           "(expecting 'leaks' or 'all'): %s", optarg);
 718            }
 719            break;
 720        case OPTION_OUTPUT:
 721            output = optarg;
 722            break;
 723        case 'T':
 724            cache = optarg;
 725            break;
 726        case 'q':
 727            quiet = true;
 728            break;
 729        case 'U':
 730            force_share = true;
 731            break;
 732        case OPTION_OBJECT: {
 733            QemuOpts *opts;
 734            opts = qemu_opts_parse_noisily(&qemu_object_opts,
 735                                           optarg, true);
 736            if (!opts) {
 737                return 1;
 738            }
 739        }   break;
 740        case OPTION_IMAGE_OPTS:
 741            image_opts = true;
 742            break;
 743        }
 744    }
 745    if (optind != argc - 1) {
 746        error_exit("Expecting one image file name");
 747    }
 748    filename = argv[optind++];
 749
 750    if (output && !strcmp(output, "json")) {
 751        output_format = OFORMAT_JSON;
 752    } else if (output && !strcmp(output, "human")) {
 753        output_format = OFORMAT_HUMAN;
 754    } else if (output) {
 755        error_report("--output must be used with human or json as argument.");
 756        return 1;
 757    }
 758
 759    if (qemu_opts_foreach(&qemu_object_opts,
 760                          user_creatable_add_opts_foreach,
 761                          NULL, NULL)) {
 762        return 1;
 763    }
 764
 765    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
 766    if (ret < 0) {
 767        error_report("Invalid source cache option: %s", cache);
 768        return 1;
 769    }
 770
 771    blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
 772                   force_share);
 773    if (!blk) {
 774        return 1;
 775    }
 776    bs = blk_bs(blk);
 777
 778    check = g_new0(ImageCheck, 1);
 779    ret = collect_image_check(bs, check, filename, fmt, fix);
 780
 781    if (ret == -ENOTSUP) {
 782        error_report("This image format does not support checks");
 783        ret = 63;
 784        goto fail;
 785    }
 786
 787    if (check->corruptions_fixed || check->leaks_fixed) {
 788        int corruptions_fixed, leaks_fixed;
 789
 790        leaks_fixed         = check->leaks_fixed;
 791        corruptions_fixed   = check->corruptions_fixed;
 792
 793        if (output_format == OFORMAT_HUMAN) {
 794            qprintf(quiet,
 795                    "The following inconsistencies were found and repaired:\n\n"
 796                    "    %" PRId64 " leaked clusters\n"
 797                    "    %" PRId64 " corruptions\n\n"
 798                    "Double checking the fixed image now...\n",
 799                    check->leaks_fixed,
 800                    check->corruptions_fixed);
 801        }
 802
 803        ret = collect_image_check(bs, check, filename, fmt, 0);
 804
 805        check->leaks_fixed          = leaks_fixed;
 806        check->corruptions_fixed    = corruptions_fixed;
 807    }
 808
 809    if (!ret) {
 810        switch (output_format) {
 811        case OFORMAT_HUMAN:
 812            dump_human_image_check(check, quiet);
 813            break;
 814        case OFORMAT_JSON:
 815            dump_json_image_check(check, quiet);
 816            break;
 817        }
 818    }
 819
 820    if (ret || check->check_errors) {
 821        if (ret) {
 822            error_report("Check failed: %s", strerror(-ret));
 823        } else {
 824            error_report("Check failed");
 825        }
 826        ret = 1;
 827        goto fail;
 828    }
 829
 830    if (check->corruptions) {
 831        ret = 2;
 832    } else if (check->leaks) {
 833        ret = 3;
 834    } else {
 835        ret = 0;
 836    }
 837
 838fail:
 839    qapi_free_ImageCheck(check);
 840    blk_unref(blk);
 841    return ret;
 842}
 843
 844typedef struct CommonBlockJobCBInfo {
 845    BlockDriverState *bs;
 846    Error **errp;
 847} CommonBlockJobCBInfo;
 848
 849static void common_block_job_cb(void *opaque, int ret)
 850{
 851    CommonBlockJobCBInfo *cbi = opaque;
 852
 853    if (ret < 0) {
 854        error_setg_errno(cbi->errp, -ret, "Block job failed");
 855    }
 856}
 857
 858static void run_block_job(BlockJob *job, Error **errp)
 859{
 860    AioContext *aio_context = blk_get_aio_context(job->blk);
 861    int ret = 0;
 862
 863    aio_context_acquire(aio_context);
 864    block_job_ref(job);
 865    do {
 866        aio_poll(aio_context, true);
 867        qemu_progress_print(job->len ?
 868                            ((float)job->offset / job->len * 100.f) : 0.0f, 0);
 869    } while (!job->ready && !job->completed);
 870
 871    if (!job->completed) {
 872        ret = block_job_complete_sync(job, errp);
 873    } else {
 874        ret = job->ret;
 875    }
 876    block_job_unref(job);
 877    aio_context_release(aio_context);
 878
 879    /* publish completion progress only when success */
 880    if (!ret) {
 881        qemu_progress_print(100.f, 0);
 882    }
 883}
 884
 885static int img_commit(int argc, char **argv)
 886{
 887    int c, ret, flags;
 888    const char *filename, *fmt, *cache, *base;
 889    BlockBackend *blk;
 890    BlockDriverState *bs, *base_bs;
 891    BlockJob *job;
 892    bool progress = false, quiet = false, drop = false;
 893    bool writethrough;
 894    Error *local_err = NULL;
 895    CommonBlockJobCBInfo cbi;
 896    bool image_opts = false;
 897    AioContext *aio_context;
 898
 899    fmt = NULL;
 900    cache = BDRV_DEFAULT_CACHE;
 901    base = NULL;
 902    for(;;) {
 903        static const struct option long_options[] = {
 904            {"help", no_argument, 0, 'h'},
 905            {"object", required_argument, 0, OPTION_OBJECT},
 906            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
 907            {0, 0, 0, 0}
 908        };
 909        c = getopt_long(argc, argv, ":f:ht:b:dpq",
 910                        long_options, NULL);
 911        if (c == -1) {
 912            break;
 913        }
 914        switch(c) {
 915        case ':':
 916            missing_argument(argv[optind - 1]);
 917            break;
 918        case '?':
 919            unrecognized_option(argv[optind - 1]);
 920            break;
 921        case 'h':
 922            help();
 923            break;
 924        case 'f':
 925            fmt = optarg;
 926            break;
 927        case 't':
 928            cache = optarg;
 929            break;
 930        case 'b':
 931            base = optarg;
 932            /* -b implies -d */
 933            drop = true;
 934            break;
 935        case 'd':
 936            drop = true;
 937            break;
 938        case 'p':
 939            progress = true;
 940            break;
 941        case 'q':
 942            quiet = true;
 943            break;
 944        case OPTION_OBJECT: {
 945            QemuOpts *opts;
 946            opts = qemu_opts_parse_noisily(&qemu_object_opts,
 947                                           optarg, true);
 948            if (!opts) {
 949                return 1;
 950            }
 951        }   break;
 952        case OPTION_IMAGE_OPTS:
 953            image_opts = true;
 954            break;
 955        }
 956    }
 957
 958    /* Progress is not shown in Quiet mode */
 959    if (quiet) {
 960        progress = false;
 961    }
 962
 963    if (optind != argc - 1) {
 964        error_exit("Expecting one image file name");
 965    }
 966    filename = argv[optind++];
 967
 968    if (qemu_opts_foreach(&qemu_object_opts,
 969                          user_creatable_add_opts_foreach,
 970                          NULL, NULL)) {
 971        return 1;
 972    }
 973
 974    flags = BDRV_O_RDWR | BDRV_O_UNMAP;
 975    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
 976    if (ret < 0) {
 977        error_report("Invalid cache option: %s", cache);
 978        return 1;
 979    }
 980
 981    blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
 982                   false);
 983    if (!blk) {
 984        return 1;
 985    }
 986    bs = blk_bs(blk);
 987
 988    qemu_progress_init(progress, 1.f);
 989    qemu_progress_print(0.f, 100);
 990
 991    if (base) {
 992        base_bs = bdrv_find_backing_image(bs, base);
 993        if (!base_bs) {
 994            error_setg(&local_err,
 995                       "Did not find '%s' in the backing chain of '%s'",
 996                       base, filename);
 997            goto done;
 998        }
 999    } else {
1000        /* This is different from QMP, which by default uses the deepest file in
1001         * the backing chain (i.e., the very base); however, the traditional
1002         * behavior of qemu-img commit is using the immediate backing file. */
1003        base_bs = backing_bs(bs);
1004        if (!base_bs) {
1005            error_setg(&local_err, "Image does not have a backing file");
1006            goto done;
1007        }
1008    }
1009
1010    cbi = (CommonBlockJobCBInfo){
1011        .errp = &local_err,
1012        .bs   = bs,
1013    };
1014
1015    aio_context = bdrv_get_aio_context(bs);
1016    aio_context_acquire(aio_context);
1017    commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
1018                        BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb,
1019                        &cbi, false, &local_err);
1020    aio_context_release(aio_context);
1021    if (local_err) {
1022        goto done;
1023    }
1024
1025    /* When the block job completes, the BlockBackend reference will point to
1026     * the old backing file. In order to avoid that the top image is already
1027     * deleted, so we can still empty it afterwards, increment the reference
1028     * counter here preemptively. */
1029    if (!drop) {
1030        bdrv_ref(bs);
1031    }
1032
1033    job = block_job_get("commit");
1034    run_block_job(job, &local_err);
1035    if (local_err) {
1036        goto unref_backing;
1037    }
1038
1039    if (!drop && bs->drv->bdrv_make_empty) {
1040        ret = bs->drv->bdrv_make_empty(bs);
1041        if (ret) {
1042            error_setg_errno(&local_err, -ret, "Could not empty %s",
1043                             filename);
1044            goto unref_backing;
1045        }
1046    }
1047
1048unref_backing:
1049    if (!drop) {
1050        bdrv_unref(bs);
1051    }
1052
1053done:
1054    qemu_progress_end();
1055
1056    blk_unref(blk);
1057
1058    if (local_err) {
1059        error_report_err(local_err);
1060        return 1;
1061    }
1062
1063    qprintf(quiet, "Image committed.\n");
1064    return 0;
1065}
1066
1067/*
1068 * Returns -1 if 'buf' contains only zeroes, otherwise the byte index
1069 * of the first sector boundary within buf where the sector contains a
1070 * non-zero byte.  This function is robust to a buffer that is not
1071 * sector-aligned.
1072 */
1073static int64_t find_nonzero(const uint8_t *buf, int64_t n)
1074{
1075    int64_t i;
1076    int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE);
1077
1078    for (i = 0; i < end; i += BDRV_SECTOR_SIZE) {
1079        if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) {
1080            return i;
1081        }
1082    }
1083    if (i < n && !buffer_is_zero(buf + i, n - end)) {
1084        return i;
1085    }
1086    return -1;
1087}
1088
1089/*
1090 * Returns true iff the first sector pointed to by 'buf' contains at least
1091 * a non-NUL byte.
1092 *
1093 * 'pnum' is set to the number of sectors (including and immediately following
1094 * the first one) that are known to be in the same allocated/unallocated state.
1095 */
1096static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1097{
1098    bool is_zero;
1099    int i;
1100
1101    if (n <= 0) {
1102        *pnum = 0;
1103        return 0;
1104    }
1105    is_zero = buffer_is_zero(buf, 512);
1106    for(i = 1; i < n; i++) {
1107        buf += 512;
1108        if (is_zero != buffer_is_zero(buf, 512)) {
1109            break;
1110        }
1111    }
1112    *pnum = i;
1113    return !is_zero;
1114}
1115
1116/*
1117 * Like is_allocated_sectors, but if the buffer starts with a used sector,
1118 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1119 * breaking up write requests for only small sparse areas.
1120 */
1121static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1122    int min)
1123{
1124    int ret;
1125    int num_checked, num_used;
1126
1127    if (n < min) {
1128        min = n;
1129    }
1130
1131    ret = is_allocated_sectors(buf, n, pnum);
1132    if (!ret) {
1133        return ret;
1134    }
1135
1136    num_used = *pnum;
1137    buf += BDRV_SECTOR_SIZE * *pnum;
1138    n -= *pnum;
1139    num_checked = num_used;
1140
1141    while (n > 0) {
1142        ret = is_allocated_sectors(buf, n, pnum);
1143
1144        buf += BDRV_SECTOR_SIZE * *pnum;
1145        n -= *pnum;
1146        num_checked += *pnum;
1147        if (ret) {
1148            num_used = num_checked;
1149        } else if (*pnum >= min) {
1150            break;
1151        }
1152    }
1153
1154    *pnum = num_used;
1155    return 1;
1156}
1157
1158/*
1159 * Compares two buffers sector by sector. Returns 0 if the first
1160 * sector of each buffer matches, non-zero otherwise.
1161 *
1162 * pnum is set to the sector-aligned size of the buffer prefix that
1163 * has the same matching status as the first sector.
1164 */
1165static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1166                           int64_t bytes, int64_t *pnum)
1167{
1168    bool res;
1169    int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
1170
1171    assert(bytes > 0);
1172
1173    res = !!memcmp(buf1, buf2, i);
1174    while (i < bytes) {
1175        int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
1176
1177        if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
1178            break;
1179        }
1180        i += len;
1181    }
1182
1183    *pnum = i;
1184    return res;
1185}
1186
1187#define IO_BUF_SIZE (2 * 1024 * 1024)
1188
1189/*
1190 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1191 *
1192 * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1193 * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1194 * failure), and 4 on error (the exit status for read errors), after emitting
1195 * an error message.
1196 *
1197 * @param blk:  BlockBackend for the image
1198 * @param offset: Starting offset to check
1199 * @param bytes: Number of bytes to check
1200 * @param filename: Name of disk file we are checking (logging purpose)
1201 * @param buffer: Allocated buffer for storing read data
1202 * @param quiet: Flag for quiet mode
1203 */
1204static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1205                               int64_t bytes, const char *filename,
1206                               uint8_t *buffer, bool quiet)
1207{
1208    int ret = 0;
1209    int64_t idx;
1210
1211    ret = blk_pread(blk, offset, buffer, bytes);
1212    if (ret < 0) {
1213        error_report("Error while reading offset %" PRId64 " of %s: %s",
1214                     offset, filename, strerror(-ret));
1215        return 4;
1216    }
1217    idx = find_nonzero(buffer, bytes);
1218    if (idx >= 0) {
1219        qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1220                offset + idx);
1221        return 1;
1222    }
1223
1224    return 0;
1225}
1226
1227/*
1228 * Compares two images. Exit codes:
1229 *
1230 * 0 - Images are identical
1231 * 1 - Images differ
1232 * >1 - Error occurred
1233 */
1234static int img_compare(int argc, char **argv)
1235{
1236    const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
1237    BlockBackend *blk1, *blk2;
1238    BlockDriverState *bs1, *bs2;
1239    int64_t total_size1, total_size2;
1240    uint8_t *buf1 = NULL, *buf2 = NULL;
1241    int64_t pnum1, pnum2;
1242    int allocated1, allocated2;
1243    int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1244    bool progress = false, quiet = false, strict = false;
1245    int flags;
1246    bool writethrough;
1247    int64_t total_size;
1248    int64_t offset = 0;
1249    int64_t chunk;
1250    int c;
1251    uint64_t progress_base;
1252    bool image_opts = false;
1253    bool force_share = false;
1254
1255    cache = BDRV_DEFAULT_CACHE;
1256    for (;;) {
1257        static const struct option long_options[] = {
1258            {"help", no_argument, 0, 'h'},
1259            {"object", required_argument, 0, OPTION_OBJECT},
1260            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1261            {"force-share", no_argument, 0, 'U'},
1262            {0, 0, 0, 0}
1263        };
1264        c = getopt_long(argc, argv, ":hf:F:T:pqsU",
1265                        long_options, NULL);
1266        if (c == -1) {
1267            break;
1268        }
1269        switch (c) {
1270        case ':':
1271            missing_argument(argv[optind - 1]);
1272            break;
1273        case '?':
1274            unrecognized_option(argv[optind - 1]);
1275            break;
1276        case 'h':
1277            help();
1278            break;
1279        case 'f':
1280            fmt1 = optarg;
1281            break;
1282        case 'F':
1283            fmt2 = optarg;
1284            break;
1285        case 'T':
1286            cache = optarg;
1287            break;
1288        case 'p':
1289            progress = true;
1290            break;
1291        case 'q':
1292            quiet = true;
1293            break;
1294        case 's':
1295            strict = true;
1296            break;
1297        case 'U':
1298            force_share = true;
1299            break;
1300        case OPTION_OBJECT: {
1301            QemuOpts *opts;
1302            opts = qemu_opts_parse_noisily(&qemu_object_opts,
1303                                           optarg, true);
1304            if (!opts) {
1305                ret = 2;
1306                goto out4;
1307            }
1308        }   break;
1309        case OPTION_IMAGE_OPTS:
1310            image_opts = true;
1311            break;
1312        }
1313    }
1314
1315    /* Progress is not shown in Quiet mode */
1316    if (quiet) {
1317        progress = false;
1318    }
1319
1320
1321    if (optind != argc - 2) {
1322        error_exit("Expecting two image file names");
1323    }
1324    filename1 = argv[optind++];
1325    filename2 = argv[optind++];
1326
1327    if (qemu_opts_foreach(&qemu_object_opts,
1328                          user_creatable_add_opts_foreach,
1329                          NULL, NULL)) {
1330        ret = 2;
1331        goto out4;
1332    }
1333
1334    /* Initialize before goto out */
1335    qemu_progress_init(progress, 2.0);
1336
1337    flags = 0;
1338    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
1339    if (ret < 0) {
1340        error_report("Invalid source cache option: %s", cache);
1341        ret = 2;
1342        goto out3;
1343    }
1344
1345    blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1346                    force_share);
1347    if (!blk1) {
1348        ret = 2;
1349        goto out3;
1350    }
1351
1352    blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1353                    force_share);
1354    if (!blk2) {
1355        ret = 2;
1356        goto out2;
1357    }
1358    bs1 = blk_bs(blk1);
1359    bs2 = blk_bs(blk2);
1360
1361    buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1362    buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1363    total_size1 = blk_getlength(blk1);
1364    if (total_size1 < 0) {
1365        error_report("Can't get size of %s: %s",
1366                     filename1, strerror(-total_size1));
1367        ret = 4;
1368        goto out;
1369    }
1370    total_size2 = blk_getlength(blk2);
1371    if (total_size2 < 0) {
1372        error_report("Can't get size of %s: %s",
1373                     filename2, strerror(-total_size2));
1374        ret = 4;
1375        goto out;
1376    }
1377    total_size = MIN(total_size1, total_size2);
1378    progress_base = MAX(total_size1, total_size2);
1379
1380    qemu_progress_print(0, 100);
1381
1382    if (strict && total_size1 != total_size2) {
1383        ret = 1;
1384        qprintf(quiet, "Strict mode: Image size mismatch!\n");
1385        goto out;
1386    }
1387
1388    while (offset < total_size) {
1389        int status1, status2;
1390
1391        status1 = bdrv_block_status_above(bs1, NULL, offset,
1392                                          total_size1 - offset, &pnum1, NULL,
1393                                          NULL);
1394        if (status1 < 0) {
1395            ret = 3;
1396            error_report("Sector allocation test failed for %s", filename1);
1397            goto out;
1398        }
1399        allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
1400
1401        status2 = bdrv_block_status_above(bs2, NULL, offset,
1402                                          total_size2 - offset, &pnum2, NULL,
1403                                          NULL);
1404        if (status2 < 0) {
1405            ret = 3;
1406            error_report("Sector allocation test failed for %s", filename2);
1407            goto out;
1408        }
1409        allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1410
1411        assert(pnum1 && pnum2);
1412        chunk = MIN(pnum1, pnum2);
1413
1414        if (strict) {
1415            if (status1 != status2) {
1416                ret = 1;
1417                qprintf(quiet, "Strict mode: Offset %" PRId64
1418                        " block status mismatch!\n", offset);
1419                goto out;
1420            }
1421        }
1422        if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1423            /* nothing to do */
1424        } else if (allocated1 == allocated2) {
1425            if (allocated1) {
1426                int64_t pnum;
1427
1428                chunk = MIN(chunk, IO_BUF_SIZE);
1429                ret = blk_pread(blk1, offset, buf1, chunk);
1430                if (ret < 0) {
1431                    error_report("Error while reading offset %" PRId64
1432                                 " of %s: %s",
1433                                 offset, filename1, strerror(-ret));
1434                    ret = 4;
1435                    goto out;
1436                }
1437                ret = blk_pread(blk2, offset, buf2, chunk);
1438                if (ret < 0) {
1439                    error_report("Error while reading offset %" PRId64
1440                                 " of %s: %s",
1441                                 offset, filename2, strerror(-ret));
1442                    ret = 4;
1443                    goto out;
1444                }
1445                ret = compare_buffers(buf1, buf2, chunk, &pnum);
1446                if (ret || pnum != chunk) {
1447                    qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1448                            offset + (ret ? 0 : pnum));
1449                    ret = 1;
1450                    goto out;
1451                }
1452            }
1453        } else {
1454            chunk = MIN(chunk, IO_BUF_SIZE);
1455            if (allocated1) {
1456                ret = check_empty_sectors(blk1, offset, chunk,
1457                                          filename1, buf1, quiet);
1458            } else {
1459                ret = check_empty_sectors(blk2, offset, chunk,
1460                                          filename2, buf1, quiet);
1461            }
1462            if (ret) {
1463                goto out;
1464            }
1465        }
1466        offset += chunk;
1467        qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1468    }
1469
1470    if (total_size1 != total_size2) {
1471        BlockBackend *blk_over;
1472        const char *filename_over;
1473
1474        qprintf(quiet, "Warning: Image size mismatch!\n");
1475        if (total_size1 > total_size2) {
1476            blk_over = blk1;
1477            filename_over = filename1;
1478        } else {
1479            blk_over = blk2;
1480            filename_over = filename2;
1481        }
1482
1483        while (offset < progress_base) {
1484            ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1485                                          progress_base - offset, &chunk,
1486                                          NULL, NULL);
1487            if (ret < 0) {
1488                ret = 3;
1489                error_report("Sector allocation test failed for %s",
1490                             filename_over);
1491                goto out;
1492
1493            }
1494            if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
1495                chunk = MIN(chunk, IO_BUF_SIZE);
1496                ret = check_empty_sectors(blk_over, offset, chunk,
1497                                          filename_over, buf1, quiet);
1498                if (ret) {
1499                    goto out;
1500                }
1501            }
1502            offset += chunk;
1503            qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1504        }
1505    }
1506
1507    qprintf(quiet, "Images are identical.\n");
1508    ret = 0;
1509
1510out:
1511    qemu_vfree(buf1);
1512    qemu_vfree(buf2);
1513    blk_unref(blk2);
1514out2:
1515    blk_unref(blk1);
1516out3:
1517    qemu_progress_end();
1518out4:
1519    return ret;
1520}
1521
1522enum ImgConvertBlockStatus {
1523    BLK_DATA,
1524    BLK_ZERO,
1525    BLK_BACKING_FILE,
1526};
1527
1528#define MAX_COROUTINES 16
1529
1530typedef struct ImgConvertState {
1531    BlockBackend **src;
1532    int64_t *src_sectors;
1533    int src_num;
1534    int64_t total_sectors;
1535    int64_t allocated_sectors;
1536    int64_t allocated_done;
1537    int64_t sector_num;
1538    int64_t wr_offs;
1539    enum ImgConvertBlockStatus status;
1540    int64_t sector_next_status;
1541    BlockBackend *target;
1542    bool has_zero_init;
1543    bool compressed;
1544    bool target_has_backing;
1545    bool wr_in_order;
1546    int min_sparse;
1547    size_t cluster_sectors;
1548    size_t buf_sectors;
1549    long num_coroutines;
1550    int running_coroutines;
1551    Coroutine *co[MAX_COROUTINES];
1552    int64_t wait_sector_num[MAX_COROUTINES];
1553    CoMutex lock;
1554    int ret;
1555} ImgConvertState;
1556
1557static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1558                                int *src_cur, int64_t *src_cur_offset)
1559{
1560    *src_cur = 0;
1561    *src_cur_offset = 0;
1562    while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1563        *src_cur_offset += s->src_sectors[*src_cur];
1564        (*src_cur)++;
1565        assert(*src_cur < s->src_num);
1566    }
1567}
1568
1569static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1570{
1571    int64_t src_cur_offset;
1572    int ret, n, src_cur;
1573
1574    convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1575
1576    assert(s->total_sectors > sector_num);
1577    n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1578
1579    if (s->sector_next_status <= sector_num) {
1580        int64_t count = n * BDRV_SECTOR_SIZE;
1581
1582        if (s->target_has_backing) {
1583
1584            ret = bdrv_block_status(blk_bs(s->src[src_cur]),
1585                                    (sector_num - src_cur_offset) *
1586                                    BDRV_SECTOR_SIZE,
1587                                    count, &count, NULL, NULL);
1588        } else {
1589            ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL,
1590                                          (sector_num - src_cur_offset) *
1591                                          BDRV_SECTOR_SIZE,
1592                                          count, &count, NULL, NULL);
1593        }
1594        if (ret < 0) {
1595            return ret;
1596        }
1597        n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
1598
1599        if (ret & BDRV_BLOCK_ZERO) {
1600            s->status = BLK_ZERO;
1601        } else if (ret & BDRV_BLOCK_DATA) {
1602            s->status = BLK_DATA;
1603        } else {
1604            s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
1605        }
1606
1607        s->sector_next_status = sector_num + n;
1608    }
1609
1610    n = MIN(n, s->sector_next_status - sector_num);
1611    if (s->status == BLK_DATA) {
1612        n = MIN(n, s->buf_sectors);
1613    }
1614
1615    /* We need to write complete clusters for compressed images, so if an
1616     * unallocated area is shorter than that, we must consider the whole
1617     * cluster allocated. */
1618    if (s->compressed) {
1619        if (n < s->cluster_sectors) {
1620            n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1621            s->status = BLK_DATA;
1622        } else {
1623            n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1624        }
1625    }
1626
1627    return n;
1628}
1629
1630static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1631                                        int nb_sectors, uint8_t *buf)
1632{
1633    int n, ret;
1634    QEMUIOVector qiov;
1635    struct iovec iov;
1636
1637    assert(nb_sectors <= s->buf_sectors);
1638    while (nb_sectors > 0) {
1639        BlockBackend *blk;
1640        int src_cur;
1641        int64_t bs_sectors, src_cur_offset;
1642
1643        /* In the case of compression with multiple source files, we can get a
1644         * nb_sectors that spreads into the next part. So we must be able to
1645         * read across multiple BDSes for one convert_read() call. */
1646        convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1647        blk = s->src[src_cur];
1648        bs_sectors = s->src_sectors[src_cur];
1649
1650        n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1651        iov.iov_base = buf;
1652        iov.iov_len = n << BDRV_SECTOR_BITS;
1653        qemu_iovec_init_external(&qiov, &iov, 1);
1654
1655        ret = blk_co_preadv(
1656                blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1657                n << BDRV_SECTOR_BITS, &qiov, 0);
1658        if (ret < 0) {
1659            return ret;
1660        }
1661
1662        sector_num += n;
1663        nb_sectors -= n;
1664        buf += n * BDRV_SECTOR_SIZE;
1665    }
1666
1667    return 0;
1668}
1669
1670
1671static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1672                                         int nb_sectors, uint8_t *buf,
1673                                         enum ImgConvertBlockStatus status)
1674{
1675    int ret;
1676    QEMUIOVector qiov;
1677    struct iovec iov;
1678
1679    while (nb_sectors > 0) {
1680        int n = nb_sectors;
1681        BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1682
1683        switch (status) {
1684        case BLK_BACKING_FILE:
1685            /* If we have a backing file, leave clusters unallocated that are
1686             * unallocated in the source image, so that the backing file is
1687             * visible at the respective offset. */
1688            assert(s->target_has_backing);
1689            break;
1690
1691        case BLK_DATA:
1692            /* If we're told to keep the target fully allocated (-S 0) or there
1693             * is real non-zero data, we must write it. Otherwise we can treat
1694             * it as zero sectors.
1695             * Compressed clusters need to be written as a whole, so in that
1696             * case we can only save the write if the buffer is completely
1697             * zeroed. */
1698            if (!s->min_sparse ||
1699                (!s->compressed &&
1700                 is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1701                (s->compressed &&
1702                 !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
1703            {
1704                iov.iov_base = buf;
1705                iov.iov_len = n << BDRV_SECTOR_BITS;
1706                qemu_iovec_init_external(&qiov, &iov, 1);
1707
1708                ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1709                                     n << BDRV_SECTOR_BITS, &qiov, flags);
1710                if (ret < 0) {
1711                    return ret;
1712                }
1713                break;
1714            }
1715            /* fall-through */
1716
1717        case BLK_ZERO:
1718            if (s->has_zero_init) {
1719                assert(!s->target_has_backing);
1720                break;
1721            }
1722            ret = blk_co_pwrite_zeroes(s->target,
1723                                       sector_num << BDRV_SECTOR_BITS,
1724                                       n << BDRV_SECTOR_BITS, 0);
1725            if (ret < 0) {
1726                return ret;
1727            }
1728            break;
1729        }
1730
1731        sector_num += n;
1732        nb_sectors -= n;
1733        buf += n * BDRV_SECTOR_SIZE;
1734    }
1735
1736    return 0;
1737}
1738
1739static void coroutine_fn convert_co_do_copy(void *opaque)
1740{
1741    ImgConvertState *s = opaque;
1742    uint8_t *buf = NULL;
1743    int ret, i;
1744    int index = -1;
1745
1746    for (i = 0; i < s->num_coroutines; i++) {
1747        if (s->co[i] == qemu_coroutine_self()) {
1748            index = i;
1749            break;
1750        }
1751    }
1752    assert(index >= 0);
1753
1754    s->running_coroutines++;
1755    buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1756
1757    while (1) {
1758        int n;
1759        int64_t sector_num;
1760        enum ImgConvertBlockStatus status;
1761
1762        qemu_co_mutex_lock(&s->lock);
1763        if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1764            qemu_co_mutex_unlock(&s->lock);
1765            break;
1766        }
1767        n = convert_iteration_sectors(s, s->sector_num);
1768        if (n < 0) {
1769            qemu_co_mutex_unlock(&s->lock);
1770            s->ret = n;
1771            break;
1772        }
1773        /* save current sector and allocation status to local variables */
1774        sector_num = s->sector_num;
1775        status = s->status;
1776        if (!s->min_sparse && s->status == BLK_ZERO) {
1777            n = MIN(n, s->buf_sectors);
1778        }
1779        /* increment global sector counter so that other coroutines can
1780         * already continue reading beyond this request */
1781        s->sector_num += n;
1782        qemu_co_mutex_unlock(&s->lock);
1783
1784        if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1785            s->allocated_done += n;
1786            qemu_progress_print(100.0 * s->allocated_done /
1787                                        s->allocated_sectors, 0);
1788        }
1789
1790        if (status == BLK_DATA) {
1791            ret = convert_co_read(s, sector_num, n, buf);
1792            if (ret < 0) {
1793                error_report("error while reading sector %" PRId64
1794                             ": %s", sector_num, strerror(-ret));
1795                s->ret = ret;
1796            }
1797        } else if (!s->min_sparse && status == BLK_ZERO) {
1798            status = BLK_DATA;
1799            memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1800        }
1801
1802        if (s->wr_in_order) {
1803            /* keep writes in order */
1804            while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
1805                s->wait_sector_num[index] = sector_num;
1806                qemu_coroutine_yield();
1807            }
1808            s->wait_sector_num[index] = -1;
1809        }
1810
1811        if (s->ret == -EINPROGRESS) {
1812            ret = convert_co_write(s, sector_num, n, buf, status);
1813            if (ret < 0) {
1814                error_report("error while writing sector %" PRId64
1815                             ": %s", sector_num, strerror(-ret));
1816                s->ret = ret;
1817            }
1818        }
1819
1820        if (s->wr_in_order) {
1821            /* reenter the coroutine that might have waited
1822             * for this write to complete */
1823            s->wr_offs = sector_num + n;
1824            for (i = 0; i < s->num_coroutines; i++) {
1825                if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1826                    /*
1827                     * A -> B -> A cannot occur because A has
1828                     * s->wait_sector_num[i] == -1 during A -> B.  Therefore
1829                     * B will never enter A during this time window.
1830                     */
1831                    qemu_coroutine_enter(s->co[i]);
1832                    break;
1833                }
1834            }
1835        }
1836    }
1837
1838    qemu_vfree(buf);
1839    s->co[index] = NULL;
1840    s->running_coroutines--;
1841    if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1842        /* the convert job finished successfully */
1843        s->ret = 0;
1844    }
1845}
1846
1847static int convert_do_copy(ImgConvertState *s)
1848{
1849    int ret, i, n;
1850    int64_t sector_num = 0;
1851
1852    /* Check whether we have zero initialisation or can get it efficiently */
1853    s->has_zero_init = s->min_sparse && !s->target_has_backing
1854                     ? bdrv_has_zero_init(blk_bs(s->target))
1855                     : false;
1856
1857    if (!s->has_zero_init && !s->target_has_backing &&
1858        bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1859    {
1860        ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
1861        if (ret == 0) {
1862            s->has_zero_init = true;
1863        }
1864    }
1865
1866    /* Allocate buffer for copied data. For compressed images, only one cluster
1867     * can be copied at a time. */
1868    if (s->compressed) {
1869        if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1870            error_report("invalid cluster size");
1871            return -EINVAL;
1872        }
1873        s->buf_sectors = s->cluster_sectors;
1874    }
1875
1876    while (sector_num < s->total_sectors) {
1877        n = convert_iteration_sectors(s, sector_num);
1878        if (n < 0) {
1879            return n;
1880        }
1881        if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1882        {
1883            s->allocated_sectors += n;
1884        }
1885        sector_num += n;
1886    }
1887
1888    /* Do the copy */
1889    s->sector_next_status = 0;
1890    s->ret = -EINPROGRESS;
1891
1892    qemu_co_mutex_init(&s->lock);
1893    for (i = 0; i < s->num_coroutines; i++) {
1894        s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1895        s->wait_sector_num[i] = -1;
1896        qemu_coroutine_enter(s->co[i]);
1897    }
1898
1899    while (s->running_coroutines) {
1900        main_loop_wait(false);
1901    }
1902
1903    if (s->compressed && !s->ret) {
1904        /* signal EOF to align */
1905        ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
1906        if (ret < 0) {
1907            return ret;
1908        }
1909    }
1910
1911    return s->ret;
1912}
1913
1914static int img_convert(int argc, char **argv)
1915{
1916    int c, bs_i, flags, src_flags = 0;
1917    const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
1918               *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1919               *out_filename, *out_baseimg_param, *snapshot_name = NULL;
1920    BlockDriver *drv = NULL, *proto_drv = NULL;
1921    BlockDriverInfo bdi;
1922    BlockDriverState *out_bs;
1923    QemuOpts *opts = NULL, *sn_opts = NULL;
1924    QemuOptsList *create_opts = NULL;
1925    char *options = NULL;
1926    Error *local_err = NULL;
1927    bool writethrough, src_writethrough, quiet = false, image_opts = false,
1928         skip_create = false, progress = false, tgt_image_opts = false;
1929    int64_t ret = -EINVAL;
1930    bool force_share = false;
1931
1932    ImgConvertState s = (ImgConvertState) {
1933        /* Need at least 4k of zeros for sparse detection */
1934        .min_sparse         = 8,
1935        .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1936        .wr_in_order        = true,
1937        .num_coroutines     = 8,
1938    };
1939
1940    for(;;) {
1941        static const struct option long_options[] = {
1942            {"help", no_argument, 0, 'h'},
1943            {"object", required_argument, 0, OPTION_OBJECT},
1944            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1945            {"force-share", no_argument, 0, 'U'},
1946            {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
1947            {0, 0, 0, 0}
1948        };
1949        c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
1950                        long_options, NULL);
1951        if (c == -1) {
1952            break;
1953        }
1954        switch(c) {
1955        case ':':
1956            missing_argument(argv[optind - 1]);
1957            break;
1958        case '?':
1959            unrecognized_option(argv[optind - 1]);
1960            break;
1961        case 'h':
1962            help();
1963            break;
1964        case 'f':
1965            fmt = optarg;
1966            break;
1967        case 'O':
1968            out_fmt = optarg;
1969            break;
1970        case 'B':
1971            out_baseimg = optarg;
1972            break;
1973        case 'c':
1974            s.compressed = true;
1975            break;
1976        case 'o':
1977            if (!is_valid_option_list(optarg)) {
1978                error_report("Invalid option list: %s", optarg);
1979                goto fail_getopt;
1980            }
1981            if (!options) {
1982                options = g_strdup(optarg);
1983            } else {
1984                char *old_options = options;
1985                options = g_strdup_printf("%s,%s", options, optarg);
1986                g_free(old_options);
1987            }
1988            break;
1989        case 's':
1990            snapshot_name = optarg;
1991            break;
1992        case 'l':
1993            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1994                sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
1995                                                  optarg, false);
1996                if (!sn_opts) {
1997                    error_report("Failed in parsing snapshot param '%s'",
1998                                 optarg);
1999                    goto fail_getopt;
2000                }
2001            } else {
2002                snapshot_name = optarg;
2003            }
2004            break;
2005        case 'S':
2006        {
2007            int64_t sval;
2008
2009            sval = cvtnum(optarg);
2010            if (sval < 0) {
2011                error_report("Invalid minimum zero buffer size for sparse output specified");
2012                goto fail_getopt;
2013            }
2014
2015            s.min_sparse = sval / BDRV_SECTOR_SIZE;
2016            break;
2017        }
2018        case 'p':
2019            progress = true;
2020            break;
2021        case 't':
2022            cache = optarg;
2023            break;
2024        case 'T':
2025            src_cache = optarg;
2026            break;
2027        case 'q':
2028            quiet = true;
2029            break;
2030        case 'n':
2031            skip_create = true;
2032            break;
2033        case 'm':
2034            if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2035                s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2036                error_report("Invalid number of coroutines. Allowed number of"
2037                             " coroutines is between 1 and %d", MAX_COROUTINES);
2038                goto fail_getopt;
2039            }
2040            break;
2041        case 'W':
2042            s.wr_in_order = false;
2043            break;
2044        case 'U':
2045            force_share = true;
2046            break;
2047        case OPTION_OBJECT: {
2048            QemuOpts *object_opts;
2049            object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2050                                                  optarg, true);
2051            if (!object_opts) {
2052                goto fail_getopt;
2053            }
2054            break;
2055        }
2056        case OPTION_IMAGE_OPTS:
2057            image_opts = true;
2058            break;
2059        case OPTION_TARGET_IMAGE_OPTS:
2060            tgt_image_opts = true;
2061            break;
2062        }
2063    }
2064
2065    if (!out_fmt && !tgt_image_opts) {
2066        out_fmt = "raw";
2067    }
2068
2069    if (qemu_opts_foreach(&qemu_object_opts,
2070                          user_creatable_add_opts_foreach,
2071                          NULL, NULL)) {
2072        goto fail_getopt;
2073    }
2074
2075    if (!s.wr_in_order && s.compressed) {
2076        error_report("Out of order write and compress are mutually exclusive");
2077        goto fail_getopt;
2078    }
2079
2080    if (tgt_image_opts && !skip_create) {
2081        error_report("--target-image-opts requires use of -n flag");
2082        goto fail_getopt;
2083    }
2084
2085    s.src_num = argc - optind - 1;
2086    out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2087
2088    if (options && has_help_option(options)) {
2089        if (out_fmt) {
2090            ret = print_block_option_help(out_filename, out_fmt);
2091            goto fail_getopt;
2092        } else {
2093            error_report("Option help requires a format be specified");
2094            goto fail_getopt;
2095        }
2096    }
2097
2098    if (s.src_num < 1) {
2099        error_report("Must specify image file name");
2100        goto fail_getopt;
2101    }
2102
2103
2104    /* ret is still -EINVAL until here */
2105    ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2106    if (ret < 0) {
2107        error_report("Invalid source cache option: %s", src_cache);
2108        goto fail_getopt;
2109    }
2110
2111    /* Initialize before goto out */
2112    if (quiet) {
2113        progress = false;
2114    }
2115    qemu_progress_init(progress, 1.0);
2116    qemu_progress_print(0, 100);
2117
2118    s.src = g_new0(BlockBackend *, s.src_num);
2119    s.src_sectors = g_new(int64_t, s.src_num);
2120
2121    for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2122        s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2123                               fmt, src_flags, src_writethrough, quiet,
2124                               force_share);
2125        if (!s.src[bs_i]) {
2126            ret = -1;
2127            goto out;
2128        }
2129        s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2130        if (s.src_sectors[bs_i] < 0) {
2131            error_report("Could not get size of %s: %s",
2132                         argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
2133            ret = -1;
2134            goto out;
2135        }
2136        s.total_sectors += s.src_sectors[bs_i];
2137    }
2138
2139    if (sn_opts) {
2140        bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
2141                               qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2142                               qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2143                               &local_err);
2144    } else if (snapshot_name != NULL) {
2145        if (s.src_num > 1) {
2146            error_report("No support for concatenating multiple snapshot");
2147            ret = -1;
2148            goto out;
2149        }
2150
2151        bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2152                                             &local_err);
2153    }
2154    if (local_err) {
2155        error_reportf_err(local_err, "Failed to load snapshot: ");
2156        ret = -1;
2157        goto out;
2158    }
2159
2160    if (!skip_create) {
2161        /* Find driver and parse its options */
2162        drv = bdrv_find_format(out_fmt);
2163        if (!drv) {
2164            error_report("Unknown file format '%s'", out_fmt);
2165            ret = -1;
2166            goto out;
2167        }
2168
2169        proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2170        if (!proto_drv) {
2171            error_report_err(local_err);
2172            ret = -1;
2173            goto out;
2174        }
2175
2176        if (!drv->create_opts) {
2177            error_report("Format driver '%s' does not support image creation",
2178                         drv->format_name);
2179            ret = -1;
2180            goto out;
2181        }
2182
2183        if (!proto_drv->create_opts) {
2184            error_report("Protocol driver '%s' does not support image creation",
2185                         proto_drv->format_name);
2186            ret = -1;
2187            goto out;
2188        }
2189
2190        create_opts = qemu_opts_append(create_opts, drv->create_opts);
2191        create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
2192
2193        opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2194        if (options) {
2195            qemu_opts_do_parse(opts, options, NULL, &local_err);
2196            if (local_err) {
2197                error_report_err(local_err);
2198                ret = -1;
2199                goto out;
2200            }
2201        }
2202
2203        qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
2204                            &error_abort);
2205        ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2206        if (ret < 0) {
2207            goto out;
2208        }
2209    }
2210
2211    /* Get backing file name if -o backing_file was used */
2212    out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2213    if (out_baseimg_param) {
2214        out_baseimg = out_baseimg_param;
2215    }
2216    s.target_has_backing = (bool) out_baseimg;
2217
2218    if (s.src_num > 1 && out_baseimg) {
2219        error_report("Having a backing file for the target makes no sense when "
2220                     "concatenating multiple input images");
2221        ret = -1;
2222        goto out;
2223    }
2224
2225    /* Check if compression is supported */
2226    if (s.compressed) {
2227        bool encryption =
2228            qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2229        const char *encryptfmt =
2230            qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2231        const char *preallocation =
2232            qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
2233
2234        if (drv && !drv->bdrv_co_pwritev_compressed) {
2235            error_report("Compression not supported for this file format");
2236            ret = -1;
2237            goto out;
2238        }
2239
2240        if (encryption || encryptfmt) {
2241            error_report("Compression and encryption not supported at "
2242                         "the same time");
2243            ret = -1;
2244            goto out;
2245        }
2246
2247        if (preallocation
2248            && strcmp(preallocation, "off"))
2249        {
2250            error_report("Compression and preallocation not supported at "
2251                         "the same time");
2252            ret = -1;
2253            goto out;
2254        }
2255    }
2256
2257    if (!skip_create) {
2258        /* Create the new image */
2259        ret = bdrv_create(drv, out_filename, opts, &local_err);
2260        if (ret < 0) {
2261            error_reportf_err(local_err, "%s: error while converting %s: ",
2262                              out_filename, out_fmt);
2263            goto out;
2264        }
2265    }
2266
2267    flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
2268    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
2269    if (ret < 0) {
2270        error_report("Invalid cache option: %s", cache);
2271        goto out;
2272    }
2273
2274    if (skip_create) {
2275        s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2276                            flags, writethrough, quiet, false);
2277    } else {
2278        /* TODO ultimately we should allow --target-image-opts
2279         * to be used even when -n is not given.
2280         * That has to wait for bdrv_create to be improved
2281         * to allow filenames in option syntax
2282         */
2283        s.target = img_open_new_file(out_filename, opts, out_fmt,
2284                                     flags, writethrough, quiet, false);
2285    }
2286    if (!s.target) {
2287        ret = -1;
2288        goto out;
2289    }
2290    out_bs = blk_bs(s.target);
2291
2292    if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2293        error_report("Compression not supported for this file format");
2294        ret = -1;
2295        goto out;
2296    }
2297
2298    /* increase bufsectors from the default 4096 (2M) if opt_transfer
2299     * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2300     * as maximum. */
2301    s.buf_sectors = MIN(32768,
2302                        MAX(s.buf_sectors,
2303                            MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2304                                out_bs->bl.pdiscard_alignment >>
2305                                BDRV_SECTOR_BITS)));
2306
2307    if (skip_create) {
2308        int64_t output_sectors = blk_nb_sectors(s.target);
2309        if (output_sectors < 0) {
2310            error_report("unable to get output image length: %s",
2311                         strerror(-output_sectors));
2312            ret = -1;
2313            goto out;
2314        } else if (output_sectors < s.total_sectors) {
2315            error_report("output file is smaller than input file");
2316            ret = -1;
2317            goto out;
2318        }
2319    }
2320
2321    ret = bdrv_get_info(out_bs, &bdi);
2322    if (ret < 0) {
2323        if (s.compressed) {
2324            error_report("could not get block driver info");
2325            goto out;
2326        }
2327    } else {
2328        s.compressed = s.compressed || bdi.needs_compressed_writes;
2329        s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2330    }
2331
2332    ret = convert_do_copy(&s);
2333out:
2334    if (!ret) {
2335        qemu_progress_print(100, 0);
2336    }
2337    qemu_progress_end();
2338    qemu_opts_del(opts);
2339    qemu_opts_free(create_opts);
2340    qemu_opts_del(sn_opts);
2341    blk_unref(s.target);
2342    if (s.src) {
2343        for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2344            blk_unref(s.src[bs_i]);
2345        }
2346        g_free(s.src);
2347    }
2348    g_free(s.src_sectors);
2349fail_getopt:
2350    g_free(options);
2351
2352    return !!ret;
2353}
2354
2355
2356static void dump_snapshots(BlockDriverState *bs)
2357{
2358    QEMUSnapshotInfo *sn_tab, *sn;
2359    int nb_sns, i;
2360
2361    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2362    if (nb_sns <= 0)
2363        return;
2364    printf("Snapshot list:\n");
2365    bdrv_snapshot_dump(fprintf, stdout, NULL);
2366    printf("\n");
2367    for(i = 0; i < nb_sns; i++) {
2368        sn = &sn_tab[i];
2369        bdrv_snapshot_dump(fprintf, stdout, sn);
2370        printf("\n");
2371    }
2372    g_free(sn_tab);
2373}
2374
2375static void dump_json_image_info_list(ImageInfoList *list)
2376{
2377    QString *str;
2378    QObject *obj;
2379    Visitor *v = qobject_output_visitor_new(&obj);
2380
2381    visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2382    visit_complete(v, &obj);
2383    str = qobject_to_json_pretty(obj);
2384    assert(str != NULL);
2385    printf("%s\n", qstring_get_str(str));
2386    qobject_decref(obj);
2387    visit_free(v);
2388    QDECREF(str);
2389}
2390
2391static void dump_json_image_info(ImageInfo *info)
2392{
2393    QString *str;
2394    QObject *obj;
2395    Visitor *v = qobject_output_visitor_new(&obj);
2396
2397    visit_type_ImageInfo(v, NULL, &info, &error_abort);
2398    visit_complete(v, &obj);
2399    str = qobject_to_json_pretty(obj);
2400    assert(str != NULL);
2401    printf("%s\n", qstring_get_str(str));
2402    qobject_decref(obj);
2403    visit_free(v);
2404    QDECREF(str);
2405}
2406
2407static void dump_human_image_info_list(ImageInfoList *list)
2408{
2409    ImageInfoList *elem;
2410    bool delim = false;
2411
2412    for (elem = list; elem; elem = elem->next) {
2413        if (delim) {
2414            printf("\n");
2415        }
2416        delim = true;
2417
2418        bdrv_image_info_dump(fprintf, stdout, elem->value);
2419    }
2420}
2421
2422static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2423{
2424    return strcmp(a, b) == 0;
2425}
2426
2427/**
2428 * Open an image file chain and return an ImageInfoList
2429 *
2430 * @filename: topmost image filename
2431 * @fmt: topmost image format (may be NULL to autodetect)
2432 * @chain: true  - enumerate entire backing file chain
2433 *         false - only topmost image file
2434 *
2435 * Returns a list of ImageInfo objects or NULL if there was an error opening an
2436 * image file.  If there was an error a message will have been printed to
2437 * stderr.
2438 */
2439static ImageInfoList *collect_image_info_list(bool image_opts,
2440                                              const char *filename,
2441                                              const char *fmt,
2442                                              bool chain, bool force_share)
2443{
2444    ImageInfoList *head = NULL;
2445    ImageInfoList **last = &head;
2446    GHashTable *filenames;
2447    Error *err = NULL;
2448
2449    filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2450
2451    while (filename) {
2452        BlockBackend *blk;
2453        BlockDriverState *bs;
2454        ImageInfo *info;
2455        ImageInfoList *elem;
2456
2457        if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2458            error_report("Backing file '%s' creates an infinite loop.",
2459                         filename);
2460            goto err;
2461        }
2462        g_hash_table_insert(filenames, (gpointer)filename, NULL);
2463
2464        blk = img_open(image_opts, filename, fmt,
2465                       BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2466                       force_share);
2467        if (!blk) {
2468            goto err;
2469        }
2470        bs = blk_bs(blk);
2471
2472        bdrv_query_image_info(bs, &info, &err);
2473        if (err) {
2474            error_report_err(err);
2475            blk_unref(blk);
2476            goto err;
2477        }
2478
2479        elem = g_new0(ImageInfoList, 1);
2480        elem->value = info;
2481        *last = elem;
2482        last = &elem->next;
2483
2484        blk_unref(blk);
2485
2486        filename = fmt = NULL;
2487        if (chain) {
2488            if (info->has_full_backing_filename) {
2489                filename = info->full_backing_filename;
2490            } else if (info->has_backing_filename) {
2491                error_report("Could not determine absolute backing filename,"
2492                             " but backing filename '%s' present",
2493                             info->backing_filename);
2494                goto err;
2495            }
2496            if (info->has_backing_filename_format) {
2497                fmt = info->backing_filename_format;
2498            }
2499        }
2500    }
2501    g_hash_table_destroy(filenames);
2502    return head;
2503
2504err:
2505    qapi_free_ImageInfoList(head);
2506    g_hash_table_destroy(filenames);
2507    return NULL;
2508}
2509
2510static int img_info(int argc, char **argv)
2511{
2512    int c;
2513    OutputFormat output_format = OFORMAT_HUMAN;
2514    bool chain = false;
2515    const char *filename, *fmt, *output;
2516    ImageInfoList *list;
2517    bool image_opts = false;
2518    bool force_share = false;
2519
2520    fmt = NULL;
2521    output = NULL;
2522    for(;;) {
2523        int option_index = 0;
2524        static const struct option long_options[] = {
2525            {"help", no_argument, 0, 'h'},
2526            {"format", required_argument, 0, 'f'},
2527            {"output", required_argument, 0, OPTION_OUTPUT},
2528            {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
2529            {"object", required_argument, 0, OPTION_OBJECT},
2530            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2531            {"force-share", no_argument, 0, 'U'},
2532            {0, 0, 0, 0}
2533        };
2534        c = getopt_long(argc, argv, ":f:hU",
2535                        long_options, &option_index);
2536        if (c == -1) {
2537            break;
2538        }
2539        switch(c) {
2540        case ':':
2541            missing_argument(argv[optind - 1]);
2542            break;
2543        case '?':
2544            unrecognized_option(argv[optind - 1]);
2545            break;
2546        case 'h':
2547            help();
2548            break;
2549        case 'f':
2550            fmt = optarg;
2551            break;
2552        case 'U':
2553            force_share = true;
2554            break;
2555        case OPTION_OUTPUT:
2556            output = optarg;
2557            break;
2558        case OPTION_BACKING_CHAIN:
2559            chain = true;
2560            break;
2561        case OPTION_OBJECT: {
2562            QemuOpts *opts;
2563            opts = qemu_opts_parse_noisily(&qemu_object_opts,
2564                                           optarg, true);
2565            if (!opts) {
2566                return 1;
2567            }
2568        }   break;
2569        case OPTION_IMAGE_OPTS:
2570            image_opts = true;
2571            break;
2572        }
2573    }
2574    if (optind != argc - 1) {
2575        error_exit("Expecting one image file name");
2576    }
2577    filename = argv[optind++];
2578
2579    if (output && !strcmp(output, "json")) {
2580        output_format = OFORMAT_JSON;
2581    } else if (output && !strcmp(output, "human")) {
2582        output_format = OFORMAT_HUMAN;
2583    } else if (output) {
2584        error_report("--output must be used with human or json as argument.");
2585        return 1;
2586    }
2587
2588    if (qemu_opts_foreach(&qemu_object_opts,
2589                          user_creatable_add_opts_foreach,
2590                          NULL, NULL)) {
2591        return 1;
2592    }
2593
2594    list = collect_image_info_list(image_opts, filename, fmt, chain,
2595                                   force_share);
2596    if (!list) {
2597        return 1;
2598    }
2599
2600    switch (output_format) {
2601    case OFORMAT_HUMAN:
2602        dump_human_image_info_list(list);
2603        break;
2604    case OFORMAT_JSON:
2605        if (chain) {
2606            dump_json_image_info_list(list);
2607        } else {
2608            dump_json_image_info(list->value);
2609        }
2610        break;
2611    }
2612
2613    qapi_free_ImageInfoList(list);
2614    return 0;
2615}
2616
2617static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2618                           MapEntry *next)
2619{
2620    switch (output_format) {
2621    case OFORMAT_HUMAN:
2622        if (e->data && !e->has_offset) {
2623            error_report("File contains external, encrypted or compressed clusters.");
2624            exit(1);
2625        }
2626        if (e->data && !e->zero) {
2627            printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
2628                   e->start, e->length,
2629                   e->has_offset ? e->offset : 0,
2630                   e->has_filename ? e->filename : "");
2631        }
2632        /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2633         * Modify the flags here to allow more coalescing.
2634         */
2635        if (next && (!next->data || next->zero)) {
2636            next->data = false;
2637            next->zero = true;
2638        }
2639        break;
2640    case OFORMAT_JSON:
2641        printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2642               " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
2643               (e->start == 0 ? "[" : ",\n"),
2644               e->start, e->length, e->depth,
2645               e->zero ? "true" : "false",
2646               e->data ? "true" : "false");
2647        if (e->has_offset) {
2648            printf(", \"offset\": %"PRId64"", e->offset);
2649        }
2650        putchar('}');
2651
2652        if (!next) {
2653            printf("]\n");
2654        }
2655        break;
2656    }
2657}
2658
2659static int get_block_status(BlockDriverState *bs, int64_t offset,
2660                            int64_t bytes, MapEntry *e)
2661{
2662    int ret;
2663    int depth;
2664    BlockDriverState *file;
2665    bool has_offset;
2666    int64_t map;
2667
2668    /* As an optimization, we could cache the current range of unallocated
2669     * clusters in each file of the chain, and avoid querying the same
2670     * range repeatedly.
2671     */
2672
2673    depth = 0;
2674    for (;;) {
2675        ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
2676        if (ret < 0) {
2677            return ret;
2678        }
2679        assert(bytes);
2680        if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2681            break;
2682        }
2683        bs = backing_bs(bs);
2684        if (bs == NULL) {
2685            ret = 0;
2686            break;
2687        }
2688
2689        depth++;
2690    }
2691
2692    has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2693
2694    *e = (MapEntry) {
2695        .start = offset,
2696        .length = bytes,
2697        .data = !!(ret & BDRV_BLOCK_DATA),
2698        .zero = !!(ret & BDRV_BLOCK_ZERO),
2699        .offset = map,
2700        .has_offset = has_offset,
2701        .depth = depth,
2702        .has_filename = file && has_offset,
2703        .filename = file && has_offset ? file->filename : NULL,
2704    };
2705
2706    return 0;
2707}
2708
2709static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2710{
2711    if (curr->length == 0) {
2712        return false;
2713    }
2714    if (curr->zero != next->zero ||
2715        curr->data != next->data ||
2716        curr->depth != next->depth ||
2717        curr->has_filename != next->has_filename ||
2718        curr->has_offset != next->has_offset) {
2719        return false;
2720    }
2721    if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2722        return false;
2723    }
2724    if (curr->has_offset && curr->offset + curr->length != next->offset) {
2725        return false;
2726    }
2727    return true;
2728}
2729
2730static int img_map(int argc, char **argv)
2731{
2732    int c;
2733    OutputFormat output_format = OFORMAT_HUMAN;
2734    BlockBackend *blk;
2735    BlockDriverState *bs;
2736    const char *filename, *fmt, *output;
2737    int64_t length;
2738    MapEntry curr = { .length = 0 }, next;
2739    int ret = 0;
2740    bool image_opts = false;
2741    bool force_share = false;
2742
2743    fmt = NULL;
2744    output = NULL;
2745    for (;;) {
2746        int option_index = 0;
2747        static const struct option long_options[] = {
2748            {"help", no_argument, 0, 'h'},
2749            {"format", required_argument, 0, 'f'},
2750            {"output", required_argument, 0, OPTION_OUTPUT},
2751            {"object", required_argument, 0, OPTION_OBJECT},
2752            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2753            {"force-share", no_argument, 0, 'U'},
2754            {0, 0, 0, 0}
2755        };
2756        c = getopt_long(argc, argv, ":f:hU",
2757                        long_options, &option_index);
2758        if (c == -1) {
2759            break;
2760        }
2761        switch (c) {
2762        case ':':
2763            missing_argument(argv[optind - 1]);
2764            break;
2765        case '?':
2766            unrecognized_option(argv[optind - 1]);
2767            break;
2768        case 'h':
2769            help();
2770            break;
2771        case 'f':
2772            fmt = optarg;
2773            break;
2774        case 'U':
2775            force_share = true;
2776            break;
2777        case OPTION_OUTPUT:
2778            output = optarg;
2779            break;
2780        case OPTION_OBJECT: {
2781            QemuOpts *opts;
2782            opts = qemu_opts_parse_noisily(&qemu_object_opts,
2783                                           optarg, true);
2784            if (!opts) {
2785                return 1;
2786            }
2787        }   break;
2788        case OPTION_IMAGE_OPTS:
2789            image_opts = true;
2790            break;
2791        }
2792    }
2793    if (optind != argc - 1) {
2794        error_exit("Expecting one image file name");
2795    }
2796    filename = argv[optind];
2797
2798    if (output && !strcmp(output, "json")) {
2799        output_format = OFORMAT_JSON;
2800    } else if (output && !strcmp(output, "human")) {
2801        output_format = OFORMAT_HUMAN;
2802    } else if (output) {
2803        error_report("--output must be used with human or json as argument.");
2804        return 1;
2805    }
2806
2807    if (qemu_opts_foreach(&qemu_object_opts,
2808                          user_creatable_add_opts_foreach,
2809                          NULL, NULL)) {
2810        return 1;
2811    }
2812
2813    blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
2814    if (!blk) {
2815        return 1;
2816    }
2817    bs = blk_bs(blk);
2818
2819    if (output_format == OFORMAT_HUMAN) {
2820        printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2821    }
2822
2823    length = blk_getlength(blk);
2824    while (curr.start + curr.length < length) {
2825        int64_t offset = curr.start + curr.length;
2826        int64_t n;
2827
2828        /* Probe up to 1 GiB at a time.  */
2829        n = MIN(1 << 30, length - offset);
2830        ret = get_block_status(bs, offset, n, &next);
2831
2832        if (ret < 0) {
2833            error_report("Could not read file metadata: %s", strerror(-ret));
2834            goto out;
2835        }
2836
2837        if (entry_mergeable(&curr, &next)) {
2838            curr.length += next.length;
2839            continue;
2840        }
2841
2842        if (curr.length > 0) {
2843            dump_map_entry(output_format, &curr, &next);
2844        }
2845        curr = next;
2846    }
2847
2848    dump_map_entry(output_format, &curr, NULL);
2849
2850out:
2851    blk_unref(blk);
2852    return ret < 0;
2853}
2854
2855#define SNAPSHOT_LIST   1
2856#define SNAPSHOT_CREATE 2
2857#define SNAPSHOT_APPLY  3
2858#define SNAPSHOT_DELETE 4
2859
2860static int img_snapshot(int argc, char **argv)
2861{
2862    BlockBackend *blk;
2863    BlockDriverState *bs;
2864    QEMUSnapshotInfo sn;
2865    char *filename, *snapshot_name = NULL;
2866    int c, ret = 0, bdrv_oflags;
2867    int action = 0;
2868    qemu_timeval tv;
2869    bool quiet = false;
2870    Error *err = NULL;
2871    bool image_opts = false;
2872    bool force_share = false;
2873
2874    bdrv_oflags = BDRV_O_RDWR;
2875    /* Parse commandline parameters */
2876    for(;;) {
2877        static const struct option long_options[] = {
2878            {"help", no_argument, 0, 'h'},
2879            {"object", required_argument, 0, OPTION_OBJECT},
2880            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2881            {"force-share", no_argument, 0, 'U'},
2882            {0, 0, 0, 0}
2883        };
2884        c = getopt_long(argc, argv, ":la:c:d:hqU",
2885                        long_options, NULL);
2886        if (c == -1) {
2887            break;
2888        }
2889        switch(c) {
2890        case ':':
2891            missing_argument(argv[optind - 1]);
2892            break;
2893        case '?':
2894            unrecognized_option(argv[optind - 1]);
2895            break;
2896        case 'h':
2897            help();
2898            return 0;
2899        case 'l':
2900            if (action) {
2901                error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2902                return 0;
2903            }
2904            action = SNAPSHOT_LIST;
2905            bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
2906            break;
2907        case 'a':
2908            if (action) {
2909                error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2910                return 0;
2911            }
2912            action = SNAPSHOT_APPLY;
2913            snapshot_name = optarg;
2914            break;
2915        case 'c':
2916            if (action) {
2917                error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2918                return 0;
2919            }
2920            action = SNAPSHOT_CREATE;
2921            snapshot_name = optarg;
2922            break;
2923        case 'd':
2924            if (action) {
2925                error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2926                return 0;
2927            }
2928            action = SNAPSHOT_DELETE;
2929            snapshot_name = optarg;
2930            break;
2931        case 'q':
2932            quiet = true;
2933            break;
2934        case 'U':
2935            force_share = true;
2936            break;
2937        case OPTION_OBJECT: {
2938            QemuOpts *opts;
2939            opts = qemu_opts_parse_noisily(&qemu_object_opts,
2940                                           optarg, true);
2941            if (!opts) {
2942                return 1;
2943            }
2944        }   break;
2945        case OPTION_IMAGE_OPTS:
2946            image_opts = true;
2947            break;
2948        }
2949    }
2950
2951    if (optind != argc - 1) {
2952        error_exit("Expecting one image file name");
2953    }
2954    filename = argv[optind++];
2955
2956    if (qemu_opts_foreach(&qemu_object_opts,
2957                          user_creatable_add_opts_foreach,
2958                          NULL, NULL)) {
2959        return 1;
2960    }
2961
2962    /* Open the image */
2963    blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2964                   force_share);
2965    if (!blk) {
2966        return 1;
2967    }
2968    bs = blk_bs(blk);
2969
2970    /* Perform the requested action */
2971    switch(action) {
2972    case SNAPSHOT_LIST:
2973        dump_snapshots(bs);
2974        break;
2975
2976    case SNAPSHOT_CREATE:
2977        memset(&sn, 0, sizeof(sn));
2978        pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2979
2980        qemu_gettimeofday(&tv);
2981        sn.date_sec = tv.tv_sec;
2982        sn.date_nsec = tv.tv_usec * 1000;
2983
2984        ret = bdrv_snapshot_create(bs, &sn);
2985        if (ret) {
2986            error_report("Could not create snapshot '%s': %d (%s)",
2987                snapshot_name, ret, strerror(-ret));
2988        }
2989        break;
2990
2991    case SNAPSHOT_APPLY:
2992        ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
2993        if (ret) {
2994            error_reportf_err(err, "Could not apply snapshot '%s': ",
2995                              snapshot_name);
2996        }
2997        break;
2998
2999    case SNAPSHOT_DELETE:
3000        bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
3001        if (err) {
3002            error_reportf_err(err, "Could not delete snapshot '%s': ",
3003                              snapshot_name);
3004            ret = 1;
3005        }
3006        break;
3007    }
3008
3009    /* Cleanup */
3010    blk_unref(blk);
3011    if (ret) {
3012        return 1;
3013    }
3014    return 0;
3015}
3016
3017static int img_rebase(int argc, char **argv)
3018{
3019    BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
3020    uint8_t *buf_old = NULL;
3021    uint8_t *buf_new = NULL;
3022    BlockDriverState *bs = NULL;
3023    char *filename;
3024    const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3025    int c, flags, src_flags, ret;
3026    bool writethrough, src_writethrough;
3027    int unsafe = 0;
3028    bool force_share = false;
3029    int progress = 0;
3030    bool quiet = false;
3031    Error *local_err = NULL;
3032    bool image_opts = false;
3033
3034    /* Parse commandline parameters */
3035    fmt = NULL;
3036    cache = BDRV_DEFAULT_CACHE;
3037    src_cache = BDRV_DEFAULT_CACHE;
3038    out_baseimg = NULL;
3039    out_basefmt = NULL;
3040    for(;;) {
3041        static const struct option long_options[] = {
3042            {"help", no_argument, 0, 'h'},
3043            {"object", required_argument, 0, OPTION_OBJECT},
3044            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3045            {"force-share", no_argument, 0, 'U'},
3046            {0, 0, 0, 0}
3047        };
3048        c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
3049                        long_options, NULL);
3050        if (c == -1) {
3051            break;
3052        }
3053        switch(c) {
3054        case ':':
3055            missing_argument(argv[optind - 1]);
3056            break;
3057        case '?':
3058            unrecognized_option(argv[optind - 1]);
3059            break;
3060        case 'h':
3061            help();
3062            return 0;
3063        case 'f':
3064            fmt = optarg;
3065            break;
3066        case 'F':
3067            out_basefmt = optarg;
3068            break;
3069        case 'b':
3070            out_baseimg = optarg;
3071            break;
3072        case 'u':
3073            unsafe = 1;
3074            break;
3075        case 'p':
3076            progress = 1;
3077            break;
3078        case 't':
3079            cache = optarg;
3080            break;
3081        case 'T':
3082            src_cache = optarg;
3083            break;
3084        case 'q':
3085            quiet = true;
3086            break;
3087        case OPTION_OBJECT: {
3088            QemuOpts *opts;
3089            opts = qemu_opts_parse_noisily(&qemu_object_opts,
3090                                           optarg, true);
3091            if (!opts) {
3092                return 1;
3093            }
3094        }   break;
3095        case OPTION_IMAGE_OPTS:
3096            image_opts = true;
3097            break;
3098        case 'U':
3099            force_share = true;
3100            break;
3101        }
3102    }
3103
3104    if (quiet) {
3105        progress = 0;
3106    }
3107
3108    if (optind != argc - 1) {
3109        error_exit("Expecting one image file name");
3110    }
3111    if (!unsafe && !out_baseimg) {
3112        error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3113    }
3114    filename = argv[optind++];
3115
3116    if (qemu_opts_foreach(&qemu_object_opts,
3117                          user_creatable_add_opts_foreach,
3118                          NULL, NULL)) {
3119        return 1;
3120    }
3121
3122    qemu_progress_init(progress, 2.0);
3123    qemu_progress_print(0, 100);
3124
3125    flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
3126    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3127    if (ret < 0) {
3128        error_report("Invalid cache option: %s", cache);
3129        goto out;
3130    }
3131
3132    src_flags = 0;
3133    ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
3134    if (ret < 0) {
3135        error_report("Invalid source cache option: %s", src_cache);
3136        goto out;
3137    }
3138
3139    /* The source files are opened read-only, don't care about WCE */
3140    assert((src_flags & BDRV_O_RDWR) == 0);
3141    (void) src_writethrough;
3142
3143    /*
3144     * Open the images.
3145     *
3146     * Ignore the old backing file for unsafe rebase in case we want to correct
3147     * the reference to a renamed or moved backing file.
3148     */
3149    blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3150                   false);
3151    if (!blk) {
3152        ret = -1;
3153        goto out;
3154    }
3155    bs = blk_bs(blk);
3156
3157    if (out_basefmt != NULL) {
3158        if (bdrv_find_format(out_basefmt) == NULL) {
3159            error_report("Invalid format name: '%s'", out_basefmt);
3160            ret = -1;
3161            goto out;
3162        }
3163    }
3164
3165    /* For safe rebasing we need to compare old and new backing file */
3166    if (!unsafe) {
3167        char backing_name[PATH_MAX];
3168        QDict *options = NULL;
3169
3170        if (bs->backing_format[0] != '\0') {
3171            options = qdict_new();
3172            qdict_put_str(options, "driver", bs->backing_format);
3173        }
3174
3175        if (force_share) {
3176            if (!options) {
3177                options = qdict_new();
3178            }
3179            qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3180        }
3181        bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3182        blk_old_backing = blk_new_open(backing_name, NULL,
3183                                       options, src_flags, &local_err);
3184        if (!blk_old_backing) {
3185            error_reportf_err(local_err,
3186                              "Could not open old backing file '%s': ",
3187                              backing_name);
3188            ret = -1;
3189            goto out;
3190        }
3191
3192        if (out_baseimg[0]) {
3193            const char *overlay_filename;
3194            char *out_real_path;
3195
3196            options = qdict_new();
3197            if (out_basefmt) {
3198                qdict_put_str(options, "driver", out_basefmt);
3199            }
3200            if (force_share) {
3201                qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3202            }
3203
3204            overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3205                                                     : bs->filename;
3206            out_real_path = g_malloc(PATH_MAX);
3207
3208            bdrv_get_full_backing_filename_from_filename(overlay_filename,
3209                                                         out_baseimg,
3210                                                         out_real_path,
3211                                                         PATH_MAX,
3212                                                         &local_err);
3213            if (local_err) {
3214                error_reportf_err(local_err,
3215                                  "Could not resolve backing filename: ");
3216                ret = -1;
3217                g_free(out_real_path);
3218                goto out;
3219            }
3220
3221            blk_new_backing = blk_new_open(out_real_path, NULL,
3222                                           options, src_flags, &local_err);
3223            g_free(out_real_path);
3224            if (!blk_new_backing) {
3225                error_reportf_err(local_err,
3226                                  "Could not open new backing file '%s': ",
3227                                  out_baseimg);
3228                ret = -1;
3229                goto out;
3230            }
3231        }
3232    }
3233
3234    /*
3235     * Check each unallocated cluster in the COW file. If it is unallocated,
3236     * accesses go to the backing file. We must therefore compare this cluster
3237     * in the old and new backing file, and if they differ we need to copy it
3238     * from the old backing file into the COW file.
3239     *
3240     * If qemu-img crashes during this step, no harm is done. The content of
3241     * the image is the same as the original one at any time.
3242     */
3243    if (!unsafe) {
3244        int64_t size;
3245        int64_t old_backing_size;
3246        int64_t new_backing_size = 0;
3247        uint64_t offset;
3248        int64_t n;
3249        float local_progress = 0;
3250
3251        buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3252        buf_new = blk_blockalign(blk, IO_BUF_SIZE);
3253
3254        size = blk_getlength(blk);
3255        if (size < 0) {
3256            error_report("Could not get size of '%s': %s",
3257                         filename, strerror(-size));
3258            ret = -1;
3259            goto out;
3260        }
3261        old_backing_size = blk_getlength(blk_old_backing);
3262        if (old_backing_size < 0) {
3263            char backing_name[PATH_MAX];
3264
3265            bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3266            error_report("Could not get size of '%s': %s",
3267                         backing_name, strerror(-old_backing_size));
3268            ret = -1;
3269            goto out;
3270        }
3271        if (blk_new_backing) {
3272            new_backing_size = blk_getlength(blk_new_backing);
3273            if (new_backing_size < 0) {
3274                error_report("Could not get size of '%s': %s",
3275                             out_baseimg, strerror(-new_backing_size));
3276                ret = -1;
3277                goto out;
3278            }
3279        }
3280
3281        if (size != 0) {
3282            local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
3283        }
3284
3285        for (offset = 0; offset < size; offset += n) {
3286            /* How many bytes can we handle with the next read? */
3287            n = MIN(IO_BUF_SIZE, size - offset);
3288
3289            /* If the cluster is allocated, we don't need to take action */
3290            ret = bdrv_is_allocated(bs, offset, n, &n);
3291            if (ret < 0) {
3292                error_report("error while reading image metadata: %s",
3293                             strerror(-ret));
3294                goto out;
3295            }
3296            if (ret) {
3297                continue;
3298            }
3299
3300            /*
3301             * Read old and new backing file and take into consideration that
3302             * backing files may be smaller than the COW image.
3303             */
3304            if (offset >= old_backing_size) {
3305                memset(buf_old, 0, n);
3306            } else {
3307                if (offset + n > old_backing_size) {
3308                    n = old_backing_size - offset;
3309                }
3310
3311                ret = blk_pread(blk_old_backing, offset, buf_old, n);
3312                if (ret < 0) {
3313                    error_report("error while reading from old backing file");
3314                    goto out;
3315                }
3316            }
3317
3318            if (offset >= new_backing_size || !blk_new_backing) {
3319                memset(buf_new, 0, n);
3320            } else {
3321                if (offset + n > new_backing_size) {
3322                    n = new_backing_size - offset;
3323                }
3324
3325                ret = blk_pread(blk_new_backing, offset, buf_new, n);
3326                if (ret < 0) {
3327                    error_report("error while reading from new backing file");
3328                    goto out;
3329                }
3330            }
3331
3332            /* If they differ, we need to write to the COW file */
3333            uint64_t written = 0;
3334
3335            while (written < n) {
3336                int64_t pnum;
3337
3338                if (compare_buffers(buf_old + written, buf_new + written,
3339                                    n - written, &pnum))
3340                {
3341                    ret = blk_pwrite(blk, offset + written,
3342                                     buf_old + written, pnum, 0);
3343                    if (ret < 0) {
3344                        error_report("Error while writing to COW image: %s",
3345                            strerror(-ret));
3346                        goto out;
3347                    }
3348                }
3349
3350                written += pnum;
3351            }
3352            qemu_progress_print(local_progress, 100);
3353        }
3354    }
3355
3356    /*
3357     * Change the backing file. All clusters that are different from the old
3358     * backing file are overwritten in the COW file now, so the visible content
3359     * doesn't change when we switch the backing file.
3360     */
3361    if (out_baseimg && *out_baseimg) {
3362        ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3363    } else {
3364        ret = bdrv_change_backing_file(bs, NULL, NULL);
3365    }
3366
3367    if (ret == -ENOSPC) {
3368        error_report("Could not change the backing file to '%s': No "
3369                     "space left in the file header", out_baseimg);
3370    } else if (ret < 0) {
3371        error_report("Could not change the backing file to '%s': %s",
3372            out_baseimg, strerror(-ret));
3373    }
3374
3375    qemu_progress_print(100, 0);
3376    /*
3377     * TODO At this point it is possible to check if any clusters that are
3378     * allocated in the COW file are the same in the backing file. If so, they
3379     * could be dropped from the COW file. Don't do this before switching the
3380     * backing file, in case of a crash this would lead to corruption.
3381     */
3382out:
3383    qemu_progress_end();
3384    /* Cleanup */
3385    if (!unsafe) {
3386        blk_unref(blk_old_backing);
3387        blk_unref(blk_new_backing);
3388    }
3389    qemu_vfree(buf_old);
3390    qemu_vfree(buf_new);
3391
3392    blk_unref(blk);
3393    if (ret) {
3394        return 1;
3395    }
3396    return 0;
3397}
3398
3399static int img_resize(int argc, char **argv)
3400{
3401    Error *err = NULL;
3402    int c, ret, relative;
3403    const char *filename, *fmt, *size;
3404    int64_t n, total_size, current_size;
3405    bool quiet = false;
3406    BlockBackend *blk = NULL;
3407    PreallocMode prealloc = PREALLOC_MODE_OFF;
3408    QemuOpts *param;
3409
3410    static QemuOptsList resize_options = {
3411        .name = "resize_options",
3412        .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3413        .desc = {
3414            {
3415                .name = BLOCK_OPT_SIZE,
3416                .type = QEMU_OPT_SIZE,
3417                .help = "Virtual disk size"
3418            }, {
3419                /* end of list */
3420            }
3421        },
3422    };
3423    bool image_opts = false;
3424    bool shrink = false;
3425
3426    /* Remove size from argv manually so that negative numbers are not treated
3427     * as options by getopt. */
3428    if (argc < 3) {
3429        error_exit("Not enough arguments");
3430        return 1;
3431    }
3432
3433    size = argv[--argc];
3434
3435    /* Parse getopt arguments */
3436    fmt = NULL;
3437    for(;;) {
3438        static const struct option long_options[] = {
3439            {"help", no_argument, 0, 'h'},
3440            {"object", required_argument, 0, OPTION_OBJECT},
3441            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3442            {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
3443            {"shrink", no_argument, 0, OPTION_SHRINK},
3444            {0, 0, 0, 0}
3445        };
3446        c = getopt_long(argc, argv, ":f:hq",
3447                        long_options, NULL);
3448        if (c == -1) {
3449            break;
3450        }
3451        switch(c) {
3452        case ':':
3453            missing_argument(argv[optind - 1]);
3454            break;
3455        case '?':
3456            unrecognized_option(argv[optind - 1]);
3457            break;
3458        case 'h':
3459            help();
3460            break;
3461        case 'f':
3462            fmt = optarg;
3463            break;
3464        case 'q':
3465            quiet = true;
3466            break;
3467        case OPTION_OBJECT: {
3468            QemuOpts *opts;
3469            opts = qemu_opts_parse_noisily(&qemu_object_opts,
3470                                           optarg, true);
3471            if (!opts) {
3472                return 1;
3473            }
3474        }   break;
3475        case OPTION_IMAGE_OPTS:
3476            image_opts = true;
3477            break;
3478        case OPTION_PREALLOCATION:
3479            prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
3480                                       PREALLOC_MODE__MAX, NULL);
3481            if (prealloc == PREALLOC_MODE__MAX) {
3482                error_report("Invalid preallocation mode '%s'", optarg);
3483                return 1;
3484            }
3485            break;
3486        case OPTION_SHRINK:
3487            shrink = true;
3488            break;
3489        }
3490    }
3491    if (optind != argc - 1) {
3492        error_exit("Expecting one image file name");
3493    }
3494    filename = argv[optind++];
3495
3496    if (qemu_opts_foreach(&qemu_object_opts,
3497                          user_creatable_add_opts_foreach,
3498                          NULL, NULL)) {
3499        return 1;
3500    }
3501
3502    /* Choose grow, shrink, or absolute resize mode */
3503    switch (size[0]) {
3504    case '+':
3505        relative = 1;
3506        size++;
3507        break;
3508    case '-':
3509        relative = -1;
3510        size++;
3511        break;
3512    default:
3513        relative = 0;
3514        break;
3515    }
3516
3517    /* Parse size */
3518    param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
3519    qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
3520    if (err) {
3521        error_report_err(err);
3522        ret = -1;
3523        qemu_opts_del(param);
3524        goto out;
3525    }
3526    n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3527    qemu_opts_del(param);
3528
3529    blk = img_open(image_opts, filename, fmt,
3530                   BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3531                   false);
3532    if (!blk) {
3533        ret = -1;
3534        goto out;
3535    }
3536
3537    current_size = blk_getlength(blk);
3538    if (current_size < 0) {
3539        error_report("Failed to inquire current image length: %s",
3540                     strerror(-current_size));
3541        ret = -1;
3542        goto out;
3543    }
3544
3545    if (relative) {
3546        total_size = current_size + n * relative;
3547    } else {
3548        total_size = n;
3549    }
3550    if (total_size <= 0) {
3551        error_report("New image size must be positive");
3552        ret = -1;
3553        goto out;
3554    }
3555
3556    if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3557        error_report("Preallocation can only be used for growing images");
3558        ret = -1;
3559        goto out;
3560    }
3561
3562    if (total_size < current_size && !shrink) {
3563        warn_report("Shrinking an image will delete all data beyond the "
3564                    "shrunken image's end. Before performing such an "
3565                    "operation, make sure there is no important data there.");
3566
3567        if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3568            error_report(
3569              "Use the --shrink option to perform a shrink operation.");
3570            ret = -1;
3571            goto out;
3572        } else {
3573            warn_report("Using the --shrink option will suppress this message. "
3574                        "Note that future versions of qemu-img may refuse to "
3575                        "shrink images without this option.");
3576        }
3577    }
3578
3579    ret = blk_truncate(blk, total_size, prealloc, &err);
3580    if (!ret) {
3581        qprintf(quiet, "Image resized.\n");
3582    } else {
3583        error_report_err(err);
3584    }
3585out:
3586    blk_unref(blk);
3587    if (ret) {
3588        return 1;
3589    }
3590    return 0;
3591}
3592
3593static void amend_status_cb(BlockDriverState *bs,
3594                            int64_t offset, int64_t total_work_size,
3595                            void *opaque)
3596{
3597    qemu_progress_print(100.f * offset / total_work_size, 0);
3598}
3599
3600static int img_amend(int argc, char **argv)
3601{
3602    Error *err = NULL;
3603    int c, ret = 0;
3604    char *options = NULL;
3605    QemuOptsList *create_opts = NULL;
3606    QemuOpts *opts = NULL;
3607    const char *fmt = NULL, *filename, *cache;
3608    int flags;
3609    bool writethrough;
3610    bool quiet = false, progress = false;
3611    BlockBackend *blk = NULL;
3612    BlockDriverState *bs = NULL;
3613    bool image_opts = false;
3614
3615    cache = BDRV_DEFAULT_CACHE;
3616    for (;;) {
3617        static const struct option long_options[] = {
3618            {"help", no_argument, 0, 'h'},
3619            {"object", required_argument, 0, OPTION_OBJECT},
3620            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3621            {0, 0, 0, 0}
3622        };
3623        c = getopt_long(argc, argv, ":ho:f:t:pq",
3624                        long_options, NULL);
3625        if (c == -1) {
3626            break;
3627        }
3628
3629        switch (c) {
3630        case ':':
3631            missing_argument(argv[optind - 1]);
3632            break;
3633        case '?':
3634            unrecognized_option(argv[optind - 1]);
3635            break;
3636        case 'h':
3637            help();
3638            break;
3639        case 'o':
3640            if (!is_valid_option_list(optarg)) {
3641                error_report("Invalid option list: %s", optarg);
3642                ret = -1;
3643                goto out_no_progress;
3644            }
3645            if (!options) {
3646                options = g_strdup(optarg);
3647            } else {
3648                char *old_options = options;
3649                options = g_strdup_printf("%s,%s", options, optarg);
3650                g_free(old_options);
3651            }
3652            break;
3653        case 'f':
3654            fmt = optarg;
3655            break;
3656        case 't':
3657            cache = optarg;
3658            break;
3659        case 'p':
3660            progress = true;
3661            break;
3662        case 'q':
3663            quiet = true;
3664            break;
3665        case OPTION_OBJECT:
3666            opts = qemu_opts_parse_noisily(&qemu_object_opts,
3667                                           optarg, true);
3668            if (!opts) {
3669                ret = -1;
3670                goto out_no_progress;
3671            }
3672            break;
3673        case OPTION_IMAGE_OPTS:
3674            image_opts = true;
3675            break;
3676        }
3677    }
3678
3679    if (!options) {
3680        error_exit("Must specify options (-o)");
3681    }
3682
3683    if (qemu_opts_foreach(&qemu_object_opts,
3684                          user_creatable_add_opts_foreach,
3685                          NULL, NULL)) {
3686        ret = -1;
3687        goto out_no_progress;
3688    }
3689
3690    if (quiet) {
3691        progress = false;
3692    }
3693    qemu_progress_init(progress, 1.0);
3694
3695    filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3696    if (fmt && has_help_option(options)) {
3697        /* If a format is explicitly specified (and possibly no filename is
3698         * given), print option help here */
3699        ret = print_block_option_help(filename, fmt);
3700        goto out;
3701    }
3702
3703    if (optind != argc - 1) {
3704        error_report("Expecting one image file name");
3705        ret = -1;
3706        goto out;
3707    }
3708
3709    flags = BDRV_O_RDWR;
3710    ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3711    if (ret < 0) {
3712        error_report("Invalid cache option: %s", cache);
3713        goto out;
3714    }
3715
3716    blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3717                   false);
3718    if (!blk) {
3719        ret = -1;
3720        goto out;
3721    }
3722    bs = blk_bs(blk);
3723
3724    fmt = bs->drv->format_name;
3725
3726    if (has_help_option(options)) {
3727        /* If the format was auto-detected, print option help here */
3728        ret = print_block_option_help(filename, fmt);
3729        goto out;
3730    }
3731
3732    if (!bs->drv->create_opts) {
3733        error_report("Format driver '%s' does not support any options to amend",
3734                     fmt);
3735        ret = -1;
3736        goto out;
3737    }
3738
3739    create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
3740    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
3741    qemu_opts_do_parse(opts, options, NULL, &err);
3742    if (err) {
3743        error_report_err(err);
3744        ret = -1;
3745        goto out;
3746    }
3747
3748    /* In case the driver does not call amend_status_cb() */
3749    qemu_progress_print(0.f, 0);
3750    ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
3751    qemu_progress_print(100.f, 0);
3752    if (ret < 0) {
3753        error_report("Error while amending options: %s", strerror(-ret));
3754        goto out;
3755    }
3756
3757out:
3758    qemu_progress_end();
3759
3760out_no_progress:
3761    blk_unref(blk);
3762    qemu_opts_del(opts);
3763    qemu_opts_free(create_opts);
3764    g_free(options);
3765
3766    if (ret) {
3767        return 1;
3768    }
3769    return 0;
3770}
3771
3772typedef struct BenchData {
3773    BlockBackend *blk;
3774    uint64_t image_size;
3775    bool write;
3776    int bufsize;
3777    int step;
3778    int nrreq;
3779    int n;
3780    int flush_interval;
3781    bool drain_on_flush;
3782    uint8_t *buf;
3783    QEMUIOVector *qiov;
3784
3785    int in_flight;
3786    bool in_flush;
3787    uint64_t offset;
3788} BenchData;
3789
3790static void bench_undrained_flush_cb(void *opaque, int ret)
3791{
3792    if (ret < 0) {
3793        error_report("Failed flush request: %s", strerror(-ret));
3794        exit(EXIT_FAILURE);
3795    }
3796}
3797
3798static void bench_cb(void *opaque, int ret)
3799{
3800    BenchData *b = opaque;
3801    BlockAIOCB *acb;
3802
3803    if (ret < 0) {
3804        error_report("Failed request: %s", strerror(-ret));
3805        exit(EXIT_FAILURE);
3806    }
3807
3808    if (b->in_flush) {
3809        /* Just finished a flush with drained queue: Start next requests */
3810        assert(b->in_flight == 0);
3811        b->in_flush = false;
3812    } else if (b->in_flight > 0) {
3813        int remaining = b->n - b->in_flight;
3814
3815        b->n--;
3816        b->in_flight--;
3817
3818        /* Time for flush? Drain queue if requested, then flush */
3819        if (b->flush_interval && remaining % b->flush_interval == 0) {
3820            if (!b->in_flight || !b->drain_on_flush) {
3821                BlockCompletionFunc *cb;
3822
3823                if (b->drain_on_flush) {
3824                    b->in_flush = true;
3825                    cb = bench_cb;
3826                } else {
3827                    cb = bench_undrained_flush_cb;
3828                }
3829
3830                acb = blk_aio_flush(b->blk, cb, b);
3831                if (!acb) {
3832                    error_report("Failed to issue flush request");
3833                    exit(EXIT_FAILURE);
3834                }
3835            }
3836            if (b->drain_on_flush) {
3837                return;
3838            }
3839        }
3840    }
3841
3842    while (b->n > b->in_flight && b->in_flight < b->nrreq) {
3843        int64_t offset = b->offset;
3844        /* blk_aio_* might look for completed I/Os and kick bench_cb
3845         * again, so make sure this operation is counted by in_flight
3846         * and b->offset is ready for the next submission.
3847         */
3848        b->in_flight++;
3849        b->offset += b->step;
3850        b->offset %= b->image_size;
3851        if (b->write) {
3852            acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
3853        } else {
3854            acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
3855        }
3856        if (!acb) {
3857            error_report("Failed to issue request");
3858            exit(EXIT_FAILURE);
3859        }
3860    }
3861}
3862
3863static int img_bench(int argc, char **argv)
3864{
3865    int c, ret = 0;
3866    const char *fmt = NULL, *filename;
3867    bool quiet = false;
3868    bool image_opts = false;
3869    bool is_write = false;
3870    int count = 75000;
3871    int depth = 64;
3872    int64_t offset = 0;
3873    size_t bufsize = 4096;
3874    int pattern = 0;
3875    size_t step = 0;
3876    int flush_interval = 0;
3877    bool drain_on_flush = true;
3878    int64_t image_size;
3879    BlockBackend *blk = NULL;
3880    BenchData data = {};
3881    int flags = 0;
3882    bool writethrough = false;
3883    struct timeval t1, t2;
3884    int i;
3885    bool force_share = false;
3886
3887    for (;;) {
3888        static const struct option long_options[] = {
3889            {"help", no_argument, 0, 'h'},
3890            {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
3891            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3892            {"pattern", required_argument, 0, OPTION_PATTERN},
3893            {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
3894            {"force-share", no_argument, 0, 'U'},
3895            {0, 0, 0, 0}
3896        };
3897        c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
3898        if (c == -1) {
3899            break;
3900        }
3901
3902        switch (c) {
3903        case ':':
3904            missing_argument(argv[optind - 1]);
3905            break;
3906        case '?':
3907            unrecognized_option(argv[optind - 1]);
3908            break;
3909        case 'h':
3910            help();
3911            break;
3912        case 'c':
3913        {
3914            unsigned long res;
3915
3916            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3917                error_report("Invalid request count specified");
3918                return 1;
3919            }
3920            count = res;
3921            break;
3922        }
3923        case 'd':
3924        {
3925            unsigned long res;
3926
3927            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3928                error_report("Invalid queue depth specified");
3929                return 1;
3930            }
3931            depth = res;
3932            break;
3933        }
3934        case 'f':
3935            fmt = optarg;
3936            break;
3937        case 'n':
3938            flags |= BDRV_O_NATIVE_AIO;
3939            break;
3940        case 'o':
3941        {
3942            offset = cvtnum(optarg);
3943            if (offset < 0) {
3944                error_report("Invalid offset specified");
3945                return 1;
3946            }
3947            break;
3948        }
3949            break;
3950        case 'q':
3951            quiet = true;
3952            break;
3953        case 's':
3954        {
3955            int64_t sval;
3956
3957            sval = cvtnum(optarg);
3958            if (sval < 0 || sval > INT_MAX) {
3959                error_report("Invalid buffer size specified");
3960                return 1;
3961            }
3962
3963            bufsize = sval;
3964            break;
3965        }
3966        case 'S':
3967        {
3968            int64_t sval;
3969
3970            sval = cvtnum(optarg);
3971            if (sval < 0 || sval > INT_MAX) {
3972                error_report("Invalid step size specified");
3973                return 1;
3974            }
3975
3976            step = sval;
3977            break;
3978        }
3979        case 't':
3980            ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3981            if (ret < 0) {
3982                error_report("Invalid cache mode");
3983                ret = -1;
3984                goto out;
3985            }
3986            break;
3987        case 'w':
3988            flags |= BDRV_O_RDWR;
3989            is_write = true;
3990            break;
3991        case 'U':
3992            force_share = true;
3993            break;
3994        case OPTION_PATTERN:
3995        {
3996            unsigned long res;
3997
3998            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
3999                error_report("Invalid pattern byte specified");
4000                return 1;
4001            }
4002            pattern = res;
4003            break;
4004        }
4005        case OPTION_FLUSH_INTERVAL:
4006        {
4007            unsigned long res;
4008
4009            if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4010                error_report("Invalid flush interval specified");
4011                return 1;
4012            }
4013            flush_interval = res;
4014            break;
4015        }
4016        case OPTION_NO_DRAIN:
4017            drain_on_flush = false;
4018            break;
4019        case OPTION_IMAGE_OPTS:
4020            image_opts = true;
4021            break;
4022        }
4023    }
4024
4025    if (optind != argc - 1) {
4026        error_exit("Expecting one image file name");
4027    }
4028    filename = argv[argc - 1];
4029
4030    if (!is_write && flush_interval) {
4031        error_report("--flush-interval is only available in write tests");
4032        ret = -1;
4033        goto out;
4034    }
4035    if (flush_interval && flush_interval < depth) {
4036        error_report("Flush interval can't be smaller than depth");
4037        ret = -1;
4038        goto out;
4039    }
4040
4041    blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4042                   force_share);
4043    if (!blk) {
4044        ret = -1;
4045        goto out;
4046    }
4047
4048    image_size = blk_getlength(blk);
4049    if (image_size < 0) {
4050        ret = image_size;
4051        goto out;
4052    }
4053
4054    data = (BenchData) {
4055        .blk            = blk,
4056        .image_size     = image_size,
4057        .bufsize        = bufsize,
4058        .step           = step ?: bufsize,
4059        .nrreq          = depth,
4060        .n              = count,
4061        .offset         = offset,
4062        .write          = is_write,
4063        .flush_interval = flush_interval,
4064        .drain_on_flush = drain_on_flush,
4065    };
4066    printf("Sending %d %s requests, %d bytes each, %d in parallel "
4067           "(starting at offset %" PRId64 ", step size %d)\n",
4068           data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
4069           data.offset, data.step);
4070    if (flush_interval) {
4071        printf("Sending flush every %d requests\n", flush_interval);
4072    }
4073
4074    data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
4075    memset(data.buf, pattern, data.nrreq * data.bufsize);
4076
4077    data.qiov = g_new(QEMUIOVector, data.nrreq);
4078    for (i = 0; i < data.nrreq; i++) {
4079        qemu_iovec_init(&data.qiov[i], 1);
4080        qemu_iovec_add(&data.qiov[i],
4081                       data.buf + i * data.bufsize, data.bufsize);
4082    }
4083
4084    gettimeofday(&t1, NULL);
4085    bench_cb(&data, 0);
4086
4087    while (data.n > 0) {
4088        main_loop_wait(false);
4089    }
4090    gettimeofday(&t2, NULL);
4091
4092    printf("Run completed in %3.3f seconds.\n",
4093           (t2.tv_sec - t1.tv_sec)
4094           + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4095
4096out:
4097    qemu_vfree(data.buf);
4098    blk_unref(blk);
4099
4100    if (ret) {
4101        return 1;
4102    }
4103    return 0;
4104}
4105
4106#define C_BS      01
4107#define C_COUNT   02
4108#define C_IF      04
4109#define C_OF      010
4110#define C_SKIP    020
4111
4112struct DdInfo {
4113    unsigned int flags;
4114    int64_t count;
4115};
4116
4117struct DdIo {
4118    int bsz;    /* Block size */
4119    char *filename;
4120    uint8_t *buf;
4121    int64_t offset;
4122};
4123
4124struct DdOpts {
4125    const char *name;
4126    int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4127    unsigned int flag;
4128};
4129
4130static int img_dd_bs(const char *arg,
4131                     struct DdIo *in, struct DdIo *out,
4132                     struct DdInfo *dd)
4133{
4134    int64_t res;
4135
4136    res = cvtnum(arg);
4137
4138    if (res <= 0 || res > INT_MAX) {
4139        error_report("invalid number: '%s'", arg);
4140        return 1;
4141    }
4142    in->bsz = out->bsz = res;
4143
4144    return 0;
4145}
4146
4147static int img_dd_count(const char *arg,
4148                        struct DdIo *in, struct DdIo *out,
4149                        struct DdInfo *dd)
4150{
4151    dd->count = cvtnum(arg);
4152
4153    if (dd->count < 0) {
4154        error_report("invalid number: '%s'", arg);
4155        return 1;
4156    }
4157
4158    return 0;
4159}
4160
4161static int img_dd_if(const char *arg,
4162                     struct DdIo *in, struct DdIo *out,
4163                     struct DdInfo *dd)
4164{
4165    in->filename = g_strdup(arg);
4166
4167    return 0;
4168}
4169
4170static int img_dd_of(const char *arg,
4171                     struct DdIo *in, struct DdIo *out,
4172                     struct DdInfo *dd)
4173{
4174    out->filename = g_strdup(arg);
4175
4176    return 0;
4177}
4178
4179static int img_dd_skip(const char *arg,
4180                       struct DdIo *in, struct DdIo *out,
4181                       struct DdInfo *dd)
4182{
4183    in->offset = cvtnum(arg);
4184
4185    if (in->offset < 0) {
4186        error_report("invalid number: '%s'", arg);
4187        return 1;
4188    }
4189
4190    return 0;
4191}
4192
4193static int img_dd(int argc, char **argv)
4194{
4195    int ret = 0;
4196    char *arg = NULL;
4197    char *tmp;
4198    BlockDriver *drv = NULL, *proto_drv = NULL;
4199    BlockBackend *blk1 = NULL, *blk2 = NULL;
4200    QemuOpts *opts = NULL;
4201    QemuOptsList *create_opts = NULL;
4202    Error *local_err = NULL;
4203    bool image_opts = false;
4204    int c, i;
4205    const char *out_fmt = "raw";
4206    const char *fmt = NULL;
4207    int64_t size = 0;
4208    int64_t block_count = 0, out_pos, in_pos;
4209    bool force_share = false;
4210    struct DdInfo dd = {
4211        .flags = 0,
4212        .count = 0,
4213    };
4214    struct DdIo in = {
4215        .bsz = 512, /* Block size is by default 512 bytes */
4216        .filename = NULL,
4217        .buf = NULL,
4218        .offset = 0
4219    };
4220    struct DdIo out = {
4221        .bsz = 512,
4222        .filename = NULL,
4223        .buf = NULL,
4224        .offset = 0
4225    };
4226
4227    const struct DdOpts options[] = {
4228        { "bs", img_dd_bs, C_BS },
4229        { "count", img_dd_count, C_COUNT },
4230        { "if", img_dd_if, C_IF },
4231        { "of", img_dd_of, C_OF },
4232        { "skip", img_dd_skip, C_SKIP },
4233        { NULL, NULL, 0 }
4234    };
4235    const struct option long_options[] = {
4236        { "help", no_argument, 0, 'h'},
4237        { "object", required_argument, 0, OPTION_OBJECT},
4238        { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4239        { "force-share", no_argument, 0, 'U'},
4240        { 0, 0, 0, 0 }
4241    };
4242
4243    while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
4244        if (c == EOF) {
4245            break;
4246        }
4247        switch (c) {
4248        case 'O':
4249            out_fmt = optarg;
4250            break;
4251        case 'f':
4252            fmt = optarg;
4253            break;
4254        case ':':
4255            missing_argument(argv[optind - 1]);
4256            break;
4257        case '?':
4258            unrecognized_option(argv[optind - 1]);
4259            break;
4260        case 'h':
4261            help();
4262            break;
4263        case 'U':
4264            force_share = true;
4265            break;
4266        case OPTION_OBJECT:
4267            if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
4268                ret = -1;
4269                goto out;
4270            }
4271            break;
4272        case OPTION_IMAGE_OPTS:
4273            image_opts = true;
4274            break;
4275        }
4276    }
4277
4278    for (i = optind; i < argc; i++) {
4279        int j;
4280        arg = g_strdup(argv[i]);
4281
4282        tmp = strchr(arg, '=');
4283        if (tmp == NULL) {
4284            error_report("unrecognized operand %s", arg);
4285            ret = -1;
4286            goto out;
4287        }
4288
4289        *tmp++ = '\0';
4290
4291        for (j = 0; options[j].name != NULL; j++) {
4292            if (!strcmp(arg, options[j].name)) {
4293                break;
4294            }
4295        }
4296        if (options[j].name == NULL) {
4297            error_report("unrecognized operand %s", arg);
4298            ret = -1;
4299            goto out;
4300        }
4301
4302        if (options[j].f(tmp, &in, &out, &dd) != 0) {
4303            ret = -1;
4304            goto out;
4305        }
4306        dd.flags |= options[j].flag;
4307        g_free(arg);
4308        arg = NULL;
4309    }
4310
4311    if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4312        error_report("Must specify both input and output files");
4313        ret = -1;
4314        goto out;
4315    }
4316
4317    if (qemu_opts_foreach(&qemu_object_opts,
4318                          user_creatable_add_opts_foreach,
4319                          NULL, NULL)) {
4320        ret = -1;
4321        goto out;
4322    }
4323
4324    blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4325                    force_share);
4326
4327    if (!blk1) {
4328        ret = -1;
4329        goto out;
4330    }
4331
4332    drv = bdrv_find_format(out_fmt);
4333    if (!drv) {
4334        error_report("Unknown file format");
4335        ret = -1;
4336        goto out;
4337    }
4338    proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4339
4340    if (!proto_drv) {
4341        error_report_err(local_err);
4342        ret = -1;
4343        goto out;
4344    }
4345    if (!drv->create_opts) {
4346        error_report("Format driver '%s' does not support image creation",
4347                     drv->format_name);
4348        ret = -1;
4349        goto out;
4350    }
4351    if (!proto_drv->create_opts) {
4352        error_report("Protocol driver '%s' does not support image creation",
4353                     proto_drv->format_name);
4354        ret = -1;
4355        goto out;
4356    }
4357    create_opts = qemu_opts_append(create_opts, drv->create_opts);
4358    create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4359
4360    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4361
4362    size = blk_getlength(blk1);
4363    if (size < 0) {
4364        error_report("Failed to get size for '%s'", in.filename);
4365        ret = -1;
4366        goto out;
4367    }
4368
4369    if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4370        dd.count * in.bsz < size) {
4371        size = dd.count * in.bsz;
4372    }
4373
4374    /* Overflow means the specified offset is beyond input image's size */
4375    if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4376                              size < in.bsz * in.offset)) {
4377        qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4378    } else {
4379        qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4380                            size - in.bsz * in.offset, &error_abort);
4381    }
4382
4383    ret = bdrv_create(drv, out.filename, opts, &local_err);
4384    if (ret < 0) {
4385        error_reportf_err(local_err,
4386                          "%s: error while creating output image: ",
4387                          out.filename);
4388        ret = -1;
4389        goto out;
4390    }
4391
4392    /* TODO, we can't honour --image-opts for the target,
4393     * since it needs to be given in a format compatible
4394     * with the bdrv_create() call above which does not
4395     * support image-opts style.
4396     */
4397    blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
4398                         false, false, false);
4399
4400    if (!blk2) {
4401        ret = -1;
4402        goto out;
4403    }
4404
4405    if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4406                              size < in.offset * in.bsz)) {
4407        /* We give a warning if the skip option is bigger than the input
4408         * size and create an empty output disk image (i.e. like dd(1)).
4409         */
4410        error_report("%s: cannot skip to specified offset", in.filename);
4411        in_pos = size;
4412    } else {
4413        in_pos = in.offset * in.bsz;
4414    }
4415
4416    in.buf = g_new(uint8_t, in.bsz);
4417
4418    for (out_pos = 0; in_pos < size; block_count++) {
4419        int in_ret, out_ret;
4420
4421        if (in_pos + in.bsz > size) {
4422            in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4423        } else {
4424            in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4425        }
4426        if (in_ret < 0) {
4427            error_report("error while reading from input image file: %s",
4428                         strerror(-in_ret));
4429            ret = -1;
4430            goto out;
4431        }
4432        in_pos += in_ret;
4433
4434        out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4435
4436        if (out_ret < 0) {
4437            error_report("error while writing to output image file: %s",
4438                         strerror(-out_ret));
4439            ret = -1;
4440            goto out;
4441        }
4442        out_pos += out_ret;
4443    }
4444
4445out:
4446    g_free(arg);
4447    qemu_opts_del(opts);
4448    qemu_opts_free(create_opts);
4449    blk_unref(blk1);
4450    blk_unref(blk2);
4451    g_free(in.filename);
4452    g_free(out.filename);
4453    g_free(in.buf);
4454    g_free(out.buf);
4455
4456    if (ret) {
4457        return 1;
4458    }
4459    return 0;
4460}
4461
4462static void dump_json_block_measure_info(BlockMeasureInfo *info)
4463{
4464    QString *str;
4465    QObject *obj;
4466    Visitor *v = qobject_output_visitor_new(&obj);
4467
4468    visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4469    visit_complete(v, &obj);
4470    str = qobject_to_json_pretty(obj);
4471    assert(str != NULL);
4472    printf("%s\n", qstring_get_str(str));
4473    qobject_decref(obj);
4474    visit_free(v);
4475    QDECREF(str);
4476}
4477
4478static int img_measure(int argc, char **argv)
4479{
4480    static const struct option long_options[] = {
4481        {"help", no_argument, 0, 'h'},
4482        {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4483        {"object", required_argument, 0, OPTION_OBJECT},
4484        {"output", required_argument, 0, OPTION_OUTPUT},
4485        {"size", required_argument, 0, OPTION_SIZE},
4486        {"force-share", no_argument, 0, 'U'},
4487        {0, 0, 0, 0}
4488    };
4489    OutputFormat output_format = OFORMAT_HUMAN;
4490    BlockBackend *in_blk = NULL;
4491    BlockDriver *drv;
4492    const char *filename = NULL;
4493    const char *fmt = NULL;
4494    const char *out_fmt = "raw";
4495    char *options = NULL;
4496    char *snapshot_name = NULL;
4497    bool force_share = false;
4498    QemuOpts *opts = NULL;
4499    QemuOpts *object_opts = NULL;
4500    QemuOpts *sn_opts = NULL;
4501    QemuOptsList *create_opts = NULL;
4502    bool image_opts = false;
4503    uint64_t img_size = UINT64_MAX;
4504    BlockMeasureInfo *info = NULL;
4505    Error *local_err = NULL;
4506    int ret = 1;
4507    int c;
4508
4509    while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4510                            long_options, NULL)) != -1) {
4511        switch (c) {
4512        case '?':
4513        case 'h':
4514            help();
4515            break;
4516        case 'f':
4517            fmt = optarg;
4518            break;
4519        case 'O':
4520            out_fmt = optarg;
4521            break;
4522        case 'o':
4523            if (!is_valid_option_list(optarg)) {
4524                error_report("Invalid option list: %s", optarg);
4525                goto out;
4526            }
4527            if (!options) {
4528                options = g_strdup(optarg);
4529            } else {
4530                char *old_options = options;
4531                options = g_strdup_printf("%s,%s", options, optarg);
4532                g_free(old_options);
4533            }
4534            break;
4535        case 'l':
4536            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4537                sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4538                                                  optarg, false);
4539                if (!sn_opts) {
4540                    error_report("Failed in parsing snapshot param '%s'",
4541                                 optarg);
4542                    goto out;
4543                }
4544            } else {
4545                snapshot_name = optarg;
4546            }
4547            break;
4548        case 'U':
4549            force_share = true;
4550            break;
4551        case OPTION_OBJECT:
4552            object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4553                                                  optarg, true);
4554            if (!object_opts) {
4555                goto out;
4556            }
4557            break;
4558        case OPTION_IMAGE_OPTS:
4559            image_opts = true;
4560            break;
4561        case OPTION_OUTPUT:
4562            if (!strcmp(optarg, "json")) {
4563                output_format = OFORMAT_JSON;
4564            } else if (!strcmp(optarg, "human")) {
4565                output_format = OFORMAT_HUMAN;
4566            } else {
4567                error_report("--output must be used with human or json "
4568                             "as argument.");
4569                goto out;
4570            }
4571            break;
4572        case OPTION_SIZE:
4573        {
4574            int64_t sval;
4575
4576            sval = cvtnum(optarg);
4577            if (sval < 0) {
4578                if (sval == -ERANGE) {
4579                    error_report("Image size must be less than 8 EiB!");
4580                } else {
4581                    error_report("Invalid image size specified! You may use "
4582                                 "k, M, G, T, P or E suffixes for ");
4583                    error_report("kilobytes, megabytes, gigabytes, terabytes, "
4584                                 "petabytes and exabytes.");
4585                }
4586                goto out;
4587            }
4588            img_size = (uint64_t)sval;
4589        }
4590        break;
4591        }
4592    }
4593
4594    if (qemu_opts_foreach(&qemu_object_opts,
4595                          user_creatable_add_opts_foreach,
4596                          NULL, NULL)) {
4597        goto out;
4598    }
4599
4600    if (argc - optind > 1) {
4601        error_report("At most one filename argument is allowed.");
4602        goto out;
4603    } else if (argc - optind == 1) {
4604        filename = argv[optind];
4605    }
4606
4607    if (!filename &&
4608        (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4609        error_report("--object, --image-opts, -f, and -l "
4610                     "require a filename argument.");
4611        goto out;
4612    }
4613    if (filename && img_size != UINT64_MAX) {
4614        error_report("--size N cannot be used together with a filename.");
4615        goto out;
4616    }
4617    if (!filename && img_size == UINT64_MAX) {
4618        error_report("Either --size N or one filename must be specified.");
4619        goto out;
4620    }
4621
4622    if (filename) {
4623        in_blk = img_open(image_opts, filename, fmt, 0,
4624                          false, false, force_share);
4625        if (!in_blk) {
4626            goto out;
4627        }
4628
4629        if (sn_opts) {
4630            bdrv_snapshot_load_tmp(blk_bs(in_blk),
4631                    qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4632                    qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4633                    &local_err);
4634        } else if (snapshot_name != NULL) {
4635            bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4636                    snapshot_name, &local_err);
4637        }
4638        if (local_err) {
4639            error_reportf_err(local_err, "Failed to load snapshot: ");
4640            goto out;
4641        }
4642    }
4643
4644    drv = bdrv_find_format(out_fmt);
4645    if (!drv) {
4646        error_report("Unknown file format '%s'", out_fmt);
4647        goto out;
4648    }
4649    if (!drv->create_opts) {
4650        error_report("Format driver '%s' does not support image creation",
4651                     drv->format_name);
4652        goto out;
4653    }
4654
4655    create_opts = qemu_opts_append(create_opts, drv->create_opts);
4656    create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4657    opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4658    if (options) {
4659        qemu_opts_do_parse(opts, options, NULL, &local_err);
4660        if (local_err) {
4661            error_report_err(local_err);
4662            error_report("Invalid options for file format '%s'", out_fmt);
4663            goto out;
4664        }
4665    }
4666    if (img_size != UINT64_MAX) {
4667        qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4668    }
4669
4670    info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4671    if (local_err) {
4672        error_report_err(local_err);
4673        goto out;
4674    }
4675
4676    if (output_format == OFORMAT_HUMAN) {
4677        printf("required size: %" PRIu64 "\n", info->required);
4678        printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4679    } else {
4680        dump_json_block_measure_info(info);
4681    }
4682
4683    ret = 0;
4684
4685out:
4686    qapi_free_BlockMeasureInfo(info);
4687    qemu_opts_del(object_opts);
4688    qemu_opts_del(opts);
4689    qemu_opts_del(sn_opts);
4690    qemu_opts_free(create_opts);
4691    g_free(options);
4692    blk_unref(in_blk);
4693    return ret;
4694}
4695
4696static const img_cmd_t img_cmds[] = {
4697#define DEF(option, callback, arg_string)        \
4698    { option, callback },
4699#include "qemu-img-cmds.h"
4700#undef DEF
4701#undef GEN_DOCS
4702    { NULL, NULL, },
4703};
4704
4705int main(int argc, char **argv)
4706{
4707    const img_cmd_t *cmd;
4708    const char *cmdname;
4709    Error *local_error = NULL;
4710    char *trace_file = NULL;
4711    int c;
4712    static const struct option long_options[] = {
4713        {"help", no_argument, 0, 'h'},
4714        {"version", no_argument, 0, 'V'},
4715        {"trace", required_argument, NULL, 'T'},
4716        {0, 0, 0, 0}
4717    };
4718
4719#ifdef CONFIG_POSIX
4720    signal(SIGPIPE, SIG_IGN);
4721#endif
4722
4723    module_call_init(MODULE_INIT_TRACE);
4724    error_set_progname(argv[0]);
4725    qemu_init_exec_dir(argv[0]);
4726
4727    if (qemu_init_main_loop(&local_error)) {
4728        error_report_err(local_error);
4729        exit(EXIT_FAILURE);
4730    }
4731
4732    qcrypto_init(&error_fatal);
4733
4734    module_call_init(MODULE_INIT_QOM);
4735    bdrv_init();
4736    if (argc < 2) {
4737        error_exit("Not enough arguments");
4738    }
4739
4740    qemu_add_opts(&qemu_object_opts);
4741    qemu_add_opts(&qemu_source_opts);
4742    qemu_add_opts(&qemu_trace_opts);
4743
4744    while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
4745        switch (c) {
4746        case ':':
4747            missing_argument(argv[optind - 1]);
4748            return 0;
4749        case '?':
4750            unrecognized_option(argv[optind - 1]);
4751            return 0;
4752        case 'h':
4753            help();
4754            return 0;
4755        case 'V':
4756            printf(QEMU_IMG_VERSION);
4757            return 0;
4758        case 'T':
4759            g_free(trace_file);
4760            trace_file = trace_opt_parse(optarg);
4761            break;
4762        }
4763    }
4764
4765    cmdname = argv[optind];
4766
4767    /* reset getopt_long scanning */
4768    argc -= optind;
4769    if (argc < 1) {
4770        return 0;
4771    }
4772    argv += optind;
4773    optind = 0;
4774
4775    if (!trace_init_backends()) {
4776        exit(1);
4777    }
4778    trace_init_file(trace_file);
4779    qemu_set_log(LOG_TRACE);
4780
4781    /* find the command */
4782    for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4783        if (!strcmp(cmdname, cmd->name)) {
4784            return cmd->handler(argc, argv);
4785        }
4786    }
4787
4788    /* not found */
4789    error_exit("Command not found: %s", cmdname);
4790}
4791