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