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