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