OSDN Git Service

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