blob: f35ea627d7b85a47a7f56ae632661b23c0f18e2a [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"
Peter Maydell5df022c2022-02-26 18:07:23 +000024#include "qemu/memalign.h"
Kevin Wolf797ac582013-06-05 14:19:31 +020025
26#define CMD_NOFILE_OK 0x01
27
Stefan Weilf9883882014-03-05 22:23:00 +010028bool qemuio_misalign;
Kevin Wolf797ac582013-06-05 14:19:31 +020029
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020030static cmdinfo_t *cmdtab;
31static int ncmds;
32
33static int compare_cmdname(const void *a, const void *b)
34{
35 return strcmp(((const cmdinfo_t *)a)->name,
36 ((const cmdinfo_t *)b)->name);
37}
38
39void qemuio_add_command(const cmdinfo_t *ci)
40{
Peter Maydell6aabeb52017-03-31 14:38:49 +010041 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
42 * flags allow it not to be, so that combination is invalid.
43 * Catch it now rather than letting it manifest as a crash if a
44 * particular set of command line options are used.
45 */
46 assert(ci->perm == 0 ||
47 (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
Markus Armbruster02c4f262014-08-19 10:31:09 +020048 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020049 cmdtab[ncmds - 1] = *ci;
50 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
51}
52
Max Reitzb444d0e2018-05-09 21:42:58 +020053void qemuio_command_usage(const cmdinfo_t *ci)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020054{
55 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020056}
57
Max Reitz4c7b7e92015-02-05 13:58:22 -050058static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020059{
60 if (ct->flags & CMD_FLAG_GLOBAL) {
61 return 1;
62 }
Max Reitz4c7b7e92015-02-05 13:58:22 -050063 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020064 fprintf(stderr, "no file open, try 'help open'\n");
65 return 0;
66 }
67 return 1;
68}
69
Max Reitzb32d7a32018-05-09 21:42:59 +020070static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
71 char **argv)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020072{
73 char *cmd = argv[0];
74
Max Reitz4c7b7e92015-02-05 13:58:22 -050075 if (!init_check_command(blk, ct)) {
Max Reitzb32d7a32018-05-09 21:42:59 +020076 return -EINVAL;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020077 }
78
79 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
80 if (ct->argmax == -1) {
81 fprintf(stderr,
82 "bad argument count %d to %s, expected at least %d arguments\n",
83 argc-1, cmd, ct->argmin);
84 } else if (ct->argmin == ct->argmax) {
85 fprintf(stderr,
86 "bad argument count %d to %s, expected %d arguments\n",
87 argc-1, cmd, ct->argmin);
88 } else {
89 fprintf(stderr,
90 "bad argument count %d to %s, expected between %d and %d arguments\n",
91 argc-1, cmd, ct->argmin, ct->argmax);
92 }
Max Reitzb32d7a32018-05-09 21:42:59 +020093 return -EINVAL;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020094 }
Kevin Wolf887354b2017-02-10 16:24:56 +010095
Vladimir Sementsov-Ogievskiy8eaf1012021-05-19 12:05:32 +030096 /*
97 * Request additional permissions if necessary for this command. The caller
Kevin Wolf887354b2017-02-10 16:24:56 +010098 * is responsible for restoring the original permissions afterwards if this
Vladimir Sementsov-Ogievskiy8eaf1012021-05-19 12:05:32 +030099 * is what it wants.
100 *
101 * Coverity thinks that blk may be NULL in the following if condition. It's
102 * not so: in init_check_command() we fail if blk is NULL for command with
103 * both CMD_FLAG_GLOBAL and CMD_NOFILE_OK flags unset. And in
104 * qemuio_add_command() we assert that command with non-zero .perm field
105 * doesn't set this flags. So, the following assertion is to silence
106 * Coverity:
107 */
108 assert(blk || !ct->perm);
Kevin Wolf887354b2017-02-10 16:24:56 +0100109 if (ct->perm && blk_is_available(blk)) {
110 uint64_t orig_perm, orig_shared_perm;
111 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
112
113 if (ct->perm & ~orig_perm) {
114 uint64_t new_perm;
115 Error *local_err = NULL;
116 int ret;
117
118 new_perm = orig_perm | ct->perm;
119
120 ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
121 if (ret < 0) {
122 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +0200123 return ret;
Kevin Wolf887354b2017-02-10 16:24:56 +0100124 }
125 }
126 }
127
Richard W.M. Jonesd339d762019-01-18 10:11:14 +0000128 qemu_reset_optind();
Max Reitzb32d7a32018-05-09 21:42:59 +0200129 return ct->cfunc(blk, argc, argv);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200130}
131
132static const cmdinfo_t *find_command(const char *cmd)
133{
134 cmdinfo_t *ct;
135
136 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
137 if (strcmp(ct->name, cmd) == 0 ||
138 (ct->altname && strcmp(ct->altname, cmd) == 0))
139 {
140 return (const cmdinfo_t *)ct;
141 }
142 }
143 return NULL;
144}
145
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100146/* Invoke fn() for commands with a matching prefix */
147void qemuio_complete_command(const char *input,
148 void (*fn)(const char *cmd, void *opaque),
149 void *opaque)
150{
151 cmdinfo_t *ct;
152 size_t input_len = strlen(input);
153
154 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
155 if (strncmp(input, ct->name, input_len) == 0) {
156 fn(ct->name, opaque);
157 }
158 }
159}
160
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200161static char **breakline(char *input, int *count)
162{
163 int c = 0;
164 char *p;
Markus Armbruster5839e532014-08-19 10:31:08 +0200165 char **rval = g_new0(char *, 1);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200166
167 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
168 if (!*p) {
169 continue;
170 }
171 c++;
Markus Armbruster08193dd2014-08-19 10:31:10 +0200172 rval = g_renew(char *, rval, (c + 1));
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200173 rval[c - 1] = p;
174 rval[c] = NULL;
175 }
176 *count = c;
177 return rval;
178}
179
Kevin Wolf797ac582013-06-05 14:19:31 +0200180static int64_t cvtnum(const char *s)
181{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100182 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100183 uint64_t value;
John Snowef5a7882015-11-05 18:53:03 -0500184
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100185 err = qemu_strtosz(s, NULL, &value);
186 if (err < 0) {
187 return err;
188 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100189 if (value > INT64_MAX) {
190 return -ERANGE;
191 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100192 return value;
Kevin Wolf797ac582013-06-05 14:19:31 +0200193}
194
John Snowa9ecfa02015-11-05 18:53:04 -0500195static void print_cvtnum_err(int64_t rc, const char *arg)
196{
197 switch (rc) {
198 case -EINVAL:
199 printf("Parsing error: non-numeric argument,"
200 " or extraneous/unrecognized suffix -- %s\n", arg);
201 break;
202 case -ERANGE:
203 printf("Parsing error: argument too large -- %s\n", arg);
204 break;
205 default:
206 printf("Parsing error: %s\n", arg);
207 }
208}
209
Kevin Wolf0b613882013-06-05 14:19:38 +0200210#define EXABYTES(x) ((long long)(x) << 60)
211#define PETABYTES(x) ((long long)(x) << 50)
212#define TERABYTES(x) ((long long)(x) << 40)
213#define GIGABYTES(x) ((long long)(x) << 30)
214#define MEGABYTES(x) ((long long)(x) << 20)
215#define KILOBYTES(x) ((long long)(x) << 10)
216
217#define TO_EXABYTES(x) ((x) / EXABYTES(1))
218#define TO_PETABYTES(x) ((x) / PETABYTES(1))
219#define TO_TERABYTES(x) ((x) / TERABYTES(1))
220#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
221#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
222#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
223
224static void cvtstr(double value, char *str, size_t size)
225{
226 char *trim;
227 const char *suffix;
228
229 if (value >= EXABYTES(1)) {
230 suffix = " EiB";
231 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
232 } else if (value >= PETABYTES(1)) {
233 suffix = " PiB";
234 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
235 } else if (value >= TERABYTES(1)) {
236 suffix = " TiB";
237 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
238 } else if (value >= GIGABYTES(1)) {
239 suffix = " GiB";
240 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
241 } else if (value >= MEGABYTES(1)) {
242 suffix = " MiB";
243 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
244 } else if (value >= KILOBYTES(1)) {
245 suffix = " KiB";
246 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
247 } else {
248 suffix = " bytes";
249 snprintf(str, size - 6, "%f", value);
250 }
251
252 trim = strstr(str, ".000");
253 if (trim) {
254 strcpy(trim, suffix);
255 } else {
256 strcat(str, suffix);
257 }
258}
259
260
261
Alex Bennée50290c02019-05-29 17:16:32 +0100262static struct timespec tsub(struct timespec t1, struct timespec t2)
Kevin Wolf0b613882013-06-05 14:19:38 +0200263{
Alex Bennée50290c02019-05-29 17:16:32 +0100264 t1.tv_nsec -= t2.tv_nsec;
265 if (t1.tv_nsec < 0) {
266 t1.tv_nsec += NANOSECONDS_PER_SECOND;
Kevin Wolf0b613882013-06-05 14:19:38 +0200267 t1.tv_sec--;
268 }
269 t1.tv_sec -= t2.tv_sec;
270 return t1;
271}
272
Alex Bennée50290c02019-05-29 17:16:32 +0100273static double tdiv(double value, struct timespec tv)
Kevin Wolf0b613882013-06-05 14:19:38 +0200274{
Alex Bennée50290c02019-05-29 17:16:32 +0100275 double seconds = tv.tv_sec + (tv.tv_nsec / 1e9);
276 return value / seconds;
Kevin Wolf0b613882013-06-05 14:19:38 +0200277}
278
279#define HOURS(sec) ((sec) / (60 * 60))
280#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
281#define SECONDS(sec) ((sec) % 60)
282
283enum {
284 DEFAULT_TIME = 0x0,
285 TERSE_FIXED_TIME = 0x1,
286 VERBOSE_FIXED_TIME = 0x2,
287};
288
Alex Bennée50290c02019-05-29 17:16:32 +0100289static void timestr(struct timespec *tv, char *ts, size_t size, int format)
Kevin Wolf0b613882013-06-05 14:19:38 +0200290{
Alex Bennée50290c02019-05-29 17:16:32 +0100291 double frac_sec = tv->tv_nsec / 1e9;
Kevin Wolf0b613882013-06-05 14:19:38 +0200292
293 if (format & TERSE_FIXED_TIME) {
294 if (!HOURS(tv->tv_sec)) {
Alex Bennée50290c02019-05-29 17:16:32 +0100295 snprintf(ts, size, "%u:%05.2f",
296 (unsigned int) MINUTES(tv->tv_sec),
297 SECONDS(tv->tv_sec) + frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200298 return;
299 }
300 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
301 }
302
303 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
Alex Bennée50290c02019-05-29 17:16:32 +0100304 snprintf(ts, size, "%u:%02u:%05.2f",
Kevin Wolf0b613882013-06-05 14:19:38 +0200305 (unsigned int) HOURS(tv->tv_sec),
306 (unsigned int) MINUTES(tv->tv_sec),
Alex Bennée50290c02019-05-29 17:16:32 +0100307 SECONDS(tv->tv_sec) + frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200308 } else {
Alex Bennée50290c02019-05-29 17:16:32 +0100309 snprintf(ts, size, "%05.2f sec", frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200310 }
311}
312
Kevin Wolf797ac582013-06-05 14:19:31 +0200313/*
314 * Parse the pattern argument to various sub-commands.
315 *
316 * Because the pattern is used as an argument to memset it must evaluate
317 * to an unsigned integer that fits into a single byte.
318 */
319static int parse_pattern(const char *arg)
320{
321 char *endptr = NULL;
322 long pattern;
323
324 pattern = strtol(arg, &endptr, 0);
325 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
326 printf("%s is not a valid pattern byte\n", arg);
327 return -1;
328 }
329
330 return pattern;
331}
332
333/*
334 * Memory allocation helpers.
335 *
336 * Make sure memory is aligned by default, or purposefully misaligned if
337 * that is specified on the command line.
338 */
339
340#define MISALIGN_OFFSET 16
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500341static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern,
342 bool register_buf)
Kevin Wolf797ac582013-06-05 14:19:31 +0200343{
344 void *buf;
345
346 if (qemuio_misalign) {
347 len += MISALIGN_OFFSET;
348 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500349 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200350 memset(buf, pattern, len);
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500351 if (register_buf) {
352 blk_register_buf(blk, buf, len, &error_abort);
353 }
Kevin Wolf797ac582013-06-05 14:19:31 +0200354 if (qemuio_misalign) {
355 buf += MISALIGN_OFFSET;
356 }
357 return buf;
358}
359
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500360static void qemu_io_free(BlockBackend *blk, void *p, size_t len,
361 bool unregister_buf)
Kevin Wolf797ac582013-06-05 14:19:31 +0200362{
363 if (qemuio_misalign) {
364 p -= MISALIGN_OFFSET;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500365 len += MISALIGN_OFFSET;
366 }
367 if (unregister_buf) {
368 blk_unregister_buf(blk, p, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200369 }
370 qemu_vfree(p);
371}
372
Denis Plotnikov4d731512019-08-20 19:46:16 +0300373/*
374 * qemu_io_alloc_from_file()
375 *
376 * Allocates the buffer and populates it with the content of the given file
377 * up to @len bytes. If the file length is less than @len, then the buffer
378 * is populated with the file content cyclically.
379 *
380 * @blk - the block backend where the buffer content is going to be written to
381 * @len - the buffer length
382 * @file_name - the file to read the content from
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500383 * @register_buf - call blk_register_buf()
Denis Plotnikov4d731512019-08-20 19:46:16 +0300384 *
385 * Returns: the buffer pointer on success
386 * NULL on error
387 */
388static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500389 const char *file_name, bool register_buf)
Denis Plotnikov4d731512019-08-20 19:46:16 +0300390{
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500391 size_t alloc_len = len + (qemuio_misalign ? MISALIGN_OFFSET : 0);
392 char *alloc_buf, *buf, *end;
Denis Plotnikov4d731512019-08-20 19:46:16 +0300393 FILE *f = fopen(file_name, "r");
394 int pattern_len;
395
396 if (!f) {
397 perror(file_name);
398 return NULL;
399 }
400
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500401 alloc_buf = buf = blk_blockalign(blk, alloc_len);
Denis Plotnikov4d731512019-08-20 19:46:16 +0300402
403 if (qemuio_misalign) {
Denis Plotnikov4d731512019-08-20 19:46:16 +0300404 buf += MISALIGN_OFFSET;
Denis Plotnikov4d731512019-08-20 19:46:16 +0300405 }
406
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500407 pattern_len = fread(buf, 1, len, f);
Denis Plotnikov4d731512019-08-20 19:46:16 +0300408
409 if (ferror(f)) {
410 perror(file_name);
411 goto error;
412 }
413
414 if (pattern_len == 0) {
415 fprintf(stderr, "%s: file is empty\n", file_name);
416 goto error;
417 }
418
419 fclose(f);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200420 f = NULL;
Denis Plotnikov4d731512019-08-20 19:46:16 +0300421
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500422 if (register_buf) {
423 blk_register_buf(blk, alloc_buf, alloc_len, &error_abort);
Denis Plotnikov4d731512019-08-20 19:46:16 +0300424 }
425
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500426 end = buf + len;
427 for (char *p = buf + pattern_len; p < end; p += pattern_len) {
428 memcpy(p, buf, MIN(pattern_len, end - p));
429 }
430
431 return buf;
Denis Plotnikov4d731512019-08-20 19:46:16 +0300432
433error:
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500434 /*
435 * This code path is only taken before blk_register_buf() is called, so
436 * hardcode the qemu_io_free() unregister_buf argument to false.
437 */
438 qemu_io_free(blk, alloc_buf, alloc_len, false);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200439 if (f) {
440 fclose(f);
441 }
Denis Plotnikov4d731512019-08-20 19:46:16 +0300442 return NULL;
443}
444
John Snow9b0beaf2015-11-05 18:53:02 -0500445static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200446{
John Snow9b0beaf2015-11-05 18:53:02 -0500447 uint64_t i;
448 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200449 const uint8_t *p;
450
451 for (i = 0, p = buffer; i < len; i += 16) {
452 const uint8_t *s = p;
453
454 printf("%08" PRIx64 ": ", offset + i);
455 for (j = 0; j < 16 && i + j < len; j++, p++) {
456 printf("%02x ", *p);
457 }
458 printf(" ");
459 for (j = 0; j < 16 && i + j < len; j++, s++) {
460 if (isalnum(*s)) {
461 printf("%c", *s);
462 } else {
463 printf(".");
464 }
465 }
466 printf("\n");
467 }
468}
469
Alex Bennée50290c02019-05-29 17:16:32 +0100470static void print_report(const char *op, struct timespec *t, int64_t offset,
Eric Blakedc388522016-05-07 21:16:42 -0600471 int64_t count, int64_t total, int cnt, bool Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200472{
473 char s1[64], s2[64], ts[64];
474
475 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
476 if (!Cflag) {
477 cvtstr((double)total, s1, sizeof(s1));
478 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500479 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200480 op, total, count, offset);
481 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
482 s1, cnt, ts, s2, tdiv((double)cnt, *t));
483 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500484 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200485 total, cnt, ts,
486 tdiv((double)total, *t),
487 tdiv((double)cnt, *t));
488 }
489}
490
491/*
492 * Parse multiple length statements for vectored I/O, and construct an I/O
493 * vector matching it.
494 */
495static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500496create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500497 int pattern, bool register_buf)
Kevin Wolf797ac582013-06-05 14:19:31 +0200498{
499 size_t *sizes = g_new0(size_t, nr_iov);
500 size_t count = 0;
501 void *buf = NULL;
502 void *p;
503 int i;
504
505 for (i = 0; i < nr_iov; i++) {
506 char *arg = argv[i];
507 int64_t len;
508
509 len = cvtnum(arg);
510 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500511 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200512 goto fail;
513 }
514
Alberto Garcia3026c462017-01-31 18:09:54 +0200515 if (len > BDRV_REQUEST_MAX_BYTES) {
516 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
517 (uint64_t)BDRV_REQUEST_MAX_BYTES);
518 goto fail;
519 }
520
521 if (count > BDRV_REQUEST_MAX_BYTES - len) {
522 printf("The total number of bytes exceed the maximum size %" PRIu64
523 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
Kevin Wolf797ac582013-06-05 14:19:31 +0200524 goto fail;
525 }
526
Kevin Wolf797ac582013-06-05 14:19:31 +0200527 sizes[i] = len;
528 count += len;
529 }
530
531 qemu_iovec_init(qiov, nr_iov);
532
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500533 buf = p = qemu_io_alloc(blk, count, pattern, register_buf);
Kevin Wolf797ac582013-06-05 14:19:31 +0200534
535 for (i = 0; i < nr_iov; i++) {
536 qemu_iovec_add(qiov, p, sizes[i]);
537 p += sizes[i];
538 }
539
540fail:
541 g_free(sizes);
542 return buf;
543}
544
John Snow9b0beaf2015-11-05 18:53:02 -0500545static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500546 int64_t bytes, BdrvRequestFlags flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200547{
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100548 int ret;
549
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300550 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500551 return -ERANGE;
552 }
553
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500554 ret = blk_pread(blk, offset, bytes, (uint8_t *)buf, flags);
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100555 if (ret < 0) {
556 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200557 }
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100558 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200559 return 1;
560}
561
John Snow9b0beaf2015-11-05 18:53:02 -0500562static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Stefan Hajnoczi1321e002023-02-07 15:37:17 -0500563 int64_t bytes, BdrvRequestFlags flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200564{
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100565 int ret;
566
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300567 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500568 return -ERANGE;
569 }
570
Alberto Fariaa9262f52022-07-05 17:15:11 +0100571 ret = blk_pwrite(blk, offset, bytes, (uint8_t *)buf, flags);
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100572 if (ret < 0) {
573 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200574 }
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100575 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200576 return 1;
577}
578
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100579static int do_pwrite_zeroes(BlockBackend *blk, int64_t offset,
Stefan Hajnoczi1321e002023-02-07 15:37:17 -0500580 int64_t bytes, BdrvRequestFlags flags,
581 int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200582{
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100583 int ret = blk_pwrite_zeroes(blk, offset, bytes,
584 flags | BDRV_REQ_ZERO_WRITE);
Kevin Wolf797ac582013-06-05 14:19:31 +0200585
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100586 if (ret < 0) {
587 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200588 }
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100589 *total = bytes;
590 return 1;
Kevin Wolf797ac582013-06-05 14:19:31 +0200591}
592
Max Reitz4c7b7e92015-02-05 13:58:22 -0500593static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300594 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200595{
596 int ret;
597
Alberto Garcia41ae31e2019-05-14 16:57:35 +0300598 if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500599 return -ERANGE;
600 }
601
Alberto Faria0cadf2c2022-07-05 17:15:18 +0100602 ret = blk_pwrite_compressed(blk, offset, bytes, buf);
Kevin Wolf797ac582013-06-05 14:19:31 +0200603 if (ret < 0) {
604 return ret;
605 }
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300606 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200607 return 1;
608}
609
Max Reitz4c7b7e92015-02-05 13:58:22 -0500610static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500611 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200612{
John Snow9b0beaf2015-11-05 18:53:02 -0500613 if (count > INT_MAX) {
614 return -ERANGE;
615 }
616
Max Reitz4c7b7e92015-02-05 13:58:22 -0500617 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200618 if (*total < 0) {
619 return *total;
620 }
621 return 1;
622}
623
Max Reitz4c7b7e92015-02-05 13:58:22 -0500624static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500625 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200626{
John Snow9b0beaf2015-11-05 18:53:02 -0500627 if (count > INT_MAX) {
628 return -ERANGE;
629 }
630
Max Reitz4c7b7e92015-02-05 13:58:22 -0500631 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200632 if (*total < 0) {
633 return *total;
634 }
635 return 1;
636}
637
638#define NOT_DONE 0x7fffffff
639static void aio_rw_done(void *opaque, int ret)
640{
641 *(int *)opaque = ret;
642}
643
Max Reitz4c7b7e92015-02-05 13:58:22 -0500644static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500645 int64_t offset, BdrvRequestFlags flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200646{
647 int async_ret = NOT_DONE;
648
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500649 blk_aio_preadv(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200650 while (async_ret == NOT_DONE) {
651 main_loop_wait(false);
652 }
653
654 *total = qiov->size;
655 return async_ret < 0 ? async_ret : 1;
656}
657
Max Reitz4c7b7e92015-02-05 13:58:22 -0500658static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Stefan Hajnoczi1321e002023-02-07 15:37:17 -0500659 int64_t offset, BdrvRequestFlags flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200660{
661 int async_ret = NOT_DONE;
662
Eric Blake770e0e02016-05-07 21:16:44 -0600663 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200664 while (async_ret == NOT_DONE) {
665 main_loop_wait(false);
666 }
667
668 *total = qiov->size;
669 return async_ret < 0 ? async_ret : 1;
670}
671
Kevin Wolf797ac582013-06-05 14:19:31 +0200672static void read_help(void)
673{
674 printf(
675"\n"
676" reads a range of bytes from the given offset\n"
677"\n"
678" Example:\n"
679" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
680"\n"
681" Reads a segment of the currently open file, optionally dumping it to the\n"
682" standard output stream (with -v option) for subsequent inspection.\n"
683" -b, -- read from the VM state rather than the virtual disk\n"
684" -C, -- report statistics in a machine parsable format\n"
685" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600686" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200687" -P, -- use a pattern to verify read data\n"
688" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500689" -r, -- register I/O buffer\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200690" -s, -- start offset for pattern verification (only with -P)\n"
691" -v, -- dump buffer to standard output\n"
692"\n");
693}
694
Max Reitzb32d7a32018-05-09 21:42:59 +0200695static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200696
697static const cmdinfo_t read_cmd = {
698 .name = "read",
699 .altname = "r",
700 .cfunc = read_f,
701 .argmin = 2,
702 .argmax = -1,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500703 .args = "[-abCqrv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200704 .oneline = "reads a number of bytes at a specified offset",
705 .help = read_help,
706};
707
Max Reitzb32d7a32018-05-09 21:42:59 +0200708static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200709{
Alex Bennée50290c02019-05-29 17:16:32 +0100710 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600711 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600712 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200713 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200714 char *buf;
715 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500716 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200717 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500718 int64_t total = 0;
719 int pattern = 0;
720 int64_t pattern_offset = 0, pattern_count = 0;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500721 BdrvRequestFlags flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200722
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500723 while ((c = getopt(argc, argv, "bCl:pP:qrs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200724 switch (c) {
725 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600726 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200727 break;
728 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600729 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200730 break;
731 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600732 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200733 pattern_count = cvtnum(optarg);
734 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500735 print_cvtnum_err(pattern_count, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200736 return pattern_count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200737 }
738 break;
739 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600740 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200741 break;
742 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600743 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200744 pattern = parse_pattern(optarg);
745 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200746 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200747 }
748 break;
749 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600750 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200751 break;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500752 case 'r':
753 flags |= BDRV_REQ_REGISTERED_BUF;
754 break;
Kevin Wolf797ac582013-06-05 14:19:31 +0200755 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600756 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200757 pattern_offset = cvtnum(optarg);
758 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500759 print_cvtnum_err(pattern_offset, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200760 return pattern_offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200761 }
762 break;
763 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600764 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200765 break;
766 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200767 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200768 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200769 }
770 }
771
772 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200773 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200774 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200775 }
776
Kevin Wolf797ac582013-06-05 14:19:31 +0200777 offset = cvtnum(argv[optind]);
778 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500779 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200780 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200781 }
782
783 optind++;
784 count = cvtnum(argv[optind]);
785 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500786 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200787 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +0200788 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500789 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200790 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200791 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200792 }
793
794 if (!Pflag && (lflag || sflag)) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200795 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200796 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200797 }
798
799 if (!lflag) {
800 pattern_count = count - pattern_offset;
801 }
802
803 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
804 printf("pattern verification range exceeds end of read data\n");
Max Reitzb32d7a32018-05-09 21:42:59 +0200805 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200806 }
807
Eric Blake093ea232016-05-07 21:16:43 -0600808 if (bflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -0500809 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
810 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200811 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +0200812 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200813 }
Eric Blake1bce6b42017-04-29 14:14:11 -0500814 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
815 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200816 count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200817 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200818 }
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500819 if (flags & BDRV_REQ_REGISTERED_BUF) {
820 printf("I/O buffer registration is not supported when reading "
821 "from vmstate\n");
822 return -EINVAL;
823 }
Kevin Wolf797ac582013-06-05 14:19:31 +0200824 }
825
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500826 buf = qemu_io_alloc(blk, count, 0xab, flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +0200827
Alex Bennée50290c02019-05-29 17:16:32 +0100828 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -0600829 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200830 ret = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200831 } else {
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500832 ret = do_pread(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200833 }
Alex Bennée50290c02019-05-29 17:16:32 +0100834 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200835
Max Reitzb32d7a32018-05-09 21:42:59 +0200836 if (ret < 0) {
837 printf("read failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200838 goto out;
839 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200840 cnt = ret;
841
842 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200843
844 if (Pflag) {
845 void *cmp_buf = g_malloc(pattern_count);
846 memset(cmp_buf, pattern, pattern_count);
847 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
848 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500849 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200850 offset + pattern_offset, pattern_count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200851 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200852 }
853 g_free(cmp_buf);
854 }
855
856 if (qflag) {
857 goto out;
858 }
859
860 if (vflag) {
861 dump_buffer(buf, offset, count);
862 }
863
864 /* Finally, report back -- -C gives a parsable format */
865 t2 = tsub(t2, t1);
866 print_report("read", &t2, offset, count, total, cnt, Cflag);
867
868out:
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500869 qemu_io_free(blk, buf, count, flags & BDRV_REQ_REGISTERED_BUF);
Max Reitzb32d7a32018-05-09 21:42:59 +0200870 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200871}
872
873static void readv_help(void)
874{
875 printf(
876"\n"
877" reads a range of bytes from the given offset into multiple buffers\n"
878"\n"
879" Example:\n"
880" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
881"\n"
882" Reads a segment of the currently open file, optionally dumping it to the\n"
883" standard output stream (with -v option) for subsequent inspection.\n"
884" Uses multiple iovec buffers if more than one byte range is specified.\n"
885" -C, -- report statistics in a machine parsable format\n"
886" -P, -- use a pattern to verify read data\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200887" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500888" -r, -- register I/O buffer\n"
889" -v, -- dump buffer to standard output\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200890"\n");
891}
892
Max Reitzb32d7a32018-05-09 21:42:59 +0200893static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200894
895static const cmdinfo_t readv_cmd = {
896 .name = "readv",
897 .cfunc = readv_f,
898 .argmin = 2,
899 .argmax = -1,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500900 .args = "[-Cqrv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200901 .oneline = "reads a number of bytes at a specified offset",
902 .help = readv_help,
903};
904
Max Reitzb32d7a32018-05-09 21:42:59 +0200905static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200906{
Alex Bennée50290c02019-05-29 17:16:32 +0100907 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600908 bool Cflag = false, qflag = false, vflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200909 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200910 char *buf;
911 int64_t offset;
912 /* Some compilers get confused and warn if this is not initialized. */
913 int total = 0;
914 int nr_iov;
915 QEMUIOVector qiov;
916 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600917 bool Pflag = false;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500918 BdrvRequestFlags flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200919
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500920 while ((c = getopt(argc, argv, "CP:qrv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200921 switch (c) {
922 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600923 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200924 break;
925 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600926 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200927 pattern = parse_pattern(optarg);
928 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200929 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200930 }
931 break;
932 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600933 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200934 break;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500935 case 'r':
936 flags |= BDRV_REQ_REGISTERED_BUF;
937 break;
Kevin Wolf797ac582013-06-05 14:19:31 +0200938 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600939 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200940 break;
941 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200942 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200943 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200944 }
945 }
946
947 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200948 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200949 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200950 }
951
952
953 offset = cvtnum(argv[optind]);
954 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500955 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200956 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200957 }
958 optind++;
959
Kevin Wolf797ac582013-06-05 14:19:31 +0200960 nr_iov = argc - optind;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500961 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab,
962 flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +0200963 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200964 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200965 }
966
Alex Bennée50290c02019-05-29 17:16:32 +0100967 clock_gettime(CLOCK_MONOTONIC, &t1);
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -0500968 ret = do_aio_readv(blk, &qiov, offset, flags, &total);
Alex Bennée50290c02019-05-29 17:16:32 +0100969 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200970
Max Reitzb32d7a32018-05-09 21:42:59 +0200971 if (ret < 0) {
972 printf("readv failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200973 goto out;
974 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200975 cnt = ret;
976
977 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200978
979 if (Pflag) {
980 void *cmp_buf = g_malloc(qiov.size);
981 memset(cmp_buf, pattern, qiov.size);
982 if (memcmp(buf, cmp_buf, qiov.size)) {
983 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +0200984 PRId64 ", %zu bytes\n", offset, qiov.size);
Max Reitzb32d7a32018-05-09 21:42:59 +0200985 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200986 }
987 g_free(cmp_buf);
988 }
989
990 if (qflag) {
991 goto out;
992 }
993
994 if (vflag) {
995 dump_buffer(buf, offset, qiov.size);
996 }
997
998 /* Finally, report back -- -C gives a parsable format */
999 t2 = tsub(t2, t1);
1000 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
1001
1002out:
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001003 qemu_io_free(blk, buf, qiov.size, flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +02001004 qemu_iovec_destroy(&qiov);
Max Reitzb32d7a32018-05-09 21:42:59 +02001005 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001006}
1007
1008static void write_help(void)
1009{
1010 printf(
1011"\n"
1012" writes a range of bytes from the given offset\n"
1013"\n"
1014" Example:\n"
1015" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1016"\n"
1017" Writes into a segment of the currently open file, using a buffer\n"
1018" filled with a set pattern (0xcdcdcdcd).\n"
1019" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -05001020" -c, -- write compressed data with blk_write_compressed\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001021" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001022" -f, -- use Force Unit Access semantics\n"
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001023" -n, -- with -z, don't allow slow fallback\n"
Eric Blake093ea232016-05-07 21:16:43 -06001024" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001025" -P, -- use different pattern to fill file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001026" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001027" -r, -- register I/O buffer\n"
1028" -s, -- use a pattern file to fill the write buffer\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001029" -u, -- with -z, allow unmapping\n"
Paolo Bonzini264dcbb2022-12-15 14:02:23 +01001030" -z, -- write zeroes using blk_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001031"\n");
1032}
1033
Max Reitzb32d7a32018-05-09 21:42:59 +02001034static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001035
1036static const cmdinfo_t write_cmd = {
1037 .name = "write",
1038 .altname = "w",
1039 .cfunc = write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001040 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001041 .argmin = 2,
1042 .argmax = -1,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001043 .args = "[-bcCfnqruz] [-P pattern | -s source_file] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +02001044 .oneline = "writes a number of bytes at a specified offset",
1045 .help = write_help,
1046};
1047
Max Reitzb32d7a32018-05-09 21:42:59 +02001048static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001049{
Alex Bennée50290c02019-05-29 17:16:32 +01001050 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -06001051 bool Cflag = false, qflag = false, bflag = false;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001052 bool Pflag = false, zflag = false, cflag = false, sflag = false;
Stefan Hajnoczi1321e002023-02-07 15:37:17 -05001053 BdrvRequestFlags flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001054 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001055 char *buf = NULL;
1056 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -05001057 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001058 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -05001059 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001060 int pattern = 0xcd;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001061 const char *file_name = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001062
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001063 while ((c = getopt(argc, argv, "bcCfnpP:qrs:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001064 switch (c) {
1065 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -06001066 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001067 break;
1068 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -06001069 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001070 break;
1071 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001072 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001073 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001074 case 'f':
1075 flags |= BDRV_REQ_FUA;
1076 break;
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001077 case 'n':
1078 flags |= BDRV_REQ_NO_FALLBACK;
1079 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001080 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -06001081 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +02001082 break;
1083 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001084 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001085 pattern = parse_pattern(optarg);
1086 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001087 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001088 }
1089 break;
1090 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001091 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001092 break;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001093 case 'r':
1094 flags |= BDRV_REQ_REGISTERED_BUF;
1095 break;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001096 case 's':
1097 sflag = true;
1098 file_name = optarg;
1099 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001100 case 'u':
1101 flags |= BDRV_REQ_MAY_UNMAP;
1102 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001103 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001104 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001105 break;
1106 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001107 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001108 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001109 }
1110 }
1111
1112 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001113 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001114 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001115 }
1116
Eric Blake093ea232016-05-07 21:16:43 -06001117 if (bflag && zflag) {
1118 printf("-b and -z cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001119 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001120 }
1121
Eric Blake770e0e02016-05-07 21:16:44 -06001122 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1123 printf("-f and -b or -c cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001124 return -EINVAL;
Eric Blake770e0e02016-05-07 21:16:44 -06001125 }
1126
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001127 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1128 printf("-n requires -z to be specified\n");
1129 return -EINVAL;
1130 }
1131
Eric Blakec2e001c2016-05-07 21:16:45 -06001132 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1133 printf("-u requires -z to be specified\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001134 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001135 }
1136
Denis Plotnikov4d731512019-08-20 19:46:16 +03001137 if (zflag + Pflag + sflag > 1) {
1138 printf("Only one of -z, -P, and -s "
1139 "can be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001140 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001141 }
1142
1143 offset = cvtnum(argv[optind]);
1144 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001145 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001146 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001147 }
1148
1149 optind++;
1150 count = cvtnum(argv[optind]);
1151 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001152 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001153 return count;
Eric Blake395aecd2021-12-03 17:15:28 -06001154 } else if (count > BDRV_REQUEST_MAX_BYTES &&
1155 !(flags & BDRV_REQ_NO_FALLBACK)) {
1156 printf("length cannot exceed %" PRIu64 " without -n, given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001157 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001158 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001159 }
1160
Eric Blake093ea232016-05-07 21:16:43 -06001161 if (bflag || cflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -05001162 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1163 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001164 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +02001165 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001166 }
1167
Eric Blake1bce6b42017-04-29 14:14:11 -05001168 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1169 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001170 count);
Max Reitzb32d7a32018-05-09 21:42:59 +02001171 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001172 }
1173 }
1174
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001175 if (zflag) {
1176 if (flags & BDRV_REQ_REGISTERED_BUF) {
1177 printf("cannot combine zero write with registered I/O buffer\n");
1178 return -EINVAL;
1179 }
1180 } else {
Denis Plotnikov4d731512019-08-20 19:46:16 +03001181 if (sflag) {
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001182 buf = qemu_io_alloc_from_file(blk, count, file_name,
1183 flags & BDRV_REQ_REGISTERED_BUF);
Denis Plotnikov4d731512019-08-20 19:46:16 +03001184 if (!buf) {
1185 return -EINVAL;
1186 }
1187 } else {
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001188 buf = qemu_io_alloc(blk, count, pattern,
1189 flags & BDRV_REQ_REGISTERED_BUF);
Denis Plotnikov4d731512019-08-20 19:46:16 +03001190 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001191 }
1192
Alex Bennée50290c02019-05-29 17:16:32 +01001193 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -06001194 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001195 ret = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001196 } else if (zflag) {
Paolo Bonzini264dcbb2022-12-15 14:02:23 +01001197 ret = do_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001198 } else if (cflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001199 ret = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001200 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +02001201 ret = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001202 }
Alex Bennée50290c02019-05-29 17:16:32 +01001203 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001204
Max Reitzb32d7a32018-05-09 21:42:59 +02001205 if (ret < 0) {
1206 printf("write failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001207 goto out;
1208 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001209 cnt = ret;
1210
1211 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001212
1213 if (qflag) {
1214 goto out;
1215 }
1216
1217 /* Finally, report back -- -C gives a parsable format */
1218 t2 = tsub(t2, t1);
1219 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1220
1221out:
1222 if (!zflag) {
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001223 qemu_io_free(blk, buf, count, flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +02001224 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001225 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001226}
1227
1228static void
1229writev_help(void)
1230{
1231 printf(
1232"\n"
1233" writes a range of bytes from the given offset source from multiple buffers\n"
1234"\n"
1235" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001236" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001237"\n"
1238" Writes into a segment of the currently open file, using a buffer\n"
1239" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001240" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001241" -f, -- use Force Unit Access semantics\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001242" -P, -- use different pattern to fill file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001243" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001244" -r, -- register I/O buffer\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001245"\n");
1246}
1247
Max Reitzb32d7a32018-05-09 21:42:59 +02001248static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001249
1250static const cmdinfo_t writev_cmd = {
1251 .name = "writev",
1252 .cfunc = writev_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001253 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001254 .argmin = 2,
1255 .argmax = -1,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001256 .args = "[-Cfqr] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001257 .oneline = "writes a number of bytes at a specified offset",
1258 .help = writev_help,
1259};
1260
Max Reitzb32d7a32018-05-09 21:42:59 +02001261static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001262{
Alex Bennée50290c02019-05-29 17:16:32 +01001263 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001264 bool Cflag = false, qflag = false;
Stefan Hajnoczi1321e002023-02-07 15:37:17 -05001265 BdrvRequestFlags flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001266 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001267 char *buf;
1268 int64_t offset;
1269 /* Some compilers get confused and warn if this is not initialized. */
1270 int total = 0;
1271 int nr_iov;
1272 int pattern = 0xcd;
1273 QEMUIOVector qiov;
1274
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001275 while ((c = getopt(argc, argv, "CfP:qr")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001276 switch (c) {
1277 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001278 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001279 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001280 case 'f':
1281 flags |= BDRV_REQ_FUA;
1282 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001283 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001284 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001285 break;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001286 case 'r':
1287 flags |= BDRV_REQ_REGISTERED_BUF;
1288 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001289 case 'P':
1290 pattern = parse_pattern(optarg);
1291 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001292 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001293 }
1294 break;
1295 default:
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
1301 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001302 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001303 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001304 }
1305
1306 offset = cvtnum(argv[optind]);
1307 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001308 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001309 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001310 }
1311 optind++;
1312
Kevin Wolf797ac582013-06-05 14:19:31 +02001313 nr_iov = argc - optind;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001314 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern,
1315 flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +02001316 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001317 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001318 }
1319
Alex Bennée50290c02019-05-29 17:16:32 +01001320 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001321 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
Alex Bennée50290c02019-05-29 17:16:32 +01001322 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001323
Max Reitzb32d7a32018-05-09 21:42:59 +02001324 if (ret < 0) {
1325 printf("writev failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001326 goto out;
1327 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001328 cnt = ret;
1329
1330 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001331
1332 if (qflag) {
1333 goto out;
1334 }
1335
1336 /* Finally, report back -- -C gives a parsable format */
1337 t2 = tsub(t2, t1);
1338 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1339out:
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001340 qemu_io_free(blk, buf, qiov.size, flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +02001341 qemu_iovec_destroy(&qiov);
Max Reitzb32d7a32018-05-09 21:42:59 +02001342 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001343}
1344
Kevin Wolf797ac582013-06-05 14:19:31 +02001345struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001346 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001347 QEMUIOVector qiov;
1348 int64_t offset;
1349 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001350 bool qflag;
1351 bool vflag;
1352 bool Cflag;
1353 bool Pflag;
1354 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001355 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001356 int pattern;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001357 BdrvRequestFlags flags;
Alex Bennée50290c02019-05-29 17:16:32 +01001358 struct timespec t1;
Kevin Wolf797ac582013-06-05 14:19:31 +02001359};
1360
1361static void aio_write_done(void *opaque, int ret)
1362{
1363 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001364 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001365
Alex Bennée50290c02019-05-29 17:16:32 +01001366 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001367
1368
1369 if (ret < 0) {
1370 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001371 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001372 goto out;
1373 }
1374
Max Reitz4c7b7e92015-02-05 13:58:22 -05001375 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001376
Kevin Wolf797ac582013-06-05 14:19:31 +02001377 if (ctx->qflag) {
1378 goto out;
1379 }
1380
1381 /* Finally, report back -- -C gives a parsable format */
1382 t2 = tsub(t2, ctx->t1);
1383 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1384 ctx->qiov.size, 1, ctx->Cflag);
1385out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001386 if (!ctx->zflag) {
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001387 qemu_io_free(ctx->blk, ctx->buf, ctx->qiov.size,
1388 ctx->flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001389 qemu_iovec_destroy(&ctx->qiov);
1390 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001391 g_free(ctx);
1392}
1393
1394static void aio_read_done(void *opaque, int ret)
1395{
1396 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001397 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001398
Alex Bennée50290c02019-05-29 17:16:32 +01001399 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001400
1401 if (ret < 0) {
1402 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001403 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001404 goto out;
1405 }
1406
1407 if (ctx->Pflag) {
1408 void *cmp_buf = g_malloc(ctx->qiov.size);
1409
1410 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1411 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1412 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +02001413 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
Kevin Wolf797ac582013-06-05 14:19:31 +02001414 }
1415 g_free(cmp_buf);
1416 }
1417
Max Reitz4c7b7e92015-02-05 13:58:22 -05001418 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001419
Kevin Wolf797ac582013-06-05 14:19:31 +02001420 if (ctx->qflag) {
1421 goto out;
1422 }
1423
1424 if (ctx->vflag) {
1425 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1426 }
1427
1428 /* Finally, report back -- -C gives a parsable format */
1429 t2 = tsub(t2, ctx->t1);
1430 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1431 ctx->qiov.size, 1, ctx->Cflag);
1432out:
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001433 qemu_io_free(ctx->blk, ctx->buf, ctx->qiov.size,
1434 ctx->flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +02001435 qemu_iovec_destroy(&ctx->qiov);
1436 g_free(ctx);
1437}
1438
1439static void aio_read_help(void)
1440{
1441 printf(
1442"\n"
1443" asynchronously reads a range of bytes from the given offset\n"
1444"\n"
1445" Example:\n"
1446" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1447"\n"
1448" Reads a segment of the currently open file, optionally dumping it to the\n"
1449" standard output stream (with -v option) for subsequent inspection.\n"
1450" The read is performed asynchronously and the aio_flush command must be\n"
1451" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001452" Note that due to its asynchronous nature, this command will be\n"
1453" considered successful once the request is submitted, independently\n"
1454" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001455" -C, -- report statistics in a machine parsable format\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001456" -i, -- treat request as invalid, for exercising stats\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001457" -P, -- use a pattern to verify read data\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001458" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001459" -r, -- register I/O buffer\n"
1460" -v, -- dump buffer to standard output\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001461"\n");
1462}
1463
Max Reitzb32d7a32018-05-09 21:42:59 +02001464static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001465
1466static const cmdinfo_t aio_read_cmd = {
1467 .name = "aio_read",
1468 .cfunc = aio_read_f,
1469 .argmin = 2,
1470 .argmax = -1,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001471 .args = "[-Ciqrv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001472 .oneline = "asynchronously reads a number of bytes",
1473 .help = aio_read_help,
1474};
1475
Max Reitzb32d7a32018-05-09 21:42:59 +02001476static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001477{
1478 int nr_iov, c;
1479 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1480
Max Reitz4c7b7e92015-02-05 13:58:22 -05001481 ctx->blk = blk;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001482 while ((c = getopt(argc, argv, "CiP:qrv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001483 switch (c) {
1484 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001485 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001486 break;
1487 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001488 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001489 ctx->pattern = parse_pattern(optarg);
1490 if (ctx->pattern < 0) {
1491 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001492 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001493 }
1494 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001495 case 'i':
1496 printf("injecting invalid read request\n");
1497 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1498 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001499 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001500 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001501 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001502 break;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001503 case 'r':
1504 ctx->flags |= BDRV_REQ_REGISTERED_BUF;
1505 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001506 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001507 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001508 break;
1509 default:
1510 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001511 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001512 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001513 }
1514 }
1515
1516 if (optind > argc - 2) {
1517 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001518 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001519 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001520 }
1521
1522 ctx->offset = cvtnum(argv[optind]);
1523 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001524 int ret = ctx->offset;
1525 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001526 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001527 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001528 }
1529 optind++;
1530
Kevin Wolf797ac582013-06-05 14:19:31 +02001531 nr_iov = argc - optind;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001532 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab,
1533 ctx->flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf797ac582013-06-05 14:19:31 +02001534 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001535 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001536 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001537 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001538 }
1539
Alex Bennée50290c02019-05-29 17:16:32 +01001540 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001541 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1542 BLOCK_ACCT_READ);
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001543 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, ctx->flags, aio_read_done,
1544 ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001545 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001546}
1547
1548static void aio_write_help(void)
1549{
1550 printf(
1551"\n"
1552" asynchronously writes a range of bytes from the given offset source\n"
1553" from multiple buffers\n"
1554"\n"
1555" Example:\n"
1556" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1557"\n"
1558" Writes into a segment of the currently open file, using a buffer\n"
1559" filled with a set pattern (0xcdcdcdcd).\n"
1560" The write is performed asynchronously and the aio_flush command must be\n"
1561" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001562" Note that due to its asynchronous nature, this command will be\n"
1563" considered successful once the request is submitted, independently\n"
1564" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001565" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001566" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001567" -i, -- treat request as invalid, for exercising stats\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001568" -P, -- use different pattern to fill file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001569" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001570" -r, -- register I/O buffer\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001571" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001572" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001573"\n");
1574}
1575
Max Reitzb32d7a32018-05-09 21:42:59 +02001576static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001577
1578static const cmdinfo_t aio_write_cmd = {
1579 .name = "aio_write",
1580 .cfunc = aio_write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001581 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001582 .argmin = 2,
1583 .argmax = -1,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001584 .args = "[-Cfiqruz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001585 .oneline = "asynchronously writes a number of bytes",
1586 .help = aio_write_help,
1587};
1588
Max Reitzb32d7a32018-05-09 21:42:59 +02001589static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001590{
1591 int nr_iov, c;
1592 int pattern = 0xcd;
1593 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1594
Max Reitz4c7b7e92015-02-05 13:58:22 -05001595 ctx->blk = blk;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001596 while ((c = getopt(argc, argv, "CfiP:qruz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001597 switch (c) {
1598 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001599 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001600 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001601 case 'f':
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001602 ctx->flags |= BDRV_REQ_FUA;
Eric Blake770e0e02016-05-07 21:16:44 -06001603 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001604 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001605 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001606 break;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001607 case 'r':
1608 ctx->flags |= BDRV_REQ_REGISTERED_BUF;
1609 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001610 case 'u':
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001611 ctx->flags |= BDRV_REQ_MAY_UNMAP;
Eric Blakec2e001c2016-05-07 21:16:45 -06001612 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001613 case 'P':
1614 pattern = parse_pattern(optarg);
1615 if (pattern < 0) {
1616 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001617 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001618 }
1619 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001620 case 'i':
1621 printf("injecting invalid write request\n");
1622 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1623 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001624 return 0;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001625 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001626 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001627 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001628 default:
1629 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001630 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001631 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001632 }
1633 }
1634
1635 if (optind > argc - 2) {
1636 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001637 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001638 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001639 }
1640
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001641 if (ctx->zflag && optind != argc - 2) {
1642 printf("-z supports only a single length parameter\n");
1643 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001644 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001645 }
1646
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001647 if ((ctx->flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
Eric Blakec2e001c2016-05-07 21:16:45 -06001648 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001649 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001650 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001651 }
1652
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001653 if (ctx->zflag && ctx->Pflag) {
1654 printf("-z and -P cannot be specified at the same time\n");
1655 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001656 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001657 }
1658
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001659 if (ctx->zflag && (ctx->flags & BDRV_REQ_REGISTERED_BUF)) {
1660 printf("cannot combine zero write with registered I/O buffer\n");
1661 g_free(ctx);
1662 return -EINVAL;
1663 }
1664
Kevin Wolf797ac582013-06-05 14:19:31 +02001665 ctx->offset = cvtnum(argv[optind]);
1666 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001667 int ret = ctx->offset;
1668 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001669 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001670 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001671 }
1672 optind++;
1673
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001674 if (ctx->zflag) {
1675 int64_t count = cvtnum(argv[optind]);
1676 if (count < 0) {
1677 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001678 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001679 return count;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001680 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001681
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001682 ctx->qiov.size = count;
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001683 blk_aio_pwrite_zeroes(blk, ctx->offset, count, ctx->flags,
1684 aio_write_done, ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001685 } else {
1686 nr_iov = argc - optind;
1687 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001688 pattern, ctx->flags & BDRV_REQ_REGISTERED_BUF);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001689 if (ctx->buf == NULL) {
1690 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1691 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001692 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001693 }
1694
Alex Bennée50290c02019-05-29 17:16:32 +01001695 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001696 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1697 BLOCK_ACCT_WRITE);
1698
Stefan Hajnoczi00e2a042023-02-07 15:37:18 -05001699 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, ctx->flags,
1700 aio_write_done, ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001701 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001702
1703 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001704}
1705
Max Reitzb32d7a32018-05-09 21:42:59 +02001706static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001707{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001708 BlockAcctCookie cookie;
1709 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001710 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001711 block_acct_done(blk_get_stats(blk), &cookie);
Max Reitzb32d7a32018-05-09 21:42:59 +02001712 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001713}
1714
1715static const cmdinfo_t aio_flush_cmd = {
1716 .name = "aio_flush",
1717 .cfunc = aio_flush_f,
1718 .oneline = "completes all outstanding aio requests"
1719};
1720
Max Reitzb32d7a32018-05-09 21:42:59 +02001721static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001722{
Max Reitzb32d7a32018-05-09 21:42:59 +02001723 return blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001724}
1725
1726static const cmdinfo_t flush_cmd = {
1727 .name = "flush",
1728 .altname = "f",
1729 .cfunc = flush_f,
1730 .oneline = "flush all in-core file state to disk",
1731};
1732
Sam Li6d43eaa2023-05-08 12:55:28 +08001733static inline int64_t tosector(int64_t bytes)
1734{
1735 return bytes >> BDRV_SECTOR_BITS;
1736}
1737
1738static int zone_report_f(BlockBackend *blk, int argc, char **argv)
1739{
1740 int ret;
1741 int64_t offset;
1742 unsigned int nr_zones;
1743
1744 ++optind;
1745 offset = cvtnum(argv[optind]);
1746 ++optind;
1747 nr_zones = cvtnum(argv[optind]);
1748
1749 g_autofree BlockZoneDescriptor *zones = NULL;
1750 zones = g_new(BlockZoneDescriptor, nr_zones);
1751 ret = blk_zone_report(blk, offset, &nr_zones, zones);
1752 if (ret < 0) {
1753 printf("zone report failed: %s\n", strerror(-ret));
1754 } else {
1755 for (int i = 0; i < nr_zones; ++i) {
1756 printf("start: 0x%" PRIx64 ", len 0x%" PRIx64 ", "
1757 "cap"" 0x%" PRIx64 ", wptr 0x%" PRIx64 ", "
1758 "zcond:%u, [type: %u]\n",
1759 tosector(zones[i].start), tosector(zones[i].length),
1760 tosector(zones[i].cap), tosector(zones[i].wp),
1761 zones[i].state, zones[i].type);
1762 }
1763 }
1764 return ret;
1765}
1766
1767static const cmdinfo_t zone_report_cmd = {
1768 .name = "zone_report",
1769 .altname = "zrp",
1770 .cfunc = zone_report_f,
1771 .argmin = 2,
1772 .argmax = 2,
1773 .args = "offset number",
1774 .oneline = "report zone information",
1775};
1776
1777static int zone_open_f(BlockBackend *blk, int argc, char **argv)
1778{
1779 int ret;
1780 int64_t offset, len;
1781 ++optind;
1782 offset = cvtnum(argv[optind]);
1783 ++optind;
1784 len = cvtnum(argv[optind]);
1785 ret = blk_zone_mgmt(blk, BLK_ZO_OPEN, offset, len);
1786 if (ret < 0) {
1787 printf("zone open failed: %s\n", strerror(-ret));
1788 }
1789 return ret;
1790}
1791
1792static const cmdinfo_t zone_open_cmd = {
1793 .name = "zone_open",
1794 .altname = "zo",
1795 .cfunc = zone_open_f,
1796 .argmin = 2,
1797 .argmax = 2,
1798 .args = "offset len",
1799 .oneline = "explicit open a range of zones in zone block device",
1800};
1801
1802static int zone_close_f(BlockBackend *blk, int argc, char **argv)
1803{
1804 int ret;
1805 int64_t offset, len;
1806 ++optind;
1807 offset = cvtnum(argv[optind]);
1808 ++optind;
1809 len = cvtnum(argv[optind]);
1810 ret = blk_zone_mgmt(blk, BLK_ZO_CLOSE, offset, len);
1811 if (ret < 0) {
1812 printf("zone close failed: %s\n", strerror(-ret));
1813 }
1814 return ret;
1815}
1816
1817static const cmdinfo_t zone_close_cmd = {
1818 .name = "zone_close",
1819 .altname = "zc",
1820 .cfunc = zone_close_f,
1821 .argmin = 2,
1822 .argmax = 2,
1823 .args = "offset len",
1824 .oneline = "close a range of zones in zone block device",
1825};
1826
1827static int zone_finish_f(BlockBackend *blk, int argc, char **argv)
1828{
1829 int ret;
1830 int64_t offset, len;
1831 ++optind;
1832 offset = cvtnum(argv[optind]);
1833 ++optind;
1834 len = cvtnum(argv[optind]);
1835 ret = blk_zone_mgmt(blk, BLK_ZO_FINISH, offset, len);
1836 if (ret < 0) {
1837 printf("zone finish failed: %s\n", strerror(-ret));
1838 }
1839 return ret;
1840}
1841
1842static const cmdinfo_t zone_finish_cmd = {
1843 .name = "zone_finish",
1844 .altname = "zf",
1845 .cfunc = zone_finish_f,
1846 .argmin = 2,
1847 .argmax = 2,
1848 .args = "offset len",
1849 .oneline = "finish a range of zones in zone block device",
1850};
1851
1852static int zone_reset_f(BlockBackend *blk, int argc, char **argv)
1853{
1854 int ret;
1855 int64_t offset, len;
1856 ++optind;
1857 offset = cvtnum(argv[optind]);
1858 ++optind;
1859 len = cvtnum(argv[optind]);
1860 ret = blk_zone_mgmt(blk, BLK_ZO_RESET, offset, len);
1861 if (ret < 0) {
1862 printf("zone reset failed: %s\n", strerror(-ret));
1863 }
1864 return ret;
1865}
1866
1867static const cmdinfo_t zone_reset_cmd = {
1868 .name = "zone_reset",
1869 .altname = "zrs",
1870 .cfunc = zone_reset_f,
1871 .argmin = 2,
1872 .argmax = 2,
1873 .args = "offset len",
1874 .oneline = "reset a zone write pointer in zone block device",
1875};
1876
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001877static int truncate_f(BlockBackend *blk, int argc, char **argv);
1878static const cmdinfo_t truncate_cmd = {
1879 .name = "truncate",
1880 .altname = "t",
1881 .cfunc = truncate_f,
1882 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1883 .argmin = 1,
1884 .argmax = 3,
1885 .args = "[-m prealloc_mode] off",
1886 .oneline = "truncates the current file at the given offset",
1887};
1888
Max Reitzb32d7a32018-05-09 21:42:59 +02001889static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001890{
Max Reitzed3d2ec2017-03-28 22:51:27 +02001891 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001892 int64_t offset;
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001893 int c, ret;
1894 PreallocMode prealloc = PREALLOC_MODE_OFF;
Kevin Wolf797ac582013-06-05 14:19:31 +02001895
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001896 while ((c = getopt(argc, argv, "m:")) != -1) {
1897 switch (c) {
1898 case 'm':
1899 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
1900 PREALLOC_MODE__MAX, NULL);
1901 if (prealloc == PREALLOC_MODE__MAX) {
1902 error_report("Invalid preallocation mode '%s'", optarg);
1903 return -EINVAL;
1904 }
1905 break;
1906 default:
1907 qemuio_command_usage(&truncate_cmd);
1908 return -EINVAL;
1909 }
1910 }
1911
1912 offset = cvtnum(argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001913 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001914 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001915 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001916 }
1917
Max Reitze8d04f92019-09-18 11:51:43 +02001918 /*
1919 * qemu-io is a debugging tool, so let us be strict here and pass
1920 * exact=true. It is better to err on the "emit more errors" side
1921 * than to be overly permissive.
1922 */
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001923 ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err);
Kevin Wolf797ac582013-06-05 14:19:31 +02001924 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001925 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02001926 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001927 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001928
1929 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001930}
1931
Max Reitzb32d7a32018-05-09 21:42:59 +02001932static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001933{
1934 int64_t size;
1935 char s1[64];
1936
Max Reitz4c7b7e92015-02-05 13:58:22 -05001937 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001938 if (size < 0) {
1939 printf("getlength: %s\n", strerror(-size));
Max Reitzb32d7a32018-05-09 21:42:59 +02001940 return size;
Kevin Wolf797ac582013-06-05 14:19:31 +02001941 }
1942
1943 cvtstr(size, s1, sizeof(s1));
1944 printf("%s\n", s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001945 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001946}
1947
1948
1949static const cmdinfo_t length_cmd = {
1950 .name = "length",
1951 .altname = "l",
1952 .cfunc = length_f,
1953 .oneline = "gets the length of the current file",
1954};
1955
1956
Max Reitzb32d7a32018-05-09 21:42:59 +02001957static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001958{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001959 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001960 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001961 ImageInfoSpecific *spec_info;
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001962 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001963 char s1[64], s2[64];
1964 int ret;
1965
1966 if (bs->drv && bs->drv->format_name) {
1967 printf("format name: %s\n", bs->drv->format_name);
1968 }
1969 if (bs->drv && bs->drv->protocol_name) {
1970 printf("format name: %s\n", bs->drv->protocol_name);
1971 }
1972
1973 ret = bdrv_get_info(bs, &bdi);
1974 if (ret) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001975 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001976 }
1977
1978 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1979 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1980
1981 printf("cluster size: %s\n", s1);
1982 printf("vm state offset: %s\n", s2);
1983
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001984 spec_info = bdrv_get_specific_info(bs, &local_err);
1985 if (local_err) {
1986 error_report_err(local_err);
1987 return -EIO;
1988 }
Max Reitza8d8ecb2013-10-09 10:46:17 +02001989 if (spec_info) {
Hanna Reitz37164702022-06-20 18:26:53 +02001990 bdrv_image_info_specific_dump(spec_info,
Hanna Reitz76c9e972022-06-20 18:27:00 +02001991 "Format specific information:\n",
1992 0);
Max Reitza8d8ecb2013-10-09 10:46:17 +02001993 qapi_free_ImageInfoSpecific(spec_info);
1994 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001995
1996 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001997}
1998
1999
2000
2001static const cmdinfo_t info_cmd = {
2002 .name = "info",
2003 .altname = "i",
2004 .cfunc = info_f,
2005 .oneline = "prints information about the current file",
2006};
2007
2008static void discard_help(void)
2009{
2010 printf(
2011"\n"
2012" discards a range of bytes from the given offset\n"
2013"\n"
2014" Example:\n"
2015" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
2016"\n"
2017" Discards a segment of the currently open file.\n"
2018" -C, -- report statistics in a machine parsable format\n"
2019" -q, -- quiet mode, do not show I/O statistics\n"
2020"\n");
2021}
2022
Max Reitzb32d7a32018-05-09 21:42:59 +02002023static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02002024
2025static const cmdinfo_t discard_cmd = {
2026 .name = "discard",
2027 .altname = "d",
2028 .cfunc = discard_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01002029 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02002030 .argmin = 2,
2031 .argmax = -1,
2032 .args = "[-Cq] off len",
2033 .oneline = "discards a number of bytes at a specified offset",
2034 .help = discard_help,
2035};
2036
Max Reitzb32d7a32018-05-09 21:42:59 +02002037static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002038{
Alex Bennée50290c02019-05-29 17:16:32 +01002039 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06002040 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02002041 int c, ret;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03002042 int64_t offset, bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +02002043
Eric Blakeb062ad82015-05-12 09:10:56 -06002044 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02002045 switch (c) {
2046 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06002047 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02002048 break;
2049 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06002050 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02002051 break;
2052 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02002053 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002054 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02002055 }
2056 }
2057
2058 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02002059 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002060 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02002061 }
2062
2063 offset = cvtnum(argv[optind]);
2064 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002065 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002066 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02002067 }
2068
2069 optind++;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03002070 bytes = cvtnum(argv[optind]);
2071 if (bytes < 0) {
2072 print_cvtnum_err(bytes, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002073 return bytes;
Alberto Garcia41ae31e2019-05-14 16:57:35 +03002074 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05002075 printf("length cannot exceed %"PRIu64", given %s\n",
Alberto Garcia41ae31e2019-05-14 16:57:35 +03002076 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002077 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02002078 }
2079
Alex Bennée50290c02019-05-29 17:16:32 +01002080 clock_gettime(CLOCK_MONOTONIC, &t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03002081 ret = blk_pdiscard(blk, offset, bytes);
Alex Bennée50290c02019-05-29 17:16:32 +01002082 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02002083
2084 if (ret < 0) {
2085 printf("discard failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002086 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002087 }
2088
2089 /* Finally, report back -- -C gives a parsable format */
2090 if (!qflag) {
2091 t2 = tsub(t2, t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03002092 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
Kevin Wolf797ac582013-06-05 14:19:31 +02002093 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002094
2095 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002096}
2097
Max Reitzb32d7a32018-05-09 21:42:59 +02002098static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002099{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002100 BlockDriverState *bs = blk_bs(blk);
Eric Blaked6a644b2017-07-07 07:44:57 -05002101 int64_t offset, start, remaining, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02002102 char s1[64];
Eric Blaked6a644b2017-07-07 07:44:57 -05002103 int ret;
2104 int64_t num, sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02002105
Eric Blaked6a644b2017-07-07 07:44:57 -05002106 start = offset = cvtnum(argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002107 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002108 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002109 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02002110 }
2111
2112 if (argc == 3) {
Eric Blake4401fdc2017-04-29 14:14:12 -05002113 count = cvtnum(argv[2]);
2114 if (count < 0) {
2115 print_cvtnum_err(count, argv[2]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002116 return count;
Kevin Wolf797ac582013-06-05 14:19:31 +02002117 }
2118 } else {
Eric Blake4401fdc2017-04-29 14:14:12 -05002119 count = BDRV_SECTOR_SIZE;
Kevin Wolf797ac582013-06-05 14:19:31 +02002120 }
2121
Eric Blaked6a644b2017-07-07 07:44:57 -05002122 remaining = count;
Kevin Wolf797ac582013-06-05 14:19:31 +02002123 sum_alloc = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002124 while (remaining) {
Eric Blaked6a644b2017-07-07 07:44:57 -05002125 ret = bdrv_is_allocated(bs, offset, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02002126 if (ret < 0) {
2127 printf("is_allocated failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002128 return ret;
Paolo Bonzinid6636402013-09-04 19:00:25 +02002129 }
Eric Blaked6a644b2017-07-07 07:44:57 -05002130 offset += num;
Kevin Wolf797ac582013-06-05 14:19:31 +02002131 remaining -= num;
2132 if (ret) {
2133 sum_alloc += num;
2134 }
2135 if (num == 0) {
Eric Blaked6a644b2017-07-07 07:44:57 -05002136 count -= remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02002137 remaining = 0;
2138 }
2139 }
2140
Eric Blaked6a644b2017-07-07 07:44:57 -05002141 cvtstr(start, s1, sizeof(s1));
Kevin Wolf797ac582013-06-05 14:19:31 +02002142
Eric Blake4401fdc2017-04-29 14:14:12 -05002143 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05002144 sum_alloc, count, s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02002145 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002146}
2147
2148static const cmdinfo_t alloc_cmd = {
2149 .name = "alloc",
2150 .altname = "a",
2151 .argmin = 1,
2152 .argmax = 2,
2153 .cfunc = alloc_f,
Eric Blake4401fdc2017-04-29 14:14:12 -05002154 .args = "offset [count]",
2155 .oneline = "checks if offset is allocated in the file",
Kevin Wolf797ac582013-06-05 14:19:31 +02002156};
2157
2158
Eric Blaked6a644b2017-07-07 07:44:57 -05002159static int map_is_allocated(BlockDriverState *bs, int64_t offset,
2160 int64_t bytes, int64_t *pnum)
Kevin Wolf797ac582013-06-05 14:19:31 +02002161{
Eric Blaked6a644b2017-07-07 07:44:57 -05002162 int64_t num;
Kevin Wolf797ac582013-06-05 14:19:31 +02002163 int ret, firstret;
2164
Eric Blake087f2fb2021-12-03 17:15:27 -06002165 ret = bdrv_is_allocated(bs, offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002166 if (ret < 0) {
2167 return ret;
2168 }
2169
2170 firstret = ret;
2171 *pnum = num;
2172
Eric Blaked6a644b2017-07-07 07:44:57 -05002173 while (bytes > 0 && ret == firstret) {
2174 offset += num;
2175 bytes -= num;
Kevin Wolf797ac582013-06-05 14:19:31 +02002176
Eric Blake087f2fb2021-12-03 17:15:27 -06002177 ret = bdrv_is_allocated(bs, offset, bytes, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02002178 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02002179 *pnum += num;
2180 } else {
2181 break;
2182 }
2183 }
2184
2185 return firstret;
2186}
2187
Max Reitzb32d7a32018-05-09 21:42:59 +02002188static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002189{
Eric Blaked6a644b2017-07-07 07:44:57 -05002190 int64_t offset, bytes;
Eric Blake6f3c90a2017-04-29 14:14:13 -05002191 char s1[64], s2[64];
Kevin Wolf797ac582013-06-05 14:19:31 +02002192 int64_t num;
2193 int ret;
2194 const char *retstr;
2195
2196 offset = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05002197 bytes = blk_getlength(blk);
2198 if (bytes < 0) {
2199 error_report("Failed to query image length: %s", strerror(-bytes));
Max Reitzb32d7a32018-05-09 21:42:59 +02002200 return bytes;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002201 }
2202
Eric Blaked6a644b2017-07-07 07:44:57 -05002203 while (bytes) {
2204 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002205 if (ret < 0) {
2206 error_report("Failed to get allocation status: %s", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002207 return ret;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002208 } else if (!num) {
2209 error_report("Unexpected end of image");
Max Reitzb32d7a32018-05-09 21:42:59 +02002210 return -EIO;
Kevin Wolf797ac582013-06-05 14:19:31 +02002211 }
2212
2213 retstr = ret ? " allocated" : "not allocated";
Eric Blaked6a644b2017-07-07 07:44:57 -05002214 cvtstr(num, s1, sizeof(s1));
2215 cvtstr(offset, s2, sizeof(s2));
Eric Blake6f3c90a2017-04-29 14:14:13 -05002216 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05002217 s1, num, retstr, s2, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02002218
2219 offset += num;
Eric Blaked6a644b2017-07-07 07:44:57 -05002220 bytes -= num;
2221 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002222
2223 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002224}
2225
2226static const cmdinfo_t map_cmd = {
2227 .name = "map",
2228 .argmin = 0,
2229 .argmax = 0,
2230 .cfunc = map_f,
2231 .args = "",
2232 .oneline = "prints the allocated areas of a file",
2233};
2234
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002235static void reopen_help(void)
2236{
2237 printf(
2238"\n"
2239" Changes the open options of an already opened image\n"
2240"\n"
2241" Example:\n"
2242" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2243"\n"
2244" -r, -- Reopen the image read-only\n"
Kevin Wolfea922032017-08-03 17:03:00 +02002245" -w, -- Reopen the image read-write\n"
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002246" -c, -- Change the cache mode to the given value\n"
2247" -o, -- Changes block driver options (cf. 'open' command)\n"
2248"\n");
2249}
2250
Max Reitzb32d7a32018-05-09 21:42:59 +02002251static int reopen_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002252
2253static QemuOptsList reopen_opts = {
2254 .name = "reopen",
2255 .merge_lists = true,
2256 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2257 .desc = {
2258 /* no elements => accept any params */
2259 { /* end of list */ }
2260 },
2261};
2262
2263static const cmdinfo_t reopen_cmd = {
2264 .name = "reopen",
2265 .argmin = 0,
2266 .argmax = -1,
2267 .cfunc = reopen_f,
Kevin Wolfea922032017-08-03 17:03:00 +02002268 .args = "[(-r|-w)] [-c cache] [-o options]",
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002269 .oneline = "reopens an image with new options",
2270 .help = reopen_help,
2271};
2272
Max Reitzb32d7a32018-05-09 21:42:59 +02002273static int reopen_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002274{
2275 BlockDriverState *bs = blk_bs(blk);
2276 QemuOpts *qopts;
2277 QDict *opts;
2278 int c;
2279 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002280 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolfea922032017-08-03 17:03:00 +02002281 bool has_rw_option = false;
Alberto Garciadc900c32018-11-12 16:00:42 +02002282 bool has_cache_option = false;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002283 Error *local_err = NULL;
2284
Kevin Wolfea922032017-08-03 17:03:00 +02002285 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002286 switch (c) {
2287 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002288 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002289 error_report("Invalid cache option: %s", optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +02002290 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002291 }
Alberto Garciadc900c32018-11-12 16:00:42 +02002292 has_cache_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002293 break;
2294 case 'o':
2295 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2296 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002297 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002298 }
2299 break;
2300 case 'r':
Kevin Wolfea922032017-08-03 17:03:00 +02002301 if (has_rw_option) {
2302 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002303 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002304 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002305 flags &= ~BDRV_O_RDWR;
Kevin Wolfea922032017-08-03 17:03:00 +02002306 has_rw_option = true;
2307 break;
2308 case 'w':
2309 if (has_rw_option) {
2310 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002311 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002312 }
2313 flags |= BDRV_O_RDWR;
2314 has_rw_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002315 break;
2316 default:
2317 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002318 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002319 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002320 }
2321 }
2322
2323 if (optind != argc) {
2324 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002325 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002326 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002327 }
2328
Alberto Garciaa8003ec2018-09-06 12:37:01 +03002329 if (!writethrough != blk_enable_write_cache(blk) &&
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002330 blk_get_attached_dev(blk))
2331 {
2332 error_report("Cannot change cache.writeback: Device attached");
2333 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002334 return -EBUSY;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002335 }
2336
Kevin Wolff3adefb2017-09-22 14:50:12 +02002337 if (!(flags & BDRV_O_RDWR)) {
2338 uint64_t orig_perm, orig_shared_perm;
2339
2340 bdrv_drain(bs);
2341
2342 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2343 blk_set_perm(blk,
2344 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2345 orig_shared_perm,
2346 &error_abort);
2347 }
2348
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002349 qopts = qemu_opts_find(&reopen_opts, NULL);
Alberto Garciadc900c32018-11-12 16:00:42 +02002350 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002351 qemu_opts_reset(&reopen_opts);
2352
Alberto Garciadc900c32018-11-12 16:00:42 +02002353 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2354 if (has_rw_option) {
2355 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2356 qobject_unref(opts);
2357 return -EINVAL;
2358 }
2359 } else {
2360 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2361 }
2362
2363 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2364 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2365 if (has_cache_option) {
2366 error_report("Cannot set both -c and the cache options");
2367 qobject_unref(opts);
2368 return -EINVAL;
2369 }
2370 } else {
2371 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2372 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2373 }
2374
Kevin Wolf6cf42ca2021-07-08 13:47:06 +02002375 bdrv_reopen(bs, opts, true, &local_err);
Kevin Wolf1a63a902017-12-06 20:24:44 +01002376
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002377 if (local_err) {
2378 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02002379 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002380 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002381
2382 blk_set_enable_write_cache(blk, !writethrough);
2383 return 0;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002384}
2385
Max Reitzb32d7a32018-05-09 21:42:59 +02002386static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002387{
2388 int ret;
2389
Max Reitz4c7b7e92015-02-05 13:58:22 -05002390 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002391 if (ret < 0) {
2392 printf("Could not set breakpoint: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002393 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002394 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002395
2396 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002397}
2398
Max Reitzb32d7a32018-05-09 21:42:59 +02002399static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002400{
2401 int ret;
2402
Max Reitz4c7b7e92015-02-05 13:58:22 -05002403 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002404 if (ret < 0) {
2405 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002406 return ret;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002407 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002408
2409 return 0;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002410}
2411
Kevin Wolf797ac582013-06-05 14:19:31 +02002412static const cmdinfo_t break_cmd = {
2413 .name = "break",
2414 .argmin = 2,
2415 .argmax = 2,
2416 .cfunc = break_f,
2417 .args = "event tag",
2418 .oneline = "sets a breakpoint on event and tags the stopped "
2419 "request as tag",
2420};
2421
Fam Zheng4cc70e92013-11-20 10:01:54 +08002422static const cmdinfo_t remove_break_cmd = {
2423 .name = "remove_break",
2424 .argmin = 1,
2425 .argmax = 1,
2426 .cfunc = remove_break_f,
2427 .args = "tag",
2428 .oneline = "remove a breakpoint by tag",
2429};
2430
Max Reitzb32d7a32018-05-09 21:42:59 +02002431static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002432{
2433 int ret;
2434
Max Reitz4c7b7e92015-02-05 13:58:22 -05002435 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002436 if (ret < 0) {
2437 printf("Could not resume request: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002438 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002439 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002440
2441 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002442}
2443
2444static const cmdinfo_t resume_cmd = {
2445 .name = "resume",
2446 .argmin = 1,
2447 .argmax = 1,
2448 .cfunc = resume_f,
2449 .args = "tag",
2450 .oneline = "resumes the request tagged as tag",
2451};
2452
Max Reitzb32d7a32018-05-09 21:42:59 +02002453static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002454{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002455 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2456 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002457 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002458 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002459}
2460
2461static const cmdinfo_t wait_break_cmd = {
2462 .name = "wait_break",
2463 .argmin = 1,
2464 .argmax = 1,
2465 .cfunc = wait_break_f,
2466 .args = "tag",
2467 .oneline = "waits for the suspension of a request",
2468};
2469
Max Reitzb32d7a32018-05-09 21:42:59 +02002470static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002471{
2472 abort();
2473}
2474
2475static const cmdinfo_t abort_cmd = {
2476 .name = "abort",
2477 .cfunc = abort_f,
2478 .flags = CMD_NOFILE_OK,
2479 .oneline = "simulate a program crash using abort(3)",
2480};
2481
Max Reitz0e82dc72014-12-08 10:48:10 +01002482static void sigraise_help(void)
2483{
2484 printf(
2485"\n"
2486" raises the given signal\n"
2487"\n"
2488" Example:\n"
2489" 'sigraise %i' - raises SIGTERM\n"
2490"\n"
2491" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2492" given to sigraise.\n"
2493"\n", SIGTERM);
2494}
2495
Max Reitzb32d7a32018-05-09 21:42:59 +02002496static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002497
2498static const cmdinfo_t sigraise_cmd = {
2499 .name = "sigraise",
2500 .cfunc = sigraise_f,
2501 .argmin = 1,
2502 .argmax = 1,
2503 .flags = CMD_NOFILE_OK,
2504 .args = "signal",
2505 .oneline = "raises a signal",
2506 .help = sigraise_help,
2507};
2508
Max Reitzb32d7a32018-05-09 21:42:59 +02002509static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002510{
John Snow9b0beaf2015-11-05 18:53:02 -05002511 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002512 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002513 print_cvtnum_err(sig, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002514 return sig;
John Snow9b0beaf2015-11-05 18:53:02 -05002515 } else if (sig > NSIG) {
2516 printf("signal argument '%s' is too large to be a valid signal\n",
2517 argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002518 return -EINVAL;
Max Reitz0e82dc72014-12-08 10:48:10 +01002519 }
2520
2521 /* Using raise() to kill this process does not necessarily flush all open
2522 * streams. At least stdout and stderr (although the latter should be
2523 * non-buffered anyway) should be flushed, though. */
2524 fflush(stdout);
2525 fflush(stderr);
2526
2527 raise(sig);
Max Reitzb32d7a32018-05-09 21:42:59 +02002528
2529 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002530}
2531
Kevin Wolfcd33d022014-01-15 15:39:10 +01002532static void sleep_cb(void *opaque)
2533{
2534 bool *expired = opaque;
2535 *expired = true;
2536}
2537
Max Reitzb32d7a32018-05-09 21:42:59 +02002538static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002539{
2540 char *endptr;
2541 long ms;
2542 struct QEMUTimer *timer;
2543 bool expired = false;
2544
2545 ms = strtol(argv[1], &endptr, 0);
2546 if (ms < 0 || *endptr != '\0') {
2547 printf("%s is not a valid number\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002548 return -EINVAL;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002549 }
2550
2551 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2552 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2553
2554 while (!expired) {
2555 main_loop_wait(false);
2556 }
2557
2558 timer_free(timer);
Max Reitzb32d7a32018-05-09 21:42:59 +02002559 return 0;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002560}
2561
2562static const cmdinfo_t sleep_cmd = {
2563 .name = "sleep",
2564 .argmin = 1,
2565 .argmax = 1,
2566 .cfunc = sleep_f,
2567 .flags = CMD_NOFILE_OK,
2568 .oneline = "waits for the given value in milliseconds",
2569};
2570
Kevin Wolff18a8342013-06-05 14:19:33 +02002571static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2572{
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002573 printf("%s ", cmd);
Kevin Wolff18a8342013-06-05 14:19:33 +02002574
2575 if (ct->args) {
2576 printf("%s ", ct->args);
2577 }
2578 printf("-- %s\n", ct->oneline);
2579}
2580
2581static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2582{
2583 help_oneline(cmd, ct);
2584 if (ct->help) {
2585 ct->help();
2586 }
2587}
2588
2589static void help_all(void)
2590{
2591 const cmdinfo_t *ct;
2592
2593 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2594 help_oneline(ct->name, ct);
2595 }
2596 printf("\nUse 'help commandname' for extended help.\n");
2597}
2598
Max Reitzb32d7a32018-05-09 21:42:59 +02002599static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002600{
2601 const cmdinfo_t *ct;
2602
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002603 if (argc < 2) {
Kevin Wolff18a8342013-06-05 14:19:33 +02002604 help_all();
Max Reitzb32d7a32018-05-09 21:42:59 +02002605 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002606 }
2607
2608 ct = find_command(argv[1]);
2609 if (ct == NULL) {
2610 printf("command %s not found\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002611 return -EINVAL;
Kevin Wolff18a8342013-06-05 14:19:33 +02002612 }
2613
2614 help_onecmd(argv[1], ct);
Max Reitzb32d7a32018-05-09 21:42:59 +02002615 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002616}
2617
2618static const cmdinfo_t help_cmd = {
2619 .name = "help",
2620 .altname = "?",
2621 .cfunc = help_f,
2622 .argmin = 0,
2623 .argmax = 1,
2624 .flags = CMD_FLAG_GLOBAL,
2625 .args = "[command]",
2626 .oneline = "help for one or all commands",
2627};
2628
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +03002629/*
2630 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2631 * context acquired if blk is NULL.
2632 */
Max Reitzb32d7a32018-05-09 21:42:59 +02002633int qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002634{
2635 char *input;
2636 const cmdinfo_t *ct;
2637 char **v;
2638 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +02002639 int ret = 0;
Kevin Wolfdd583292013-06-05 14:19:32 +02002640
2641 input = g_strdup(cmd);
2642 v = breakline(input, &c);
2643 if (c) {
2644 ct = find_command(v[0]);
2645 if (ct) {
Max Reitzb32d7a32018-05-09 21:42:59 +02002646 ret = command(blk, ct, c, v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002647 } else {
2648 fprintf(stderr, "command \"%s\" not found\n", v[0]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002649 ret = -EINVAL;
Kevin Wolfdd583292013-06-05 14:19:32 +02002650 }
2651 }
2652 g_free(input);
2653 g_free(v);
Max Reitzb32d7a32018-05-09 21:42:59 +02002654
2655 return ret;
Kevin Wolfdd583292013-06-05 14:19:32 +02002656}
2657
Kevin Wolf797ac582013-06-05 14:19:31 +02002658static void __attribute((constructor)) init_qemuio_commands(void)
2659{
2660 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002661 qemuio_add_command(&help_cmd);
2662 qemuio_add_command(&read_cmd);
2663 qemuio_add_command(&readv_cmd);
2664 qemuio_add_command(&write_cmd);
2665 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002666 qemuio_add_command(&aio_read_cmd);
2667 qemuio_add_command(&aio_write_cmd);
2668 qemuio_add_command(&aio_flush_cmd);
2669 qemuio_add_command(&flush_cmd);
Sam Li6d43eaa2023-05-08 12:55:28 +08002670 qemuio_add_command(&zone_report_cmd);
2671 qemuio_add_command(&zone_open_cmd);
2672 qemuio_add_command(&zone_close_cmd);
2673 qemuio_add_command(&zone_finish_cmd);
2674 qemuio_add_command(&zone_reset_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002675 qemuio_add_command(&truncate_cmd);
2676 qemuio_add_command(&length_cmd);
2677 qemuio_add_command(&info_cmd);
2678 qemuio_add_command(&discard_cmd);
2679 qemuio_add_command(&alloc_cmd);
2680 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002681 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002682 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002683 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002684 qemuio_add_command(&resume_cmd);
2685 qemuio_add_command(&wait_break_cmd);
2686 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002687 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002688 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002689}