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