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