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