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