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