xref: /openbmc/qemu/qemu-img.c (revision ab938ae4)
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/qerror.h"
32 #include "qapi/qmp/qjson.h"
33 #include "qapi/qmp/qbool.h"
34 #include "qemu/cutils.h"
35 #include "qemu/config-file.h"
36 #include "qemu/option.h"
37 #include "qemu/error-report.h"
38 #include "qemu/log.h"
39 #include "qom/object_interfaces.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/block-backend.h"
42 #include "block/block_int.h"
43 #include "block/blockjob.h"
44 #include "block/qapi.h"
45 #include "crypto/init.h"
46 #include "trace/control.h"
47 
48 #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \
49                           "\n" QEMU_COPYRIGHT "\n"
50 
51 typedef struct img_cmd_t {
52     const char *name;
53     int (*handler)(int argc, char **argv);
54 } img_cmd_t;
55 
56 enum {
57     OPTION_OUTPUT = 256,
58     OPTION_BACKING_CHAIN = 257,
59     OPTION_OBJECT = 258,
60     OPTION_IMAGE_OPTS = 259,
61     OPTION_PATTERN = 260,
62     OPTION_FLUSH_INTERVAL = 261,
63     OPTION_NO_DRAIN = 262,
64     OPTION_TARGET_IMAGE_OPTS = 263,
65     OPTION_SIZE = 264,
66     OPTION_PREALLOCATION = 265,
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 true iff the first sector pointed to by 'buf' contains at least
1068  * a non-NUL byte.
1069  *
1070  * 'pnum' is set to the number of sectors (including and immediately following
1071  * the first one) that are known to be in the same allocated/unallocated state.
1072  */
1073 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
1074 {
1075     bool is_zero;
1076     int i;
1077 
1078     if (n <= 0) {
1079         *pnum = 0;
1080         return 0;
1081     }
1082     is_zero = buffer_is_zero(buf, 512);
1083     for(i = 1; i < n; i++) {
1084         buf += 512;
1085         if (is_zero != buffer_is_zero(buf, 512)) {
1086             break;
1087         }
1088     }
1089     *pnum = i;
1090     return !is_zero;
1091 }
1092 
1093 /*
1094  * Like is_allocated_sectors, but if the buffer starts with a used sector,
1095  * up to 'min' consecutive sectors containing zeros are ignored. This avoids
1096  * breaking up write requests for only small sparse areas.
1097  */
1098 static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
1099     int min)
1100 {
1101     int ret;
1102     int num_checked, num_used;
1103 
1104     if (n < min) {
1105         min = n;
1106     }
1107 
1108     ret = is_allocated_sectors(buf, n, pnum);
1109     if (!ret) {
1110         return ret;
1111     }
1112 
1113     num_used = *pnum;
1114     buf += BDRV_SECTOR_SIZE * *pnum;
1115     n -= *pnum;
1116     num_checked = num_used;
1117 
1118     while (n > 0) {
1119         ret = is_allocated_sectors(buf, n, pnum);
1120 
1121         buf += BDRV_SECTOR_SIZE * *pnum;
1122         n -= *pnum;
1123         num_checked += *pnum;
1124         if (ret) {
1125             num_used = num_checked;
1126         } else if (*pnum >= min) {
1127             break;
1128         }
1129     }
1130 
1131     *pnum = num_used;
1132     return 1;
1133 }
1134 
1135 /*
1136  * Compares two buffers sector by sector. Returns 0 if the first sector of both
1137  * buffers matches, non-zero otherwise.
1138  *
1139  * pnum is set to the number of sectors (including and immediately following
1140  * the first one) that are known to have the same comparison result
1141  */
1142 static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
1143     int *pnum)
1144 {
1145     bool res;
1146     int i;
1147 
1148     if (n <= 0) {
1149         *pnum = 0;
1150         return 0;
1151     }
1152 
1153     res = !!memcmp(buf1, buf2, 512);
1154     for(i = 1; i < n; i++) {
1155         buf1 += 512;
1156         buf2 += 512;
1157 
1158         if (!!memcmp(buf1, buf2, 512) != res) {
1159             break;
1160         }
1161     }
1162 
1163     *pnum = i;
1164     return res;
1165 }
1166 
1167 #define IO_BUF_SIZE (2 * 1024 * 1024)
1168 
1169 static int64_t sectors_to_bytes(int64_t sectors)
1170 {
1171     return sectors << BDRV_SECTOR_BITS;
1172 }
1173 
1174 static int64_t sectors_to_process(int64_t total, int64_t from)
1175 {
1176     return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
1177 }
1178 
1179 /*
1180  * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1181  *
1182  * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
1183  * data and negative value on error.
1184  *
1185  * @param blk:  BlockBackend for the image
1186  * @param sect_num: Number of first sector to check
1187  * @param sect_count: Number of sectors to check
1188  * @param filename: Name of disk file we are checking (logging purpose)
1189  * @param buffer: Allocated buffer for storing read data
1190  * @param quiet: Flag for quiet mode
1191  */
1192 static int check_empty_sectors(BlockBackend *blk, int64_t sect_num,
1193                                int sect_count, const char *filename,
1194                                uint8_t *buffer, bool quiet)
1195 {
1196     int pnum, ret = 0;
1197     ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer,
1198                     sect_count << BDRV_SECTOR_BITS);
1199     if (ret < 0) {
1200         error_report("Error while reading offset %" PRId64 " of %s: %s",
1201                      sectors_to_bytes(sect_num), filename, strerror(-ret));
1202         return ret;
1203     }
1204     ret = is_allocated_sectors(buffer, sect_count, &pnum);
1205     if (ret || pnum != sect_count) {
1206         qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1207                 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
1208         return 1;
1209     }
1210 
1211     return 0;
1212 }
1213 
1214 /*
1215  * Compares two images. Exit codes:
1216  *
1217  * 0 - Images are identical
1218  * 1 - Images differ
1219  * >1 - Error occurred
1220  */
1221 static int img_compare(int argc, char **argv)
1222 {
1223     const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
1224     BlockBackend *blk1, *blk2;
1225     BlockDriverState *bs1, *bs2;
1226     int64_t total_sectors1, total_sectors2;
1227     uint8_t *buf1 = NULL, *buf2 = NULL;
1228     int pnum1, pnum2;
1229     int allocated1, allocated2;
1230     int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1231     bool progress = false, quiet = false, strict = false;
1232     int flags;
1233     bool writethrough;
1234     int64_t total_sectors;
1235     int64_t sector_num = 0;
1236     int64_t nb_sectors;
1237     int c, pnum;
1238     uint64_t progress_base;
1239     bool image_opts = false;
1240     bool force_share = false;
1241 
1242     cache = BDRV_DEFAULT_CACHE;
1243     for (;;) {
1244         static const struct option long_options[] = {
1245             {"help", no_argument, 0, 'h'},
1246             {"object", required_argument, 0, OPTION_OBJECT},
1247             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1248             {"force-share", no_argument, 0, 'U'},
1249             {0, 0, 0, 0}
1250         };
1251         c = getopt_long(argc, argv, ":hf:F:T:pqsU",
1252                         long_options, NULL);
1253         if (c == -1) {
1254             break;
1255         }
1256         switch (c) {
1257         case ':':
1258             missing_argument(argv[optind - 1]);
1259             break;
1260         case '?':
1261             unrecognized_option(argv[optind - 1]);
1262             break;
1263         case 'h':
1264             help();
1265             break;
1266         case 'f':
1267             fmt1 = optarg;
1268             break;
1269         case 'F':
1270             fmt2 = optarg;
1271             break;
1272         case 'T':
1273             cache = optarg;
1274             break;
1275         case 'p':
1276             progress = true;
1277             break;
1278         case 'q':
1279             quiet = true;
1280             break;
1281         case 's':
1282             strict = true;
1283             break;
1284         case 'U':
1285             force_share = true;
1286             break;
1287         case OPTION_OBJECT: {
1288             QemuOpts *opts;
1289             opts = qemu_opts_parse_noisily(&qemu_object_opts,
1290                                            optarg, true);
1291             if (!opts) {
1292                 ret = 2;
1293                 goto out4;
1294             }
1295         }   break;
1296         case OPTION_IMAGE_OPTS:
1297             image_opts = true;
1298             break;
1299         }
1300     }
1301 
1302     /* Progress is not shown in Quiet mode */
1303     if (quiet) {
1304         progress = false;
1305     }
1306 
1307 
1308     if (optind != argc - 2) {
1309         error_exit("Expecting two image file names");
1310     }
1311     filename1 = argv[optind++];
1312     filename2 = argv[optind++];
1313 
1314     if (qemu_opts_foreach(&qemu_object_opts,
1315                           user_creatable_add_opts_foreach,
1316                           NULL, NULL)) {
1317         ret = 2;
1318         goto out4;
1319     }
1320 
1321     /* Initialize before goto out */
1322     qemu_progress_init(progress, 2.0);
1323 
1324     flags = 0;
1325     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
1326     if (ret < 0) {
1327         error_report("Invalid source cache option: %s", cache);
1328         ret = 2;
1329         goto out3;
1330     }
1331 
1332     blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1333                     force_share);
1334     if (!blk1) {
1335         ret = 2;
1336         goto out3;
1337     }
1338 
1339     blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1340                     force_share);
1341     if (!blk2) {
1342         ret = 2;
1343         goto out2;
1344     }
1345     bs1 = blk_bs(blk1);
1346     bs2 = blk_bs(blk2);
1347 
1348     buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1349     buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1350     total_sectors1 = blk_nb_sectors(blk1);
1351     if (total_sectors1 < 0) {
1352         error_report("Can't get size of %s: %s",
1353                      filename1, strerror(-total_sectors1));
1354         ret = 4;
1355         goto out;
1356     }
1357     total_sectors2 = blk_nb_sectors(blk2);
1358     if (total_sectors2 < 0) {
1359         error_report("Can't get size of %s: %s",
1360                      filename2, strerror(-total_sectors2));
1361         ret = 4;
1362         goto out;
1363     }
1364     total_sectors = MIN(total_sectors1, total_sectors2);
1365     progress_base = MAX(total_sectors1, total_sectors2);
1366 
1367     qemu_progress_print(0, 100);
1368 
1369     if (strict && total_sectors1 != total_sectors2) {
1370         ret = 1;
1371         qprintf(quiet, "Strict mode: Image size mismatch!\n");
1372         goto out;
1373     }
1374 
1375     for (;;) {
1376         int64_t status1, status2;
1377         BlockDriverState *file;
1378 
1379         nb_sectors = sectors_to_process(total_sectors, sector_num);
1380         if (nb_sectors <= 0) {
1381             break;
1382         }
1383         status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
1384                                               total_sectors1 - sector_num,
1385                                               &pnum1, &file);
1386         if (status1 < 0) {
1387             ret = 3;
1388             error_report("Sector allocation test failed for %s", filename1);
1389             goto out;
1390         }
1391         allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
1392 
1393         status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
1394                                               total_sectors2 - sector_num,
1395                                               &pnum2, &file);
1396         if (status2 < 0) {
1397             ret = 3;
1398             error_report("Sector allocation test failed for %s", filename2);
1399             goto out;
1400         }
1401         allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1402         if (pnum1) {
1403             nb_sectors = MIN(nb_sectors, pnum1);
1404         }
1405         if (pnum2) {
1406             nb_sectors = MIN(nb_sectors, pnum2);
1407         }
1408 
1409         if (strict) {
1410             if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) !=
1411                 (status2 & ~BDRV_BLOCK_OFFSET_MASK)) {
1412                 ret = 1;
1413                 qprintf(quiet, "Strict mode: Offset %" PRId64
1414                         " block status mismatch!\n",
1415                         sectors_to_bytes(sector_num));
1416                 goto out;
1417             }
1418         }
1419         if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1420             nb_sectors = MIN(pnum1, pnum2);
1421         } else if (allocated1 == allocated2) {
1422             if (allocated1) {
1423                 ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1,
1424                                 nb_sectors << BDRV_SECTOR_BITS);
1425                 if (ret < 0) {
1426                     error_report("Error while reading offset %" PRId64 " of %s:"
1427                                  " %s", sectors_to_bytes(sector_num), filename1,
1428                                  strerror(-ret));
1429                     ret = 4;
1430                     goto out;
1431                 }
1432                 ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2,
1433                                 nb_sectors << BDRV_SECTOR_BITS);
1434                 if (ret < 0) {
1435                     error_report("Error while reading offset %" PRId64
1436                                  " of %s: %s", sectors_to_bytes(sector_num),
1437                                  filename2, strerror(-ret));
1438                     ret = 4;
1439                     goto out;
1440                 }
1441                 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1442                 if (ret || pnum != nb_sectors) {
1443                     qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1444                             sectors_to_bytes(
1445                                 ret ? sector_num : sector_num + pnum));
1446                     ret = 1;
1447                     goto out;
1448                 }
1449             }
1450         } else {
1451 
1452             if (allocated1) {
1453                 ret = check_empty_sectors(blk1, sector_num, nb_sectors,
1454                                           filename1, buf1, quiet);
1455             } else {
1456                 ret = check_empty_sectors(blk2, sector_num, nb_sectors,
1457                                           filename2, buf1, quiet);
1458             }
1459             if (ret) {
1460                 if (ret < 0) {
1461                     error_report("Error while reading offset %" PRId64 ": %s",
1462                                  sectors_to_bytes(sector_num), strerror(-ret));
1463                     ret = 4;
1464                 }
1465                 goto out;
1466             }
1467         }
1468         sector_num += nb_sectors;
1469         qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1470     }
1471 
1472     if (total_sectors1 != total_sectors2) {
1473         BlockBackend *blk_over;
1474         int64_t total_sectors_over;
1475         const char *filename_over;
1476 
1477         qprintf(quiet, "Warning: Image size mismatch!\n");
1478         if (total_sectors1 > total_sectors2) {
1479             total_sectors_over = total_sectors1;
1480             blk_over = blk1;
1481             filename_over = filename1;
1482         } else {
1483             total_sectors_over = total_sectors2;
1484             blk_over = blk2;
1485             filename_over = filename2;
1486         }
1487 
1488         for (;;) {
1489             int64_t count;
1490 
1491             nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1492             if (nb_sectors <= 0) {
1493                 break;
1494             }
1495             ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL,
1496                                           sector_num * BDRV_SECTOR_SIZE,
1497                                           nb_sectors * BDRV_SECTOR_SIZE,
1498                                           &count);
1499             if (ret < 0) {
1500                 ret = 3;
1501                 error_report("Sector allocation test failed for %s",
1502                              filename_over);
1503                 goto out;
1504 
1505             }
1506             /* TODO relax this once bdrv_is_allocated_above does not enforce
1507              * sector alignment */
1508             assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
1509             nb_sectors = count >> BDRV_SECTOR_BITS;
1510             if (ret) {
1511                 ret = check_empty_sectors(blk_over, sector_num, nb_sectors,
1512                                           filename_over, buf1, quiet);
1513                 if (ret) {
1514                     if (ret < 0) {
1515                         error_report("Error while reading offset %" PRId64
1516                                      " of %s: %s", sectors_to_bytes(sector_num),
1517                                      filename_over, strerror(-ret));
1518                         ret = 4;
1519                     }
1520                     goto out;
1521                 }
1522             }
1523             sector_num += nb_sectors;
1524             qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1525         }
1526     }
1527 
1528     qprintf(quiet, "Images are identical.\n");
1529     ret = 0;
1530 
1531 out:
1532     qemu_vfree(buf1);
1533     qemu_vfree(buf2);
1534     blk_unref(blk2);
1535 out2:
1536     blk_unref(blk1);
1537 out3:
1538     qemu_progress_end();
1539 out4:
1540     return ret;
1541 }
1542 
1543 enum ImgConvertBlockStatus {
1544     BLK_DATA,
1545     BLK_ZERO,
1546     BLK_BACKING_FILE,
1547 };
1548 
1549 #define MAX_COROUTINES 16
1550 
1551 typedef struct ImgConvertState {
1552     BlockBackend **src;
1553     int64_t *src_sectors;
1554     int src_num;
1555     int64_t total_sectors;
1556     int64_t allocated_sectors;
1557     int64_t allocated_done;
1558     int64_t sector_num;
1559     int64_t wr_offs;
1560     enum ImgConvertBlockStatus status;
1561     int64_t sector_next_status;
1562     BlockBackend *target;
1563     bool has_zero_init;
1564     bool compressed;
1565     bool target_has_backing;
1566     bool wr_in_order;
1567     int min_sparse;
1568     size_t cluster_sectors;
1569     size_t buf_sectors;
1570     long num_coroutines;
1571     int running_coroutines;
1572     Coroutine *co[MAX_COROUTINES];
1573     int64_t wait_sector_num[MAX_COROUTINES];
1574     CoMutex lock;
1575     int ret;
1576 } ImgConvertState;
1577 
1578 static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1579                                 int *src_cur, int64_t *src_cur_offset)
1580 {
1581     *src_cur = 0;
1582     *src_cur_offset = 0;
1583     while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1584         *src_cur_offset += s->src_sectors[*src_cur];
1585         (*src_cur)++;
1586         assert(*src_cur < s->src_num);
1587     }
1588 }
1589 
1590 static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1591 {
1592     int64_t ret, src_cur_offset;
1593     int n, src_cur;
1594 
1595     convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1596 
1597     assert(s->total_sectors > sector_num);
1598     n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1599 
1600     if (s->sector_next_status <= sector_num) {
1601         BlockDriverState *file;
1602         if (s->target_has_backing) {
1603             ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
1604                                         sector_num - src_cur_offset,
1605                                         n, &n, &file);
1606         } else {
1607             ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
1608                                               sector_num - src_cur_offset,
1609                                               n, &n, &file);
1610         }
1611         if (ret < 0) {
1612             return ret;
1613         }
1614 
1615         if (ret & BDRV_BLOCK_ZERO) {
1616             s->status = BLK_ZERO;
1617         } else if (ret & BDRV_BLOCK_DATA) {
1618             s->status = BLK_DATA;
1619         } else {
1620             s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
1621         }
1622 
1623         s->sector_next_status = sector_num + n;
1624     }
1625 
1626     n = MIN(n, s->sector_next_status - sector_num);
1627     if (s->status == BLK_DATA) {
1628         n = MIN(n, s->buf_sectors);
1629     }
1630 
1631     /* We need to write complete clusters for compressed images, so if an
1632      * unallocated area is shorter than that, we must consider the whole
1633      * cluster allocated. */
1634     if (s->compressed) {
1635         if (n < s->cluster_sectors) {
1636             n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1637             s->status = BLK_DATA;
1638         } else {
1639             n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1640         }
1641     }
1642 
1643     return n;
1644 }
1645 
1646 static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1647                                         int nb_sectors, uint8_t *buf)
1648 {
1649     int n, ret;
1650     QEMUIOVector qiov;
1651     struct iovec iov;
1652 
1653     assert(nb_sectors <= s->buf_sectors);
1654     while (nb_sectors > 0) {
1655         BlockBackend *blk;
1656         int src_cur;
1657         int64_t bs_sectors, src_cur_offset;
1658 
1659         /* In the case of compression with multiple source files, we can get a
1660          * nb_sectors that spreads into the next part. So we must be able to
1661          * read across multiple BDSes for one convert_read() call. */
1662         convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1663         blk = s->src[src_cur];
1664         bs_sectors = s->src_sectors[src_cur];
1665 
1666         n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1667         iov.iov_base = buf;
1668         iov.iov_len = n << BDRV_SECTOR_BITS;
1669         qemu_iovec_init_external(&qiov, &iov, 1);
1670 
1671         ret = blk_co_preadv(
1672                 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1673                 n << BDRV_SECTOR_BITS, &qiov, 0);
1674         if (ret < 0) {
1675             return ret;
1676         }
1677 
1678         sector_num += n;
1679         nb_sectors -= n;
1680         buf += n * BDRV_SECTOR_SIZE;
1681     }
1682 
1683     return 0;
1684 }
1685 
1686 
1687 static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1688                                          int nb_sectors, uint8_t *buf,
1689                                          enum ImgConvertBlockStatus status)
1690 {
1691     int ret;
1692     QEMUIOVector qiov;
1693     struct iovec iov;
1694 
1695     while (nb_sectors > 0) {
1696         int n = nb_sectors;
1697         BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1698 
1699         switch (status) {
1700         case BLK_BACKING_FILE:
1701             /* If we have a backing file, leave clusters unallocated that are
1702              * unallocated in the source image, so that the backing file is
1703              * visible at the respective offset. */
1704             assert(s->target_has_backing);
1705             break;
1706 
1707         case BLK_DATA:
1708             /* If we're told to keep the target fully allocated (-S 0) or there
1709              * is real non-zero data, we must write it. Otherwise we can treat
1710              * it as zero sectors.
1711              * Compressed clusters need to be written as a whole, so in that
1712              * case we can only save the write if the buffer is completely
1713              * zeroed. */
1714             if (!s->min_sparse ||
1715                 (!s->compressed &&
1716                  is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
1717                 (s->compressed &&
1718                  !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
1719             {
1720                 iov.iov_base = buf;
1721                 iov.iov_len = n << BDRV_SECTOR_BITS;
1722                 qemu_iovec_init_external(&qiov, &iov, 1);
1723 
1724                 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1725                                      n << BDRV_SECTOR_BITS, &qiov, flags);
1726                 if (ret < 0) {
1727                     return ret;
1728                 }
1729                 break;
1730             }
1731             /* fall-through */
1732 
1733         case BLK_ZERO:
1734             if (s->has_zero_init) {
1735                 assert(!s->target_has_backing);
1736                 break;
1737             }
1738             ret = blk_co_pwrite_zeroes(s->target,
1739                                        sector_num << BDRV_SECTOR_BITS,
1740                                        n << BDRV_SECTOR_BITS, 0);
1741             if (ret < 0) {
1742                 return ret;
1743             }
1744             break;
1745         }
1746 
1747         sector_num += n;
1748         nb_sectors -= n;
1749         buf += n * BDRV_SECTOR_SIZE;
1750     }
1751 
1752     return 0;
1753 }
1754 
1755 static void coroutine_fn convert_co_do_copy(void *opaque)
1756 {
1757     ImgConvertState *s = opaque;
1758     uint8_t *buf = NULL;
1759     int ret, i;
1760     int index = -1;
1761 
1762     for (i = 0; i < s->num_coroutines; i++) {
1763         if (s->co[i] == qemu_coroutine_self()) {
1764             index = i;
1765             break;
1766         }
1767     }
1768     assert(index >= 0);
1769 
1770     s->running_coroutines++;
1771     buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1772 
1773     while (1) {
1774         int n;
1775         int64_t sector_num;
1776         enum ImgConvertBlockStatus status;
1777 
1778         qemu_co_mutex_lock(&s->lock);
1779         if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1780             qemu_co_mutex_unlock(&s->lock);
1781             break;
1782         }
1783         n = convert_iteration_sectors(s, s->sector_num);
1784         if (n < 0) {
1785             qemu_co_mutex_unlock(&s->lock);
1786             s->ret = n;
1787             break;
1788         }
1789         /* save current sector and allocation status to local variables */
1790         sector_num = s->sector_num;
1791         status = s->status;
1792         if (!s->min_sparse && s->status == BLK_ZERO) {
1793             n = MIN(n, s->buf_sectors);
1794         }
1795         /* increment global sector counter so that other coroutines can
1796          * already continue reading beyond this request */
1797         s->sector_num += n;
1798         qemu_co_mutex_unlock(&s->lock);
1799 
1800         if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1801             s->allocated_done += n;
1802             qemu_progress_print(100.0 * s->allocated_done /
1803                                         s->allocated_sectors, 0);
1804         }
1805 
1806         if (status == BLK_DATA) {
1807             ret = convert_co_read(s, sector_num, n, buf);
1808             if (ret < 0) {
1809                 error_report("error while reading sector %" PRId64
1810                              ": %s", sector_num, strerror(-ret));
1811                 s->ret = ret;
1812             }
1813         } else if (!s->min_sparse && status == BLK_ZERO) {
1814             status = BLK_DATA;
1815             memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1816         }
1817 
1818         if (s->wr_in_order) {
1819             /* keep writes in order */
1820             while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
1821                 s->wait_sector_num[index] = sector_num;
1822                 qemu_coroutine_yield();
1823             }
1824             s->wait_sector_num[index] = -1;
1825         }
1826 
1827         if (s->ret == -EINPROGRESS) {
1828             ret = convert_co_write(s, sector_num, n, buf, status);
1829             if (ret < 0) {
1830                 error_report("error while writing sector %" PRId64
1831                              ": %s", sector_num, strerror(-ret));
1832                 s->ret = ret;
1833             }
1834         }
1835 
1836         if (s->wr_in_order) {
1837             /* reenter the coroutine that might have waited
1838              * for this write to complete */
1839             s->wr_offs = sector_num + n;
1840             for (i = 0; i < s->num_coroutines; i++) {
1841                 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1842                     /*
1843                      * A -> B -> A cannot occur because A has
1844                      * s->wait_sector_num[i] == -1 during A -> B.  Therefore
1845                      * B will never enter A during this time window.
1846                      */
1847                     qemu_coroutine_enter(s->co[i]);
1848                     break;
1849                 }
1850             }
1851         }
1852     }
1853 
1854     qemu_vfree(buf);
1855     s->co[index] = NULL;
1856     s->running_coroutines--;
1857     if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1858         /* the convert job finished successfully */
1859         s->ret = 0;
1860     }
1861 }
1862 
1863 static int convert_do_copy(ImgConvertState *s)
1864 {
1865     int ret, i, n;
1866     int64_t sector_num = 0;
1867 
1868     /* Check whether we have zero initialisation or can get it efficiently */
1869     s->has_zero_init = s->min_sparse && !s->target_has_backing
1870                      ? bdrv_has_zero_init(blk_bs(s->target))
1871                      : false;
1872 
1873     if (!s->has_zero_init && !s->target_has_backing &&
1874         bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1875     {
1876         ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
1877         if (ret == 0) {
1878             s->has_zero_init = true;
1879         }
1880     }
1881 
1882     /* Allocate buffer for copied data. For compressed images, only one cluster
1883      * can be copied at a time. */
1884     if (s->compressed) {
1885         if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1886             error_report("invalid cluster size");
1887             return -EINVAL;
1888         }
1889         s->buf_sectors = s->cluster_sectors;
1890     }
1891 
1892     while (sector_num < s->total_sectors) {
1893         n = convert_iteration_sectors(s, sector_num);
1894         if (n < 0) {
1895             return n;
1896         }
1897         if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1898         {
1899             s->allocated_sectors += n;
1900         }
1901         sector_num += n;
1902     }
1903 
1904     /* Do the copy */
1905     s->sector_next_status = 0;
1906     s->ret = -EINPROGRESS;
1907 
1908     qemu_co_mutex_init(&s->lock);
1909     for (i = 0; i < s->num_coroutines; i++) {
1910         s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1911         s->wait_sector_num[i] = -1;
1912         qemu_coroutine_enter(s->co[i]);
1913     }
1914 
1915     while (s->running_coroutines) {
1916         main_loop_wait(false);
1917     }
1918 
1919     if (s->compressed && !s->ret) {
1920         /* signal EOF to align */
1921         ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
1922         if (ret < 0) {
1923             return ret;
1924         }
1925     }
1926 
1927     return s->ret;
1928 }
1929 
1930 static int img_convert(int argc, char **argv)
1931 {
1932     int c, bs_i, flags, src_flags = 0;
1933     const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
1934                *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
1935                *out_filename, *out_baseimg_param, *snapshot_name = NULL;
1936     BlockDriver *drv = NULL, *proto_drv = NULL;
1937     BlockDriverInfo bdi;
1938     BlockDriverState *out_bs;
1939     QemuOpts *opts = NULL, *sn_opts = NULL;
1940     QemuOptsList *create_opts = NULL;
1941     char *options = NULL;
1942     Error *local_err = NULL;
1943     bool writethrough, src_writethrough, quiet = false, image_opts = false,
1944          skip_create = false, progress = false, tgt_image_opts = false;
1945     int64_t ret = -EINVAL;
1946     bool force_share = false;
1947 
1948     ImgConvertState s = (ImgConvertState) {
1949         /* Need at least 4k of zeros for sparse detection */
1950         .min_sparse         = 8,
1951         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
1952         .wr_in_order        = true,
1953         .num_coroutines     = 8,
1954     };
1955 
1956     for(;;) {
1957         static const struct option long_options[] = {
1958             {"help", no_argument, 0, 'h'},
1959             {"object", required_argument, 0, OPTION_OBJECT},
1960             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1961             {"force-share", no_argument, 0, 'U'},
1962             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
1963             {0, 0, 0, 0}
1964         };
1965         c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU",
1966                         long_options, NULL);
1967         if (c == -1) {
1968             break;
1969         }
1970         switch(c) {
1971         case ':':
1972             missing_argument(argv[optind - 1]);
1973             break;
1974         case '?':
1975             unrecognized_option(argv[optind - 1]);
1976             break;
1977         case 'h':
1978             help();
1979             break;
1980         case 'f':
1981             fmt = optarg;
1982             break;
1983         case 'O':
1984             out_fmt = optarg;
1985             break;
1986         case 'B':
1987             out_baseimg = optarg;
1988             break;
1989         case 'c':
1990             s.compressed = true;
1991             break;
1992         case 'o':
1993             if (!is_valid_option_list(optarg)) {
1994                 error_report("Invalid option list: %s", optarg);
1995                 goto fail_getopt;
1996             }
1997             if (!options) {
1998                 options = g_strdup(optarg);
1999             } else {
2000                 char *old_options = options;
2001                 options = g_strdup_printf("%s,%s", options, optarg);
2002                 g_free(old_options);
2003             }
2004             break;
2005         case 's':
2006             snapshot_name = optarg;
2007             break;
2008         case 'l':
2009             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
2010                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2011                                                   optarg, false);
2012                 if (!sn_opts) {
2013                     error_report("Failed in parsing snapshot param '%s'",
2014                                  optarg);
2015                     goto fail_getopt;
2016                 }
2017             } else {
2018                 snapshot_name = optarg;
2019             }
2020             break;
2021         case 'S':
2022         {
2023             int64_t sval;
2024 
2025             sval = cvtnum(optarg);
2026             if (sval < 0) {
2027                 error_report("Invalid minimum zero buffer size for sparse output specified");
2028                 goto fail_getopt;
2029             }
2030 
2031             s.min_sparse = sval / BDRV_SECTOR_SIZE;
2032             break;
2033         }
2034         case 'p':
2035             progress = true;
2036             break;
2037         case 't':
2038             cache = optarg;
2039             break;
2040         case 'T':
2041             src_cache = optarg;
2042             break;
2043         case 'q':
2044             quiet = true;
2045             break;
2046         case 'n':
2047             skip_create = true;
2048             break;
2049         case 'm':
2050             if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2051                 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2052                 error_report("Invalid number of coroutines. Allowed number of"
2053                              " coroutines is between 1 and %d", MAX_COROUTINES);
2054                 goto fail_getopt;
2055             }
2056             break;
2057         case 'W':
2058             s.wr_in_order = false;
2059             break;
2060         case 'U':
2061             force_share = true;
2062             break;
2063         case OPTION_OBJECT: {
2064             QemuOpts *object_opts;
2065             object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2066                                                   optarg, true);
2067             if (!object_opts) {
2068                 goto fail_getopt;
2069             }
2070             break;
2071         }
2072         case OPTION_IMAGE_OPTS:
2073             image_opts = true;
2074             break;
2075         case OPTION_TARGET_IMAGE_OPTS:
2076             tgt_image_opts = true;
2077             break;
2078         }
2079     }
2080 
2081     if (!out_fmt && !tgt_image_opts) {
2082         out_fmt = "raw";
2083     }
2084 
2085     if (qemu_opts_foreach(&qemu_object_opts,
2086                           user_creatable_add_opts_foreach,
2087                           NULL, NULL)) {
2088         goto fail_getopt;
2089     }
2090 
2091     if (!s.wr_in_order && s.compressed) {
2092         error_report("Out of order write and compress are mutually exclusive");
2093         goto fail_getopt;
2094     }
2095 
2096     if (tgt_image_opts && !skip_create) {
2097         error_report("--target-image-opts requires use of -n flag");
2098         goto fail_getopt;
2099     }
2100 
2101     s.src_num = argc - optind - 1;
2102     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2103 
2104     if (options && has_help_option(options)) {
2105         if (out_fmt) {
2106             ret = print_block_option_help(out_filename, out_fmt);
2107             goto fail_getopt;
2108         } else {
2109             error_report("Option help requires a format be specified");
2110             goto fail_getopt;
2111         }
2112     }
2113 
2114     if (s.src_num < 1) {
2115         error_report("Must specify image file name");
2116         goto fail_getopt;
2117     }
2118 
2119 
2120     /* ret is still -EINVAL until here */
2121     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2122     if (ret < 0) {
2123         error_report("Invalid source cache option: %s", src_cache);
2124         goto fail_getopt;
2125     }
2126 
2127     /* Initialize before goto out */
2128     if (quiet) {
2129         progress = false;
2130     }
2131     qemu_progress_init(progress, 1.0);
2132     qemu_progress_print(0, 100);
2133 
2134     s.src = g_new0(BlockBackend *, s.src_num);
2135     s.src_sectors = g_new(int64_t, s.src_num);
2136 
2137     for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2138         s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2139                                fmt, src_flags, src_writethrough, quiet,
2140                                force_share);
2141         if (!s.src[bs_i]) {
2142             ret = -1;
2143             goto out;
2144         }
2145         s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2146         if (s.src_sectors[bs_i] < 0) {
2147             error_report("Could not get size of %s: %s",
2148                          argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
2149             ret = -1;
2150             goto out;
2151         }
2152         s.total_sectors += s.src_sectors[bs_i];
2153     }
2154 
2155     if (sn_opts) {
2156         bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
2157                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2158                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2159                                &local_err);
2160     } else if (snapshot_name != NULL) {
2161         if (s.src_num > 1) {
2162             error_report("No support for concatenating multiple snapshot");
2163             ret = -1;
2164             goto out;
2165         }
2166 
2167         bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2168                                              &local_err);
2169     }
2170     if (local_err) {
2171         error_reportf_err(local_err, "Failed to load snapshot: ");
2172         ret = -1;
2173         goto out;
2174     }
2175 
2176     if (!skip_create) {
2177         /* Find driver and parse its options */
2178         drv = bdrv_find_format(out_fmt);
2179         if (!drv) {
2180             error_report("Unknown file format '%s'", out_fmt);
2181             ret = -1;
2182             goto out;
2183         }
2184 
2185         proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2186         if (!proto_drv) {
2187             error_report_err(local_err);
2188             ret = -1;
2189             goto out;
2190         }
2191 
2192         if (!drv->create_opts) {
2193             error_report("Format driver '%s' does not support image creation",
2194                          drv->format_name);
2195             ret = -1;
2196             goto out;
2197         }
2198 
2199         if (!proto_drv->create_opts) {
2200             error_report("Protocol driver '%s' does not support image creation",
2201                          proto_drv->format_name);
2202             ret = -1;
2203             goto out;
2204         }
2205 
2206         create_opts = qemu_opts_append(create_opts, drv->create_opts);
2207         create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
2208 
2209         opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2210         if (options) {
2211             qemu_opts_do_parse(opts, options, NULL, &local_err);
2212             if (local_err) {
2213                 error_report_err(local_err);
2214                 ret = -1;
2215                 goto out;
2216             }
2217         }
2218 
2219         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
2220                             &error_abort);
2221         ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2222         if (ret < 0) {
2223             goto out;
2224         }
2225     }
2226 
2227     /* Get backing file name if -o backing_file was used */
2228     out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2229     if (out_baseimg_param) {
2230         out_baseimg = out_baseimg_param;
2231     }
2232     s.target_has_backing = (bool) out_baseimg;
2233 
2234     if (s.src_num > 1 && out_baseimg) {
2235         error_report("Having a backing file for the target makes no sense when "
2236                      "concatenating multiple input images");
2237         ret = -1;
2238         goto out;
2239     }
2240 
2241     /* Check if compression is supported */
2242     if (s.compressed) {
2243         bool encryption =
2244             qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2245         const char *encryptfmt =
2246             qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2247         const char *preallocation =
2248             qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
2249 
2250         if (drv && !drv->bdrv_co_pwritev_compressed) {
2251             error_report("Compression not supported for this file format");
2252             ret = -1;
2253             goto out;
2254         }
2255 
2256         if (encryption || encryptfmt) {
2257             error_report("Compression and encryption not supported at "
2258                          "the same time");
2259             ret = -1;
2260             goto out;
2261         }
2262 
2263         if (preallocation
2264             && strcmp(preallocation, "off"))
2265         {
2266             error_report("Compression and preallocation not supported at "
2267                          "the same time");
2268             ret = -1;
2269             goto out;
2270         }
2271     }
2272 
2273     if (!skip_create) {
2274         /* Create the new image */
2275         ret = bdrv_create(drv, out_filename, opts, &local_err);
2276         if (ret < 0) {
2277             error_reportf_err(local_err, "%s: error while converting %s: ",
2278                               out_filename, out_fmt);
2279             goto out;
2280         }
2281     }
2282 
2283     flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
2284     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
2285     if (ret < 0) {
2286         error_report("Invalid cache option: %s", cache);
2287         goto out;
2288     }
2289 
2290     if (skip_create) {
2291         s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2292                             flags, writethrough, quiet, false);
2293     } else {
2294         /* TODO ultimately we should allow --target-image-opts
2295          * to be used even when -n is not given.
2296          * That has to wait for bdrv_create to be improved
2297          * to allow filenames in option syntax
2298          */
2299         s.target = img_open_new_file(out_filename, opts, out_fmt,
2300                                      flags, writethrough, quiet, false);
2301     }
2302     if (!s.target) {
2303         ret = -1;
2304         goto out;
2305     }
2306     out_bs = blk_bs(s.target);
2307 
2308     if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2309         error_report("Compression not supported for this file format");
2310         ret = -1;
2311         goto out;
2312     }
2313 
2314     /* increase bufsectors from the default 4096 (2M) if opt_transfer
2315      * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
2316      * as maximum. */
2317     s.buf_sectors = MIN(32768,
2318                         MAX(s.buf_sectors,
2319                             MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2320                                 out_bs->bl.pdiscard_alignment >>
2321                                 BDRV_SECTOR_BITS)));
2322 
2323     if (skip_create) {
2324         int64_t output_sectors = blk_nb_sectors(s.target);
2325         if (output_sectors < 0) {
2326             error_report("unable to get output image length: %s",
2327                          strerror(-output_sectors));
2328             ret = -1;
2329             goto out;
2330         } else if (output_sectors < s.total_sectors) {
2331             error_report("output file is smaller than input file");
2332             ret = -1;
2333             goto out;
2334         }
2335     }
2336 
2337     ret = bdrv_get_info(out_bs, &bdi);
2338     if (ret < 0) {
2339         if (s.compressed) {
2340             error_report("could not get block driver info");
2341             goto out;
2342         }
2343     } else {
2344         s.compressed = s.compressed || bdi.needs_compressed_writes;
2345         s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2346     }
2347 
2348     ret = convert_do_copy(&s);
2349 out:
2350     if (!ret) {
2351         qemu_progress_print(100, 0);
2352     }
2353     qemu_progress_end();
2354     qemu_opts_del(opts);
2355     qemu_opts_free(create_opts);
2356     qemu_opts_del(sn_opts);
2357     blk_unref(s.target);
2358     if (s.src) {
2359         for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2360             blk_unref(s.src[bs_i]);
2361         }
2362         g_free(s.src);
2363     }
2364     g_free(s.src_sectors);
2365 fail_getopt:
2366     g_free(options);
2367 
2368     return !!ret;
2369 }
2370 
2371 
2372 static void dump_snapshots(BlockDriverState *bs)
2373 {
2374     QEMUSnapshotInfo *sn_tab, *sn;
2375     int nb_sns, i;
2376 
2377     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2378     if (nb_sns <= 0)
2379         return;
2380     printf("Snapshot list:\n");
2381     bdrv_snapshot_dump(fprintf, stdout, NULL);
2382     printf("\n");
2383     for(i = 0; i < nb_sns; i++) {
2384         sn = &sn_tab[i];
2385         bdrv_snapshot_dump(fprintf, stdout, sn);
2386         printf("\n");
2387     }
2388     g_free(sn_tab);
2389 }
2390 
2391 static void dump_json_image_info_list(ImageInfoList *list)
2392 {
2393     QString *str;
2394     QObject *obj;
2395     Visitor *v = qobject_output_visitor_new(&obj);
2396 
2397     visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2398     visit_complete(v, &obj);
2399     str = qobject_to_json_pretty(obj);
2400     assert(str != NULL);
2401     printf("%s\n", qstring_get_str(str));
2402     qobject_decref(obj);
2403     visit_free(v);
2404     QDECREF(str);
2405 }
2406 
2407 static void dump_json_image_info(ImageInfo *info)
2408 {
2409     QString *str;
2410     QObject *obj;
2411     Visitor *v = qobject_output_visitor_new(&obj);
2412 
2413     visit_type_ImageInfo(v, NULL, &info, &error_abort);
2414     visit_complete(v, &obj);
2415     str = qobject_to_json_pretty(obj);
2416     assert(str != NULL);
2417     printf("%s\n", qstring_get_str(str));
2418     qobject_decref(obj);
2419     visit_free(v);
2420     QDECREF(str);
2421 }
2422 
2423 static void dump_human_image_info_list(ImageInfoList *list)
2424 {
2425     ImageInfoList *elem;
2426     bool delim = false;
2427 
2428     for (elem = list; elem; elem = elem->next) {
2429         if (delim) {
2430             printf("\n");
2431         }
2432         delim = true;
2433 
2434         bdrv_image_info_dump(fprintf, stdout, elem->value);
2435     }
2436 }
2437 
2438 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2439 {
2440     return strcmp(a, b) == 0;
2441 }
2442 
2443 /**
2444  * Open an image file chain and return an ImageInfoList
2445  *
2446  * @filename: topmost image filename
2447  * @fmt: topmost image format (may be NULL to autodetect)
2448  * @chain: true  - enumerate entire backing file chain
2449  *         false - only topmost image file
2450  *
2451  * Returns a list of ImageInfo objects or NULL if there was an error opening an
2452  * image file.  If there was an error a message will have been printed to
2453  * stderr.
2454  */
2455 static ImageInfoList *collect_image_info_list(bool image_opts,
2456                                               const char *filename,
2457                                               const char *fmt,
2458                                               bool chain, bool force_share)
2459 {
2460     ImageInfoList *head = NULL;
2461     ImageInfoList **last = &head;
2462     GHashTable *filenames;
2463     Error *err = NULL;
2464 
2465     filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2466 
2467     while (filename) {
2468         BlockBackend *blk;
2469         BlockDriverState *bs;
2470         ImageInfo *info;
2471         ImageInfoList *elem;
2472 
2473         if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2474             error_report("Backing file '%s' creates an infinite loop.",
2475                          filename);
2476             goto err;
2477         }
2478         g_hash_table_insert(filenames, (gpointer)filename, NULL);
2479 
2480         blk = img_open(image_opts, filename, fmt,
2481                        BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2482                        force_share);
2483         if (!blk) {
2484             goto err;
2485         }
2486         bs = blk_bs(blk);
2487 
2488         bdrv_query_image_info(bs, &info, &err);
2489         if (err) {
2490             error_report_err(err);
2491             blk_unref(blk);
2492             goto err;
2493         }
2494 
2495         elem = g_new0(ImageInfoList, 1);
2496         elem->value = info;
2497         *last = elem;
2498         last = &elem->next;
2499 
2500         blk_unref(blk);
2501 
2502         filename = fmt = NULL;
2503         if (chain) {
2504             if (info->has_full_backing_filename) {
2505                 filename = info->full_backing_filename;
2506             } else if (info->has_backing_filename) {
2507                 error_report("Could not determine absolute backing filename,"
2508                              " but backing filename '%s' present",
2509                              info->backing_filename);
2510                 goto err;
2511             }
2512             if (info->has_backing_filename_format) {
2513                 fmt = info->backing_filename_format;
2514             }
2515         }
2516     }
2517     g_hash_table_destroy(filenames);
2518     return head;
2519 
2520 err:
2521     qapi_free_ImageInfoList(head);
2522     g_hash_table_destroy(filenames);
2523     return NULL;
2524 }
2525 
2526 static int img_info(int argc, char **argv)
2527 {
2528     int c;
2529     OutputFormat output_format = OFORMAT_HUMAN;
2530     bool chain = false;
2531     const char *filename, *fmt, *output;
2532     ImageInfoList *list;
2533     bool image_opts = false;
2534     bool force_share = false;
2535 
2536     fmt = NULL;
2537     output = NULL;
2538     for(;;) {
2539         int option_index = 0;
2540         static const struct option long_options[] = {
2541             {"help", no_argument, 0, 'h'},
2542             {"format", required_argument, 0, 'f'},
2543             {"output", required_argument, 0, OPTION_OUTPUT},
2544             {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
2545             {"object", required_argument, 0, OPTION_OBJECT},
2546             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2547             {"force-share", no_argument, 0, 'U'},
2548             {0, 0, 0, 0}
2549         };
2550         c = getopt_long(argc, argv, ":f:hU",
2551                         long_options, &option_index);
2552         if (c == -1) {
2553             break;
2554         }
2555         switch(c) {
2556         case ':':
2557             missing_argument(argv[optind - 1]);
2558             break;
2559         case '?':
2560             unrecognized_option(argv[optind - 1]);
2561             break;
2562         case 'h':
2563             help();
2564             break;
2565         case 'f':
2566             fmt = optarg;
2567             break;
2568         case 'U':
2569             force_share = true;
2570             break;
2571         case OPTION_OUTPUT:
2572             output = optarg;
2573             break;
2574         case OPTION_BACKING_CHAIN:
2575             chain = true;
2576             break;
2577         case OPTION_OBJECT: {
2578             QemuOpts *opts;
2579             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2580                                            optarg, true);
2581             if (!opts) {
2582                 return 1;
2583             }
2584         }   break;
2585         case OPTION_IMAGE_OPTS:
2586             image_opts = true;
2587             break;
2588         }
2589     }
2590     if (optind != argc - 1) {
2591         error_exit("Expecting one image file name");
2592     }
2593     filename = argv[optind++];
2594 
2595     if (output && !strcmp(output, "json")) {
2596         output_format = OFORMAT_JSON;
2597     } else if (output && !strcmp(output, "human")) {
2598         output_format = OFORMAT_HUMAN;
2599     } else if (output) {
2600         error_report("--output must be used with human or json as argument.");
2601         return 1;
2602     }
2603 
2604     if (qemu_opts_foreach(&qemu_object_opts,
2605                           user_creatable_add_opts_foreach,
2606                           NULL, NULL)) {
2607         return 1;
2608     }
2609 
2610     list = collect_image_info_list(image_opts, filename, fmt, chain,
2611                                    force_share);
2612     if (!list) {
2613         return 1;
2614     }
2615 
2616     switch (output_format) {
2617     case OFORMAT_HUMAN:
2618         dump_human_image_info_list(list);
2619         break;
2620     case OFORMAT_JSON:
2621         if (chain) {
2622             dump_json_image_info_list(list);
2623         } else {
2624             dump_json_image_info(list->value);
2625         }
2626         break;
2627     }
2628 
2629     qapi_free_ImageInfoList(list);
2630     return 0;
2631 }
2632 
2633 static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2634                            MapEntry *next)
2635 {
2636     switch (output_format) {
2637     case OFORMAT_HUMAN:
2638         if (e->data && !e->has_offset) {
2639             error_report("File contains external, encrypted or compressed clusters.");
2640             exit(1);
2641         }
2642         if (e->data && !e->zero) {
2643             printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
2644                    e->start, e->length,
2645                    e->has_offset ? e->offset : 0,
2646                    e->has_filename ? e->filename : "");
2647         }
2648         /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2649          * Modify the flags here to allow more coalescing.
2650          */
2651         if (next && (!next->data || next->zero)) {
2652             next->data = false;
2653             next->zero = true;
2654         }
2655         break;
2656     case OFORMAT_JSON:
2657         printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2658                " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
2659                (e->start == 0 ? "[" : ",\n"),
2660                e->start, e->length, e->depth,
2661                e->zero ? "true" : "false",
2662                e->data ? "true" : "false");
2663         if (e->has_offset) {
2664             printf(", \"offset\": %"PRId64"", e->offset);
2665         }
2666         putchar('}');
2667 
2668         if (!next) {
2669             printf("]\n");
2670         }
2671         break;
2672     }
2673 }
2674 
2675 static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2676                             int nb_sectors, MapEntry *e)
2677 {
2678     int64_t ret;
2679     int depth;
2680     BlockDriverState *file;
2681     bool has_offset;
2682 
2683     /* As an optimization, we could cache the current range of unallocated
2684      * clusters in each file of the chain, and avoid querying the same
2685      * range repeatedly.
2686      */
2687 
2688     depth = 0;
2689     for (;;) {
2690         ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors,
2691                                     &file);
2692         if (ret < 0) {
2693             return ret;
2694         }
2695         assert(nb_sectors);
2696         if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2697             break;
2698         }
2699         bs = backing_bs(bs);
2700         if (bs == NULL) {
2701             ret = 0;
2702             break;
2703         }
2704 
2705         depth++;
2706     }
2707 
2708     has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2709 
2710     *e = (MapEntry) {
2711         .start = sector_num * BDRV_SECTOR_SIZE,
2712         .length = nb_sectors * BDRV_SECTOR_SIZE,
2713         .data = !!(ret & BDRV_BLOCK_DATA),
2714         .zero = !!(ret & BDRV_BLOCK_ZERO),
2715         .offset = ret & BDRV_BLOCK_OFFSET_MASK,
2716         .has_offset = has_offset,
2717         .depth = depth,
2718         .has_filename = file && has_offset,
2719         .filename = file && has_offset ? file->filename : NULL,
2720     };
2721 
2722     return 0;
2723 }
2724 
2725 static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2726 {
2727     if (curr->length == 0) {
2728         return false;
2729     }
2730     if (curr->zero != next->zero ||
2731         curr->data != next->data ||
2732         curr->depth != next->depth ||
2733         curr->has_filename != next->has_filename ||
2734         curr->has_offset != next->has_offset) {
2735         return false;
2736     }
2737     if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2738         return false;
2739     }
2740     if (curr->has_offset && curr->offset + curr->length != next->offset) {
2741         return false;
2742     }
2743     return true;
2744 }
2745 
2746 static int img_map(int argc, char **argv)
2747 {
2748     int c;
2749     OutputFormat output_format = OFORMAT_HUMAN;
2750     BlockBackend *blk;
2751     BlockDriverState *bs;
2752     const char *filename, *fmt, *output;
2753     int64_t length;
2754     MapEntry curr = { .length = 0 }, next;
2755     int ret = 0;
2756     bool image_opts = false;
2757     bool force_share = false;
2758 
2759     fmt = NULL;
2760     output = NULL;
2761     for (;;) {
2762         int option_index = 0;
2763         static const struct option long_options[] = {
2764             {"help", no_argument, 0, 'h'},
2765             {"format", required_argument, 0, 'f'},
2766             {"output", required_argument, 0, OPTION_OUTPUT},
2767             {"object", required_argument, 0, OPTION_OBJECT},
2768             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2769             {"force-share", no_argument, 0, 'U'},
2770             {0, 0, 0, 0}
2771         };
2772         c = getopt_long(argc, argv, ":f:hU",
2773                         long_options, &option_index);
2774         if (c == -1) {
2775             break;
2776         }
2777         switch (c) {
2778         case ':':
2779             missing_argument(argv[optind - 1]);
2780             break;
2781         case '?':
2782             unrecognized_option(argv[optind - 1]);
2783             break;
2784         case 'h':
2785             help();
2786             break;
2787         case 'f':
2788             fmt = optarg;
2789             break;
2790         case 'U':
2791             force_share = true;
2792             break;
2793         case OPTION_OUTPUT:
2794             output = optarg;
2795             break;
2796         case OPTION_OBJECT: {
2797             QemuOpts *opts;
2798             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2799                                            optarg, true);
2800             if (!opts) {
2801                 return 1;
2802             }
2803         }   break;
2804         case OPTION_IMAGE_OPTS:
2805             image_opts = true;
2806             break;
2807         }
2808     }
2809     if (optind != argc - 1) {
2810         error_exit("Expecting one image file name");
2811     }
2812     filename = argv[optind];
2813 
2814     if (output && !strcmp(output, "json")) {
2815         output_format = OFORMAT_JSON;
2816     } else if (output && !strcmp(output, "human")) {
2817         output_format = OFORMAT_HUMAN;
2818     } else if (output) {
2819         error_report("--output must be used with human or json as argument.");
2820         return 1;
2821     }
2822 
2823     if (qemu_opts_foreach(&qemu_object_opts,
2824                           user_creatable_add_opts_foreach,
2825                           NULL, NULL)) {
2826         return 1;
2827     }
2828 
2829     blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
2830     if (!blk) {
2831         return 1;
2832     }
2833     bs = blk_bs(blk);
2834 
2835     if (output_format == OFORMAT_HUMAN) {
2836         printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2837     }
2838 
2839     length = blk_getlength(blk);
2840     while (curr.start + curr.length < length) {
2841         int64_t nsectors_left;
2842         int64_t sector_num;
2843         int n;
2844 
2845         sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2846 
2847         /* Probe up to 1 GiB at a time.  */
2848         nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2849         n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2850         ret = get_block_status(bs, sector_num, n, &next);
2851 
2852         if (ret < 0) {
2853             error_report("Could not read file metadata: %s", strerror(-ret));
2854             goto out;
2855         }
2856 
2857         if (entry_mergeable(&curr, &next)) {
2858             curr.length += next.length;
2859             continue;
2860         }
2861 
2862         if (curr.length > 0) {
2863             dump_map_entry(output_format, &curr, &next);
2864         }
2865         curr = next;
2866     }
2867 
2868     dump_map_entry(output_format, &curr, NULL);
2869 
2870 out:
2871     blk_unref(blk);
2872     return ret < 0;
2873 }
2874 
2875 #define SNAPSHOT_LIST   1
2876 #define SNAPSHOT_CREATE 2
2877 #define SNAPSHOT_APPLY  3
2878 #define SNAPSHOT_DELETE 4
2879 
2880 static int img_snapshot(int argc, char **argv)
2881 {
2882     BlockBackend *blk;
2883     BlockDriverState *bs;
2884     QEMUSnapshotInfo sn;
2885     char *filename, *snapshot_name = NULL;
2886     int c, ret = 0, bdrv_oflags;
2887     int action = 0;
2888     qemu_timeval tv;
2889     bool quiet = false;
2890     Error *err = NULL;
2891     bool image_opts = false;
2892     bool force_share = false;
2893 
2894     bdrv_oflags = BDRV_O_RDWR;
2895     /* Parse commandline parameters */
2896     for(;;) {
2897         static const struct option long_options[] = {
2898             {"help", no_argument, 0, 'h'},
2899             {"object", required_argument, 0, OPTION_OBJECT},
2900             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2901             {"force-share", no_argument, 0, 'U'},
2902             {0, 0, 0, 0}
2903         };
2904         c = getopt_long(argc, argv, ":la:c:d:hqU",
2905                         long_options, NULL);
2906         if (c == -1) {
2907             break;
2908         }
2909         switch(c) {
2910         case ':':
2911             missing_argument(argv[optind - 1]);
2912             break;
2913         case '?':
2914             unrecognized_option(argv[optind - 1]);
2915             break;
2916         case 'h':
2917             help();
2918             return 0;
2919         case 'l':
2920             if (action) {
2921                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2922                 return 0;
2923             }
2924             action = SNAPSHOT_LIST;
2925             bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
2926             break;
2927         case 'a':
2928             if (action) {
2929                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2930                 return 0;
2931             }
2932             action = SNAPSHOT_APPLY;
2933             snapshot_name = optarg;
2934             break;
2935         case 'c':
2936             if (action) {
2937                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2938                 return 0;
2939             }
2940             action = SNAPSHOT_CREATE;
2941             snapshot_name = optarg;
2942             break;
2943         case 'd':
2944             if (action) {
2945                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
2946                 return 0;
2947             }
2948             action = SNAPSHOT_DELETE;
2949             snapshot_name = optarg;
2950             break;
2951         case 'q':
2952             quiet = true;
2953             break;
2954         case 'U':
2955             force_share = true;
2956             break;
2957         case OPTION_OBJECT: {
2958             QemuOpts *opts;
2959             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2960                                            optarg, true);
2961             if (!opts) {
2962                 return 1;
2963             }
2964         }   break;
2965         case OPTION_IMAGE_OPTS:
2966             image_opts = true;
2967             break;
2968         }
2969     }
2970 
2971     if (optind != argc - 1) {
2972         error_exit("Expecting one image file name");
2973     }
2974     filename = argv[optind++];
2975 
2976     if (qemu_opts_foreach(&qemu_object_opts,
2977                           user_creatable_add_opts_foreach,
2978                           NULL, NULL)) {
2979         return 1;
2980     }
2981 
2982     /* Open the image */
2983     blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
2984                    force_share);
2985     if (!blk) {
2986         return 1;
2987     }
2988     bs = blk_bs(blk);
2989 
2990     /* Perform the requested action */
2991     switch(action) {
2992     case SNAPSHOT_LIST:
2993         dump_snapshots(bs);
2994         break;
2995 
2996     case SNAPSHOT_CREATE:
2997         memset(&sn, 0, sizeof(sn));
2998         pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2999 
3000         qemu_gettimeofday(&tv);
3001         sn.date_sec = tv.tv_sec;
3002         sn.date_nsec = tv.tv_usec * 1000;
3003 
3004         ret = bdrv_snapshot_create(bs, &sn);
3005         if (ret) {
3006             error_report("Could not create snapshot '%s': %d (%s)",
3007                 snapshot_name, ret, strerror(-ret));
3008         }
3009         break;
3010 
3011     case SNAPSHOT_APPLY:
3012         ret = bdrv_snapshot_goto(bs, snapshot_name);
3013         if (ret) {
3014             error_report("Could not apply snapshot '%s': %d (%s)",
3015                 snapshot_name, ret, strerror(-ret));
3016         }
3017         break;
3018 
3019     case SNAPSHOT_DELETE:
3020         bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
3021         if (err) {
3022             error_reportf_err(err, "Could not delete snapshot '%s': ",
3023                               snapshot_name);
3024             ret = 1;
3025         }
3026         break;
3027     }
3028 
3029     /* Cleanup */
3030     blk_unref(blk);
3031     if (ret) {
3032         return 1;
3033     }
3034     return 0;
3035 }
3036 
3037 static int img_rebase(int argc, char **argv)
3038 {
3039     BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
3040     uint8_t *buf_old = NULL;
3041     uint8_t *buf_new = NULL;
3042     BlockDriverState *bs = NULL;
3043     char *filename;
3044     const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3045     int c, flags, src_flags, ret;
3046     bool writethrough, src_writethrough;
3047     int unsafe = 0;
3048     bool force_share = false;
3049     int progress = 0;
3050     bool quiet = false;
3051     Error *local_err = NULL;
3052     bool image_opts = false;
3053 
3054     /* Parse commandline parameters */
3055     fmt = NULL;
3056     cache = BDRV_DEFAULT_CACHE;
3057     src_cache = BDRV_DEFAULT_CACHE;
3058     out_baseimg = NULL;
3059     out_basefmt = NULL;
3060     for(;;) {
3061         static const struct option long_options[] = {
3062             {"help", no_argument, 0, 'h'},
3063             {"object", required_argument, 0, OPTION_OBJECT},
3064             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3065             {"force-share", no_argument, 0, 'U'},
3066             {0, 0, 0, 0}
3067         };
3068         c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
3069                         long_options, NULL);
3070         if (c == -1) {
3071             break;
3072         }
3073         switch(c) {
3074         case ':':
3075             missing_argument(argv[optind - 1]);
3076             break;
3077         case '?':
3078             unrecognized_option(argv[optind - 1]);
3079             break;
3080         case 'h':
3081             help();
3082             return 0;
3083         case 'f':
3084             fmt = optarg;
3085             break;
3086         case 'F':
3087             out_basefmt = optarg;
3088             break;
3089         case 'b':
3090             out_baseimg = optarg;
3091             break;
3092         case 'u':
3093             unsafe = 1;
3094             break;
3095         case 'p':
3096             progress = 1;
3097             break;
3098         case 't':
3099             cache = optarg;
3100             break;
3101         case 'T':
3102             src_cache = optarg;
3103             break;
3104         case 'q':
3105             quiet = true;
3106             break;
3107         case OPTION_OBJECT: {
3108             QemuOpts *opts;
3109             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3110                                            optarg, true);
3111             if (!opts) {
3112                 return 1;
3113             }
3114         }   break;
3115         case OPTION_IMAGE_OPTS:
3116             image_opts = true;
3117             break;
3118         case 'U':
3119             force_share = true;
3120             break;
3121         }
3122     }
3123 
3124     if (quiet) {
3125         progress = 0;
3126     }
3127 
3128     if (optind != argc - 1) {
3129         error_exit("Expecting one image file name");
3130     }
3131     if (!unsafe && !out_baseimg) {
3132         error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3133     }
3134     filename = argv[optind++];
3135 
3136     if (qemu_opts_foreach(&qemu_object_opts,
3137                           user_creatable_add_opts_foreach,
3138                           NULL, NULL)) {
3139         return 1;
3140     }
3141 
3142     qemu_progress_init(progress, 2.0);
3143     qemu_progress_print(0, 100);
3144 
3145     flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
3146     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3147     if (ret < 0) {
3148         error_report("Invalid cache option: %s", cache);
3149         goto out;
3150     }
3151 
3152     src_flags = 0;
3153     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
3154     if (ret < 0) {
3155         error_report("Invalid source cache option: %s", src_cache);
3156         goto out;
3157     }
3158 
3159     /* The source files are opened read-only, don't care about WCE */
3160     assert((src_flags & BDRV_O_RDWR) == 0);
3161     (void) src_writethrough;
3162 
3163     /*
3164      * Open the images.
3165      *
3166      * Ignore the old backing file for unsafe rebase in case we want to correct
3167      * the reference to a renamed or moved backing file.
3168      */
3169     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3170                    false);
3171     if (!blk) {
3172         ret = -1;
3173         goto out;
3174     }
3175     bs = blk_bs(blk);
3176 
3177     if (out_basefmt != NULL) {
3178         if (bdrv_find_format(out_basefmt) == NULL) {
3179             error_report("Invalid format name: '%s'", out_basefmt);
3180             ret = -1;
3181             goto out;
3182         }
3183     }
3184 
3185     /* For safe rebasing we need to compare old and new backing file */
3186     if (!unsafe) {
3187         char backing_name[PATH_MAX];
3188         QDict *options = NULL;
3189 
3190         if (bs->backing_format[0] != '\0') {
3191             options = qdict_new();
3192             qdict_put_str(options, "driver", bs->backing_format);
3193         }
3194 
3195         if (force_share) {
3196             if (!options) {
3197                 options = qdict_new();
3198             }
3199             qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3200         }
3201         bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3202         blk_old_backing = blk_new_open(backing_name, NULL,
3203                                        options, src_flags, &local_err);
3204         if (!blk_old_backing) {
3205             error_reportf_err(local_err,
3206                               "Could not open old backing file '%s': ",
3207                               backing_name);
3208             ret = -1;
3209             goto out;
3210         }
3211 
3212         if (out_baseimg[0]) {
3213             options = qdict_new();
3214             if (out_basefmt) {
3215                 qdict_put_str(options, "driver", out_basefmt);
3216             }
3217             if (force_share) {
3218                 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3219             }
3220 
3221             blk_new_backing = blk_new_open(out_baseimg, NULL,
3222                                            options, src_flags, &local_err);
3223             if (!blk_new_backing) {
3224                 error_reportf_err(local_err,
3225                                   "Could not open new backing file '%s': ",
3226                                   out_baseimg);
3227                 ret = -1;
3228                 goto out;
3229             }
3230         }
3231     }
3232 
3233     /*
3234      * Check each unallocated cluster in the COW file. If it is unallocated,
3235      * accesses go to the backing file. We must therefore compare this cluster
3236      * in the old and new backing file, and if they differ we need to copy it
3237      * from the old backing file into the COW file.
3238      *
3239      * If qemu-img crashes during this step, no harm is done. The content of
3240      * the image is the same as the original one at any time.
3241      */
3242     if (!unsafe) {
3243         int64_t num_sectors;
3244         int64_t old_backing_num_sectors;
3245         int64_t new_backing_num_sectors = 0;
3246         uint64_t sector;
3247         int n;
3248         int64_t count;
3249         float local_progress = 0;
3250 
3251         buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3252         buf_new = blk_blockalign(blk, IO_BUF_SIZE);
3253 
3254         num_sectors = blk_nb_sectors(blk);
3255         if (num_sectors < 0) {
3256             error_report("Could not get size of '%s': %s",
3257                          filename, strerror(-num_sectors));
3258             ret = -1;
3259             goto out;
3260         }
3261         old_backing_num_sectors = blk_nb_sectors(blk_old_backing);
3262         if (old_backing_num_sectors < 0) {
3263             char backing_name[PATH_MAX];
3264 
3265             bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3266             error_report("Could not get size of '%s': %s",
3267                          backing_name, strerror(-old_backing_num_sectors));
3268             ret = -1;
3269             goto out;
3270         }
3271         if (blk_new_backing) {
3272             new_backing_num_sectors = blk_nb_sectors(blk_new_backing);
3273             if (new_backing_num_sectors < 0) {
3274                 error_report("Could not get size of '%s': %s",
3275                              out_baseimg, strerror(-new_backing_num_sectors));
3276                 ret = -1;
3277                 goto out;
3278             }
3279         }
3280 
3281         if (num_sectors != 0) {
3282             local_progress = (float)100 /
3283                 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
3284         }
3285 
3286         for (sector = 0; sector < num_sectors; sector += n) {
3287 
3288             /* How many sectors can we handle with the next read? */
3289             if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
3290                 n = (IO_BUF_SIZE / 512);
3291             } else {
3292                 n = num_sectors - sector;
3293             }
3294 
3295             /* If the cluster is allocated, we don't need to take action */
3296             ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS,
3297                                     n << BDRV_SECTOR_BITS, &count);
3298             if (ret < 0) {
3299                 error_report("error while reading image metadata: %s",
3300                              strerror(-ret));
3301                 goto out;
3302             }
3303             /* TODO relax this once bdrv_is_allocated does not enforce
3304              * sector alignment */
3305             assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE));
3306             n = count >> BDRV_SECTOR_BITS;
3307             if (ret) {
3308                 continue;
3309             }
3310 
3311             /*
3312              * Read old and new backing file and take into consideration that
3313              * backing files may be smaller than the COW image.
3314              */
3315             if (sector >= old_backing_num_sectors) {
3316                 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
3317             } else {
3318                 if (sector + n > old_backing_num_sectors) {
3319                     n = old_backing_num_sectors - sector;
3320                 }
3321 
3322                 ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS,
3323                                 buf_old, n << BDRV_SECTOR_BITS);
3324                 if (ret < 0) {
3325                     error_report("error while reading from old backing file");
3326                     goto out;
3327                 }
3328             }
3329 
3330             if (sector >= new_backing_num_sectors || !blk_new_backing) {
3331                 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
3332             } else {
3333                 if (sector + n > new_backing_num_sectors) {
3334                     n = new_backing_num_sectors - sector;
3335                 }
3336 
3337                 ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS,
3338                                 buf_new, n << BDRV_SECTOR_BITS);
3339                 if (ret < 0) {
3340                     error_report("error while reading from new backing file");
3341                     goto out;
3342                 }
3343             }
3344 
3345             /* If they differ, we need to write to the COW file */
3346             uint64_t written = 0;
3347 
3348             while (written < n) {
3349                 int pnum;
3350 
3351                 if (compare_sectors(buf_old + written * 512,
3352                     buf_new + written * 512, n - written, &pnum))
3353                 {
3354                     ret = blk_pwrite(blk,
3355                                      (sector + written) << BDRV_SECTOR_BITS,
3356                                      buf_old + written * 512,
3357                                      pnum << BDRV_SECTOR_BITS, 0);
3358                     if (ret < 0) {
3359                         error_report("Error while writing to COW image: %s",
3360                             strerror(-ret));
3361                         goto out;
3362                     }
3363                 }
3364 
3365                 written += pnum;
3366             }
3367             qemu_progress_print(local_progress, 100);
3368         }
3369     }
3370 
3371     /*
3372      * Change the backing file. All clusters that are different from the old
3373      * backing file are overwritten in the COW file now, so the visible content
3374      * doesn't change when we switch the backing file.
3375      */
3376     if (out_baseimg && *out_baseimg) {
3377         ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3378     } else {
3379         ret = bdrv_change_backing_file(bs, NULL, NULL);
3380     }
3381 
3382     if (ret == -ENOSPC) {
3383         error_report("Could not change the backing file to '%s': No "
3384                      "space left in the file header", out_baseimg);
3385     } else if (ret < 0) {
3386         error_report("Could not change the backing file to '%s': %s",
3387             out_baseimg, strerror(-ret));
3388     }
3389 
3390     qemu_progress_print(100, 0);
3391     /*
3392      * TODO At this point it is possible to check if any clusters that are
3393      * allocated in the COW file are the same in the backing file. If so, they
3394      * could be dropped from the COW file. Don't do this before switching the
3395      * backing file, in case of a crash this would lead to corruption.
3396      */
3397 out:
3398     qemu_progress_end();
3399     /* Cleanup */
3400     if (!unsafe) {
3401         blk_unref(blk_old_backing);
3402         blk_unref(blk_new_backing);
3403     }
3404     qemu_vfree(buf_old);
3405     qemu_vfree(buf_new);
3406 
3407     blk_unref(blk);
3408     if (ret) {
3409         return 1;
3410     }
3411     return 0;
3412 }
3413 
3414 static int img_resize(int argc, char **argv)
3415 {
3416     Error *err = NULL;
3417     int c, ret, relative;
3418     const char *filename, *fmt, *size;
3419     int64_t n, total_size, current_size;
3420     bool quiet = false;
3421     BlockBackend *blk = NULL;
3422     PreallocMode prealloc = PREALLOC_MODE_OFF;
3423     QemuOpts *param;
3424 
3425     static QemuOptsList resize_options = {
3426         .name = "resize_options",
3427         .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3428         .desc = {
3429             {
3430                 .name = BLOCK_OPT_SIZE,
3431                 .type = QEMU_OPT_SIZE,
3432                 .help = "Virtual disk size"
3433             }, {
3434                 /* end of list */
3435             }
3436         },
3437     };
3438     bool image_opts = false;
3439 
3440     /* Remove size from argv manually so that negative numbers are not treated
3441      * as options by getopt. */
3442     if (argc < 3) {
3443         error_exit("Not enough arguments");
3444         return 1;
3445     }
3446 
3447     size = argv[--argc];
3448 
3449     /* Parse getopt arguments */
3450     fmt = NULL;
3451     for(;;) {
3452         static const struct option long_options[] = {
3453             {"help", no_argument, 0, 'h'},
3454             {"object", required_argument, 0, OPTION_OBJECT},
3455             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3456             {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
3457             {0, 0, 0, 0}
3458         };
3459         c = getopt_long(argc, argv, ":f:hq",
3460                         long_options, NULL);
3461         if (c == -1) {
3462             break;
3463         }
3464         switch(c) {
3465         case ':':
3466             missing_argument(argv[optind - 1]);
3467             break;
3468         case '?':
3469             unrecognized_option(argv[optind - 1]);
3470             break;
3471         case 'h':
3472             help();
3473             break;
3474         case 'f':
3475             fmt = optarg;
3476             break;
3477         case 'q':
3478             quiet = true;
3479             break;
3480         case OPTION_OBJECT: {
3481             QemuOpts *opts;
3482             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3483                                            optarg, true);
3484             if (!opts) {
3485                 return 1;
3486             }
3487         }   break;
3488         case OPTION_IMAGE_OPTS:
3489             image_opts = true;
3490             break;
3491         case OPTION_PREALLOCATION:
3492             prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
3493                                        PREALLOC_MODE__MAX, NULL);
3494             if (prealloc == PREALLOC_MODE__MAX) {
3495                 error_report("Invalid preallocation mode '%s'", optarg);
3496                 return 1;
3497             }
3498             break;
3499         }
3500     }
3501     if (optind != argc - 1) {
3502         error_exit("Expecting one image file name");
3503     }
3504     filename = argv[optind++];
3505 
3506     if (qemu_opts_foreach(&qemu_object_opts,
3507                           user_creatable_add_opts_foreach,
3508                           NULL, NULL)) {
3509         return 1;
3510     }
3511 
3512     /* Choose grow, shrink, or absolute resize mode */
3513     switch (size[0]) {
3514     case '+':
3515         relative = 1;
3516         size++;
3517         break;
3518     case '-':
3519         relative = -1;
3520         size++;
3521         break;
3522     default:
3523         relative = 0;
3524         break;
3525     }
3526 
3527     /* Parse size */
3528     param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
3529     qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
3530     if (err) {
3531         error_report_err(err);
3532         ret = -1;
3533         qemu_opts_del(param);
3534         goto out;
3535     }
3536     n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3537     qemu_opts_del(param);
3538 
3539     blk = img_open(image_opts, filename, fmt,
3540                    BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3541                    false);
3542     if (!blk) {
3543         ret = -1;
3544         goto out;
3545     }
3546 
3547     current_size = blk_getlength(blk);
3548     if (current_size < 0) {
3549         error_report("Failed to inquire current image length: %s",
3550                      strerror(-current_size));
3551         ret = -1;
3552         goto out;
3553     }
3554 
3555     if (relative) {
3556         total_size = current_size + n * relative;
3557     } else {
3558         total_size = n;
3559     }
3560     if (total_size <= 0) {
3561         error_report("New image size must be positive");
3562         ret = -1;
3563         goto out;
3564     }
3565 
3566     if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3567         error_report("Preallocation can only be used for growing images");
3568         ret = -1;
3569         goto out;
3570     }
3571 
3572     ret = blk_truncate(blk, total_size, prealloc, &err);
3573     if (!ret) {
3574         qprintf(quiet, "Image resized.\n");
3575     } else {
3576         error_report_err(err);
3577     }
3578 out:
3579     blk_unref(blk);
3580     if (ret) {
3581         return 1;
3582     }
3583     return 0;
3584 }
3585 
3586 static void amend_status_cb(BlockDriverState *bs,
3587                             int64_t offset, int64_t total_work_size,
3588                             void *opaque)
3589 {
3590     qemu_progress_print(100.f * offset / total_work_size, 0);
3591 }
3592 
3593 static int img_amend(int argc, char **argv)
3594 {
3595     Error *err = NULL;
3596     int c, ret = 0;
3597     char *options = NULL;
3598     QemuOptsList *create_opts = NULL;
3599     QemuOpts *opts = NULL;
3600     const char *fmt = NULL, *filename, *cache;
3601     int flags;
3602     bool writethrough;
3603     bool quiet = false, progress = false;
3604     BlockBackend *blk = NULL;
3605     BlockDriverState *bs = NULL;
3606     bool image_opts = false;
3607 
3608     cache = BDRV_DEFAULT_CACHE;
3609     for (;;) {
3610         static const struct option long_options[] = {
3611             {"help", no_argument, 0, 'h'},
3612             {"object", required_argument, 0, OPTION_OBJECT},
3613             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3614             {0, 0, 0, 0}
3615         };
3616         c = getopt_long(argc, argv, ":ho:f:t:pq",
3617                         long_options, NULL);
3618         if (c == -1) {
3619             break;
3620         }
3621 
3622         switch (c) {
3623         case ':':
3624             missing_argument(argv[optind - 1]);
3625             break;
3626         case '?':
3627             unrecognized_option(argv[optind - 1]);
3628             break;
3629         case 'h':
3630             help();
3631             break;
3632         case 'o':
3633             if (!is_valid_option_list(optarg)) {
3634                 error_report("Invalid option list: %s", optarg);
3635                 ret = -1;
3636                 goto out_no_progress;
3637             }
3638             if (!options) {
3639                 options = g_strdup(optarg);
3640             } else {
3641                 char *old_options = options;
3642                 options = g_strdup_printf("%s,%s", options, optarg);
3643                 g_free(old_options);
3644             }
3645             break;
3646         case 'f':
3647             fmt = optarg;
3648             break;
3649         case 't':
3650             cache = optarg;
3651             break;
3652         case 'p':
3653             progress = true;
3654             break;
3655         case 'q':
3656             quiet = true;
3657             break;
3658         case OPTION_OBJECT:
3659             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3660                                            optarg, true);
3661             if (!opts) {
3662                 ret = -1;
3663                 goto out_no_progress;
3664             }
3665             break;
3666         case OPTION_IMAGE_OPTS:
3667             image_opts = true;
3668             break;
3669         }
3670     }
3671 
3672     if (!options) {
3673         error_exit("Must specify options (-o)");
3674     }
3675 
3676     if (qemu_opts_foreach(&qemu_object_opts,
3677                           user_creatable_add_opts_foreach,
3678                           NULL, NULL)) {
3679         ret = -1;
3680         goto out_no_progress;
3681     }
3682 
3683     if (quiet) {
3684         progress = false;
3685     }
3686     qemu_progress_init(progress, 1.0);
3687 
3688     filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3689     if (fmt && has_help_option(options)) {
3690         /* If a format is explicitly specified (and possibly no filename is
3691          * given), print option help here */
3692         ret = print_block_option_help(filename, fmt);
3693         goto out;
3694     }
3695 
3696     if (optind != argc - 1) {
3697         error_report("Expecting one image file name");
3698         ret = -1;
3699         goto out;
3700     }
3701 
3702     flags = BDRV_O_RDWR;
3703     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3704     if (ret < 0) {
3705         error_report("Invalid cache option: %s", cache);
3706         goto out;
3707     }
3708 
3709     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3710                    false);
3711     if (!blk) {
3712         ret = -1;
3713         goto out;
3714     }
3715     bs = blk_bs(blk);
3716 
3717     fmt = bs->drv->format_name;
3718 
3719     if (has_help_option(options)) {
3720         /* If the format was auto-detected, print option help here */
3721         ret = print_block_option_help(filename, fmt);
3722         goto out;
3723     }
3724 
3725     if (!bs->drv->create_opts) {
3726         error_report("Format driver '%s' does not support any options to amend",
3727                      fmt);
3728         ret = -1;
3729         goto out;
3730     }
3731 
3732     create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
3733     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
3734     qemu_opts_do_parse(opts, options, NULL, &err);
3735     if (err) {
3736         error_report_err(err);
3737         ret = -1;
3738         goto out;
3739     }
3740 
3741     /* In case the driver does not call amend_status_cb() */
3742     qemu_progress_print(0.f, 0);
3743     ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL);
3744     qemu_progress_print(100.f, 0);
3745     if (ret < 0) {
3746         error_report("Error while amending options: %s", strerror(-ret));
3747         goto out;
3748     }
3749 
3750 out:
3751     qemu_progress_end();
3752 
3753 out_no_progress:
3754     blk_unref(blk);
3755     qemu_opts_del(opts);
3756     qemu_opts_free(create_opts);
3757     g_free(options);
3758 
3759     if (ret) {
3760         return 1;
3761     }
3762     return 0;
3763 }
3764 
3765 typedef struct BenchData {
3766     BlockBackend *blk;
3767     uint64_t image_size;
3768     bool write;
3769     int bufsize;
3770     int step;
3771     int nrreq;
3772     int n;
3773     int flush_interval;
3774     bool drain_on_flush;
3775     uint8_t *buf;
3776     QEMUIOVector *qiov;
3777 
3778     int in_flight;
3779     bool in_flush;
3780     uint64_t offset;
3781 } BenchData;
3782 
3783 static void bench_undrained_flush_cb(void *opaque, int ret)
3784 {
3785     if (ret < 0) {
3786         error_report("Failed flush request: %s", strerror(-ret));
3787         exit(EXIT_FAILURE);
3788     }
3789 }
3790 
3791 static void bench_cb(void *opaque, int ret)
3792 {
3793     BenchData *b = opaque;
3794     BlockAIOCB *acb;
3795 
3796     if (ret < 0) {
3797         error_report("Failed request: %s", strerror(-ret));
3798         exit(EXIT_FAILURE);
3799     }
3800 
3801     if (b->in_flush) {
3802         /* Just finished a flush with drained queue: Start next requests */
3803         assert(b->in_flight == 0);
3804         b->in_flush = false;
3805     } else if (b->in_flight > 0) {
3806         int remaining = b->n - b->in_flight;
3807 
3808         b->n--;
3809         b->in_flight--;
3810 
3811         /* Time for flush? Drain queue if requested, then flush */
3812         if (b->flush_interval && remaining % b->flush_interval == 0) {
3813             if (!b->in_flight || !b->drain_on_flush) {
3814                 BlockCompletionFunc *cb;
3815 
3816                 if (b->drain_on_flush) {
3817                     b->in_flush = true;
3818                     cb = bench_cb;
3819                 } else {
3820                     cb = bench_undrained_flush_cb;
3821                 }
3822 
3823                 acb = blk_aio_flush(b->blk, cb, b);
3824                 if (!acb) {
3825                     error_report("Failed to issue flush request");
3826                     exit(EXIT_FAILURE);
3827                 }
3828             }
3829             if (b->drain_on_flush) {
3830                 return;
3831             }
3832         }
3833     }
3834 
3835     while (b->n > b->in_flight && b->in_flight < b->nrreq) {
3836         int64_t offset = b->offset;
3837         /* blk_aio_* might look for completed I/Os and kick bench_cb
3838          * again, so make sure this operation is counted by in_flight
3839          * and b->offset is ready for the next submission.
3840          */
3841         b->in_flight++;
3842         b->offset += b->step;
3843         b->offset %= b->image_size;
3844         if (b->write) {
3845             acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
3846         } else {
3847             acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
3848         }
3849         if (!acb) {
3850             error_report("Failed to issue request");
3851             exit(EXIT_FAILURE);
3852         }
3853     }
3854 }
3855 
3856 static int img_bench(int argc, char **argv)
3857 {
3858     int c, ret = 0;
3859     const char *fmt = NULL, *filename;
3860     bool quiet = false;
3861     bool image_opts = false;
3862     bool is_write = false;
3863     int count = 75000;
3864     int depth = 64;
3865     int64_t offset = 0;
3866     size_t bufsize = 4096;
3867     int pattern = 0;
3868     size_t step = 0;
3869     int flush_interval = 0;
3870     bool drain_on_flush = true;
3871     int64_t image_size;
3872     BlockBackend *blk = NULL;
3873     BenchData data = {};
3874     int flags = 0;
3875     bool writethrough = false;
3876     struct timeval t1, t2;
3877     int i;
3878     bool force_share = false;
3879 
3880     for (;;) {
3881         static const struct option long_options[] = {
3882             {"help", no_argument, 0, 'h'},
3883             {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
3884             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3885             {"pattern", required_argument, 0, OPTION_PATTERN},
3886             {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
3887             {"force-share", no_argument, 0, 'U'},
3888             {0, 0, 0, 0}
3889         };
3890         c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
3891         if (c == -1) {
3892             break;
3893         }
3894 
3895         switch (c) {
3896         case ':':
3897             missing_argument(argv[optind - 1]);
3898             break;
3899         case '?':
3900             unrecognized_option(argv[optind - 1]);
3901             break;
3902         case 'h':
3903             help();
3904             break;
3905         case 'c':
3906         {
3907             unsigned long res;
3908 
3909             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3910                 error_report("Invalid request count specified");
3911                 return 1;
3912             }
3913             count = res;
3914             break;
3915         }
3916         case 'd':
3917         {
3918             unsigned long res;
3919 
3920             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
3921                 error_report("Invalid queue depth specified");
3922                 return 1;
3923             }
3924             depth = res;
3925             break;
3926         }
3927         case 'f':
3928             fmt = optarg;
3929             break;
3930         case 'n':
3931             flags |= BDRV_O_NATIVE_AIO;
3932             break;
3933         case 'o':
3934         {
3935             offset = cvtnum(optarg);
3936             if (offset < 0) {
3937                 error_report("Invalid offset specified");
3938                 return 1;
3939             }
3940             break;
3941         }
3942             break;
3943         case 'q':
3944             quiet = true;
3945             break;
3946         case 's':
3947         {
3948             int64_t sval;
3949 
3950             sval = cvtnum(optarg);
3951             if (sval < 0 || sval > INT_MAX) {
3952                 error_report("Invalid buffer size specified");
3953                 return 1;
3954             }
3955 
3956             bufsize = sval;
3957             break;
3958         }
3959         case 'S':
3960         {
3961             int64_t sval;
3962 
3963             sval = cvtnum(optarg);
3964             if (sval < 0 || sval > INT_MAX) {
3965                 error_report("Invalid step size specified");
3966                 return 1;
3967             }
3968 
3969             step = sval;
3970             break;
3971         }
3972         case 't':
3973             ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
3974             if (ret < 0) {
3975                 error_report("Invalid cache mode");
3976                 ret = -1;
3977                 goto out;
3978             }
3979             break;
3980         case 'w':
3981             flags |= BDRV_O_RDWR;
3982             is_write = true;
3983             break;
3984         case 'U':
3985             force_share = true;
3986             break;
3987         case OPTION_PATTERN:
3988         {
3989             unsigned long res;
3990 
3991             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
3992                 error_report("Invalid pattern byte specified");
3993                 return 1;
3994             }
3995             pattern = res;
3996             break;
3997         }
3998         case OPTION_FLUSH_INTERVAL:
3999         {
4000             unsigned long res;
4001 
4002             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4003                 error_report("Invalid flush interval specified");
4004                 return 1;
4005             }
4006             flush_interval = res;
4007             break;
4008         }
4009         case OPTION_NO_DRAIN:
4010             drain_on_flush = false;
4011             break;
4012         case OPTION_IMAGE_OPTS:
4013             image_opts = true;
4014             break;
4015         }
4016     }
4017 
4018     if (optind != argc - 1) {
4019         error_exit("Expecting one image file name");
4020     }
4021     filename = argv[argc - 1];
4022 
4023     if (!is_write && flush_interval) {
4024         error_report("--flush-interval is only available in write tests");
4025         ret = -1;
4026         goto out;
4027     }
4028     if (flush_interval && flush_interval < depth) {
4029         error_report("Flush interval can't be smaller than depth");
4030         ret = -1;
4031         goto out;
4032     }
4033 
4034     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4035                    force_share);
4036     if (!blk) {
4037         ret = -1;
4038         goto out;
4039     }
4040 
4041     image_size = blk_getlength(blk);
4042     if (image_size < 0) {
4043         ret = image_size;
4044         goto out;
4045     }
4046 
4047     data = (BenchData) {
4048         .blk            = blk,
4049         .image_size     = image_size,
4050         .bufsize        = bufsize,
4051         .step           = step ?: bufsize,
4052         .nrreq          = depth,
4053         .n              = count,
4054         .offset         = offset,
4055         .write          = is_write,
4056         .flush_interval = flush_interval,
4057         .drain_on_flush = drain_on_flush,
4058     };
4059     printf("Sending %d %s requests, %d bytes each, %d in parallel "
4060            "(starting at offset %" PRId64 ", step size %d)\n",
4061            data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
4062            data.offset, data.step);
4063     if (flush_interval) {
4064         printf("Sending flush every %d requests\n", flush_interval);
4065     }
4066 
4067     data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
4068     memset(data.buf, pattern, data.nrreq * data.bufsize);
4069 
4070     data.qiov = g_new(QEMUIOVector, data.nrreq);
4071     for (i = 0; i < data.nrreq; i++) {
4072         qemu_iovec_init(&data.qiov[i], 1);
4073         qemu_iovec_add(&data.qiov[i],
4074                        data.buf + i * data.bufsize, data.bufsize);
4075     }
4076 
4077     gettimeofday(&t1, NULL);
4078     bench_cb(&data, 0);
4079 
4080     while (data.n > 0) {
4081         main_loop_wait(false);
4082     }
4083     gettimeofday(&t2, NULL);
4084 
4085     printf("Run completed in %3.3f seconds.\n",
4086            (t2.tv_sec - t1.tv_sec)
4087            + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4088 
4089 out:
4090     qemu_vfree(data.buf);
4091     blk_unref(blk);
4092 
4093     if (ret) {
4094         return 1;
4095     }
4096     return 0;
4097 }
4098 
4099 #define C_BS      01
4100 #define C_COUNT   02
4101 #define C_IF      04
4102 #define C_OF      010
4103 #define C_SKIP    020
4104 
4105 struct DdInfo {
4106     unsigned int flags;
4107     int64_t count;
4108 };
4109 
4110 struct DdIo {
4111     int bsz;    /* Block size */
4112     char *filename;
4113     uint8_t *buf;
4114     int64_t offset;
4115 };
4116 
4117 struct DdOpts {
4118     const char *name;
4119     int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4120     unsigned int flag;
4121 };
4122 
4123 static int img_dd_bs(const char *arg,
4124                      struct DdIo *in, struct DdIo *out,
4125                      struct DdInfo *dd)
4126 {
4127     int64_t res;
4128 
4129     res = cvtnum(arg);
4130 
4131     if (res <= 0 || res > INT_MAX) {
4132         error_report("invalid number: '%s'", arg);
4133         return 1;
4134     }
4135     in->bsz = out->bsz = res;
4136 
4137     return 0;
4138 }
4139 
4140 static int img_dd_count(const char *arg,
4141                         struct DdIo *in, struct DdIo *out,
4142                         struct DdInfo *dd)
4143 {
4144     dd->count = cvtnum(arg);
4145 
4146     if (dd->count < 0) {
4147         error_report("invalid number: '%s'", arg);
4148         return 1;
4149     }
4150 
4151     return 0;
4152 }
4153 
4154 static int img_dd_if(const char *arg,
4155                      struct DdIo *in, struct DdIo *out,
4156                      struct DdInfo *dd)
4157 {
4158     in->filename = g_strdup(arg);
4159 
4160     return 0;
4161 }
4162 
4163 static int img_dd_of(const char *arg,
4164                      struct DdIo *in, struct DdIo *out,
4165                      struct DdInfo *dd)
4166 {
4167     out->filename = g_strdup(arg);
4168 
4169     return 0;
4170 }
4171 
4172 static int img_dd_skip(const char *arg,
4173                        struct DdIo *in, struct DdIo *out,
4174                        struct DdInfo *dd)
4175 {
4176     in->offset = cvtnum(arg);
4177 
4178     if (in->offset < 0) {
4179         error_report("invalid number: '%s'", arg);
4180         return 1;
4181     }
4182 
4183     return 0;
4184 }
4185 
4186 static int img_dd(int argc, char **argv)
4187 {
4188     int ret = 0;
4189     char *arg = NULL;
4190     char *tmp;
4191     BlockDriver *drv = NULL, *proto_drv = NULL;
4192     BlockBackend *blk1 = NULL, *blk2 = NULL;
4193     QemuOpts *opts = NULL;
4194     QemuOptsList *create_opts = NULL;
4195     Error *local_err = NULL;
4196     bool image_opts = false;
4197     int c, i;
4198     const char *out_fmt = "raw";
4199     const char *fmt = NULL;
4200     int64_t size = 0;
4201     int64_t block_count = 0, out_pos, in_pos;
4202     bool force_share = false;
4203     struct DdInfo dd = {
4204         .flags = 0,
4205         .count = 0,
4206     };
4207     struct DdIo in = {
4208         .bsz = 512, /* Block size is by default 512 bytes */
4209         .filename = NULL,
4210         .buf = NULL,
4211         .offset = 0
4212     };
4213     struct DdIo out = {
4214         .bsz = 512,
4215         .filename = NULL,
4216         .buf = NULL,
4217         .offset = 0
4218     };
4219 
4220     const struct DdOpts options[] = {
4221         { "bs", img_dd_bs, C_BS },
4222         { "count", img_dd_count, C_COUNT },
4223         { "if", img_dd_if, C_IF },
4224         { "of", img_dd_of, C_OF },
4225         { "skip", img_dd_skip, C_SKIP },
4226         { NULL, NULL, 0 }
4227     };
4228     const struct option long_options[] = {
4229         { "help", no_argument, 0, 'h'},
4230         { "object", required_argument, 0, OPTION_OBJECT},
4231         { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4232         { "force-share", no_argument, 0, 'U'},
4233         { 0, 0, 0, 0 }
4234     };
4235 
4236     while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
4237         if (c == EOF) {
4238             break;
4239         }
4240         switch (c) {
4241         case 'O':
4242             out_fmt = optarg;
4243             break;
4244         case 'f':
4245             fmt = optarg;
4246             break;
4247         case ':':
4248             missing_argument(argv[optind - 1]);
4249             break;
4250         case '?':
4251             unrecognized_option(argv[optind - 1]);
4252             break;
4253         case 'h':
4254             help();
4255             break;
4256         case 'U':
4257             force_share = true;
4258             break;
4259         case OPTION_OBJECT:
4260             if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
4261                 ret = -1;
4262                 goto out;
4263             }
4264             break;
4265         case OPTION_IMAGE_OPTS:
4266             image_opts = true;
4267             break;
4268         }
4269     }
4270 
4271     for (i = optind; i < argc; i++) {
4272         int j;
4273         arg = g_strdup(argv[i]);
4274 
4275         tmp = strchr(arg, '=');
4276         if (tmp == NULL) {
4277             error_report("unrecognized operand %s", arg);
4278             ret = -1;
4279             goto out;
4280         }
4281 
4282         *tmp++ = '\0';
4283 
4284         for (j = 0; options[j].name != NULL; j++) {
4285             if (!strcmp(arg, options[j].name)) {
4286                 break;
4287             }
4288         }
4289         if (options[j].name == NULL) {
4290             error_report("unrecognized operand %s", arg);
4291             ret = -1;
4292             goto out;
4293         }
4294 
4295         if (options[j].f(tmp, &in, &out, &dd) != 0) {
4296             ret = -1;
4297             goto out;
4298         }
4299         dd.flags |= options[j].flag;
4300         g_free(arg);
4301         arg = NULL;
4302     }
4303 
4304     if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4305         error_report("Must specify both input and output files");
4306         ret = -1;
4307         goto out;
4308     }
4309 
4310     if (qemu_opts_foreach(&qemu_object_opts,
4311                           user_creatable_add_opts_foreach,
4312                           NULL, NULL)) {
4313         ret = -1;
4314         goto out;
4315     }
4316 
4317     blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4318                     force_share);
4319 
4320     if (!blk1) {
4321         ret = -1;
4322         goto out;
4323     }
4324 
4325     drv = bdrv_find_format(out_fmt);
4326     if (!drv) {
4327         error_report("Unknown file format");
4328         ret = -1;
4329         goto out;
4330     }
4331     proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4332 
4333     if (!proto_drv) {
4334         error_report_err(local_err);
4335         ret = -1;
4336         goto out;
4337     }
4338     if (!drv->create_opts) {
4339         error_report("Format driver '%s' does not support image creation",
4340                      drv->format_name);
4341         ret = -1;
4342         goto out;
4343     }
4344     if (!proto_drv->create_opts) {
4345         error_report("Protocol driver '%s' does not support image creation",
4346                      proto_drv->format_name);
4347         ret = -1;
4348         goto out;
4349     }
4350     create_opts = qemu_opts_append(create_opts, drv->create_opts);
4351     create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4352 
4353     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4354 
4355     size = blk_getlength(blk1);
4356     if (size < 0) {
4357         error_report("Failed to get size for '%s'", in.filename);
4358         ret = -1;
4359         goto out;
4360     }
4361 
4362     if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4363         dd.count * in.bsz < size) {
4364         size = dd.count * in.bsz;
4365     }
4366 
4367     /* Overflow means the specified offset is beyond input image's size */
4368     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4369                               size < in.bsz * in.offset)) {
4370         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4371     } else {
4372         qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4373                             size - in.bsz * in.offset, &error_abort);
4374     }
4375 
4376     ret = bdrv_create(drv, out.filename, opts, &local_err);
4377     if (ret < 0) {
4378         error_reportf_err(local_err,
4379                           "%s: error while creating output image: ",
4380                           out.filename);
4381         ret = -1;
4382         goto out;
4383     }
4384 
4385     /* TODO, we can't honour --image-opts for the target,
4386      * since it needs to be given in a format compatible
4387      * with the bdrv_create() call above which does not
4388      * support image-opts style.
4389      */
4390     blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
4391                          false, false, false);
4392 
4393     if (!blk2) {
4394         ret = -1;
4395         goto out;
4396     }
4397 
4398     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4399                               size < in.offset * in.bsz)) {
4400         /* We give a warning if the skip option is bigger than the input
4401          * size and create an empty output disk image (i.e. like dd(1)).
4402          */
4403         error_report("%s: cannot skip to specified offset", in.filename);
4404         in_pos = size;
4405     } else {
4406         in_pos = in.offset * in.bsz;
4407     }
4408 
4409     in.buf = g_new(uint8_t, in.bsz);
4410 
4411     for (out_pos = 0; in_pos < size; block_count++) {
4412         int in_ret, out_ret;
4413 
4414         if (in_pos + in.bsz > size) {
4415             in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4416         } else {
4417             in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4418         }
4419         if (in_ret < 0) {
4420             error_report("error while reading from input image file: %s",
4421                          strerror(-in_ret));
4422             ret = -1;
4423             goto out;
4424         }
4425         in_pos += in_ret;
4426 
4427         out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4428 
4429         if (out_ret < 0) {
4430             error_report("error while writing to output image file: %s",
4431                          strerror(-out_ret));
4432             ret = -1;
4433             goto out;
4434         }
4435         out_pos += out_ret;
4436     }
4437 
4438 out:
4439     g_free(arg);
4440     qemu_opts_del(opts);
4441     qemu_opts_free(create_opts);
4442     blk_unref(blk1);
4443     blk_unref(blk2);
4444     g_free(in.filename);
4445     g_free(out.filename);
4446     g_free(in.buf);
4447     g_free(out.buf);
4448 
4449     if (ret) {
4450         return 1;
4451     }
4452     return 0;
4453 }
4454 
4455 static void dump_json_block_measure_info(BlockMeasureInfo *info)
4456 {
4457     QString *str;
4458     QObject *obj;
4459     Visitor *v = qobject_output_visitor_new(&obj);
4460 
4461     visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4462     visit_complete(v, &obj);
4463     str = qobject_to_json_pretty(obj);
4464     assert(str != NULL);
4465     printf("%s\n", qstring_get_str(str));
4466     qobject_decref(obj);
4467     visit_free(v);
4468     QDECREF(str);
4469 }
4470 
4471 static int img_measure(int argc, char **argv)
4472 {
4473     static const struct option long_options[] = {
4474         {"help", no_argument, 0, 'h'},
4475         {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4476         {"object", required_argument, 0, OPTION_OBJECT},
4477         {"output", required_argument, 0, OPTION_OUTPUT},
4478         {"size", required_argument, 0, OPTION_SIZE},
4479         {"force-share", no_argument, 0, 'U'},
4480         {0, 0, 0, 0}
4481     };
4482     OutputFormat output_format = OFORMAT_HUMAN;
4483     BlockBackend *in_blk = NULL;
4484     BlockDriver *drv;
4485     const char *filename = NULL;
4486     const char *fmt = NULL;
4487     const char *out_fmt = "raw";
4488     char *options = NULL;
4489     char *snapshot_name = NULL;
4490     bool force_share = false;
4491     QemuOpts *opts = NULL;
4492     QemuOpts *object_opts = NULL;
4493     QemuOpts *sn_opts = NULL;
4494     QemuOptsList *create_opts = NULL;
4495     bool image_opts = false;
4496     uint64_t img_size = UINT64_MAX;
4497     BlockMeasureInfo *info = NULL;
4498     Error *local_err = NULL;
4499     int ret = 1;
4500     int c;
4501 
4502     while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4503                             long_options, NULL)) != -1) {
4504         switch (c) {
4505         case '?':
4506         case 'h':
4507             help();
4508             break;
4509         case 'f':
4510             fmt = optarg;
4511             break;
4512         case 'O':
4513             out_fmt = optarg;
4514             break;
4515         case 'o':
4516             if (!is_valid_option_list(optarg)) {
4517                 error_report("Invalid option list: %s", optarg);
4518                 goto out;
4519             }
4520             if (!options) {
4521                 options = g_strdup(optarg);
4522             } else {
4523                 char *old_options = options;
4524                 options = g_strdup_printf("%s,%s", options, optarg);
4525                 g_free(old_options);
4526             }
4527             break;
4528         case 'l':
4529             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4530                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4531                                                   optarg, false);
4532                 if (!sn_opts) {
4533                     error_report("Failed in parsing snapshot param '%s'",
4534                                  optarg);
4535                     goto out;
4536                 }
4537             } else {
4538                 snapshot_name = optarg;
4539             }
4540             break;
4541         case 'U':
4542             force_share = true;
4543             break;
4544         case OPTION_OBJECT:
4545             object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4546                                                   optarg, true);
4547             if (!object_opts) {
4548                 goto out;
4549             }
4550             break;
4551         case OPTION_IMAGE_OPTS:
4552             image_opts = true;
4553             break;
4554         case OPTION_OUTPUT:
4555             if (!strcmp(optarg, "json")) {
4556                 output_format = OFORMAT_JSON;
4557             } else if (!strcmp(optarg, "human")) {
4558                 output_format = OFORMAT_HUMAN;
4559             } else {
4560                 error_report("--output must be used with human or json "
4561                              "as argument.");
4562                 goto out;
4563             }
4564             break;
4565         case OPTION_SIZE:
4566         {
4567             int64_t sval;
4568 
4569             sval = cvtnum(optarg);
4570             if (sval < 0) {
4571                 if (sval == -ERANGE) {
4572                     error_report("Image size must be less than 8 EiB!");
4573                 } else {
4574                     error_report("Invalid image size specified! You may use "
4575                                  "k, M, G, T, P or E suffixes for ");
4576                     error_report("kilobytes, megabytes, gigabytes, terabytes, "
4577                                  "petabytes and exabytes.");
4578                 }
4579                 goto out;
4580             }
4581             img_size = (uint64_t)sval;
4582         }
4583         break;
4584         }
4585     }
4586 
4587     if (qemu_opts_foreach(&qemu_object_opts,
4588                           user_creatable_add_opts_foreach,
4589                           NULL, NULL)) {
4590         goto out;
4591     }
4592 
4593     if (argc - optind > 1) {
4594         error_report("At most one filename argument is allowed.");
4595         goto out;
4596     } else if (argc - optind == 1) {
4597         filename = argv[optind];
4598     }
4599 
4600     if (!filename &&
4601         (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4602         error_report("--object, --image-opts, -f, and -l "
4603                      "require a filename argument.");
4604         goto out;
4605     }
4606     if (filename && img_size != UINT64_MAX) {
4607         error_report("--size N cannot be used together with a filename.");
4608         goto out;
4609     }
4610     if (!filename && img_size == UINT64_MAX) {
4611         error_report("Either --size N or one filename must be specified.");
4612         goto out;
4613     }
4614 
4615     if (filename) {
4616         in_blk = img_open(image_opts, filename, fmt, 0,
4617                           false, false, force_share);
4618         if (!in_blk) {
4619             goto out;
4620         }
4621 
4622         if (sn_opts) {
4623             bdrv_snapshot_load_tmp(blk_bs(in_blk),
4624                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4625                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4626                     &local_err);
4627         } else if (snapshot_name != NULL) {
4628             bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4629                     snapshot_name, &local_err);
4630         }
4631         if (local_err) {
4632             error_reportf_err(local_err, "Failed to load snapshot: ");
4633             goto out;
4634         }
4635     }
4636 
4637     drv = bdrv_find_format(out_fmt);
4638     if (!drv) {
4639         error_report("Unknown file format '%s'", out_fmt);
4640         goto out;
4641     }
4642     if (!drv->create_opts) {
4643         error_report("Format driver '%s' does not support image creation",
4644                      drv->format_name);
4645         goto out;
4646     }
4647 
4648     create_opts = qemu_opts_append(create_opts, drv->create_opts);
4649     create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4650     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4651     if (options) {
4652         qemu_opts_do_parse(opts, options, NULL, &local_err);
4653         if (local_err) {
4654             error_report_err(local_err);
4655             error_report("Invalid options for file format '%s'", out_fmt);
4656             goto out;
4657         }
4658     }
4659     if (img_size != UINT64_MAX) {
4660         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4661     }
4662 
4663     info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4664     if (local_err) {
4665         error_report_err(local_err);
4666         goto out;
4667     }
4668 
4669     if (output_format == OFORMAT_HUMAN) {
4670         printf("required size: %" PRIu64 "\n", info->required);
4671         printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4672     } else {
4673         dump_json_block_measure_info(info);
4674     }
4675 
4676     ret = 0;
4677 
4678 out:
4679     qapi_free_BlockMeasureInfo(info);
4680     qemu_opts_del(object_opts);
4681     qemu_opts_del(opts);
4682     qemu_opts_del(sn_opts);
4683     qemu_opts_free(create_opts);
4684     g_free(options);
4685     blk_unref(in_blk);
4686     return ret;
4687 }
4688 
4689 static const img_cmd_t img_cmds[] = {
4690 #define DEF(option, callback, arg_string)        \
4691     { option, callback },
4692 #include "qemu-img-cmds.h"
4693 #undef DEF
4694 #undef GEN_DOCS
4695     { NULL, NULL, },
4696 };
4697 
4698 int main(int argc, char **argv)
4699 {
4700     const img_cmd_t *cmd;
4701     const char *cmdname;
4702     Error *local_error = NULL;
4703     char *trace_file = NULL;
4704     int c;
4705     static const struct option long_options[] = {
4706         {"help", no_argument, 0, 'h'},
4707         {"version", no_argument, 0, 'V'},
4708         {"trace", required_argument, NULL, 'T'},
4709         {0, 0, 0, 0}
4710     };
4711 
4712 #ifdef CONFIG_POSIX
4713     signal(SIGPIPE, SIG_IGN);
4714 #endif
4715 
4716     module_call_init(MODULE_INIT_TRACE);
4717     error_set_progname(argv[0]);
4718     qemu_init_exec_dir(argv[0]);
4719 
4720     if (qemu_init_main_loop(&local_error)) {
4721         error_report_err(local_error);
4722         exit(EXIT_FAILURE);
4723     }
4724 
4725     qcrypto_init(&error_fatal);
4726 
4727     module_call_init(MODULE_INIT_QOM);
4728     bdrv_init();
4729     if (argc < 2) {
4730         error_exit("Not enough arguments");
4731     }
4732 
4733     qemu_add_opts(&qemu_object_opts);
4734     qemu_add_opts(&qemu_source_opts);
4735     qemu_add_opts(&qemu_trace_opts);
4736 
4737     while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
4738         switch (c) {
4739         case ':':
4740             missing_argument(argv[optind - 1]);
4741             return 0;
4742         case '?':
4743             unrecognized_option(argv[optind - 1]);
4744             return 0;
4745         case 'h':
4746             help();
4747             return 0;
4748         case 'V':
4749             printf(QEMU_IMG_VERSION);
4750             return 0;
4751         case 'T':
4752             g_free(trace_file);
4753             trace_file = trace_opt_parse(optarg);
4754             break;
4755         }
4756     }
4757 
4758     cmdname = argv[optind];
4759 
4760     /* reset getopt_long scanning */
4761     argc -= optind;
4762     if (argc < 1) {
4763         return 0;
4764     }
4765     argv += optind;
4766     optind = 0;
4767 
4768     if (!trace_init_backends()) {
4769         exit(1);
4770     }
4771     trace_init_file(trace_file);
4772     qemu_set_log(LOG_TRACE);
4773 
4774     /* find the command */
4775     for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4776         if (!strcmp(cmdname, cmd->name)) {
4777             return cmd->handler(argc, argv);
4778         }
4779     }
4780 
4781     /* not found */
4782     error_exit("Command not found: %s", cmdname);
4783 }
4784