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