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