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