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