blob: a061031615da76be732c6b82c67d1961358176dd [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
Max Reitz4c7b7e92015-02-05 13:58:22 -0500341static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
Kevin Wolf797ac582013-06-05 14:19:31 +0200342{
343 void *buf;
344
345 if (qemuio_misalign) {
346 len += MISALIGN_OFFSET;
347 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500348 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200349 memset(buf, pattern, len);
350 if (qemuio_misalign) {
351 buf += MISALIGN_OFFSET;
352 }
353 return buf;
354}
355
356static void qemu_io_free(void *p)
357{
358 if (qemuio_misalign) {
359 p -= MISALIGN_OFFSET;
360 }
361 qemu_vfree(p);
362}
363
Denis Plotnikov4d731512019-08-20 19:46:16 +0300364/*
365 * qemu_io_alloc_from_file()
366 *
367 * Allocates the buffer and populates it with the content of the given file
368 * up to @len bytes. If the file length is less than @len, then the buffer
369 * is populated with the file content cyclically.
370 *
371 * @blk - the block backend where the buffer content is going to be written to
372 * @len - the buffer length
373 * @file_name - the file to read the content from
374 *
375 * Returns: the buffer pointer on success
376 * NULL on error
377 */
378static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
379 const char *file_name)
380{
381 char *buf, *buf_origin;
382 FILE *f = fopen(file_name, "r");
383 int pattern_len;
384
385 if (!f) {
386 perror(file_name);
387 return NULL;
388 }
389
390 if (qemuio_misalign) {
391 len += MISALIGN_OFFSET;
392 }
393
394 buf_origin = buf = blk_blockalign(blk, len);
395
396 if (qemuio_misalign) {
397 buf_origin += MISALIGN_OFFSET;
398 buf += MISALIGN_OFFSET;
399 len -= MISALIGN_OFFSET;
400 }
401
402 pattern_len = fread(buf_origin, 1, len, f);
403
404 if (ferror(f)) {
405 perror(file_name);
406 goto error;
407 }
408
409 if (pattern_len == 0) {
410 fprintf(stderr, "%s: file is empty\n", file_name);
411 goto error;
412 }
413
414 fclose(f);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200415 f = NULL;
Denis Plotnikov4d731512019-08-20 19:46:16 +0300416
417 if (len > pattern_len) {
418 len -= pattern_len;
419 buf += pattern_len;
420
421 while (len > 0) {
422 size_t len_to_copy = MIN(pattern_len, len);
423
424 memcpy(buf, buf_origin, len_to_copy);
425
426 len -= len_to_copy;
427 buf += len_to_copy;
428 }
429 }
430
431 return buf_origin;
432
433error:
434 qemu_io_free(buf_origin);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200435 if (f) {
436 fclose(f);
437 }
Denis Plotnikov4d731512019-08-20 19:46:16 +0300438 return NULL;
439}
440
John Snow9b0beaf2015-11-05 18:53:02 -0500441static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200442{
John Snow9b0beaf2015-11-05 18:53:02 -0500443 uint64_t i;
444 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200445 const uint8_t *p;
446
447 for (i = 0, p = buffer; i < len; i += 16) {
448 const uint8_t *s = p;
449
450 printf("%08" PRIx64 ": ", offset + i);
451 for (j = 0; j < 16 && i + j < len; j++, p++) {
452 printf("%02x ", *p);
453 }
454 printf(" ");
455 for (j = 0; j < 16 && i + j < len; j++, s++) {
456 if (isalnum(*s)) {
457 printf("%c", *s);
458 } else {
459 printf(".");
460 }
461 }
462 printf("\n");
463 }
464}
465
Alex Bennée50290c02019-05-29 17:16:32 +0100466static void print_report(const char *op, struct timespec *t, int64_t offset,
Eric Blakedc388522016-05-07 21:16:42 -0600467 int64_t count, int64_t total, int cnt, bool Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200468{
469 char s1[64], s2[64], ts[64];
470
471 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
472 if (!Cflag) {
473 cvtstr((double)total, s1, sizeof(s1));
474 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500475 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200476 op, total, count, offset);
477 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
478 s1, cnt, ts, s2, tdiv((double)cnt, *t));
479 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500480 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200481 total, cnt, ts,
482 tdiv((double)total, *t),
483 tdiv((double)cnt, *t));
484 }
485}
486
487/*
488 * Parse multiple length statements for vectored I/O, and construct an I/O
489 * vector matching it.
490 */
491static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500492create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200493 int pattern)
494{
495 size_t *sizes = g_new0(size_t, nr_iov);
496 size_t count = 0;
497 void *buf = NULL;
498 void *p;
499 int i;
500
501 for (i = 0; i < nr_iov; i++) {
502 char *arg = argv[i];
503 int64_t len;
504
505 len = cvtnum(arg);
506 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500507 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200508 goto fail;
509 }
510
Alberto Garcia3026c462017-01-31 18:09:54 +0200511 if (len > BDRV_REQUEST_MAX_BYTES) {
512 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
513 (uint64_t)BDRV_REQUEST_MAX_BYTES);
514 goto fail;
515 }
516
517 if (count > BDRV_REQUEST_MAX_BYTES - len) {
518 printf("The total number of bytes exceed the maximum size %" PRIu64
519 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
Kevin Wolf797ac582013-06-05 14:19:31 +0200520 goto fail;
521 }
522
Kevin Wolf797ac582013-06-05 14:19:31 +0200523 sizes[i] = len;
524 count += len;
525 }
526
527 qemu_iovec_init(qiov, nr_iov);
528
Max Reitz4c7b7e92015-02-05 13:58:22 -0500529 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200530
531 for (i = 0; i < nr_iov; i++) {
532 qemu_iovec_add(qiov, p, sizes[i]);
533 p += sizes[i];
534 }
535
536fail:
537 g_free(sizes);
538 return buf;
539}
540
John Snow9b0beaf2015-11-05 18:53:02 -0500541static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300542 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200543{
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100544 int ret;
545
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300546 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500547 return -ERANGE;
548 }
549
Alberto Fariaa9262f52022-07-05 17:15:11 +0100550 ret = blk_pread(blk, offset, bytes, (uint8_t *)buf, 0);
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100551 if (ret < 0) {
552 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200553 }
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100554 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200555 return 1;
556}
557
John Snow9b0beaf2015-11-05 18:53:02 -0500558static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300559 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200560{
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100561 int ret;
562
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300563 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500564 return -ERANGE;
565 }
566
Alberto Fariaa9262f52022-07-05 17:15:11 +0100567 ret = blk_pwrite(blk, offset, bytes, (uint8_t *)buf, flags);
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100568 if (ret < 0) {
569 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200570 }
Alberto Fariabf5b16f2022-07-05 17:15:09 +0100571 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200572 return 1;
573}
574
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100575static int do_pwrite_zeroes(BlockBackend *blk, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300576 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200577{
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100578 int ret = blk_pwrite_zeroes(blk, offset, bytes,
579 flags | BDRV_REQ_ZERO_WRITE);
Kevin Wolf797ac582013-06-05 14:19:31 +0200580
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100581 if (ret < 0) {
582 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200583 }
Paolo Bonzini264dcbb2022-12-15 14:02:23 +0100584 *total = bytes;
585 return 1;
Kevin Wolf797ac582013-06-05 14:19:31 +0200586}
587
Max Reitz4c7b7e92015-02-05 13:58:22 -0500588static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300589 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200590{
591 int ret;
592
Alberto Garcia41ae31e2019-05-14 16:57:35 +0300593 if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500594 return -ERANGE;
595 }
596
Alberto Faria0cadf2c2022-07-05 17:15:18 +0100597 ret = blk_pwrite_compressed(blk, offset, bytes, buf);
Kevin Wolf797ac582013-06-05 14:19:31 +0200598 if (ret < 0) {
599 return ret;
600 }
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300601 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200602 return 1;
603}
604
Max Reitz4c7b7e92015-02-05 13:58:22 -0500605static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500606 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200607{
John Snow9b0beaf2015-11-05 18:53:02 -0500608 if (count > INT_MAX) {
609 return -ERANGE;
610 }
611
Max Reitz4c7b7e92015-02-05 13:58:22 -0500612 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200613 if (*total < 0) {
614 return *total;
615 }
616 return 1;
617}
618
Max Reitz4c7b7e92015-02-05 13:58:22 -0500619static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500620 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200621{
John Snow9b0beaf2015-11-05 18:53:02 -0500622 if (count > INT_MAX) {
623 return -ERANGE;
624 }
625
Max Reitz4c7b7e92015-02-05 13:58:22 -0500626 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200627 if (*total < 0) {
628 return *total;
629 }
630 return 1;
631}
632
633#define NOT_DONE 0x7fffffff
634static void aio_rw_done(void *opaque, int ret)
635{
636 *(int *)opaque = ret;
637}
638
Max Reitz4c7b7e92015-02-05 13:58:22 -0500639static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200640 int64_t offset, int *total)
641{
642 int async_ret = NOT_DONE;
643
Eric Blake7b3f9712016-05-06 10:26:44 -0600644 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200645 while (async_ret == NOT_DONE) {
646 main_loop_wait(false);
647 }
648
649 *total = qiov->size;
650 return async_ret < 0 ? async_ret : 1;
651}
652
Max Reitz4c7b7e92015-02-05 13:58:22 -0500653static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600654 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200655{
656 int async_ret = NOT_DONE;
657
Eric Blake770e0e02016-05-07 21:16:44 -0600658 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200659 while (async_ret == NOT_DONE) {
660 main_loop_wait(false);
661 }
662
663 *total = qiov->size;
664 return async_ret < 0 ? async_ret : 1;
665}
666
Kevin Wolf797ac582013-06-05 14:19:31 +0200667static void read_help(void)
668{
669 printf(
670"\n"
671" reads a range of bytes from the given offset\n"
672"\n"
673" Example:\n"
674" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
675"\n"
676" Reads a segment of the currently open file, optionally dumping it to the\n"
677" standard output stream (with -v option) for subsequent inspection.\n"
678" -b, -- read from the VM state rather than the virtual disk\n"
679" -C, -- report statistics in a machine parsable format\n"
680" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600681" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200682" -P, -- use a pattern to verify read data\n"
683" -q, -- quiet mode, do not show I/O statistics\n"
684" -s, -- start offset for pattern verification (only with -P)\n"
685" -v, -- dump buffer to standard output\n"
686"\n");
687}
688
Max Reitzb32d7a32018-05-09 21:42:59 +0200689static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200690
691static const cmdinfo_t read_cmd = {
692 .name = "read",
693 .altname = "r",
694 .cfunc = read_f,
695 .argmin = 2,
696 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600697 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200698 .oneline = "reads a number of bytes at a specified offset",
699 .help = read_help,
700};
701
Max Reitzb32d7a32018-05-09 21:42:59 +0200702static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200703{
Alex Bennée50290c02019-05-29 17:16:32 +0100704 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600705 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600706 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200707 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200708 char *buf;
709 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500710 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200711 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500712 int64_t total = 0;
713 int pattern = 0;
714 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200715
Eric Blakeb062ad82015-05-12 09:10:56 -0600716 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200717 switch (c) {
718 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600719 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200720 break;
721 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600722 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200723 break;
724 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600725 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200726 pattern_count = cvtnum(optarg);
727 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500728 print_cvtnum_err(pattern_count, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200729 return pattern_count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200730 }
731 break;
732 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600733 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200734 break;
735 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600736 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200737 pattern = parse_pattern(optarg);
738 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200739 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200740 }
741 break;
742 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600743 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200744 break;
745 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600746 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200747 pattern_offset = cvtnum(optarg);
748 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500749 print_cvtnum_err(pattern_offset, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200750 return pattern_offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200751 }
752 break;
753 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600754 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200755 break;
756 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200757 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200758 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200759 }
760 }
761
762 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200763 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200764 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200765 }
766
Kevin Wolf797ac582013-06-05 14:19:31 +0200767 offset = cvtnum(argv[optind]);
768 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500769 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200770 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200771 }
772
773 optind++;
774 count = cvtnum(argv[optind]);
775 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500776 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200777 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +0200778 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500779 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200780 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200781 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200782 }
783
784 if (!Pflag && (lflag || sflag)) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200785 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200786 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200787 }
788
789 if (!lflag) {
790 pattern_count = count - pattern_offset;
791 }
792
793 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
794 printf("pattern verification range exceeds end of read data\n");
Max Reitzb32d7a32018-05-09 21:42:59 +0200795 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200796 }
797
Eric Blake093ea232016-05-07 21:16:43 -0600798 if (bflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -0500799 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
800 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200801 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +0200802 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200803 }
Eric Blake1bce6b42017-04-29 14:14:11 -0500804 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
805 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200806 count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200807 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200808 }
809 }
810
Max Reitz4c7b7e92015-02-05 13:58:22 -0500811 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200812
Alex Bennée50290c02019-05-29 17:16:32 +0100813 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -0600814 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200815 ret = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200816 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +0200817 ret = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200818 }
Alex Bennée50290c02019-05-29 17:16:32 +0100819 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200820
Max Reitzb32d7a32018-05-09 21:42:59 +0200821 if (ret < 0) {
822 printf("read failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200823 goto out;
824 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200825 cnt = ret;
826
827 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200828
829 if (Pflag) {
830 void *cmp_buf = g_malloc(pattern_count);
831 memset(cmp_buf, pattern, pattern_count);
832 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
833 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500834 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200835 offset + pattern_offset, pattern_count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200836 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200837 }
838 g_free(cmp_buf);
839 }
840
841 if (qflag) {
842 goto out;
843 }
844
845 if (vflag) {
846 dump_buffer(buf, offset, count);
847 }
848
849 /* Finally, report back -- -C gives a parsable format */
850 t2 = tsub(t2, t1);
851 print_report("read", &t2, offset, count, total, cnt, Cflag);
852
853out:
854 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +0200855 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200856}
857
858static void readv_help(void)
859{
860 printf(
861"\n"
862" reads a range of bytes from the given offset into multiple buffers\n"
863"\n"
864" Example:\n"
865" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
866"\n"
867" Reads a segment of the currently open file, optionally dumping it to the\n"
868" standard output stream (with -v option) for subsequent inspection.\n"
869" Uses multiple iovec buffers if more than one byte range is specified.\n"
870" -C, -- report statistics in a machine parsable format\n"
871" -P, -- use a pattern to verify read data\n"
872" -v, -- dump buffer to standard output\n"
873" -q, -- quiet mode, do not show I/O statistics\n"
874"\n");
875}
876
Max Reitzb32d7a32018-05-09 21:42:59 +0200877static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200878
879static const cmdinfo_t readv_cmd = {
880 .name = "readv",
881 .cfunc = readv_f,
882 .argmin = 2,
883 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600884 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200885 .oneline = "reads a number of bytes at a specified offset",
886 .help = readv_help,
887};
888
Max Reitzb32d7a32018-05-09 21:42:59 +0200889static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200890{
Alex Bennée50290c02019-05-29 17:16:32 +0100891 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600892 bool Cflag = false, qflag = false, vflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200893 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200894 char *buf;
895 int64_t offset;
896 /* Some compilers get confused and warn if this is not initialized. */
897 int total = 0;
898 int nr_iov;
899 QEMUIOVector qiov;
900 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600901 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200902
Eric Blakeb062ad82015-05-12 09:10:56 -0600903 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200904 switch (c) {
905 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600906 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200907 break;
908 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600909 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200910 pattern = parse_pattern(optarg);
911 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200912 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200913 }
914 break;
915 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600916 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200917 break;
918 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600919 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200920 break;
921 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200922 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200923 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200924 }
925 }
926
927 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200928 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200929 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200930 }
931
932
933 offset = cvtnum(argv[optind]);
934 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500935 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200936 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200937 }
938 optind++;
939
Kevin Wolf797ac582013-06-05 14:19:31 +0200940 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500941 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200942 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200943 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200944 }
945
Alex Bennée50290c02019-05-29 17:16:32 +0100946 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +0200947 ret = do_aio_readv(blk, &qiov, offset, &total);
Alex Bennée50290c02019-05-29 17:16:32 +0100948 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200949
Max Reitzb32d7a32018-05-09 21:42:59 +0200950 if (ret < 0) {
951 printf("readv failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200952 goto out;
953 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200954 cnt = ret;
955
956 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200957
958 if (Pflag) {
959 void *cmp_buf = g_malloc(qiov.size);
960 memset(cmp_buf, pattern, qiov.size);
961 if (memcmp(buf, cmp_buf, qiov.size)) {
962 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +0200963 PRId64 ", %zu bytes\n", offset, qiov.size);
Max Reitzb32d7a32018-05-09 21:42:59 +0200964 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200965 }
966 g_free(cmp_buf);
967 }
968
969 if (qflag) {
970 goto out;
971 }
972
973 if (vflag) {
974 dump_buffer(buf, offset, qiov.size);
975 }
976
977 /* Finally, report back -- -C gives a parsable format */
978 t2 = tsub(t2, t1);
979 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
980
981out:
982 qemu_iovec_destroy(&qiov);
983 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +0200984 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200985}
986
987static void write_help(void)
988{
989 printf(
990"\n"
991" writes a range of bytes from the given offset\n"
992"\n"
993" Example:\n"
994" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
995"\n"
996" Writes into a segment of the currently open file, using a buffer\n"
997" filled with a set pattern (0xcdcdcdcd).\n"
998" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500999" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001000" -f, -- use Force Unit Access semantics\n"
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001001" -n, -- with -z, don't allow slow fallback\n"
Eric Blake093ea232016-05-07 21:16:43 -06001002" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001003" -P, -- use different pattern to fill file\n"
Denis Plotnikov4d731512019-08-20 19:46:16 +03001004" -s, -- use a pattern file to fill the write buffer\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001005" -C, -- report statistics in a machine parsable format\n"
1006" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001007" -u, -- with -z, allow unmapping\n"
Paolo Bonzini264dcbb2022-12-15 14:02:23 +01001008" -z, -- write zeroes using blk_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001009"\n");
1010}
1011
Max Reitzb32d7a32018-05-09 21:42:59 +02001012static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001013
1014static const cmdinfo_t write_cmd = {
1015 .name = "write",
1016 .altname = "w",
1017 .cfunc = write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001018 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001019 .argmin = 2,
1020 .argmax = -1,
Denis Plotnikov4d731512019-08-20 19:46:16 +03001021 .args = "[-bcCfnquz] [-P pattern | -s source_file] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +02001022 .oneline = "writes a number of bytes at a specified offset",
1023 .help = write_help,
1024};
1025
Max Reitzb32d7a32018-05-09 21:42:59 +02001026static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001027{
Alex Bennée50290c02019-05-29 17:16:32 +01001028 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -06001029 bool Cflag = false, qflag = false, bflag = false;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001030 bool Pflag = false, zflag = false, cflag = false, sflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001031 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001032 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001033 char *buf = NULL;
1034 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -05001035 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001036 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -05001037 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001038 int pattern = 0xcd;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001039 const char *file_name = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001040
Denis Plotnikov4d731512019-08-20 19:46:16 +03001041 while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001042 switch (c) {
1043 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -06001044 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001045 break;
1046 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -06001047 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001048 break;
1049 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001050 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001051 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001052 case 'f':
1053 flags |= BDRV_REQ_FUA;
1054 break;
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001055 case 'n':
1056 flags |= BDRV_REQ_NO_FALLBACK;
1057 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001058 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -06001059 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +02001060 break;
1061 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001062 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001063 pattern = parse_pattern(optarg);
1064 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001065 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001066 }
1067 break;
1068 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001069 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001070 break;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001071 case 's':
1072 sflag = true;
1073 file_name = optarg;
1074 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001075 case 'u':
1076 flags |= BDRV_REQ_MAY_UNMAP;
1077 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001078 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001079 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001080 break;
1081 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001082 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001083 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001084 }
1085 }
1086
1087 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001088 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001089 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001090 }
1091
Eric Blake093ea232016-05-07 21:16:43 -06001092 if (bflag && zflag) {
1093 printf("-b and -z cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001094 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001095 }
1096
Eric Blake770e0e02016-05-07 21:16:44 -06001097 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1098 printf("-f and -b or -c cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001099 return -EINVAL;
Eric Blake770e0e02016-05-07 21:16:44 -06001100 }
1101
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001102 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1103 printf("-n requires -z to be specified\n");
1104 return -EINVAL;
1105 }
1106
Eric Blakec2e001c2016-05-07 21:16:45 -06001107 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1108 printf("-u requires -z to be specified\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001109 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001110 }
1111
Denis Plotnikov4d731512019-08-20 19:46:16 +03001112 if (zflag + Pflag + sflag > 1) {
1113 printf("Only one of -z, -P, and -s "
1114 "can be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001115 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001116 }
1117
1118 offset = cvtnum(argv[optind]);
1119 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001120 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001121 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001122 }
1123
1124 optind++;
1125 count = cvtnum(argv[optind]);
1126 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001127 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001128 return count;
Eric Blake395aecd2021-12-03 17:15:28 -06001129 } else if (count > BDRV_REQUEST_MAX_BYTES &&
1130 !(flags & BDRV_REQ_NO_FALLBACK)) {
1131 printf("length cannot exceed %" PRIu64 " without -n, given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001132 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001133 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001134 }
1135
Eric Blake093ea232016-05-07 21:16:43 -06001136 if (bflag || cflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -05001137 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1138 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001139 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +02001140 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001141 }
1142
Eric Blake1bce6b42017-04-29 14:14:11 -05001143 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1144 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001145 count);
Max Reitzb32d7a32018-05-09 21:42:59 +02001146 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001147 }
1148 }
1149
1150 if (!zflag) {
Denis Plotnikov4d731512019-08-20 19:46:16 +03001151 if (sflag) {
1152 buf = qemu_io_alloc_from_file(blk, count, file_name);
1153 if (!buf) {
1154 return -EINVAL;
1155 }
1156 } else {
1157 buf = qemu_io_alloc(blk, count, pattern);
1158 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001159 }
1160
Alex Bennée50290c02019-05-29 17:16:32 +01001161 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -06001162 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001163 ret = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001164 } else if (zflag) {
Paolo Bonzini264dcbb2022-12-15 14:02:23 +01001165 ret = do_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001166 } else if (cflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001167 ret = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001168 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +02001169 ret = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001170 }
Alex Bennée50290c02019-05-29 17:16:32 +01001171 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001172
Max Reitzb32d7a32018-05-09 21:42:59 +02001173 if (ret < 0) {
1174 printf("write failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001175 goto out;
1176 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001177 cnt = ret;
1178
1179 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001180
1181 if (qflag) {
1182 goto out;
1183 }
1184
1185 /* Finally, report back -- -C gives a parsable format */
1186 t2 = tsub(t2, t1);
1187 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1188
1189out:
1190 if (!zflag) {
1191 qemu_io_free(buf);
1192 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001193 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001194}
1195
1196static void
1197writev_help(void)
1198{
1199 printf(
1200"\n"
1201" writes a range of bytes from the given offset source from multiple buffers\n"
1202"\n"
1203" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001204" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001205"\n"
1206" Writes into a segment of the currently open file, using a buffer\n"
1207" filled with a set pattern (0xcdcdcdcd).\n"
1208" -P, -- use different pattern to fill file\n"
1209" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001210" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001211" -q, -- quiet mode, do not show I/O statistics\n"
1212"\n");
1213}
1214
Max Reitzb32d7a32018-05-09 21:42:59 +02001215static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001216
1217static const cmdinfo_t writev_cmd = {
1218 .name = "writev",
1219 .cfunc = writev_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001220 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001221 .argmin = 2,
1222 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001223 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001224 .oneline = "writes a number of bytes at a specified offset",
1225 .help = writev_help,
1226};
1227
Max Reitzb32d7a32018-05-09 21:42:59 +02001228static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001229{
Alex Bennée50290c02019-05-29 17:16:32 +01001230 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001231 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001232 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001233 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001234 char *buf;
1235 int64_t offset;
1236 /* Some compilers get confused and warn if this is not initialized. */
1237 int total = 0;
1238 int nr_iov;
1239 int pattern = 0xcd;
1240 QEMUIOVector qiov;
1241
Eric Blake4ca1d342016-05-16 10:43:01 -06001242 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001243 switch (c) {
1244 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001245 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001246 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001247 case 'f':
1248 flags |= BDRV_REQ_FUA;
1249 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001250 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001251 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001252 break;
1253 case 'P':
1254 pattern = parse_pattern(optarg);
1255 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001256 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001257 }
1258 break;
1259 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001260 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001261 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001262 }
1263 }
1264
1265 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001266 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001267 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001268 }
1269
1270 offset = cvtnum(argv[optind]);
1271 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001272 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001273 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001274 }
1275 optind++;
1276
Kevin Wolf797ac582013-06-05 14:19:31 +02001277 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001278 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001279 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001280 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001281 }
1282
Alex Bennée50290c02019-05-29 17:16:32 +01001283 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001284 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
Alex Bennée50290c02019-05-29 17:16:32 +01001285 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001286
Max Reitzb32d7a32018-05-09 21:42:59 +02001287 if (ret < 0) {
1288 printf("writev failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001289 goto out;
1290 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001291 cnt = ret;
1292
1293 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001294
1295 if (qflag) {
1296 goto out;
1297 }
1298
1299 /* Finally, report back -- -C gives a parsable format */
1300 t2 = tsub(t2, t1);
1301 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1302out:
1303 qemu_iovec_destroy(&qiov);
1304 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001305 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001306}
1307
Kevin Wolf797ac582013-06-05 14:19:31 +02001308struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001309 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001310 QEMUIOVector qiov;
1311 int64_t offset;
1312 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001313 bool qflag;
1314 bool vflag;
1315 bool Cflag;
1316 bool Pflag;
1317 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001318 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001319 int pattern;
Alex Bennée50290c02019-05-29 17:16:32 +01001320 struct timespec t1;
Kevin Wolf797ac582013-06-05 14:19:31 +02001321};
1322
1323static void aio_write_done(void *opaque, int ret)
1324{
1325 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001326 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001327
Alex Bennée50290c02019-05-29 17:16:32 +01001328 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001329
1330
1331 if (ret < 0) {
1332 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001333 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001334 goto out;
1335 }
1336
Max Reitz4c7b7e92015-02-05 13:58:22 -05001337 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001338
Kevin Wolf797ac582013-06-05 14:19:31 +02001339 if (ctx->qflag) {
1340 goto out;
1341 }
1342
1343 /* Finally, report back -- -C gives a parsable format */
1344 t2 = tsub(t2, ctx->t1);
1345 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1346 ctx->qiov.size, 1, ctx->Cflag);
1347out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001348 if (!ctx->zflag) {
1349 qemu_io_free(ctx->buf);
1350 qemu_iovec_destroy(&ctx->qiov);
1351 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001352 g_free(ctx);
1353}
1354
1355static void aio_read_done(void *opaque, int ret)
1356{
1357 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001358 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001359
Alex Bennée50290c02019-05-29 17:16:32 +01001360 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001361
1362 if (ret < 0) {
1363 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001364 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001365 goto out;
1366 }
1367
1368 if (ctx->Pflag) {
1369 void *cmp_buf = g_malloc(ctx->qiov.size);
1370
1371 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1372 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1373 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +02001374 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
Kevin Wolf797ac582013-06-05 14:19:31 +02001375 }
1376 g_free(cmp_buf);
1377 }
1378
Max Reitz4c7b7e92015-02-05 13:58:22 -05001379 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001380
Kevin Wolf797ac582013-06-05 14:19:31 +02001381 if (ctx->qflag) {
1382 goto out;
1383 }
1384
1385 if (ctx->vflag) {
1386 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1387 }
1388
1389 /* Finally, report back -- -C gives a parsable format */
1390 t2 = tsub(t2, ctx->t1);
1391 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1392 ctx->qiov.size, 1, ctx->Cflag);
1393out:
1394 qemu_io_free(ctx->buf);
1395 qemu_iovec_destroy(&ctx->qiov);
1396 g_free(ctx);
1397}
1398
1399static void aio_read_help(void)
1400{
1401 printf(
1402"\n"
1403" asynchronously reads a range of bytes from the given offset\n"
1404"\n"
1405" Example:\n"
1406" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1407"\n"
1408" Reads a segment of the currently open file, optionally dumping it to the\n"
1409" standard output stream (with -v option) for subsequent inspection.\n"
1410" The read is performed asynchronously and the aio_flush command must be\n"
1411" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001412" Note that due to its asynchronous nature, this command will be\n"
1413" considered successful once the request is submitted, independently\n"
1414" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001415" -C, -- report statistics in a machine parsable format\n"
1416" -P, -- use a pattern to verify read data\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001417" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001418" -v, -- dump buffer to standard output\n"
1419" -q, -- quiet mode, do not show I/O statistics\n"
1420"\n");
1421}
1422
Max Reitzb32d7a32018-05-09 21:42:59 +02001423static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001424
1425static const cmdinfo_t aio_read_cmd = {
1426 .name = "aio_read",
1427 .cfunc = aio_read_f,
1428 .argmin = 2,
1429 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001430 .args = "[-Ciqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001431 .oneline = "asynchronously reads a number of bytes",
1432 .help = aio_read_help,
1433};
1434
Max Reitzb32d7a32018-05-09 21:42:59 +02001435static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001436{
1437 int nr_iov, c;
1438 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1439
Max Reitz4c7b7e92015-02-05 13:58:22 -05001440 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001441 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001442 switch (c) {
1443 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001444 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001445 break;
1446 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001447 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001448 ctx->pattern = parse_pattern(optarg);
1449 if (ctx->pattern < 0) {
1450 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001451 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001452 }
1453 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001454 case 'i':
1455 printf("injecting invalid read request\n");
1456 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1457 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001458 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001459 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001460 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001461 break;
1462 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001463 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001464 break;
1465 default:
1466 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001467 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001468 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001469 }
1470 }
1471
1472 if (optind > argc - 2) {
1473 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001474 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001475 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001476 }
1477
1478 ctx->offset = cvtnum(argv[optind]);
1479 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001480 int ret = ctx->offset;
1481 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001482 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001483 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001484 }
1485 optind++;
1486
Kevin Wolf797ac582013-06-05 14:19:31 +02001487 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001488 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001489 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001490 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001491 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001492 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001493 }
1494
Alex Bennée50290c02019-05-29 17:16:32 +01001495 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001496 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1497 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001498 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001499 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001500}
1501
1502static void aio_write_help(void)
1503{
1504 printf(
1505"\n"
1506" asynchronously writes a range of bytes from the given offset source\n"
1507" from multiple buffers\n"
1508"\n"
1509" Example:\n"
1510" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1511"\n"
1512" Writes into a segment of the currently open file, using a buffer\n"
1513" filled with a set pattern (0xcdcdcdcd).\n"
1514" The write is performed asynchronously and the aio_flush command must be\n"
1515" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001516" Note that due to its asynchronous nature, this command will be\n"
1517" considered successful once the request is submitted, independently\n"
1518" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001519" -P, -- use different pattern to fill file\n"
1520" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001521" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001522" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001523" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001524" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001525" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001526"\n");
1527}
1528
Max Reitzb32d7a32018-05-09 21:42:59 +02001529static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001530
1531static const cmdinfo_t aio_write_cmd = {
1532 .name = "aio_write",
1533 .cfunc = aio_write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001534 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001535 .argmin = 2,
1536 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001537 .args = "[-Cfiquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001538 .oneline = "asynchronously writes a number of bytes",
1539 .help = aio_write_help,
1540};
1541
Max Reitzb32d7a32018-05-09 21:42:59 +02001542static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001543{
1544 int nr_iov, c;
1545 int pattern = 0xcd;
1546 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001547 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001548
Max Reitz4c7b7e92015-02-05 13:58:22 -05001549 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001550 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001551 switch (c) {
1552 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001553 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001554 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001555 case 'f':
1556 flags |= BDRV_REQ_FUA;
1557 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001558 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001559 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001560 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001561 case 'u':
1562 flags |= BDRV_REQ_MAY_UNMAP;
1563 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001564 case 'P':
1565 pattern = parse_pattern(optarg);
1566 if (pattern < 0) {
1567 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001568 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001569 }
1570 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001571 case 'i':
1572 printf("injecting invalid write request\n");
1573 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1574 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001575 return 0;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001576 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001577 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001578 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001579 default:
1580 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001581 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001582 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001583 }
1584 }
1585
1586 if (optind > argc - 2) {
1587 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001588 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001589 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001590 }
1591
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001592 if (ctx->zflag && optind != argc - 2) {
1593 printf("-z supports only a single length parameter\n");
1594 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001595 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001596 }
1597
Eric Blakec2e001c2016-05-07 21:16:45 -06001598 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1599 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001600 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001601 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001602 }
1603
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001604 if (ctx->zflag && ctx->Pflag) {
1605 printf("-z and -P cannot be specified at the same time\n");
1606 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001607 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001608 }
1609
Kevin Wolf797ac582013-06-05 14:19:31 +02001610 ctx->offset = cvtnum(argv[optind]);
1611 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001612 int ret = ctx->offset;
1613 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001614 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001615 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001616 }
1617 optind++;
1618
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001619 if (ctx->zflag) {
1620 int64_t count = cvtnum(argv[optind]);
1621 if (count < 0) {
1622 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001623 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001624 return count;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001625 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001626
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001627 ctx->qiov.size = count;
Eric Blaked004bd52016-05-24 16:25:20 -06001628 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1629 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001630 } else {
1631 nr_iov = argc - optind;
1632 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1633 pattern);
1634 if (ctx->buf == NULL) {
1635 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1636 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001637 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001638 }
1639
Alex Bennée50290c02019-05-29 17:16:32 +01001640 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001641 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1642 BLOCK_ACCT_WRITE);
1643
Eric Blake770e0e02016-05-07 21:16:44 -06001644 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1645 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001646 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001647
1648 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001649}
1650
Max Reitzb32d7a32018-05-09 21:42:59 +02001651static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001652{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001653 BlockAcctCookie cookie;
1654 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001655 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001656 block_acct_done(blk_get_stats(blk), &cookie);
Max Reitzb32d7a32018-05-09 21:42:59 +02001657 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001658}
1659
1660static const cmdinfo_t aio_flush_cmd = {
1661 .name = "aio_flush",
1662 .cfunc = aio_flush_f,
1663 .oneline = "completes all outstanding aio requests"
1664};
1665
Max Reitzb32d7a32018-05-09 21:42:59 +02001666static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001667{
Max Reitzb32d7a32018-05-09 21:42:59 +02001668 return blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001669}
1670
1671static const cmdinfo_t flush_cmd = {
1672 .name = "flush",
1673 .altname = "f",
1674 .cfunc = flush_f,
1675 .oneline = "flush all in-core file state to disk",
1676};
1677
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001678static int truncate_f(BlockBackend *blk, int argc, char **argv);
1679static const cmdinfo_t truncate_cmd = {
1680 .name = "truncate",
1681 .altname = "t",
1682 .cfunc = truncate_f,
1683 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1684 .argmin = 1,
1685 .argmax = 3,
1686 .args = "[-m prealloc_mode] off",
1687 .oneline = "truncates the current file at the given offset",
1688};
1689
Max Reitzb32d7a32018-05-09 21:42:59 +02001690static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001691{
Max Reitzed3d2ec2017-03-28 22:51:27 +02001692 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001693 int64_t offset;
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001694 int c, ret;
1695 PreallocMode prealloc = PREALLOC_MODE_OFF;
Kevin Wolf797ac582013-06-05 14:19:31 +02001696
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001697 while ((c = getopt(argc, argv, "m:")) != -1) {
1698 switch (c) {
1699 case 'm':
1700 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
1701 PREALLOC_MODE__MAX, NULL);
1702 if (prealloc == PREALLOC_MODE__MAX) {
1703 error_report("Invalid preallocation mode '%s'", optarg);
1704 return -EINVAL;
1705 }
1706 break;
1707 default:
1708 qemuio_command_usage(&truncate_cmd);
1709 return -EINVAL;
1710 }
1711 }
1712
1713 offset = cvtnum(argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001714 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001715 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001716 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001717 }
1718
Max Reitze8d04f92019-09-18 11:51:43 +02001719 /*
1720 * qemu-io is a debugging tool, so let us be strict here and pass
1721 * exact=true. It is better to err on the "emit more errors" side
1722 * than to be overly permissive.
1723 */
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001724 ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err);
Kevin Wolf797ac582013-06-05 14:19:31 +02001725 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001726 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02001727 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001728 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001729
1730 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001731}
1732
Max Reitzb32d7a32018-05-09 21:42:59 +02001733static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001734{
1735 int64_t size;
1736 char s1[64];
1737
Max Reitz4c7b7e92015-02-05 13:58:22 -05001738 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001739 if (size < 0) {
1740 printf("getlength: %s\n", strerror(-size));
Max Reitzb32d7a32018-05-09 21:42:59 +02001741 return size;
Kevin Wolf797ac582013-06-05 14:19:31 +02001742 }
1743
1744 cvtstr(size, s1, sizeof(s1));
1745 printf("%s\n", s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001746 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001747}
1748
1749
1750static const cmdinfo_t length_cmd = {
1751 .name = "length",
1752 .altname = "l",
1753 .cfunc = length_f,
1754 .oneline = "gets the length of the current file",
1755};
1756
1757
Max Reitzb32d7a32018-05-09 21:42:59 +02001758static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001759{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001760 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001761 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001762 ImageInfoSpecific *spec_info;
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001763 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001764 char s1[64], s2[64];
1765 int ret;
1766
1767 if (bs->drv && bs->drv->format_name) {
1768 printf("format name: %s\n", bs->drv->format_name);
1769 }
1770 if (bs->drv && bs->drv->protocol_name) {
1771 printf("format name: %s\n", bs->drv->protocol_name);
1772 }
1773
1774 ret = bdrv_get_info(bs, &bdi);
1775 if (ret) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001776 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001777 }
1778
1779 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1780 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1781
1782 printf("cluster size: %s\n", s1);
1783 printf("vm state offset: %s\n", s2);
1784
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001785 spec_info = bdrv_get_specific_info(bs, &local_err);
1786 if (local_err) {
1787 error_report_err(local_err);
1788 return -EIO;
1789 }
Max Reitza8d8ecb2013-10-09 10:46:17 +02001790 if (spec_info) {
Hanna Reitz37164702022-06-20 18:26:53 +02001791 bdrv_image_info_specific_dump(spec_info,
Hanna Reitz76c9e972022-06-20 18:27:00 +02001792 "Format specific information:\n",
1793 0);
Max Reitza8d8ecb2013-10-09 10:46:17 +02001794 qapi_free_ImageInfoSpecific(spec_info);
1795 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001796
1797 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001798}
1799
1800
1801
1802static const cmdinfo_t info_cmd = {
1803 .name = "info",
1804 .altname = "i",
1805 .cfunc = info_f,
1806 .oneline = "prints information about the current file",
1807};
1808
1809static void discard_help(void)
1810{
1811 printf(
1812"\n"
1813" discards a range of bytes from the given offset\n"
1814"\n"
1815" Example:\n"
1816" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1817"\n"
1818" Discards a segment of the currently open file.\n"
1819" -C, -- report statistics in a machine parsable format\n"
1820" -q, -- quiet mode, do not show I/O statistics\n"
1821"\n");
1822}
1823
Max Reitzb32d7a32018-05-09 21:42:59 +02001824static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001825
1826static const cmdinfo_t discard_cmd = {
1827 .name = "discard",
1828 .altname = "d",
1829 .cfunc = discard_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001830 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001831 .argmin = 2,
1832 .argmax = -1,
1833 .args = "[-Cq] off len",
1834 .oneline = "discards a number of bytes at a specified offset",
1835 .help = discard_help,
1836};
1837
Max Reitzb32d7a32018-05-09 21:42:59 +02001838static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001839{
Alex Bennée50290c02019-05-29 17:16:32 +01001840 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001841 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001842 int c, ret;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001843 int64_t offset, bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +02001844
Eric Blakeb062ad82015-05-12 09:10:56 -06001845 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001846 switch (c) {
1847 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001848 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001849 break;
1850 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001851 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001852 break;
1853 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001854 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001855 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001856 }
1857 }
1858
1859 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001860 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001861 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001862 }
1863
1864 offset = cvtnum(argv[optind]);
1865 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001866 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001867 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001868 }
1869
1870 optind++;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001871 bytes = cvtnum(argv[optind]);
1872 if (bytes < 0) {
1873 print_cvtnum_err(bytes, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001874 return bytes;
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001875 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001876 printf("length cannot exceed %"PRIu64", given %s\n",
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001877 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001878 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001879 }
1880
Alex Bennée50290c02019-05-29 17:16:32 +01001881 clock_gettime(CLOCK_MONOTONIC, &t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001882 ret = blk_pdiscard(blk, offset, bytes);
Alex Bennée50290c02019-05-29 17:16:32 +01001883 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001884
1885 if (ret < 0) {
1886 printf("discard failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001887 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001888 }
1889
1890 /* Finally, report back -- -C gives a parsable format */
1891 if (!qflag) {
1892 t2 = tsub(t2, t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001893 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
Kevin Wolf797ac582013-06-05 14:19:31 +02001894 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001895
1896 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001897}
1898
Max Reitzb32d7a32018-05-09 21:42:59 +02001899static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001900{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001901 BlockDriverState *bs = blk_bs(blk);
Eric Blaked6a644b2017-07-07 07:44:57 -05001902 int64_t offset, start, remaining, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001903 char s1[64];
Eric Blaked6a644b2017-07-07 07:44:57 -05001904 int ret;
1905 int64_t num, sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001906
Eric Blaked6a644b2017-07-07 07:44:57 -05001907 start = offset = cvtnum(argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001908 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001909 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001910 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001911 }
1912
1913 if (argc == 3) {
Eric Blake4401fdc2017-04-29 14:14:12 -05001914 count = cvtnum(argv[2]);
1915 if (count < 0) {
1916 print_cvtnum_err(count, argv[2]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001917 return count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001918 }
1919 } else {
Eric Blake4401fdc2017-04-29 14:14:12 -05001920 count = BDRV_SECTOR_SIZE;
Kevin Wolf797ac582013-06-05 14:19:31 +02001921 }
1922
Eric Blaked6a644b2017-07-07 07:44:57 -05001923 remaining = count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001924 sum_alloc = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001925 while (remaining) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001926 ret = bdrv_is_allocated(bs, offset, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001927 if (ret < 0) {
1928 printf("is_allocated failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001929 return ret;
Paolo Bonzinid6636402013-09-04 19:00:25 +02001930 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001931 offset += num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001932 remaining -= num;
1933 if (ret) {
1934 sum_alloc += num;
1935 }
1936 if (num == 0) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001937 count -= remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001938 remaining = 0;
1939 }
1940 }
1941
Eric Blaked6a644b2017-07-07 07:44:57 -05001942 cvtstr(start, s1, sizeof(s1));
Kevin Wolf797ac582013-06-05 14:19:31 +02001943
Eric Blake4401fdc2017-04-29 14:14:12 -05001944 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001945 sum_alloc, count, s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001946 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001947}
1948
1949static const cmdinfo_t alloc_cmd = {
1950 .name = "alloc",
1951 .altname = "a",
1952 .argmin = 1,
1953 .argmax = 2,
1954 .cfunc = alloc_f,
Eric Blake4401fdc2017-04-29 14:14:12 -05001955 .args = "offset [count]",
1956 .oneline = "checks if offset is allocated in the file",
Kevin Wolf797ac582013-06-05 14:19:31 +02001957};
1958
1959
Eric Blaked6a644b2017-07-07 07:44:57 -05001960static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1961 int64_t bytes, int64_t *pnum)
Kevin Wolf797ac582013-06-05 14:19:31 +02001962{
Eric Blaked6a644b2017-07-07 07:44:57 -05001963 int64_t num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001964 int ret, firstret;
1965
Eric Blake087f2fb2021-12-03 17:15:27 -06001966 ret = bdrv_is_allocated(bs, offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001967 if (ret < 0) {
1968 return ret;
1969 }
1970
1971 firstret = ret;
1972 *pnum = num;
1973
Eric Blaked6a644b2017-07-07 07:44:57 -05001974 while (bytes > 0 && ret == firstret) {
1975 offset += num;
1976 bytes -= num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001977
Eric Blake087f2fb2021-12-03 17:15:27 -06001978 ret = bdrv_is_allocated(bs, offset, bytes, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02001979 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001980 *pnum += num;
1981 } else {
1982 break;
1983 }
1984 }
1985
1986 return firstret;
1987}
1988
Max Reitzb32d7a32018-05-09 21:42:59 +02001989static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001990{
Eric Blaked6a644b2017-07-07 07:44:57 -05001991 int64_t offset, bytes;
Eric Blake6f3c90a2017-04-29 14:14:13 -05001992 char s1[64], s2[64];
Kevin Wolf797ac582013-06-05 14:19:31 +02001993 int64_t num;
1994 int ret;
1995 const char *retstr;
1996
1997 offset = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05001998 bytes = blk_getlength(blk);
1999 if (bytes < 0) {
2000 error_report("Failed to query image length: %s", strerror(-bytes));
Max Reitzb32d7a32018-05-09 21:42:59 +02002001 return bytes;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002002 }
2003
Eric Blaked6a644b2017-07-07 07:44:57 -05002004 while (bytes) {
2005 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002006 if (ret < 0) {
2007 error_report("Failed to get allocation status: %s", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002008 return ret;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002009 } else if (!num) {
2010 error_report("Unexpected end of image");
Max Reitzb32d7a32018-05-09 21:42:59 +02002011 return -EIO;
Kevin Wolf797ac582013-06-05 14:19:31 +02002012 }
2013
2014 retstr = ret ? " allocated" : "not allocated";
Eric Blaked6a644b2017-07-07 07:44:57 -05002015 cvtstr(num, s1, sizeof(s1));
2016 cvtstr(offset, s2, sizeof(s2));
Eric Blake6f3c90a2017-04-29 14:14:13 -05002017 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05002018 s1, num, retstr, s2, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02002019
2020 offset += num;
Eric Blaked6a644b2017-07-07 07:44:57 -05002021 bytes -= num;
2022 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002023
2024 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002025}
2026
2027static const cmdinfo_t map_cmd = {
2028 .name = "map",
2029 .argmin = 0,
2030 .argmax = 0,
2031 .cfunc = map_f,
2032 .args = "",
2033 .oneline = "prints the allocated areas of a file",
2034};
2035
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002036static void reopen_help(void)
2037{
2038 printf(
2039"\n"
2040" Changes the open options of an already opened image\n"
2041"\n"
2042" Example:\n"
2043" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2044"\n"
2045" -r, -- Reopen the image read-only\n"
Kevin Wolfea922032017-08-03 17:03:00 +02002046" -w, -- Reopen the image read-write\n"
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002047" -c, -- Change the cache mode to the given value\n"
2048" -o, -- Changes block driver options (cf. 'open' command)\n"
2049"\n");
2050}
2051
Max Reitzb32d7a32018-05-09 21:42:59 +02002052static int reopen_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002053
2054static QemuOptsList reopen_opts = {
2055 .name = "reopen",
2056 .merge_lists = true,
2057 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2058 .desc = {
2059 /* no elements => accept any params */
2060 { /* end of list */ }
2061 },
2062};
2063
2064static const cmdinfo_t reopen_cmd = {
2065 .name = "reopen",
2066 .argmin = 0,
2067 .argmax = -1,
2068 .cfunc = reopen_f,
Kevin Wolfea922032017-08-03 17:03:00 +02002069 .args = "[(-r|-w)] [-c cache] [-o options]",
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002070 .oneline = "reopens an image with new options",
2071 .help = reopen_help,
2072};
2073
Max Reitzb32d7a32018-05-09 21:42:59 +02002074static int reopen_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002075{
2076 BlockDriverState *bs = blk_bs(blk);
2077 QemuOpts *qopts;
2078 QDict *opts;
2079 int c;
2080 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002081 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolfea922032017-08-03 17:03:00 +02002082 bool has_rw_option = false;
Alberto Garciadc900c32018-11-12 16:00:42 +02002083 bool has_cache_option = false;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002084 Error *local_err = NULL;
2085
Kevin Wolfea922032017-08-03 17:03:00 +02002086 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002087 switch (c) {
2088 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002089 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002090 error_report("Invalid cache option: %s", optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +02002091 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002092 }
Alberto Garciadc900c32018-11-12 16:00:42 +02002093 has_cache_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002094 break;
2095 case 'o':
2096 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2097 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002098 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002099 }
2100 break;
2101 case 'r':
Kevin Wolfea922032017-08-03 17:03:00 +02002102 if (has_rw_option) {
2103 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002104 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002105 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002106 flags &= ~BDRV_O_RDWR;
Kevin Wolfea922032017-08-03 17:03:00 +02002107 has_rw_option = true;
2108 break;
2109 case 'w':
2110 if (has_rw_option) {
2111 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002112 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002113 }
2114 flags |= BDRV_O_RDWR;
2115 has_rw_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002116 break;
2117 default:
2118 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002119 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002120 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002121 }
2122 }
2123
2124 if (optind != argc) {
2125 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002126 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002127 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002128 }
2129
Alberto Garciaa8003ec2018-09-06 12:37:01 +03002130 if (!writethrough != blk_enable_write_cache(blk) &&
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002131 blk_get_attached_dev(blk))
2132 {
2133 error_report("Cannot change cache.writeback: Device attached");
2134 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002135 return -EBUSY;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002136 }
2137
Kevin Wolff3adefb2017-09-22 14:50:12 +02002138 if (!(flags & BDRV_O_RDWR)) {
2139 uint64_t orig_perm, orig_shared_perm;
2140
2141 bdrv_drain(bs);
2142
2143 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2144 blk_set_perm(blk,
2145 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2146 orig_shared_perm,
2147 &error_abort);
2148 }
2149
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002150 qopts = qemu_opts_find(&reopen_opts, NULL);
Alberto Garciadc900c32018-11-12 16:00:42 +02002151 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002152 qemu_opts_reset(&reopen_opts);
2153
Alberto Garciadc900c32018-11-12 16:00:42 +02002154 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2155 if (has_rw_option) {
2156 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2157 qobject_unref(opts);
2158 return -EINVAL;
2159 }
2160 } else {
2161 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2162 }
2163
2164 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2165 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2166 if (has_cache_option) {
2167 error_report("Cannot set both -c and the cache options");
2168 qobject_unref(opts);
2169 return -EINVAL;
2170 }
2171 } else {
2172 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2173 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2174 }
2175
Kevin Wolf6cf42ca2021-07-08 13:47:06 +02002176 bdrv_reopen(bs, opts, true, &local_err);
Kevin Wolf1a63a902017-12-06 20:24:44 +01002177
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002178 if (local_err) {
2179 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02002180 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002181 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002182
2183 blk_set_enable_write_cache(blk, !writethrough);
2184 return 0;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002185}
2186
Max Reitzb32d7a32018-05-09 21:42:59 +02002187static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002188{
2189 int ret;
2190
Max Reitz4c7b7e92015-02-05 13:58:22 -05002191 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002192 if (ret < 0) {
2193 printf("Could not set breakpoint: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002194 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002195 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002196
2197 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002198}
2199
Max Reitzb32d7a32018-05-09 21:42:59 +02002200static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002201{
2202 int ret;
2203
Max Reitz4c7b7e92015-02-05 13:58:22 -05002204 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002205 if (ret < 0) {
2206 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002207 return ret;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002208 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002209
2210 return 0;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002211}
2212
Kevin Wolf797ac582013-06-05 14:19:31 +02002213static const cmdinfo_t break_cmd = {
2214 .name = "break",
2215 .argmin = 2,
2216 .argmax = 2,
2217 .cfunc = break_f,
2218 .args = "event tag",
2219 .oneline = "sets a breakpoint on event and tags the stopped "
2220 "request as tag",
2221};
2222
Fam Zheng4cc70e92013-11-20 10:01:54 +08002223static const cmdinfo_t remove_break_cmd = {
2224 .name = "remove_break",
2225 .argmin = 1,
2226 .argmax = 1,
2227 .cfunc = remove_break_f,
2228 .args = "tag",
2229 .oneline = "remove a breakpoint by tag",
2230};
2231
Max Reitzb32d7a32018-05-09 21:42:59 +02002232static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002233{
2234 int ret;
2235
Max Reitz4c7b7e92015-02-05 13:58:22 -05002236 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002237 if (ret < 0) {
2238 printf("Could not resume request: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002239 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002240 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002241
2242 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002243}
2244
2245static const cmdinfo_t resume_cmd = {
2246 .name = "resume",
2247 .argmin = 1,
2248 .argmax = 1,
2249 .cfunc = resume_f,
2250 .args = "tag",
2251 .oneline = "resumes the request tagged as tag",
2252};
2253
Max Reitzb32d7a32018-05-09 21:42:59 +02002254static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002255{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002256 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2257 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002258 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002259 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002260}
2261
2262static const cmdinfo_t wait_break_cmd = {
2263 .name = "wait_break",
2264 .argmin = 1,
2265 .argmax = 1,
2266 .cfunc = wait_break_f,
2267 .args = "tag",
2268 .oneline = "waits for the suspension of a request",
2269};
2270
Max Reitzb32d7a32018-05-09 21:42:59 +02002271static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002272{
2273 abort();
2274}
2275
2276static const cmdinfo_t abort_cmd = {
2277 .name = "abort",
2278 .cfunc = abort_f,
2279 .flags = CMD_NOFILE_OK,
2280 .oneline = "simulate a program crash using abort(3)",
2281};
2282
Max Reitz0e82dc72014-12-08 10:48:10 +01002283static void sigraise_help(void)
2284{
2285 printf(
2286"\n"
2287" raises the given signal\n"
2288"\n"
2289" Example:\n"
2290" 'sigraise %i' - raises SIGTERM\n"
2291"\n"
2292" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2293" given to sigraise.\n"
2294"\n", SIGTERM);
2295}
2296
Max Reitzb32d7a32018-05-09 21:42:59 +02002297static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002298
2299static const cmdinfo_t sigraise_cmd = {
2300 .name = "sigraise",
2301 .cfunc = sigraise_f,
2302 .argmin = 1,
2303 .argmax = 1,
2304 .flags = CMD_NOFILE_OK,
2305 .args = "signal",
2306 .oneline = "raises a signal",
2307 .help = sigraise_help,
2308};
2309
Max Reitzb32d7a32018-05-09 21:42:59 +02002310static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002311{
John Snow9b0beaf2015-11-05 18:53:02 -05002312 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002313 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002314 print_cvtnum_err(sig, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002315 return sig;
John Snow9b0beaf2015-11-05 18:53:02 -05002316 } else if (sig > NSIG) {
2317 printf("signal argument '%s' is too large to be a valid signal\n",
2318 argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002319 return -EINVAL;
Max Reitz0e82dc72014-12-08 10:48:10 +01002320 }
2321
2322 /* Using raise() to kill this process does not necessarily flush all open
2323 * streams. At least stdout and stderr (although the latter should be
2324 * non-buffered anyway) should be flushed, though. */
2325 fflush(stdout);
2326 fflush(stderr);
2327
2328 raise(sig);
Max Reitzb32d7a32018-05-09 21:42:59 +02002329
2330 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002331}
2332
Kevin Wolfcd33d022014-01-15 15:39:10 +01002333static void sleep_cb(void *opaque)
2334{
2335 bool *expired = opaque;
2336 *expired = true;
2337}
2338
Max Reitzb32d7a32018-05-09 21:42:59 +02002339static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002340{
2341 char *endptr;
2342 long ms;
2343 struct QEMUTimer *timer;
2344 bool expired = false;
2345
2346 ms = strtol(argv[1], &endptr, 0);
2347 if (ms < 0 || *endptr != '\0') {
2348 printf("%s is not a valid number\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002349 return -EINVAL;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002350 }
2351
2352 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2353 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2354
2355 while (!expired) {
2356 main_loop_wait(false);
2357 }
2358
2359 timer_free(timer);
Max Reitzb32d7a32018-05-09 21:42:59 +02002360 return 0;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002361}
2362
2363static const cmdinfo_t sleep_cmd = {
2364 .name = "sleep",
2365 .argmin = 1,
2366 .argmax = 1,
2367 .cfunc = sleep_f,
2368 .flags = CMD_NOFILE_OK,
2369 .oneline = "waits for the given value in milliseconds",
2370};
2371
Kevin Wolff18a8342013-06-05 14:19:33 +02002372static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2373{
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002374 printf("%s ", cmd);
Kevin Wolff18a8342013-06-05 14:19:33 +02002375
2376 if (ct->args) {
2377 printf("%s ", ct->args);
2378 }
2379 printf("-- %s\n", ct->oneline);
2380}
2381
2382static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2383{
2384 help_oneline(cmd, ct);
2385 if (ct->help) {
2386 ct->help();
2387 }
2388}
2389
2390static void help_all(void)
2391{
2392 const cmdinfo_t *ct;
2393
2394 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2395 help_oneline(ct->name, ct);
2396 }
2397 printf("\nUse 'help commandname' for extended help.\n");
2398}
2399
Max Reitzb32d7a32018-05-09 21:42:59 +02002400static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002401{
2402 const cmdinfo_t *ct;
2403
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002404 if (argc < 2) {
Kevin Wolff18a8342013-06-05 14:19:33 +02002405 help_all();
Max Reitzb32d7a32018-05-09 21:42:59 +02002406 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002407 }
2408
2409 ct = find_command(argv[1]);
2410 if (ct == NULL) {
2411 printf("command %s not found\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002412 return -EINVAL;
Kevin Wolff18a8342013-06-05 14:19:33 +02002413 }
2414
2415 help_onecmd(argv[1], ct);
Max Reitzb32d7a32018-05-09 21:42:59 +02002416 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002417}
2418
2419static const cmdinfo_t help_cmd = {
2420 .name = "help",
2421 .altname = "?",
2422 .cfunc = help_f,
2423 .argmin = 0,
2424 .argmax = 1,
2425 .flags = CMD_FLAG_GLOBAL,
2426 .args = "[command]",
2427 .oneline = "help for one or all commands",
2428};
2429
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +03002430/*
2431 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2432 * context acquired if blk is NULL.
2433 */
Max Reitzb32d7a32018-05-09 21:42:59 +02002434int qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002435{
2436 char *input;
2437 const cmdinfo_t *ct;
2438 char **v;
2439 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +02002440 int ret = 0;
Kevin Wolfdd583292013-06-05 14:19:32 +02002441
2442 input = g_strdup(cmd);
2443 v = breakline(input, &c);
2444 if (c) {
2445 ct = find_command(v[0]);
2446 if (ct) {
Max Reitzb32d7a32018-05-09 21:42:59 +02002447 ret = command(blk, ct, c, v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002448 } else {
2449 fprintf(stderr, "command \"%s\" not found\n", v[0]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002450 ret = -EINVAL;
Kevin Wolfdd583292013-06-05 14:19:32 +02002451 }
2452 }
2453 g_free(input);
2454 g_free(v);
Max Reitzb32d7a32018-05-09 21:42:59 +02002455
2456 return ret;
Kevin Wolfdd583292013-06-05 14:19:32 +02002457}
2458
Kevin Wolf797ac582013-06-05 14:19:31 +02002459static void __attribute((constructor)) init_qemuio_commands(void)
2460{
2461 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002462 qemuio_add_command(&help_cmd);
2463 qemuio_add_command(&read_cmd);
2464 qemuio_add_command(&readv_cmd);
2465 qemuio_add_command(&write_cmd);
2466 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002467 qemuio_add_command(&aio_read_cmd);
2468 qemuio_add_command(&aio_write_cmd);
2469 qemuio_add_command(&aio_flush_cmd);
2470 qemuio_add_command(&flush_cmd);
2471 qemuio_add_command(&truncate_cmd);
2472 qemuio_add_command(&length_cmd);
2473 qemuio_add_command(&info_cmd);
2474 qemuio_add_command(&discard_cmd);
2475 qemuio_add_command(&alloc_cmd);
2476 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002477 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002478 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002479 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002480 qemuio_add_command(&resume_cmd);
2481 qemuio_add_command(&wait_break_cmd);
2482 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002483 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002484 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002485}