blob: 45a9570933690c83203c7802e17582c1e687f230 [file] [log] [blame]
Kevin Wolf797ac582013-06-05 14:19:31 +02001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
Eric Blake093ea232016-05-07 21:16:43 -06004 * Copyright (C) 2009-2016 Red Hat, Inc.
Kevin Wolf797ac582013-06-05 14:19:31 +02005 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010012#include "qapi/error.h"
Alberto Garciadc900c32018-11-12 16:00:42 +020013#include "qapi/qmp/qdict.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020014#include "qemu-io.h"
Max Reitz4c7b7e92015-02-05 13:58:22 -050015#include "sysemu/block-backend.h"
16#include "block/block.h"
17#include "block/block_int.h" /* for info_f() */
Max Reitza8d8ecb2013-10-09 10:46:17 +020018#include "block/qapi.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010019#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010020#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010021#include "qemu/option.h"
Kevin Wolfcd33d022014-01-15 15:39:10 +010022#include "qemu/timer.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020023#include "qemu/cutils.h"
Kevin Wolf797ac582013-06-05 14:19:31 +020024
25#define CMD_NOFILE_OK 0x01
26
Stefan Weilf9883882014-03-05 22:23:00 +010027bool qemuio_misalign;
Kevin Wolf797ac582013-06-05 14:19:31 +020028
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020029static cmdinfo_t *cmdtab;
30static int ncmds;
31
32static int compare_cmdname(const void *a, const void *b)
33{
34 return strcmp(((const cmdinfo_t *)a)->name,
35 ((const cmdinfo_t *)b)->name);
36}
37
38void qemuio_add_command(const cmdinfo_t *ci)
39{
Peter Maydell6aabeb52017-03-31 14:38:49 +010040 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
41 * flags allow it not to be, so that combination is invalid.
42 * Catch it now rather than letting it manifest as a crash if a
43 * particular set of command line options are used.
44 */
45 assert(ci->perm == 0 ||
46 (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
Markus Armbruster02c4f262014-08-19 10:31:09 +020047 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020048 cmdtab[ncmds - 1] = *ci;
49 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
50}
51
Max Reitzb444d0e2018-05-09 21:42:58 +020052void qemuio_command_usage(const cmdinfo_t *ci)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020053{
54 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020055}
56
Max Reitz4c7b7e92015-02-05 13:58:22 -050057static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020058{
59 if (ct->flags & CMD_FLAG_GLOBAL) {
60 return 1;
61 }
Max Reitz4c7b7e92015-02-05 13:58:22 -050062 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020063 fprintf(stderr, "no file open, try 'help open'\n");
64 return 0;
65 }
66 return 1;
67}
68
Max Reitzb32d7a32018-05-09 21:42:59 +020069static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
70 char **argv)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020071{
72 char *cmd = argv[0];
73
Max Reitz4c7b7e92015-02-05 13:58:22 -050074 if (!init_check_command(blk, ct)) {
Max Reitzb32d7a32018-05-09 21:42:59 +020075 return -EINVAL;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020076 }
77
78 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
79 if (ct->argmax == -1) {
80 fprintf(stderr,
81 "bad argument count %d to %s, expected at least %d arguments\n",
82 argc-1, cmd, ct->argmin);
83 } else if (ct->argmin == ct->argmax) {
84 fprintf(stderr,
85 "bad argument count %d to %s, expected %d arguments\n",
86 argc-1, cmd, ct->argmin);
87 } else {
88 fprintf(stderr,
89 "bad argument count %d to %s, expected between %d and %d arguments\n",
90 argc-1, cmd, ct->argmin, ct->argmax);
91 }
Max Reitzb32d7a32018-05-09 21:42:59 +020092 return -EINVAL;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020093 }
Kevin Wolf887354b2017-02-10 16:24:56 +010094
Vladimir Sementsov-Ogievskiy8eaf1012021-05-19 12:05:32 +030095 /*
96 * Request additional permissions if necessary for this command. The caller
Kevin Wolf887354b2017-02-10 16:24:56 +010097 * is responsible for restoring the original permissions afterwards if this
Vladimir Sementsov-Ogievskiy8eaf1012021-05-19 12:05:32 +030098 * is what it wants.
99 *
100 * Coverity thinks that blk may be NULL in the following if condition. It's
101 * not so: in init_check_command() we fail if blk is NULL for command with
102 * both CMD_FLAG_GLOBAL and CMD_NOFILE_OK flags unset. And in
103 * qemuio_add_command() we assert that command with non-zero .perm field
104 * doesn't set this flags. So, the following assertion is to silence
105 * Coverity:
106 */
107 assert(blk || !ct->perm);
Kevin Wolf887354b2017-02-10 16:24:56 +0100108 if (ct->perm && blk_is_available(blk)) {
109 uint64_t orig_perm, orig_shared_perm;
110 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
111
112 if (ct->perm & ~orig_perm) {
113 uint64_t new_perm;
114 Error *local_err = NULL;
115 int ret;
116
117 new_perm = orig_perm | ct->perm;
118
119 ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
120 if (ret < 0) {
121 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +0200122 return ret;
Kevin Wolf887354b2017-02-10 16:24:56 +0100123 }
124 }
125 }
126
Richard W.M. Jonesd339d762019-01-18 10:11:14 +0000127 qemu_reset_optind();
Max Reitzb32d7a32018-05-09 21:42:59 +0200128 return ct->cfunc(blk, argc, argv);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200129}
130
131static const cmdinfo_t *find_command(const char *cmd)
132{
133 cmdinfo_t *ct;
134
135 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
136 if (strcmp(ct->name, cmd) == 0 ||
137 (ct->altname && strcmp(ct->altname, cmd) == 0))
138 {
139 return (const cmdinfo_t *)ct;
140 }
141 }
142 return NULL;
143}
144
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100145/* Invoke fn() for commands with a matching prefix */
146void qemuio_complete_command(const char *input,
147 void (*fn)(const char *cmd, void *opaque),
148 void *opaque)
149{
150 cmdinfo_t *ct;
151 size_t input_len = strlen(input);
152
153 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
154 if (strncmp(input, ct->name, input_len) == 0) {
155 fn(ct->name, opaque);
156 }
157 }
158}
159
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200160static char **breakline(char *input, int *count)
161{
162 int c = 0;
163 char *p;
Markus Armbruster5839e532014-08-19 10:31:08 +0200164 char **rval = g_new0(char *, 1);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200165
166 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
167 if (!*p) {
168 continue;
169 }
170 c++;
Markus Armbruster08193dd2014-08-19 10:31:10 +0200171 rval = g_renew(char *, rval, (c + 1));
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200172 rval[c - 1] = p;
173 rval[c] = NULL;
174 }
175 *count = c;
176 return rval;
177}
178
Kevin Wolf797ac582013-06-05 14:19:31 +0200179static int64_t cvtnum(const char *s)
180{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100181 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100182 uint64_t value;
John Snowef5a7882015-11-05 18:53:03 -0500183
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100184 err = qemu_strtosz(s, NULL, &value);
185 if (err < 0) {
186 return err;
187 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100188 if (value > INT64_MAX) {
189 return -ERANGE;
190 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100191 return value;
Kevin Wolf797ac582013-06-05 14:19:31 +0200192}
193
John Snowa9ecfa02015-11-05 18:53:04 -0500194static void print_cvtnum_err(int64_t rc, const char *arg)
195{
196 switch (rc) {
197 case -EINVAL:
198 printf("Parsing error: non-numeric argument,"
199 " or extraneous/unrecognized suffix -- %s\n", arg);
200 break;
201 case -ERANGE:
202 printf("Parsing error: argument too large -- %s\n", arg);
203 break;
204 default:
205 printf("Parsing error: %s\n", arg);
206 }
207}
208
Kevin Wolf0b613882013-06-05 14:19:38 +0200209#define EXABYTES(x) ((long long)(x) << 60)
210#define PETABYTES(x) ((long long)(x) << 50)
211#define TERABYTES(x) ((long long)(x) << 40)
212#define GIGABYTES(x) ((long long)(x) << 30)
213#define MEGABYTES(x) ((long long)(x) << 20)
214#define KILOBYTES(x) ((long long)(x) << 10)
215
216#define TO_EXABYTES(x) ((x) / EXABYTES(1))
217#define TO_PETABYTES(x) ((x) / PETABYTES(1))
218#define TO_TERABYTES(x) ((x) / TERABYTES(1))
219#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
220#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
221#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
222
223static void cvtstr(double value, char *str, size_t size)
224{
225 char *trim;
226 const char *suffix;
227
228 if (value >= EXABYTES(1)) {
229 suffix = " EiB";
230 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
231 } else if (value >= PETABYTES(1)) {
232 suffix = " PiB";
233 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
234 } else if (value >= TERABYTES(1)) {
235 suffix = " TiB";
236 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
237 } else if (value >= GIGABYTES(1)) {
238 suffix = " GiB";
239 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
240 } else if (value >= MEGABYTES(1)) {
241 suffix = " MiB";
242 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
243 } else if (value >= KILOBYTES(1)) {
244 suffix = " KiB";
245 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
246 } else {
247 suffix = " bytes";
248 snprintf(str, size - 6, "%f", value);
249 }
250
251 trim = strstr(str, ".000");
252 if (trim) {
253 strcpy(trim, suffix);
254 } else {
255 strcat(str, suffix);
256 }
257}
258
259
260
Alex Bennée50290c02019-05-29 17:16:32 +0100261static struct timespec tsub(struct timespec t1, struct timespec t2)
Kevin Wolf0b613882013-06-05 14:19:38 +0200262{
Alex Bennée50290c02019-05-29 17:16:32 +0100263 t1.tv_nsec -= t2.tv_nsec;
264 if (t1.tv_nsec < 0) {
265 t1.tv_nsec += NANOSECONDS_PER_SECOND;
Kevin Wolf0b613882013-06-05 14:19:38 +0200266 t1.tv_sec--;
267 }
268 t1.tv_sec -= t2.tv_sec;
269 return t1;
270}
271
Alex Bennée50290c02019-05-29 17:16:32 +0100272static double tdiv(double value, struct timespec tv)
Kevin Wolf0b613882013-06-05 14:19:38 +0200273{
Alex Bennée50290c02019-05-29 17:16:32 +0100274 double seconds = tv.tv_sec + (tv.tv_nsec / 1e9);
275 return value / seconds;
Kevin Wolf0b613882013-06-05 14:19:38 +0200276}
277
278#define HOURS(sec) ((sec) / (60 * 60))
279#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
280#define SECONDS(sec) ((sec) % 60)
281
282enum {
283 DEFAULT_TIME = 0x0,
284 TERSE_FIXED_TIME = 0x1,
285 VERBOSE_FIXED_TIME = 0x2,
286};
287
Alex Bennée50290c02019-05-29 17:16:32 +0100288static void timestr(struct timespec *tv, char *ts, size_t size, int format)
Kevin Wolf0b613882013-06-05 14:19:38 +0200289{
Alex Bennée50290c02019-05-29 17:16:32 +0100290 double frac_sec = tv->tv_nsec / 1e9;
Kevin Wolf0b613882013-06-05 14:19:38 +0200291
292 if (format & TERSE_FIXED_TIME) {
293 if (!HOURS(tv->tv_sec)) {
Alex Bennée50290c02019-05-29 17:16:32 +0100294 snprintf(ts, size, "%u:%05.2f",
295 (unsigned int) MINUTES(tv->tv_sec),
296 SECONDS(tv->tv_sec) + frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200297 return;
298 }
299 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
300 }
301
302 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
Alex Bennée50290c02019-05-29 17:16:32 +0100303 snprintf(ts, size, "%u:%02u:%05.2f",
Kevin Wolf0b613882013-06-05 14:19:38 +0200304 (unsigned int) HOURS(tv->tv_sec),
305 (unsigned int) MINUTES(tv->tv_sec),
Alex Bennée50290c02019-05-29 17:16:32 +0100306 SECONDS(tv->tv_sec) + frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200307 } else {
Alex Bennée50290c02019-05-29 17:16:32 +0100308 snprintf(ts, size, "%05.2f sec", frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200309 }
310}
311
Kevin Wolf797ac582013-06-05 14:19:31 +0200312/*
313 * Parse the pattern argument to various sub-commands.
314 *
315 * Because the pattern is used as an argument to memset it must evaluate
316 * to an unsigned integer that fits into a single byte.
317 */
318static int parse_pattern(const char *arg)
319{
320 char *endptr = NULL;
321 long pattern;
322
323 pattern = strtol(arg, &endptr, 0);
324 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
325 printf("%s is not a valid pattern byte\n", arg);
326 return -1;
327 }
328
329 return pattern;
330}
331
332/*
333 * Memory allocation helpers.
334 *
335 * Make sure memory is aligned by default, or purposefully misaligned if
336 * that is specified on the command line.
337 */
338
339#define MISALIGN_OFFSET 16
Max Reitz4c7b7e92015-02-05 13:58:22 -0500340static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
Kevin Wolf797ac582013-06-05 14:19:31 +0200341{
342 void *buf;
343
344 if (qemuio_misalign) {
345 len += MISALIGN_OFFSET;
346 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500347 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200348 memset(buf, pattern, len);
349 if (qemuio_misalign) {
350 buf += MISALIGN_OFFSET;
351 }
352 return buf;
353}
354
355static void qemu_io_free(void *p)
356{
357 if (qemuio_misalign) {
358 p -= MISALIGN_OFFSET;
359 }
360 qemu_vfree(p);
361}
362
Denis Plotnikov4d731512019-08-20 19:46:16 +0300363/*
364 * qemu_io_alloc_from_file()
365 *
366 * Allocates the buffer and populates it with the content of the given file
367 * up to @len bytes. If the file length is less than @len, then the buffer
368 * is populated with the file content cyclically.
369 *
370 * @blk - the block backend where the buffer content is going to be written to
371 * @len - the buffer length
372 * @file_name - the file to read the content from
373 *
374 * Returns: the buffer pointer on success
375 * NULL on error
376 */
377static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
378 const char *file_name)
379{
380 char *buf, *buf_origin;
381 FILE *f = fopen(file_name, "r");
382 int pattern_len;
383
384 if (!f) {
385 perror(file_name);
386 return NULL;
387 }
388
389 if (qemuio_misalign) {
390 len += MISALIGN_OFFSET;
391 }
392
393 buf_origin = buf = blk_blockalign(blk, len);
394
395 if (qemuio_misalign) {
396 buf_origin += MISALIGN_OFFSET;
397 buf += MISALIGN_OFFSET;
398 len -= MISALIGN_OFFSET;
399 }
400
401 pattern_len = fread(buf_origin, 1, len, f);
402
403 if (ferror(f)) {
404 perror(file_name);
405 goto error;
406 }
407
408 if (pattern_len == 0) {
409 fprintf(stderr, "%s: file is empty\n", file_name);
410 goto error;
411 }
412
413 fclose(f);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200414 f = NULL;
Denis Plotnikov4d731512019-08-20 19:46:16 +0300415
416 if (len > pattern_len) {
417 len -= pattern_len;
418 buf += pattern_len;
419
420 while (len > 0) {
421 size_t len_to_copy = MIN(pattern_len, len);
422
423 memcpy(buf, buf_origin, len_to_copy);
424
425 len -= len_to_copy;
426 buf += len_to_copy;
427 }
428 }
429
430 return buf_origin;
431
432error:
433 qemu_io_free(buf_origin);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200434 if (f) {
435 fclose(f);
436 }
Denis Plotnikov4d731512019-08-20 19:46:16 +0300437 return NULL;
438}
439
John Snow9b0beaf2015-11-05 18:53:02 -0500440static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200441{
John Snow9b0beaf2015-11-05 18:53:02 -0500442 uint64_t i;
443 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200444 const uint8_t *p;
445
446 for (i = 0, p = buffer; i < len; i += 16) {
447 const uint8_t *s = p;
448
449 printf("%08" PRIx64 ": ", offset + i);
450 for (j = 0; j < 16 && i + j < len; j++, p++) {
451 printf("%02x ", *p);
452 }
453 printf(" ");
454 for (j = 0; j < 16 && i + j < len; j++, s++) {
455 if (isalnum(*s)) {
456 printf("%c", *s);
457 } else {
458 printf(".");
459 }
460 }
461 printf("\n");
462 }
463}
464
Alex Bennée50290c02019-05-29 17:16:32 +0100465static void print_report(const char *op, struct timespec *t, int64_t offset,
Eric Blakedc388522016-05-07 21:16:42 -0600466 int64_t count, int64_t total, int cnt, bool Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200467{
468 char s1[64], s2[64], ts[64];
469
470 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
471 if (!Cflag) {
472 cvtstr((double)total, s1, sizeof(s1));
473 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500474 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200475 op, total, count, offset);
476 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
477 s1, cnt, ts, s2, tdiv((double)cnt, *t));
478 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500479 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200480 total, cnt, ts,
481 tdiv((double)total, *t),
482 tdiv((double)cnt, *t));
483 }
484}
485
486/*
487 * Parse multiple length statements for vectored I/O, and construct an I/O
488 * vector matching it.
489 */
490static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500491create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200492 int pattern)
493{
494 size_t *sizes = g_new0(size_t, nr_iov);
495 size_t count = 0;
496 void *buf = NULL;
497 void *p;
498 int i;
499
500 for (i = 0; i < nr_iov; i++) {
501 char *arg = argv[i];
502 int64_t len;
503
504 len = cvtnum(arg);
505 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500506 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200507 goto fail;
508 }
509
Alberto Garcia3026c462017-01-31 18:09:54 +0200510 if (len > BDRV_REQUEST_MAX_BYTES) {
511 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
512 (uint64_t)BDRV_REQUEST_MAX_BYTES);
513 goto fail;
514 }
515
516 if (count > BDRV_REQUEST_MAX_BYTES - len) {
517 printf("The total number of bytes exceed the maximum size %" PRIu64
518 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
Kevin Wolf797ac582013-06-05 14:19:31 +0200519 goto fail;
520 }
521
Kevin Wolf797ac582013-06-05 14:19:31 +0200522 sizes[i] = len;
523 count += len;
524 }
525
526 qemu_iovec_init(qiov, nr_iov);
527
Max Reitz4c7b7e92015-02-05 13:58:22 -0500528 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200529
530 for (i = 0; i < nr_iov; i++) {
531 qemu_iovec_add(qiov, p, sizes[i]);
532 p += sizes[i];
533 }
534
535fail:
536 g_free(sizes);
537 return buf;
538}
539
John Snow9b0beaf2015-11-05 18:53:02 -0500540static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300541 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200542{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300543 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500544 return -ERANGE;
545 }
546
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300547 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200548 if (*total < 0) {
549 return *total;
550 }
551 return 1;
552}
553
John Snow9b0beaf2015-11-05 18:53:02 -0500554static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300555 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200556{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300557 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500558 return -ERANGE;
559 }
560
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300561 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200562 if (*total < 0) {
563 return *total;
564 }
565 return 1;
566}
567
568typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500569 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200570 int64_t offset;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300571 int64_t bytes;
John Snow9b0beaf2015-11-05 18:53:02 -0500572 int64_t *total;
Eric Blake770e0e02016-05-07 21:16:44 -0600573 int flags;
Kevin Wolf797ac582013-06-05 14:19:31 +0200574 int ret;
575 bool done;
576} CoWriteZeroes;
577
Eric Blaked004bd52016-05-24 16:25:20 -0600578static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
Kevin Wolf797ac582013-06-05 14:19:31 +0200579{
580 CoWriteZeroes *data = opaque;
581
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300582 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
Eric Blaked004bd52016-05-24 16:25:20 -0600583 data->flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200584 data->done = true;
585 if (data->ret < 0) {
586 *data->total = data->ret;
587 return;
588 }
589
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300590 *data->total = data->bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200591}
592
Eric Blaked004bd52016-05-24 16:25:20 -0600593static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300594 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200595{
596 Coroutine *co;
597 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500598 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200599 .offset = offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300600 .bytes = bytes,
Kevin Wolf797ac582013-06-05 14:19:31 +0200601 .total = total,
Eric Blake770e0e02016-05-07 21:16:44 -0600602 .flags = flags,
Kevin Wolf797ac582013-06-05 14:19:31 +0200603 .done = false,
604 };
605
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200606 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
Fam Zheng324ec3e2017-04-10 20:16:18 +0800607 bdrv_coroutine_enter(blk_bs(blk), co);
Kevin Wolf797ac582013-06-05 14:19:31 +0200608 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500609 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200610 }
611 if (data.ret < 0) {
612 return data.ret;
613 } else {
614 return 1;
615 }
616}
617
Max Reitz4c7b7e92015-02-05 13:58:22 -0500618static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300619 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200620{
621 int ret;
622
Alberto Garcia41ae31e2019-05-14 16:57:35 +0300623 if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500624 return -ERANGE;
625 }
626
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300627 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200628 if (ret < 0) {
629 return ret;
630 }
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300631 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200632 return 1;
633}
634
Max Reitz4c7b7e92015-02-05 13:58:22 -0500635static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500636 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200637{
John Snow9b0beaf2015-11-05 18:53:02 -0500638 if (count > INT_MAX) {
639 return -ERANGE;
640 }
641
Max Reitz4c7b7e92015-02-05 13:58:22 -0500642 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200643 if (*total < 0) {
644 return *total;
645 }
646 return 1;
647}
648
Max Reitz4c7b7e92015-02-05 13:58:22 -0500649static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500650 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200651{
John Snow9b0beaf2015-11-05 18:53:02 -0500652 if (count > INT_MAX) {
653 return -ERANGE;
654 }
655
Max Reitz4c7b7e92015-02-05 13:58:22 -0500656 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200657 if (*total < 0) {
658 return *total;
659 }
660 return 1;
661}
662
663#define NOT_DONE 0x7fffffff
664static void aio_rw_done(void *opaque, int ret)
665{
666 *(int *)opaque = ret;
667}
668
Max Reitz4c7b7e92015-02-05 13:58:22 -0500669static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200670 int64_t offset, int *total)
671{
672 int async_ret = NOT_DONE;
673
Eric Blake7b3f9712016-05-06 10:26:44 -0600674 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200675 while (async_ret == NOT_DONE) {
676 main_loop_wait(false);
677 }
678
679 *total = qiov->size;
680 return async_ret < 0 ? async_ret : 1;
681}
682
Max Reitz4c7b7e92015-02-05 13:58:22 -0500683static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600684 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200685{
686 int async_ret = NOT_DONE;
687
Eric Blake770e0e02016-05-07 21:16:44 -0600688 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200689 while (async_ret == NOT_DONE) {
690 main_loop_wait(false);
691 }
692
693 *total = qiov->size;
694 return async_ret < 0 ? async_ret : 1;
695}
696
Kevin Wolf797ac582013-06-05 14:19:31 +0200697static void read_help(void)
698{
699 printf(
700"\n"
701" reads a range of bytes from the given offset\n"
702"\n"
703" Example:\n"
704" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
705"\n"
706" Reads a segment of the currently open file, optionally dumping it to the\n"
707" standard output stream (with -v option) for subsequent inspection.\n"
708" -b, -- read from the VM state rather than the virtual disk\n"
709" -C, -- report statistics in a machine parsable format\n"
710" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600711" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200712" -P, -- use a pattern to verify read data\n"
713" -q, -- quiet mode, do not show I/O statistics\n"
714" -s, -- start offset for pattern verification (only with -P)\n"
715" -v, -- dump buffer to standard output\n"
716"\n");
717}
718
Max Reitzb32d7a32018-05-09 21:42:59 +0200719static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200720
721static const cmdinfo_t read_cmd = {
722 .name = "read",
723 .altname = "r",
724 .cfunc = read_f,
725 .argmin = 2,
726 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600727 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200728 .oneline = "reads a number of bytes at a specified offset",
729 .help = read_help,
730};
731
Max Reitzb32d7a32018-05-09 21:42:59 +0200732static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200733{
Alex Bennée50290c02019-05-29 17:16:32 +0100734 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600735 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600736 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200737 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200738 char *buf;
739 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500740 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200741 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500742 int64_t total = 0;
743 int pattern = 0;
744 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200745
Eric Blakeb062ad82015-05-12 09:10:56 -0600746 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200747 switch (c) {
748 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600749 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200750 break;
751 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600752 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200753 break;
754 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600755 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200756 pattern_count = cvtnum(optarg);
757 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500758 print_cvtnum_err(pattern_count, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200759 return pattern_count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200760 }
761 break;
762 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600763 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200764 break;
765 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600766 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200767 pattern = parse_pattern(optarg);
768 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200769 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200770 }
771 break;
772 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600773 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200774 break;
775 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600776 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200777 pattern_offset = cvtnum(optarg);
778 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500779 print_cvtnum_err(pattern_offset, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200780 return pattern_offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200781 }
782 break;
783 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600784 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200785 break;
786 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200787 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200788 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200789 }
790 }
791
792 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200793 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200794 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200795 }
796
Kevin Wolf797ac582013-06-05 14:19:31 +0200797 offset = cvtnum(argv[optind]);
798 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500799 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200800 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200801 }
802
803 optind++;
804 count = cvtnum(argv[optind]);
805 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500806 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200807 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +0200808 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500809 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200810 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200811 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200812 }
813
814 if (!Pflag && (lflag || sflag)) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200815 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200816 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200817 }
818
819 if (!lflag) {
820 pattern_count = count - pattern_offset;
821 }
822
823 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
824 printf("pattern verification range exceeds end of read data\n");
Max Reitzb32d7a32018-05-09 21:42:59 +0200825 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200826 }
827
Eric Blake093ea232016-05-07 21:16:43 -0600828 if (bflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -0500829 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
830 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200831 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +0200832 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200833 }
Eric Blake1bce6b42017-04-29 14:14:11 -0500834 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
835 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200836 count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200837 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200838 }
839 }
840
Max Reitz4c7b7e92015-02-05 13:58:22 -0500841 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200842
Alex Bennée50290c02019-05-29 17:16:32 +0100843 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -0600844 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200845 ret = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200846 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +0200847 ret = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200848 }
Alex Bennée50290c02019-05-29 17:16:32 +0100849 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200850
Max Reitzb32d7a32018-05-09 21:42:59 +0200851 if (ret < 0) {
852 printf("read failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200853 goto out;
854 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200855 cnt = ret;
856
857 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200858
859 if (Pflag) {
860 void *cmp_buf = g_malloc(pattern_count);
861 memset(cmp_buf, pattern, pattern_count);
862 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
863 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500864 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200865 offset + pattern_offset, pattern_count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200866 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200867 }
868 g_free(cmp_buf);
869 }
870
871 if (qflag) {
872 goto out;
873 }
874
875 if (vflag) {
876 dump_buffer(buf, offset, count);
877 }
878
879 /* Finally, report back -- -C gives a parsable format */
880 t2 = tsub(t2, t1);
881 print_report("read", &t2, offset, count, total, cnt, Cflag);
882
883out:
884 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +0200885 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200886}
887
888static void readv_help(void)
889{
890 printf(
891"\n"
892" reads a range of bytes from the given offset into multiple buffers\n"
893"\n"
894" Example:\n"
895" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
896"\n"
897" Reads a segment of the currently open file, optionally dumping it to the\n"
898" standard output stream (with -v option) for subsequent inspection.\n"
899" Uses multiple iovec buffers if more than one byte range is specified.\n"
900" -C, -- report statistics in a machine parsable format\n"
901" -P, -- use a pattern to verify read data\n"
902" -v, -- dump buffer to standard output\n"
903" -q, -- quiet mode, do not show I/O statistics\n"
904"\n");
905}
906
Max Reitzb32d7a32018-05-09 21:42:59 +0200907static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200908
909static const cmdinfo_t readv_cmd = {
910 .name = "readv",
911 .cfunc = readv_f,
912 .argmin = 2,
913 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600914 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200915 .oneline = "reads a number of bytes at a specified offset",
916 .help = readv_help,
917};
918
Max Reitzb32d7a32018-05-09 21:42:59 +0200919static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200920{
Alex Bennée50290c02019-05-29 17:16:32 +0100921 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600922 bool Cflag = false, qflag = false, vflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200923 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200924 char *buf;
925 int64_t offset;
926 /* Some compilers get confused and warn if this is not initialized. */
927 int total = 0;
928 int nr_iov;
929 QEMUIOVector qiov;
930 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600931 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200932
Eric Blakeb062ad82015-05-12 09:10:56 -0600933 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200934 switch (c) {
935 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600936 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200937 break;
938 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600939 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200940 pattern = parse_pattern(optarg);
941 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200942 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200943 }
944 break;
945 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600946 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200947 break;
948 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600949 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200950 break;
951 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200952 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200953 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200954 }
955 }
956
957 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200958 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200959 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200960 }
961
962
963 offset = cvtnum(argv[optind]);
964 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500965 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200966 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200967 }
968 optind++;
969
Kevin Wolf797ac582013-06-05 14:19:31 +0200970 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500971 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200972 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200973 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200974 }
975
Alex Bennée50290c02019-05-29 17:16:32 +0100976 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +0200977 ret = do_aio_readv(blk, &qiov, offset, &total);
Alex Bennée50290c02019-05-29 17:16:32 +0100978 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200979
Max Reitzb32d7a32018-05-09 21:42:59 +0200980 if (ret < 0) {
981 printf("readv failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200982 goto out;
983 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200984 cnt = ret;
985
986 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200987
988 if (Pflag) {
989 void *cmp_buf = g_malloc(qiov.size);
990 memset(cmp_buf, pattern, qiov.size);
991 if (memcmp(buf, cmp_buf, qiov.size)) {
992 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +0200993 PRId64 ", %zu bytes\n", offset, qiov.size);
Max Reitzb32d7a32018-05-09 21:42:59 +0200994 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200995 }
996 g_free(cmp_buf);
997 }
998
999 if (qflag) {
1000 goto out;
1001 }
1002
1003 if (vflag) {
1004 dump_buffer(buf, offset, qiov.size);
1005 }
1006
1007 /* Finally, report back -- -C gives a parsable format */
1008 t2 = tsub(t2, t1);
1009 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
1010
1011out:
1012 qemu_iovec_destroy(&qiov);
1013 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001014 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001015}
1016
1017static void write_help(void)
1018{
1019 printf(
1020"\n"
1021" writes a range of bytes from the given offset\n"
1022"\n"
1023" Example:\n"
1024" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1025"\n"
1026" Writes into a segment of the currently open file, using a buffer\n"
1027" filled with a set pattern (0xcdcdcdcd).\n"
1028" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -05001029" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001030" -f, -- use Force Unit Access semantics\n"
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001031" -n, -- with -z, don't allow slow fallback\n"
Eric Blake093ea232016-05-07 21:16:43 -06001032" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001033" -P, -- use different pattern to fill file\n"
Denis Plotnikov4d731512019-08-20 19:46:16 +03001034" -s, -- use a pattern file to fill the write buffer\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001035" -C, -- report statistics in a machine parsable format\n"
1036" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001037" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001038" -z, -- write zeroes using blk_co_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001039"\n");
1040}
1041
Max Reitzb32d7a32018-05-09 21:42:59 +02001042static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001043
1044static const cmdinfo_t write_cmd = {
1045 .name = "write",
1046 .altname = "w",
1047 .cfunc = write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001048 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001049 .argmin = 2,
1050 .argmax = -1,
Denis Plotnikov4d731512019-08-20 19:46:16 +03001051 .args = "[-bcCfnquz] [-P pattern | -s source_file] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +02001052 .oneline = "writes a number of bytes at a specified offset",
1053 .help = write_help,
1054};
1055
Max Reitzb32d7a32018-05-09 21:42:59 +02001056static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001057{
Alex Bennée50290c02019-05-29 17:16:32 +01001058 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -06001059 bool Cflag = false, qflag = false, bflag = false;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001060 bool Pflag = false, zflag = false, cflag = false, sflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001061 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001062 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001063 char *buf = NULL;
1064 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -05001065 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001066 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -05001067 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001068 int pattern = 0xcd;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001069 const char *file_name = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001070
Denis Plotnikov4d731512019-08-20 19:46:16 +03001071 while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001072 switch (c) {
1073 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -06001074 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001075 break;
1076 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -06001077 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001078 break;
1079 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001080 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001081 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001082 case 'f':
1083 flags |= BDRV_REQ_FUA;
1084 break;
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001085 case 'n':
1086 flags |= BDRV_REQ_NO_FALLBACK;
1087 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001088 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -06001089 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +02001090 break;
1091 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001092 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001093 pattern = parse_pattern(optarg);
1094 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001095 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001096 }
1097 break;
1098 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001099 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001100 break;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001101 case 's':
1102 sflag = true;
1103 file_name = optarg;
1104 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001105 case 'u':
1106 flags |= BDRV_REQ_MAY_UNMAP;
1107 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001108 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001109 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001110 break;
1111 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001112 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001113 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001114 }
1115 }
1116
1117 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001118 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001119 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001120 }
1121
Eric Blake093ea232016-05-07 21:16:43 -06001122 if (bflag && zflag) {
1123 printf("-b and -z cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001124 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001125 }
1126
Eric Blake770e0e02016-05-07 21:16:44 -06001127 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1128 printf("-f and -b or -c cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001129 return -EINVAL;
Eric Blake770e0e02016-05-07 21:16:44 -06001130 }
1131
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001132 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1133 printf("-n requires -z to be specified\n");
1134 return -EINVAL;
1135 }
1136
Eric Blakec2e001c2016-05-07 21:16:45 -06001137 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1138 printf("-u requires -z to be specified\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001139 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001140 }
1141
Denis Plotnikov4d731512019-08-20 19:46:16 +03001142 if (zflag + Pflag + sflag > 1) {
1143 printf("Only one of -z, -P, and -s "
1144 "can be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001145 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001146 }
1147
1148 offset = cvtnum(argv[optind]);
1149 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001150 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001151 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001152 }
1153
1154 optind++;
1155 count = cvtnum(argv[optind]);
1156 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001157 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001158 return count;
Eric Blake395aecd2021-12-03 17:15:28 -06001159 } else if (count > BDRV_REQUEST_MAX_BYTES &&
1160 !(flags & BDRV_REQ_NO_FALLBACK)) {
1161 printf("length cannot exceed %" PRIu64 " without -n, given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001162 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001163 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001164 }
1165
Eric Blake093ea232016-05-07 21:16:43 -06001166 if (bflag || cflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -05001167 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1168 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001169 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +02001170 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001171 }
1172
Eric Blake1bce6b42017-04-29 14:14:11 -05001173 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1174 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001175 count);
Max Reitzb32d7a32018-05-09 21:42:59 +02001176 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001177 }
1178 }
1179
1180 if (!zflag) {
Denis Plotnikov4d731512019-08-20 19:46:16 +03001181 if (sflag) {
1182 buf = qemu_io_alloc_from_file(blk, count, file_name);
1183 if (!buf) {
1184 return -EINVAL;
1185 }
1186 } else {
1187 buf = qemu_io_alloc(blk, count, pattern);
1188 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001189 }
1190
Alex Bennée50290c02019-05-29 17:16:32 +01001191 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -06001192 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001193 ret = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001194 } else if (zflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001195 ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001196 } else if (cflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001197 ret = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001198 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +02001199 ret = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001200 }
Alex Bennée50290c02019-05-29 17:16:32 +01001201 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001202
Max Reitzb32d7a32018-05-09 21:42:59 +02001203 if (ret < 0) {
1204 printf("write failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001205 goto out;
1206 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001207 cnt = ret;
1208
1209 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001210
1211 if (qflag) {
1212 goto out;
1213 }
1214
1215 /* Finally, report back -- -C gives a parsable format */
1216 t2 = tsub(t2, t1);
1217 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1218
1219out:
1220 if (!zflag) {
1221 qemu_io_free(buf);
1222 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001223 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001224}
1225
1226static void
1227writev_help(void)
1228{
1229 printf(
1230"\n"
1231" writes a range of bytes from the given offset source from multiple buffers\n"
1232"\n"
1233" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001234" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001235"\n"
1236" Writes into a segment of the currently open file, using a buffer\n"
1237" filled with a set pattern (0xcdcdcdcd).\n"
1238" -P, -- use different pattern to fill file\n"
1239" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001240" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001241" -q, -- quiet mode, do not show I/O statistics\n"
1242"\n");
1243}
1244
Max Reitzb32d7a32018-05-09 21:42:59 +02001245static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001246
1247static const cmdinfo_t writev_cmd = {
1248 .name = "writev",
1249 .cfunc = writev_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001250 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001251 .argmin = 2,
1252 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001253 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001254 .oneline = "writes a number of bytes at a specified offset",
1255 .help = writev_help,
1256};
1257
Max Reitzb32d7a32018-05-09 21:42:59 +02001258static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001259{
Alex Bennée50290c02019-05-29 17:16:32 +01001260 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001261 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001262 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001263 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001264 char *buf;
1265 int64_t offset;
1266 /* Some compilers get confused and warn if this is not initialized. */
1267 int total = 0;
1268 int nr_iov;
1269 int pattern = 0xcd;
1270 QEMUIOVector qiov;
1271
Eric Blake4ca1d342016-05-16 10:43:01 -06001272 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001273 switch (c) {
1274 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001275 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001276 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001277 case 'f':
1278 flags |= BDRV_REQ_FUA;
1279 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001280 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001281 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001282 break;
1283 case 'P':
1284 pattern = parse_pattern(optarg);
1285 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001286 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001287 }
1288 break;
1289 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001290 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001291 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001292 }
1293 }
1294
1295 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001296 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001297 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001298 }
1299
1300 offset = cvtnum(argv[optind]);
1301 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001302 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001303 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001304 }
1305 optind++;
1306
Kevin Wolf797ac582013-06-05 14:19:31 +02001307 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001308 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001309 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001310 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001311 }
1312
Alex Bennée50290c02019-05-29 17:16:32 +01001313 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001314 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
Alex Bennée50290c02019-05-29 17:16:32 +01001315 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001316
Max Reitzb32d7a32018-05-09 21:42:59 +02001317 if (ret < 0) {
1318 printf("writev failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001319 goto out;
1320 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001321 cnt = ret;
1322
1323 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001324
1325 if (qflag) {
1326 goto out;
1327 }
1328
1329 /* Finally, report back -- -C gives a parsable format */
1330 t2 = tsub(t2, t1);
1331 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1332out:
1333 qemu_iovec_destroy(&qiov);
1334 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001335 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001336}
1337
Kevin Wolf797ac582013-06-05 14:19:31 +02001338struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001339 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001340 QEMUIOVector qiov;
1341 int64_t offset;
1342 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001343 bool qflag;
1344 bool vflag;
1345 bool Cflag;
1346 bool Pflag;
1347 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001348 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001349 int pattern;
Alex Bennée50290c02019-05-29 17:16:32 +01001350 struct timespec t1;
Kevin Wolf797ac582013-06-05 14:19:31 +02001351};
1352
1353static void aio_write_done(void *opaque, int ret)
1354{
1355 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001356 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001357
Alex Bennée50290c02019-05-29 17:16:32 +01001358 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001359
1360
1361 if (ret < 0) {
1362 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001363 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001364 goto out;
1365 }
1366
Max Reitz4c7b7e92015-02-05 13:58:22 -05001367 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001368
Kevin Wolf797ac582013-06-05 14:19:31 +02001369 if (ctx->qflag) {
1370 goto out;
1371 }
1372
1373 /* Finally, report back -- -C gives a parsable format */
1374 t2 = tsub(t2, ctx->t1);
1375 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1376 ctx->qiov.size, 1, ctx->Cflag);
1377out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001378 if (!ctx->zflag) {
1379 qemu_io_free(ctx->buf);
1380 qemu_iovec_destroy(&ctx->qiov);
1381 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001382 g_free(ctx);
1383}
1384
1385static void aio_read_done(void *opaque, int ret)
1386{
1387 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001388 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001389
Alex Bennée50290c02019-05-29 17:16:32 +01001390 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001391
1392 if (ret < 0) {
1393 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001394 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001395 goto out;
1396 }
1397
1398 if (ctx->Pflag) {
1399 void *cmp_buf = g_malloc(ctx->qiov.size);
1400
1401 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1402 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1403 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +02001404 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
Kevin Wolf797ac582013-06-05 14:19:31 +02001405 }
1406 g_free(cmp_buf);
1407 }
1408
Max Reitz4c7b7e92015-02-05 13:58:22 -05001409 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001410
Kevin Wolf797ac582013-06-05 14:19:31 +02001411 if (ctx->qflag) {
1412 goto out;
1413 }
1414
1415 if (ctx->vflag) {
1416 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1417 }
1418
1419 /* Finally, report back -- -C gives a parsable format */
1420 t2 = tsub(t2, ctx->t1);
1421 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1422 ctx->qiov.size, 1, ctx->Cflag);
1423out:
1424 qemu_io_free(ctx->buf);
1425 qemu_iovec_destroy(&ctx->qiov);
1426 g_free(ctx);
1427}
1428
1429static void aio_read_help(void)
1430{
1431 printf(
1432"\n"
1433" asynchronously reads a range of bytes from the given offset\n"
1434"\n"
1435" Example:\n"
1436" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1437"\n"
1438" Reads a segment of the currently open file, optionally dumping it to the\n"
1439" standard output stream (with -v option) for subsequent inspection.\n"
1440" The read is performed asynchronously and the aio_flush command must be\n"
1441" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001442" Note that due to its asynchronous nature, this command will be\n"
1443" considered successful once the request is submitted, independently\n"
1444" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001445" -C, -- report statistics in a machine parsable format\n"
1446" -P, -- use a pattern to verify read data\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001447" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001448" -v, -- dump buffer to standard output\n"
1449" -q, -- quiet mode, do not show I/O statistics\n"
1450"\n");
1451}
1452
Max Reitzb32d7a32018-05-09 21:42:59 +02001453static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001454
1455static const cmdinfo_t aio_read_cmd = {
1456 .name = "aio_read",
1457 .cfunc = aio_read_f,
1458 .argmin = 2,
1459 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001460 .args = "[-Ciqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001461 .oneline = "asynchronously reads a number of bytes",
1462 .help = aio_read_help,
1463};
1464
Max Reitzb32d7a32018-05-09 21:42:59 +02001465static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001466{
1467 int nr_iov, c;
1468 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1469
Max Reitz4c7b7e92015-02-05 13:58:22 -05001470 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001471 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001472 switch (c) {
1473 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001474 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001475 break;
1476 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001477 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001478 ctx->pattern = parse_pattern(optarg);
1479 if (ctx->pattern < 0) {
1480 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001481 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001482 }
1483 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001484 case 'i':
1485 printf("injecting invalid read request\n");
1486 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1487 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001488 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001489 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001490 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001491 break;
1492 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001493 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001494 break;
1495 default:
1496 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001497 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001498 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001499 }
1500 }
1501
1502 if (optind > argc - 2) {
1503 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001504 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001505 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001506 }
1507
1508 ctx->offset = cvtnum(argv[optind]);
1509 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001510 int ret = ctx->offset;
1511 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001512 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001513 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001514 }
1515 optind++;
1516
Kevin Wolf797ac582013-06-05 14:19:31 +02001517 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001518 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001519 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001520 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001521 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001522 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001523 }
1524
Alex Bennée50290c02019-05-29 17:16:32 +01001525 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001526 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1527 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001528 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001529 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001530}
1531
1532static void aio_write_help(void)
1533{
1534 printf(
1535"\n"
1536" asynchronously writes a range of bytes from the given offset source\n"
1537" from multiple buffers\n"
1538"\n"
1539" Example:\n"
1540" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1541"\n"
1542" Writes into a segment of the currently open file, using a buffer\n"
1543" filled with a set pattern (0xcdcdcdcd).\n"
1544" The write is performed asynchronously and the aio_flush command must be\n"
1545" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001546" Note that due to its asynchronous nature, this command will be\n"
1547" considered successful once the request is submitted, independently\n"
1548" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001549" -P, -- use different pattern to fill file\n"
1550" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001551" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001552" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001553" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001554" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001555" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001556"\n");
1557}
1558
Max Reitzb32d7a32018-05-09 21:42:59 +02001559static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001560
1561static const cmdinfo_t aio_write_cmd = {
1562 .name = "aio_write",
1563 .cfunc = aio_write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001564 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001565 .argmin = 2,
1566 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001567 .args = "[-Cfiquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001568 .oneline = "asynchronously writes a number of bytes",
1569 .help = aio_write_help,
1570};
1571
Max Reitzb32d7a32018-05-09 21:42:59 +02001572static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001573{
1574 int nr_iov, c;
1575 int pattern = 0xcd;
1576 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001577 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001578
Max Reitz4c7b7e92015-02-05 13:58:22 -05001579 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001580 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001581 switch (c) {
1582 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001583 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001584 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001585 case 'f':
1586 flags |= BDRV_REQ_FUA;
1587 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001588 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001589 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001590 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001591 case 'u':
1592 flags |= BDRV_REQ_MAY_UNMAP;
1593 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001594 case 'P':
1595 pattern = parse_pattern(optarg);
1596 if (pattern < 0) {
1597 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001598 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001599 }
1600 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001601 case 'i':
1602 printf("injecting invalid write request\n");
1603 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1604 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001605 return 0;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001606 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001607 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001608 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001609 default:
1610 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001611 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001612 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001613 }
1614 }
1615
1616 if (optind > argc - 2) {
1617 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001618 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001619 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001620 }
1621
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001622 if (ctx->zflag && optind != argc - 2) {
1623 printf("-z supports only a single length parameter\n");
1624 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001625 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001626 }
1627
Eric Blakec2e001c2016-05-07 21:16:45 -06001628 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1629 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001630 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001631 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001632 }
1633
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001634 if (ctx->zflag && ctx->Pflag) {
1635 printf("-z and -P cannot be specified at the same time\n");
1636 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001637 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001638 }
1639
Kevin Wolf797ac582013-06-05 14:19:31 +02001640 ctx->offset = cvtnum(argv[optind]);
1641 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001642 int ret = ctx->offset;
1643 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001644 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001645 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001646 }
1647 optind++;
1648
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001649 if (ctx->zflag) {
1650 int64_t count = cvtnum(argv[optind]);
1651 if (count < 0) {
1652 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001653 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001654 return count;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001655 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001656
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001657 ctx->qiov.size = count;
Eric Blaked004bd52016-05-24 16:25:20 -06001658 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1659 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001660 } else {
1661 nr_iov = argc - optind;
1662 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1663 pattern);
1664 if (ctx->buf == NULL) {
1665 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1666 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001667 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001668 }
1669
Alex Bennée50290c02019-05-29 17:16:32 +01001670 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001671 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1672 BLOCK_ACCT_WRITE);
1673
Eric Blake770e0e02016-05-07 21:16:44 -06001674 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1675 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001676 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001677
1678 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001679}
1680
Max Reitzb32d7a32018-05-09 21:42:59 +02001681static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001682{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001683 BlockAcctCookie cookie;
1684 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001685 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001686 block_acct_done(blk_get_stats(blk), &cookie);
Max Reitzb32d7a32018-05-09 21:42:59 +02001687 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001688}
1689
1690static const cmdinfo_t aio_flush_cmd = {
1691 .name = "aio_flush",
1692 .cfunc = aio_flush_f,
1693 .oneline = "completes all outstanding aio requests"
1694};
1695
Max Reitzb32d7a32018-05-09 21:42:59 +02001696static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001697{
Max Reitzb32d7a32018-05-09 21:42:59 +02001698 return blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001699}
1700
1701static const cmdinfo_t flush_cmd = {
1702 .name = "flush",
1703 .altname = "f",
1704 .cfunc = flush_f,
1705 .oneline = "flush all in-core file state to disk",
1706};
1707
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001708static int truncate_f(BlockBackend *blk, int argc, char **argv);
1709static const cmdinfo_t truncate_cmd = {
1710 .name = "truncate",
1711 .altname = "t",
1712 .cfunc = truncate_f,
1713 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1714 .argmin = 1,
1715 .argmax = 3,
1716 .args = "[-m prealloc_mode] off",
1717 .oneline = "truncates the current file at the given offset",
1718};
1719
Max Reitzb32d7a32018-05-09 21:42:59 +02001720static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001721{
Max Reitzed3d2ec2017-03-28 22:51:27 +02001722 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001723 int64_t offset;
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001724 int c, ret;
1725 PreallocMode prealloc = PREALLOC_MODE_OFF;
Kevin Wolf797ac582013-06-05 14:19:31 +02001726
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001727 while ((c = getopt(argc, argv, "m:")) != -1) {
1728 switch (c) {
1729 case 'm':
1730 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
1731 PREALLOC_MODE__MAX, NULL);
1732 if (prealloc == PREALLOC_MODE__MAX) {
1733 error_report("Invalid preallocation mode '%s'", optarg);
1734 return -EINVAL;
1735 }
1736 break;
1737 default:
1738 qemuio_command_usage(&truncate_cmd);
1739 return -EINVAL;
1740 }
1741 }
1742
1743 offset = cvtnum(argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001744 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001745 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001746 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001747 }
1748
Max Reitze8d04f92019-09-18 11:51:43 +02001749 /*
1750 * qemu-io is a debugging tool, so let us be strict here and pass
1751 * exact=true. It is better to err on the "emit more errors" side
1752 * than to be overly permissive.
1753 */
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001754 ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err);
Kevin Wolf797ac582013-06-05 14:19:31 +02001755 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001756 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02001757 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001758 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001759
1760 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001761}
1762
Max Reitzb32d7a32018-05-09 21:42:59 +02001763static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001764{
1765 int64_t size;
1766 char s1[64];
1767
Max Reitz4c7b7e92015-02-05 13:58:22 -05001768 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001769 if (size < 0) {
1770 printf("getlength: %s\n", strerror(-size));
Max Reitzb32d7a32018-05-09 21:42:59 +02001771 return size;
Kevin Wolf797ac582013-06-05 14:19:31 +02001772 }
1773
1774 cvtstr(size, s1, sizeof(s1));
1775 printf("%s\n", s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001776 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001777}
1778
1779
1780static const cmdinfo_t length_cmd = {
1781 .name = "length",
1782 .altname = "l",
1783 .cfunc = length_f,
1784 .oneline = "gets the length of the current file",
1785};
1786
1787
Max Reitzb32d7a32018-05-09 21:42:59 +02001788static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001789{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001790 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001791 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001792 ImageInfoSpecific *spec_info;
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001793 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001794 char s1[64], s2[64];
1795 int ret;
1796
1797 if (bs->drv && bs->drv->format_name) {
1798 printf("format name: %s\n", bs->drv->format_name);
1799 }
1800 if (bs->drv && bs->drv->protocol_name) {
1801 printf("format name: %s\n", bs->drv->protocol_name);
1802 }
1803
1804 ret = bdrv_get_info(bs, &bdi);
1805 if (ret) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001806 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001807 }
1808
1809 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1810 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1811
1812 printf("cluster size: %s\n", s1);
1813 printf("vm state offset: %s\n", s2);
1814
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001815 spec_info = bdrv_get_specific_info(bs, &local_err);
1816 if (local_err) {
1817 error_report_err(local_err);
1818 return -EIO;
1819 }
Max Reitza8d8ecb2013-10-09 10:46:17 +02001820 if (spec_info) {
1821 printf("Format specific information:\n");
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02001822 bdrv_image_info_specific_dump(spec_info);
Max Reitza8d8ecb2013-10-09 10:46:17 +02001823 qapi_free_ImageInfoSpecific(spec_info);
1824 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001825
1826 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001827}
1828
1829
1830
1831static const cmdinfo_t info_cmd = {
1832 .name = "info",
1833 .altname = "i",
1834 .cfunc = info_f,
1835 .oneline = "prints information about the current file",
1836};
1837
1838static void discard_help(void)
1839{
1840 printf(
1841"\n"
1842" discards a range of bytes from the given offset\n"
1843"\n"
1844" Example:\n"
1845" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1846"\n"
1847" Discards a segment of the currently open file.\n"
1848" -C, -- report statistics in a machine parsable format\n"
1849" -q, -- quiet mode, do not show I/O statistics\n"
1850"\n");
1851}
1852
Max Reitzb32d7a32018-05-09 21:42:59 +02001853static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001854
1855static const cmdinfo_t discard_cmd = {
1856 .name = "discard",
1857 .altname = "d",
1858 .cfunc = discard_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001859 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001860 .argmin = 2,
1861 .argmax = -1,
1862 .args = "[-Cq] off len",
1863 .oneline = "discards a number of bytes at a specified offset",
1864 .help = discard_help,
1865};
1866
Max Reitzb32d7a32018-05-09 21:42:59 +02001867static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001868{
Alex Bennée50290c02019-05-29 17:16:32 +01001869 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001870 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001871 int c, ret;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001872 int64_t offset, bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +02001873
Eric Blakeb062ad82015-05-12 09:10:56 -06001874 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001875 switch (c) {
1876 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001877 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001878 break;
1879 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001880 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001881 break;
1882 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001883 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001884 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001885 }
1886 }
1887
1888 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001889 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001890 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001891 }
1892
1893 offset = cvtnum(argv[optind]);
1894 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001895 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001896 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001897 }
1898
1899 optind++;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001900 bytes = cvtnum(argv[optind]);
1901 if (bytes < 0) {
1902 print_cvtnum_err(bytes, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001903 return bytes;
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001904 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001905 printf("length cannot exceed %"PRIu64", given %s\n",
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001906 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001907 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001908 }
1909
Alex Bennée50290c02019-05-29 17:16:32 +01001910 clock_gettime(CLOCK_MONOTONIC, &t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001911 ret = blk_pdiscard(blk, offset, bytes);
Alex Bennée50290c02019-05-29 17:16:32 +01001912 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001913
1914 if (ret < 0) {
1915 printf("discard failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001916 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001917 }
1918
1919 /* Finally, report back -- -C gives a parsable format */
1920 if (!qflag) {
1921 t2 = tsub(t2, t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001922 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
Kevin Wolf797ac582013-06-05 14:19:31 +02001923 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001924
1925 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001926}
1927
Max Reitzb32d7a32018-05-09 21:42:59 +02001928static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001929{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001930 BlockDriverState *bs = blk_bs(blk);
Eric Blaked6a644b2017-07-07 07:44:57 -05001931 int64_t offset, start, remaining, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001932 char s1[64];
Eric Blaked6a644b2017-07-07 07:44:57 -05001933 int ret;
1934 int64_t num, sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001935
Eric Blaked6a644b2017-07-07 07:44:57 -05001936 start = offset = cvtnum(argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001937 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001938 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001939 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001940 }
1941
1942 if (argc == 3) {
Eric Blake4401fdc2017-04-29 14:14:12 -05001943 count = cvtnum(argv[2]);
1944 if (count < 0) {
1945 print_cvtnum_err(count, argv[2]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001946 return count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001947 }
1948 } else {
Eric Blake4401fdc2017-04-29 14:14:12 -05001949 count = BDRV_SECTOR_SIZE;
Kevin Wolf797ac582013-06-05 14:19:31 +02001950 }
1951
Eric Blaked6a644b2017-07-07 07:44:57 -05001952 remaining = count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001953 sum_alloc = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001954 while (remaining) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001955 ret = bdrv_is_allocated(bs, offset, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001956 if (ret < 0) {
1957 printf("is_allocated failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001958 return ret;
Paolo Bonzinid6636402013-09-04 19:00:25 +02001959 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001960 offset += num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001961 remaining -= num;
1962 if (ret) {
1963 sum_alloc += num;
1964 }
1965 if (num == 0) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001966 count -= remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001967 remaining = 0;
1968 }
1969 }
1970
Eric Blaked6a644b2017-07-07 07:44:57 -05001971 cvtstr(start, s1, sizeof(s1));
Kevin Wolf797ac582013-06-05 14:19:31 +02001972
Eric Blake4401fdc2017-04-29 14:14:12 -05001973 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001974 sum_alloc, count, s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001975 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001976}
1977
1978static const cmdinfo_t alloc_cmd = {
1979 .name = "alloc",
1980 .altname = "a",
1981 .argmin = 1,
1982 .argmax = 2,
1983 .cfunc = alloc_f,
Eric Blake4401fdc2017-04-29 14:14:12 -05001984 .args = "offset [count]",
1985 .oneline = "checks if offset is allocated in the file",
Kevin Wolf797ac582013-06-05 14:19:31 +02001986};
1987
1988
Eric Blaked6a644b2017-07-07 07:44:57 -05001989static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1990 int64_t bytes, int64_t *pnum)
Kevin Wolf797ac582013-06-05 14:19:31 +02001991{
Eric Blaked6a644b2017-07-07 07:44:57 -05001992 int64_t num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001993 int ret, firstret;
1994
Eric Blake087f2fb2021-12-03 17:15:27 -06001995 ret = bdrv_is_allocated(bs, offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001996 if (ret < 0) {
1997 return ret;
1998 }
1999
2000 firstret = ret;
2001 *pnum = num;
2002
Eric Blaked6a644b2017-07-07 07:44:57 -05002003 while (bytes > 0 && ret == firstret) {
2004 offset += num;
2005 bytes -= num;
Kevin Wolf797ac582013-06-05 14:19:31 +02002006
Eric Blake087f2fb2021-12-03 17:15:27 -06002007 ret = bdrv_is_allocated(bs, offset, bytes, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02002008 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02002009 *pnum += num;
2010 } else {
2011 break;
2012 }
2013 }
2014
2015 return firstret;
2016}
2017
Max Reitzb32d7a32018-05-09 21:42:59 +02002018static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002019{
Eric Blaked6a644b2017-07-07 07:44:57 -05002020 int64_t offset, bytes;
Eric Blake6f3c90a2017-04-29 14:14:13 -05002021 char s1[64], s2[64];
Kevin Wolf797ac582013-06-05 14:19:31 +02002022 int64_t num;
2023 int ret;
2024 const char *retstr;
2025
2026 offset = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05002027 bytes = blk_getlength(blk);
2028 if (bytes < 0) {
2029 error_report("Failed to query image length: %s", strerror(-bytes));
Max Reitzb32d7a32018-05-09 21:42:59 +02002030 return bytes;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002031 }
2032
Eric Blaked6a644b2017-07-07 07:44:57 -05002033 while (bytes) {
2034 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002035 if (ret < 0) {
2036 error_report("Failed to get allocation status: %s", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002037 return ret;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002038 } else if (!num) {
2039 error_report("Unexpected end of image");
Max Reitzb32d7a32018-05-09 21:42:59 +02002040 return -EIO;
Kevin Wolf797ac582013-06-05 14:19:31 +02002041 }
2042
2043 retstr = ret ? " allocated" : "not allocated";
Eric Blaked6a644b2017-07-07 07:44:57 -05002044 cvtstr(num, s1, sizeof(s1));
2045 cvtstr(offset, s2, sizeof(s2));
Eric Blake6f3c90a2017-04-29 14:14:13 -05002046 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05002047 s1, num, retstr, s2, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02002048
2049 offset += num;
Eric Blaked6a644b2017-07-07 07:44:57 -05002050 bytes -= num;
2051 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002052
2053 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002054}
2055
2056static const cmdinfo_t map_cmd = {
2057 .name = "map",
2058 .argmin = 0,
2059 .argmax = 0,
2060 .cfunc = map_f,
2061 .args = "",
2062 .oneline = "prints the allocated areas of a file",
2063};
2064
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002065static void reopen_help(void)
2066{
2067 printf(
2068"\n"
2069" Changes the open options of an already opened image\n"
2070"\n"
2071" Example:\n"
2072" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2073"\n"
2074" -r, -- Reopen the image read-only\n"
Kevin Wolfea922032017-08-03 17:03:00 +02002075" -w, -- Reopen the image read-write\n"
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002076" -c, -- Change the cache mode to the given value\n"
2077" -o, -- Changes block driver options (cf. 'open' command)\n"
2078"\n");
2079}
2080
Max Reitzb32d7a32018-05-09 21:42:59 +02002081static int reopen_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002082
2083static QemuOptsList reopen_opts = {
2084 .name = "reopen",
2085 .merge_lists = true,
2086 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2087 .desc = {
2088 /* no elements => accept any params */
2089 { /* end of list */ }
2090 },
2091};
2092
2093static const cmdinfo_t reopen_cmd = {
2094 .name = "reopen",
2095 .argmin = 0,
2096 .argmax = -1,
2097 .cfunc = reopen_f,
Kevin Wolfea922032017-08-03 17:03:00 +02002098 .args = "[(-r|-w)] [-c cache] [-o options]",
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002099 .oneline = "reopens an image with new options",
2100 .help = reopen_help,
2101};
2102
Max Reitzb32d7a32018-05-09 21:42:59 +02002103static int reopen_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002104{
2105 BlockDriverState *bs = blk_bs(blk);
2106 QemuOpts *qopts;
2107 QDict *opts;
2108 int c;
2109 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002110 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolfea922032017-08-03 17:03:00 +02002111 bool has_rw_option = false;
Alberto Garciadc900c32018-11-12 16:00:42 +02002112 bool has_cache_option = false;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002113 Error *local_err = NULL;
2114
Kevin Wolfea922032017-08-03 17:03:00 +02002115 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002116 switch (c) {
2117 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002118 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002119 error_report("Invalid cache option: %s", optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +02002120 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002121 }
Alberto Garciadc900c32018-11-12 16:00:42 +02002122 has_cache_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002123 break;
2124 case 'o':
2125 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2126 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002127 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002128 }
2129 break;
2130 case 'r':
Kevin Wolfea922032017-08-03 17:03:00 +02002131 if (has_rw_option) {
2132 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002133 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002134 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002135 flags &= ~BDRV_O_RDWR;
Kevin Wolfea922032017-08-03 17:03:00 +02002136 has_rw_option = true;
2137 break;
2138 case 'w':
2139 if (has_rw_option) {
2140 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002141 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002142 }
2143 flags |= BDRV_O_RDWR;
2144 has_rw_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002145 break;
2146 default:
2147 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002148 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002149 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002150 }
2151 }
2152
2153 if (optind != argc) {
2154 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002155 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002156 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002157 }
2158
Alberto Garciaa8003ec2018-09-06 12:37:01 +03002159 if (!writethrough != blk_enable_write_cache(blk) &&
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002160 blk_get_attached_dev(blk))
2161 {
2162 error_report("Cannot change cache.writeback: Device attached");
2163 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002164 return -EBUSY;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002165 }
2166
Kevin Wolff3adefb2017-09-22 14:50:12 +02002167 if (!(flags & BDRV_O_RDWR)) {
2168 uint64_t orig_perm, orig_shared_perm;
2169
2170 bdrv_drain(bs);
2171
2172 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2173 blk_set_perm(blk,
2174 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2175 orig_shared_perm,
2176 &error_abort);
2177 }
2178
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002179 qopts = qemu_opts_find(&reopen_opts, NULL);
Alberto Garciadc900c32018-11-12 16:00:42 +02002180 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002181 qemu_opts_reset(&reopen_opts);
2182
Alberto Garciadc900c32018-11-12 16:00:42 +02002183 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2184 if (has_rw_option) {
2185 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2186 qobject_unref(opts);
2187 return -EINVAL;
2188 }
2189 } else {
2190 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2191 }
2192
2193 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2194 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2195 if (has_cache_option) {
2196 error_report("Cannot set both -c and the cache options");
2197 qobject_unref(opts);
2198 return -EINVAL;
2199 }
2200 } else {
2201 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2202 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2203 }
2204
Kevin Wolf6cf42ca2021-07-08 13:47:06 +02002205 bdrv_reopen(bs, opts, true, &local_err);
Kevin Wolf1a63a902017-12-06 20:24:44 +01002206
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002207 if (local_err) {
2208 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02002209 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002210 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002211
2212 blk_set_enable_write_cache(blk, !writethrough);
2213 return 0;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002214}
2215
Max Reitzb32d7a32018-05-09 21:42:59 +02002216static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002217{
2218 int ret;
2219
Max Reitz4c7b7e92015-02-05 13:58:22 -05002220 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002221 if (ret < 0) {
2222 printf("Could not set breakpoint: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002223 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002224 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002225
2226 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002227}
2228
Max Reitzb32d7a32018-05-09 21:42:59 +02002229static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002230{
2231 int ret;
2232
Max Reitz4c7b7e92015-02-05 13:58:22 -05002233 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002234 if (ret < 0) {
2235 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002236 return ret;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002237 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002238
2239 return 0;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002240}
2241
Kevin Wolf797ac582013-06-05 14:19:31 +02002242static const cmdinfo_t break_cmd = {
2243 .name = "break",
2244 .argmin = 2,
2245 .argmax = 2,
2246 .cfunc = break_f,
2247 .args = "event tag",
2248 .oneline = "sets a breakpoint on event and tags the stopped "
2249 "request as tag",
2250};
2251
Fam Zheng4cc70e92013-11-20 10:01:54 +08002252static const cmdinfo_t remove_break_cmd = {
2253 .name = "remove_break",
2254 .argmin = 1,
2255 .argmax = 1,
2256 .cfunc = remove_break_f,
2257 .args = "tag",
2258 .oneline = "remove a breakpoint by tag",
2259};
2260
Max Reitzb32d7a32018-05-09 21:42:59 +02002261static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002262{
2263 int ret;
2264
Max Reitz4c7b7e92015-02-05 13:58:22 -05002265 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002266 if (ret < 0) {
2267 printf("Could not resume request: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002268 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002269 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002270
2271 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002272}
2273
2274static const cmdinfo_t resume_cmd = {
2275 .name = "resume",
2276 .argmin = 1,
2277 .argmax = 1,
2278 .cfunc = resume_f,
2279 .args = "tag",
2280 .oneline = "resumes the request tagged as tag",
2281};
2282
Max Reitzb32d7a32018-05-09 21:42:59 +02002283static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002284{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002285 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2286 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002287 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002288 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002289}
2290
2291static const cmdinfo_t wait_break_cmd = {
2292 .name = "wait_break",
2293 .argmin = 1,
2294 .argmax = 1,
2295 .cfunc = wait_break_f,
2296 .args = "tag",
2297 .oneline = "waits for the suspension of a request",
2298};
2299
Max Reitzb32d7a32018-05-09 21:42:59 +02002300static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002301{
2302 abort();
2303}
2304
2305static const cmdinfo_t abort_cmd = {
2306 .name = "abort",
2307 .cfunc = abort_f,
2308 .flags = CMD_NOFILE_OK,
2309 .oneline = "simulate a program crash using abort(3)",
2310};
2311
Max Reitz0e82dc72014-12-08 10:48:10 +01002312static void sigraise_help(void)
2313{
2314 printf(
2315"\n"
2316" raises the given signal\n"
2317"\n"
2318" Example:\n"
2319" 'sigraise %i' - raises SIGTERM\n"
2320"\n"
2321" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2322" given to sigraise.\n"
2323"\n", SIGTERM);
2324}
2325
Max Reitzb32d7a32018-05-09 21:42:59 +02002326static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002327
2328static const cmdinfo_t sigraise_cmd = {
2329 .name = "sigraise",
2330 .cfunc = sigraise_f,
2331 .argmin = 1,
2332 .argmax = 1,
2333 .flags = CMD_NOFILE_OK,
2334 .args = "signal",
2335 .oneline = "raises a signal",
2336 .help = sigraise_help,
2337};
2338
Max Reitzb32d7a32018-05-09 21:42:59 +02002339static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002340{
John Snow9b0beaf2015-11-05 18:53:02 -05002341 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002342 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002343 print_cvtnum_err(sig, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002344 return sig;
John Snow9b0beaf2015-11-05 18:53:02 -05002345 } else if (sig > NSIG) {
2346 printf("signal argument '%s' is too large to be a valid signal\n",
2347 argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002348 return -EINVAL;
Max Reitz0e82dc72014-12-08 10:48:10 +01002349 }
2350
2351 /* Using raise() to kill this process does not necessarily flush all open
2352 * streams. At least stdout and stderr (although the latter should be
2353 * non-buffered anyway) should be flushed, though. */
2354 fflush(stdout);
2355 fflush(stderr);
2356
2357 raise(sig);
Max Reitzb32d7a32018-05-09 21:42:59 +02002358
2359 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002360}
2361
Kevin Wolfcd33d022014-01-15 15:39:10 +01002362static void sleep_cb(void *opaque)
2363{
2364 bool *expired = opaque;
2365 *expired = true;
2366}
2367
Max Reitzb32d7a32018-05-09 21:42:59 +02002368static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002369{
2370 char *endptr;
2371 long ms;
2372 struct QEMUTimer *timer;
2373 bool expired = false;
2374
2375 ms = strtol(argv[1], &endptr, 0);
2376 if (ms < 0 || *endptr != '\0') {
2377 printf("%s is not a valid number\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002378 return -EINVAL;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002379 }
2380
2381 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2382 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2383
2384 while (!expired) {
2385 main_loop_wait(false);
2386 }
2387
2388 timer_free(timer);
Max Reitzb32d7a32018-05-09 21:42:59 +02002389 return 0;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002390}
2391
2392static const cmdinfo_t sleep_cmd = {
2393 .name = "sleep",
2394 .argmin = 1,
2395 .argmax = 1,
2396 .cfunc = sleep_f,
2397 .flags = CMD_NOFILE_OK,
2398 .oneline = "waits for the given value in milliseconds",
2399};
2400
Kevin Wolff18a8342013-06-05 14:19:33 +02002401static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2402{
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002403 printf("%s ", cmd);
Kevin Wolff18a8342013-06-05 14:19:33 +02002404
2405 if (ct->args) {
2406 printf("%s ", ct->args);
2407 }
2408 printf("-- %s\n", ct->oneline);
2409}
2410
2411static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2412{
2413 help_oneline(cmd, ct);
2414 if (ct->help) {
2415 ct->help();
2416 }
2417}
2418
2419static void help_all(void)
2420{
2421 const cmdinfo_t *ct;
2422
2423 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2424 help_oneline(ct->name, ct);
2425 }
2426 printf("\nUse 'help commandname' for extended help.\n");
2427}
2428
Max Reitzb32d7a32018-05-09 21:42:59 +02002429static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002430{
2431 const cmdinfo_t *ct;
2432
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002433 if (argc < 2) {
Kevin Wolff18a8342013-06-05 14:19:33 +02002434 help_all();
Max Reitzb32d7a32018-05-09 21:42:59 +02002435 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002436 }
2437
2438 ct = find_command(argv[1]);
2439 if (ct == NULL) {
2440 printf("command %s not found\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002441 return -EINVAL;
Kevin Wolff18a8342013-06-05 14:19:33 +02002442 }
2443
2444 help_onecmd(argv[1], ct);
Max Reitzb32d7a32018-05-09 21:42:59 +02002445 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002446}
2447
2448static const cmdinfo_t help_cmd = {
2449 .name = "help",
2450 .altname = "?",
2451 .cfunc = help_f,
2452 .argmin = 0,
2453 .argmax = 1,
2454 .flags = CMD_FLAG_GLOBAL,
2455 .args = "[command]",
2456 .oneline = "help for one or all commands",
2457};
2458
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +03002459/*
2460 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2461 * context acquired if blk is NULL.
2462 */
Max Reitzb32d7a32018-05-09 21:42:59 +02002463int qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002464{
2465 char *input;
2466 const cmdinfo_t *ct;
2467 char **v;
2468 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +02002469 int ret = 0;
Kevin Wolfdd583292013-06-05 14:19:32 +02002470
2471 input = g_strdup(cmd);
2472 v = breakline(input, &c);
2473 if (c) {
2474 ct = find_command(v[0]);
2475 if (ct) {
Max Reitzb32d7a32018-05-09 21:42:59 +02002476 ret = command(blk, ct, c, v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002477 } else {
2478 fprintf(stderr, "command \"%s\" not found\n", v[0]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002479 ret = -EINVAL;
Kevin Wolfdd583292013-06-05 14:19:32 +02002480 }
2481 }
2482 g_free(input);
2483 g_free(v);
Max Reitzb32d7a32018-05-09 21:42:59 +02002484
2485 return ret;
Kevin Wolfdd583292013-06-05 14:19:32 +02002486}
2487
Kevin Wolf797ac582013-06-05 14:19:31 +02002488static void __attribute((constructor)) init_qemuio_commands(void)
2489{
2490 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002491 qemuio_add_command(&help_cmd);
2492 qemuio_add_command(&read_cmd);
2493 qemuio_add_command(&readv_cmd);
2494 qemuio_add_command(&write_cmd);
2495 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002496 qemuio_add_command(&aio_read_cmd);
2497 qemuio_add_command(&aio_write_cmd);
2498 qemuio_add_command(&aio_flush_cmd);
2499 qemuio_add_command(&flush_cmd);
2500 qemuio_add_command(&truncate_cmd);
2501 qemuio_add_command(&length_cmd);
2502 qemuio_add_command(&info_cmd);
2503 qemuio_add_command(&discard_cmd);
2504 qemuio_add_command(&alloc_cmd);
2505 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002506 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002507 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002508 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002509 qemuio_add_command(&resume_cmd);
2510 qemuio_add_command(&wait_break_cmd);
2511 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002512 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002513 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002514}