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