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