blob: 633b46cdb250287fb6972cf9cfcaf0c0feb56e83 [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{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300544 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500545 return -ERANGE;
546 }
547
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300548 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200549 if (*total < 0) {
550 return *total;
551 }
552 return 1;
553}
554
John Snow9b0beaf2015-11-05 18:53:02 -0500555static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300556 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200557{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300558 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500559 return -ERANGE;
560 }
561
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300562 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200563 if (*total < 0) {
564 return *total;
565 }
566 return 1;
567}
568
569typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500570 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200571 int64_t offset;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300572 int64_t bytes;
John Snow9b0beaf2015-11-05 18:53:02 -0500573 int64_t *total;
Eric Blake770e0e02016-05-07 21:16:44 -0600574 int flags;
Kevin Wolf797ac582013-06-05 14:19:31 +0200575 int ret;
576 bool done;
577} CoWriteZeroes;
578
Eric Blaked004bd52016-05-24 16:25:20 -0600579static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
Kevin Wolf797ac582013-06-05 14:19:31 +0200580{
581 CoWriteZeroes *data = opaque;
582
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300583 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
Eric Blaked004bd52016-05-24 16:25:20 -0600584 data->flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200585 data->done = true;
586 if (data->ret < 0) {
587 *data->total = data->ret;
588 return;
589 }
590
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300591 *data->total = data->bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200592}
593
Eric Blaked004bd52016-05-24 16:25:20 -0600594static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300595 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200596{
597 Coroutine *co;
598 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500599 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200600 .offset = offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300601 .bytes = bytes,
Kevin Wolf797ac582013-06-05 14:19:31 +0200602 .total = total,
Eric Blake770e0e02016-05-07 21:16:44 -0600603 .flags = flags,
Kevin Wolf797ac582013-06-05 14:19:31 +0200604 .done = false,
605 };
606
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300607 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500608 return -ERANGE;
609 }
610
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200611 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
Fam Zheng324ec3e2017-04-10 20:16:18 +0800612 bdrv_coroutine_enter(blk_bs(blk), co);
Kevin Wolf797ac582013-06-05 14:19:31 +0200613 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500614 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200615 }
616 if (data.ret < 0) {
617 return data.ret;
618 } else {
619 return 1;
620 }
621}
622
Max Reitz4c7b7e92015-02-05 13:58:22 -0500623static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300624 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200625{
626 int ret;
627
Alberto Garcia41ae31e2019-05-14 16:57:35 +0300628 if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500629 return -ERANGE;
630 }
631
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300632 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200633 if (ret < 0) {
634 return ret;
635 }
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300636 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200637 return 1;
638}
639
Max Reitz4c7b7e92015-02-05 13:58:22 -0500640static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500641 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200642{
John Snow9b0beaf2015-11-05 18:53:02 -0500643 if (count > INT_MAX) {
644 return -ERANGE;
645 }
646
Max Reitz4c7b7e92015-02-05 13:58:22 -0500647 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200648 if (*total < 0) {
649 return *total;
650 }
651 return 1;
652}
653
Max Reitz4c7b7e92015-02-05 13:58:22 -0500654static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500655 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200656{
John Snow9b0beaf2015-11-05 18:53:02 -0500657 if (count > INT_MAX) {
658 return -ERANGE;
659 }
660
Max Reitz4c7b7e92015-02-05 13:58:22 -0500661 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200662 if (*total < 0) {
663 return *total;
664 }
665 return 1;
666}
667
668#define NOT_DONE 0x7fffffff
669static void aio_rw_done(void *opaque, int ret)
670{
671 *(int *)opaque = ret;
672}
673
Max Reitz4c7b7e92015-02-05 13:58:22 -0500674static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200675 int64_t offset, int *total)
676{
677 int async_ret = NOT_DONE;
678
Eric Blake7b3f9712016-05-06 10:26:44 -0600679 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200680 while (async_ret == NOT_DONE) {
681 main_loop_wait(false);
682 }
683
684 *total = qiov->size;
685 return async_ret < 0 ? async_ret : 1;
686}
687
Max Reitz4c7b7e92015-02-05 13:58:22 -0500688static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600689 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200690{
691 int async_ret = NOT_DONE;
692
Eric Blake770e0e02016-05-07 21:16:44 -0600693 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200694 while (async_ret == NOT_DONE) {
695 main_loop_wait(false);
696 }
697
698 *total = qiov->size;
699 return async_ret < 0 ? async_ret : 1;
700}
701
Kevin Wolf797ac582013-06-05 14:19:31 +0200702static void read_help(void)
703{
704 printf(
705"\n"
706" reads a range of bytes from the given offset\n"
707"\n"
708" Example:\n"
709" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
710"\n"
711" Reads a segment of the currently open file, optionally dumping it to the\n"
712" standard output stream (with -v option) for subsequent inspection.\n"
713" -b, -- read from the VM state rather than the virtual disk\n"
714" -C, -- report statistics in a machine parsable format\n"
715" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600716" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200717" -P, -- use a pattern to verify read data\n"
718" -q, -- quiet mode, do not show I/O statistics\n"
719" -s, -- start offset for pattern verification (only with -P)\n"
720" -v, -- dump buffer to standard output\n"
721"\n");
722}
723
Max Reitzb32d7a32018-05-09 21:42:59 +0200724static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200725
726static const cmdinfo_t read_cmd = {
727 .name = "read",
728 .altname = "r",
729 .cfunc = read_f,
730 .argmin = 2,
731 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600732 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200733 .oneline = "reads a number of bytes at a specified offset",
734 .help = read_help,
735};
736
Max Reitzb32d7a32018-05-09 21:42:59 +0200737static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200738{
Alex Bennée50290c02019-05-29 17:16:32 +0100739 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600740 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600741 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200742 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200743 char *buf;
744 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500745 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200746 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500747 int64_t total = 0;
748 int pattern = 0;
749 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200750
Eric Blakeb062ad82015-05-12 09:10:56 -0600751 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200752 switch (c) {
753 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600754 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200755 break;
756 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600757 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200758 break;
759 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600760 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200761 pattern_count = cvtnum(optarg);
762 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500763 print_cvtnum_err(pattern_count, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200764 return pattern_count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200765 }
766 break;
767 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600768 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200769 break;
770 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600771 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200772 pattern = parse_pattern(optarg);
773 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200774 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200775 }
776 break;
777 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600778 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200779 break;
780 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600781 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200782 pattern_offset = cvtnum(optarg);
783 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500784 print_cvtnum_err(pattern_offset, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200785 return pattern_offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200786 }
787 break;
788 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600789 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200790 break;
791 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200792 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200793 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200794 }
795 }
796
797 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200798 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200799 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200800 }
801
Kevin Wolf797ac582013-06-05 14:19:31 +0200802 offset = cvtnum(argv[optind]);
803 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500804 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200805 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200806 }
807
808 optind++;
809 count = cvtnum(argv[optind]);
810 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500811 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200812 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +0200813 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500814 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200815 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200816 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200817 }
818
819 if (!Pflag && (lflag || sflag)) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200820 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200821 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200822 }
823
824 if (!lflag) {
825 pattern_count = count - pattern_offset;
826 }
827
828 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
829 printf("pattern verification range exceeds end of read data\n");
Max Reitzb32d7a32018-05-09 21:42:59 +0200830 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200831 }
832
Eric Blake093ea232016-05-07 21:16:43 -0600833 if (bflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -0500834 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
835 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200836 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +0200837 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200838 }
Eric Blake1bce6b42017-04-29 14:14:11 -0500839 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
840 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200841 count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200842 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200843 }
844 }
845
Max Reitz4c7b7e92015-02-05 13:58:22 -0500846 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200847
Alex Bennée50290c02019-05-29 17:16:32 +0100848 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -0600849 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200850 ret = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200851 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +0200852 ret = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200853 }
Alex Bennée50290c02019-05-29 17:16:32 +0100854 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200855
Max Reitzb32d7a32018-05-09 21:42:59 +0200856 if (ret < 0) {
857 printf("read failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200858 goto out;
859 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200860 cnt = ret;
861
862 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200863
864 if (Pflag) {
865 void *cmp_buf = g_malloc(pattern_count);
866 memset(cmp_buf, pattern, pattern_count);
867 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
868 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500869 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200870 offset + pattern_offset, pattern_count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200871 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200872 }
873 g_free(cmp_buf);
874 }
875
876 if (qflag) {
877 goto out;
878 }
879
880 if (vflag) {
881 dump_buffer(buf, offset, count);
882 }
883
884 /* Finally, report back -- -C gives a parsable format */
885 t2 = tsub(t2, t1);
886 print_report("read", &t2, offset, count, total, cnt, Cflag);
887
888out:
889 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +0200890 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200891}
892
893static void readv_help(void)
894{
895 printf(
896"\n"
897" reads a range of bytes from the given offset into multiple buffers\n"
898"\n"
899" Example:\n"
900" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
901"\n"
902" Reads a segment of the currently open file, optionally dumping it to the\n"
903" standard output stream (with -v option) for subsequent inspection.\n"
904" Uses multiple iovec buffers if more than one byte range is specified.\n"
905" -C, -- report statistics in a machine parsable format\n"
906" -P, -- use a pattern to verify read data\n"
907" -v, -- dump buffer to standard output\n"
908" -q, -- quiet mode, do not show I/O statistics\n"
909"\n");
910}
911
Max Reitzb32d7a32018-05-09 21:42:59 +0200912static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200913
914static const cmdinfo_t readv_cmd = {
915 .name = "readv",
916 .cfunc = readv_f,
917 .argmin = 2,
918 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600919 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200920 .oneline = "reads a number of bytes at a specified offset",
921 .help = readv_help,
922};
923
Max Reitzb32d7a32018-05-09 21:42:59 +0200924static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200925{
Alex Bennée50290c02019-05-29 17:16:32 +0100926 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600927 bool Cflag = false, qflag = false, vflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200928 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200929 char *buf;
930 int64_t offset;
931 /* Some compilers get confused and warn if this is not initialized. */
932 int total = 0;
933 int nr_iov;
934 QEMUIOVector qiov;
935 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600936 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200937
Eric Blakeb062ad82015-05-12 09:10:56 -0600938 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200939 switch (c) {
940 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600941 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200942 break;
943 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600944 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200945 pattern = parse_pattern(optarg);
946 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200947 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200948 }
949 break;
950 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600951 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200952 break;
953 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600954 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200955 break;
956 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200957 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200958 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200959 }
960 }
961
962 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200963 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200964 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200965 }
966
967
968 offset = cvtnum(argv[optind]);
969 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500970 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200971 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200972 }
973 optind++;
974
Kevin Wolf797ac582013-06-05 14:19:31 +0200975 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500976 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200977 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200978 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200979 }
980
Alex Bennée50290c02019-05-29 17:16:32 +0100981 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +0200982 ret = do_aio_readv(blk, &qiov, offset, &total);
Alex Bennée50290c02019-05-29 17:16:32 +0100983 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200984
Max Reitzb32d7a32018-05-09 21:42:59 +0200985 if (ret < 0) {
986 printf("readv failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200987 goto out;
988 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200989 cnt = ret;
990
991 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200992
993 if (Pflag) {
994 void *cmp_buf = g_malloc(qiov.size);
995 memset(cmp_buf, pattern, qiov.size);
996 if (memcmp(buf, cmp_buf, qiov.size)) {
997 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +0200998 PRId64 ", %zu bytes\n", offset, qiov.size);
Max Reitzb32d7a32018-05-09 21:42:59 +0200999 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001000 }
1001 g_free(cmp_buf);
1002 }
1003
1004 if (qflag) {
1005 goto out;
1006 }
1007
1008 if (vflag) {
1009 dump_buffer(buf, offset, qiov.size);
1010 }
1011
1012 /* Finally, report back -- -C gives a parsable format */
1013 t2 = tsub(t2, t1);
1014 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
1015
1016out:
1017 qemu_iovec_destroy(&qiov);
1018 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001019 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001020}
1021
1022static void write_help(void)
1023{
1024 printf(
1025"\n"
1026" writes a range of bytes from the given offset\n"
1027"\n"
1028" Example:\n"
1029" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1030"\n"
1031" Writes into a segment of the currently open file, using a buffer\n"
1032" filled with a set pattern (0xcdcdcdcd).\n"
1033" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -05001034" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001035" -f, -- use Force Unit Access semantics\n"
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001036" -n, -- with -z, don't allow slow fallback\n"
Eric Blake093ea232016-05-07 21:16:43 -06001037" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001038" -P, -- use different pattern to fill file\n"
Denis Plotnikov4d731512019-08-20 19:46:16 +03001039" -s, -- use a pattern file to fill the write buffer\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001040" -C, -- report statistics in a machine parsable format\n"
1041" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001042" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001043" -z, -- write zeroes using blk_co_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001044"\n");
1045}
1046
Max Reitzb32d7a32018-05-09 21:42:59 +02001047static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001048
1049static const cmdinfo_t write_cmd = {
1050 .name = "write",
1051 .altname = "w",
1052 .cfunc = write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001053 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001054 .argmin = 2,
1055 .argmax = -1,
Denis Plotnikov4d731512019-08-20 19:46:16 +03001056 .args = "[-bcCfnquz] [-P pattern | -s source_file] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +02001057 .oneline = "writes a number of bytes at a specified offset",
1058 .help = write_help,
1059};
1060
Max Reitzb32d7a32018-05-09 21:42:59 +02001061static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001062{
Alex Bennée50290c02019-05-29 17:16:32 +01001063 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -06001064 bool Cflag = false, qflag = false, bflag = false;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001065 bool Pflag = false, zflag = false, cflag = false, sflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001066 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001067 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001068 char *buf = NULL;
1069 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -05001070 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001071 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -05001072 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001073 int pattern = 0xcd;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001074 const char *file_name = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001075
Denis Plotnikov4d731512019-08-20 19:46:16 +03001076 while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001077 switch (c) {
1078 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -06001079 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001080 break;
1081 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -06001082 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001083 break;
1084 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001085 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001086 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001087 case 'f':
1088 flags |= BDRV_REQ_FUA;
1089 break;
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001090 case 'n':
1091 flags |= BDRV_REQ_NO_FALLBACK;
1092 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001093 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -06001094 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +02001095 break;
1096 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001097 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001098 pattern = parse_pattern(optarg);
1099 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001100 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001101 }
1102 break;
1103 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001104 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001105 break;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001106 case 's':
1107 sflag = true;
1108 file_name = optarg;
1109 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001110 case 'u':
1111 flags |= BDRV_REQ_MAY_UNMAP;
1112 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001113 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001114 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001115 break;
1116 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001117 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001118 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001119 }
1120 }
1121
1122 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001123 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001124 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001125 }
1126
Eric Blake093ea232016-05-07 21:16:43 -06001127 if (bflag && zflag) {
1128 printf("-b and -z cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001129 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001130 }
1131
Eric Blake770e0e02016-05-07 21:16:44 -06001132 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1133 printf("-f and -b or -c cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001134 return -EINVAL;
Eric Blake770e0e02016-05-07 21:16:44 -06001135 }
1136
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001137 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1138 printf("-n requires -z to be specified\n");
1139 return -EINVAL;
1140 }
1141
Eric Blakec2e001c2016-05-07 21:16:45 -06001142 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1143 printf("-u requires -z to be specified\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001144 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001145 }
1146
Denis Plotnikov4d731512019-08-20 19:46:16 +03001147 if (zflag + Pflag + sflag > 1) {
1148 printf("Only one of -z, -P, and -s "
1149 "can be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001150 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001151 }
1152
1153 offset = cvtnum(argv[optind]);
1154 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001155 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001156 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001157 }
1158
1159 optind++;
1160 count = cvtnum(argv[optind]);
1161 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001162 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001163 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +02001164 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001165 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001166 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001167 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001168 }
1169
Eric Blake093ea232016-05-07 21:16:43 -06001170 if (bflag || cflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -05001171 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1172 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001173 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +02001174 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001175 }
1176
Eric Blake1bce6b42017-04-29 14:14:11 -05001177 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1178 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001179 count);
Max Reitzb32d7a32018-05-09 21:42:59 +02001180 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001181 }
1182 }
1183
1184 if (!zflag) {
Denis Plotnikov4d731512019-08-20 19:46:16 +03001185 if (sflag) {
1186 buf = qemu_io_alloc_from_file(blk, count, file_name);
1187 if (!buf) {
1188 return -EINVAL;
1189 }
1190 } else {
1191 buf = qemu_io_alloc(blk, count, pattern);
1192 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001193 }
1194
Alex Bennée50290c02019-05-29 17:16:32 +01001195 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -06001196 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001197 ret = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001198 } else if (zflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001199 ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001200 } else if (cflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001201 ret = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001202 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +02001203 ret = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001204 }
Alex Bennée50290c02019-05-29 17:16:32 +01001205 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001206
Max Reitzb32d7a32018-05-09 21:42:59 +02001207 if (ret < 0) {
1208 printf("write failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001209 goto out;
1210 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001211 cnt = ret;
1212
1213 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001214
1215 if (qflag) {
1216 goto out;
1217 }
1218
1219 /* Finally, report back -- -C gives a parsable format */
1220 t2 = tsub(t2, t1);
1221 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1222
1223out:
1224 if (!zflag) {
1225 qemu_io_free(buf);
1226 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001227 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001228}
1229
1230static void
1231writev_help(void)
1232{
1233 printf(
1234"\n"
1235" writes a range of bytes from the given offset source from multiple buffers\n"
1236"\n"
1237" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001238" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001239"\n"
1240" Writes into a segment of the currently open file, using a buffer\n"
1241" filled with a set pattern (0xcdcdcdcd).\n"
1242" -P, -- use different pattern to fill file\n"
1243" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001244" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001245" -q, -- quiet mode, do not show I/O statistics\n"
1246"\n");
1247}
1248
Max Reitzb32d7a32018-05-09 21:42:59 +02001249static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001250
1251static const cmdinfo_t writev_cmd = {
1252 .name = "writev",
1253 .cfunc = writev_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001254 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001255 .argmin = 2,
1256 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001257 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001258 .oneline = "writes a number of bytes at a specified offset",
1259 .help = writev_help,
1260};
1261
Max Reitzb32d7a32018-05-09 21:42:59 +02001262static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001263{
Alex Bennée50290c02019-05-29 17:16:32 +01001264 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001265 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001266 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001267 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001268 char *buf;
1269 int64_t offset;
1270 /* Some compilers get confused and warn if this is not initialized. */
1271 int total = 0;
1272 int nr_iov;
1273 int pattern = 0xcd;
1274 QEMUIOVector qiov;
1275
Eric Blake4ca1d342016-05-16 10:43:01 -06001276 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001277 switch (c) {
1278 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001279 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001280 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001281 case 'f':
1282 flags |= BDRV_REQ_FUA;
1283 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001284 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001285 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001286 break;
1287 case 'P':
1288 pattern = parse_pattern(optarg);
1289 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001290 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001291 }
1292 break;
1293 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001294 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001295 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001296 }
1297 }
1298
1299 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001300 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001301 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001302 }
1303
1304 offset = cvtnum(argv[optind]);
1305 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001306 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001307 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001308 }
1309 optind++;
1310
Kevin Wolf797ac582013-06-05 14:19:31 +02001311 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001312 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001313 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001314 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001315 }
1316
Alex Bennée50290c02019-05-29 17:16:32 +01001317 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001318 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
Alex Bennée50290c02019-05-29 17:16:32 +01001319 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001320
Max Reitzb32d7a32018-05-09 21:42:59 +02001321 if (ret < 0) {
1322 printf("writev failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001323 goto out;
1324 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001325 cnt = ret;
1326
1327 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001328
1329 if (qflag) {
1330 goto out;
1331 }
1332
1333 /* Finally, report back -- -C gives a parsable format */
1334 t2 = tsub(t2, t1);
1335 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1336out:
1337 qemu_iovec_destroy(&qiov);
1338 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001339 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001340}
1341
Kevin Wolf797ac582013-06-05 14:19:31 +02001342struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001343 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001344 QEMUIOVector qiov;
1345 int64_t offset;
1346 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001347 bool qflag;
1348 bool vflag;
1349 bool Cflag;
1350 bool Pflag;
1351 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001352 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001353 int pattern;
Alex Bennée50290c02019-05-29 17:16:32 +01001354 struct timespec t1;
Kevin Wolf797ac582013-06-05 14:19:31 +02001355};
1356
1357static void aio_write_done(void *opaque, int ret)
1358{
1359 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001360 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001361
Alex Bennée50290c02019-05-29 17:16:32 +01001362 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001363
1364
1365 if (ret < 0) {
1366 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001367 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001368 goto out;
1369 }
1370
Max Reitz4c7b7e92015-02-05 13:58:22 -05001371 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001372
Kevin Wolf797ac582013-06-05 14:19:31 +02001373 if (ctx->qflag) {
1374 goto out;
1375 }
1376
1377 /* Finally, report back -- -C gives a parsable format */
1378 t2 = tsub(t2, ctx->t1);
1379 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1380 ctx->qiov.size, 1, ctx->Cflag);
1381out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001382 if (!ctx->zflag) {
1383 qemu_io_free(ctx->buf);
1384 qemu_iovec_destroy(&ctx->qiov);
1385 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001386 g_free(ctx);
1387}
1388
1389static void aio_read_done(void *opaque, int ret)
1390{
1391 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001392 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001393
Alex Bennée50290c02019-05-29 17:16:32 +01001394 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001395
1396 if (ret < 0) {
1397 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001398 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001399 goto out;
1400 }
1401
1402 if (ctx->Pflag) {
1403 void *cmp_buf = g_malloc(ctx->qiov.size);
1404
1405 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1406 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1407 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +02001408 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
Kevin Wolf797ac582013-06-05 14:19:31 +02001409 }
1410 g_free(cmp_buf);
1411 }
1412
Max Reitz4c7b7e92015-02-05 13:58:22 -05001413 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001414
Kevin Wolf797ac582013-06-05 14:19:31 +02001415 if (ctx->qflag) {
1416 goto out;
1417 }
1418
1419 if (ctx->vflag) {
1420 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1421 }
1422
1423 /* Finally, report back -- -C gives a parsable format */
1424 t2 = tsub(t2, ctx->t1);
1425 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1426 ctx->qiov.size, 1, ctx->Cflag);
1427out:
1428 qemu_io_free(ctx->buf);
1429 qemu_iovec_destroy(&ctx->qiov);
1430 g_free(ctx);
1431}
1432
1433static void aio_read_help(void)
1434{
1435 printf(
1436"\n"
1437" asynchronously reads a range of bytes from the given offset\n"
1438"\n"
1439" Example:\n"
1440" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1441"\n"
1442" Reads a segment of the currently open file, optionally dumping it to the\n"
1443" standard output stream (with -v option) for subsequent inspection.\n"
1444" The read is performed asynchronously and the aio_flush command must be\n"
1445" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001446" Note that due to its asynchronous nature, this command will be\n"
1447" considered successful once the request is submitted, independently\n"
1448" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001449" -C, -- report statistics in a machine parsable format\n"
1450" -P, -- use a pattern to verify read data\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001451" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001452" -v, -- dump buffer to standard output\n"
1453" -q, -- quiet mode, do not show I/O statistics\n"
1454"\n");
1455}
1456
Max Reitzb32d7a32018-05-09 21:42:59 +02001457static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001458
1459static const cmdinfo_t aio_read_cmd = {
1460 .name = "aio_read",
1461 .cfunc = aio_read_f,
1462 .argmin = 2,
1463 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001464 .args = "[-Ciqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001465 .oneline = "asynchronously reads a number of bytes",
1466 .help = aio_read_help,
1467};
1468
Max Reitzb32d7a32018-05-09 21:42:59 +02001469static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001470{
1471 int nr_iov, c;
1472 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1473
Max Reitz4c7b7e92015-02-05 13:58:22 -05001474 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001475 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001476 switch (c) {
1477 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001478 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001479 break;
1480 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001481 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001482 ctx->pattern = parse_pattern(optarg);
1483 if (ctx->pattern < 0) {
1484 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001485 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001486 }
1487 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001488 case 'i':
1489 printf("injecting invalid read request\n");
1490 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1491 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001492 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001493 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001494 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001495 break;
1496 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001497 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001498 break;
1499 default:
1500 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001501 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001502 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001503 }
1504 }
1505
1506 if (optind > argc - 2) {
1507 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001508 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001509 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001510 }
1511
1512 ctx->offset = cvtnum(argv[optind]);
1513 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001514 int ret = ctx->offset;
1515 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001516 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001517 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001518 }
1519 optind++;
1520
Kevin Wolf797ac582013-06-05 14:19:31 +02001521 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001522 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001523 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001524 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001525 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001526 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001527 }
1528
Alex Bennée50290c02019-05-29 17:16:32 +01001529 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001530 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1531 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001532 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001533 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001534}
1535
1536static void aio_write_help(void)
1537{
1538 printf(
1539"\n"
1540" asynchronously writes a range of bytes from the given offset source\n"
1541" from multiple buffers\n"
1542"\n"
1543" Example:\n"
1544" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1545"\n"
1546" Writes into a segment of the currently open file, using a buffer\n"
1547" filled with a set pattern (0xcdcdcdcd).\n"
1548" The write is performed asynchronously and the aio_flush command must be\n"
1549" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001550" Note that due to its asynchronous nature, this command will be\n"
1551" considered successful once the request is submitted, independently\n"
1552" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001553" -P, -- use different pattern to fill file\n"
1554" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001555" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001556" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001557" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001558" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001559" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001560"\n");
1561}
1562
Max Reitzb32d7a32018-05-09 21:42:59 +02001563static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001564
1565static const cmdinfo_t aio_write_cmd = {
1566 .name = "aio_write",
1567 .cfunc = aio_write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001568 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001569 .argmin = 2,
1570 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001571 .args = "[-Cfiquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001572 .oneline = "asynchronously writes a number of bytes",
1573 .help = aio_write_help,
1574};
1575
Max Reitzb32d7a32018-05-09 21:42:59 +02001576static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001577{
1578 int nr_iov, c;
1579 int pattern = 0xcd;
1580 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001581 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001582
Max Reitz4c7b7e92015-02-05 13:58:22 -05001583 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001584 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001585 switch (c) {
1586 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001587 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001588 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001589 case 'f':
1590 flags |= BDRV_REQ_FUA;
1591 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001592 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001593 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001594 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001595 case 'u':
1596 flags |= BDRV_REQ_MAY_UNMAP;
1597 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001598 case 'P':
1599 pattern = parse_pattern(optarg);
1600 if (pattern < 0) {
1601 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001602 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001603 }
1604 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001605 case 'i':
1606 printf("injecting invalid write request\n");
1607 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1608 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001609 return 0;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001610 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001611 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001612 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001613 default:
1614 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001615 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001616 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001617 }
1618 }
1619
1620 if (optind > argc - 2) {
1621 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001622 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001623 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001624 }
1625
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001626 if (ctx->zflag && optind != argc - 2) {
1627 printf("-z supports only a single length parameter\n");
1628 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001629 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001630 }
1631
Eric Blakec2e001c2016-05-07 21:16:45 -06001632 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1633 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001634 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001635 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001636 }
1637
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001638 if (ctx->zflag && ctx->Pflag) {
1639 printf("-z and -P cannot be specified at the same time\n");
1640 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001641 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001642 }
1643
Kevin Wolf797ac582013-06-05 14:19:31 +02001644 ctx->offset = cvtnum(argv[optind]);
1645 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001646 int ret = ctx->offset;
1647 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001648 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001649 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001650 }
1651 optind++;
1652
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001653 if (ctx->zflag) {
1654 int64_t count = cvtnum(argv[optind]);
1655 if (count < 0) {
1656 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001657 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001658 return count;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001659 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001660
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001661 ctx->qiov.size = count;
Eric Blaked004bd52016-05-24 16:25:20 -06001662 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1663 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001664 } else {
1665 nr_iov = argc - optind;
1666 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1667 pattern);
1668 if (ctx->buf == NULL) {
1669 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1670 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001671 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001672 }
1673
Alex Bennée50290c02019-05-29 17:16:32 +01001674 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001675 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1676 BLOCK_ACCT_WRITE);
1677
Eric Blake770e0e02016-05-07 21:16:44 -06001678 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1679 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001680 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001681
1682 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001683}
1684
Max Reitzb32d7a32018-05-09 21:42:59 +02001685static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001686{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001687 BlockAcctCookie cookie;
1688 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001689 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001690 block_acct_done(blk_get_stats(blk), &cookie);
Max Reitzb32d7a32018-05-09 21:42:59 +02001691 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001692}
1693
1694static const cmdinfo_t aio_flush_cmd = {
1695 .name = "aio_flush",
1696 .cfunc = aio_flush_f,
1697 .oneline = "completes all outstanding aio requests"
1698};
1699
Max Reitzb32d7a32018-05-09 21:42:59 +02001700static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001701{
Max Reitzb32d7a32018-05-09 21:42:59 +02001702 return blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001703}
1704
1705static const cmdinfo_t flush_cmd = {
1706 .name = "flush",
1707 .altname = "f",
1708 .cfunc = flush_f,
1709 .oneline = "flush all in-core file state to disk",
1710};
1711
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001712static int truncate_f(BlockBackend *blk, int argc, char **argv);
1713static const cmdinfo_t truncate_cmd = {
1714 .name = "truncate",
1715 .altname = "t",
1716 .cfunc = truncate_f,
1717 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
1718 .argmin = 1,
1719 .argmax = 3,
1720 .args = "[-m prealloc_mode] off",
1721 .oneline = "truncates the current file at the given offset",
1722};
1723
Max Reitzb32d7a32018-05-09 21:42:59 +02001724static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001725{
Max Reitzed3d2ec2017-03-28 22:51:27 +02001726 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001727 int64_t offset;
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001728 int c, ret;
1729 PreallocMode prealloc = PREALLOC_MODE_OFF;
Kevin Wolf797ac582013-06-05 14:19:31 +02001730
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001731 while ((c = getopt(argc, argv, "m:")) != -1) {
1732 switch (c) {
1733 case 'm':
1734 prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
1735 PREALLOC_MODE__MAX, NULL);
1736 if (prealloc == PREALLOC_MODE__MAX) {
1737 error_report("Invalid preallocation mode '%s'", optarg);
1738 return -EINVAL;
1739 }
1740 break;
1741 default:
1742 qemuio_command_usage(&truncate_cmd);
1743 return -EINVAL;
1744 }
1745 }
1746
1747 offset = cvtnum(argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001748 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001749 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001750 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001751 }
1752
Max Reitze8d04f92019-09-18 11:51:43 +02001753 /*
1754 * qemu-io is a debugging tool, so let us be strict here and pass
1755 * exact=true. It is better to err on the "emit more errors" side
1756 * than to be overly permissive.
1757 */
Vladimir Sementsov-Ogievskiy42ba0222020-10-21 17:58:47 +03001758 ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err);
Kevin Wolf797ac582013-06-05 14:19:31 +02001759 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001760 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02001761 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001762 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001763
1764 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001765}
1766
Max Reitzb32d7a32018-05-09 21:42:59 +02001767static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001768{
1769 int64_t size;
1770 char s1[64];
1771
Max Reitz4c7b7e92015-02-05 13:58:22 -05001772 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001773 if (size < 0) {
1774 printf("getlength: %s\n", strerror(-size));
Max Reitzb32d7a32018-05-09 21:42:59 +02001775 return size;
Kevin Wolf797ac582013-06-05 14:19:31 +02001776 }
1777
1778 cvtstr(size, s1, sizeof(s1));
1779 printf("%s\n", s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001780 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001781}
1782
1783
1784static const cmdinfo_t length_cmd = {
1785 .name = "length",
1786 .altname = "l",
1787 .cfunc = length_f,
1788 .oneline = "gets the length of the current file",
1789};
1790
1791
Max Reitzb32d7a32018-05-09 21:42:59 +02001792static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001793{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001794 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001795 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001796 ImageInfoSpecific *spec_info;
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001797 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001798 char s1[64], s2[64];
1799 int ret;
1800
1801 if (bs->drv && bs->drv->format_name) {
1802 printf("format name: %s\n", bs->drv->format_name);
1803 }
1804 if (bs->drv && bs->drv->protocol_name) {
1805 printf("format name: %s\n", bs->drv->protocol_name);
1806 }
1807
1808 ret = bdrv_get_info(bs, &bdi);
1809 if (ret) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001810 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001811 }
1812
1813 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1814 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1815
1816 printf("cluster size: %s\n", s1);
1817 printf("vm state offset: %s\n", s2);
1818
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001819 spec_info = bdrv_get_specific_info(bs, &local_err);
1820 if (local_err) {
1821 error_report_err(local_err);
1822 return -EIO;
1823 }
Max Reitza8d8ecb2013-10-09 10:46:17 +02001824 if (spec_info) {
1825 printf("Format specific information:\n");
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02001826 bdrv_image_info_specific_dump(spec_info);
Max Reitza8d8ecb2013-10-09 10:46:17 +02001827 qapi_free_ImageInfoSpecific(spec_info);
1828 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001829
1830 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001831}
1832
1833
1834
1835static const cmdinfo_t info_cmd = {
1836 .name = "info",
1837 .altname = "i",
1838 .cfunc = info_f,
1839 .oneline = "prints information about the current file",
1840};
1841
1842static void discard_help(void)
1843{
1844 printf(
1845"\n"
1846" discards a range of bytes from the given offset\n"
1847"\n"
1848" Example:\n"
1849" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1850"\n"
1851" Discards a segment of the currently open file.\n"
1852" -C, -- report statistics in a machine parsable format\n"
1853" -q, -- quiet mode, do not show I/O statistics\n"
1854"\n");
1855}
1856
Max Reitzb32d7a32018-05-09 21:42:59 +02001857static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001858
1859static const cmdinfo_t discard_cmd = {
1860 .name = "discard",
1861 .altname = "d",
1862 .cfunc = discard_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001863 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001864 .argmin = 2,
1865 .argmax = -1,
1866 .args = "[-Cq] off len",
1867 .oneline = "discards a number of bytes at a specified offset",
1868 .help = discard_help,
1869};
1870
Max Reitzb32d7a32018-05-09 21:42:59 +02001871static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001872{
Alex Bennée50290c02019-05-29 17:16:32 +01001873 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001874 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001875 int c, ret;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001876 int64_t offset, bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +02001877
Eric Blakeb062ad82015-05-12 09:10:56 -06001878 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001879 switch (c) {
1880 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001881 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001882 break;
1883 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001884 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001885 break;
1886 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001887 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001888 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001889 }
1890 }
1891
1892 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001893 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001894 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001895 }
1896
1897 offset = cvtnum(argv[optind]);
1898 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001899 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001900 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001901 }
1902
1903 optind++;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001904 bytes = cvtnum(argv[optind]);
1905 if (bytes < 0) {
1906 print_cvtnum_err(bytes, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001907 return bytes;
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001908 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001909 printf("length cannot exceed %"PRIu64", given %s\n",
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001910 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001911 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001912 }
1913
Alex Bennée50290c02019-05-29 17:16:32 +01001914 clock_gettime(CLOCK_MONOTONIC, &t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001915 ret = blk_pdiscard(blk, offset, bytes);
Alex Bennée50290c02019-05-29 17:16:32 +01001916 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001917
1918 if (ret < 0) {
1919 printf("discard failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001920 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001921 }
1922
1923 /* Finally, report back -- -C gives a parsable format */
1924 if (!qflag) {
1925 t2 = tsub(t2, t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001926 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
Kevin Wolf797ac582013-06-05 14:19:31 +02001927 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001928
1929 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001930}
1931
Max Reitzb32d7a32018-05-09 21:42:59 +02001932static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001933{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001934 BlockDriverState *bs = blk_bs(blk);
Eric Blaked6a644b2017-07-07 07:44:57 -05001935 int64_t offset, start, remaining, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001936 char s1[64];
Eric Blaked6a644b2017-07-07 07:44:57 -05001937 int ret;
1938 int64_t num, sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001939
Eric Blaked6a644b2017-07-07 07:44:57 -05001940 start = offset = cvtnum(argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001941 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001942 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001943 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001944 }
1945
1946 if (argc == 3) {
Eric Blake4401fdc2017-04-29 14:14:12 -05001947 count = cvtnum(argv[2]);
1948 if (count < 0) {
1949 print_cvtnum_err(count, argv[2]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001950 return count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001951 }
1952 } else {
Eric Blake4401fdc2017-04-29 14:14:12 -05001953 count = BDRV_SECTOR_SIZE;
Kevin Wolf797ac582013-06-05 14:19:31 +02001954 }
1955
Eric Blaked6a644b2017-07-07 07:44:57 -05001956 remaining = count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001957 sum_alloc = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001958 while (remaining) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001959 ret = bdrv_is_allocated(bs, offset, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001960 if (ret < 0) {
1961 printf("is_allocated failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001962 return ret;
Paolo Bonzinid6636402013-09-04 19:00:25 +02001963 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001964 offset += num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001965 remaining -= num;
1966 if (ret) {
1967 sum_alloc += num;
1968 }
1969 if (num == 0) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001970 count -= remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001971 remaining = 0;
1972 }
1973 }
1974
Eric Blaked6a644b2017-07-07 07:44:57 -05001975 cvtstr(start, s1, sizeof(s1));
Kevin Wolf797ac582013-06-05 14:19:31 +02001976
Eric Blake4401fdc2017-04-29 14:14:12 -05001977 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001978 sum_alloc, count, s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001979 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001980}
1981
1982static const cmdinfo_t alloc_cmd = {
1983 .name = "alloc",
1984 .altname = "a",
1985 .argmin = 1,
1986 .argmax = 2,
1987 .cfunc = alloc_f,
Eric Blake4401fdc2017-04-29 14:14:12 -05001988 .args = "offset [count]",
1989 .oneline = "checks if offset is allocated in the file",
Kevin Wolf797ac582013-06-05 14:19:31 +02001990};
1991
1992
Eric Blaked6a644b2017-07-07 07:44:57 -05001993static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1994 int64_t bytes, int64_t *pnum)
Kevin Wolf797ac582013-06-05 14:19:31 +02001995{
Eric Blaked6a644b2017-07-07 07:44:57 -05001996 int64_t num;
1997 int num_checked;
Kevin Wolf797ac582013-06-05 14:19:31 +02001998 int ret, firstret;
1999
Eric Blaked6a644b2017-07-07 07:44:57 -05002000 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
2001 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002002 if (ret < 0) {
2003 return ret;
2004 }
2005
2006 firstret = ret;
2007 *pnum = num;
2008
Eric Blaked6a644b2017-07-07 07:44:57 -05002009 while (bytes > 0 && ret == firstret) {
2010 offset += num;
2011 bytes -= num;
Kevin Wolf797ac582013-06-05 14:19:31 +02002012
Eric Blaked6a644b2017-07-07 07:44:57 -05002013 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
2014 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02002015 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02002016 *pnum += num;
2017 } else {
2018 break;
2019 }
2020 }
2021
2022 return firstret;
2023}
2024
Max Reitzb32d7a32018-05-09 21:42:59 +02002025static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002026{
Eric Blaked6a644b2017-07-07 07:44:57 -05002027 int64_t offset, bytes;
Eric Blake6f3c90a2017-04-29 14:14:13 -05002028 char s1[64], s2[64];
Kevin Wolf797ac582013-06-05 14:19:31 +02002029 int64_t num;
2030 int ret;
2031 const char *retstr;
2032
2033 offset = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05002034 bytes = blk_getlength(blk);
2035 if (bytes < 0) {
2036 error_report("Failed to query image length: %s", strerror(-bytes));
Max Reitzb32d7a32018-05-09 21:42:59 +02002037 return bytes;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002038 }
2039
Eric Blaked6a644b2017-07-07 07:44:57 -05002040 while (bytes) {
2041 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002042 if (ret < 0) {
2043 error_report("Failed to get allocation status: %s", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002044 return ret;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002045 } else if (!num) {
2046 error_report("Unexpected end of image");
Max Reitzb32d7a32018-05-09 21:42:59 +02002047 return -EIO;
Kevin Wolf797ac582013-06-05 14:19:31 +02002048 }
2049
2050 retstr = ret ? " allocated" : "not allocated";
Eric Blaked6a644b2017-07-07 07:44:57 -05002051 cvtstr(num, s1, sizeof(s1));
2052 cvtstr(offset, s2, sizeof(s2));
Eric Blake6f3c90a2017-04-29 14:14:13 -05002053 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05002054 s1, num, retstr, s2, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02002055
2056 offset += num;
Eric Blaked6a644b2017-07-07 07:44:57 -05002057 bytes -= num;
2058 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002059
2060 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002061}
2062
2063static const cmdinfo_t map_cmd = {
2064 .name = "map",
2065 .argmin = 0,
2066 .argmax = 0,
2067 .cfunc = map_f,
2068 .args = "",
2069 .oneline = "prints the allocated areas of a file",
2070};
2071
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002072static void reopen_help(void)
2073{
2074 printf(
2075"\n"
2076" Changes the open options of an already opened image\n"
2077"\n"
2078" Example:\n"
2079" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2080"\n"
2081" -r, -- Reopen the image read-only\n"
Kevin Wolfea922032017-08-03 17:03:00 +02002082" -w, -- Reopen the image read-write\n"
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002083" -c, -- Change the cache mode to the given value\n"
2084" -o, -- Changes block driver options (cf. 'open' command)\n"
2085"\n");
2086}
2087
Max Reitzb32d7a32018-05-09 21:42:59 +02002088static int reopen_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002089
2090static QemuOptsList reopen_opts = {
2091 .name = "reopen",
2092 .merge_lists = true,
2093 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2094 .desc = {
2095 /* no elements => accept any params */
2096 { /* end of list */ }
2097 },
2098};
2099
2100static const cmdinfo_t reopen_cmd = {
2101 .name = "reopen",
2102 .argmin = 0,
2103 .argmax = -1,
2104 .cfunc = reopen_f,
Kevin Wolfea922032017-08-03 17:03:00 +02002105 .args = "[(-r|-w)] [-c cache] [-o options]",
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002106 .oneline = "reopens an image with new options",
2107 .help = reopen_help,
2108};
2109
Max Reitzb32d7a32018-05-09 21:42:59 +02002110static int reopen_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002111{
2112 BlockDriverState *bs = blk_bs(blk);
2113 QemuOpts *qopts;
2114 QDict *opts;
2115 int c;
2116 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002117 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolfea922032017-08-03 17:03:00 +02002118 bool has_rw_option = false;
Alberto Garciadc900c32018-11-12 16:00:42 +02002119 bool has_cache_option = false;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002120 Error *local_err = NULL;
2121
Kevin Wolfea922032017-08-03 17:03:00 +02002122 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002123 switch (c) {
2124 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002125 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002126 error_report("Invalid cache option: %s", optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +02002127 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002128 }
Alberto Garciadc900c32018-11-12 16:00:42 +02002129 has_cache_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002130 break;
2131 case 'o':
2132 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2133 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002134 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002135 }
2136 break;
2137 case 'r':
Kevin Wolfea922032017-08-03 17:03:00 +02002138 if (has_rw_option) {
2139 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002140 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002141 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002142 flags &= ~BDRV_O_RDWR;
Kevin Wolfea922032017-08-03 17:03:00 +02002143 has_rw_option = true;
2144 break;
2145 case 'w':
2146 if (has_rw_option) {
2147 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002148 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002149 }
2150 flags |= BDRV_O_RDWR;
2151 has_rw_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002152 break;
2153 default:
2154 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002155 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002156 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002157 }
2158 }
2159
2160 if (optind != argc) {
2161 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002162 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002163 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002164 }
2165
Alberto Garciaa8003ec2018-09-06 12:37:01 +03002166 if (!writethrough != blk_enable_write_cache(blk) &&
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002167 blk_get_attached_dev(blk))
2168 {
2169 error_report("Cannot change cache.writeback: Device attached");
2170 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002171 return -EBUSY;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002172 }
2173
Kevin Wolff3adefb2017-09-22 14:50:12 +02002174 if (!(flags & BDRV_O_RDWR)) {
2175 uint64_t orig_perm, orig_shared_perm;
2176
2177 bdrv_drain(bs);
2178
2179 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2180 blk_set_perm(blk,
2181 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2182 orig_shared_perm,
2183 &error_abort);
2184 }
2185
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002186 qopts = qemu_opts_find(&reopen_opts, NULL);
Alberto Garciadc900c32018-11-12 16:00:42 +02002187 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002188 qemu_opts_reset(&reopen_opts);
2189
Alberto Garciadc900c32018-11-12 16:00:42 +02002190 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2191 if (has_rw_option) {
2192 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2193 qobject_unref(opts);
2194 return -EINVAL;
2195 }
2196 } else {
2197 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2198 }
2199
2200 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2201 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2202 if (has_cache_option) {
2203 error_report("Cannot set both -c and the cache options");
2204 qobject_unref(opts);
2205 return -EINVAL;
2206 }
2207 } else {
2208 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2209 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2210 }
2211
Kevin Wolf6cf42ca2021-07-08 13:47:06 +02002212 bdrv_reopen(bs, opts, true, &local_err);
Kevin Wolf1a63a902017-12-06 20:24:44 +01002213
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002214 if (local_err) {
2215 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02002216 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002217 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002218
2219 blk_set_enable_write_cache(blk, !writethrough);
2220 return 0;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002221}
2222
Max Reitzb32d7a32018-05-09 21:42:59 +02002223static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002224{
2225 int ret;
2226
Max Reitz4c7b7e92015-02-05 13:58:22 -05002227 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002228 if (ret < 0) {
2229 printf("Could not set breakpoint: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002230 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002231 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002232
2233 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002234}
2235
Max Reitzb32d7a32018-05-09 21:42:59 +02002236static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002237{
2238 int ret;
2239
Max Reitz4c7b7e92015-02-05 13:58:22 -05002240 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002241 if (ret < 0) {
2242 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002243 return ret;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002244 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002245
2246 return 0;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002247}
2248
Kevin Wolf797ac582013-06-05 14:19:31 +02002249static const cmdinfo_t break_cmd = {
2250 .name = "break",
2251 .argmin = 2,
2252 .argmax = 2,
2253 .cfunc = break_f,
2254 .args = "event tag",
2255 .oneline = "sets a breakpoint on event and tags the stopped "
2256 "request as tag",
2257};
2258
Fam Zheng4cc70e92013-11-20 10:01:54 +08002259static const cmdinfo_t remove_break_cmd = {
2260 .name = "remove_break",
2261 .argmin = 1,
2262 .argmax = 1,
2263 .cfunc = remove_break_f,
2264 .args = "tag",
2265 .oneline = "remove a breakpoint by tag",
2266};
2267
Max Reitzb32d7a32018-05-09 21:42:59 +02002268static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002269{
2270 int ret;
2271
Max Reitz4c7b7e92015-02-05 13:58:22 -05002272 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002273 if (ret < 0) {
2274 printf("Could not resume request: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002275 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002276 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002277
2278 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002279}
2280
2281static const cmdinfo_t resume_cmd = {
2282 .name = "resume",
2283 .argmin = 1,
2284 .argmax = 1,
2285 .cfunc = resume_f,
2286 .args = "tag",
2287 .oneline = "resumes the request tagged as tag",
2288};
2289
Max Reitzb32d7a32018-05-09 21:42:59 +02002290static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002291{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002292 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2293 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002294 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002295 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002296}
2297
2298static const cmdinfo_t wait_break_cmd = {
2299 .name = "wait_break",
2300 .argmin = 1,
2301 .argmax = 1,
2302 .cfunc = wait_break_f,
2303 .args = "tag",
2304 .oneline = "waits for the suspension of a request",
2305};
2306
Max Reitzb32d7a32018-05-09 21:42:59 +02002307static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002308{
2309 abort();
2310}
2311
2312static const cmdinfo_t abort_cmd = {
2313 .name = "abort",
2314 .cfunc = abort_f,
2315 .flags = CMD_NOFILE_OK,
2316 .oneline = "simulate a program crash using abort(3)",
2317};
2318
Max Reitz0e82dc72014-12-08 10:48:10 +01002319static void sigraise_help(void)
2320{
2321 printf(
2322"\n"
2323" raises the given signal\n"
2324"\n"
2325" Example:\n"
2326" 'sigraise %i' - raises SIGTERM\n"
2327"\n"
2328" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2329" given to sigraise.\n"
2330"\n", SIGTERM);
2331}
2332
Max Reitzb32d7a32018-05-09 21:42:59 +02002333static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002334
2335static const cmdinfo_t sigraise_cmd = {
2336 .name = "sigraise",
2337 .cfunc = sigraise_f,
2338 .argmin = 1,
2339 .argmax = 1,
2340 .flags = CMD_NOFILE_OK,
2341 .args = "signal",
2342 .oneline = "raises a signal",
2343 .help = sigraise_help,
2344};
2345
Max Reitzb32d7a32018-05-09 21:42:59 +02002346static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002347{
John Snow9b0beaf2015-11-05 18:53:02 -05002348 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002349 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002350 print_cvtnum_err(sig, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002351 return sig;
John Snow9b0beaf2015-11-05 18:53:02 -05002352 } else if (sig > NSIG) {
2353 printf("signal argument '%s' is too large to be a valid signal\n",
2354 argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002355 return -EINVAL;
Max Reitz0e82dc72014-12-08 10:48:10 +01002356 }
2357
2358 /* Using raise() to kill this process does not necessarily flush all open
2359 * streams. At least stdout and stderr (although the latter should be
2360 * non-buffered anyway) should be flushed, though. */
2361 fflush(stdout);
2362 fflush(stderr);
2363
2364 raise(sig);
Max Reitzb32d7a32018-05-09 21:42:59 +02002365
2366 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002367}
2368
Kevin Wolfcd33d022014-01-15 15:39:10 +01002369static void sleep_cb(void *opaque)
2370{
2371 bool *expired = opaque;
2372 *expired = true;
2373}
2374
Max Reitzb32d7a32018-05-09 21:42:59 +02002375static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002376{
2377 char *endptr;
2378 long ms;
2379 struct QEMUTimer *timer;
2380 bool expired = false;
2381
2382 ms = strtol(argv[1], &endptr, 0);
2383 if (ms < 0 || *endptr != '\0') {
2384 printf("%s is not a valid number\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002385 return -EINVAL;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002386 }
2387
2388 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2389 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2390
2391 while (!expired) {
2392 main_loop_wait(false);
2393 }
2394
2395 timer_free(timer);
Max Reitzb32d7a32018-05-09 21:42:59 +02002396 return 0;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002397}
2398
2399static const cmdinfo_t sleep_cmd = {
2400 .name = "sleep",
2401 .argmin = 1,
2402 .argmax = 1,
2403 .cfunc = sleep_f,
2404 .flags = CMD_NOFILE_OK,
2405 .oneline = "waits for the given value in milliseconds",
2406};
2407
Kevin Wolff18a8342013-06-05 14:19:33 +02002408static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2409{
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002410 printf("%s ", cmd);
Kevin Wolff18a8342013-06-05 14:19:33 +02002411
2412 if (ct->args) {
2413 printf("%s ", ct->args);
2414 }
2415 printf("-- %s\n", ct->oneline);
2416}
2417
2418static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2419{
2420 help_oneline(cmd, ct);
2421 if (ct->help) {
2422 ct->help();
2423 }
2424}
2425
2426static void help_all(void)
2427{
2428 const cmdinfo_t *ct;
2429
2430 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2431 help_oneline(ct->name, ct);
2432 }
2433 printf("\nUse 'help commandname' for extended help.\n");
2434}
2435
Max Reitzb32d7a32018-05-09 21:42:59 +02002436static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002437{
2438 const cmdinfo_t *ct;
2439
Dr. David Alan Gilbertda16f4b2020-08-24 11:29:14 +01002440 if (argc < 2) {
Kevin Wolff18a8342013-06-05 14:19:33 +02002441 help_all();
Max Reitzb32d7a32018-05-09 21:42:59 +02002442 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002443 }
2444
2445 ct = find_command(argv[1]);
2446 if (ct == NULL) {
2447 printf("command %s not found\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002448 return -EINVAL;
Kevin Wolff18a8342013-06-05 14:19:33 +02002449 }
2450
2451 help_onecmd(argv[1], ct);
Max Reitzb32d7a32018-05-09 21:42:59 +02002452 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002453}
2454
2455static const cmdinfo_t help_cmd = {
2456 .name = "help",
2457 .altname = "?",
2458 .cfunc = help_f,
2459 .argmin = 0,
2460 .argmax = 1,
2461 .flags = CMD_FLAG_GLOBAL,
2462 .args = "[command]",
2463 .oneline = "help for one or all commands",
2464};
2465
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +03002466/*
2467 * Called with aio context of blk acquired. Or with qemu_get_aio_context()
2468 * context acquired if blk is NULL.
2469 */
Max Reitzb32d7a32018-05-09 21:42:59 +02002470int qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002471{
2472 char *input;
2473 const cmdinfo_t *ct;
2474 char **v;
2475 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +02002476 int ret = 0;
Kevin Wolfdd583292013-06-05 14:19:32 +02002477
2478 input = g_strdup(cmd);
2479 v = breakline(input, &c);
2480 if (c) {
2481 ct = find_command(v[0]);
2482 if (ct) {
Max Reitzb32d7a32018-05-09 21:42:59 +02002483 ret = command(blk, ct, c, v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002484 } else {
2485 fprintf(stderr, "command \"%s\" not found\n", v[0]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002486 ret = -EINVAL;
Kevin Wolfdd583292013-06-05 14:19:32 +02002487 }
2488 }
2489 g_free(input);
2490 g_free(v);
Max Reitzb32d7a32018-05-09 21:42:59 +02002491
2492 return ret;
Kevin Wolfdd583292013-06-05 14:19:32 +02002493}
2494
Kevin Wolf797ac582013-06-05 14:19:31 +02002495static void __attribute((constructor)) init_qemuio_commands(void)
2496{
2497 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002498 qemuio_add_command(&help_cmd);
2499 qemuio_add_command(&read_cmd);
2500 qemuio_add_command(&readv_cmd);
2501 qemuio_add_command(&write_cmd);
2502 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002503 qemuio_add_command(&aio_read_cmd);
2504 qemuio_add_command(&aio_write_cmd);
2505 qemuio_add_command(&aio_flush_cmd);
2506 qemuio_add_command(&flush_cmd);
2507 qemuio_add_command(&truncate_cmd);
2508 qemuio_add_command(&length_cmd);
2509 qemuio_add_command(&info_cmd);
2510 qemuio_add_command(&discard_cmd);
2511 qemuio_add_command(&alloc_cmd);
2512 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002513 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002514 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002515 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002516 qemuio_add_command(&resume_cmd);
2517 qemuio_add_command(&wait_break_cmd);
2518 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002519 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002520 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002521}