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