OSDN Git Service

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