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