xref: /openbmc/qemu/qemu-img.c (revision f30c66ba6e417a07e68ad6e0bc5da27561a3beea)
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);
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             return ret;
1634         }
1635         n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
1636 
1637         if (ret & BDRV_BLOCK_ZERO) {
1638             s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
1639         } else if (ret & BDRV_BLOCK_DATA) {
1640             s->status = BLK_DATA;
1641         } else {
1642             s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
1643         }
1644 
1645         s->sector_next_status = sector_num + n;
1646     }
1647 
1648     n = MIN(n, s->sector_next_status - sector_num);
1649     if (s->status == BLK_DATA) {
1650         n = MIN(n, s->buf_sectors);
1651     }
1652 
1653     /* We need to write complete clusters for compressed images, so if an
1654      * unallocated area is shorter than that, we must consider the whole
1655      * cluster allocated. */
1656     if (s->compressed) {
1657         if (n < s->cluster_sectors) {
1658             n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1659             s->status = BLK_DATA;
1660         } else {
1661             n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1662         }
1663     }
1664 
1665     return n;
1666 }
1667 
1668 static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1669                                         int nb_sectors, uint8_t *buf)
1670 {
1671     int n, ret;
1672     QEMUIOVector qiov;
1673     struct iovec iov;
1674 
1675     assert(nb_sectors <= s->buf_sectors);
1676     while (nb_sectors > 0) {
1677         BlockBackend *blk;
1678         int src_cur;
1679         int64_t bs_sectors, src_cur_offset;
1680 
1681         /* In the case of compression with multiple source files, we can get a
1682          * nb_sectors that spreads into the next part. So we must be able to
1683          * read across multiple BDSes for one convert_read() call. */
1684         convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1685         blk = s->src[src_cur];
1686         bs_sectors = s->src_sectors[src_cur];
1687 
1688         n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1689         iov.iov_base = buf;
1690         iov.iov_len = n << BDRV_SECTOR_BITS;
1691         qemu_iovec_init_external(&qiov, &iov, 1);
1692 
1693         ret = blk_co_preadv(
1694                 blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS,
1695                 n << BDRV_SECTOR_BITS, &qiov, 0);
1696         if (ret < 0) {
1697             return ret;
1698         }
1699 
1700         sector_num += n;
1701         nb_sectors -= n;
1702         buf += n * BDRV_SECTOR_SIZE;
1703     }
1704 
1705     return 0;
1706 }
1707 
1708 
1709 static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1710                                          int nb_sectors, uint8_t *buf,
1711                                          enum ImgConvertBlockStatus status)
1712 {
1713     int ret;
1714     QEMUIOVector qiov;
1715     struct iovec iov;
1716 
1717     while (nb_sectors > 0) {
1718         int n = nb_sectors;
1719         BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1720 
1721         switch (status) {
1722         case BLK_BACKING_FILE:
1723             /* If we have a backing file, leave clusters unallocated that are
1724              * unallocated in the source image, so that the backing file is
1725              * visible at the respective offset. */
1726             assert(s->target_has_backing);
1727             break;
1728 
1729         case BLK_DATA:
1730             /* If we're told to keep the target fully allocated (-S 0) or there
1731              * is real non-zero data, we must write it. Otherwise we can treat
1732              * it as zero sectors.
1733              * Compressed clusters need to be written as a whole, so in that
1734              * case we can only save the write if the buffer is completely
1735              * zeroed. */
1736             if (!s->min_sparse ||
1737                 (!s->compressed &&
1738                  is_allocated_sectors_min(buf, n, &n, s->min_sparse,
1739                                           sector_num, s->alignment)) ||
1740                 (s->compressed &&
1741                  !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
1742             {
1743                 iov.iov_base = buf;
1744                 iov.iov_len = n << BDRV_SECTOR_BITS;
1745                 qemu_iovec_init_external(&qiov, &iov, 1);
1746 
1747                 ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
1748                                      n << BDRV_SECTOR_BITS, &qiov, flags);
1749                 if (ret < 0) {
1750                     return ret;
1751                 }
1752                 break;
1753             }
1754             /* fall-through */
1755 
1756         case BLK_ZERO:
1757             if (s->has_zero_init) {
1758                 assert(!s->target_has_backing);
1759                 break;
1760             }
1761             ret = blk_co_pwrite_zeroes(s->target,
1762                                        sector_num << BDRV_SECTOR_BITS,
1763                                        n << BDRV_SECTOR_BITS, 0);
1764             if (ret < 0) {
1765                 return ret;
1766             }
1767             break;
1768         }
1769 
1770         sector_num += n;
1771         nb_sectors -= n;
1772         buf += n * BDRV_SECTOR_SIZE;
1773     }
1774 
1775     return 0;
1776 }
1777 
1778 static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1779                                               int nb_sectors)
1780 {
1781     int n, ret;
1782 
1783     while (nb_sectors > 0) {
1784         BlockBackend *blk;
1785         int src_cur;
1786         int64_t bs_sectors, src_cur_offset;
1787         int64_t offset;
1788 
1789         convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1790         offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1791         blk = s->src[src_cur];
1792         bs_sectors = s->src_sectors[src_cur];
1793 
1794         n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1795 
1796         ret = blk_co_copy_range(blk, offset, s->target,
1797                                 sector_num << BDRV_SECTOR_BITS,
1798                                 n << BDRV_SECTOR_BITS, 0, 0);
1799         if (ret < 0) {
1800             return ret;
1801         }
1802 
1803         sector_num += n;
1804         nb_sectors -= n;
1805     }
1806     return 0;
1807 }
1808 
1809 static void coroutine_fn convert_co_do_copy(void *opaque)
1810 {
1811     ImgConvertState *s = opaque;
1812     uint8_t *buf = NULL;
1813     int ret, i;
1814     int index = -1;
1815 
1816     for (i = 0; i < s->num_coroutines; i++) {
1817         if (s->co[i] == qemu_coroutine_self()) {
1818             index = i;
1819             break;
1820         }
1821     }
1822     assert(index >= 0);
1823 
1824     s->running_coroutines++;
1825     buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1826 
1827     while (1) {
1828         int n;
1829         int64_t sector_num;
1830         enum ImgConvertBlockStatus status;
1831         bool copy_range;
1832 
1833         qemu_co_mutex_lock(&s->lock);
1834         if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1835             qemu_co_mutex_unlock(&s->lock);
1836             break;
1837         }
1838         n = convert_iteration_sectors(s, s->sector_num);
1839         if (n < 0) {
1840             qemu_co_mutex_unlock(&s->lock);
1841             s->ret = n;
1842             break;
1843         }
1844         /* save current sector and allocation status to local variables */
1845         sector_num = s->sector_num;
1846         status = s->status;
1847         if (!s->min_sparse && s->status == BLK_ZERO) {
1848             n = MIN(n, s->buf_sectors);
1849         }
1850         /* increment global sector counter so that other coroutines can
1851          * already continue reading beyond this request */
1852         s->sector_num += n;
1853         qemu_co_mutex_unlock(&s->lock);
1854 
1855         if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
1856             s->allocated_done += n;
1857             qemu_progress_print(100.0 * s->allocated_done /
1858                                         s->allocated_sectors, 0);
1859         }
1860 
1861 retry:
1862         copy_range = s->copy_range && s->status == BLK_DATA;
1863         if (status == BLK_DATA && !copy_range) {
1864             ret = convert_co_read(s, sector_num, n, buf);
1865             if (ret < 0) {
1866                 error_report("error while reading sector %" PRId64
1867                              ": %s", sector_num, strerror(-ret));
1868                 s->ret = ret;
1869             }
1870         } else if (!s->min_sparse && status == BLK_ZERO) {
1871             status = BLK_DATA;
1872             memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
1873         }
1874 
1875         if (s->wr_in_order) {
1876             /* keep writes in order */
1877             while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
1878                 s->wait_sector_num[index] = sector_num;
1879                 qemu_coroutine_yield();
1880             }
1881             s->wait_sector_num[index] = -1;
1882         }
1883 
1884         if (s->ret == -EINPROGRESS) {
1885             if (copy_range) {
1886                 ret = convert_co_copy_range(s, sector_num, n);
1887                 if (ret) {
1888                     s->copy_range = false;
1889                     goto retry;
1890                 }
1891             } else {
1892                 ret = convert_co_write(s, sector_num, n, buf, status);
1893             }
1894             if (ret < 0) {
1895                 error_report("error while writing sector %" PRId64
1896                              ": %s", sector_num, strerror(-ret));
1897                 s->ret = ret;
1898             }
1899         }
1900 
1901         if (s->wr_in_order) {
1902             /* reenter the coroutine that might have waited
1903              * for this write to complete */
1904             s->wr_offs = sector_num + n;
1905             for (i = 0; i < s->num_coroutines; i++) {
1906                 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
1907                     /*
1908                      * A -> B -> A cannot occur because A has
1909                      * s->wait_sector_num[i] == -1 during A -> B.  Therefore
1910                      * B will never enter A during this time window.
1911                      */
1912                     qemu_coroutine_enter(s->co[i]);
1913                     break;
1914                 }
1915             }
1916         }
1917     }
1918 
1919     qemu_vfree(buf);
1920     s->co[index] = NULL;
1921     s->running_coroutines--;
1922     if (!s->running_coroutines && s->ret == -EINPROGRESS) {
1923         /* the convert job finished successfully */
1924         s->ret = 0;
1925     }
1926 }
1927 
1928 static int convert_do_copy(ImgConvertState *s)
1929 {
1930     int ret, i, n;
1931     int64_t sector_num = 0;
1932 
1933     /* Check whether we have zero initialisation or can get it efficiently */
1934     s->has_zero_init = s->min_sparse && !s->target_has_backing
1935                      ? bdrv_has_zero_init(blk_bs(s->target))
1936                      : false;
1937 
1938     if (!s->has_zero_init && !s->target_has_backing &&
1939         bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))
1940     {
1941         ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP);
1942         if (ret == 0) {
1943             s->has_zero_init = true;
1944         }
1945     }
1946 
1947     /* Allocate buffer for copied data. For compressed images, only one cluster
1948      * can be copied at a time. */
1949     if (s->compressed) {
1950         if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
1951             error_report("invalid cluster size");
1952             return -EINVAL;
1953         }
1954         s->buf_sectors = s->cluster_sectors;
1955     }
1956 
1957     while (sector_num < s->total_sectors) {
1958         n = convert_iteration_sectors(s, sector_num);
1959         if (n < 0) {
1960             return n;
1961         }
1962         if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
1963         {
1964             s->allocated_sectors += n;
1965         }
1966         sector_num += n;
1967     }
1968 
1969     /* Do the copy */
1970     s->sector_next_status = 0;
1971     s->ret = -EINPROGRESS;
1972 
1973     qemu_co_mutex_init(&s->lock);
1974     for (i = 0; i < s->num_coroutines; i++) {
1975         s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
1976         s->wait_sector_num[i] = -1;
1977         qemu_coroutine_enter(s->co[i]);
1978     }
1979 
1980     while (s->running_coroutines) {
1981         main_loop_wait(false);
1982     }
1983 
1984     if (s->compressed && !s->ret) {
1985         /* signal EOF to align */
1986         ret = blk_pwrite_compressed(s->target, 0, NULL, 0);
1987         if (ret < 0) {
1988             return ret;
1989         }
1990     }
1991 
1992     return s->ret;
1993 }
1994 
1995 #define MAX_BUF_SECTORS 32768
1996 
1997 static int img_convert(int argc, char **argv)
1998 {
1999     int c, bs_i, flags, src_flags = 0;
2000     const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
2001                *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
2002                *out_filename, *out_baseimg_param, *snapshot_name = NULL;
2003     BlockDriver *drv = NULL, *proto_drv = NULL;
2004     BlockDriverInfo bdi;
2005     BlockDriverState *out_bs;
2006     QemuOpts *opts = NULL, *sn_opts = NULL;
2007     QemuOptsList *create_opts = NULL;
2008     QDict *open_opts = NULL;
2009     char *options = NULL;
2010     Error *local_err = NULL;
2011     bool writethrough, src_writethrough, quiet = false, image_opts = false,
2012          skip_create = false, progress = false, tgt_image_opts = false;
2013     int64_t ret = -EINVAL;
2014     bool force_share = false;
2015     bool explict_min_sparse = false;
2016 
2017     ImgConvertState s = (ImgConvertState) {
2018         /* Need at least 4k of zeros for sparse detection */
2019         .min_sparse         = 8,
2020         .copy_range         = false,
2021         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2022         .wr_in_order        = true,
2023         .num_coroutines     = 8,
2024     };
2025 
2026     for(;;) {
2027         static const struct option long_options[] = {
2028             {"help", no_argument, 0, 'h'},
2029             {"object", required_argument, 0, OPTION_OBJECT},
2030             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2031             {"force-share", no_argument, 0, 'U'},
2032             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
2033             {0, 0, 0, 0}
2034         };
2035         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
2036                         long_options, NULL);
2037         if (c == -1) {
2038             break;
2039         }
2040         switch(c) {
2041         case ':':
2042             missing_argument(argv[optind - 1]);
2043             break;
2044         case '?':
2045             unrecognized_option(argv[optind - 1]);
2046             break;
2047         case 'h':
2048             help();
2049             break;
2050         case 'f':
2051             fmt = optarg;
2052             break;
2053         case 'O':
2054             out_fmt = optarg;
2055             break;
2056         case 'B':
2057             out_baseimg = optarg;
2058             break;
2059         case 'C':
2060             s.copy_range = true;
2061             break;
2062         case 'c':
2063             s.compressed = true;
2064             break;
2065         case 'o':
2066             if (!is_valid_option_list(optarg)) {
2067                 error_report("Invalid option list: %s", optarg);
2068                 goto fail_getopt;
2069             }
2070             if (!options) {
2071                 options = g_strdup(optarg);
2072             } else {
2073                 char *old_options = options;
2074                 options = g_strdup_printf("%s,%s", options, optarg);
2075                 g_free(old_options);
2076             }
2077             break;
2078         case 'l':
2079             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
2080                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2081                                                   optarg, false);
2082                 if (!sn_opts) {
2083                     error_report("Failed in parsing snapshot param '%s'",
2084                                  optarg);
2085                     goto fail_getopt;
2086                 }
2087             } else {
2088                 snapshot_name = optarg;
2089             }
2090             break;
2091         case 'S':
2092         {
2093             int64_t sval;
2094 
2095             sval = cvtnum(optarg);
2096             if (sval < 0 || sval & (BDRV_SECTOR_SIZE - 1) ||
2097                 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
2098                 error_report("Invalid buffer size for sparse output specified. "
2099                     "Valid sizes are multiples of %llu up to %llu. Select "
2100                     "0 to disable sparse detection (fully allocates output).",
2101                     BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
2102                 goto fail_getopt;
2103             }
2104 
2105             s.min_sparse = sval / BDRV_SECTOR_SIZE;
2106             explict_min_sparse = true;
2107             break;
2108         }
2109         case 'p':
2110             progress = true;
2111             break;
2112         case 't':
2113             cache = optarg;
2114             break;
2115         case 'T':
2116             src_cache = optarg;
2117             break;
2118         case 'q':
2119             quiet = true;
2120             break;
2121         case 'n':
2122             skip_create = true;
2123             break;
2124         case 'm':
2125             if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2126                 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2127                 error_report("Invalid number of coroutines. Allowed number of"
2128                              " coroutines is between 1 and %d", MAX_COROUTINES);
2129                 goto fail_getopt;
2130             }
2131             break;
2132         case 'W':
2133             s.wr_in_order = false;
2134             break;
2135         case 'U':
2136             force_share = true;
2137             break;
2138         case OPTION_OBJECT: {
2139             QemuOpts *object_opts;
2140             object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
2141                                                   optarg, true);
2142             if (!object_opts) {
2143                 goto fail_getopt;
2144             }
2145             break;
2146         }
2147         case OPTION_IMAGE_OPTS:
2148             image_opts = true;
2149             break;
2150         case OPTION_TARGET_IMAGE_OPTS:
2151             tgt_image_opts = true;
2152             break;
2153         }
2154     }
2155 
2156     if (!out_fmt && !tgt_image_opts) {
2157         out_fmt = "raw";
2158     }
2159 
2160     if (qemu_opts_foreach(&qemu_object_opts,
2161                           user_creatable_add_opts_foreach,
2162                           NULL, &error_fatal)) {
2163         goto fail_getopt;
2164     }
2165 
2166     if (s.compressed && s.copy_range) {
2167         error_report("Cannot enable copy offloading when -c is used");
2168         goto fail_getopt;
2169     }
2170 
2171     if (explict_min_sparse && s.copy_range) {
2172         error_report("Cannot enable copy offloading when -S is used");
2173         goto fail_getopt;
2174     }
2175 
2176     if (tgt_image_opts && !skip_create) {
2177         error_report("--target-image-opts requires use of -n flag");
2178         goto fail_getopt;
2179     }
2180 
2181     s.src_num = argc - optind - 1;
2182     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2183 
2184     if (options && has_help_option(options)) {
2185         if (out_fmt) {
2186             ret = print_block_option_help(out_filename, out_fmt);
2187             goto fail_getopt;
2188         } else {
2189             error_report("Option help requires a format be specified");
2190             goto fail_getopt;
2191         }
2192     }
2193 
2194     if (s.src_num < 1) {
2195         error_report("Must specify image file name");
2196         goto fail_getopt;
2197     }
2198 
2199 
2200     /* ret is still -EINVAL until here */
2201     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2202     if (ret < 0) {
2203         error_report("Invalid source cache option: %s", src_cache);
2204         goto fail_getopt;
2205     }
2206 
2207     /* Initialize before goto out */
2208     if (quiet) {
2209         progress = false;
2210     }
2211     qemu_progress_init(progress, 1.0);
2212     qemu_progress_print(0, 100);
2213 
2214     s.src = g_new0(BlockBackend *, s.src_num);
2215     s.src_sectors = g_new(int64_t, s.src_num);
2216 
2217     for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2218         s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2219                                fmt, src_flags, src_writethrough, quiet,
2220                                force_share);
2221         if (!s.src[bs_i]) {
2222             ret = -1;
2223             goto out;
2224         }
2225         s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2226         if (s.src_sectors[bs_i] < 0) {
2227             error_report("Could not get size of %s: %s",
2228                          argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
2229             ret = -1;
2230             goto out;
2231         }
2232         s.total_sectors += s.src_sectors[bs_i];
2233     }
2234 
2235     if (sn_opts) {
2236         bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
2237                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2238                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2239                                &local_err);
2240     } else if (snapshot_name != NULL) {
2241         if (s.src_num > 1) {
2242             error_report("No support for concatenating multiple snapshot");
2243             ret = -1;
2244             goto out;
2245         }
2246 
2247         bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2248                                              &local_err);
2249     }
2250     if (local_err) {
2251         error_reportf_err(local_err, "Failed to load snapshot: ");
2252         ret = -1;
2253         goto out;
2254     }
2255 
2256     if (!skip_create) {
2257         /* Find driver and parse its options */
2258         drv = bdrv_find_format(out_fmt);
2259         if (!drv) {
2260             error_report("Unknown file format '%s'", out_fmt);
2261             ret = -1;
2262             goto out;
2263         }
2264 
2265         proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2266         if (!proto_drv) {
2267             error_report_err(local_err);
2268             ret = -1;
2269             goto out;
2270         }
2271 
2272         if (!drv->create_opts) {
2273             error_report("Format driver '%s' does not support image creation",
2274                          drv->format_name);
2275             ret = -1;
2276             goto out;
2277         }
2278 
2279         if (!proto_drv->create_opts) {
2280             error_report("Protocol driver '%s' does not support image creation",
2281                          proto_drv->format_name);
2282             ret = -1;
2283             goto out;
2284         }
2285 
2286         create_opts = qemu_opts_append(create_opts, drv->create_opts);
2287         create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
2288 
2289         opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2290         if (options) {
2291             qemu_opts_do_parse(opts, options, NULL, &local_err);
2292             if (local_err) {
2293                 error_report_err(local_err);
2294                 ret = -1;
2295                 goto out;
2296             }
2297         }
2298 
2299         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512,
2300                             &error_abort);
2301         ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
2302         if (ret < 0) {
2303             goto out;
2304         }
2305     }
2306 
2307     /* Get backing file name if -o backing_file was used */
2308     out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2309     if (out_baseimg_param) {
2310         out_baseimg = out_baseimg_param;
2311     }
2312     s.target_has_backing = (bool) out_baseimg;
2313 
2314     if (s.src_num > 1 && out_baseimg) {
2315         error_report("Having a backing file for the target makes no sense when "
2316                      "concatenating multiple input images");
2317         ret = -1;
2318         goto out;
2319     }
2320 
2321     /* Check if compression is supported */
2322     if (s.compressed) {
2323         bool encryption =
2324             qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2325         const char *encryptfmt =
2326             qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2327         const char *preallocation =
2328             qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
2329 
2330         if (drv && !drv->bdrv_co_pwritev_compressed) {
2331             error_report("Compression not supported for this file format");
2332             ret = -1;
2333             goto out;
2334         }
2335 
2336         if (encryption || encryptfmt) {
2337             error_report("Compression and encryption not supported at "
2338                          "the same time");
2339             ret = -1;
2340             goto out;
2341         }
2342 
2343         if (preallocation
2344             && strcmp(preallocation, "off"))
2345         {
2346             error_report("Compression and preallocation not supported at "
2347                          "the same time");
2348             ret = -1;
2349             goto out;
2350         }
2351     }
2352 
2353     /*
2354      * The later open call will need any decryption secrets, and
2355      * bdrv_create() will purge "opts", so extract them now before
2356      * they are lost.
2357      */
2358     if (!skip_create) {
2359         open_opts = qdict_new();
2360         qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
2361     }
2362 
2363     if (!skip_create) {
2364         /* Create the new image */
2365         ret = bdrv_create(drv, out_filename, opts, &local_err);
2366         if (ret < 0) {
2367             error_reportf_err(local_err, "%s: error while converting %s: ",
2368                               out_filename, out_fmt);
2369             goto out;
2370         }
2371     }
2372 
2373     flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
2374     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
2375     if (ret < 0) {
2376         error_report("Invalid cache option: %s", cache);
2377         goto out;
2378     }
2379 
2380     if (skip_create) {
2381         s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2382                             flags, writethrough, quiet, false);
2383     } else {
2384         /* TODO ultimately we should allow --target-image-opts
2385          * to be used even when -n is not given.
2386          * That has to wait for bdrv_create to be improved
2387          * to allow filenames in option syntax
2388          */
2389         s.target = img_open_file(out_filename, open_opts, out_fmt,
2390                                  flags, writethrough, quiet, false);
2391         open_opts = NULL; /* blk_new_open will have freed it */
2392     }
2393     if (!s.target) {
2394         ret = -1;
2395         goto out;
2396     }
2397     out_bs = blk_bs(s.target);
2398 
2399     if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) {
2400         error_report("Compression not supported for this file format");
2401         ret = -1;
2402         goto out;
2403     }
2404 
2405     /* increase bufsectors from the default 4096 (2M) if opt_transfer
2406      * or discard_alignment of the out_bs is greater. Limit to
2407      * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2408     s.buf_sectors = MIN(MAX_BUF_SECTORS,
2409                         MAX(s.buf_sectors,
2410                             MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2411                                 out_bs->bl.pdiscard_alignment >>
2412                                 BDRV_SECTOR_BITS)));
2413 
2414     /* try to align the write requests to the destination to avoid unnecessary
2415      * RMW cycles. */
2416     s.alignment = MAX(pow2floor(s.min_sparse),
2417                       DIV_ROUND_UP(out_bs->bl.request_alignment,
2418                                    BDRV_SECTOR_SIZE));
2419     assert(is_power_of_2(s.alignment));
2420 
2421     if (skip_create) {
2422         int64_t output_sectors = blk_nb_sectors(s.target);
2423         if (output_sectors < 0) {
2424             error_report("unable to get output image length: %s",
2425                          strerror(-output_sectors));
2426             ret = -1;
2427             goto out;
2428         } else if (output_sectors < s.total_sectors) {
2429             error_report("output file is smaller than input file");
2430             ret = -1;
2431             goto out;
2432         }
2433     }
2434 
2435     if (s.target_has_backing) {
2436         /* Errors are treated as "backing length unknown" (which means
2437          * s.target_backing_sectors has to be negative, which it will
2438          * be automatically).  The backing file length is used only
2439          * for optimizations, so such a case is not fatal. */
2440         s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs);
2441     } else {
2442         s.target_backing_sectors = -1;
2443     }
2444 
2445     ret = bdrv_get_info(out_bs, &bdi);
2446     if (ret < 0) {
2447         if (s.compressed) {
2448             error_report("could not get block driver info");
2449             goto out;
2450         }
2451     } else {
2452         s.compressed = s.compressed || bdi.needs_compressed_writes;
2453         s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2454         s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero;
2455     }
2456 
2457     ret = convert_do_copy(&s);
2458 out:
2459     if (!ret) {
2460         qemu_progress_print(100, 0);
2461     }
2462     qemu_progress_end();
2463     qemu_opts_del(opts);
2464     qemu_opts_free(create_opts);
2465     qemu_opts_del(sn_opts);
2466     qobject_unref(open_opts);
2467     blk_unref(s.target);
2468     if (s.src) {
2469         for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2470             blk_unref(s.src[bs_i]);
2471         }
2472         g_free(s.src);
2473     }
2474     g_free(s.src_sectors);
2475 fail_getopt:
2476     g_free(options);
2477 
2478     return !!ret;
2479 }
2480 
2481 
2482 static void dump_snapshots(BlockDriverState *bs)
2483 {
2484     QEMUSnapshotInfo *sn_tab, *sn;
2485     int nb_sns, i;
2486 
2487     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2488     if (nb_sns <= 0)
2489         return;
2490     printf("Snapshot list:\n");
2491     bdrv_snapshot_dump(fprintf, stdout, NULL);
2492     printf("\n");
2493     for(i = 0; i < nb_sns; i++) {
2494         sn = &sn_tab[i];
2495         bdrv_snapshot_dump(fprintf, stdout, sn);
2496         printf("\n");
2497     }
2498     g_free(sn_tab);
2499 }
2500 
2501 static void dump_json_image_info_list(ImageInfoList *list)
2502 {
2503     QString *str;
2504     QObject *obj;
2505     Visitor *v = qobject_output_visitor_new(&obj);
2506 
2507     visit_type_ImageInfoList(v, NULL, &list, &error_abort);
2508     visit_complete(v, &obj);
2509     str = qobject_to_json_pretty(obj);
2510     assert(str != NULL);
2511     printf("%s\n", qstring_get_str(str));
2512     qobject_unref(obj);
2513     visit_free(v);
2514     qobject_unref(str);
2515 }
2516 
2517 static void dump_json_image_info(ImageInfo *info)
2518 {
2519     QString *str;
2520     QObject *obj;
2521     Visitor *v = qobject_output_visitor_new(&obj);
2522 
2523     visit_type_ImageInfo(v, NULL, &info, &error_abort);
2524     visit_complete(v, &obj);
2525     str = qobject_to_json_pretty(obj);
2526     assert(str != NULL);
2527     printf("%s\n", qstring_get_str(str));
2528     qobject_unref(obj);
2529     visit_free(v);
2530     qobject_unref(str);
2531 }
2532 
2533 static void dump_human_image_info_list(ImageInfoList *list)
2534 {
2535     ImageInfoList *elem;
2536     bool delim = false;
2537 
2538     for (elem = list; elem; elem = elem->next) {
2539         if (delim) {
2540             printf("\n");
2541         }
2542         delim = true;
2543 
2544         bdrv_image_info_dump(fprintf, stdout, elem->value);
2545     }
2546 }
2547 
2548 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2549 {
2550     return strcmp(a, b) == 0;
2551 }
2552 
2553 /**
2554  * Open an image file chain and return an ImageInfoList
2555  *
2556  * @filename: topmost image filename
2557  * @fmt: topmost image format (may be NULL to autodetect)
2558  * @chain: true  - enumerate entire backing file chain
2559  *         false - only topmost image file
2560  *
2561  * Returns a list of ImageInfo objects or NULL if there was an error opening an
2562  * image file.  If there was an error a message will have been printed to
2563  * stderr.
2564  */
2565 static ImageInfoList *collect_image_info_list(bool image_opts,
2566                                               const char *filename,
2567                                               const char *fmt,
2568                                               bool chain, bool force_share)
2569 {
2570     ImageInfoList *head = NULL;
2571     ImageInfoList **last = &head;
2572     GHashTable *filenames;
2573     Error *err = NULL;
2574 
2575     filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2576 
2577     while (filename) {
2578         BlockBackend *blk;
2579         BlockDriverState *bs;
2580         ImageInfo *info;
2581         ImageInfoList *elem;
2582 
2583         if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2584             error_report("Backing file '%s' creates an infinite loop.",
2585                          filename);
2586             goto err;
2587         }
2588         g_hash_table_insert(filenames, (gpointer)filename, NULL);
2589 
2590         blk = img_open(image_opts, filename, fmt,
2591                        BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2592                        force_share);
2593         if (!blk) {
2594             goto err;
2595         }
2596         bs = blk_bs(blk);
2597 
2598         bdrv_query_image_info(bs, &info, &err);
2599         if (err) {
2600             error_report_err(err);
2601             blk_unref(blk);
2602             goto err;
2603         }
2604 
2605         elem = g_new0(ImageInfoList, 1);
2606         elem->value = info;
2607         *last = elem;
2608         last = &elem->next;
2609 
2610         blk_unref(blk);
2611 
2612         filename = fmt = NULL;
2613         if (chain) {
2614             if (info->has_full_backing_filename) {
2615                 filename = info->full_backing_filename;
2616             } else if (info->has_backing_filename) {
2617                 error_report("Could not determine absolute backing filename,"
2618                              " but backing filename '%s' present",
2619                              info->backing_filename);
2620                 goto err;
2621             }
2622             if (info->has_backing_filename_format) {
2623                 fmt = info->backing_filename_format;
2624             }
2625         }
2626     }
2627     g_hash_table_destroy(filenames);
2628     return head;
2629 
2630 err:
2631     qapi_free_ImageInfoList(head);
2632     g_hash_table_destroy(filenames);
2633     return NULL;
2634 }
2635 
2636 static int img_info(int argc, char **argv)
2637 {
2638     int c;
2639     OutputFormat output_format = OFORMAT_HUMAN;
2640     bool chain = false;
2641     const char *filename, *fmt, *output;
2642     ImageInfoList *list;
2643     bool image_opts = false;
2644     bool force_share = false;
2645 
2646     fmt = NULL;
2647     output = NULL;
2648     for(;;) {
2649         int option_index = 0;
2650         static const struct option long_options[] = {
2651             {"help", no_argument, 0, 'h'},
2652             {"format", required_argument, 0, 'f'},
2653             {"output", required_argument, 0, OPTION_OUTPUT},
2654             {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
2655             {"object", required_argument, 0, OPTION_OBJECT},
2656             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2657             {"force-share", no_argument, 0, 'U'},
2658             {0, 0, 0, 0}
2659         };
2660         c = getopt_long(argc, argv, ":f:hU",
2661                         long_options, &option_index);
2662         if (c == -1) {
2663             break;
2664         }
2665         switch(c) {
2666         case ':':
2667             missing_argument(argv[optind - 1]);
2668             break;
2669         case '?':
2670             unrecognized_option(argv[optind - 1]);
2671             break;
2672         case 'h':
2673             help();
2674             break;
2675         case 'f':
2676             fmt = optarg;
2677             break;
2678         case 'U':
2679             force_share = true;
2680             break;
2681         case OPTION_OUTPUT:
2682             output = optarg;
2683             break;
2684         case OPTION_BACKING_CHAIN:
2685             chain = true;
2686             break;
2687         case OPTION_OBJECT: {
2688             QemuOpts *opts;
2689             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2690                                            optarg, true);
2691             if (!opts) {
2692                 return 1;
2693             }
2694         }   break;
2695         case OPTION_IMAGE_OPTS:
2696             image_opts = true;
2697             break;
2698         }
2699     }
2700     if (optind != argc - 1) {
2701         error_exit("Expecting one image file name");
2702     }
2703     filename = argv[optind++];
2704 
2705     if (output && !strcmp(output, "json")) {
2706         output_format = OFORMAT_JSON;
2707     } else if (output && !strcmp(output, "human")) {
2708         output_format = OFORMAT_HUMAN;
2709     } else if (output) {
2710         error_report("--output must be used with human or json as argument.");
2711         return 1;
2712     }
2713 
2714     if (qemu_opts_foreach(&qemu_object_opts,
2715                           user_creatable_add_opts_foreach,
2716                           NULL, &error_fatal)) {
2717         return 1;
2718     }
2719 
2720     list = collect_image_info_list(image_opts, filename, fmt, chain,
2721                                    force_share);
2722     if (!list) {
2723         return 1;
2724     }
2725 
2726     switch (output_format) {
2727     case OFORMAT_HUMAN:
2728         dump_human_image_info_list(list);
2729         break;
2730     case OFORMAT_JSON:
2731         if (chain) {
2732             dump_json_image_info_list(list);
2733         } else {
2734             dump_json_image_info(list->value);
2735         }
2736         break;
2737     }
2738 
2739     qapi_free_ImageInfoList(list);
2740     return 0;
2741 }
2742 
2743 static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2744                            MapEntry *next)
2745 {
2746     switch (output_format) {
2747     case OFORMAT_HUMAN:
2748         if (e->data && !e->has_offset) {
2749             error_report("File contains external, encrypted or compressed clusters.");
2750             exit(1);
2751         }
2752         if (e->data && !e->zero) {
2753             printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
2754                    e->start, e->length,
2755                    e->has_offset ? e->offset : 0,
2756                    e->has_filename ? e->filename : "");
2757         }
2758         /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2759          * Modify the flags here to allow more coalescing.
2760          */
2761         if (next && (!next->data || next->zero)) {
2762             next->data = false;
2763             next->zero = true;
2764         }
2765         break;
2766     case OFORMAT_JSON:
2767         printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64","
2768                " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
2769                (e->start == 0 ? "[" : ",\n"),
2770                e->start, e->length, e->depth,
2771                e->zero ? "true" : "false",
2772                e->data ? "true" : "false");
2773         if (e->has_offset) {
2774             printf(", \"offset\": %"PRId64"", e->offset);
2775         }
2776         putchar('}');
2777 
2778         if (!next) {
2779             printf("]\n");
2780         }
2781         break;
2782     }
2783 }
2784 
2785 static int get_block_status(BlockDriverState *bs, int64_t offset,
2786                             int64_t bytes, MapEntry *e)
2787 {
2788     int ret;
2789     int depth;
2790     BlockDriverState *file;
2791     bool has_offset;
2792     int64_t map;
2793     char *filename = NULL;
2794 
2795     /* As an optimization, we could cache the current range of unallocated
2796      * clusters in each file of the chain, and avoid querying the same
2797      * range repeatedly.
2798      */
2799 
2800     depth = 0;
2801     for (;;) {
2802         ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
2803         if (ret < 0) {
2804             return ret;
2805         }
2806         assert(bytes);
2807         if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2808             break;
2809         }
2810         bs = backing_bs(bs);
2811         if (bs == NULL) {
2812             ret = 0;
2813             break;
2814         }
2815 
2816         depth++;
2817     }
2818 
2819     has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
2820 
2821     if (file && has_offset) {
2822         bdrv_refresh_filename(file);
2823         filename = file->filename;
2824     }
2825 
2826     *e = (MapEntry) {
2827         .start = offset,
2828         .length = bytes,
2829         .data = !!(ret & BDRV_BLOCK_DATA),
2830         .zero = !!(ret & BDRV_BLOCK_ZERO),
2831         .offset = map,
2832         .has_offset = has_offset,
2833         .depth = depth,
2834         .has_filename = filename,
2835         .filename = filename,
2836     };
2837 
2838     return 0;
2839 }
2840 
2841 static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
2842 {
2843     if (curr->length == 0) {
2844         return false;
2845     }
2846     if (curr->zero != next->zero ||
2847         curr->data != next->data ||
2848         curr->depth != next->depth ||
2849         curr->has_filename != next->has_filename ||
2850         curr->has_offset != next->has_offset) {
2851         return false;
2852     }
2853     if (curr->has_filename && strcmp(curr->filename, next->filename)) {
2854         return false;
2855     }
2856     if (curr->has_offset && curr->offset + curr->length != next->offset) {
2857         return false;
2858     }
2859     return true;
2860 }
2861 
2862 static int img_map(int argc, char **argv)
2863 {
2864     int c;
2865     OutputFormat output_format = OFORMAT_HUMAN;
2866     BlockBackend *blk;
2867     BlockDriverState *bs;
2868     const char *filename, *fmt, *output;
2869     int64_t length;
2870     MapEntry curr = { .length = 0 }, next;
2871     int ret = 0;
2872     bool image_opts = false;
2873     bool force_share = false;
2874 
2875     fmt = NULL;
2876     output = NULL;
2877     for (;;) {
2878         int option_index = 0;
2879         static const struct option long_options[] = {
2880             {"help", no_argument, 0, 'h'},
2881             {"format", required_argument, 0, 'f'},
2882             {"output", required_argument, 0, OPTION_OUTPUT},
2883             {"object", required_argument, 0, OPTION_OBJECT},
2884             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2885             {"force-share", no_argument, 0, 'U'},
2886             {0, 0, 0, 0}
2887         };
2888         c = getopt_long(argc, argv, ":f:hU",
2889                         long_options, &option_index);
2890         if (c == -1) {
2891             break;
2892         }
2893         switch (c) {
2894         case ':':
2895             missing_argument(argv[optind - 1]);
2896             break;
2897         case '?':
2898             unrecognized_option(argv[optind - 1]);
2899             break;
2900         case 'h':
2901             help();
2902             break;
2903         case 'f':
2904             fmt = optarg;
2905             break;
2906         case 'U':
2907             force_share = true;
2908             break;
2909         case OPTION_OUTPUT:
2910             output = optarg;
2911             break;
2912         case OPTION_OBJECT: {
2913             QemuOpts *opts;
2914             opts = qemu_opts_parse_noisily(&qemu_object_opts,
2915                                            optarg, true);
2916             if (!opts) {
2917                 return 1;
2918             }
2919         }   break;
2920         case OPTION_IMAGE_OPTS:
2921             image_opts = true;
2922             break;
2923         }
2924     }
2925     if (optind != argc - 1) {
2926         error_exit("Expecting one image file name");
2927     }
2928     filename = argv[optind];
2929 
2930     if (output && !strcmp(output, "json")) {
2931         output_format = OFORMAT_JSON;
2932     } else if (output && !strcmp(output, "human")) {
2933         output_format = OFORMAT_HUMAN;
2934     } else if (output) {
2935         error_report("--output must be used with human or json as argument.");
2936         return 1;
2937     }
2938 
2939     if (qemu_opts_foreach(&qemu_object_opts,
2940                           user_creatable_add_opts_foreach,
2941                           NULL, &error_fatal)) {
2942         return 1;
2943     }
2944 
2945     blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
2946     if (!blk) {
2947         return 1;
2948     }
2949     bs = blk_bs(blk);
2950 
2951     if (output_format == OFORMAT_HUMAN) {
2952         printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2953     }
2954 
2955     length = blk_getlength(blk);
2956     while (curr.start + curr.length < length) {
2957         int64_t offset = curr.start + curr.length;
2958         int64_t n;
2959 
2960         /* Probe up to 1 GiB at a time.  */
2961         n = MIN(1 << 30, length - offset);
2962         ret = get_block_status(bs, offset, n, &next);
2963 
2964         if (ret < 0) {
2965             error_report("Could not read file metadata: %s", strerror(-ret));
2966             goto out;
2967         }
2968 
2969         if (entry_mergeable(&curr, &next)) {
2970             curr.length += next.length;
2971             continue;
2972         }
2973 
2974         if (curr.length > 0) {
2975             dump_map_entry(output_format, &curr, &next);
2976         }
2977         curr = next;
2978     }
2979 
2980     dump_map_entry(output_format, &curr, NULL);
2981 
2982 out:
2983     blk_unref(blk);
2984     return ret < 0;
2985 }
2986 
2987 #define SNAPSHOT_LIST   1
2988 #define SNAPSHOT_CREATE 2
2989 #define SNAPSHOT_APPLY  3
2990 #define SNAPSHOT_DELETE 4
2991 
2992 static int img_snapshot(int argc, char **argv)
2993 {
2994     BlockBackend *blk;
2995     BlockDriverState *bs;
2996     QEMUSnapshotInfo sn;
2997     char *filename, *snapshot_name = NULL;
2998     int c, ret = 0, bdrv_oflags;
2999     int action = 0;
3000     qemu_timeval tv;
3001     bool quiet = false;
3002     Error *err = NULL;
3003     bool image_opts = false;
3004     bool force_share = false;
3005 
3006     bdrv_oflags = BDRV_O_RDWR;
3007     /* Parse commandline parameters */
3008     for(;;) {
3009         static const struct option long_options[] = {
3010             {"help", no_argument, 0, 'h'},
3011             {"object", required_argument, 0, OPTION_OBJECT},
3012             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3013             {"force-share", no_argument, 0, 'U'},
3014             {0, 0, 0, 0}
3015         };
3016         c = getopt_long(argc, argv, ":la:c:d:hqU",
3017                         long_options, NULL);
3018         if (c == -1) {
3019             break;
3020         }
3021         switch(c) {
3022         case ':':
3023             missing_argument(argv[optind - 1]);
3024             break;
3025         case '?':
3026             unrecognized_option(argv[optind - 1]);
3027             break;
3028         case 'h':
3029             help();
3030             return 0;
3031         case 'l':
3032             if (action) {
3033                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3034                 return 0;
3035             }
3036             action = SNAPSHOT_LIST;
3037             bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
3038             break;
3039         case 'a':
3040             if (action) {
3041                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3042                 return 0;
3043             }
3044             action = SNAPSHOT_APPLY;
3045             snapshot_name = optarg;
3046             break;
3047         case 'c':
3048             if (action) {
3049                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3050                 return 0;
3051             }
3052             action = SNAPSHOT_CREATE;
3053             snapshot_name = optarg;
3054             break;
3055         case 'd':
3056             if (action) {
3057                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3058                 return 0;
3059             }
3060             action = SNAPSHOT_DELETE;
3061             snapshot_name = optarg;
3062             break;
3063         case 'q':
3064             quiet = true;
3065             break;
3066         case 'U':
3067             force_share = true;
3068             break;
3069         case OPTION_OBJECT: {
3070             QemuOpts *opts;
3071             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3072                                            optarg, true);
3073             if (!opts) {
3074                 return 1;
3075             }
3076         }   break;
3077         case OPTION_IMAGE_OPTS:
3078             image_opts = true;
3079             break;
3080         }
3081     }
3082 
3083     if (optind != argc - 1) {
3084         error_exit("Expecting one image file name");
3085     }
3086     filename = argv[optind++];
3087 
3088     if (qemu_opts_foreach(&qemu_object_opts,
3089                           user_creatable_add_opts_foreach,
3090                           NULL, &error_fatal)) {
3091         return 1;
3092     }
3093 
3094     /* Open the image */
3095     blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3096                    force_share);
3097     if (!blk) {
3098         return 1;
3099     }
3100     bs = blk_bs(blk);
3101 
3102     /* Perform the requested action */
3103     switch(action) {
3104     case SNAPSHOT_LIST:
3105         dump_snapshots(bs);
3106         break;
3107 
3108     case SNAPSHOT_CREATE:
3109         memset(&sn, 0, sizeof(sn));
3110         pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3111 
3112         qemu_gettimeofday(&tv);
3113         sn.date_sec = tv.tv_sec;
3114         sn.date_nsec = tv.tv_usec * 1000;
3115 
3116         ret = bdrv_snapshot_create(bs, &sn);
3117         if (ret) {
3118             error_report("Could not create snapshot '%s': %d (%s)",
3119                 snapshot_name, ret, strerror(-ret));
3120         }
3121         break;
3122 
3123     case SNAPSHOT_APPLY:
3124         ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
3125         if (ret) {
3126             error_reportf_err(err, "Could not apply snapshot '%s': ",
3127                               snapshot_name);
3128         }
3129         break;
3130 
3131     case SNAPSHOT_DELETE:
3132         ret = bdrv_snapshot_find(bs, &sn, snapshot_name);
3133         if (ret < 0) {
3134             error_report("Could not delete snapshot '%s': snapshot not "
3135                          "found", snapshot_name);
3136             ret = 1;
3137         } else {
3138             ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err);
3139             if (ret < 0) {
3140                 error_reportf_err(err, "Could not delete snapshot '%s': ",
3141                                   snapshot_name);
3142                 ret = 1;
3143             }
3144         }
3145         break;
3146     }
3147 
3148     /* Cleanup */
3149     blk_unref(blk);
3150     if (ret) {
3151         return 1;
3152     }
3153     return 0;
3154 }
3155 
3156 static int img_rebase(int argc, char **argv)
3157 {
3158     BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
3159     uint8_t *buf_old = NULL;
3160     uint8_t *buf_new = NULL;
3161     BlockDriverState *bs = NULL;
3162     char *filename;
3163     const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3164     int c, flags, src_flags, ret;
3165     bool writethrough, src_writethrough;
3166     int unsafe = 0;
3167     bool force_share = false;
3168     int progress = 0;
3169     bool quiet = false;
3170     Error *local_err = NULL;
3171     bool image_opts = false;
3172 
3173     /* Parse commandline parameters */
3174     fmt = NULL;
3175     cache = BDRV_DEFAULT_CACHE;
3176     src_cache = BDRV_DEFAULT_CACHE;
3177     out_baseimg = NULL;
3178     out_basefmt = NULL;
3179     for(;;) {
3180         static const struct option long_options[] = {
3181             {"help", no_argument, 0, 'h'},
3182             {"object", required_argument, 0, OPTION_OBJECT},
3183             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3184             {"force-share", no_argument, 0, 'U'},
3185             {0, 0, 0, 0}
3186         };
3187         c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
3188                         long_options, NULL);
3189         if (c == -1) {
3190             break;
3191         }
3192         switch(c) {
3193         case ':':
3194             missing_argument(argv[optind - 1]);
3195             break;
3196         case '?':
3197             unrecognized_option(argv[optind - 1]);
3198             break;
3199         case 'h':
3200             help();
3201             return 0;
3202         case 'f':
3203             fmt = optarg;
3204             break;
3205         case 'F':
3206             out_basefmt = optarg;
3207             break;
3208         case 'b':
3209             out_baseimg = optarg;
3210             break;
3211         case 'u':
3212             unsafe = 1;
3213             break;
3214         case 'p':
3215             progress = 1;
3216             break;
3217         case 't':
3218             cache = optarg;
3219             break;
3220         case 'T':
3221             src_cache = optarg;
3222             break;
3223         case 'q':
3224             quiet = true;
3225             break;
3226         case OPTION_OBJECT: {
3227             QemuOpts *opts;
3228             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3229                                            optarg, true);
3230             if (!opts) {
3231                 return 1;
3232             }
3233         }   break;
3234         case OPTION_IMAGE_OPTS:
3235             image_opts = true;
3236             break;
3237         case 'U':
3238             force_share = true;
3239             break;
3240         }
3241     }
3242 
3243     if (quiet) {
3244         progress = 0;
3245     }
3246 
3247     if (optind != argc - 1) {
3248         error_exit("Expecting one image file name");
3249     }
3250     if (!unsafe && !out_baseimg) {
3251         error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3252     }
3253     filename = argv[optind++];
3254 
3255     if (qemu_opts_foreach(&qemu_object_opts,
3256                           user_creatable_add_opts_foreach,
3257                           NULL, &error_fatal)) {
3258         return 1;
3259     }
3260 
3261     qemu_progress_init(progress, 2.0);
3262     qemu_progress_print(0, 100);
3263 
3264     flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
3265     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3266     if (ret < 0) {
3267         error_report("Invalid cache option: %s", cache);
3268         goto out;
3269     }
3270 
3271     src_flags = 0;
3272     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
3273     if (ret < 0) {
3274         error_report("Invalid source cache option: %s", src_cache);
3275         goto out;
3276     }
3277 
3278     /* The source files are opened read-only, don't care about WCE */
3279     assert((src_flags & BDRV_O_RDWR) == 0);
3280     (void) src_writethrough;
3281 
3282     /*
3283      * Open the images.
3284      *
3285      * Ignore the old backing file for unsafe rebase in case we want to correct
3286      * the reference to a renamed or moved backing file.
3287      */
3288     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3289                    false);
3290     if (!blk) {
3291         ret = -1;
3292         goto out;
3293     }
3294     bs = blk_bs(blk);
3295 
3296     if (out_basefmt != NULL) {
3297         if (bdrv_find_format(out_basefmt) == NULL) {
3298             error_report("Invalid format name: '%s'", out_basefmt);
3299             ret = -1;
3300             goto out;
3301         }
3302     }
3303 
3304     /* For safe rebasing we need to compare old and new backing file */
3305     if (!unsafe) {
3306         char backing_name[PATH_MAX];
3307         QDict *options = NULL;
3308 
3309         if (bs->backing_format[0] != '\0') {
3310             options = qdict_new();
3311             qdict_put_str(options, "driver", bs->backing_format);
3312         }
3313 
3314         if (force_share) {
3315             if (!options) {
3316                 options = qdict_new();
3317             }
3318             qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3319         }
3320         bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3321         blk_old_backing = blk_new_open(backing_name, NULL,
3322                                        options, src_flags, &local_err);
3323         if (!blk_old_backing) {
3324             error_reportf_err(local_err,
3325                               "Could not open old backing file '%s': ",
3326                               backing_name);
3327             ret = -1;
3328             goto out;
3329         }
3330 
3331         if (out_baseimg[0]) {
3332             const char *overlay_filename;
3333             char *out_real_path;
3334 
3335             options = qdict_new();
3336             if (out_basefmt) {
3337                 qdict_put_str(options, "driver", out_basefmt);
3338             }
3339             if (force_share) {
3340                 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3341             }
3342 
3343             bdrv_refresh_filename(bs);
3344             overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3345                                                      : bs->filename;
3346             out_real_path = g_malloc(PATH_MAX);
3347 
3348             bdrv_get_full_backing_filename_from_filename(overlay_filename,
3349                                                          out_baseimg,
3350                                                          out_real_path,
3351                                                          PATH_MAX,
3352                                                          &local_err);
3353             if (local_err) {
3354                 error_reportf_err(local_err,
3355                                   "Could not resolve backing filename: ");
3356                 ret = -1;
3357                 g_free(out_real_path);
3358                 goto out;
3359             }
3360 
3361             blk_new_backing = blk_new_open(out_real_path, NULL,
3362                                            options, src_flags, &local_err);
3363             g_free(out_real_path);
3364             if (!blk_new_backing) {
3365                 error_reportf_err(local_err,
3366                                   "Could not open new backing file '%s': ",
3367                                   out_baseimg);
3368                 ret = -1;
3369                 goto out;
3370             }
3371         }
3372     }
3373 
3374     /*
3375      * Check each unallocated cluster in the COW file. If it is unallocated,
3376      * accesses go to the backing file. We must therefore compare this cluster
3377      * in the old and new backing file, and if they differ we need to copy it
3378      * from the old backing file into the COW file.
3379      *
3380      * If qemu-img crashes during this step, no harm is done. The content of
3381      * the image is the same as the original one at any time.
3382      */
3383     if (!unsafe) {
3384         int64_t size;
3385         int64_t old_backing_size;
3386         int64_t new_backing_size = 0;
3387         uint64_t offset;
3388         int64_t n;
3389         float local_progress = 0;
3390 
3391         buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3392         buf_new = blk_blockalign(blk, IO_BUF_SIZE);
3393 
3394         size = blk_getlength(blk);
3395         if (size < 0) {
3396             error_report("Could not get size of '%s': %s",
3397                          filename, strerror(-size));
3398             ret = -1;
3399             goto out;
3400         }
3401         old_backing_size = blk_getlength(blk_old_backing);
3402         if (old_backing_size < 0) {
3403             char backing_name[PATH_MAX];
3404 
3405             bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
3406             error_report("Could not get size of '%s': %s",
3407                          backing_name, strerror(-old_backing_size));
3408             ret = -1;
3409             goto out;
3410         }
3411         if (blk_new_backing) {
3412             new_backing_size = blk_getlength(blk_new_backing);
3413             if (new_backing_size < 0) {
3414                 error_report("Could not get size of '%s': %s",
3415                              out_baseimg, strerror(-new_backing_size));
3416                 ret = -1;
3417                 goto out;
3418             }
3419         }
3420 
3421         if (size != 0) {
3422             local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
3423         }
3424 
3425         for (offset = 0; offset < size; offset += n) {
3426             /* How many bytes can we handle with the next read? */
3427             n = MIN(IO_BUF_SIZE, size - offset);
3428 
3429             /* If the cluster is allocated, we don't need to take action */
3430             ret = bdrv_is_allocated(bs, offset, n, &n);
3431             if (ret < 0) {
3432                 error_report("error while reading image metadata: %s",
3433                              strerror(-ret));
3434                 goto out;
3435             }
3436             if (ret) {
3437                 continue;
3438             }
3439 
3440             /*
3441              * Read old and new backing file and take into consideration that
3442              * backing files may be smaller than the COW image.
3443              */
3444             if (offset >= old_backing_size) {
3445                 memset(buf_old, 0, n);
3446             } else {
3447                 if (offset + n > old_backing_size) {
3448                     n = old_backing_size - offset;
3449                 }
3450 
3451                 ret = blk_pread(blk_old_backing, offset, buf_old, n);
3452                 if (ret < 0) {
3453                     error_report("error while reading from old backing file");
3454                     goto out;
3455                 }
3456             }
3457 
3458             if (offset >= new_backing_size || !blk_new_backing) {
3459                 memset(buf_new, 0, n);
3460             } else {
3461                 if (offset + n > new_backing_size) {
3462                     n = new_backing_size - offset;
3463                 }
3464 
3465                 ret = blk_pread(blk_new_backing, offset, buf_new, n);
3466                 if (ret < 0) {
3467                     error_report("error while reading from new backing file");
3468                     goto out;
3469                 }
3470             }
3471 
3472             /* If they differ, we need to write to the COW file */
3473             uint64_t written = 0;
3474 
3475             while (written < n) {
3476                 int64_t pnum;
3477 
3478                 if (compare_buffers(buf_old + written, buf_new + written,
3479                                     n - written, &pnum))
3480                 {
3481                     ret = blk_pwrite(blk, offset + written,
3482                                      buf_old + written, pnum, 0);
3483                     if (ret < 0) {
3484                         error_report("Error while writing to COW image: %s",
3485                             strerror(-ret));
3486                         goto out;
3487                     }
3488                 }
3489 
3490                 written += pnum;
3491             }
3492             qemu_progress_print(local_progress, 100);
3493         }
3494     }
3495 
3496     /*
3497      * Change the backing file. All clusters that are different from the old
3498      * backing file are overwritten in the COW file now, so the visible content
3499      * doesn't change when we switch the backing file.
3500      */
3501     if (out_baseimg && *out_baseimg) {
3502         ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
3503     } else {
3504         ret = bdrv_change_backing_file(bs, NULL, NULL);
3505     }
3506 
3507     if (ret == -ENOSPC) {
3508         error_report("Could not change the backing file to '%s': No "
3509                      "space left in the file header", out_baseimg);
3510     } else if (ret < 0) {
3511         error_report("Could not change the backing file to '%s': %s",
3512             out_baseimg, strerror(-ret));
3513     }
3514 
3515     qemu_progress_print(100, 0);
3516     /*
3517      * TODO At this point it is possible to check if any clusters that are
3518      * allocated in the COW file are the same in the backing file. If so, they
3519      * could be dropped from the COW file. Don't do this before switching the
3520      * backing file, in case of a crash this would lead to corruption.
3521      */
3522 out:
3523     qemu_progress_end();
3524     /* Cleanup */
3525     if (!unsafe) {
3526         blk_unref(blk_old_backing);
3527         blk_unref(blk_new_backing);
3528     }
3529     qemu_vfree(buf_old);
3530     qemu_vfree(buf_new);
3531 
3532     blk_unref(blk);
3533     if (ret) {
3534         return 1;
3535     }
3536     return 0;
3537 }
3538 
3539 static int img_resize(int argc, char **argv)
3540 {
3541     Error *err = NULL;
3542     int c, ret, relative;
3543     const char *filename, *fmt, *size;
3544     int64_t n, total_size, current_size, new_size;
3545     bool quiet = false;
3546     BlockBackend *blk = NULL;
3547     PreallocMode prealloc = PREALLOC_MODE_OFF;
3548     QemuOpts *param;
3549 
3550     static QemuOptsList resize_options = {
3551         .name = "resize_options",
3552         .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3553         .desc = {
3554             {
3555                 .name = BLOCK_OPT_SIZE,
3556                 .type = QEMU_OPT_SIZE,
3557                 .help = "Virtual disk size"
3558             }, {
3559                 /* end of list */
3560             }
3561         },
3562     };
3563     bool image_opts = false;
3564     bool shrink = false;
3565 
3566     /* Remove size from argv manually so that negative numbers are not treated
3567      * as options by getopt. */
3568     if (argc < 3) {
3569         error_exit("Not enough arguments");
3570         return 1;
3571     }
3572 
3573     size = argv[--argc];
3574 
3575     /* Parse getopt arguments */
3576     fmt = NULL;
3577     for(;;) {
3578         static const struct option long_options[] = {
3579             {"help", no_argument, 0, 'h'},
3580             {"object", required_argument, 0, OPTION_OBJECT},
3581             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3582             {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
3583             {"shrink", no_argument, 0, OPTION_SHRINK},
3584             {0, 0, 0, 0}
3585         };
3586         c = getopt_long(argc, argv, ":f:hq",
3587                         long_options, NULL);
3588         if (c == -1) {
3589             break;
3590         }
3591         switch(c) {
3592         case ':':
3593             missing_argument(argv[optind - 1]);
3594             break;
3595         case '?':
3596             unrecognized_option(argv[optind - 1]);
3597             break;
3598         case 'h':
3599             help();
3600             break;
3601         case 'f':
3602             fmt = optarg;
3603             break;
3604         case 'q':
3605             quiet = true;
3606             break;
3607         case OPTION_OBJECT: {
3608             QemuOpts *opts;
3609             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3610                                            optarg, true);
3611             if (!opts) {
3612                 return 1;
3613             }
3614         }   break;
3615         case OPTION_IMAGE_OPTS:
3616             image_opts = true;
3617             break;
3618         case OPTION_PREALLOCATION:
3619             prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
3620                                        PREALLOC_MODE__MAX, NULL);
3621             if (prealloc == PREALLOC_MODE__MAX) {
3622                 error_report("Invalid preallocation mode '%s'", optarg);
3623                 return 1;
3624             }
3625             break;
3626         case OPTION_SHRINK:
3627             shrink = true;
3628             break;
3629         }
3630     }
3631     if (optind != argc - 1) {
3632         error_exit("Expecting image file name and size");
3633     }
3634     filename = argv[optind++];
3635 
3636     if (qemu_opts_foreach(&qemu_object_opts,
3637                           user_creatable_add_opts_foreach,
3638                           NULL, &error_fatal)) {
3639         return 1;
3640     }
3641 
3642     /* Choose grow, shrink, or absolute resize mode */
3643     switch (size[0]) {
3644     case '+':
3645         relative = 1;
3646         size++;
3647         break;
3648     case '-':
3649         relative = -1;
3650         size++;
3651         break;
3652     default:
3653         relative = 0;
3654         break;
3655     }
3656 
3657     /* Parse size */
3658     param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
3659     qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err);
3660     if (err) {
3661         error_report_err(err);
3662         ret = -1;
3663         qemu_opts_del(param);
3664         goto out;
3665     }
3666     n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
3667     qemu_opts_del(param);
3668 
3669     blk = img_open(image_opts, filename, fmt,
3670                    BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
3671                    false);
3672     if (!blk) {
3673         ret = -1;
3674         goto out;
3675     }
3676 
3677     current_size = blk_getlength(blk);
3678     if (current_size < 0) {
3679         error_report("Failed to inquire current image length: %s",
3680                      strerror(-current_size));
3681         ret = -1;
3682         goto out;
3683     }
3684 
3685     if (relative) {
3686         total_size = current_size + n * relative;
3687     } else {
3688         total_size = n;
3689     }
3690     if (total_size <= 0) {
3691         error_report("New image size must be positive");
3692         ret = -1;
3693         goto out;
3694     }
3695 
3696     if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
3697         error_report("Preallocation can only be used for growing images");
3698         ret = -1;
3699         goto out;
3700     }
3701 
3702     if (total_size < current_size && !shrink) {
3703         warn_report("Shrinking an image will delete all data beyond the "
3704                     "shrunken image's end. Before performing such an "
3705                     "operation, make sure there is no important data there.");
3706 
3707         if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) {
3708             error_report(
3709               "Use the --shrink option to perform a shrink operation.");
3710             ret = -1;
3711             goto out;
3712         } else {
3713             warn_report("Using the --shrink option will suppress this message. "
3714                         "Note that future versions of qemu-img may refuse to "
3715                         "shrink images without this option.");
3716         }
3717     }
3718 
3719     ret = blk_truncate(blk, total_size, prealloc, &err);
3720     if (ret < 0) {
3721         error_report_err(err);
3722         goto out;
3723     }
3724 
3725     new_size = blk_getlength(blk);
3726     if (new_size < 0) {
3727         error_report("Failed to verify truncated image length: %s",
3728                      strerror(-new_size));
3729         ret = -1;
3730         goto out;
3731     }
3732 
3733     /* Some block drivers implement a truncation method, but only so
3734      * the user can cause qemu to refresh the image's size from disk.
3735      * The idea is that the user resizes the image outside of qemu and
3736      * then invokes block_resize to inform qemu about it.
3737      * (This includes iscsi and file-posix for device files.)
3738      * Of course, that is not the behavior someone invoking
3739      * qemu-img resize would find useful, so we catch that behavior
3740      * here and tell the user. */
3741     if (new_size != total_size && new_size == current_size) {
3742         error_report("Image was not resized; resizing may not be supported "
3743                      "for this image");
3744         ret = -1;
3745         goto out;
3746     }
3747 
3748     if (new_size != total_size) {
3749         warn_report("Image should have been resized to %" PRIi64
3750                     " bytes, but was resized to %" PRIi64 " bytes",
3751                     total_size, new_size);
3752     }
3753 
3754     qprintf(quiet, "Image resized.\n");
3755 
3756 out:
3757     blk_unref(blk);
3758     if (ret) {
3759         return 1;
3760     }
3761     return 0;
3762 }
3763 
3764 static void amend_status_cb(BlockDriverState *bs,
3765                             int64_t offset, int64_t total_work_size,
3766                             void *opaque)
3767 {
3768     qemu_progress_print(100.f * offset / total_work_size, 0);
3769 }
3770 
3771 static int print_amend_option_help(const char *format)
3772 {
3773     BlockDriver *drv;
3774 
3775     /* Find driver and parse its options */
3776     drv = bdrv_find_format(format);
3777     if (!drv) {
3778         error_report("Unknown file format '%s'", format);
3779         return 1;
3780     }
3781 
3782     if (!drv->bdrv_amend_options) {
3783         error_report("Format driver '%s' does not support option amendment",
3784                      format);
3785         return 1;
3786     }
3787 
3788     /* Every driver supporting amendment must have create_opts */
3789     assert(drv->create_opts);
3790 
3791     printf("Creation options for '%s':\n", format);
3792     qemu_opts_print_help(drv->create_opts, false);
3793     printf("\nNote that not all of these options may be amendable.\n");
3794     return 0;
3795 }
3796 
3797 static int img_amend(int argc, char **argv)
3798 {
3799     Error *err = NULL;
3800     int c, ret = 0;
3801     char *options = NULL;
3802     QemuOptsList *create_opts = NULL;
3803     QemuOpts *opts = NULL;
3804     const char *fmt = NULL, *filename, *cache;
3805     int flags;
3806     bool writethrough;
3807     bool quiet = false, progress = false;
3808     BlockBackend *blk = NULL;
3809     BlockDriverState *bs = NULL;
3810     bool image_opts = false;
3811 
3812     cache = BDRV_DEFAULT_CACHE;
3813     for (;;) {
3814         static const struct option long_options[] = {
3815             {"help", no_argument, 0, 'h'},
3816             {"object", required_argument, 0, OPTION_OBJECT},
3817             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3818             {0, 0, 0, 0}
3819         };
3820         c = getopt_long(argc, argv, ":ho:f:t:pq",
3821                         long_options, NULL);
3822         if (c == -1) {
3823             break;
3824         }
3825 
3826         switch (c) {
3827         case ':':
3828             missing_argument(argv[optind - 1]);
3829             break;
3830         case '?':
3831             unrecognized_option(argv[optind - 1]);
3832             break;
3833         case 'h':
3834             help();
3835             break;
3836         case 'o':
3837             if (!is_valid_option_list(optarg)) {
3838                 error_report("Invalid option list: %s", optarg);
3839                 ret = -1;
3840                 goto out_no_progress;
3841             }
3842             if (!options) {
3843                 options = g_strdup(optarg);
3844             } else {
3845                 char *old_options = options;
3846                 options = g_strdup_printf("%s,%s", options, optarg);
3847                 g_free(old_options);
3848             }
3849             break;
3850         case 'f':
3851             fmt = optarg;
3852             break;
3853         case 't':
3854             cache = optarg;
3855             break;
3856         case 'p':
3857             progress = true;
3858             break;
3859         case 'q':
3860             quiet = true;
3861             break;
3862         case OPTION_OBJECT:
3863             opts = qemu_opts_parse_noisily(&qemu_object_opts,
3864                                            optarg, true);
3865             if (!opts) {
3866                 ret = -1;
3867                 goto out_no_progress;
3868             }
3869             break;
3870         case OPTION_IMAGE_OPTS:
3871             image_opts = true;
3872             break;
3873         }
3874     }
3875 
3876     if (!options) {
3877         error_exit("Must specify options (-o)");
3878     }
3879 
3880     if (qemu_opts_foreach(&qemu_object_opts,
3881                           user_creatable_add_opts_foreach,
3882                           NULL, &error_fatal)) {
3883         ret = -1;
3884         goto out_no_progress;
3885     }
3886 
3887     if (quiet) {
3888         progress = false;
3889     }
3890     qemu_progress_init(progress, 1.0);
3891 
3892     filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
3893     if (fmt && has_help_option(options)) {
3894         /* If a format is explicitly specified (and possibly no filename is
3895          * given), print option help here */
3896         ret = print_amend_option_help(fmt);
3897         goto out;
3898     }
3899 
3900     if (optind != argc - 1) {
3901         error_report("Expecting one image file name");
3902         ret = -1;
3903         goto out;
3904     }
3905 
3906     flags = BDRV_O_RDWR;
3907     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3908     if (ret < 0) {
3909         error_report("Invalid cache option: %s", cache);
3910         goto out;
3911     }
3912 
3913     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3914                    false);
3915     if (!blk) {
3916         ret = -1;
3917         goto out;
3918     }
3919     bs = blk_bs(blk);
3920 
3921     fmt = bs->drv->format_name;
3922 
3923     if (has_help_option(options)) {
3924         /* If the format was auto-detected, print option help here */
3925         ret = print_amend_option_help(fmt);
3926         goto out;
3927     }
3928 
3929     if (!bs->drv->bdrv_amend_options) {
3930         error_report("Format driver '%s' does not support option amendment",
3931                      fmt);
3932         ret = -1;
3933         goto out;
3934     }
3935 
3936     /* Every driver supporting amendment must have create_opts */
3937     assert(bs->drv->create_opts);
3938 
3939     create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
3940     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
3941     qemu_opts_do_parse(opts, options, NULL, &err);
3942     if (err) {
3943         error_report_err(err);
3944         ret = -1;
3945         goto out;
3946     }
3947 
3948     /* In case the driver does not call amend_status_cb() */
3949     qemu_progress_print(0.f, 0);
3950     ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err);
3951     qemu_progress_print(100.f, 0);
3952     if (ret < 0) {
3953         error_report_err(err);
3954         goto out;
3955     }
3956 
3957 out:
3958     qemu_progress_end();
3959 
3960 out_no_progress:
3961     blk_unref(blk);
3962     qemu_opts_del(opts);
3963     qemu_opts_free(create_opts);
3964     g_free(options);
3965 
3966     if (ret) {
3967         return 1;
3968     }
3969     return 0;
3970 }
3971 
3972 typedef struct BenchData {
3973     BlockBackend *blk;
3974     uint64_t image_size;
3975     bool write;
3976     int bufsize;
3977     int step;
3978     int nrreq;
3979     int n;
3980     int flush_interval;
3981     bool drain_on_flush;
3982     uint8_t *buf;
3983     QEMUIOVector *qiov;
3984 
3985     int in_flight;
3986     bool in_flush;
3987     uint64_t offset;
3988 } BenchData;
3989 
3990 static void bench_undrained_flush_cb(void *opaque, int ret)
3991 {
3992     if (ret < 0) {
3993         error_report("Failed flush request: %s", strerror(-ret));
3994         exit(EXIT_FAILURE);
3995     }
3996 }
3997 
3998 static void bench_cb(void *opaque, int ret)
3999 {
4000     BenchData *b = opaque;
4001     BlockAIOCB *acb;
4002 
4003     if (ret < 0) {
4004         error_report("Failed request: %s", strerror(-ret));
4005         exit(EXIT_FAILURE);
4006     }
4007 
4008     if (b->in_flush) {
4009         /* Just finished a flush with drained queue: Start next requests */
4010         assert(b->in_flight == 0);
4011         b->in_flush = false;
4012     } else if (b->in_flight > 0) {
4013         int remaining = b->n - b->in_flight;
4014 
4015         b->n--;
4016         b->in_flight--;
4017 
4018         /* Time for flush? Drain queue if requested, then flush */
4019         if (b->flush_interval && remaining % b->flush_interval == 0) {
4020             if (!b->in_flight || !b->drain_on_flush) {
4021                 BlockCompletionFunc *cb;
4022 
4023                 if (b->drain_on_flush) {
4024                     b->in_flush = true;
4025                     cb = bench_cb;
4026                 } else {
4027                     cb = bench_undrained_flush_cb;
4028                 }
4029 
4030                 acb = blk_aio_flush(b->blk, cb, b);
4031                 if (!acb) {
4032                     error_report("Failed to issue flush request");
4033                     exit(EXIT_FAILURE);
4034                 }
4035             }
4036             if (b->drain_on_flush) {
4037                 return;
4038             }
4039         }
4040     }
4041 
4042     while (b->n > b->in_flight && b->in_flight < b->nrreq) {
4043         int64_t offset = b->offset;
4044         /* blk_aio_* might look for completed I/Os and kick bench_cb
4045          * again, so make sure this operation is counted by in_flight
4046          * and b->offset is ready for the next submission.
4047          */
4048         b->in_flight++;
4049         b->offset += b->step;
4050         b->offset %= b->image_size;
4051         if (b->write) {
4052             acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
4053         } else {
4054             acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
4055         }
4056         if (!acb) {
4057             error_report("Failed to issue request");
4058             exit(EXIT_FAILURE);
4059         }
4060     }
4061 }
4062 
4063 static int img_bench(int argc, char **argv)
4064 {
4065     int c, ret = 0;
4066     const char *fmt = NULL, *filename;
4067     bool quiet = false;
4068     bool image_opts = false;
4069     bool is_write = false;
4070     int count = 75000;
4071     int depth = 64;
4072     int64_t offset = 0;
4073     size_t bufsize = 4096;
4074     int pattern = 0;
4075     size_t step = 0;
4076     int flush_interval = 0;
4077     bool drain_on_flush = true;
4078     int64_t image_size;
4079     BlockBackend *blk = NULL;
4080     BenchData data = {};
4081     int flags = 0;
4082     bool writethrough = false;
4083     struct timeval t1, t2;
4084     int i;
4085     bool force_share = false;
4086     size_t buf_size;
4087 
4088     for (;;) {
4089         static const struct option long_options[] = {
4090             {"help", no_argument, 0, 'h'},
4091             {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
4092             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4093             {"pattern", required_argument, 0, OPTION_PATTERN},
4094             {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
4095             {"force-share", no_argument, 0, 'U'},
4096             {0, 0, 0, 0}
4097         };
4098         c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL);
4099         if (c == -1) {
4100             break;
4101         }
4102 
4103         switch (c) {
4104         case ':':
4105             missing_argument(argv[optind - 1]);
4106             break;
4107         case '?':
4108             unrecognized_option(argv[optind - 1]);
4109             break;
4110         case 'h':
4111             help();
4112             break;
4113         case 'c':
4114         {
4115             unsigned long res;
4116 
4117             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4118                 error_report("Invalid request count specified");
4119                 return 1;
4120             }
4121             count = res;
4122             break;
4123         }
4124         case 'd':
4125         {
4126             unsigned long res;
4127 
4128             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4129                 error_report("Invalid queue depth specified");
4130                 return 1;
4131             }
4132             depth = res;
4133             break;
4134         }
4135         case 'f':
4136             fmt = optarg;
4137             break;
4138         case 'n':
4139             flags |= BDRV_O_NATIVE_AIO;
4140             break;
4141         case 'o':
4142         {
4143             offset = cvtnum(optarg);
4144             if (offset < 0) {
4145                 error_report("Invalid offset specified");
4146                 return 1;
4147             }
4148             break;
4149         }
4150             break;
4151         case 'q':
4152             quiet = true;
4153             break;
4154         case 's':
4155         {
4156             int64_t sval;
4157 
4158             sval = cvtnum(optarg);
4159             if (sval < 0 || sval > INT_MAX) {
4160                 error_report("Invalid buffer size specified");
4161                 return 1;
4162             }
4163 
4164             bufsize = sval;
4165             break;
4166         }
4167         case 'S':
4168         {
4169             int64_t sval;
4170 
4171             sval = cvtnum(optarg);
4172             if (sval < 0 || sval > INT_MAX) {
4173                 error_report("Invalid step size specified");
4174                 return 1;
4175             }
4176 
4177             step = sval;
4178             break;
4179         }
4180         case 't':
4181             ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4182             if (ret < 0) {
4183                 error_report("Invalid cache mode");
4184                 ret = -1;
4185                 goto out;
4186             }
4187             break;
4188         case 'w':
4189             flags |= BDRV_O_RDWR;
4190             is_write = true;
4191             break;
4192         case 'U':
4193             force_share = true;
4194             break;
4195         case OPTION_PATTERN:
4196         {
4197             unsigned long res;
4198 
4199             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
4200                 error_report("Invalid pattern byte specified");
4201                 return 1;
4202             }
4203             pattern = res;
4204             break;
4205         }
4206         case OPTION_FLUSH_INTERVAL:
4207         {
4208             unsigned long res;
4209 
4210             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4211                 error_report("Invalid flush interval specified");
4212                 return 1;
4213             }
4214             flush_interval = res;
4215             break;
4216         }
4217         case OPTION_NO_DRAIN:
4218             drain_on_flush = false;
4219             break;
4220         case OPTION_IMAGE_OPTS:
4221             image_opts = true;
4222             break;
4223         }
4224     }
4225 
4226     if (optind != argc - 1) {
4227         error_exit("Expecting one image file name");
4228     }
4229     filename = argv[argc - 1];
4230 
4231     if (!is_write && flush_interval) {
4232         error_report("--flush-interval is only available in write tests");
4233         ret = -1;
4234         goto out;
4235     }
4236     if (flush_interval && flush_interval < depth) {
4237         error_report("Flush interval can't be smaller than depth");
4238         ret = -1;
4239         goto out;
4240     }
4241 
4242     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4243                    force_share);
4244     if (!blk) {
4245         ret = -1;
4246         goto out;
4247     }
4248 
4249     image_size = blk_getlength(blk);
4250     if (image_size < 0) {
4251         ret = image_size;
4252         goto out;
4253     }
4254 
4255     data = (BenchData) {
4256         .blk            = blk,
4257         .image_size     = image_size,
4258         .bufsize        = bufsize,
4259         .step           = step ?: bufsize,
4260         .nrreq          = depth,
4261         .n              = count,
4262         .offset         = offset,
4263         .write          = is_write,
4264         .flush_interval = flush_interval,
4265         .drain_on_flush = drain_on_flush,
4266     };
4267     printf("Sending %d %s requests, %d bytes each, %d in parallel "
4268            "(starting at offset %" PRId64 ", step size %d)\n",
4269            data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
4270            data.offset, data.step);
4271     if (flush_interval) {
4272         printf("Sending flush every %d requests\n", flush_interval);
4273     }
4274 
4275     buf_size = data.nrreq * data.bufsize;
4276     data.buf = blk_blockalign(blk, buf_size);
4277     memset(data.buf, pattern, data.nrreq * data.bufsize);
4278 
4279     blk_register_buf(blk, data.buf, buf_size);
4280 
4281     data.qiov = g_new(QEMUIOVector, data.nrreq);
4282     for (i = 0; i < data.nrreq; i++) {
4283         qemu_iovec_init(&data.qiov[i], 1);
4284         qemu_iovec_add(&data.qiov[i],
4285                        data.buf + i * data.bufsize, data.bufsize);
4286     }
4287 
4288     gettimeofday(&t1, NULL);
4289     bench_cb(&data, 0);
4290 
4291     while (data.n > 0) {
4292         main_loop_wait(false);
4293     }
4294     gettimeofday(&t2, NULL);
4295 
4296     printf("Run completed in %3.3f seconds.\n",
4297            (t2.tv_sec - t1.tv_sec)
4298            + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4299 
4300 out:
4301     if (data.buf) {
4302         blk_unregister_buf(blk, data.buf);
4303     }
4304     qemu_vfree(data.buf);
4305     blk_unref(blk);
4306 
4307     if (ret) {
4308         return 1;
4309     }
4310     return 0;
4311 }
4312 
4313 #define C_BS      01
4314 #define C_COUNT   02
4315 #define C_IF      04
4316 #define C_OF      010
4317 #define C_SKIP    020
4318 
4319 struct DdInfo {
4320     unsigned int flags;
4321     int64_t count;
4322 };
4323 
4324 struct DdIo {
4325     int bsz;    /* Block size */
4326     char *filename;
4327     uint8_t *buf;
4328     int64_t offset;
4329 };
4330 
4331 struct DdOpts {
4332     const char *name;
4333     int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4334     unsigned int flag;
4335 };
4336 
4337 static int img_dd_bs(const char *arg,
4338                      struct DdIo *in, struct DdIo *out,
4339                      struct DdInfo *dd)
4340 {
4341     int64_t res;
4342 
4343     res = cvtnum(arg);
4344 
4345     if (res <= 0 || res > INT_MAX) {
4346         error_report("invalid number: '%s'", arg);
4347         return 1;
4348     }
4349     in->bsz = out->bsz = res;
4350 
4351     return 0;
4352 }
4353 
4354 static int img_dd_count(const char *arg,
4355                         struct DdIo *in, struct DdIo *out,
4356                         struct DdInfo *dd)
4357 {
4358     dd->count = cvtnum(arg);
4359 
4360     if (dd->count < 0) {
4361         error_report("invalid number: '%s'", arg);
4362         return 1;
4363     }
4364 
4365     return 0;
4366 }
4367 
4368 static int img_dd_if(const char *arg,
4369                      struct DdIo *in, struct DdIo *out,
4370                      struct DdInfo *dd)
4371 {
4372     in->filename = g_strdup(arg);
4373 
4374     return 0;
4375 }
4376 
4377 static int img_dd_of(const char *arg,
4378                      struct DdIo *in, struct DdIo *out,
4379                      struct DdInfo *dd)
4380 {
4381     out->filename = g_strdup(arg);
4382 
4383     return 0;
4384 }
4385 
4386 static int img_dd_skip(const char *arg,
4387                        struct DdIo *in, struct DdIo *out,
4388                        struct DdInfo *dd)
4389 {
4390     in->offset = cvtnum(arg);
4391 
4392     if (in->offset < 0) {
4393         error_report("invalid number: '%s'", arg);
4394         return 1;
4395     }
4396 
4397     return 0;
4398 }
4399 
4400 static int img_dd(int argc, char **argv)
4401 {
4402     int ret = 0;
4403     char *arg = NULL;
4404     char *tmp;
4405     BlockDriver *drv = NULL, *proto_drv = NULL;
4406     BlockBackend *blk1 = NULL, *blk2 = NULL;
4407     QemuOpts *opts = NULL;
4408     QemuOptsList *create_opts = NULL;
4409     Error *local_err = NULL;
4410     bool image_opts = false;
4411     int c, i;
4412     const char *out_fmt = "raw";
4413     const char *fmt = NULL;
4414     int64_t size = 0;
4415     int64_t block_count = 0, out_pos, in_pos;
4416     bool force_share = false;
4417     struct DdInfo dd = {
4418         .flags = 0,
4419         .count = 0,
4420     };
4421     struct DdIo in = {
4422         .bsz = 512, /* Block size is by default 512 bytes */
4423         .filename = NULL,
4424         .buf = NULL,
4425         .offset = 0
4426     };
4427     struct DdIo out = {
4428         .bsz = 512,
4429         .filename = NULL,
4430         .buf = NULL,
4431         .offset = 0
4432     };
4433 
4434     const struct DdOpts options[] = {
4435         { "bs", img_dd_bs, C_BS },
4436         { "count", img_dd_count, C_COUNT },
4437         { "if", img_dd_if, C_IF },
4438         { "of", img_dd_of, C_OF },
4439         { "skip", img_dd_skip, C_SKIP },
4440         { NULL, NULL, 0 }
4441     };
4442     const struct option long_options[] = {
4443         { "help", no_argument, 0, 'h'},
4444         { "object", required_argument, 0, OPTION_OBJECT},
4445         { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4446         { "force-share", no_argument, 0, 'U'},
4447         { 0, 0, 0, 0 }
4448     };
4449 
4450     while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
4451         if (c == EOF) {
4452             break;
4453         }
4454         switch (c) {
4455         case 'O':
4456             out_fmt = optarg;
4457             break;
4458         case 'f':
4459             fmt = optarg;
4460             break;
4461         case ':':
4462             missing_argument(argv[optind - 1]);
4463             break;
4464         case '?':
4465             unrecognized_option(argv[optind - 1]);
4466             break;
4467         case 'h':
4468             help();
4469             break;
4470         case 'U':
4471             force_share = true;
4472             break;
4473         case OPTION_OBJECT:
4474             if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
4475                 ret = -1;
4476                 goto out;
4477             }
4478             break;
4479         case OPTION_IMAGE_OPTS:
4480             image_opts = true;
4481             break;
4482         }
4483     }
4484 
4485     for (i = optind; i < argc; i++) {
4486         int j;
4487         arg = g_strdup(argv[i]);
4488 
4489         tmp = strchr(arg, '=');
4490         if (tmp == NULL) {
4491             error_report("unrecognized operand %s", arg);
4492             ret = -1;
4493             goto out;
4494         }
4495 
4496         *tmp++ = '\0';
4497 
4498         for (j = 0; options[j].name != NULL; j++) {
4499             if (!strcmp(arg, options[j].name)) {
4500                 break;
4501             }
4502         }
4503         if (options[j].name == NULL) {
4504             error_report("unrecognized operand %s", arg);
4505             ret = -1;
4506             goto out;
4507         }
4508 
4509         if (options[j].f(tmp, &in, &out, &dd) != 0) {
4510             ret = -1;
4511             goto out;
4512         }
4513         dd.flags |= options[j].flag;
4514         g_free(arg);
4515         arg = NULL;
4516     }
4517 
4518     if (!(dd.flags & C_IF && dd.flags & C_OF)) {
4519         error_report("Must specify both input and output files");
4520         ret = -1;
4521         goto out;
4522     }
4523 
4524     if (qemu_opts_foreach(&qemu_object_opts,
4525                           user_creatable_add_opts_foreach,
4526                           NULL, &error_fatal)) {
4527         ret = -1;
4528         goto out;
4529     }
4530 
4531     blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
4532                     force_share);
4533 
4534     if (!blk1) {
4535         ret = -1;
4536         goto out;
4537     }
4538 
4539     drv = bdrv_find_format(out_fmt);
4540     if (!drv) {
4541         error_report("Unknown file format");
4542         ret = -1;
4543         goto out;
4544     }
4545     proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
4546 
4547     if (!proto_drv) {
4548         error_report_err(local_err);
4549         ret = -1;
4550         goto out;
4551     }
4552     if (!drv->create_opts) {
4553         error_report("Format driver '%s' does not support image creation",
4554                      drv->format_name);
4555         ret = -1;
4556         goto out;
4557     }
4558     if (!proto_drv->create_opts) {
4559         error_report("Protocol driver '%s' does not support image creation",
4560                      proto_drv->format_name);
4561         ret = -1;
4562         goto out;
4563     }
4564     create_opts = qemu_opts_append(create_opts, drv->create_opts);
4565     create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
4566 
4567     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4568 
4569     size = blk_getlength(blk1);
4570     if (size < 0) {
4571         error_report("Failed to get size for '%s'", in.filename);
4572         ret = -1;
4573         goto out;
4574     }
4575 
4576     if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
4577         dd.count * in.bsz < size) {
4578         size = dd.count * in.bsz;
4579     }
4580 
4581     /* Overflow means the specified offset is beyond input image's size */
4582     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4583                               size < in.bsz * in.offset)) {
4584         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
4585     } else {
4586         qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
4587                             size - in.bsz * in.offset, &error_abort);
4588     }
4589 
4590     ret = bdrv_create(drv, out.filename, opts, &local_err);
4591     if (ret < 0) {
4592         error_reportf_err(local_err,
4593                           "%s: error while creating output image: ",
4594                           out.filename);
4595         ret = -1;
4596         goto out;
4597     }
4598 
4599     /* TODO, we can't honour --image-opts for the target,
4600      * since it needs to be given in a format compatible
4601      * with the bdrv_create() call above which does not
4602      * support image-opts style.
4603      */
4604     blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
4605                          false, false, false);
4606 
4607     if (!blk2) {
4608         ret = -1;
4609         goto out;
4610     }
4611 
4612     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
4613                               size < in.offset * in.bsz)) {
4614         /* We give a warning if the skip option is bigger than the input
4615          * size and create an empty output disk image (i.e. like dd(1)).
4616          */
4617         error_report("%s: cannot skip to specified offset", in.filename);
4618         in_pos = size;
4619     } else {
4620         in_pos = in.offset * in.bsz;
4621     }
4622 
4623     in.buf = g_new(uint8_t, in.bsz);
4624 
4625     for (out_pos = 0; in_pos < size; block_count++) {
4626         int in_ret, out_ret;
4627 
4628         if (in_pos + in.bsz > size) {
4629             in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
4630         } else {
4631             in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
4632         }
4633         if (in_ret < 0) {
4634             error_report("error while reading from input image file: %s",
4635                          strerror(-in_ret));
4636             ret = -1;
4637             goto out;
4638         }
4639         in_pos += in_ret;
4640 
4641         out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
4642 
4643         if (out_ret < 0) {
4644             error_report("error while writing to output image file: %s",
4645                          strerror(-out_ret));
4646             ret = -1;
4647             goto out;
4648         }
4649         out_pos += out_ret;
4650     }
4651 
4652 out:
4653     g_free(arg);
4654     qemu_opts_del(opts);
4655     qemu_opts_free(create_opts);
4656     blk_unref(blk1);
4657     blk_unref(blk2);
4658     g_free(in.filename);
4659     g_free(out.filename);
4660     g_free(in.buf);
4661     g_free(out.buf);
4662 
4663     if (ret) {
4664         return 1;
4665     }
4666     return 0;
4667 }
4668 
4669 static void dump_json_block_measure_info(BlockMeasureInfo *info)
4670 {
4671     QString *str;
4672     QObject *obj;
4673     Visitor *v = qobject_output_visitor_new(&obj);
4674 
4675     visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
4676     visit_complete(v, &obj);
4677     str = qobject_to_json_pretty(obj);
4678     assert(str != NULL);
4679     printf("%s\n", qstring_get_str(str));
4680     qobject_unref(obj);
4681     visit_free(v);
4682     qobject_unref(str);
4683 }
4684 
4685 static int img_measure(int argc, char **argv)
4686 {
4687     static const struct option long_options[] = {
4688         {"help", no_argument, 0, 'h'},
4689         {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4690         {"object", required_argument, 0, OPTION_OBJECT},
4691         {"output", required_argument, 0, OPTION_OUTPUT},
4692         {"size", required_argument, 0, OPTION_SIZE},
4693         {"force-share", no_argument, 0, 'U'},
4694         {0, 0, 0, 0}
4695     };
4696     OutputFormat output_format = OFORMAT_HUMAN;
4697     BlockBackend *in_blk = NULL;
4698     BlockDriver *drv;
4699     const char *filename = NULL;
4700     const char *fmt = NULL;
4701     const char *out_fmt = "raw";
4702     char *options = NULL;
4703     char *snapshot_name = NULL;
4704     bool force_share = false;
4705     QemuOpts *opts = NULL;
4706     QemuOpts *object_opts = NULL;
4707     QemuOpts *sn_opts = NULL;
4708     QemuOptsList *create_opts = NULL;
4709     bool image_opts = false;
4710     uint64_t img_size = UINT64_MAX;
4711     BlockMeasureInfo *info = NULL;
4712     Error *local_err = NULL;
4713     int ret = 1;
4714     int c;
4715 
4716     while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
4717                             long_options, NULL)) != -1) {
4718         switch (c) {
4719         case '?':
4720         case 'h':
4721             help();
4722             break;
4723         case 'f':
4724             fmt = optarg;
4725             break;
4726         case 'O':
4727             out_fmt = optarg;
4728             break;
4729         case 'o':
4730             if (!is_valid_option_list(optarg)) {
4731                 error_report("Invalid option list: %s", optarg);
4732                 goto out;
4733             }
4734             if (!options) {
4735                 options = g_strdup(optarg);
4736             } else {
4737                 char *old_options = options;
4738                 options = g_strdup_printf("%s,%s", options, optarg);
4739                 g_free(old_options);
4740             }
4741             break;
4742         case 'l':
4743             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
4744                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
4745                                                   optarg, false);
4746                 if (!sn_opts) {
4747                     error_report("Failed in parsing snapshot param '%s'",
4748                                  optarg);
4749                     goto out;
4750                 }
4751             } else {
4752                 snapshot_name = optarg;
4753             }
4754             break;
4755         case 'U':
4756             force_share = true;
4757             break;
4758         case OPTION_OBJECT:
4759             object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
4760                                                   optarg, true);
4761             if (!object_opts) {
4762                 goto out;
4763             }
4764             break;
4765         case OPTION_IMAGE_OPTS:
4766             image_opts = true;
4767             break;
4768         case OPTION_OUTPUT:
4769             if (!strcmp(optarg, "json")) {
4770                 output_format = OFORMAT_JSON;
4771             } else if (!strcmp(optarg, "human")) {
4772                 output_format = OFORMAT_HUMAN;
4773             } else {
4774                 error_report("--output must be used with human or json "
4775                              "as argument.");
4776                 goto out;
4777             }
4778             break;
4779         case OPTION_SIZE:
4780         {
4781             int64_t sval;
4782 
4783             sval = cvtnum(optarg);
4784             if (sval < 0) {
4785                 if (sval == -ERANGE) {
4786                     error_report("Image size must be less than 8 EiB!");
4787                 } else {
4788                     error_report("Invalid image size specified! You may use "
4789                                  "k, M, G, T, P or E suffixes for ");
4790                     error_report("kilobytes, megabytes, gigabytes, terabytes, "
4791                                  "petabytes and exabytes.");
4792                 }
4793                 goto out;
4794             }
4795             img_size = (uint64_t)sval;
4796         }
4797         break;
4798         }
4799     }
4800 
4801     if (qemu_opts_foreach(&qemu_object_opts,
4802                           user_creatable_add_opts_foreach,
4803                           NULL, &error_fatal)) {
4804         goto out;
4805     }
4806 
4807     if (argc - optind > 1) {
4808         error_report("At most one filename argument is allowed.");
4809         goto out;
4810     } else if (argc - optind == 1) {
4811         filename = argv[optind];
4812     }
4813 
4814     if (!filename &&
4815         (object_opts || image_opts || fmt || snapshot_name || sn_opts)) {
4816         error_report("--object, --image-opts, -f, and -l "
4817                      "require a filename argument.");
4818         goto out;
4819     }
4820     if (filename && img_size != UINT64_MAX) {
4821         error_report("--size N cannot be used together with a filename.");
4822         goto out;
4823     }
4824     if (!filename && img_size == UINT64_MAX) {
4825         error_report("Either --size N or one filename must be specified.");
4826         goto out;
4827     }
4828 
4829     if (filename) {
4830         in_blk = img_open(image_opts, filename, fmt, 0,
4831                           false, false, force_share);
4832         if (!in_blk) {
4833             goto out;
4834         }
4835 
4836         if (sn_opts) {
4837             bdrv_snapshot_load_tmp(blk_bs(in_blk),
4838                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
4839                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
4840                     &local_err);
4841         } else if (snapshot_name != NULL) {
4842             bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
4843                     snapshot_name, &local_err);
4844         }
4845         if (local_err) {
4846             error_reportf_err(local_err, "Failed to load snapshot: ");
4847             goto out;
4848         }
4849     }
4850 
4851     drv = bdrv_find_format(out_fmt);
4852     if (!drv) {
4853         error_report("Unknown file format '%s'", out_fmt);
4854         goto out;
4855     }
4856     if (!drv->create_opts) {
4857         error_report("Format driver '%s' does not support image creation",
4858                      drv->format_name);
4859         goto out;
4860     }
4861 
4862     create_opts = qemu_opts_append(create_opts, drv->create_opts);
4863     create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
4864     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
4865     if (options) {
4866         qemu_opts_do_parse(opts, options, NULL, &local_err);
4867         if (local_err) {
4868             error_report_err(local_err);
4869             error_report("Invalid options for file format '%s'", out_fmt);
4870             goto out;
4871         }
4872     }
4873     if (img_size != UINT64_MAX) {
4874         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
4875     }
4876 
4877     info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
4878     if (local_err) {
4879         error_report_err(local_err);
4880         goto out;
4881     }
4882 
4883     if (output_format == OFORMAT_HUMAN) {
4884         printf("required size: %" PRIu64 "\n", info->required);
4885         printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
4886     } else {
4887         dump_json_block_measure_info(info);
4888     }
4889 
4890     ret = 0;
4891 
4892 out:
4893     qapi_free_BlockMeasureInfo(info);
4894     qemu_opts_del(object_opts);
4895     qemu_opts_del(opts);
4896     qemu_opts_del(sn_opts);
4897     qemu_opts_free(create_opts);
4898     g_free(options);
4899     blk_unref(in_blk);
4900     return ret;
4901 }
4902 
4903 static const img_cmd_t img_cmds[] = {
4904 #define DEF(option, callback, arg_string)        \
4905     { option, callback },
4906 #include "qemu-img-cmds.h"
4907 #undef DEF
4908     { NULL, NULL, },
4909 };
4910 
4911 int main(int argc, char **argv)
4912 {
4913     const img_cmd_t *cmd;
4914     const char *cmdname;
4915     Error *local_error = NULL;
4916     char *trace_file = NULL;
4917     int c;
4918     static const struct option long_options[] = {
4919         {"help", no_argument, 0, 'h'},
4920         {"version", no_argument, 0, 'V'},
4921         {"trace", required_argument, NULL, 'T'},
4922         {0, 0, 0, 0}
4923     };
4924 
4925 #ifdef CONFIG_POSIX
4926     signal(SIGPIPE, SIG_IGN);
4927 #endif
4928 
4929     module_call_init(MODULE_INIT_TRACE);
4930     error_set_progname(argv[0]);
4931     qemu_init_exec_dir(argv[0]);
4932 
4933     if (qemu_init_main_loop(&local_error)) {
4934         error_report_err(local_error);
4935         exit(EXIT_FAILURE);
4936     }
4937 
4938     qcrypto_init(&error_fatal);
4939 
4940     module_call_init(MODULE_INIT_QOM);
4941     bdrv_init();
4942     if (argc < 2) {
4943         error_exit("Not enough arguments");
4944     }
4945 
4946     qemu_add_opts(&qemu_object_opts);
4947     qemu_add_opts(&qemu_source_opts);
4948     qemu_add_opts(&qemu_trace_opts);
4949 
4950     while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
4951         switch (c) {
4952         case ':':
4953             missing_argument(argv[optind - 1]);
4954             return 0;
4955         case '?':
4956             unrecognized_option(argv[optind - 1]);
4957             return 0;
4958         case 'h':
4959             help();
4960             return 0;
4961         case 'V':
4962             printf(QEMU_IMG_VERSION);
4963             return 0;
4964         case 'T':
4965             g_free(trace_file);
4966             trace_file = trace_opt_parse(optarg);
4967             break;
4968         }
4969     }
4970 
4971     cmdname = argv[optind];
4972 
4973     /* reset getopt_long scanning */
4974     argc -= optind;
4975     if (argc < 1) {
4976         return 0;
4977     }
4978     argv += optind;
4979     qemu_reset_optind();
4980 
4981     if (!trace_init_backends()) {
4982         exit(1);
4983     }
4984     trace_init_file(trace_file);
4985     qemu_set_log(LOG_TRACE);
4986 
4987     /* find the command */
4988     for (cmd = img_cmds; cmd->name != NULL; cmd++) {
4989         if (!strcmp(cmdname, cmd->name)) {
4990             return cmd->handler(argc, argv);
4991         }
4992     }
4993 
4994     /* not found */
4995     error_exit("Command not found: %s", cmdname);
4996 }
4997