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