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