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