xref: /qemu/qemu-img.c (revision c04d0ab026201d21873a63f768cb69c4554dfec1)
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 @optarg safe for accumulate_options()?
239  * It is when multiple of them can be joined together separated by ','.
240  * To make that work, @optarg 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 *optarg)
248 {
249     size_t len = strlen(optarg);
250     size_t i;
251 
252     if (!optarg[0] || optarg[0] == ',') {
253         return false;
254     }
255 
256     for (i = len; i > 0 && optarg[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 *optarg)
266 {
267     char *new_options;
268 
269     if (!is_valid_option_list(optarg)) {
270         error_report("Invalid option list: %s", optarg);
271         return -1;
272     }
273 
274     if (!*options) {
275         *options = g_strdup(optarg);
276     } else {
277         new_options = g_strdup_printf("%s,%s", *options, optarg);
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 sector by sector. Returns 0 if the first
1278  * sector of each buffer matches, non-zero otherwise.
1279  *
1280  * pnum is set to the sector-aligned size of the buffer prefix that
1281  * has the same matching status as the first sector.
1282  */
1283 static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
1284                            int64_t bytes, int64_t *pnum)
1285 {
1286     bool res;
1287     int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
1288 
1289     assert(bytes > 0);
1290 
1291     res = !!memcmp(buf1, buf2, i);
1292     while (i < bytes) {
1293         int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE);
1294 
1295         if (!!memcmp(buf1 + i, buf2 + i, len) != res) {
1296             break;
1297         }
1298         i += len;
1299     }
1300 
1301     *pnum = i;
1302     return res;
1303 }
1304 
1305 #define IO_BUF_SIZE (2 * MiB)
1306 
1307 /*
1308  * Check if passed sectors are empty (not allocated or contain only 0 bytes)
1309  *
1310  * Intended for use by 'qemu-img compare': Returns 0 in case sectors are
1311  * filled with 0, 1 if sectors contain non-zero data (this is a comparison
1312  * failure), and 4 on error (the exit status for read errors), after emitting
1313  * an error message.
1314  *
1315  * @param blk:  BlockBackend for the image
1316  * @param offset: Starting offset to check
1317  * @param bytes: Number of bytes to check
1318  * @param filename: Name of disk file we are checking (logging purpose)
1319  * @param buffer: Allocated buffer for storing read data
1320  * @param quiet: Flag for quiet mode
1321  */
1322 static int check_empty_sectors(BlockBackend *blk, int64_t offset,
1323                                int64_t bytes, const char *filename,
1324                                uint8_t *buffer, bool quiet)
1325 {
1326     int ret = 0;
1327     int64_t idx;
1328 
1329     ret = blk_pread(blk, offset, bytes, buffer, 0);
1330     if (ret < 0) {
1331         error_report("Error while reading offset %" PRId64 " of %s: %s",
1332                      offset, filename, strerror(-ret));
1333         return 4;
1334     }
1335     idx = find_nonzero(buffer, bytes);
1336     if (idx >= 0) {
1337         qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1338                 offset + idx);
1339         return 1;
1340     }
1341 
1342     return 0;
1343 }
1344 
1345 /*
1346  * Compares two images. Exit codes:
1347  *
1348  * 0 - Images are identical or the requested help was printed
1349  * 1 - Images differ
1350  * >1 - Error occurred
1351  */
1352 static int img_compare(int argc, char **argv)
1353 {
1354     const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
1355     BlockBackend *blk1, *blk2;
1356     BlockDriverState *bs1, *bs2;
1357     int64_t total_size1, total_size2;
1358     uint8_t *buf1 = NULL, *buf2 = NULL;
1359     int64_t pnum1, pnum2;
1360     int allocated1, allocated2;
1361     int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
1362     bool progress = false, quiet = false, strict = false;
1363     int flags;
1364     bool writethrough;
1365     int64_t total_size;
1366     int64_t offset = 0;
1367     int64_t chunk;
1368     int c;
1369     uint64_t progress_base;
1370     bool image_opts = false;
1371     bool force_share = false;
1372 
1373     cache = BDRV_DEFAULT_CACHE;
1374     for (;;) {
1375         static const struct option long_options[] = {
1376             {"help", no_argument, 0, 'h'},
1377             {"object", required_argument, 0, OPTION_OBJECT},
1378             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
1379             {"force-share", no_argument, 0, 'U'},
1380             {0, 0, 0, 0}
1381         };
1382         c = getopt_long(argc, argv, ":hf:F:T:pqsU",
1383                         long_options, NULL);
1384         if (c == -1) {
1385             break;
1386         }
1387         switch (c) {
1388         case ':':
1389             missing_argument(argv[optind - 1]);
1390             break;
1391         case '?':
1392             unrecognized_option(argv[optind - 1]);
1393             break;
1394         case 'h':
1395             help();
1396             break;
1397         case 'f':
1398             fmt1 = optarg;
1399             break;
1400         case 'F':
1401             fmt2 = optarg;
1402             break;
1403         case 'T':
1404             cache = optarg;
1405             break;
1406         case 'p':
1407             progress = true;
1408             break;
1409         case 'q':
1410             quiet = true;
1411             break;
1412         case 's':
1413             strict = true;
1414             break;
1415         case 'U':
1416             force_share = true;
1417             break;
1418         case OPTION_OBJECT:
1419             {
1420                 Error *local_err = NULL;
1421 
1422                 if (!user_creatable_add_from_str(optarg, &local_err)) {
1423                     if (local_err) {
1424                         error_report_err(local_err);
1425                         exit(2);
1426                     } else {
1427                         /* Help was printed */
1428                         exit(EXIT_SUCCESS);
1429                     }
1430                 }
1431                 break;
1432             }
1433         case OPTION_IMAGE_OPTS:
1434             image_opts = true;
1435             break;
1436         }
1437     }
1438 
1439     /* Progress is not shown in Quiet mode */
1440     if (quiet) {
1441         progress = false;
1442     }
1443 
1444 
1445     if (optind != argc - 2) {
1446         error_exit("Expecting two image file names");
1447     }
1448     filename1 = argv[optind++];
1449     filename2 = argv[optind++];
1450 
1451     /* Initialize before goto out */
1452     qemu_progress_init(progress, 2.0);
1453 
1454     flags = 0;
1455     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
1456     if (ret < 0) {
1457         error_report("Invalid source cache option: %s", cache);
1458         ret = 2;
1459         goto out3;
1460     }
1461 
1462     blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet,
1463                     force_share);
1464     if (!blk1) {
1465         ret = 2;
1466         goto out3;
1467     }
1468 
1469     blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet,
1470                     force_share);
1471     if (!blk2) {
1472         ret = 2;
1473         goto out2;
1474     }
1475     bs1 = blk_bs(blk1);
1476     bs2 = blk_bs(blk2);
1477 
1478     buf1 = blk_blockalign(blk1, IO_BUF_SIZE);
1479     buf2 = blk_blockalign(blk2, IO_BUF_SIZE);
1480     total_size1 = blk_getlength(blk1);
1481     if (total_size1 < 0) {
1482         error_report("Can't get size of %s: %s",
1483                      filename1, strerror(-total_size1));
1484         ret = 4;
1485         goto out;
1486     }
1487     total_size2 = blk_getlength(blk2);
1488     if (total_size2 < 0) {
1489         error_report("Can't get size of %s: %s",
1490                      filename2, strerror(-total_size2));
1491         ret = 4;
1492         goto out;
1493     }
1494     total_size = MIN(total_size1, total_size2);
1495     progress_base = MAX(total_size1, total_size2);
1496 
1497     qemu_progress_print(0, 100);
1498 
1499     if (strict && total_size1 != total_size2) {
1500         ret = 1;
1501         qprintf(quiet, "Strict mode: Image size mismatch!\n");
1502         goto out;
1503     }
1504 
1505     while (offset < total_size) {
1506         int status1, status2;
1507 
1508         status1 = bdrv_block_status_above(bs1, NULL, offset,
1509                                           total_size1 - offset, &pnum1, NULL,
1510                                           NULL);
1511         if (status1 < 0) {
1512             ret = 3;
1513             error_report("Sector allocation test failed for %s", filename1);
1514             goto out;
1515         }
1516         allocated1 = status1 & BDRV_BLOCK_ALLOCATED;
1517 
1518         status2 = bdrv_block_status_above(bs2, NULL, offset,
1519                                           total_size2 - offset, &pnum2, NULL,
1520                                           NULL);
1521         if (status2 < 0) {
1522             ret = 3;
1523             error_report("Sector allocation test failed for %s", filename2);
1524             goto out;
1525         }
1526         allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
1527 
1528         assert(pnum1 && pnum2);
1529         chunk = MIN(pnum1, pnum2);
1530 
1531         if (strict) {
1532             if (status1 != status2) {
1533                 ret = 1;
1534                 qprintf(quiet, "Strict mode: Offset %" PRId64
1535                         " block status mismatch!\n", offset);
1536                 goto out;
1537             }
1538         }
1539         if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) {
1540             /* nothing to do */
1541         } else if (allocated1 == allocated2) {
1542             if (allocated1) {
1543                 int64_t pnum;
1544 
1545                 chunk = MIN(chunk, IO_BUF_SIZE);
1546                 ret = blk_pread(blk1, offset, chunk, buf1, 0);
1547                 if (ret < 0) {
1548                     error_report("Error while reading offset %" PRId64
1549                                  " of %s: %s",
1550                                  offset, filename1, strerror(-ret));
1551                     ret = 4;
1552                     goto out;
1553                 }
1554                 ret = blk_pread(blk2, offset, chunk, buf2, 0);
1555                 if (ret < 0) {
1556                     error_report("Error while reading offset %" PRId64
1557                                  " of %s: %s",
1558                                  offset, filename2, strerror(-ret));
1559                     ret = 4;
1560                     goto out;
1561                 }
1562                 ret = compare_buffers(buf1, buf2, chunk, &pnum);
1563                 if (ret || pnum != chunk) {
1564                     qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1565                             offset + (ret ? 0 : pnum));
1566                     ret = 1;
1567                     goto out;
1568                 }
1569             }
1570         } else {
1571             chunk = MIN(chunk, IO_BUF_SIZE);
1572             if (allocated1) {
1573                 ret = check_empty_sectors(blk1, offset, chunk,
1574                                           filename1, buf1, quiet);
1575             } else {
1576                 ret = check_empty_sectors(blk2, offset, chunk,
1577                                           filename2, buf1, quiet);
1578             }
1579             if (ret) {
1580                 goto out;
1581             }
1582         }
1583         offset += chunk;
1584         qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1585     }
1586 
1587     if (total_size1 != total_size2) {
1588         BlockBackend *blk_over;
1589         const char *filename_over;
1590 
1591         qprintf(quiet, "Warning: Image size mismatch!\n");
1592         if (total_size1 > total_size2) {
1593             blk_over = blk1;
1594             filename_over = filename1;
1595         } else {
1596             blk_over = blk2;
1597             filename_over = filename2;
1598         }
1599 
1600         while (offset < progress_base) {
1601             ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset,
1602                                           progress_base - offset, &chunk,
1603                                           NULL, NULL);
1604             if (ret < 0) {
1605                 ret = 3;
1606                 error_report("Sector allocation test failed for %s",
1607                              filename_over);
1608                 goto out;
1609 
1610             }
1611             if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) {
1612                 chunk = MIN(chunk, IO_BUF_SIZE);
1613                 ret = check_empty_sectors(blk_over, offset, chunk,
1614                                           filename_over, buf1, quiet);
1615                 if (ret) {
1616                     goto out;
1617                 }
1618             }
1619             offset += chunk;
1620             qemu_progress_print(((float) chunk / progress_base) * 100, 100);
1621         }
1622     }
1623 
1624     qprintf(quiet, "Images are identical.\n");
1625     ret = 0;
1626 
1627 out:
1628     qemu_vfree(buf1);
1629     qemu_vfree(buf2);
1630     blk_unref(blk2);
1631 out2:
1632     blk_unref(blk1);
1633 out3:
1634     qemu_progress_end();
1635     return ret;
1636 }
1637 
1638 /* Convenience wrapper around qmp_block_dirty_bitmap_merge */
1639 static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name,
1640                                   const char *src_node, const char *src_name,
1641                                   Error **errp)
1642 {
1643     BlockDirtyBitmapOrStr *merge_src;
1644     BlockDirtyBitmapOrStrList *list = NULL;
1645 
1646     merge_src = g_new0(BlockDirtyBitmapOrStr, 1);
1647     merge_src->type = QTYPE_QDICT;
1648     merge_src->u.external.node = g_strdup(src_node);
1649     merge_src->u.external.name = g_strdup(src_name);
1650     QAPI_LIST_PREPEND(list, merge_src);
1651     qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp);
1652     qapi_free_BlockDirtyBitmapOrStrList(list);
1653 }
1654 
1655 enum ImgConvertBlockStatus {
1656     BLK_DATA,
1657     BLK_ZERO,
1658     BLK_BACKING_FILE,
1659 };
1660 
1661 #define MAX_COROUTINES 16
1662 #define CONVERT_THROTTLE_GROUP "img_convert"
1663 
1664 typedef struct ImgConvertState {
1665     BlockBackend **src;
1666     int64_t *src_sectors;
1667     int *src_alignment;
1668     int src_num;
1669     int64_t total_sectors;
1670     int64_t allocated_sectors;
1671     int64_t allocated_done;
1672     int64_t sector_num;
1673     int64_t wr_offs;
1674     enum ImgConvertBlockStatus status;
1675     int64_t sector_next_status;
1676     BlockBackend *target;
1677     bool has_zero_init;
1678     bool compressed;
1679     bool target_is_new;
1680     bool target_has_backing;
1681     int64_t target_backing_sectors; /* negative if unknown */
1682     bool wr_in_order;
1683     bool copy_range;
1684     bool salvage;
1685     bool quiet;
1686     int min_sparse;
1687     int alignment;
1688     size_t cluster_sectors;
1689     size_t buf_sectors;
1690     long num_coroutines;
1691     int running_coroutines;
1692     Coroutine *co[MAX_COROUTINES];
1693     int64_t wait_sector_num[MAX_COROUTINES];
1694     CoMutex lock;
1695     int ret;
1696 } ImgConvertState;
1697 
1698 static void convert_select_part(ImgConvertState *s, int64_t sector_num,
1699                                 int *src_cur, int64_t *src_cur_offset)
1700 {
1701     *src_cur = 0;
1702     *src_cur_offset = 0;
1703     while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) {
1704         *src_cur_offset += s->src_sectors[*src_cur];
1705         (*src_cur)++;
1706         assert(*src_cur < s->src_num);
1707     }
1708 }
1709 
1710 static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
1711 {
1712     int64_t src_cur_offset;
1713     int ret, n, src_cur;
1714     bool post_backing_zero = false;
1715 
1716     convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1717 
1718     assert(s->total_sectors > sector_num);
1719     n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
1720 
1721     if (s->target_backing_sectors >= 0) {
1722         if (sector_num >= s->target_backing_sectors) {
1723             post_backing_zero = true;
1724         } else if (sector_num + n > s->target_backing_sectors) {
1725             /* Split requests around target_backing_sectors (because
1726              * starting from there, zeros are handled differently) */
1727             n = s->target_backing_sectors - sector_num;
1728         }
1729     }
1730 
1731     if (s->sector_next_status <= sector_num) {
1732         uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE;
1733         int64_t count;
1734         int tail;
1735         BlockDriverState *src_bs = blk_bs(s->src[src_cur]);
1736         BlockDriverState *base;
1737 
1738         if (s->target_has_backing) {
1739             base = bdrv_cow_bs(bdrv_skip_filters(src_bs));
1740         } else {
1741             base = NULL;
1742         }
1743 
1744         do {
1745             count = n * BDRV_SECTOR_SIZE;
1746 
1747             ret = bdrv_block_status_above(src_bs, base, offset, count, &count,
1748                                           NULL, NULL);
1749 
1750             if (ret < 0) {
1751                 if (s->salvage) {
1752                     if (n == 1) {
1753                         if (!s->quiet) {
1754                             warn_report("error while reading block status at "
1755                                         "offset %" PRIu64 ": %s", offset,
1756                                         strerror(-ret));
1757                         }
1758                         /* Just try to read the data, then */
1759                         ret = BDRV_BLOCK_DATA;
1760                         count = BDRV_SECTOR_SIZE;
1761                     } else {
1762                         /* Retry on a shorter range */
1763                         n = DIV_ROUND_UP(n, 4);
1764                     }
1765                 } else {
1766                     error_report("error while reading block status at offset "
1767                                  "%" PRIu64 ": %s", offset, strerror(-ret));
1768                     return ret;
1769                 }
1770             }
1771         } while (ret < 0);
1772 
1773         n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE);
1774 
1775         /*
1776          * Avoid that s->sector_next_status becomes unaligned to the source
1777          * request alignment and/or cluster size to avoid unnecessary read
1778          * cycles.
1779          */
1780         tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur];
1781         if (n > tail) {
1782             n -= tail;
1783         }
1784 
1785         if (ret & BDRV_BLOCK_ZERO) {
1786             s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO;
1787         } else if (ret & BDRV_BLOCK_DATA) {
1788             s->status = BLK_DATA;
1789         } else {
1790             s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA;
1791         }
1792 
1793         s->sector_next_status = sector_num + n;
1794     }
1795 
1796     n = MIN(n, s->sector_next_status - sector_num);
1797     if (s->status == BLK_DATA) {
1798         n = MIN(n, s->buf_sectors);
1799     }
1800 
1801     /* We need to write complete clusters for compressed images, so if an
1802      * unallocated area is shorter than that, we must consider the whole
1803      * cluster allocated. */
1804     if (s->compressed) {
1805         if (n < s->cluster_sectors) {
1806             n = MIN(s->cluster_sectors, s->total_sectors - sector_num);
1807             s->status = BLK_DATA;
1808         } else {
1809             n = QEMU_ALIGN_DOWN(n, s->cluster_sectors);
1810         }
1811     }
1812 
1813     return n;
1814 }
1815 
1816 static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num,
1817                                         int nb_sectors, uint8_t *buf)
1818 {
1819     uint64_t single_read_until = 0;
1820     int n, ret;
1821 
1822     assert(nb_sectors <= s->buf_sectors);
1823     while (nb_sectors > 0) {
1824         BlockBackend *blk;
1825         int src_cur;
1826         int64_t bs_sectors, src_cur_offset;
1827         uint64_t offset;
1828 
1829         /* In the case of compression with multiple source files, we can get a
1830          * nb_sectors that spreads into the next part. So we must be able to
1831          * read across multiple BDSes for one convert_read() call. */
1832         convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1833         blk = s->src[src_cur];
1834         bs_sectors = s->src_sectors[src_cur];
1835 
1836         offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1837 
1838         n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1839         if (single_read_until > offset) {
1840             n = 1;
1841         }
1842 
1843         ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0);
1844         if (ret < 0) {
1845             if (s->salvage) {
1846                 if (n > 1) {
1847                     single_read_until = offset + (n << BDRV_SECTOR_BITS);
1848                     continue;
1849                 } else {
1850                     if (!s->quiet) {
1851                         warn_report("error while reading offset %" PRIu64
1852                                     ": %s", offset, strerror(-ret));
1853                     }
1854                     memset(buf, 0, BDRV_SECTOR_SIZE);
1855                 }
1856             } else {
1857                 return ret;
1858             }
1859         }
1860 
1861         sector_num += n;
1862         nb_sectors -= n;
1863         buf += n * BDRV_SECTOR_SIZE;
1864     }
1865 
1866     return 0;
1867 }
1868 
1869 
1870 static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
1871                                          int nb_sectors, uint8_t *buf,
1872                                          enum ImgConvertBlockStatus status)
1873 {
1874     int ret;
1875 
1876     while (nb_sectors > 0) {
1877         int n = nb_sectors;
1878         BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
1879 
1880         switch (status) {
1881         case BLK_BACKING_FILE:
1882             /* If we have a backing file, leave clusters unallocated that are
1883              * unallocated in the source image, so that the backing file is
1884              * visible at the respective offset. */
1885             assert(s->target_has_backing);
1886             break;
1887 
1888         case BLK_DATA:
1889             /* If we're told to keep the target fully allocated (-S 0) or there
1890              * is real non-zero data, we must write it. Otherwise we can treat
1891              * it as zero sectors.
1892              * Compressed clusters need to be written as a whole, so in that
1893              * case we can only save the write if the buffer is completely
1894              * zeroed. */
1895             if (!s->min_sparse ||
1896                 (!s->compressed &&
1897                  is_allocated_sectors_min(buf, n, &n, s->min_sparse,
1898                                           sector_num, s->alignment)) ||
1899                 (s->compressed &&
1900                  !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
1901             {
1902                 ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS,
1903                                     n << BDRV_SECTOR_BITS, buf, flags);
1904                 if (ret < 0) {
1905                     return ret;
1906                 }
1907                 break;
1908             }
1909             /* fall-through */
1910 
1911         case BLK_ZERO:
1912             if (s->has_zero_init) {
1913                 assert(!s->target_has_backing);
1914                 break;
1915             }
1916             ret = blk_co_pwrite_zeroes(s->target,
1917                                        sector_num << BDRV_SECTOR_BITS,
1918                                        n << BDRV_SECTOR_BITS,
1919                                        BDRV_REQ_MAY_UNMAP);
1920             if (ret < 0) {
1921                 return ret;
1922             }
1923             break;
1924         }
1925 
1926         sector_num += n;
1927         nb_sectors -= n;
1928         buf += n * BDRV_SECTOR_SIZE;
1929     }
1930 
1931     return 0;
1932 }
1933 
1934 static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num,
1935                                               int nb_sectors)
1936 {
1937     int n, ret;
1938 
1939     while (nb_sectors > 0) {
1940         BlockBackend *blk;
1941         int src_cur;
1942         int64_t bs_sectors, src_cur_offset;
1943         int64_t offset;
1944 
1945         convert_select_part(s, sector_num, &src_cur, &src_cur_offset);
1946         offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS;
1947         blk = s->src[src_cur];
1948         bs_sectors = s->src_sectors[src_cur];
1949 
1950         n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset));
1951 
1952         ret = blk_co_copy_range(blk, offset, s->target,
1953                                 sector_num << BDRV_SECTOR_BITS,
1954                                 n << BDRV_SECTOR_BITS, 0, 0);
1955         if (ret < 0) {
1956             return ret;
1957         }
1958 
1959         sector_num += n;
1960         nb_sectors -= n;
1961     }
1962     return 0;
1963 }
1964 
1965 static void coroutine_fn convert_co_do_copy(void *opaque)
1966 {
1967     ImgConvertState *s = opaque;
1968     uint8_t *buf = NULL;
1969     int ret, i;
1970     int index = -1;
1971 
1972     for (i = 0; i < s->num_coroutines; i++) {
1973         if (s->co[i] == qemu_coroutine_self()) {
1974             index = i;
1975             break;
1976         }
1977     }
1978     assert(index >= 0);
1979 
1980     s->running_coroutines++;
1981     buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);
1982 
1983     while (1) {
1984         int n;
1985         int64_t sector_num;
1986         enum ImgConvertBlockStatus status;
1987         bool copy_range;
1988 
1989         qemu_co_mutex_lock(&s->lock);
1990         if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) {
1991             qemu_co_mutex_unlock(&s->lock);
1992             break;
1993         }
1994         n = convert_iteration_sectors(s, s->sector_num);
1995         if (n < 0) {
1996             qemu_co_mutex_unlock(&s->lock);
1997             s->ret = n;
1998             break;
1999         }
2000         /* save current sector and allocation status to local variables */
2001         sector_num = s->sector_num;
2002         status = s->status;
2003         if (!s->min_sparse && s->status == BLK_ZERO) {
2004             n = MIN(n, s->buf_sectors);
2005         }
2006         /* increment global sector counter so that other coroutines can
2007          * already continue reading beyond this request */
2008         s->sector_num += n;
2009         qemu_co_mutex_unlock(&s->lock);
2010 
2011         if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) {
2012             s->allocated_done += n;
2013             qemu_progress_print(100.0 * s->allocated_done /
2014                                         s->allocated_sectors, 0);
2015         }
2016 
2017 retry:
2018         copy_range = s->copy_range && s->status == BLK_DATA;
2019         if (status == BLK_DATA && !copy_range) {
2020             ret = convert_co_read(s, sector_num, n, buf);
2021             if (ret < 0) {
2022                 error_report("error while reading at byte %lld: %s",
2023                              sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
2024                 s->ret = ret;
2025             }
2026         } else if (!s->min_sparse && status == BLK_ZERO) {
2027             status = BLK_DATA;
2028             memset(buf, 0x00, n * BDRV_SECTOR_SIZE);
2029         }
2030 
2031         if (s->wr_in_order) {
2032             /* keep writes in order */
2033             while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) {
2034                 s->wait_sector_num[index] = sector_num;
2035                 qemu_coroutine_yield();
2036             }
2037             s->wait_sector_num[index] = -1;
2038         }
2039 
2040         if (s->ret == -EINPROGRESS) {
2041             if (copy_range) {
2042                 ret = convert_co_copy_range(s, sector_num, n);
2043                 if (ret) {
2044                     s->copy_range = false;
2045                     goto retry;
2046                 }
2047             } else {
2048                 ret = convert_co_write(s, sector_num, n, buf, status);
2049             }
2050             if (ret < 0) {
2051                 error_report("error while writing at byte %lld: %s",
2052                              sector_num * BDRV_SECTOR_SIZE, strerror(-ret));
2053                 s->ret = ret;
2054             }
2055         }
2056 
2057         if (s->wr_in_order) {
2058             /* reenter the coroutine that might have waited
2059              * for this write to complete */
2060             s->wr_offs = sector_num + n;
2061             for (i = 0; i < s->num_coroutines; i++) {
2062                 if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) {
2063                     /*
2064                      * A -> B -> A cannot occur because A has
2065                      * s->wait_sector_num[i] == -1 during A -> B.  Therefore
2066                      * B will never enter A during this time window.
2067                      */
2068                     qemu_coroutine_enter(s->co[i]);
2069                     break;
2070                 }
2071             }
2072         }
2073     }
2074 
2075     qemu_vfree(buf);
2076     s->co[index] = NULL;
2077     s->running_coroutines--;
2078     if (!s->running_coroutines && s->ret == -EINPROGRESS) {
2079         /* the convert job finished successfully */
2080         s->ret = 0;
2081     }
2082 }
2083 
2084 static int convert_do_copy(ImgConvertState *s)
2085 {
2086     int ret, i, n;
2087     int64_t sector_num = 0;
2088 
2089     /* Check whether we have zero initialisation or can get it efficiently */
2090     if (!s->has_zero_init && s->target_is_new && s->min_sparse &&
2091         !s->target_has_backing) {
2092         s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target));
2093     }
2094 
2095     /* Allocate buffer for copied data. For compressed images, only one cluster
2096      * can be copied at a time. */
2097     if (s->compressed) {
2098         if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {
2099             error_report("invalid cluster size");
2100             return -EINVAL;
2101         }
2102         s->buf_sectors = s->cluster_sectors;
2103     }
2104 
2105     while (sector_num < s->total_sectors) {
2106         n = convert_iteration_sectors(s, sector_num);
2107         if (n < 0) {
2108             return n;
2109         }
2110         if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO))
2111         {
2112             s->allocated_sectors += n;
2113         }
2114         sector_num += n;
2115     }
2116 
2117     /* Do the copy */
2118     s->sector_next_status = 0;
2119     s->ret = -EINPROGRESS;
2120 
2121     qemu_co_mutex_init(&s->lock);
2122     for (i = 0; i < s->num_coroutines; i++) {
2123         s->co[i] = qemu_coroutine_create(convert_co_do_copy, s);
2124         s->wait_sector_num[i] = -1;
2125         qemu_coroutine_enter(s->co[i]);
2126     }
2127 
2128     while (s->running_coroutines) {
2129         main_loop_wait(false);
2130     }
2131 
2132     if (s->compressed && !s->ret) {
2133         /* signal EOF to align */
2134         ret = blk_pwrite_compressed(s->target, 0, 0, NULL);
2135         if (ret < 0) {
2136             return ret;
2137         }
2138     }
2139 
2140     return s->ret;
2141 }
2142 
2143 /* Check that bitmaps can be copied, or output an error */
2144 static int convert_check_bitmaps(BlockDriverState *src, bool skip_broken)
2145 {
2146     BdrvDirtyBitmap *bm;
2147 
2148     if (!bdrv_supports_persistent_dirty_bitmap(src)) {
2149         error_report("Source lacks bitmap support");
2150         return -1;
2151     }
2152     FOR_EACH_DIRTY_BITMAP(src, bm) {
2153         if (!bdrv_dirty_bitmap_get_persistence(bm)) {
2154             continue;
2155         }
2156         if (!skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
2157             error_report("Cannot copy inconsistent bitmap '%s'",
2158                          bdrv_dirty_bitmap_name(bm));
2159             error_printf("Try --skip-broken-bitmaps, or "
2160                          "use 'qemu-img bitmap --remove' to delete it\n");
2161             return -1;
2162         }
2163     }
2164     return 0;
2165 }
2166 
2167 static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst,
2168                                 bool skip_broken)
2169 {
2170     BdrvDirtyBitmap *bm;
2171     Error *err = NULL;
2172 
2173     FOR_EACH_DIRTY_BITMAP(src, bm) {
2174         const char *name;
2175 
2176         if (!bdrv_dirty_bitmap_get_persistence(bm)) {
2177             continue;
2178         }
2179         name = bdrv_dirty_bitmap_name(bm);
2180         if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) {
2181             warn_report("Skipping inconsistent bitmap '%s'", name);
2182             continue;
2183         }
2184         qmp_block_dirty_bitmap_add(dst->node_name, name,
2185                                    true, bdrv_dirty_bitmap_granularity(bm),
2186                                    true, true,
2187                                    true, !bdrv_dirty_bitmap_enabled(bm),
2188                                    &err);
2189         if (err) {
2190             error_reportf_err(err, "Failed to create bitmap %s: ", name);
2191             return -1;
2192         }
2193 
2194         do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name,
2195                               &err);
2196         if (err) {
2197             error_reportf_err(err, "Failed to populate bitmap %s: ", name);
2198             qmp_block_dirty_bitmap_remove(dst->node_name, name, NULL);
2199             return -1;
2200         }
2201     }
2202 
2203     return 0;
2204 }
2205 
2206 #define MAX_BUF_SECTORS 32768
2207 
2208 static void set_rate_limit(BlockBackend *blk, int64_t rate_limit)
2209 {
2210     ThrottleConfig cfg;
2211 
2212     throttle_config_init(&cfg);
2213     cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit;
2214 
2215     blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP);
2216     blk_set_io_limits(blk, &cfg);
2217 }
2218 
2219 static int img_convert(int argc, char **argv)
2220 {
2221     int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE;
2222     const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
2223                *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
2224                *out_filename, *out_baseimg_param, *snapshot_name = NULL,
2225                *backing_fmt = NULL;
2226     BlockDriver *drv = NULL, *proto_drv = NULL;
2227     BlockDriverInfo bdi;
2228     BlockDriverState *out_bs;
2229     QemuOpts *opts = NULL, *sn_opts = NULL;
2230     QemuOptsList *create_opts = NULL;
2231     QDict *open_opts = NULL;
2232     char *options = NULL;
2233     Error *local_err = NULL;
2234     bool writethrough, src_writethrough, image_opts = false,
2235          skip_create = false, progress = false, tgt_image_opts = false;
2236     int64_t ret = -EINVAL;
2237     bool force_share = false;
2238     bool explict_min_sparse = false;
2239     bool bitmaps = false;
2240     bool skip_broken = false;
2241     int64_t rate_limit = 0;
2242 
2243     ImgConvertState s = (ImgConvertState) {
2244         /* Need at least 4k of zeros for sparse detection */
2245         .min_sparse         = 8,
2246         .copy_range         = false,
2247         .buf_sectors        = IO_BUF_SIZE / BDRV_SECTOR_SIZE,
2248         .wr_in_order        = true,
2249         .num_coroutines     = 8,
2250     };
2251 
2252     for(;;) {
2253         static const struct option long_options[] = {
2254             {"help", no_argument, 0, 'h'},
2255             {"object", required_argument, 0, OPTION_OBJECT},
2256             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
2257             {"force-share", no_argument, 0, 'U'},
2258             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
2259             {"salvage", no_argument, 0, OPTION_SALVAGE},
2260             {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO},
2261             {"bitmaps", no_argument, 0, OPTION_BITMAPS},
2262             {"skip-broken-bitmaps", no_argument, 0, OPTION_SKIP_BROKEN},
2263             {0, 0, 0, 0}
2264         };
2265         c = getopt_long(argc, argv, ":hf:O:B:CcF:o:l:S:pt:T:qnm:WUr:",
2266                         long_options, NULL);
2267         if (c == -1) {
2268             break;
2269         }
2270         switch(c) {
2271         case ':':
2272             missing_argument(argv[optind - 1]);
2273             break;
2274         case '?':
2275             unrecognized_option(argv[optind - 1]);
2276             break;
2277         case 'h':
2278             help();
2279             break;
2280         case 'f':
2281             fmt = optarg;
2282             break;
2283         case 'O':
2284             out_fmt = optarg;
2285             break;
2286         case 'B':
2287             out_baseimg = optarg;
2288             break;
2289         case 'C':
2290             s.copy_range = true;
2291             break;
2292         case 'c':
2293             s.compressed = true;
2294             break;
2295         case 'F':
2296             backing_fmt = optarg;
2297             break;
2298         case 'o':
2299             if (accumulate_options(&options, optarg) < 0) {
2300                 goto fail_getopt;
2301             }
2302             break;
2303         case 'l':
2304             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
2305                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
2306                                                   optarg, false);
2307                 if (!sn_opts) {
2308                     error_report("Failed in parsing snapshot param '%s'",
2309                                  optarg);
2310                     goto fail_getopt;
2311                 }
2312             } else {
2313                 snapshot_name = optarg;
2314             }
2315             break;
2316         case 'S':
2317         {
2318             int64_t sval;
2319 
2320             sval = cvtnum("buffer size for sparse output", optarg);
2321             if (sval < 0) {
2322                 goto fail_getopt;
2323             } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) ||
2324                 sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) {
2325                 error_report("Invalid buffer size for sparse output specified. "
2326                     "Valid sizes are multiples of %llu up to %llu. Select "
2327                     "0 to disable sparse detection (fully allocates output).",
2328                     BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE);
2329                 goto fail_getopt;
2330             }
2331 
2332             s.min_sparse = sval / BDRV_SECTOR_SIZE;
2333             explict_min_sparse = true;
2334             break;
2335         }
2336         case 'p':
2337             progress = true;
2338             break;
2339         case 't':
2340             cache = optarg;
2341             break;
2342         case 'T':
2343             src_cache = optarg;
2344             break;
2345         case 'q':
2346             s.quiet = true;
2347             break;
2348         case 'n':
2349             skip_create = true;
2350             break;
2351         case 'm':
2352             if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) ||
2353                 s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) {
2354                 error_report("Invalid number of coroutines. Allowed number of"
2355                              " coroutines is between 1 and %d", MAX_COROUTINES);
2356                 goto fail_getopt;
2357             }
2358             break;
2359         case 'W':
2360             s.wr_in_order = false;
2361             break;
2362         case 'U':
2363             force_share = true;
2364             break;
2365         case 'r':
2366             rate_limit = cvtnum("rate limit", optarg);
2367             if (rate_limit < 0) {
2368                 goto fail_getopt;
2369             }
2370             break;
2371         case OPTION_OBJECT:
2372             user_creatable_process_cmdline(optarg);
2373             break;
2374         case OPTION_IMAGE_OPTS:
2375             image_opts = true;
2376             break;
2377         case OPTION_SALVAGE:
2378             s.salvage = true;
2379             break;
2380         case OPTION_TARGET_IMAGE_OPTS:
2381             tgt_image_opts = true;
2382             break;
2383         case OPTION_TARGET_IS_ZERO:
2384             /*
2385              * The user asserting that the target is blank has the
2386              * same effect as the target driver supporting zero
2387              * initialisation.
2388              */
2389             s.has_zero_init = true;
2390             break;
2391         case OPTION_BITMAPS:
2392             bitmaps = true;
2393             break;
2394         case OPTION_SKIP_BROKEN:
2395             skip_broken = true;
2396             break;
2397         }
2398     }
2399 
2400     if (!out_fmt && !tgt_image_opts) {
2401         out_fmt = "raw";
2402     }
2403 
2404     if (skip_broken && !bitmaps) {
2405         error_report("Use of --skip-broken-bitmaps requires --bitmaps");
2406         goto fail_getopt;
2407     }
2408 
2409     if (s.compressed && s.copy_range) {
2410         error_report("Cannot enable copy offloading when -c is used");
2411         goto fail_getopt;
2412     }
2413 
2414     if (explict_min_sparse && s.copy_range) {
2415         error_report("Cannot enable copy offloading when -S is used");
2416         goto fail_getopt;
2417     }
2418 
2419     if (s.copy_range && s.salvage) {
2420         error_report("Cannot use copy offloading in salvaging mode");
2421         goto fail_getopt;
2422     }
2423 
2424     if (tgt_image_opts && !skip_create) {
2425         error_report("--target-image-opts requires use of -n flag");
2426         goto fail_getopt;
2427     }
2428 
2429     if (skip_create && options) {
2430         error_report("-o has no effect when skipping image creation");
2431         goto fail_getopt;
2432     }
2433 
2434     if (s.has_zero_init && !skip_create) {
2435         error_report("--target-is-zero requires use of -n flag");
2436         goto fail_getopt;
2437     }
2438 
2439     s.src_num = argc - optind - 1;
2440     out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL;
2441 
2442     if (options && has_help_option(options)) {
2443         if (out_fmt) {
2444             ret = print_block_option_help(out_filename, out_fmt);
2445             goto fail_getopt;
2446         } else {
2447             error_report("Option help requires a format be specified");
2448             goto fail_getopt;
2449         }
2450     }
2451 
2452     if (s.src_num < 1) {
2453         error_report("Must specify image file name");
2454         goto fail_getopt;
2455     }
2456 
2457     /* ret is still -EINVAL until here */
2458     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
2459     if (ret < 0) {
2460         error_report("Invalid source cache option: %s", src_cache);
2461         goto fail_getopt;
2462     }
2463 
2464     /* Initialize before goto out */
2465     if (s.quiet) {
2466         progress = false;
2467     }
2468     qemu_progress_init(progress, 1.0);
2469     qemu_progress_print(0, 100);
2470 
2471     s.src = g_new0(BlockBackend *, s.src_num);
2472     s.src_sectors = g_new(int64_t, s.src_num);
2473     s.src_alignment = g_new(int, s.src_num);
2474 
2475     for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2476         BlockDriverState *src_bs;
2477         s.src[bs_i] = img_open(image_opts, argv[optind + bs_i],
2478                                fmt, src_flags, src_writethrough, s.quiet,
2479                                force_share);
2480         if (!s.src[bs_i]) {
2481             ret = -1;
2482             goto out;
2483         }
2484         s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]);
2485         if (s.src_sectors[bs_i] < 0) {
2486             error_report("Could not get size of %s: %s",
2487                          argv[optind + bs_i], strerror(-s.src_sectors[bs_i]));
2488             ret = -1;
2489             goto out;
2490         }
2491         src_bs = blk_bs(s.src[bs_i]);
2492         s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment,
2493                                              BDRV_SECTOR_SIZE);
2494         if (!bdrv_get_info(src_bs, &bdi)) {
2495             s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i],
2496                                         bdi.cluster_size / BDRV_SECTOR_SIZE);
2497         }
2498         s.total_sectors += s.src_sectors[bs_i];
2499     }
2500 
2501     if (sn_opts) {
2502         bdrv_snapshot_load_tmp(blk_bs(s.src[0]),
2503                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
2504                                qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
2505                                &local_err);
2506     } else if (snapshot_name != NULL) {
2507         if (s.src_num > 1) {
2508             error_report("No support for concatenating multiple snapshot");
2509             ret = -1;
2510             goto out;
2511         }
2512 
2513         bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name,
2514                                              &local_err);
2515     }
2516     if (local_err) {
2517         error_reportf_err(local_err, "Failed to load snapshot: ");
2518         ret = -1;
2519         goto out;
2520     }
2521 
2522     if (!skip_create) {
2523         /* Find driver and parse its options */
2524         drv = bdrv_find_format(out_fmt);
2525         if (!drv) {
2526             error_report("Unknown file format '%s'", out_fmt);
2527             ret = -1;
2528             goto out;
2529         }
2530 
2531         proto_drv = bdrv_find_protocol(out_filename, true, &local_err);
2532         if (!proto_drv) {
2533             error_report_err(local_err);
2534             ret = -1;
2535             goto out;
2536         }
2537 
2538         if (!drv->create_opts) {
2539             error_report("Format driver '%s' does not support image creation",
2540                          drv->format_name);
2541             ret = -1;
2542             goto out;
2543         }
2544 
2545         if (!proto_drv->create_opts) {
2546             error_report("Protocol driver '%s' does not support image creation",
2547                          proto_drv->format_name);
2548             ret = -1;
2549             goto out;
2550         }
2551 
2552         create_opts = qemu_opts_append(create_opts, drv->create_opts);
2553         create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
2554 
2555         opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2556         if (options) {
2557             if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
2558                 error_report_err(local_err);
2559                 ret = -1;
2560                 goto out;
2561             }
2562         }
2563 
2564         qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
2565                             s.total_sectors * BDRV_SECTOR_SIZE, &error_abort);
2566         ret = add_old_style_options(out_fmt, opts, out_baseimg, backing_fmt);
2567         if (ret < 0) {
2568             goto out;
2569         }
2570     }
2571 
2572     /* Get backing file name if -o backing_file was used */
2573     out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
2574     if (out_baseimg_param) {
2575         out_baseimg = out_baseimg_param;
2576     }
2577     s.target_has_backing = (bool) out_baseimg;
2578 
2579     if (s.has_zero_init && s.target_has_backing) {
2580         error_report("Cannot use --target-is-zero when the destination "
2581                      "image has a backing file");
2582         goto out;
2583     }
2584 
2585     if (s.src_num > 1 && out_baseimg) {
2586         error_report("Having a backing file for the target makes no sense when "
2587                      "concatenating multiple input images");
2588         ret = -1;
2589         goto out;
2590     }
2591 
2592     if (out_baseimg_param) {
2593         if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) {
2594             error_report("Use of backing file requires explicit "
2595                          "backing format");
2596             ret = -1;
2597             goto out;
2598         }
2599     }
2600 
2601     /* Check if compression is supported */
2602     if (s.compressed) {
2603         bool encryption =
2604             qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
2605         const char *encryptfmt =
2606             qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2607         const char *preallocation =
2608             qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
2609 
2610         if (drv && !block_driver_can_compress(drv)) {
2611             error_report("Compression not supported for this file format");
2612             ret = -1;
2613             goto out;
2614         }
2615 
2616         if (encryption || encryptfmt) {
2617             error_report("Compression and encryption not supported at "
2618                          "the same time");
2619             ret = -1;
2620             goto out;
2621         }
2622 
2623         if (preallocation
2624             && strcmp(preallocation, "off"))
2625         {
2626             error_report("Compression and preallocation not supported at "
2627                          "the same time");
2628             ret = -1;
2629             goto out;
2630         }
2631     }
2632 
2633     /* Determine if bitmaps need copying */
2634     if (bitmaps) {
2635         if (s.src_num > 1) {
2636             error_report("Copying bitmaps only possible with single source");
2637             ret = -1;
2638             goto out;
2639         }
2640         ret = convert_check_bitmaps(blk_bs(s.src[0]), skip_broken);
2641         if (ret < 0) {
2642             goto out;
2643         }
2644     }
2645 
2646     /*
2647      * The later open call will need any decryption secrets, and
2648      * bdrv_create() will purge "opts", so extract them now before
2649      * they are lost.
2650      */
2651     if (!skip_create) {
2652         open_opts = qdict_new();
2653         qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
2654 
2655         /* Create the new image */
2656         ret = bdrv_create(drv, out_filename, opts, &local_err);
2657         if (ret < 0) {
2658             error_reportf_err(local_err, "%s: error while converting %s: ",
2659                               out_filename, out_fmt);
2660             goto out;
2661         }
2662     }
2663 
2664     s.target_is_new = !skip_create;
2665 
2666     flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
2667     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
2668     if (ret < 0) {
2669         error_report("Invalid cache option: %s", cache);
2670         goto out;
2671     }
2672 
2673     if (flags & BDRV_O_NOCACHE) {
2674         /*
2675          * If we open the target with O_DIRECT, it may be necessary to
2676          * extend its size to align to the physical sector size.
2677          */
2678         flags |= BDRV_O_RESIZE;
2679     }
2680 
2681     if (skip_create) {
2682         s.target = img_open(tgt_image_opts, out_filename, out_fmt,
2683                             flags, writethrough, s.quiet, false);
2684     } else {
2685         /* TODO ultimately we should allow --target-image-opts
2686          * to be used even when -n is not given.
2687          * That has to wait for bdrv_create to be improved
2688          * to allow filenames in option syntax
2689          */
2690         s.target = img_open_file(out_filename, open_opts, out_fmt,
2691                                  flags, writethrough, s.quiet, false);
2692         open_opts = NULL; /* blk_new_open will have freed it */
2693     }
2694     if (!s.target) {
2695         ret = -1;
2696         goto out;
2697     }
2698     out_bs = blk_bs(s.target);
2699 
2700     if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) {
2701         error_report("Format driver '%s' does not support bitmaps",
2702                      out_bs->drv->format_name);
2703         ret = -1;
2704         goto out;
2705     }
2706 
2707     if (s.compressed && !block_driver_can_compress(out_bs->drv)) {
2708         error_report("Compression not supported for this file format");
2709         ret = -1;
2710         goto out;
2711     }
2712 
2713     /* increase bufsectors from the default 4096 (2M) if opt_transfer
2714      * or discard_alignment of the out_bs is greater. Limit to
2715      * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */
2716     s.buf_sectors = MIN(MAX_BUF_SECTORS,
2717                         MAX(s.buf_sectors,
2718                             MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS,
2719                                 out_bs->bl.pdiscard_alignment >>
2720                                 BDRV_SECTOR_BITS)));
2721 
2722     /* try to align the write requests to the destination to avoid unnecessary
2723      * RMW cycles. */
2724     s.alignment = MAX(pow2floor(s.min_sparse),
2725                       DIV_ROUND_UP(out_bs->bl.request_alignment,
2726                                    BDRV_SECTOR_SIZE));
2727     assert(is_power_of_2(s.alignment));
2728 
2729     if (skip_create) {
2730         int64_t output_sectors = blk_nb_sectors(s.target);
2731         if (output_sectors < 0) {
2732             error_report("unable to get output image length: %s",
2733                          strerror(-output_sectors));
2734             ret = -1;
2735             goto out;
2736         } else if (output_sectors < s.total_sectors) {
2737             error_report("output file is smaller than input file");
2738             ret = -1;
2739             goto out;
2740         }
2741     }
2742 
2743     if (s.target_has_backing && s.target_is_new) {
2744         /* Errors are treated as "backing length unknown" (which means
2745          * s.target_backing_sectors has to be negative, which it will
2746          * be automatically).  The backing file length is used only
2747          * for optimizations, so such a case is not fatal. */
2748         s.target_backing_sectors =
2749             bdrv_nb_sectors(bdrv_backing_chain_next(out_bs));
2750     } else {
2751         s.target_backing_sectors = -1;
2752     }
2753 
2754     ret = bdrv_get_info(out_bs, &bdi);
2755     if (ret < 0) {
2756         if (s.compressed) {
2757             error_report("could not get block driver info");
2758             goto out;
2759         }
2760     } else {
2761         s.compressed = s.compressed || bdi.needs_compressed_writes;
2762         s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
2763     }
2764 
2765     if (rate_limit) {
2766         set_rate_limit(s.target, rate_limit);
2767     }
2768 
2769     ret = convert_do_copy(&s);
2770 
2771     /* Now copy the bitmaps */
2772     if (bitmaps && ret == 0) {
2773         ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs, skip_broken);
2774     }
2775 
2776 out:
2777     if (!ret) {
2778         qemu_progress_print(100, 0);
2779     }
2780     qemu_progress_end();
2781     qemu_opts_del(opts);
2782     qemu_opts_free(create_opts);
2783     qobject_unref(open_opts);
2784     blk_unref(s.target);
2785     if (s.src) {
2786         for (bs_i = 0; bs_i < s.src_num; bs_i++) {
2787             blk_unref(s.src[bs_i]);
2788         }
2789         g_free(s.src);
2790     }
2791     g_free(s.src_sectors);
2792     g_free(s.src_alignment);
2793 fail_getopt:
2794     qemu_opts_del(sn_opts);
2795     g_free(options);
2796 
2797     return !!ret;
2798 }
2799 
2800 
2801 static void dump_snapshots(BlockDriverState *bs)
2802 {
2803     QEMUSnapshotInfo *sn_tab, *sn;
2804     int nb_sns, i;
2805 
2806     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2807     if (nb_sns <= 0)
2808         return;
2809     printf("Snapshot list:\n");
2810     bdrv_snapshot_dump(NULL);
2811     printf("\n");
2812     for(i = 0; i < nb_sns; i++) {
2813         sn = &sn_tab[i];
2814         bdrv_snapshot_dump(sn);
2815         printf("\n");
2816     }
2817     g_free(sn_tab);
2818 }
2819 
2820 static void dump_json_block_graph_info_list(BlockGraphInfoList *list)
2821 {
2822     GString *str;
2823     QObject *obj;
2824     Visitor *v = qobject_output_visitor_new(&obj);
2825 
2826     visit_type_BlockGraphInfoList(v, NULL, &list, &error_abort);
2827     visit_complete(v, &obj);
2828     str = qobject_to_json_pretty(obj, true);
2829     assert(str != NULL);
2830     printf("%s\n", str->str);
2831     qobject_unref(obj);
2832     visit_free(v);
2833     g_string_free(str, true);
2834 }
2835 
2836 static void dump_json_block_graph_info(BlockGraphInfo *info)
2837 {
2838     GString *str;
2839     QObject *obj;
2840     Visitor *v = qobject_output_visitor_new(&obj);
2841 
2842     visit_type_BlockGraphInfo(v, NULL, &info, &error_abort);
2843     visit_complete(v, &obj);
2844     str = qobject_to_json_pretty(obj, true);
2845     assert(str != NULL);
2846     printf("%s\n", str->str);
2847     qobject_unref(obj);
2848     visit_free(v);
2849     g_string_free(str, true);
2850 }
2851 
2852 static void dump_human_image_info(BlockGraphInfo *info, int indentation,
2853                                   const char *path)
2854 {
2855     BlockChildInfoList *children_list;
2856 
2857     bdrv_node_info_dump(qapi_BlockGraphInfo_base(info), indentation);
2858 
2859     for (children_list = info->children; children_list;
2860          children_list = children_list->next)
2861     {
2862         BlockChildInfo *child = children_list->value;
2863         g_autofree char *child_path = NULL;
2864 
2865         printf("%*sChild node '%s%s':\n",
2866                indentation * 4, "", path, child->name);
2867         child_path = g_strdup_printf("%s%s/", path, child->name);
2868         dump_human_image_info(child->info, indentation + 1, child_path);
2869     }
2870 }
2871 
2872 static void dump_human_image_info_list(BlockGraphInfoList *list)
2873 {
2874     BlockGraphInfoList *elem;
2875     bool delim = false;
2876 
2877     for (elem = list; elem; elem = elem->next) {
2878         if (delim) {
2879             printf("\n");
2880         }
2881         delim = true;
2882 
2883         dump_human_image_info(elem->value, 0, "/");
2884     }
2885 }
2886 
2887 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
2888 {
2889     return strcmp(a, b) == 0;
2890 }
2891 
2892 /**
2893  * Open an image file chain and return an BlockGraphInfoList
2894  *
2895  * @filename: topmost image filename
2896  * @fmt: topmost image format (may be NULL to autodetect)
2897  * @chain: true  - enumerate entire backing file chain
2898  *         false - only topmost image file
2899  *
2900  * Returns a list of BlockNodeInfo objects or NULL if there was an error
2901  * opening an image file.  If there was an error a message will have been
2902  * printed to stderr.
2903  */
2904 static BlockGraphInfoList *collect_image_info_list(bool image_opts,
2905                                                    const char *filename,
2906                                                    const char *fmt,
2907                                                    bool chain, bool force_share)
2908 {
2909     BlockGraphInfoList *head = NULL;
2910     BlockGraphInfoList **tail = &head;
2911     GHashTable *filenames;
2912     Error *err = NULL;
2913 
2914     filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
2915 
2916     while (filename) {
2917         BlockBackend *blk;
2918         BlockDriverState *bs;
2919         BlockGraphInfo *info;
2920 
2921         if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
2922             error_report("Backing file '%s' creates an infinite loop.",
2923                          filename);
2924             goto err;
2925         }
2926         g_hash_table_insert(filenames, (gpointer)filename, NULL);
2927 
2928         blk = img_open(image_opts, filename, fmt,
2929                        BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false,
2930                        force_share);
2931         if (!blk) {
2932             goto err;
2933         }
2934         bs = blk_bs(blk);
2935 
2936         /*
2937          * Note that the returned BlockGraphInfo object will not have
2938          * information about this image's backing node, because we have opened
2939          * it with BDRV_O_NO_BACKING.  Printing this object will therefore not
2940          * duplicate the backing chain information that we obtain by walking
2941          * the chain manually here.
2942          */
2943         bdrv_query_block_graph_info(bs, &info, &err);
2944         if (err) {
2945             error_report_err(err);
2946             blk_unref(blk);
2947             goto err;
2948         }
2949 
2950         QAPI_LIST_APPEND(tail, info);
2951 
2952         blk_unref(blk);
2953 
2954         /* Clear parameters that only apply to the topmost image */
2955         filename = fmt = NULL;
2956         image_opts = false;
2957 
2958         if (chain) {
2959             if (info->full_backing_filename) {
2960                 filename = info->full_backing_filename;
2961             } else if (info->backing_filename) {
2962                 error_report("Could not determine absolute backing filename,"
2963                              " but backing filename '%s' present",
2964                              info->backing_filename);
2965                 goto err;
2966             }
2967             if (info->backing_filename_format) {
2968                 fmt = info->backing_filename_format;
2969             }
2970         }
2971     }
2972     g_hash_table_destroy(filenames);
2973     return head;
2974 
2975 err:
2976     qapi_free_BlockGraphInfoList(head);
2977     g_hash_table_destroy(filenames);
2978     return NULL;
2979 }
2980 
2981 static int img_info(int argc, char **argv)
2982 {
2983     int c;
2984     OutputFormat output_format = OFORMAT_HUMAN;
2985     bool chain = false;
2986     const char *filename, *fmt, *output;
2987     BlockGraphInfoList *list;
2988     bool image_opts = false;
2989     bool force_share = false;
2990 
2991     fmt = NULL;
2992     output = NULL;
2993     for(;;) {
2994         int option_index = 0;
2995         static const struct option long_options[] = {
2996             {"help", no_argument, 0, 'h'},
2997             {"format", required_argument, 0, 'f'},
2998             {"output", required_argument, 0, OPTION_OUTPUT},
2999             {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
3000             {"object", required_argument, 0, OPTION_OBJECT},
3001             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3002             {"force-share", no_argument, 0, 'U'},
3003             {0, 0, 0, 0}
3004         };
3005         c = getopt_long(argc, argv, ":f:hU",
3006                         long_options, &option_index);
3007         if (c == -1) {
3008             break;
3009         }
3010         switch(c) {
3011         case ':':
3012             missing_argument(argv[optind - 1]);
3013             break;
3014         case '?':
3015             unrecognized_option(argv[optind - 1]);
3016             break;
3017         case 'h':
3018             help();
3019             break;
3020         case 'f':
3021             fmt = optarg;
3022             break;
3023         case 'U':
3024             force_share = true;
3025             break;
3026         case OPTION_OUTPUT:
3027             output = optarg;
3028             break;
3029         case OPTION_BACKING_CHAIN:
3030             chain = true;
3031             break;
3032         case OPTION_OBJECT:
3033             user_creatable_process_cmdline(optarg);
3034             break;
3035         case OPTION_IMAGE_OPTS:
3036             image_opts = true;
3037             break;
3038         }
3039     }
3040     if (optind != argc - 1) {
3041         error_exit("Expecting one image file name");
3042     }
3043     filename = argv[optind++];
3044 
3045     if (output && !strcmp(output, "json")) {
3046         output_format = OFORMAT_JSON;
3047     } else if (output && !strcmp(output, "human")) {
3048         output_format = OFORMAT_HUMAN;
3049     } else if (output) {
3050         error_report("--output must be used with human or json as argument.");
3051         return 1;
3052     }
3053 
3054     list = collect_image_info_list(image_opts, filename, fmt, chain,
3055                                    force_share);
3056     if (!list) {
3057         return 1;
3058     }
3059 
3060     switch (output_format) {
3061     case OFORMAT_HUMAN:
3062         dump_human_image_info_list(list);
3063         break;
3064     case OFORMAT_JSON:
3065         if (chain) {
3066             dump_json_block_graph_info_list(list);
3067         } else {
3068             dump_json_block_graph_info(list->value);
3069         }
3070         break;
3071     }
3072 
3073     qapi_free_BlockGraphInfoList(list);
3074     return 0;
3075 }
3076 
3077 static int dump_map_entry(OutputFormat output_format, MapEntry *e,
3078                           MapEntry *next)
3079 {
3080     switch (output_format) {
3081     case OFORMAT_HUMAN:
3082         if (e->data && !e->has_offset) {
3083             error_report("File contains external, encrypted or compressed clusters.");
3084             return -1;
3085         }
3086         if (e->data && !e->zero) {
3087             printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
3088                    e->start, e->length,
3089                    e->has_offset ? e->offset : 0,
3090                    e->filename ?: "");
3091         }
3092         /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
3093          * Modify the flags here to allow more coalescing.
3094          */
3095         if (next && (!next->data || next->zero)) {
3096             next->data = false;
3097             next->zero = true;
3098         }
3099         break;
3100     case OFORMAT_JSON:
3101         printf("{ \"start\": %"PRId64", \"length\": %"PRId64","
3102                " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s,"
3103                " \"data\": %s", e->start, e->length, e->depth,
3104                e->present ? "true" : "false",
3105                e->zero ? "true" : "false",
3106                e->data ? "true" : "false");
3107         if (e->has_offset) {
3108             printf(", \"offset\": %"PRId64"", e->offset);
3109         }
3110         putchar('}');
3111 
3112         if (next) {
3113             puts(",");
3114         }
3115         break;
3116     }
3117     return 0;
3118 }
3119 
3120 static int get_block_status(BlockDriverState *bs, int64_t offset,
3121                             int64_t bytes, MapEntry *e)
3122 {
3123     int ret;
3124     int depth;
3125     BlockDriverState *file;
3126     bool has_offset;
3127     int64_t map;
3128     char *filename = NULL;
3129 
3130     /* As an optimization, we could cache the current range of unallocated
3131      * clusters in each file of the chain, and avoid querying the same
3132      * range repeatedly.
3133      */
3134 
3135     depth = 0;
3136     for (;;) {
3137         bs = bdrv_skip_filters(bs);
3138         ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file);
3139         if (ret < 0) {
3140             return ret;
3141         }
3142         assert(bytes);
3143         if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
3144             break;
3145         }
3146         bs = bdrv_cow_bs(bs);
3147         if (bs == NULL) {
3148             ret = 0;
3149             break;
3150         }
3151 
3152         depth++;
3153     }
3154 
3155     has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID);
3156 
3157     if (file && has_offset) {
3158         bdrv_refresh_filename(file);
3159         filename = file->filename;
3160     }
3161 
3162     *e = (MapEntry) {
3163         .start = offset,
3164         .length = bytes,
3165         .data = !!(ret & BDRV_BLOCK_DATA),
3166         .zero = !!(ret & BDRV_BLOCK_ZERO),
3167         .offset = map,
3168         .has_offset = has_offset,
3169         .depth = depth,
3170         .present = !!(ret & BDRV_BLOCK_ALLOCATED),
3171         .filename = filename,
3172     };
3173 
3174     return 0;
3175 }
3176 
3177 static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next)
3178 {
3179     if (curr->length == 0) {
3180         return false;
3181     }
3182     if (curr->zero != next->zero ||
3183         curr->data != next->data ||
3184         curr->depth != next->depth ||
3185         curr->present != next->present ||
3186         !curr->filename != !next->filename ||
3187         curr->has_offset != next->has_offset) {
3188         return false;
3189     }
3190     if (curr->filename && strcmp(curr->filename, next->filename)) {
3191         return false;
3192     }
3193     if (curr->has_offset && curr->offset + curr->length != next->offset) {
3194         return false;
3195     }
3196     return true;
3197 }
3198 
3199 static int img_map(int argc, char **argv)
3200 {
3201     int c;
3202     OutputFormat output_format = OFORMAT_HUMAN;
3203     BlockBackend *blk;
3204     BlockDriverState *bs;
3205     const char *filename, *fmt, *output;
3206     int64_t length;
3207     MapEntry curr = { .length = 0 }, next;
3208     int ret = 0;
3209     bool image_opts = false;
3210     bool force_share = false;
3211     int64_t start_offset = 0;
3212     int64_t max_length = -1;
3213 
3214     fmt = NULL;
3215     output = NULL;
3216     for (;;) {
3217         int option_index = 0;
3218         static const struct option long_options[] = {
3219             {"help", no_argument, 0, 'h'},
3220             {"format", required_argument, 0, 'f'},
3221             {"output", required_argument, 0, OPTION_OUTPUT},
3222             {"object", required_argument, 0, OPTION_OBJECT},
3223             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3224             {"force-share", no_argument, 0, 'U'},
3225             {"start-offset", required_argument, 0, 's'},
3226             {"max-length", required_argument, 0, 'l'},
3227             {0, 0, 0, 0}
3228         };
3229         c = getopt_long(argc, argv, ":f:s:l:hU",
3230                         long_options, &option_index);
3231         if (c == -1) {
3232             break;
3233         }
3234         switch (c) {
3235         case ':':
3236             missing_argument(argv[optind - 1]);
3237             break;
3238         case '?':
3239             unrecognized_option(argv[optind - 1]);
3240             break;
3241         case 'h':
3242             help();
3243             break;
3244         case 'f':
3245             fmt = optarg;
3246             break;
3247         case 'U':
3248             force_share = true;
3249             break;
3250         case OPTION_OUTPUT:
3251             output = optarg;
3252             break;
3253         case 's':
3254             start_offset = cvtnum("start offset", optarg);
3255             if (start_offset < 0) {
3256                 return 1;
3257             }
3258             break;
3259         case 'l':
3260             max_length = cvtnum("max length", optarg);
3261             if (max_length < 0) {
3262                 return 1;
3263             }
3264             break;
3265         case OPTION_OBJECT:
3266             user_creatable_process_cmdline(optarg);
3267             break;
3268         case OPTION_IMAGE_OPTS:
3269             image_opts = true;
3270             break;
3271         }
3272     }
3273     if (optind != argc - 1) {
3274         error_exit("Expecting one image file name");
3275     }
3276     filename = argv[optind];
3277 
3278     if (output && !strcmp(output, "json")) {
3279         output_format = OFORMAT_JSON;
3280     } else if (output && !strcmp(output, "human")) {
3281         output_format = OFORMAT_HUMAN;
3282     } else if (output) {
3283         error_report("--output must be used with human or json as argument.");
3284         return 1;
3285     }
3286 
3287     blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
3288     if (!blk) {
3289         return 1;
3290     }
3291     bs = blk_bs(blk);
3292 
3293     if (output_format == OFORMAT_HUMAN) {
3294         printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
3295     } else if (output_format == OFORMAT_JSON) {
3296         putchar('[');
3297     }
3298 
3299     length = blk_getlength(blk);
3300     if (length < 0) {
3301         error_report("Failed to get size for '%s'", filename);
3302         return 1;
3303     }
3304     if (max_length != -1) {
3305         length = MIN(start_offset + max_length, length);
3306     }
3307 
3308     curr.start = start_offset;
3309     while (curr.start + curr.length < length) {
3310         int64_t offset = curr.start + curr.length;
3311         int64_t n = length - offset;
3312 
3313         ret = get_block_status(bs, offset, n, &next);
3314         if (ret < 0) {
3315             error_report("Could not read file metadata: %s", strerror(-ret));
3316             goto out;
3317         }
3318 
3319         if (entry_mergeable(&curr, &next)) {
3320             curr.length += next.length;
3321             continue;
3322         }
3323 
3324         if (curr.length > 0) {
3325             ret = dump_map_entry(output_format, &curr, &next);
3326             if (ret < 0) {
3327                 goto out;
3328             }
3329         }
3330         curr = next;
3331     }
3332 
3333     ret = dump_map_entry(output_format, &curr, NULL);
3334     if (output_format == OFORMAT_JSON) {
3335         puts("]");
3336     }
3337 
3338 out:
3339     blk_unref(blk);
3340     return ret < 0;
3341 }
3342 
3343 #define SNAPSHOT_LIST   1
3344 #define SNAPSHOT_CREATE 2
3345 #define SNAPSHOT_APPLY  3
3346 #define SNAPSHOT_DELETE 4
3347 
3348 static int img_snapshot(int argc, char **argv)
3349 {
3350     BlockBackend *blk;
3351     BlockDriverState *bs;
3352     QEMUSnapshotInfo sn;
3353     char *filename, *snapshot_name = NULL;
3354     int c, ret = 0, bdrv_oflags;
3355     int action = 0;
3356     bool quiet = false;
3357     Error *err = NULL;
3358     bool image_opts = false;
3359     bool force_share = false;
3360     int64_t rt;
3361 
3362     bdrv_oflags = BDRV_O_RDWR;
3363     /* Parse commandline parameters */
3364     for(;;) {
3365         static const struct option long_options[] = {
3366             {"help", no_argument, 0, 'h'},
3367             {"object", required_argument, 0, OPTION_OBJECT},
3368             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3369             {"force-share", no_argument, 0, 'U'},
3370             {0, 0, 0, 0}
3371         };
3372         c = getopt_long(argc, argv, ":la:c:d:hqU",
3373                         long_options, NULL);
3374         if (c == -1) {
3375             break;
3376         }
3377         switch(c) {
3378         case ':':
3379             missing_argument(argv[optind - 1]);
3380             break;
3381         case '?':
3382             unrecognized_option(argv[optind - 1]);
3383             break;
3384         case 'h':
3385             help();
3386             return 0;
3387         case 'l':
3388             if (action) {
3389                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3390                 return 0;
3391             }
3392             action = SNAPSHOT_LIST;
3393             bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
3394             break;
3395         case 'a':
3396             if (action) {
3397                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3398                 return 0;
3399             }
3400             action = SNAPSHOT_APPLY;
3401             snapshot_name = optarg;
3402             break;
3403         case 'c':
3404             if (action) {
3405                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3406                 return 0;
3407             }
3408             action = SNAPSHOT_CREATE;
3409             snapshot_name = optarg;
3410             break;
3411         case 'd':
3412             if (action) {
3413                 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
3414                 return 0;
3415             }
3416             action = SNAPSHOT_DELETE;
3417             snapshot_name = optarg;
3418             break;
3419         case 'q':
3420             quiet = true;
3421             break;
3422         case 'U':
3423             force_share = true;
3424             break;
3425         case OPTION_OBJECT:
3426             user_creatable_process_cmdline(optarg);
3427             break;
3428         case OPTION_IMAGE_OPTS:
3429             image_opts = true;
3430             break;
3431         }
3432     }
3433 
3434     if (optind != argc - 1) {
3435         error_exit("Expecting one image file name");
3436     }
3437     filename = argv[optind++];
3438 
3439     /* Open the image */
3440     blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
3441                    force_share);
3442     if (!blk) {
3443         return 1;
3444     }
3445     bs = blk_bs(blk);
3446 
3447     /* Perform the requested action */
3448     switch(action) {
3449     case SNAPSHOT_LIST:
3450         dump_snapshots(bs);
3451         break;
3452 
3453     case SNAPSHOT_CREATE:
3454         memset(&sn, 0, sizeof(sn));
3455         pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
3456 
3457         rt = g_get_real_time();
3458         sn.date_sec = rt / G_USEC_PER_SEC;
3459         sn.date_nsec = (rt % G_USEC_PER_SEC) * 1000;
3460 
3461         ret = bdrv_snapshot_create(bs, &sn);
3462         if (ret) {
3463             error_report("Could not create snapshot '%s': %d (%s)",
3464                 snapshot_name, ret, strerror(-ret));
3465         }
3466         break;
3467 
3468     case SNAPSHOT_APPLY:
3469         ret = bdrv_snapshot_goto(bs, snapshot_name, &err);
3470         if (ret) {
3471             error_reportf_err(err, "Could not apply snapshot '%s': ",
3472                               snapshot_name);
3473         }
3474         break;
3475 
3476     case SNAPSHOT_DELETE:
3477         ret = bdrv_snapshot_find(bs, &sn, snapshot_name);
3478         if (ret < 0) {
3479             error_report("Could not delete snapshot '%s': snapshot not "
3480                          "found", snapshot_name);
3481             ret = 1;
3482         } else {
3483             ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err);
3484             if (ret < 0) {
3485                 error_reportf_err(err, "Could not delete snapshot '%s': ",
3486                                   snapshot_name);
3487                 ret = 1;
3488             }
3489         }
3490         break;
3491     }
3492 
3493     /* Cleanup */
3494     blk_unref(blk);
3495     if (ret) {
3496         return 1;
3497     }
3498     return 0;
3499 }
3500 
3501 static int img_rebase(int argc, char **argv)
3502 {
3503     BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
3504     uint8_t *buf_old = NULL;
3505     uint8_t *buf_new = NULL;
3506     BlockDriverState *bs = NULL, *prefix_chain_bs = NULL;
3507     BlockDriverState *unfiltered_bs;
3508     char *filename;
3509     const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
3510     int c, flags, src_flags, ret;
3511     bool writethrough, src_writethrough;
3512     int unsafe = 0;
3513     bool force_share = false;
3514     int progress = 0;
3515     bool quiet = false;
3516     Error *local_err = NULL;
3517     bool image_opts = false;
3518 
3519     /* Parse commandline parameters */
3520     fmt = NULL;
3521     cache = BDRV_DEFAULT_CACHE;
3522     src_cache = BDRV_DEFAULT_CACHE;
3523     out_baseimg = NULL;
3524     out_basefmt = NULL;
3525     for(;;) {
3526         static const struct option long_options[] = {
3527             {"help", no_argument, 0, 'h'},
3528             {"object", required_argument, 0, OPTION_OBJECT},
3529             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3530             {"force-share", no_argument, 0, 'U'},
3531             {0, 0, 0, 0}
3532         };
3533         c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU",
3534                         long_options, NULL);
3535         if (c == -1) {
3536             break;
3537         }
3538         switch(c) {
3539         case ':':
3540             missing_argument(argv[optind - 1]);
3541             break;
3542         case '?':
3543             unrecognized_option(argv[optind - 1]);
3544             break;
3545         case 'h':
3546             help();
3547             return 0;
3548         case 'f':
3549             fmt = optarg;
3550             break;
3551         case 'F':
3552             out_basefmt = optarg;
3553             break;
3554         case 'b':
3555             out_baseimg = optarg;
3556             break;
3557         case 'u':
3558             unsafe = 1;
3559             break;
3560         case 'p':
3561             progress = 1;
3562             break;
3563         case 't':
3564             cache = optarg;
3565             break;
3566         case 'T':
3567             src_cache = optarg;
3568             break;
3569         case 'q':
3570             quiet = true;
3571             break;
3572         case OPTION_OBJECT:
3573             user_creatable_process_cmdline(optarg);
3574             break;
3575         case OPTION_IMAGE_OPTS:
3576             image_opts = true;
3577             break;
3578         case 'U':
3579             force_share = true;
3580             break;
3581         }
3582     }
3583 
3584     if (quiet) {
3585         progress = 0;
3586     }
3587 
3588     if (optind != argc - 1) {
3589         error_exit("Expecting one image file name");
3590     }
3591     if (!unsafe && !out_baseimg) {
3592         error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
3593     }
3594     filename = argv[optind++];
3595 
3596     qemu_progress_init(progress, 2.0);
3597     qemu_progress_print(0, 100);
3598 
3599     flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
3600     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
3601     if (ret < 0) {
3602         error_report("Invalid cache option: %s", cache);
3603         goto out;
3604     }
3605 
3606     src_flags = 0;
3607     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
3608     if (ret < 0) {
3609         error_report("Invalid source cache option: %s", src_cache);
3610         goto out;
3611     }
3612 
3613     /* The source files are opened read-only, don't care about WCE */
3614     assert((src_flags & BDRV_O_RDWR) == 0);
3615     (void) src_writethrough;
3616 
3617     /*
3618      * Open the images.
3619      *
3620      * Ignore the old backing file for unsafe rebase in case we want to correct
3621      * the reference to a renamed or moved backing file.
3622      */
3623     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
3624                    false);
3625     if (!blk) {
3626         ret = -1;
3627         goto out;
3628     }
3629     bs = blk_bs(blk);
3630 
3631     unfiltered_bs = bdrv_skip_filters(bs);
3632 
3633     if (out_basefmt != NULL) {
3634         if (bdrv_find_format(out_basefmt) == NULL) {
3635             error_report("Invalid format name: '%s'", out_basefmt);
3636             ret = -1;
3637             goto out;
3638         }
3639     }
3640 
3641     /* For safe rebasing we need to compare old and new backing file */
3642     if (!unsafe) {
3643         QDict *options = NULL;
3644         BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs);
3645 
3646         if (base_bs) {
3647             blk_old_backing = blk_new(qemu_get_aio_context(),
3648                                       BLK_PERM_CONSISTENT_READ,
3649                                       BLK_PERM_ALL);
3650             ret = blk_insert_bs(blk_old_backing, base_bs,
3651                                 &local_err);
3652             if (ret < 0) {
3653                 error_reportf_err(local_err,
3654                                   "Could not reuse old backing file '%s': ",
3655                                   base_bs->filename);
3656                 goto out;
3657             }
3658         } else {
3659             blk_old_backing = NULL;
3660         }
3661 
3662         if (out_baseimg[0]) {
3663             const char *overlay_filename;
3664             char *out_real_path;
3665 
3666             options = qdict_new();
3667             if (out_basefmt) {
3668                 qdict_put_str(options, "driver", out_basefmt);
3669             }
3670             if (force_share) {
3671                 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
3672             }
3673 
3674             bdrv_refresh_filename(bs);
3675             overlay_filename = bs->exact_filename[0] ? bs->exact_filename
3676                                                      : bs->filename;
3677             out_real_path =
3678                 bdrv_get_full_backing_filename_from_filename(overlay_filename,
3679                                                              out_baseimg,
3680                                                              &local_err);
3681             if (local_err) {
3682                 qobject_unref(options);
3683                 error_reportf_err(local_err,
3684                                   "Could not resolve backing filename: ");
3685                 ret = -1;
3686                 goto out;
3687             }
3688 
3689             /*
3690              * Find out whether we rebase an image on top of a previous image
3691              * in its chain.
3692              */
3693             prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path);
3694             if (prefix_chain_bs) {
3695                 qobject_unref(options);
3696                 g_free(out_real_path);
3697 
3698                 blk_new_backing = blk_new(qemu_get_aio_context(),
3699                                           BLK_PERM_CONSISTENT_READ,
3700                                           BLK_PERM_ALL);
3701                 ret = blk_insert_bs(blk_new_backing, prefix_chain_bs,
3702                                     &local_err);
3703                 if (ret < 0) {
3704                     error_reportf_err(local_err,
3705                                       "Could not reuse backing file '%s': ",
3706                                       out_baseimg);
3707                     goto out;
3708                 }
3709             } else {
3710                 blk_new_backing = blk_new_open(out_real_path, NULL,
3711                                                options, src_flags, &local_err);
3712                 g_free(out_real_path);
3713                 if (!blk_new_backing) {
3714                     error_reportf_err(local_err,
3715                                       "Could not open new backing file '%s': ",
3716                                       out_baseimg);
3717                     ret = -1;
3718                     goto out;
3719                 }
3720             }
3721         }
3722     }
3723 
3724     /*
3725      * Check each unallocated cluster in the COW file. If it is unallocated,
3726      * accesses go to the backing file. We must therefore compare this cluster
3727      * in the old and new backing file, and if they differ we need to copy it
3728      * from the old backing file into the COW file.
3729      *
3730      * If qemu-img crashes during this step, no harm is done. The content of
3731      * the image is the same as the original one at any time.
3732      */
3733     if (!unsafe) {
3734         int64_t size;
3735         int64_t old_backing_size = 0;
3736         int64_t new_backing_size = 0;
3737         uint64_t offset;
3738         int64_t n;
3739         float local_progress = 0;
3740 
3741         buf_old = blk_blockalign(blk, IO_BUF_SIZE);
3742         buf_new = blk_blockalign(blk, IO_BUF_SIZE);
3743 
3744         size = blk_getlength(blk);
3745         if (size < 0) {
3746             error_report("Could not get size of '%s': %s",
3747                          filename, strerror(-size));
3748             ret = -1;
3749             goto out;
3750         }
3751         if (blk_old_backing) {
3752             old_backing_size = blk_getlength(blk_old_backing);
3753             if (old_backing_size < 0) {
3754                 char backing_name[PATH_MAX];
3755 
3756                 bdrv_get_backing_filename(bs, backing_name,
3757                                           sizeof(backing_name));
3758                 error_report("Could not get size of '%s': %s",
3759                              backing_name, strerror(-old_backing_size));
3760                 ret = -1;
3761                 goto out;
3762             }
3763         }
3764         if (blk_new_backing) {
3765             new_backing_size = blk_getlength(blk_new_backing);
3766             if (new_backing_size < 0) {
3767                 error_report("Could not get size of '%s': %s",
3768                              out_baseimg, strerror(-new_backing_size));
3769                 ret = -1;
3770                 goto out;
3771             }
3772         }
3773 
3774         if (size != 0) {
3775             local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE));
3776         }
3777 
3778         for (offset = 0; offset < size; offset += n) {
3779             bool buf_old_is_zero = false;
3780 
3781             /* How many bytes can we handle with the next read? */
3782             n = MIN(IO_BUF_SIZE, size - offset);
3783 
3784             /* If the cluster is allocated, we don't need to take action */
3785             ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n);
3786             if (ret < 0) {
3787                 error_report("error while reading image metadata: %s",
3788                              strerror(-ret));
3789                 goto out;
3790             }
3791             if (ret) {
3792                 continue;
3793             }
3794 
3795             if (prefix_chain_bs) {
3796                 /*
3797                  * If cluster wasn't changed since prefix_chain, we don't need
3798                  * to take action
3799                  */
3800                 ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs),
3801                                               prefix_chain_bs, false,
3802                                               offset, n, &n);
3803                 if (ret < 0) {
3804                     error_report("error while reading image metadata: %s",
3805                                  strerror(-ret));
3806                     goto out;
3807                 }
3808                 if (!ret) {
3809                     continue;
3810                 }
3811             }
3812 
3813             /*
3814              * Read old and new backing file and take into consideration that
3815              * backing files may be smaller than the COW image.
3816              */
3817             if (offset >= old_backing_size) {
3818                 memset(buf_old, 0, n);
3819                 buf_old_is_zero = true;
3820             } else {
3821                 if (offset + n > old_backing_size) {
3822                     n = old_backing_size - offset;
3823                 }
3824 
3825                 ret = blk_pread(blk_old_backing, offset, n, buf_old, 0);
3826                 if (ret < 0) {
3827                     error_report("error while reading from old backing file");
3828                     goto out;
3829                 }
3830             }
3831 
3832             if (offset >= new_backing_size || !blk_new_backing) {
3833                 memset(buf_new, 0, n);
3834             } else {
3835                 if (offset + n > new_backing_size) {
3836                     n = new_backing_size - offset;
3837                 }
3838 
3839                 ret = blk_pread(blk_new_backing, offset, n, buf_new, 0);
3840                 if (ret < 0) {
3841                     error_report("error while reading from new backing file");
3842                     goto out;
3843                 }
3844             }
3845 
3846             /* If they differ, we need to write to the COW file */
3847             uint64_t written = 0;
3848 
3849             while (written < n) {
3850                 int64_t pnum;
3851 
3852                 if (compare_buffers(buf_old + written, buf_new + written,
3853                                     n - written, &pnum))
3854                 {
3855                     if (buf_old_is_zero) {
3856                         ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0);
3857                     } else {
3858                         ret = blk_pwrite(blk, offset + written, pnum,
3859                                          buf_old + written, 0);
3860                     }
3861                     if (ret < 0) {
3862                         error_report("Error while writing to COW image: %s",
3863                             strerror(-ret));
3864                         goto out;
3865                     }
3866                 }
3867 
3868                 written += pnum;
3869             }
3870             qemu_progress_print(local_progress, 100);
3871         }
3872     }
3873 
3874     /*
3875      * Change the backing file. All clusters that are different from the old
3876      * backing file are overwritten in the COW file now, so the visible content
3877      * doesn't change when we switch the backing file.
3878      */
3879     if (out_baseimg && *out_baseimg) {
3880         ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt,
3881                                        true);
3882     } else {
3883         ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false);
3884     }
3885 
3886     if (ret == -ENOSPC) {
3887         error_report("Could not change the backing file to '%s': No "
3888                      "space left in the file header", out_baseimg);
3889     } else if (ret == -EINVAL && out_baseimg && !out_basefmt) {
3890         error_report("Could not change the backing file to '%s': backing "
3891                      "format must be specified", out_baseimg);
3892     } else if (ret < 0) {
3893         error_report("Could not change the backing file to '%s': %s",
3894             out_baseimg, strerror(-ret));
3895     }
3896 
3897     qemu_progress_print(100, 0);
3898     /*
3899      * TODO At this point it is possible to check if any clusters that are
3900      * allocated in the COW file are the same in the backing file. If so, they
3901      * could be dropped from the COW file. Don't do this before switching the
3902      * backing file, in case of a crash this would lead to corruption.
3903      */
3904 out:
3905     qemu_progress_end();
3906     /* Cleanup */
3907     if (!unsafe) {
3908         blk_unref(blk_old_backing);
3909         blk_unref(blk_new_backing);
3910     }
3911     qemu_vfree(buf_old);
3912     qemu_vfree(buf_new);
3913 
3914     blk_unref(blk);
3915     if (ret) {
3916         return 1;
3917     }
3918     return 0;
3919 }
3920 
3921 static int img_resize(int argc, char **argv)
3922 {
3923     Error *err = NULL;
3924     int c, ret, relative;
3925     const char *filename, *fmt, *size;
3926     int64_t n, total_size, current_size;
3927     bool quiet = false;
3928     BlockBackend *blk = NULL;
3929     PreallocMode prealloc = PREALLOC_MODE_OFF;
3930     QemuOpts *param;
3931 
3932     static QemuOptsList resize_options = {
3933         .name = "resize_options",
3934         .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
3935         .desc = {
3936             {
3937                 .name = BLOCK_OPT_SIZE,
3938                 .type = QEMU_OPT_SIZE,
3939                 .help = "Virtual disk size"
3940             }, {
3941                 /* end of list */
3942             }
3943         },
3944     };
3945     bool image_opts = false;
3946     bool shrink = false;
3947 
3948     /* Remove size from argv manually so that negative numbers are not treated
3949      * as options by getopt. */
3950     if (argc < 3) {
3951         error_exit("Not enough arguments");
3952         return 1;
3953     }
3954 
3955     size = argv[--argc];
3956 
3957     /* Parse getopt arguments */
3958     fmt = NULL;
3959     for(;;) {
3960         static const struct option long_options[] = {
3961             {"help", no_argument, 0, 'h'},
3962             {"object", required_argument, 0, OPTION_OBJECT},
3963             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
3964             {"preallocation", required_argument, 0, OPTION_PREALLOCATION},
3965             {"shrink", no_argument, 0, OPTION_SHRINK},
3966             {0, 0, 0, 0}
3967         };
3968         c = getopt_long(argc, argv, ":f:hq",
3969                         long_options, NULL);
3970         if (c == -1) {
3971             break;
3972         }
3973         switch(c) {
3974         case ':':
3975             missing_argument(argv[optind - 1]);
3976             break;
3977         case '?':
3978             unrecognized_option(argv[optind - 1]);
3979             break;
3980         case 'h':
3981             help();
3982             break;
3983         case 'f':
3984             fmt = optarg;
3985             break;
3986         case 'q':
3987             quiet = true;
3988             break;
3989         case OPTION_OBJECT:
3990             user_creatable_process_cmdline(optarg);
3991             break;
3992         case OPTION_IMAGE_OPTS:
3993             image_opts = true;
3994             break;
3995         case OPTION_PREALLOCATION:
3996             prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
3997                                        PREALLOC_MODE__MAX, NULL);
3998             if (prealloc == PREALLOC_MODE__MAX) {
3999                 error_report("Invalid preallocation mode '%s'", optarg);
4000                 return 1;
4001             }
4002             break;
4003         case OPTION_SHRINK:
4004             shrink = true;
4005             break;
4006         }
4007     }
4008     if (optind != argc - 1) {
4009         error_exit("Expecting image file name and size");
4010     }
4011     filename = argv[optind++];
4012 
4013     /* Choose grow, shrink, or absolute resize mode */
4014     switch (size[0]) {
4015     case '+':
4016         relative = 1;
4017         size++;
4018         break;
4019     case '-':
4020         relative = -1;
4021         size++;
4022         break;
4023     default:
4024         relative = 0;
4025         break;
4026     }
4027 
4028     /* Parse size */
4029     param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
4030     if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) {
4031         error_report_err(err);
4032         ret = -1;
4033         qemu_opts_del(param);
4034         goto out;
4035     }
4036     n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
4037     qemu_opts_del(param);
4038 
4039     blk = img_open(image_opts, filename, fmt,
4040                    BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet,
4041                    false);
4042     if (!blk) {
4043         ret = -1;
4044         goto out;
4045     }
4046 
4047     current_size = blk_getlength(blk);
4048     if (current_size < 0) {
4049         error_report("Failed to inquire current image length: %s",
4050                      strerror(-current_size));
4051         ret = -1;
4052         goto out;
4053     }
4054 
4055     if (relative) {
4056         total_size = current_size + n * relative;
4057     } else {
4058         total_size = n;
4059     }
4060     if (total_size <= 0) {
4061         error_report("New image size must be positive");
4062         ret = -1;
4063         goto out;
4064     }
4065 
4066     if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) {
4067         error_report("Preallocation can only be used for growing images");
4068         ret = -1;
4069         goto out;
4070     }
4071 
4072     if (total_size < current_size && !shrink) {
4073         error_report("Use the --shrink option to perform a shrink operation.");
4074         warn_report("Shrinking an image will delete all data beyond the "
4075                     "shrunken image's end. Before performing such an "
4076                     "operation, make sure there is no important data there.");
4077         ret = -1;
4078         goto out;
4079     }
4080 
4081     /*
4082      * The user expects the image to have the desired size after
4083      * resizing, so pass @exact=true.  It is of no use to report
4084      * success when the image has not actually been resized.
4085      */
4086     ret = blk_truncate(blk, total_size, true, prealloc, 0, &err);
4087     if (!ret) {
4088         qprintf(quiet, "Image resized.\n");
4089     } else {
4090         error_report_err(err);
4091     }
4092 out:
4093     blk_unref(blk);
4094     if (ret) {
4095         return 1;
4096     }
4097     return 0;
4098 }
4099 
4100 static void amend_status_cb(BlockDriverState *bs,
4101                             int64_t offset, int64_t total_work_size,
4102                             void *opaque)
4103 {
4104     qemu_progress_print(100.f * offset / total_work_size, 0);
4105 }
4106 
4107 static int print_amend_option_help(const char *format)
4108 {
4109     BlockDriver *drv;
4110 
4111     /* Find driver and parse its options */
4112     drv = bdrv_find_format(format);
4113     if (!drv) {
4114         error_report("Unknown file format '%s'", format);
4115         return 1;
4116     }
4117 
4118     if (!drv->bdrv_amend_options) {
4119         error_report("Format driver '%s' does not support option amendment",
4120                      format);
4121         return 1;
4122     }
4123 
4124     /* Every driver supporting amendment must have amend_opts */
4125     assert(drv->amend_opts);
4126 
4127     printf("Amend options for '%s':\n", format);
4128     qemu_opts_print_help(drv->amend_opts, false);
4129     return 0;
4130 }
4131 
4132 static int img_amend(int argc, char **argv)
4133 {
4134     Error *err = NULL;
4135     int c, ret = 0;
4136     char *options = NULL;
4137     QemuOptsList *amend_opts = NULL;
4138     QemuOpts *opts = NULL;
4139     const char *fmt = NULL, *filename, *cache;
4140     int flags;
4141     bool writethrough;
4142     bool quiet = false, progress = false;
4143     BlockBackend *blk = NULL;
4144     BlockDriverState *bs = NULL;
4145     bool image_opts = false;
4146     bool force = false;
4147 
4148     cache = BDRV_DEFAULT_CACHE;
4149     for (;;) {
4150         static const struct option long_options[] = {
4151             {"help", no_argument, 0, 'h'},
4152             {"object", required_argument, 0, OPTION_OBJECT},
4153             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4154             {"force", no_argument, 0, OPTION_FORCE},
4155             {0, 0, 0, 0}
4156         };
4157         c = getopt_long(argc, argv, ":ho:f:t:pq",
4158                         long_options, NULL);
4159         if (c == -1) {
4160             break;
4161         }
4162 
4163         switch (c) {
4164         case ':':
4165             missing_argument(argv[optind - 1]);
4166             break;
4167         case '?':
4168             unrecognized_option(argv[optind - 1]);
4169             break;
4170         case 'h':
4171             help();
4172             break;
4173         case 'o':
4174             if (accumulate_options(&options, optarg) < 0) {
4175                 ret = -1;
4176                 goto out_no_progress;
4177             }
4178             break;
4179         case 'f':
4180             fmt = optarg;
4181             break;
4182         case 't':
4183             cache = optarg;
4184             break;
4185         case 'p':
4186             progress = true;
4187             break;
4188         case 'q':
4189             quiet = true;
4190             break;
4191         case OPTION_OBJECT:
4192             user_creatable_process_cmdline(optarg);
4193             break;
4194         case OPTION_IMAGE_OPTS:
4195             image_opts = true;
4196             break;
4197         case OPTION_FORCE:
4198             force = true;
4199             break;
4200         }
4201     }
4202 
4203     if (!options) {
4204         error_exit("Must specify options (-o)");
4205     }
4206 
4207     if (quiet) {
4208         progress = false;
4209     }
4210     qemu_progress_init(progress, 1.0);
4211 
4212     filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
4213     if (fmt && has_help_option(options)) {
4214         /* If a format is explicitly specified (and possibly no filename is
4215          * given), print option help here */
4216         ret = print_amend_option_help(fmt);
4217         goto out;
4218     }
4219 
4220     if (optind != argc - 1) {
4221         error_report("Expecting one image file name");
4222         ret = -1;
4223         goto out;
4224     }
4225 
4226     flags = BDRV_O_RDWR;
4227     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
4228     if (ret < 0) {
4229         error_report("Invalid cache option: %s", cache);
4230         goto out;
4231     }
4232 
4233     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4234                    false);
4235     if (!blk) {
4236         ret = -1;
4237         goto out;
4238     }
4239     bs = blk_bs(blk);
4240 
4241     fmt = bs->drv->format_name;
4242 
4243     if (has_help_option(options)) {
4244         /* If the format was auto-detected, print option help here */
4245         ret = print_amend_option_help(fmt);
4246         goto out;
4247     }
4248 
4249     if (!bs->drv->bdrv_amend_options) {
4250         error_report("Format driver '%s' does not support option amendment",
4251                      fmt);
4252         ret = -1;
4253         goto out;
4254     }
4255 
4256     /* Every driver supporting amendment must have amend_opts */
4257     assert(bs->drv->amend_opts);
4258 
4259     amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts);
4260     opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort);
4261     if (!qemu_opts_do_parse(opts, options, NULL, &err)) {
4262         /* Try to parse options using the create options */
4263         amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts);
4264         qemu_opts_del(opts);
4265         opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort);
4266         if (qemu_opts_do_parse(opts, options, NULL, NULL)) {
4267             error_append_hint(&err,
4268                               "This option is only supported for image creation\n");
4269         }
4270 
4271         error_report_err(err);
4272         ret = -1;
4273         goto out;
4274     }
4275 
4276     /* In case the driver does not call amend_status_cb() */
4277     qemu_progress_print(0.f, 0);
4278     ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err);
4279     qemu_progress_print(100.f, 0);
4280     if (ret < 0) {
4281         error_report_err(err);
4282         goto out;
4283     }
4284 
4285 out:
4286     qemu_progress_end();
4287 
4288 out_no_progress:
4289     blk_unref(blk);
4290     qemu_opts_del(opts);
4291     qemu_opts_free(amend_opts);
4292     g_free(options);
4293 
4294     if (ret) {
4295         return 1;
4296     }
4297     return 0;
4298 }
4299 
4300 typedef struct BenchData {
4301     BlockBackend *blk;
4302     uint64_t image_size;
4303     bool write;
4304     int bufsize;
4305     int step;
4306     int nrreq;
4307     int n;
4308     int flush_interval;
4309     bool drain_on_flush;
4310     uint8_t *buf;
4311     QEMUIOVector *qiov;
4312 
4313     int in_flight;
4314     bool in_flush;
4315     uint64_t offset;
4316 } BenchData;
4317 
4318 static void bench_undrained_flush_cb(void *opaque, int ret)
4319 {
4320     if (ret < 0) {
4321         error_report("Failed flush request: %s", strerror(-ret));
4322         exit(EXIT_FAILURE);
4323     }
4324 }
4325 
4326 static void bench_cb(void *opaque, int ret)
4327 {
4328     BenchData *b = opaque;
4329     BlockAIOCB *acb;
4330 
4331     if (ret < 0) {
4332         error_report("Failed request: %s", strerror(-ret));
4333         exit(EXIT_FAILURE);
4334     }
4335 
4336     if (b->in_flush) {
4337         /* Just finished a flush with drained queue: Start next requests */
4338         assert(b->in_flight == 0);
4339         b->in_flush = false;
4340     } else if (b->in_flight > 0) {
4341         int remaining = b->n - b->in_flight;
4342 
4343         b->n--;
4344         b->in_flight--;
4345 
4346         /* Time for flush? Drain queue if requested, then flush */
4347         if (b->flush_interval && remaining % b->flush_interval == 0) {
4348             if (!b->in_flight || !b->drain_on_flush) {
4349                 BlockCompletionFunc *cb;
4350 
4351                 if (b->drain_on_flush) {
4352                     b->in_flush = true;
4353                     cb = bench_cb;
4354                 } else {
4355                     cb = bench_undrained_flush_cb;
4356                 }
4357 
4358                 acb = blk_aio_flush(b->blk, cb, b);
4359                 if (!acb) {
4360                     error_report("Failed to issue flush request");
4361                     exit(EXIT_FAILURE);
4362                 }
4363             }
4364             if (b->drain_on_flush) {
4365                 return;
4366             }
4367         }
4368     }
4369 
4370     while (b->n > b->in_flight && b->in_flight < b->nrreq) {
4371         int64_t offset = b->offset;
4372         /* blk_aio_* might look for completed I/Os and kick bench_cb
4373          * again, so make sure this operation is counted by in_flight
4374          * and b->offset is ready for the next submission.
4375          */
4376         b->in_flight++;
4377         b->offset += b->step;
4378         b->offset %= b->image_size;
4379         if (b->write) {
4380             acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
4381         } else {
4382             acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
4383         }
4384         if (!acb) {
4385             error_report("Failed to issue request");
4386             exit(EXIT_FAILURE);
4387         }
4388     }
4389 }
4390 
4391 static int img_bench(int argc, char **argv)
4392 {
4393     int c, ret = 0;
4394     const char *fmt = NULL, *filename;
4395     bool quiet = false;
4396     bool image_opts = false;
4397     bool is_write = false;
4398     int count = 75000;
4399     int depth = 64;
4400     int64_t offset = 0;
4401     size_t bufsize = 4096;
4402     int pattern = 0;
4403     size_t step = 0;
4404     int flush_interval = 0;
4405     bool drain_on_flush = true;
4406     int64_t image_size;
4407     BlockBackend *blk = NULL;
4408     BenchData data = {};
4409     int flags = 0;
4410     bool writethrough = false;
4411     struct timeval t1, t2;
4412     int i;
4413     bool force_share = false;
4414     size_t buf_size = 0;
4415 
4416     for (;;) {
4417         static const struct option long_options[] = {
4418             {"help", no_argument, 0, 'h'},
4419             {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL},
4420             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4421             {"pattern", required_argument, 0, OPTION_PATTERN},
4422             {"no-drain", no_argument, 0, OPTION_NO_DRAIN},
4423             {"force-share", no_argument, 0, 'U'},
4424             {0, 0, 0, 0}
4425         };
4426         c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options,
4427                         NULL);
4428         if (c == -1) {
4429             break;
4430         }
4431 
4432         switch (c) {
4433         case ':':
4434             missing_argument(argv[optind - 1]);
4435             break;
4436         case '?':
4437             unrecognized_option(argv[optind - 1]);
4438             break;
4439         case 'h':
4440             help();
4441             break;
4442         case 'c':
4443         {
4444             unsigned long res;
4445 
4446             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4447                 error_report("Invalid request count specified");
4448                 return 1;
4449             }
4450             count = res;
4451             break;
4452         }
4453         case 'd':
4454         {
4455             unsigned long res;
4456 
4457             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4458                 error_report("Invalid queue depth specified");
4459                 return 1;
4460             }
4461             depth = res;
4462             break;
4463         }
4464         case 'f':
4465             fmt = optarg;
4466             break;
4467         case 'n':
4468             flags |= BDRV_O_NATIVE_AIO;
4469             break;
4470         case 'i':
4471             ret = bdrv_parse_aio(optarg, &flags);
4472             if (ret < 0) {
4473                 error_report("Invalid aio option: %s", optarg);
4474                 ret = -1;
4475                 goto out;
4476             }
4477             break;
4478         case 'o':
4479         {
4480             offset = cvtnum("offset", optarg);
4481             if (offset < 0) {
4482                 return 1;
4483             }
4484             break;
4485         }
4486             break;
4487         case 'q':
4488             quiet = true;
4489             break;
4490         case 's':
4491         {
4492             int64_t sval;
4493 
4494             sval = cvtnum_full("buffer size", optarg, 0, INT_MAX);
4495             if (sval < 0) {
4496                 return 1;
4497             }
4498 
4499             bufsize = sval;
4500             break;
4501         }
4502         case 'S':
4503         {
4504             int64_t sval;
4505 
4506             sval = cvtnum_full("step_size", optarg, 0, INT_MAX);
4507             if (sval < 0) {
4508                 return 1;
4509             }
4510 
4511             step = sval;
4512             break;
4513         }
4514         case 't':
4515             ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough);
4516             if (ret < 0) {
4517                 error_report("Invalid cache mode");
4518                 ret = -1;
4519                 goto out;
4520             }
4521             break;
4522         case 'w':
4523             flags |= BDRV_O_RDWR;
4524             is_write = true;
4525             break;
4526         case 'U':
4527             force_share = true;
4528             break;
4529         case OPTION_PATTERN:
4530         {
4531             unsigned long res;
4532 
4533             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
4534                 error_report("Invalid pattern byte specified");
4535                 return 1;
4536             }
4537             pattern = res;
4538             break;
4539         }
4540         case OPTION_FLUSH_INTERVAL:
4541         {
4542             unsigned long res;
4543 
4544             if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
4545                 error_report("Invalid flush interval specified");
4546                 return 1;
4547             }
4548             flush_interval = res;
4549             break;
4550         }
4551         case OPTION_NO_DRAIN:
4552             drain_on_flush = false;
4553             break;
4554         case OPTION_IMAGE_OPTS:
4555             image_opts = true;
4556             break;
4557         }
4558     }
4559 
4560     if (optind != argc - 1) {
4561         error_exit("Expecting one image file name");
4562     }
4563     filename = argv[argc - 1];
4564 
4565     if (!is_write && flush_interval) {
4566         error_report("--flush-interval is only available in write tests");
4567         ret = -1;
4568         goto out;
4569     }
4570     if (flush_interval && flush_interval < depth) {
4571         error_report("Flush interval can't be smaller than depth");
4572         ret = -1;
4573         goto out;
4574     }
4575 
4576     blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet,
4577                    force_share);
4578     if (!blk) {
4579         ret = -1;
4580         goto out;
4581     }
4582 
4583     image_size = blk_getlength(blk);
4584     if (image_size < 0) {
4585         ret = image_size;
4586         goto out;
4587     }
4588 
4589     data = (BenchData) {
4590         .blk            = blk,
4591         .image_size     = image_size,
4592         .bufsize        = bufsize,
4593         .step           = step ?: bufsize,
4594         .nrreq          = depth,
4595         .n              = count,
4596         .offset         = offset,
4597         .write          = is_write,
4598         .flush_interval = flush_interval,
4599         .drain_on_flush = drain_on_flush,
4600     };
4601     printf("Sending %d %s requests, %d bytes each, %d in parallel "
4602            "(starting at offset %" PRId64 ", step size %d)\n",
4603            data.n, data.write ? "write" : "read", data.bufsize, data.nrreq,
4604            data.offset, data.step);
4605     if (flush_interval) {
4606         printf("Sending flush every %d requests\n", flush_interval);
4607     }
4608 
4609     buf_size = data.nrreq * data.bufsize;
4610     data.buf = blk_blockalign(blk, buf_size);
4611     memset(data.buf, pattern, data.nrreq * data.bufsize);
4612 
4613     blk_register_buf(blk, data.buf, buf_size, &error_fatal);
4614 
4615     data.qiov = g_new(QEMUIOVector, data.nrreq);
4616     for (i = 0; i < data.nrreq; i++) {
4617         qemu_iovec_init(&data.qiov[i], 1);
4618         qemu_iovec_add(&data.qiov[i],
4619                        data.buf + i * data.bufsize, data.bufsize);
4620     }
4621 
4622     gettimeofday(&t1, NULL);
4623     bench_cb(&data, 0);
4624 
4625     while (data.n > 0) {
4626         main_loop_wait(false);
4627     }
4628     gettimeofday(&t2, NULL);
4629 
4630     printf("Run completed in %3.3f seconds.\n",
4631            (t2.tv_sec - t1.tv_sec)
4632            + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
4633 
4634 out:
4635     if (data.buf) {
4636         blk_unregister_buf(blk, data.buf, buf_size);
4637     }
4638     qemu_vfree(data.buf);
4639     blk_unref(blk);
4640 
4641     if (ret) {
4642         return 1;
4643     }
4644     return 0;
4645 }
4646 
4647 enum ImgBitmapAct {
4648     BITMAP_ADD,
4649     BITMAP_REMOVE,
4650     BITMAP_CLEAR,
4651     BITMAP_ENABLE,
4652     BITMAP_DISABLE,
4653     BITMAP_MERGE,
4654 };
4655 typedef struct ImgBitmapAction {
4656     enum ImgBitmapAct act;
4657     const char *src; /* only used for merge */
4658     QSIMPLEQ_ENTRY(ImgBitmapAction) next;
4659 } ImgBitmapAction;
4660 
4661 static int img_bitmap(int argc, char **argv)
4662 {
4663     Error *err = NULL;
4664     int c, ret = 1;
4665     QemuOpts *opts = NULL;
4666     const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL;
4667     const char *filename, *bitmap;
4668     BlockBackend *blk = NULL, *src = NULL;
4669     BlockDriverState *bs = NULL, *src_bs = NULL;
4670     bool image_opts = false;
4671     int64_t granularity = 0;
4672     bool add = false, merge = false;
4673     QSIMPLEQ_HEAD(, ImgBitmapAction) actions;
4674     ImgBitmapAction *act, *act_next;
4675     const char *op;
4676     int inactivate_ret;
4677 
4678     QSIMPLEQ_INIT(&actions);
4679 
4680     for (;;) {
4681         static const struct option long_options[] = {
4682             {"help", no_argument, 0, 'h'},
4683             {"object", required_argument, 0, OPTION_OBJECT},
4684             {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
4685             {"add", no_argument, 0, OPTION_ADD},
4686             {"remove", no_argument, 0, OPTION_REMOVE},
4687             {"clear", no_argument, 0, OPTION_CLEAR},
4688             {"enable", no_argument, 0, OPTION_ENABLE},
4689             {"disable", no_argument, 0, OPTION_DISABLE},
4690             {"merge", required_argument, 0, OPTION_MERGE},
4691             {"granularity", required_argument, 0, 'g'},
4692             {"source-file", required_argument, 0, 'b'},
4693             {"source-format", required_argument, 0, 'F'},
4694             {0, 0, 0, 0}
4695         };
4696         c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL);
4697         if (c == -1) {
4698             break;
4699         }
4700 
4701         switch (c) {
4702         case ':':
4703             missing_argument(argv[optind - 1]);
4704             break;
4705         case '?':
4706             unrecognized_option(argv[optind - 1]);
4707             break;
4708         case 'h':
4709             help();
4710             break;
4711         case 'b':
4712             src_filename = optarg;
4713             break;
4714         case 'f':
4715             fmt = optarg;
4716             break;
4717         case 'F':
4718             src_fmt = optarg;
4719             break;
4720         case 'g':
4721             granularity = cvtnum("granularity", optarg);
4722             if (granularity < 0) {
4723                 return 1;
4724             }
4725             break;
4726         case OPTION_ADD:
4727             act = g_new0(ImgBitmapAction, 1);
4728             act->act = BITMAP_ADD;
4729             QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4730             add = true;
4731             break;
4732         case OPTION_REMOVE:
4733             act = g_new0(ImgBitmapAction, 1);
4734             act->act = BITMAP_REMOVE;
4735             QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4736             break;
4737         case OPTION_CLEAR:
4738             act = g_new0(ImgBitmapAction, 1);
4739             act->act = BITMAP_CLEAR;
4740             QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4741             break;
4742         case OPTION_ENABLE:
4743             act = g_new0(ImgBitmapAction, 1);
4744             act->act = BITMAP_ENABLE;
4745             QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4746             break;
4747         case OPTION_DISABLE:
4748             act = g_new0(ImgBitmapAction, 1);
4749             act->act = BITMAP_DISABLE;
4750             QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4751             break;
4752         case OPTION_MERGE:
4753             act = g_new0(ImgBitmapAction, 1);
4754             act->act = BITMAP_MERGE;
4755             act->src = optarg;
4756             QSIMPLEQ_INSERT_TAIL(&actions, act, next);
4757             merge = true;
4758             break;
4759         case OPTION_OBJECT:
4760             user_creatable_process_cmdline(optarg);
4761             break;
4762         case OPTION_IMAGE_OPTS:
4763             image_opts = true;
4764             break;
4765         }
4766     }
4767 
4768     if (QSIMPLEQ_EMPTY(&actions)) {
4769         error_report("Need at least one of --add, --remove, --clear, "
4770                      "--enable, --disable, or --merge");
4771         goto out;
4772     }
4773 
4774     if (granularity && !add) {
4775         error_report("granularity only supported with --add");
4776         goto out;
4777     }
4778     if (src_fmt && !src_filename) {
4779         error_report("-F only supported with -b");
4780         goto out;
4781     }
4782     if (src_filename && !merge) {
4783         error_report("Merge bitmap source file only supported with "
4784                      "--merge");
4785         goto out;
4786     }
4787 
4788     if (optind != argc - 2) {
4789         error_report("Expecting filename and bitmap name");
4790         goto out;
4791     }
4792 
4793     filename = argv[optind];
4794     bitmap = argv[optind + 1];
4795 
4796     /*
4797      * No need to open backing chains; we will be manipulating bitmaps
4798      * directly in this image without reference to image contents.
4799      */
4800     blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING,
4801                    false, false, false);
4802     if (!blk) {
4803         goto out;
4804     }
4805     bs = blk_bs(blk);
4806     if (src_filename) {
4807         src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING,
4808                        false, false, false);
4809         if (!src) {
4810             goto out;
4811         }
4812         src_bs = blk_bs(src);
4813     } else {
4814         src_bs = bs;
4815     }
4816 
4817     QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) {
4818         switch (act->act) {
4819         case BITMAP_ADD:
4820             qmp_block_dirty_bitmap_add(bs->node_name, bitmap,
4821                                        !!granularity, granularity, true, true,
4822                                        false, false, &err);
4823             op = "add";
4824             break;
4825         case BITMAP_REMOVE:
4826             qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err);
4827             op = "remove";
4828             break;
4829         case BITMAP_CLEAR:
4830             qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err);
4831             op = "clear";
4832             break;
4833         case BITMAP_ENABLE:
4834             qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err);
4835             op = "enable";
4836             break;
4837         case BITMAP_DISABLE:
4838             qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err);
4839             op = "disable";
4840             break;
4841         case BITMAP_MERGE:
4842             do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name,
4843                                   act->src, &err);
4844             op = "merge";
4845             break;
4846         default:
4847             g_assert_not_reached();
4848         }
4849 
4850         if (err) {
4851             error_reportf_err(err, "Operation %s on bitmap %s failed: ",
4852                               op, bitmap);
4853             goto out;
4854         }
4855         g_free(act);
4856     }
4857 
4858     ret = 0;
4859 
4860  out:
4861     /*
4862      * Manually inactivate the images first because this way we can know whether
4863      * an error occurred. blk_unref() doesn't tell us about failures.
4864      */
4865     inactivate_ret = bdrv_inactivate_all();
4866     if (inactivate_ret < 0) {
4867         error_report("Error while closing the image: %s", strerror(-inactivate_ret));
4868         ret = 1;
4869     }
4870 
4871     blk_unref(src);
4872     blk_unref(blk);
4873     qemu_opts_del(opts);
4874     return ret;
4875 }
4876 
4877 #define C_BS      01
4878 #define C_COUNT   02
4879 #define C_IF      04
4880 #define C_OF      010
4881 #define C_SKIP    020
4882 
4883 struct DdInfo {
4884     unsigned int flags;
4885     int64_t count;
4886 };
4887 
4888 struct DdIo {
4889     int bsz;    /* Block size */
4890     char *filename;
4891     uint8_t *buf;
4892     int64_t offset;
4893 };
4894 
4895 struct DdOpts {
4896     const char *name;
4897     int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *);
4898     unsigned int flag;
4899 };
4900 
4901 static int img_dd_bs(const char *arg,
4902                      struct DdIo *in, struct DdIo *out,
4903                      struct DdInfo *dd)
4904 {
4905     int64_t res;
4906 
4907     res = cvtnum_full("bs", arg, 1, INT_MAX);
4908 
4909     if (res < 0) {
4910         return 1;
4911     }
4912     in->bsz = out->bsz = res;
4913 
4914     return 0;
4915 }
4916 
4917 static int img_dd_count(const char *arg,
4918                         struct DdIo *in, struct DdIo *out,
4919                         struct DdInfo *dd)
4920 {
4921     dd->count = cvtnum("count", arg);
4922 
4923     if (dd->count < 0) {
4924         return 1;
4925     }
4926 
4927     return 0;
4928 }
4929 
4930 static int img_dd_if(const char *arg,
4931                      struct DdIo *in, struct DdIo *out,
4932                      struct DdInfo *dd)
4933 {
4934     in->filename = g_strdup(arg);
4935 
4936     return 0;
4937 }
4938 
4939 static int img_dd_of(const char *arg,
4940                      struct DdIo *in, struct DdIo *out,
4941                      struct DdInfo *dd)
4942 {
4943     out->filename = g_strdup(arg);
4944 
4945     return 0;
4946 }
4947 
4948 static int img_dd_skip(const char *arg,
4949                        struct DdIo *in, struct DdIo *out,
4950                        struct DdInfo *dd)
4951 {
4952     in->offset = cvtnum("skip", arg);
4953 
4954     if (in->offset < 0) {
4955         return 1;
4956     }
4957 
4958     return 0;
4959 }
4960 
4961 static int img_dd(int argc, char **argv)
4962 {
4963     int ret = 0;
4964     char *arg = NULL;
4965     char *tmp;
4966     BlockDriver *drv = NULL, *proto_drv = NULL;
4967     BlockBackend *blk1 = NULL, *blk2 = NULL;
4968     QemuOpts *opts = NULL;
4969     QemuOptsList *create_opts = NULL;
4970     Error *local_err = NULL;
4971     bool image_opts = false;
4972     int c, i;
4973     const char *out_fmt = "raw";
4974     const char *fmt = NULL;
4975     int64_t size = 0;
4976     int64_t out_pos, in_pos;
4977     bool force_share = false;
4978     struct DdInfo dd = {
4979         .flags = 0,
4980         .count = 0,
4981     };
4982     struct DdIo in = {
4983         .bsz = 512, /* Block size is by default 512 bytes */
4984         .filename = NULL,
4985         .buf = NULL,
4986         .offset = 0
4987     };
4988     struct DdIo out = {
4989         .bsz = 512,
4990         .filename = NULL,
4991         .buf = NULL,
4992         .offset = 0
4993     };
4994 
4995     const struct DdOpts options[] = {
4996         { "bs", img_dd_bs, C_BS },
4997         { "count", img_dd_count, C_COUNT },
4998         { "if", img_dd_if, C_IF },
4999         { "of", img_dd_of, C_OF },
5000         { "skip", img_dd_skip, C_SKIP },
5001         { NULL, NULL, 0 }
5002     };
5003     const struct option long_options[] = {
5004         { "help", no_argument, 0, 'h'},
5005         { "object", required_argument, 0, OPTION_OBJECT},
5006         { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
5007         { "force-share", no_argument, 0, 'U'},
5008         { 0, 0, 0, 0 }
5009     };
5010 
5011     while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) {
5012         if (c == EOF) {
5013             break;
5014         }
5015         switch (c) {
5016         case 'O':
5017             out_fmt = optarg;
5018             break;
5019         case 'f':
5020             fmt = optarg;
5021             break;
5022         case ':':
5023             missing_argument(argv[optind - 1]);
5024             break;
5025         case '?':
5026             unrecognized_option(argv[optind - 1]);
5027             break;
5028         case 'h':
5029             help();
5030             break;
5031         case 'U':
5032             force_share = true;
5033             break;
5034         case OPTION_OBJECT:
5035             user_creatable_process_cmdline(optarg);
5036             break;
5037         case OPTION_IMAGE_OPTS:
5038             image_opts = true;
5039             break;
5040         }
5041     }
5042 
5043     for (i = optind; i < argc; i++) {
5044         int j;
5045         arg = g_strdup(argv[i]);
5046 
5047         tmp = strchr(arg, '=');
5048         if (tmp == NULL) {
5049             error_report("unrecognized operand %s", arg);
5050             ret = -1;
5051             goto out;
5052         }
5053 
5054         *tmp++ = '\0';
5055 
5056         for (j = 0; options[j].name != NULL; j++) {
5057             if (!strcmp(arg, options[j].name)) {
5058                 break;
5059             }
5060         }
5061         if (options[j].name == NULL) {
5062             error_report("unrecognized operand %s", arg);
5063             ret = -1;
5064             goto out;
5065         }
5066 
5067         if (options[j].f(tmp, &in, &out, &dd) != 0) {
5068             ret = -1;
5069             goto out;
5070         }
5071         dd.flags |= options[j].flag;
5072         g_free(arg);
5073         arg = NULL;
5074     }
5075 
5076     if (!(dd.flags & C_IF && dd.flags & C_OF)) {
5077         error_report("Must specify both input and output files");
5078         ret = -1;
5079         goto out;
5080     }
5081 
5082     blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
5083                     force_share);
5084 
5085     if (!blk1) {
5086         ret = -1;
5087         goto out;
5088     }
5089 
5090     drv = bdrv_find_format(out_fmt);
5091     if (!drv) {
5092         error_report("Unknown file format");
5093         ret = -1;
5094         goto out;
5095     }
5096     proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
5097 
5098     if (!proto_drv) {
5099         error_report_err(local_err);
5100         ret = -1;
5101         goto out;
5102     }
5103     if (!drv->create_opts) {
5104         error_report("Format driver '%s' does not support image creation",
5105                      drv->format_name);
5106         ret = -1;
5107         goto out;
5108     }
5109     if (!proto_drv->create_opts) {
5110         error_report("Protocol driver '%s' does not support image creation",
5111                      proto_drv->format_name);
5112         ret = -1;
5113         goto out;
5114     }
5115     create_opts = qemu_opts_append(create_opts, drv->create_opts);
5116     create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
5117 
5118     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
5119 
5120     size = blk_getlength(blk1);
5121     if (size < 0) {
5122         error_report("Failed to get size for '%s'", in.filename);
5123         ret = -1;
5124         goto out;
5125     }
5126 
5127     if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
5128         dd.count * in.bsz < size) {
5129         size = dd.count * in.bsz;
5130     }
5131 
5132     /* Overflow means the specified offset is beyond input image's size */
5133     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
5134                               size < in.bsz * in.offset)) {
5135         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
5136     } else {
5137         qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
5138                             size - in.bsz * in.offset, &error_abort);
5139     }
5140 
5141     ret = bdrv_create(drv, out.filename, opts, &local_err);
5142     if (ret < 0) {
5143         error_reportf_err(local_err,
5144                           "%s: error while creating output image: ",
5145                           out.filename);
5146         ret = -1;
5147         goto out;
5148     }
5149 
5150     /* TODO, we can't honour --image-opts for the target,
5151      * since it needs to be given in a format compatible
5152      * with the bdrv_create() call above which does not
5153      * support image-opts style.
5154      */
5155     blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
5156                          false, false, false);
5157 
5158     if (!blk2) {
5159         ret = -1;
5160         goto out;
5161     }
5162 
5163     if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
5164                               size < in.offset * in.bsz)) {
5165         /* We give a warning if the skip option is bigger than the input
5166          * size and create an empty output disk image (i.e. like dd(1)).
5167          */
5168         error_report("%s: cannot skip to specified offset", in.filename);
5169         in_pos = size;
5170     } else {
5171         in_pos = in.offset * in.bsz;
5172     }
5173 
5174     in.buf = g_new(uint8_t, in.bsz);
5175 
5176     for (out_pos = 0; in_pos < size; ) {
5177         int bytes = (in_pos + in.bsz > size) ? size - in_pos : in.bsz;
5178 
5179         ret = blk_pread(blk1, in_pos, bytes, in.buf, 0);
5180         if (ret < 0) {
5181             error_report("error while reading from input image file: %s",
5182                          strerror(-ret));
5183             goto out;
5184         }
5185         in_pos += bytes;
5186 
5187         ret = blk_pwrite(blk2, out_pos, bytes, in.buf, 0);
5188         if (ret < 0) {
5189             error_report("error while writing to output image file: %s",
5190                          strerror(-ret));
5191             goto out;
5192         }
5193         out_pos += bytes;
5194     }
5195 
5196 out:
5197     g_free(arg);
5198     qemu_opts_del(opts);
5199     qemu_opts_free(create_opts);
5200     blk_unref(blk1);
5201     blk_unref(blk2);
5202     g_free(in.filename);
5203     g_free(out.filename);
5204     g_free(in.buf);
5205     g_free(out.buf);
5206 
5207     if (ret) {
5208         return 1;
5209     }
5210     return 0;
5211 }
5212 
5213 static void dump_json_block_measure_info(BlockMeasureInfo *info)
5214 {
5215     GString *str;
5216     QObject *obj;
5217     Visitor *v = qobject_output_visitor_new(&obj);
5218 
5219     visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort);
5220     visit_complete(v, &obj);
5221     str = qobject_to_json_pretty(obj, true);
5222     assert(str != NULL);
5223     printf("%s\n", str->str);
5224     qobject_unref(obj);
5225     visit_free(v);
5226     g_string_free(str, true);
5227 }
5228 
5229 static int img_measure(int argc, char **argv)
5230 {
5231     static const struct option long_options[] = {
5232         {"help", no_argument, 0, 'h'},
5233         {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
5234         {"object", required_argument, 0, OPTION_OBJECT},
5235         {"output", required_argument, 0, OPTION_OUTPUT},
5236         {"size", required_argument, 0, OPTION_SIZE},
5237         {"force-share", no_argument, 0, 'U'},
5238         {0, 0, 0, 0}
5239     };
5240     OutputFormat output_format = OFORMAT_HUMAN;
5241     BlockBackend *in_blk = NULL;
5242     BlockDriver *drv;
5243     const char *filename = NULL;
5244     const char *fmt = NULL;
5245     const char *out_fmt = "raw";
5246     char *options = NULL;
5247     char *snapshot_name = NULL;
5248     bool force_share = false;
5249     QemuOpts *opts = NULL;
5250     QemuOpts *object_opts = NULL;
5251     QemuOpts *sn_opts = NULL;
5252     QemuOptsList *create_opts = NULL;
5253     bool image_opts = false;
5254     uint64_t img_size = UINT64_MAX;
5255     BlockMeasureInfo *info = NULL;
5256     Error *local_err = NULL;
5257     int ret = 1;
5258     int c;
5259 
5260     while ((c = getopt_long(argc, argv, "hf:O:o:l:U",
5261                             long_options, NULL)) != -1) {
5262         switch (c) {
5263         case '?':
5264         case 'h':
5265             help();
5266             break;
5267         case 'f':
5268             fmt = optarg;
5269             break;
5270         case 'O':
5271             out_fmt = optarg;
5272             break;
5273         case 'o':
5274             if (accumulate_options(&options, optarg) < 0) {
5275                 goto out;
5276             }
5277             break;
5278         case 'l':
5279             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
5280                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
5281                                                   optarg, false);
5282                 if (!sn_opts) {
5283                     error_report("Failed in parsing snapshot param '%s'",
5284                                  optarg);
5285                     goto out;
5286                 }
5287             } else {
5288                 snapshot_name = optarg;
5289             }
5290             break;
5291         case 'U':
5292             force_share = true;
5293             break;
5294         case OPTION_OBJECT:
5295             user_creatable_process_cmdline(optarg);
5296             break;
5297         case OPTION_IMAGE_OPTS:
5298             image_opts = true;
5299             break;
5300         case OPTION_OUTPUT:
5301             if (!strcmp(optarg, "json")) {
5302                 output_format = OFORMAT_JSON;
5303             } else if (!strcmp(optarg, "human")) {
5304                 output_format = OFORMAT_HUMAN;
5305             } else {
5306                 error_report("--output must be used with human or json "
5307                              "as argument.");
5308                 goto out;
5309             }
5310             break;
5311         case OPTION_SIZE:
5312         {
5313             int64_t sval;
5314 
5315             sval = cvtnum("image size", optarg);
5316             if (sval < 0) {
5317                 goto out;
5318             }
5319             img_size = (uint64_t)sval;
5320         }
5321         break;
5322         }
5323     }
5324 
5325     if (argc - optind > 1) {
5326         error_report("At most one filename argument is allowed.");
5327         goto out;
5328     } else if (argc - optind == 1) {
5329         filename = argv[optind];
5330     }
5331 
5332     if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) {
5333         error_report("--image-opts, -f, and -l require a filename argument.");
5334         goto out;
5335     }
5336     if (filename && img_size != UINT64_MAX) {
5337         error_report("--size N cannot be used together with a filename.");
5338         goto out;
5339     }
5340     if (!filename && img_size == UINT64_MAX) {
5341         error_report("Either --size N or one filename must be specified.");
5342         goto out;
5343     }
5344 
5345     if (filename) {
5346         in_blk = img_open(image_opts, filename, fmt, 0,
5347                           false, false, force_share);
5348         if (!in_blk) {
5349             goto out;
5350         }
5351 
5352         if (sn_opts) {
5353             bdrv_snapshot_load_tmp(blk_bs(in_blk),
5354                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
5355                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
5356                     &local_err);
5357         } else if (snapshot_name != NULL) {
5358             bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk),
5359                     snapshot_name, &local_err);
5360         }
5361         if (local_err) {
5362             error_reportf_err(local_err, "Failed to load snapshot: ");
5363             goto out;
5364         }
5365     }
5366 
5367     drv = bdrv_find_format(out_fmt);
5368     if (!drv) {
5369         error_report("Unknown file format '%s'", out_fmt);
5370         goto out;
5371     }
5372     if (!drv->create_opts) {
5373         error_report("Format driver '%s' does not support image creation",
5374                      drv->format_name);
5375         goto out;
5376     }
5377 
5378     create_opts = qemu_opts_append(create_opts, drv->create_opts);
5379     create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts);
5380     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
5381     if (options) {
5382         if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) {
5383             error_report_err(local_err);
5384             error_report("Invalid options for file format '%s'", out_fmt);
5385             goto out;
5386         }
5387     }
5388     if (img_size != UINT64_MAX) {
5389         qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort);
5390     }
5391 
5392     info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err);
5393     if (local_err) {
5394         error_report_err(local_err);
5395         goto out;
5396     }
5397 
5398     if (output_format == OFORMAT_HUMAN) {
5399         printf("required size: %" PRIu64 "\n", info->required);
5400         printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated);
5401         if (info->has_bitmaps) {
5402             printf("bitmaps size: %" PRIu64 "\n", info->bitmaps);
5403         }
5404     } else {
5405         dump_json_block_measure_info(info);
5406     }
5407 
5408     ret = 0;
5409 
5410 out:
5411     qapi_free_BlockMeasureInfo(info);
5412     qemu_opts_del(object_opts);
5413     qemu_opts_del(opts);
5414     qemu_opts_del(sn_opts);
5415     qemu_opts_free(create_opts);
5416     g_free(options);
5417     blk_unref(in_blk);
5418     return ret;
5419 }
5420 
5421 static const img_cmd_t img_cmds[] = {
5422 #define DEF(option, callback, arg_string)        \
5423     { option, callback },
5424 #include "qemu-img-cmds.h"
5425 #undef DEF
5426     { NULL, NULL, },
5427 };
5428 
5429 int main(int argc, char **argv)
5430 {
5431     const img_cmd_t *cmd;
5432     const char *cmdname;
5433     int c;
5434     static const struct option long_options[] = {
5435         {"help", no_argument, 0, 'h'},
5436         {"version", no_argument, 0, 'V'},
5437         {"trace", required_argument, NULL, 'T'},
5438         {0, 0, 0, 0}
5439     };
5440 
5441 #ifdef CONFIG_POSIX
5442     signal(SIGPIPE, SIG_IGN);
5443 #endif
5444 
5445     socket_init();
5446     error_init(argv[0]);
5447     module_call_init(MODULE_INIT_TRACE);
5448     qemu_init_exec_dir(argv[0]);
5449 
5450     qemu_init_main_loop(&error_fatal);
5451 
5452     qcrypto_init(&error_fatal);
5453 
5454     module_call_init(MODULE_INIT_QOM);
5455     bdrv_init();
5456     if (argc < 2) {
5457         error_exit("Not enough arguments");
5458     }
5459 
5460     qemu_add_opts(&qemu_source_opts);
5461     qemu_add_opts(&qemu_trace_opts);
5462 
5463     while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) {
5464         switch (c) {
5465         case ':':
5466             missing_argument(argv[optind - 1]);
5467             return 0;
5468         case '?':
5469             unrecognized_option(argv[optind - 1]);
5470             return 0;
5471         case 'h':
5472             help();
5473             return 0;
5474         case 'V':
5475             printf(QEMU_IMG_VERSION);
5476             return 0;
5477         case 'T':
5478             trace_opt_parse(optarg);
5479             break;
5480         }
5481     }
5482 
5483     cmdname = argv[optind];
5484 
5485     /* reset getopt_long scanning */
5486     argc -= optind;
5487     if (argc < 1) {
5488         return 0;
5489     }
5490     argv += optind;
5491     qemu_reset_optind();
5492 
5493     if (!trace_init_backends()) {
5494         exit(1);
5495     }
5496     trace_init_file();
5497     qemu_set_log(LOG_TRACE, &error_fatal);
5498 
5499     /* find the command */
5500     for (cmd = img_cmds; cmd->name != NULL; cmd++) {
5501         if (!strcmp(cmdname, cmd->name)) {
5502             return cmd->handler(argc, argv);
5503         }
5504     }
5505 
5506     /* not found */
5507     error_exit("Command not found: %s", cmdname);
5508 }
5509