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