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