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