blob: 349256a5fe1228a889c79cca0c29cf7fcab75eae [file] [log] [blame]
Kevin Wolf797ac582013-06-05 14:19:31 +02001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
Eric Blake093ea232016-05-07 21:16:43 -06004 * Copyright (C) 2009-2016 Red Hat, Inc.
Kevin Wolf797ac582013-06-05 14:19:31 +02005 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010012#include "qapi/error.h"
Alberto Garciadc900c32018-11-12 16:00:42 +020013#include "qapi/qmp/qdict.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020014#include "qemu-io.h"
Max Reitz4c7b7e92015-02-05 13:58:22 -050015#include "sysemu/block-backend.h"
16#include "block/block.h"
17#include "block/block_int.h" /* for info_f() */
Max Reitza8d8ecb2013-10-09 10:46:17 +020018#include "block/qapi.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010019#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010020#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010021#include "qemu/option.h"
Kevin Wolfcd33d022014-01-15 15:39:10 +010022#include "qemu/timer.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020023#include "qemu/cutils.h"
Kevin Wolf797ac582013-06-05 14:19:31 +020024
25#define CMD_NOFILE_OK 0x01
26
Stefan Weilf9883882014-03-05 22:23:00 +010027bool qemuio_misalign;
Kevin Wolf797ac582013-06-05 14:19:31 +020028
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020029static cmdinfo_t *cmdtab;
30static int ncmds;
31
32static int compare_cmdname(const void *a, const void *b)
33{
34 return strcmp(((const cmdinfo_t *)a)->name,
35 ((const cmdinfo_t *)b)->name);
36}
37
38void qemuio_add_command(const cmdinfo_t *ci)
39{
Peter Maydell6aabeb52017-03-31 14:38:49 +010040 /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
41 * flags allow it not to be, so that combination is invalid.
42 * Catch it now rather than letting it manifest as a crash if a
43 * particular set of command line options are used.
44 */
45 assert(ci->perm == 0 ||
46 (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
Markus Armbruster02c4f262014-08-19 10:31:09 +020047 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020048 cmdtab[ncmds - 1] = *ci;
49 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
50}
51
Max Reitzb444d0e2018-05-09 21:42:58 +020052void qemuio_command_usage(const cmdinfo_t *ci)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020053{
54 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020055}
56
Max Reitz4c7b7e92015-02-05 13:58:22 -050057static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020058{
59 if (ct->flags & CMD_FLAG_GLOBAL) {
60 return 1;
61 }
Max Reitz4c7b7e92015-02-05 13:58:22 -050062 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020063 fprintf(stderr, "no file open, try 'help open'\n");
64 return 0;
65 }
66 return 1;
67}
68
Max Reitzb32d7a32018-05-09 21:42:59 +020069static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
70 char **argv)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020071{
72 char *cmd = argv[0];
73
Max Reitz4c7b7e92015-02-05 13:58:22 -050074 if (!init_check_command(blk, ct)) {
Max Reitzb32d7a32018-05-09 21:42:59 +020075 return -EINVAL;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020076 }
77
78 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
79 if (ct->argmax == -1) {
80 fprintf(stderr,
81 "bad argument count %d to %s, expected at least %d arguments\n",
82 argc-1, cmd, ct->argmin);
83 } else if (ct->argmin == ct->argmax) {
84 fprintf(stderr,
85 "bad argument count %d to %s, expected %d arguments\n",
86 argc-1, cmd, ct->argmin);
87 } else {
88 fprintf(stderr,
89 "bad argument count %d to %s, expected between %d and %d arguments\n",
90 argc-1, cmd, ct->argmin, ct->argmax);
91 }
Max Reitzb32d7a32018-05-09 21:42:59 +020092 return -EINVAL;
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020093 }
Kevin Wolf887354b2017-02-10 16:24:56 +010094
95 /* Request additional permissions if necessary for this command. The caller
96 * is responsible for restoring the original permissions afterwards if this
97 * is what it wants. */
98 if (ct->perm && blk_is_available(blk)) {
99 uint64_t orig_perm, orig_shared_perm;
100 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
101
102 if (ct->perm & ~orig_perm) {
103 uint64_t new_perm;
104 Error *local_err = NULL;
105 int ret;
106
107 new_perm = orig_perm | ct->perm;
108
109 ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
110 if (ret < 0) {
111 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +0200112 return ret;
Kevin Wolf887354b2017-02-10 16:24:56 +0100113 }
114 }
115 }
116
Richard W.M. Jonesd339d762019-01-18 10:11:14 +0000117 qemu_reset_optind();
Max Reitzb32d7a32018-05-09 21:42:59 +0200118 return ct->cfunc(blk, argc, argv);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200119}
120
121static const cmdinfo_t *find_command(const char *cmd)
122{
123 cmdinfo_t *ct;
124
125 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
126 if (strcmp(ct->name, cmd) == 0 ||
127 (ct->altname && strcmp(ct->altname, cmd) == 0))
128 {
129 return (const cmdinfo_t *)ct;
130 }
131 }
132 return NULL;
133}
134
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100135/* Invoke fn() for commands with a matching prefix */
136void qemuio_complete_command(const char *input,
137 void (*fn)(const char *cmd, void *opaque),
138 void *opaque)
139{
140 cmdinfo_t *ct;
141 size_t input_len = strlen(input);
142
143 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
144 if (strncmp(input, ct->name, input_len) == 0) {
145 fn(ct->name, opaque);
146 }
147 }
148}
149
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200150static char **breakline(char *input, int *count)
151{
152 int c = 0;
153 char *p;
Markus Armbruster5839e532014-08-19 10:31:08 +0200154 char **rval = g_new0(char *, 1);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200155
156 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
157 if (!*p) {
158 continue;
159 }
160 c++;
Markus Armbruster08193dd2014-08-19 10:31:10 +0200161 rval = g_renew(char *, rval, (c + 1));
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200162 rval[c - 1] = p;
163 rval[c] = NULL;
164 }
165 *count = c;
166 return rval;
167}
168
Kevin Wolf797ac582013-06-05 14:19:31 +0200169static int64_t cvtnum(const char *s)
170{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100171 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100172 uint64_t value;
John Snowef5a7882015-11-05 18:53:03 -0500173
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100174 err = qemu_strtosz(s, NULL, &value);
175 if (err < 0) {
176 return err;
177 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100178 if (value > INT64_MAX) {
179 return -ERANGE;
180 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100181 return value;
Kevin Wolf797ac582013-06-05 14:19:31 +0200182}
183
John Snowa9ecfa02015-11-05 18:53:04 -0500184static void print_cvtnum_err(int64_t rc, const char *arg)
185{
186 switch (rc) {
187 case -EINVAL:
188 printf("Parsing error: non-numeric argument,"
189 " or extraneous/unrecognized suffix -- %s\n", arg);
190 break;
191 case -ERANGE:
192 printf("Parsing error: argument too large -- %s\n", arg);
193 break;
194 default:
195 printf("Parsing error: %s\n", arg);
196 }
197}
198
Kevin Wolf0b613882013-06-05 14:19:38 +0200199#define EXABYTES(x) ((long long)(x) << 60)
200#define PETABYTES(x) ((long long)(x) << 50)
201#define TERABYTES(x) ((long long)(x) << 40)
202#define GIGABYTES(x) ((long long)(x) << 30)
203#define MEGABYTES(x) ((long long)(x) << 20)
204#define KILOBYTES(x) ((long long)(x) << 10)
205
206#define TO_EXABYTES(x) ((x) / EXABYTES(1))
207#define TO_PETABYTES(x) ((x) / PETABYTES(1))
208#define TO_TERABYTES(x) ((x) / TERABYTES(1))
209#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
210#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
211#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
212
213static void cvtstr(double value, char *str, size_t size)
214{
215 char *trim;
216 const char *suffix;
217
218 if (value >= EXABYTES(1)) {
219 suffix = " EiB";
220 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
221 } else if (value >= PETABYTES(1)) {
222 suffix = " PiB";
223 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
224 } else if (value >= TERABYTES(1)) {
225 suffix = " TiB";
226 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
227 } else if (value >= GIGABYTES(1)) {
228 suffix = " GiB";
229 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
230 } else if (value >= MEGABYTES(1)) {
231 suffix = " MiB";
232 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
233 } else if (value >= KILOBYTES(1)) {
234 suffix = " KiB";
235 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
236 } else {
237 suffix = " bytes";
238 snprintf(str, size - 6, "%f", value);
239 }
240
241 trim = strstr(str, ".000");
242 if (trim) {
243 strcpy(trim, suffix);
244 } else {
245 strcat(str, suffix);
246 }
247}
248
249
250
Alex Bennée50290c02019-05-29 17:16:32 +0100251static struct timespec tsub(struct timespec t1, struct timespec t2)
Kevin Wolf0b613882013-06-05 14:19:38 +0200252{
Alex Bennée50290c02019-05-29 17:16:32 +0100253 t1.tv_nsec -= t2.tv_nsec;
254 if (t1.tv_nsec < 0) {
255 t1.tv_nsec += NANOSECONDS_PER_SECOND;
Kevin Wolf0b613882013-06-05 14:19:38 +0200256 t1.tv_sec--;
257 }
258 t1.tv_sec -= t2.tv_sec;
259 return t1;
260}
261
Alex Bennée50290c02019-05-29 17:16:32 +0100262static double tdiv(double value, struct timespec tv)
Kevin Wolf0b613882013-06-05 14:19:38 +0200263{
Alex Bennée50290c02019-05-29 17:16:32 +0100264 double seconds = tv.tv_sec + (tv.tv_nsec / 1e9);
265 return value / seconds;
Kevin Wolf0b613882013-06-05 14:19:38 +0200266}
267
268#define HOURS(sec) ((sec) / (60 * 60))
269#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
270#define SECONDS(sec) ((sec) % 60)
271
272enum {
273 DEFAULT_TIME = 0x0,
274 TERSE_FIXED_TIME = 0x1,
275 VERBOSE_FIXED_TIME = 0x2,
276};
277
Alex Bennée50290c02019-05-29 17:16:32 +0100278static void timestr(struct timespec *tv, char *ts, size_t size, int format)
Kevin Wolf0b613882013-06-05 14:19:38 +0200279{
Alex Bennée50290c02019-05-29 17:16:32 +0100280 double frac_sec = tv->tv_nsec / 1e9;
Kevin Wolf0b613882013-06-05 14:19:38 +0200281
282 if (format & TERSE_FIXED_TIME) {
283 if (!HOURS(tv->tv_sec)) {
Alex Bennée50290c02019-05-29 17:16:32 +0100284 snprintf(ts, size, "%u:%05.2f",
285 (unsigned int) MINUTES(tv->tv_sec),
286 SECONDS(tv->tv_sec) + frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200287 return;
288 }
289 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
290 }
291
292 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
Alex Bennée50290c02019-05-29 17:16:32 +0100293 snprintf(ts, size, "%u:%02u:%05.2f",
Kevin Wolf0b613882013-06-05 14:19:38 +0200294 (unsigned int) HOURS(tv->tv_sec),
295 (unsigned int) MINUTES(tv->tv_sec),
Alex Bennée50290c02019-05-29 17:16:32 +0100296 SECONDS(tv->tv_sec) + frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200297 } else {
Alex Bennée50290c02019-05-29 17:16:32 +0100298 snprintf(ts, size, "%05.2f sec", frac_sec);
Kevin Wolf0b613882013-06-05 14:19:38 +0200299 }
300}
301
Kevin Wolf797ac582013-06-05 14:19:31 +0200302/*
303 * Parse the pattern argument to various sub-commands.
304 *
305 * Because the pattern is used as an argument to memset it must evaluate
306 * to an unsigned integer that fits into a single byte.
307 */
308static int parse_pattern(const char *arg)
309{
310 char *endptr = NULL;
311 long pattern;
312
313 pattern = strtol(arg, &endptr, 0);
314 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
315 printf("%s is not a valid pattern byte\n", arg);
316 return -1;
317 }
318
319 return pattern;
320}
321
322/*
323 * Memory allocation helpers.
324 *
325 * Make sure memory is aligned by default, or purposefully misaligned if
326 * that is specified on the command line.
327 */
328
329#define MISALIGN_OFFSET 16
Max Reitz4c7b7e92015-02-05 13:58:22 -0500330static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
Kevin Wolf797ac582013-06-05 14:19:31 +0200331{
332 void *buf;
333
334 if (qemuio_misalign) {
335 len += MISALIGN_OFFSET;
336 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500337 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200338 memset(buf, pattern, len);
339 if (qemuio_misalign) {
340 buf += MISALIGN_OFFSET;
341 }
342 return buf;
343}
344
345static void qemu_io_free(void *p)
346{
347 if (qemuio_misalign) {
348 p -= MISALIGN_OFFSET;
349 }
350 qemu_vfree(p);
351}
352
Denis Plotnikov4d731512019-08-20 19:46:16 +0300353/*
354 * qemu_io_alloc_from_file()
355 *
356 * Allocates the buffer and populates it with the content of the given file
357 * up to @len bytes. If the file length is less than @len, then the buffer
358 * is populated with the file content cyclically.
359 *
360 * @blk - the block backend where the buffer content is going to be written to
361 * @len - the buffer length
362 * @file_name - the file to read the content from
363 *
364 * Returns: the buffer pointer on success
365 * NULL on error
366 */
367static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
368 const char *file_name)
369{
370 char *buf, *buf_origin;
371 FILE *f = fopen(file_name, "r");
372 int pattern_len;
373
374 if (!f) {
375 perror(file_name);
376 return NULL;
377 }
378
379 if (qemuio_misalign) {
380 len += MISALIGN_OFFSET;
381 }
382
383 buf_origin = buf = blk_blockalign(blk, len);
384
385 if (qemuio_misalign) {
386 buf_origin += MISALIGN_OFFSET;
387 buf += MISALIGN_OFFSET;
388 len -= MISALIGN_OFFSET;
389 }
390
391 pattern_len = fread(buf_origin, 1, len, f);
392
393 if (ferror(f)) {
394 perror(file_name);
395 goto error;
396 }
397
398 if (pattern_len == 0) {
399 fprintf(stderr, "%s: file is empty\n", file_name);
400 goto error;
401 }
402
403 fclose(f);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200404 f = NULL;
Denis Plotnikov4d731512019-08-20 19:46:16 +0300405
406 if (len > pattern_len) {
407 len -= pattern_len;
408 buf += pattern_len;
409
410 while (len > 0) {
411 size_t len_to_copy = MIN(pattern_len, len);
412
413 memcpy(buf, buf_origin, len_to_copy);
414
415 len -= len_to_copy;
416 buf += len_to_copy;
417 }
418 }
419
420 return buf_origin;
421
422error:
423 qemu_io_free(buf_origin);
Kevin Wolfc8e68b42019-09-10 09:03:06 +0200424 if (f) {
425 fclose(f);
426 }
Denis Plotnikov4d731512019-08-20 19:46:16 +0300427 return NULL;
428}
429
John Snow9b0beaf2015-11-05 18:53:02 -0500430static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200431{
John Snow9b0beaf2015-11-05 18:53:02 -0500432 uint64_t i;
433 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200434 const uint8_t *p;
435
436 for (i = 0, p = buffer; i < len; i += 16) {
437 const uint8_t *s = p;
438
439 printf("%08" PRIx64 ": ", offset + i);
440 for (j = 0; j < 16 && i + j < len; j++, p++) {
441 printf("%02x ", *p);
442 }
443 printf(" ");
444 for (j = 0; j < 16 && i + j < len; j++, s++) {
445 if (isalnum(*s)) {
446 printf("%c", *s);
447 } else {
448 printf(".");
449 }
450 }
451 printf("\n");
452 }
453}
454
Alex Bennée50290c02019-05-29 17:16:32 +0100455static void print_report(const char *op, struct timespec *t, int64_t offset,
Eric Blakedc388522016-05-07 21:16:42 -0600456 int64_t count, int64_t total, int cnt, bool Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200457{
458 char s1[64], s2[64], ts[64];
459
460 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
461 if (!Cflag) {
462 cvtstr((double)total, s1, sizeof(s1));
463 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500464 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200465 op, total, count, offset);
466 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
467 s1, cnt, ts, s2, tdiv((double)cnt, *t));
468 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500469 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200470 total, cnt, ts,
471 tdiv((double)total, *t),
472 tdiv((double)cnt, *t));
473 }
474}
475
476/*
477 * Parse multiple length statements for vectored I/O, and construct an I/O
478 * vector matching it.
479 */
480static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500481create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200482 int pattern)
483{
484 size_t *sizes = g_new0(size_t, nr_iov);
485 size_t count = 0;
486 void *buf = NULL;
487 void *p;
488 int i;
489
490 for (i = 0; i < nr_iov; i++) {
491 char *arg = argv[i];
492 int64_t len;
493
494 len = cvtnum(arg);
495 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500496 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200497 goto fail;
498 }
499
Alberto Garcia3026c462017-01-31 18:09:54 +0200500 if (len > BDRV_REQUEST_MAX_BYTES) {
501 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
502 (uint64_t)BDRV_REQUEST_MAX_BYTES);
503 goto fail;
504 }
505
506 if (count > BDRV_REQUEST_MAX_BYTES - len) {
507 printf("The total number of bytes exceed the maximum size %" PRIu64
508 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
Kevin Wolf797ac582013-06-05 14:19:31 +0200509 goto fail;
510 }
511
Kevin Wolf797ac582013-06-05 14:19:31 +0200512 sizes[i] = len;
513 count += len;
514 }
515
516 qemu_iovec_init(qiov, nr_iov);
517
Max Reitz4c7b7e92015-02-05 13:58:22 -0500518 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200519
520 for (i = 0; i < nr_iov; i++) {
521 qemu_iovec_add(qiov, p, sizes[i]);
522 p += sizes[i];
523 }
524
525fail:
526 g_free(sizes);
527 return buf;
528}
529
John Snow9b0beaf2015-11-05 18:53:02 -0500530static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300531 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200532{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300533 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500534 return -ERANGE;
535 }
536
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300537 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200538 if (*total < 0) {
539 return *total;
540 }
541 return 1;
542}
543
John Snow9b0beaf2015-11-05 18:53:02 -0500544static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300545 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200546{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300547 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500548 return -ERANGE;
549 }
550
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300551 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200552 if (*total < 0) {
553 return *total;
554 }
555 return 1;
556}
557
558typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500559 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200560 int64_t offset;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300561 int64_t bytes;
John Snow9b0beaf2015-11-05 18:53:02 -0500562 int64_t *total;
Eric Blake770e0e02016-05-07 21:16:44 -0600563 int flags;
Kevin Wolf797ac582013-06-05 14:19:31 +0200564 int ret;
565 bool done;
566} CoWriteZeroes;
567
Eric Blaked004bd52016-05-24 16:25:20 -0600568static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
Kevin Wolf797ac582013-06-05 14:19:31 +0200569{
570 CoWriteZeroes *data = opaque;
571
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300572 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
Eric Blaked004bd52016-05-24 16:25:20 -0600573 data->flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200574 data->done = true;
575 if (data->ret < 0) {
576 *data->total = data->ret;
577 return;
578 }
579
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300580 *data->total = data->bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200581}
582
Eric Blaked004bd52016-05-24 16:25:20 -0600583static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300584 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200585{
586 Coroutine *co;
587 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500588 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200589 .offset = offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300590 .bytes = bytes,
Kevin Wolf797ac582013-06-05 14:19:31 +0200591 .total = total,
Eric Blake770e0e02016-05-07 21:16:44 -0600592 .flags = flags,
Kevin Wolf797ac582013-06-05 14:19:31 +0200593 .done = false,
594 };
595
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300596 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500597 return -ERANGE;
598 }
599
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200600 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
Fam Zheng324ec3e2017-04-10 20:16:18 +0800601 bdrv_coroutine_enter(blk_bs(blk), co);
Kevin Wolf797ac582013-06-05 14:19:31 +0200602 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500603 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200604 }
605 if (data.ret < 0) {
606 return data.ret;
607 } else {
608 return 1;
609 }
610}
611
Max Reitz4c7b7e92015-02-05 13:58:22 -0500612static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300613 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200614{
615 int ret;
616
Alberto Garcia41ae31e2019-05-14 16:57:35 +0300617 if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500618 return -ERANGE;
619 }
620
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300621 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200622 if (ret < 0) {
623 return ret;
624 }
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300625 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200626 return 1;
627}
628
Max Reitz4c7b7e92015-02-05 13:58:22 -0500629static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500630 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200631{
John Snow9b0beaf2015-11-05 18:53:02 -0500632 if (count > INT_MAX) {
633 return -ERANGE;
634 }
635
Max Reitz4c7b7e92015-02-05 13:58:22 -0500636 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200637 if (*total < 0) {
638 return *total;
639 }
640 return 1;
641}
642
Max Reitz4c7b7e92015-02-05 13:58:22 -0500643static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500644 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200645{
John Snow9b0beaf2015-11-05 18:53:02 -0500646 if (count > INT_MAX) {
647 return -ERANGE;
648 }
649
Max Reitz4c7b7e92015-02-05 13:58:22 -0500650 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200651 if (*total < 0) {
652 return *total;
653 }
654 return 1;
655}
656
657#define NOT_DONE 0x7fffffff
658static void aio_rw_done(void *opaque, int ret)
659{
660 *(int *)opaque = ret;
661}
662
Max Reitz4c7b7e92015-02-05 13:58:22 -0500663static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200664 int64_t offset, int *total)
665{
666 int async_ret = NOT_DONE;
667
Eric Blake7b3f9712016-05-06 10:26:44 -0600668 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200669 while (async_ret == NOT_DONE) {
670 main_loop_wait(false);
671 }
672
673 *total = qiov->size;
674 return async_ret < 0 ? async_ret : 1;
675}
676
Max Reitz4c7b7e92015-02-05 13:58:22 -0500677static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600678 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200679{
680 int async_ret = NOT_DONE;
681
Eric Blake770e0e02016-05-07 21:16:44 -0600682 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200683 while (async_ret == NOT_DONE) {
684 main_loop_wait(false);
685 }
686
687 *total = qiov->size;
688 return async_ret < 0 ? async_ret : 1;
689}
690
Kevin Wolf797ac582013-06-05 14:19:31 +0200691static void read_help(void)
692{
693 printf(
694"\n"
695" reads a range of bytes from the given offset\n"
696"\n"
697" Example:\n"
698" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
699"\n"
700" Reads a segment of the currently open file, optionally dumping it to the\n"
701" standard output stream (with -v option) for subsequent inspection.\n"
702" -b, -- read from the VM state rather than the virtual disk\n"
703" -C, -- report statistics in a machine parsable format\n"
704" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600705" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200706" -P, -- use a pattern to verify read data\n"
707" -q, -- quiet mode, do not show I/O statistics\n"
708" -s, -- start offset for pattern verification (only with -P)\n"
709" -v, -- dump buffer to standard output\n"
710"\n");
711}
712
Max Reitzb32d7a32018-05-09 21:42:59 +0200713static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200714
715static const cmdinfo_t read_cmd = {
716 .name = "read",
717 .altname = "r",
718 .cfunc = read_f,
719 .argmin = 2,
720 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600721 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200722 .oneline = "reads a number of bytes at a specified offset",
723 .help = read_help,
724};
725
Max Reitzb32d7a32018-05-09 21:42:59 +0200726static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200727{
Alex Bennée50290c02019-05-29 17:16:32 +0100728 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600729 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600730 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200731 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200732 char *buf;
733 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500734 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200735 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500736 int64_t total = 0;
737 int pattern = 0;
738 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200739
Eric Blakeb062ad82015-05-12 09:10:56 -0600740 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200741 switch (c) {
742 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600743 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200744 break;
745 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600746 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200747 break;
748 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600749 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200750 pattern_count = cvtnum(optarg);
751 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500752 print_cvtnum_err(pattern_count, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200753 return pattern_count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200754 }
755 break;
756 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600757 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200758 break;
759 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600760 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200761 pattern = parse_pattern(optarg);
762 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200763 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200764 }
765 break;
766 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600767 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200768 break;
769 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600770 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200771 pattern_offset = cvtnum(optarg);
772 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500773 print_cvtnum_err(pattern_offset, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200774 return pattern_offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200775 }
776 break;
777 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600778 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200779 break;
780 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200781 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200782 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200783 }
784 }
785
786 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200787 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200788 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200789 }
790
Kevin Wolf797ac582013-06-05 14:19:31 +0200791 offset = cvtnum(argv[optind]);
792 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500793 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200794 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200795 }
796
797 optind++;
798 count = cvtnum(argv[optind]);
799 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500800 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200801 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +0200802 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500803 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200804 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200805 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200806 }
807
808 if (!Pflag && (lflag || sflag)) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200809 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200810 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200811 }
812
813 if (!lflag) {
814 pattern_count = count - pattern_offset;
815 }
816
817 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
818 printf("pattern verification range exceeds end of read data\n");
Max Reitzb32d7a32018-05-09 21:42:59 +0200819 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200820 }
821
Eric Blake093ea232016-05-07 21:16:43 -0600822 if (bflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -0500823 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
824 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200825 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +0200826 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200827 }
Eric Blake1bce6b42017-04-29 14:14:11 -0500828 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
829 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200830 count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200831 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200832 }
833 }
834
Max Reitz4c7b7e92015-02-05 13:58:22 -0500835 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200836
Alex Bennée50290c02019-05-29 17:16:32 +0100837 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -0600838 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200839 ret = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200840 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +0200841 ret = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200842 }
Alex Bennée50290c02019-05-29 17:16:32 +0100843 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200844
Max Reitzb32d7a32018-05-09 21:42:59 +0200845 if (ret < 0) {
846 printf("read failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200847 goto out;
848 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200849 cnt = ret;
850
851 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200852
853 if (Pflag) {
854 void *cmp_buf = g_malloc(pattern_count);
855 memset(cmp_buf, pattern, pattern_count);
856 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
857 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500858 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200859 offset + pattern_offset, pattern_count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200860 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200861 }
862 g_free(cmp_buf);
863 }
864
865 if (qflag) {
866 goto out;
867 }
868
869 if (vflag) {
870 dump_buffer(buf, offset, count);
871 }
872
873 /* Finally, report back -- -C gives a parsable format */
874 t2 = tsub(t2, t1);
875 print_report("read", &t2, offset, count, total, cnt, Cflag);
876
877out:
878 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +0200879 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200880}
881
882static void readv_help(void)
883{
884 printf(
885"\n"
886" reads a range of bytes from the given offset into multiple buffers\n"
887"\n"
888" Example:\n"
889" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
890"\n"
891" Reads a segment of the currently open file, optionally dumping it to the\n"
892" standard output stream (with -v option) for subsequent inspection.\n"
893" Uses multiple iovec buffers if more than one byte range is specified.\n"
894" -C, -- report statistics in a machine parsable format\n"
895" -P, -- use a pattern to verify read data\n"
896" -v, -- dump buffer to standard output\n"
897" -q, -- quiet mode, do not show I/O statistics\n"
898"\n");
899}
900
Max Reitzb32d7a32018-05-09 21:42:59 +0200901static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200902
903static const cmdinfo_t readv_cmd = {
904 .name = "readv",
905 .cfunc = readv_f,
906 .argmin = 2,
907 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600908 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200909 .oneline = "reads a number of bytes at a specified offset",
910 .help = readv_help,
911};
912
Max Reitzb32d7a32018-05-09 21:42:59 +0200913static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200914{
Alex Bennée50290c02019-05-29 17:16:32 +0100915 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600916 bool Cflag = false, qflag = false, vflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200917 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200918 char *buf;
919 int64_t offset;
920 /* Some compilers get confused and warn if this is not initialized. */
921 int total = 0;
922 int nr_iov;
923 QEMUIOVector qiov;
924 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600925 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200926
Eric Blakeb062ad82015-05-12 09:10:56 -0600927 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200928 switch (c) {
929 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600930 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200931 break;
932 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600933 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200934 pattern = parse_pattern(optarg);
935 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200936 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200937 }
938 break;
939 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600940 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200941 break;
942 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600943 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200944 break;
945 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200946 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200947 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200948 }
949 }
950
951 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200952 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200953 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200954 }
955
956
957 offset = cvtnum(argv[optind]);
958 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500959 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200960 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200961 }
962 optind++;
963
Kevin Wolf797ac582013-06-05 14:19:31 +0200964 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500965 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200966 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200967 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200968 }
969
Alex Bennée50290c02019-05-29 17:16:32 +0100970 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +0200971 ret = do_aio_readv(blk, &qiov, offset, &total);
Alex Bennée50290c02019-05-29 17:16:32 +0100972 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +0200973
Max Reitzb32d7a32018-05-09 21:42:59 +0200974 if (ret < 0) {
975 printf("readv failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200976 goto out;
977 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200978 cnt = ret;
979
980 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200981
982 if (Pflag) {
983 void *cmp_buf = g_malloc(qiov.size);
984 memset(cmp_buf, pattern, qiov.size);
985 if (memcmp(buf, cmp_buf, qiov.size)) {
986 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +0200987 PRId64 ", %zu bytes\n", offset, qiov.size);
Max Reitzb32d7a32018-05-09 21:42:59 +0200988 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200989 }
990 g_free(cmp_buf);
991 }
992
993 if (qflag) {
994 goto out;
995 }
996
997 if (vflag) {
998 dump_buffer(buf, offset, qiov.size);
999 }
1000
1001 /* Finally, report back -- -C gives a parsable format */
1002 t2 = tsub(t2, t1);
1003 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
1004
1005out:
1006 qemu_iovec_destroy(&qiov);
1007 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001008 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001009}
1010
1011static void write_help(void)
1012{
1013 printf(
1014"\n"
1015" writes a range of bytes from the given offset\n"
1016"\n"
1017" Example:\n"
1018" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
1019"\n"
1020" Writes into a segment of the currently open file, using a buffer\n"
1021" filled with a set pattern (0xcdcdcdcd).\n"
1022" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -05001023" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001024" -f, -- use Force Unit Access semantics\n"
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001025" -n, -- with -z, don't allow slow fallback\n"
Eric Blake093ea232016-05-07 21:16:43 -06001026" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001027" -P, -- use different pattern to fill file\n"
Denis Plotnikov4d731512019-08-20 19:46:16 +03001028" -s, -- use a pattern file to fill the write buffer\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001029" -C, -- report statistics in a machine parsable format\n"
1030" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001031" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001032" -z, -- write zeroes using blk_co_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001033"\n");
1034}
1035
Max Reitzb32d7a32018-05-09 21:42:59 +02001036static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001037
1038static const cmdinfo_t write_cmd = {
1039 .name = "write",
1040 .altname = "w",
1041 .cfunc = write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001042 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001043 .argmin = 2,
1044 .argmax = -1,
Denis Plotnikov4d731512019-08-20 19:46:16 +03001045 .args = "[-bcCfnquz] [-P pattern | -s source_file] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +02001046 .oneline = "writes a number of bytes at a specified offset",
1047 .help = write_help,
1048};
1049
Max Reitzb32d7a32018-05-09 21:42:59 +02001050static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001051{
Alex Bennée50290c02019-05-29 17:16:32 +01001052 struct timespec t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -06001053 bool Cflag = false, qflag = false, bflag = false;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001054 bool Pflag = false, zflag = false, cflag = false, sflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001055 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001056 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001057 char *buf = NULL;
1058 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -05001059 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001060 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -05001061 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001062 int pattern = 0xcd;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001063 const char *file_name = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001064
Denis Plotnikov4d731512019-08-20 19:46:16 +03001065 while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001066 switch (c) {
1067 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -06001068 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001069 break;
1070 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -06001071 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001072 break;
1073 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001074 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001075 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001076 case 'f':
1077 flags |= BDRV_REQ_FUA;
1078 break;
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001079 case 'n':
1080 flags |= BDRV_REQ_NO_FALLBACK;
1081 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001082 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -06001083 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +02001084 break;
1085 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001086 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001087 pattern = parse_pattern(optarg);
1088 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001089 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001090 }
1091 break;
1092 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001093 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001094 break;
Denis Plotnikov4d731512019-08-20 19:46:16 +03001095 case 's':
1096 sflag = true;
1097 file_name = optarg;
1098 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001099 case 'u':
1100 flags |= BDRV_REQ_MAY_UNMAP;
1101 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001102 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001103 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001104 break;
1105 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001106 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001107 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001108 }
1109 }
1110
1111 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001112 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001113 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001114 }
1115
Eric Blake093ea232016-05-07 21:16:43 -06001116 if (bflag && zflag) {
1117 printf("-b and -z cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001118 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001119 }
1120
Eric Blake770e0e02016-05-07 21:16:44 -06001121 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1122 printf("-f and -b or -c cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001123 return -EINVAL;
Eric Blake770e0e02016-05-07 21:16:44 -06001124 }
1125
Kevin Wolfc6e3f522019-03-22 13:53:03 +01001126 if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
1127 printf("-n requires -z to be specified\n");
1128 return -EINVAL;
1129 }
1130
Eric Blakec2e001c2016-05-07 21:16:45 -06001131 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1132 printf("-u requires -z to be specified\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001133 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001134 }
1135
Denis Plotnikov4d731512019-08-20 19:46:16 +03001136 if (zflag + Pflag + sflag > 1) {
1137 printf("Only one of -z, -P, and -s "
1138 "can be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001139 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001140 }
1141
1142 offset = cvtnum(argv[optind]);
1143 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001144 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001145 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001146 }
1147
1148 optind++;
1149 count = cvtnum(argv[optind]);
1150 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001151 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001152 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +02001153 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001154 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001155 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001156 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001157 }
1158
Eric Blake093ea232016-05-07 21:16:43 -06001159 if (bflag || cflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -05001160 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1161 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001162 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +02001163 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001164 }
1165
Eric Blake1bce6b42017-04-29 14:14:11 -05001166 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1167 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001168 count);
Max Reitzb32d7a32018-05-09 21:42:59 +02001169 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001170 }
1171 }
1172
1173 if (!zflag) {
Denis Plotnikov4d731512019-08-20 19:46:16 +03001174 if (sflag) {
1175 buf = qemu_io_alloc_from_file(blk, count, file_name);
1176 if (!buf) {
1177 return -EINVAL;
1178 }
1179 } else {
1180 buf = qemu_io_alloc(blk, count, pattern);
1181 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001182 }
1183
Alex Bennée50290c02019-05-29 17:16:32 +01001184 clock_gettime(CLOCK_MONOTONIC, &t1);
Eric Blake7b3f9712016-05-06 10:26:44 -06001185 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001186 ret = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001187 } else if (zflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001188 ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001189 } else if (cflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001190 ret = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001191 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +02001192 ret = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001193 }
Alex Bennée50290c02019-05-29 17:16:32 +01001194 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001195
Max Reitzb32d7a32018-05-09 21:42:59 +02001196 if (ret < 0) {
1197 printf("write failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001198 goto out;
1199 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001200 cnt = ret;
1201
1202 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001203
1204 if (qflag) {
1205 goto out;
1206 }
1207
1208 /* Finally, report back -- -C gives a parsable format */
1209 t2 = tsub(t2, t1);
1210 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1211
1212out:
1213 if (!zflag) {
1214 qemu_io_free(buf);
1215 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001216 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001217}
1218
1219static void
1220writev_help(void)
1221{
1222 printf(
1223"\n"
1224" writes a range of bytes from the given offset source from multiple buffers\n"
1225"\n"
1226" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001227" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001228"\n"
1229" Writes into a segment of the currently open file, using a buffer\n"
1230" filled with a set pattern (0xcdcdcdcd).\n"
1231" -P, -- use different pattern to fill file\n"
1232" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001233" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001234" -q, -- quiet mode, do not show I/O statistics\n"
1235"\n");
1236}
1237
Max Reitzb32d7a32018-05-09 21:42:59 +02001238static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001239
1240static const cmdinfo_t writev_cmd = {
1241 .name = "writev",
1242 .cfunc = writev_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001243 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001244 .argmin = 2,
1245 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001246 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001247 .oneline = "writes a number of bytes at a specified offset",
1248 .help = writev_help,
1249};
1250
Max Reitzb32d7a32018-05-09 21:42:59 +02001251static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001252{
Alex Bennée50290c02019-05-29 17:16:32 +01001253 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001254 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001255 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001256 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001257 char *buf;
1258 int64_t offset;
1259 /* Some compilers get confused and warn if this is not initialized. */
1260 int total = 0;
1261 int nr_iov;
1262 int pattern = 0xcd;
1263 QEMUIOVector qiov;
1264
Eric Blake4ca1d342016-05-16 10:43:01 -06001265 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001266 switch (c) {
1267 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001268 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001269 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001270 case 'f':
1271 flags |= BDRV_REQ_FUA;
1272 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001273 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001274 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001275 break;
1276 case 'P':
1277 pattern = parse_pattern(optarg);
1278 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001279 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001280 }
1281 break;
1282 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001283 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001284 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001285 }
1286 }
1287
1288 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001289 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001290 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001291 }
1292
1293 offset = cvtnum(argv[optind]);
1294 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001295 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001296 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001297 }
1298 optind++;
1299
Kevin Wolf797ac582013-06-05 14:19:31 +02001300 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001301 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001302 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001303 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001304 }
1305
Alex Bennée50290c02019-05-29 17:16:32 +01001306 clock_gettime(CLOCK_MONOTONIC, &t1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001307 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
Alex Bennée50290c02019-05-29 17:16:32 +01001308 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001309
Max Reitzb32d7a32018-05-09 21:42:59 +02001310 if (ret < 0) {
1311 printf("writev failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001312 goto out;
1313 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001314 cnt = ret;
1315
1316 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001317
1318 if (qflag) {
1319 goto out;
1320 }
1321
1322 /* Finally, report back -- -C gives a parsable format */
1323 t2 = tsub(t2, t1);
1324 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1325out:
1326 qemu_iovec_destroy(&qiov);
1327 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001328 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001329}
1330
Kevin Wolf797ac582013-06-05 14:19:31 +02001331struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001332 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001333 QEMUIOVector qiov;
1334 int64_t offset;
1335 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001336 bool qflag;
1337 bool vflag;
1338 bool Cflag;
1339 bool Pflag;
1340 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001341 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001342 int pattern;
Alex Bennée50290c02019-05-29 17:16:32 +01001343 struct timespec t1;
Kevin Wolf797ac582013-06-05 14:19:31 +02001344};
1345
1346static void aio_write_done(void *opaque, int ret)
1347{
1348 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001349 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001350
Alex Bennée50290c02019-05-29 17:16:32 +01001351 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001352
1353
1354 if (ret < 0) {
1355 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001356 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001357 goto out;
1358 }
1359
Max Reitz4c7b7e92015-02-05 13:58:22 -05001360 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001361
Kevin Wolf797ac582013-06-05 14:19:31 +02001362 if (ctx->qflag) {
1363 goto out;
1364 }
1365
1366 /* Finally, report back -- -C gives a parsable format */
1367 t2 = tsub(t2, ctx->t1);
1368 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1369 ctx->qiov.size, 1, ctx->Cflag);
1370out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001371 if (!ctx->zflag) {
1372 qemu_io_free(ctx->buf);
1373 qemu_iovec_destroy(&ctx->qiov);
1374 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001375 g_free(ctx);
1376}
1377
1378static void aio_read_done(void *opaque, int ret)
1379{
1380 struct aio_ctx *ctx = opaque;
Alex Bennée50290c02019-05-29 17:16:32 +01001381 struct timespec t2;
Kevin Wolf797ac582013-06-05 14:19:31 +02001382
Alex Bennée50290c02019-05-29 17:16:32 +01001383 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001384
1385 if (ret < 0) {
1386 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001387 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001388 goto out;
1389 }
1390
1391 if (ctx->Pflag) {
1392 void *cmp_buf = g_malloc(ctx->qiov.size);
1393
1394 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1395 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1396 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +02001397 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
Kevin Wolf797ac582013-06-05 14:19:31 +02001398 }
1399 g_free(cmp_buf);
1400 }
1401
Max Reitz4c7b7e92015-02-05 13:58:22 -05001402 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001403
Kevin Wolf797ac582013-06-05 14:19:31 +02001404 if (ctx->qflag) {
1405 goto out;
1406 }
1407
1408 if (ctx->vflag) {
1409 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1410 }
1411
1412 /* Finally, report back -- -C gives a parsable format */
1413 t2 = tsub(t2, ctx->t1);
1414 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1415 ctx->qiov.size, 1, ctx->Cflag);
1416out:
1417 qemu_io_free(ctx->buf);
1418 qemu_iovec_destroy(&ctx->qiov);
1419 g_free(ctx);
1420}
1421
1422static void aio_read_help(void)
1423{
1424 printf(
1425"\n"
1426" asynchronously reads a range of bytes from the given offset\n"
1427"\n"
1428" Example:\n"
1429" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1430"\n"
1431" Reads a segment of the currently open file, optionally dumping it to the\n"
1432" standard output stream (with -v option) for subsequent inspection.\n"
1433" The read is performed asynchronously and the aio_flush command must be\n"
1434" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001435" Note that due to its asynchronous nature, this command will be\n"
1436" considered successful once the request is submitted, independently\n"
1437" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001438" -C, -- report statistics in a machine parsable format\n"
1439" -P, -- use a pattern to verify read data\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001440" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001441" -v, -- dump buffer to standard output\n"
1442" -q, -- quiet mode, do not show I/O statistics\n"
1443"\n");
1444}
1445
Max Reitzb32d7a32018-05-09 21:42:59 +02001446static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001447
1448static const cmdinfo_t aio_read_cmd = {
1449 .name = "aio_read",
1450 .cfunc = aio_read_f,
1451 .argmin = 2,
1452 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001453 .args = "[-Ciqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001454 .oneline = "asynchronously reads a number of bytes",
1455 .help = aio_read_help,
1456};
1457
Max Reitzb32d7a32018-05-09 21:42:59 +02001458static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001459{
1460 int nr_iov, c;
1461 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1462
Max Reitz4c7b7e92015-02-05 13:58:22 -05001463 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001464 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001465 switch (c) {
1466 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001467 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001468 break;
1469 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001470 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001471 ctx->pattern = parse_pattern(optarg);
1472 if (ctx->pattern < 0) {
1473 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001474 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001475 }
1476 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001477 case 'i':
1478 printf("injecting invalid read request\n");
1479 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1480 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001481 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001482 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001483 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001484 break;
1485 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001486 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001487 break;
1488 default:
1489 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001490 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001491 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001492 }
1493 }
1494
1495 if (optind > argc - 2) {
1496 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001497 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001498 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001499 }
1500
1501 ctx->offset = cvtnum(argv[optind]);
1502 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001503 int ret = ctx->offset;
1504 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001505 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001506 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001507 }
1508 optind++;
1509
Kevin Wolf797ac582013-06-05 14:19:31 +02001510 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001511 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001512 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001513 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001514 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001515 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001516 }
1517
Alex Bennée50290c02019-05-29 17:16:32 +01001518 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001519 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1520 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001521 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001522 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001523}
1524
1525static void aio_write_help(void)
1526{
1527 printf(
1528"\n"
1529" asynchronously writes a range of bytes from the given offset source\n"
1530" from multiple buffers\n"
1531"\n"
1532" Example:\n"
1533" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1534"\n"
1535" Writes into a segment of the currently open file, using a buffer\n"
1536" filled with a set pattern (0xcdcdcdcd).\n"
1537" The write is performed asynchronously and the aio_flush command must be\n"
1538" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001539" Note that due to its asynchronous nature, this command will be\n"
1540" considered successful once the request is submitted, independently\n"
1541" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001542" -P, -- use different pattern to fill file\n"
1543" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001544" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001545" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001546" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001547" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001548" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001549"\n");
1550}
1551
Max Reitzb32d7a32018-05-09 21:42:59 +02001552static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001553
1554static const cmdinfo_t aio_write_cmd = {
1555 .name = "aio_write",
1556 .cfunc = aio_write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001557 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001558 .argmin = 2,
1559 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001560 .args = "[-Cfiquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001561 .oneline = "asynchronously writes a number of bytes",
1562 .help = aio_write_help,
1563};
1564
Max Reitzb32d7a32018-05-09 21:42:59 +02001565static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001566{
1567 int nr_iov, c;
1568 int pattern = 0xcd;
1569 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001570 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001571
Max Reitz4c7b7e92015-02-05 13:58:22 -05001572 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001573 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001574 switch (c) {
1575 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001576 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001577 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001578 case 'f':
1579 flags |= BDRV_REQ_FUA;
1580 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001581 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001582 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001583 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001584 case 'u':
1585 flags |= BDRV_REQ_MAY_UNMAP;
1586 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001587 case 'P':
1588 pattern = parse_pattern(optarg);
1589 if (pattern < 0) {
1590 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001591 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001592 }
1593 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001594 case 'i':
1595 printf("injecting invalid write request\n");
1596 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1597 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001598 return 0;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001599 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001600 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001601 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001602 default:
1603 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001604 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001605 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001606 }
1607 }
1608
1609 if (optind > argc - 2) {
1610 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001611 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001612 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001613 }
1614
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001615 if (ctx->zflag && optind != argc - 2) {
1616 printf("-z supports only a single length parameter\n");
1617 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001618 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001619 }
1620
Eric Blakec2e001c2016-05-07 21:16:45 -06001621 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1622 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001623 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001624 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001625 }
1626
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001627 if (ctx->zflag && ctx->Pflag) {
1628 printf("-z and -P cannot be specified at the same time\n");
1629 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001630 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001631 }
1632
Kevin Wolf797ac582013-06-05 14:19:31 +02001633 ctx->offset = cvtnum(argv[optind]);
1634 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001635 int ret = ctx->offset;
1636 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001637 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001638 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001639 }
1640 optind++;
1641
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001642 if (ctx->zflag) {
1643 int64_t count = cvtnum(argv[optind]);
1644 if (count < 0) {
1645 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001646 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001647 return count;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001648 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001649
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001650 ctx->qiov.size = count;
Eric Blaked004bd52016-05-24 16:25:20 -06001651 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1652 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001653 } else {
1654 nr_iov = argc - optind;
1655 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1656 pattern);
1657 if (ctx->buf == NULL) {
1658 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1659 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001660 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001661 }
1662
Alex Bennée50290c02019-05-29 17:16:32 +01001663 clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001664 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1665 BLOCK_ACCT_WRITE);
1666
Eric Blake770e0e02016-05-07 21:16:44 -06001667 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1668 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001669 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001670
1671 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001672}
1673
Max Reitzb32d7a32018-05-09 21:42:59 +02001674static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001675{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001676 BlockAcctCookie cookie;
1677 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001678 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001679 block_acct_done(blk_get_stats(blk), &cookie);
Max Reitzb32d7a32018-05-09 21:42:59 +02001680 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001681}
1682
1683static const cmdinfo_t aio_flush_cmd = {
1684 .name = "aio_flush",
1685 .cfunc = aio_flush_f,
1686 .oneline = "completes all outstanding aio requests"
1687};
1688
Max Reitzb32d7a32018-05-09 21:42:59 +02001689static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001690{
Max Reitzb32d7a32018-05-09 21:42:59 +02001691 return blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001692}
1693
1694static const cmdinfo_t flush_cmd = {
1695 .name = "flush",
1696 .altname = "f",
1697 .cfunc = flush_f,
1698 .oneline = "flush all in-core file state to disk",
1699};
1700
Max Reitzb32d7a32018-05-09 21:42:59 +02001701static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001702{
Max Reitzed3d2ec2017-03-28 22:51:27 +02001703 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001704 int64_t offset;
1705 int ret;
1706
1707 offset = cvtnum(argv[1]);
1708 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001709 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001710 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001711 }
1712
Max Reitz3a691c52017-06-13 22:20:54 +02001713 ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
Kevin Wolf797ac582013-06-05 14:19:31 +02001714 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001715 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02001716 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001717 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001718
1719 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001720}
1721
1722static const cmdinfo_t truncate_cmd = {
1723 .name = "truncate",
1724 .altname = "t",
1725 .cfunc = truncate_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001726 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001727 .argmin = 1,
1728 .argmax = 1,
1729 .args = "off",
1730 .oneline = "truncates the current file at the given offset",
1731};
1732
Max Reitzb32d7a32018-05-09 21:42:59 +02001733static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001734{
1735 int64_t size;
1736 char s1[64];
1737
Max Reitz4c7b7e92015-02-05 13:58:22 -05001738 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001739 if (size < 0) {
1740 printf("getlength: %s\n", strerror(-size));
Max Reitzb32d7a32018-05-09 21:42:59 +02001741 return size;
Kevin Wolf797ac582013-06-05 14:19:31 +02001742 }
1743
1744 cvtstr(size, s1, sizeof(s1));
1745 printf("%s\n", s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001746 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001747}
1748
1749
1750static const cmdinfo_t length_cmd = {
1751 .name = "length",
1752 .altname = "l",
1753 .cfunc = length_f,
1754 .oneline = "gets the length of the current file",
1755};
1756
1757
Max Reitzb32d7a32018-05-09 21:42:59 +02001758static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001759{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001760 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001761 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001762 ImageInfoSpecific *spec_info;
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001763 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001764 char s1[64], s2[64];
1765 int ret;
1766
1767 if (bs->drv && bs->drv->format_name) {
1768 printf("format name: %s\n", bs->drv->format_name);
1769 }
1770 if (bs->drv && bs->drv->protocol_name) {
1771 printf("format name: %s\n", bs->drv->protocol_name);
1772 }
1773
1774 ret = bdrv_get_info(bs, &bdi);
1775 if (ret) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001776 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001777 }
1778
1779 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1780 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1781
1782 printf("cluster size: %s\n", s1);
1783 printf("vm state offset: %s\n", s2);
1784
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +03001785 spec_info = bdrv_get_specific_info(bs, &local_err);
1786 if (local_err) {
1787 error_report_err(local_err);
1788 return -EIO;
1789 }
Max Reitza8d8ecb2013-10-09 10:46:17 +02001790 if (spec_info) {
1791 printf("Format specific information:\n");
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02001792 bdrv_image_info_specific_dump(spec_info);
Max Reitza8d8ecb2013-10-09 10:46:17 +02001793 qapi_free_ImageInfoSpecific(spec_info);
1794 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001795
1796 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001797}
1798
1799
1800
1801static const cmdinfo_t info_cmd = {
1802 .name = "info",
1803 .altname = "i",
1804 .cfunc = info_f,
1805 .oneline = "prints information about the current file",
1806};
1807
1808static void discard_help(void)
1809{
1810 printf(
1811"\n"
1812" discards a range of bytes from the given offset\n"
1813"\n"
1814" Example:\n"
1815" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1816"\n"
1817" Discards a segment of the currently open file.\n"
1818" -C, -- report statistics in a machine parsable format\n"
1819" -q, -- quiet mode, do not show I/O statistics\n"
1820"\n");
1821}
1822
Max Reitzb32d7a32018-05-09 21:42:59 +02001823static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001824
1825static const cmdinfo_t discard_cmd = {
1826 .name = "discard",
1827 .altname = "d",
1828 .cfunc = discard_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001829 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001830 .argmin = 2,
1831 .argmax = -1,
1832 .args = "[-Cq] off len",
1833 .oneline = "discards a number of bytes at a specified offset",
1834 .help = discard_help,
1835};
1836
Max Reitzb32d7a32018-05-09 21:42:59 +02001837static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001838{
Alex Bennée50290c02019-05-29 17:16:32 +01001839 struct timespec t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001840 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001841 int c, ret;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001842 int64_t offset, bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +02001843
Eric Blakeb062ad82015-05-12 09:10:56 -06001844 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001845 switch (c) {
1846 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001847 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001848 break;
1849 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001850 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001851 break;
1852 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001853 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001854 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001855 }
1856 }
1857
1858 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001859 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001860 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001861 }
1862
1863 offset = cvtnum(argv[optind]);
1864 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001865 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001866 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001867 }
1868
1869 optind++;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001870 bytes = cvtnum(argv[optind]);
1871 if (bytes < 0) {
1872 print_cvtnum_err(bytes, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001873 return bytes;
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001874 } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001875 printf("length cannot exceed %"PRIu64", given %s\n",
Alberto Garcia41ae31e2019-05-14 16:57:35 +03001876 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001877 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001878 }
1879
Alex Bennée50290c02019-05-29 17:16:32 +01001880 clock_gettime(CLOCK_MONOTONIC, &t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001881 ret = blk_pdiscard(blk, offset, bytes);
Alex Bennée50290c02019-05-29 17:16:32 +01001882 clock_gettime(CLOCK_MONOTONIC, &t2);
Kevin Wolf797ac582013-06-05 14:19:31 +02001883
1884 if (ret < 0) {
1885 printf("discard failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001886 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001887 }
1888
1889 /* Finally, report back -- -C gives a parsable format */
1890 if (!qflag) {
1891 t2 = tsub(t2, t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001892 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
Kevin Wolf797ac582013-06-05 14:19:31 +02001893 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001894
1895 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001896}
1897
Max Reitzb32d7a32018-05-09 21:42:59 +02001898static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001899{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001900 BlockDriverState *bs = blk_bs(blk);
Eric Blaked6a644b2017-07-07 07:44:57 -05001901 int64_t offset, start, remaining, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001902 char s1[64];
Eric Blaked6a644b2017-07-07 07:44:57 -05001903 int ret;
1904 int64_t num, sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001905
Eric Blaked6a644b2017-07-07 07:44:57 -05001906 start = offset = cvtnum(argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001907 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001908 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001909 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001910 }
1911
1912 if (argc == 3) {
Eric Blake4401fdc2017-04-29 14:14:12 -05001913 count = cvtnum(argv[2]);
1914 if (count < 0) {
1915 print_cvtnum_err(count, argv[2]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001916 return count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001917 }
1918 } else {
Eric Blake4401fdc2017-04-29 14:14:12 -05001919 count = BDRV_SECTOR_SIZE;
Kevin Wolf797ac582013-06-05 14:19:31 +02001920 }
1921
Eric Blaked6a644b2017-07-07 07:44:57 -05001922 remaining = count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001923 sum_alloc = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001924 while (remaining) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001925 ret = bdrv_is_allocated(bs, offset, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001926 if (ret < 0) {
1927 printf("is_allocated failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001928 return ret;
Paolo Bonzinid6636402013-09-04 19:00:25 +02001929 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001930 offset += num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001931 remaining -= num;
1932 if (ret) {
1933 sum_alloc += num;
1934 }
1935 if (num == 0) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001936 count -= remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001937 remaining = 0;
1938 }
1939 }
1940
Eric Blaked6a644b2017-07-07 07:44:57 -05001941 cvtstr(start, s1, sizeof(s1));
Kevin Wolf797ac582013-06-05 14:19:31 +02001942
Eric Blake4401fdc2017-04-29 14:14:12 -05001943 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001944 sum_alloc, count, s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001945 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001946}
1947
1948static const cmdinfo_t alloc_cmd = {
1949 .name = "alloc",
1950 .altname = "a",
1951 .argmin = 1,
1952 .argmax = 2,
1953 .cfunc = alloc_f,
Eric Blake4401fdc2017-04-29 14:14:12 -05001954 .args = "offset [count]",
1955 .oneline = "checks if offset is allocated in the file",
Kevin Wolf797ac582013-06-05 14:19:31 +02001956};
1957
1958
Eric Blaked6a644b2017-07-07 07:44:57 -05001959static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1960 int64_t bytes, int64_t *pnum)
Kevin Wolf797ac582013-06-05 14:19:31 +02001961{
Eric Blaked6a644b2017-07-07 07:44:57 -05001962 int64_t num;
1963 int num_checked;
Kevin Wolf797ac582013-06-05 14:19:31 +02001964 int ret, firstret;
1965
Eric Blaked6a644b2017-07-07 07:44:57 -05001966 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1967 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001968 if (ret < 0) {
1969 return ret;
1970 }
1971
1972 firstret = ret;
1973 *pnum = num;
1974
Eric Blaked6a644b2017-07-07 07:44:57 -05001975 while (bytes > 0 && ret == firstret) {
1976 offset += num;
1977 bytes -= num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001978
Eric Blaked6a644b2017-07-07 07:44:57 -05001979 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1980 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02001981 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001982 *pnum += num;
1983 } else {
1984 break;
1985 }
1986 }
1987
1988 return firstret;
1989}
1990
Max Reitzb32d7a32018-05-09 21:42:59 +02001991static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001992{
Eric Blaked6a644b2017-07-07 07:44:57 -05001993 int64_t offset, bytes;
Eric Blake6f3c90a2017-04-29 14:14:13 -05001994 char s1[64], s2[64];
Kevin Wolf797ac582013-06-05 14:19:31 +02001995 int64_t num;
1996 int ret;
1997 const char *retstr;
1998
1999 offset = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05002000 bytes = blk_getlength(blk);
2001 if (bytes < 0) {
2002 error_report("Failed to query image length: %s", strerror(-bytes));
Max Reitzb32d7a32018-05-09 21:42:59 +02002003 return bytes;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002004 }
2005
Eric Blaked6a644b2017-07-07 07:44:57 -05002006 while (bytes) {
2007 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002008 if (ret < 0) {
2009 error_report("Failed to get allocation status: %s", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002010 return ret;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002011 } else if (!num) {
2012 error_report("Unexpected end of image");
Max Reitzb32d7a32018-05-09 21:42:59 +02002013 return -EIO;
Kevin Wolf797ac582013-06-05 14:19:31 +02002014 }
2015
2016 retstr = ret ? " allocated" : "not allocated";
Eric Blaked6a644b2017-07-07 07:44:57 -05002017 cvtstr(num, s1, sizeof(s1));
2018 cvtstr(offset, s2, sizeof(s2));
Eric Blake6f3c90a2017-04-29 14:14:13 -05002019 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05002020 s1, num, retstr, s2, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02002021
2022 offset += num;
Eric Blaked6a644b2017-07-07 07:44:57 -05002023 bytes -= num;
2024 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002025
2026 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002027}
2028
2029static const cmdinfo_t map_cmd = {
2030 .name = "map",
2031 .argmin = 0,
2032 .argmax = 0,
2033 .cfunc = map_f,
2034 .args = "",
2035 .oneline = "prints the allocated areas of a file",
2036};
2037
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002038static void reopen_help(void)
2039{
2040 printf(
2041"\n"
2042" Changes the open options of an already opened image\n"
2043"\n"
2044" Example:\n"
2045" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2046"\n"
2047" -r, -- Reopen the image read-only\n"
Kevin Wolfea922032017-08-03 17:03:00 +02002048" -w, -- Reopen the image read-write\n"
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002049" -c, -- Change the cache mode to the given value\n"
2050" -o, -- Changes block driver options (cf. 'open' command)\n"
2051"\n");
2052}
2053
Max Reitzb32d7a32018-05-09 21:42:59 +02002054static int reopen_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002055
2056static QemuOptsList reopen_opts = {
2057 .name = "reopen",
2058 .merge_lists = true,
2059 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2060 .desc = {
2061 /* no elements => accept any params */
2062 { /* end of list */ }
2063 },
2064};
2065
2066static const cmdinfo_t reopen_cmd = {
2067 .name = "reopen",
2068 .argmin = 0,
2069 .argmax = -1,
2070 .cfunc = reopen_f,
Kevin Wolfea922032017-08-03 17:03:00 +02002071 .args = "[(-r|-w)] [-c cache] [-o options]",
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002072 .oneline = "reopens an image with new options",
2073 .help = reopen_help,
2074};
2075
Max Reitzb32d7a32018-05-09 21:42:59 +02002076static int reopen_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002077{
2078 BlockDriverState *bs = blk_bs(blk);
2079 QemuOpts *qopts;
2080 QDict *opts;
2081 int c;
2082 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002083 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolfea922032017-08-03 17:03:00 +02002084 bool has_rw_option = false;
Alberto Garciadc900c32018-11-12 16:00:42 +02002085 bool has_cache_option = false;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002086
2087 BlockReopenQueue *brq;
2088 Error *local_err = NULL;
2089
Kevin Wolfea922032017-08-03 17:03:00 +02002090 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002091 switch (c) {
2092 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002093 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002094 error_report("Invalid cache option: %s", optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +02002095 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002096 }
Alberto Garciadc900c32018-11-12 16:00:42 +02002097 has_cache_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002098 break;
2099 case 'o':
2100 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2101 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002102 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002103 }
2104 break;
2105 case 'r':
Kevin Wolfea922032017-08-03 17:03:00 +02002106 if (has_rw_option) {
2107 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002108 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002109 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002110 flags &= ~BDRV_O_RDWR;
Kevin Wolfea922032017-08-03 17:03:00 +02002111 has_rw_option = true;
2112 break;
2113 case 'w':
2114 if (has_rw_option) {
2115 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002116 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002117 }
2118 flags |= BDRV_O_RDWR;
2119 has_rw_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002120 break;
2121 default:
2122 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002123 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002124 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002125 }
2126 }
2127
2128 if (optind != argc) {
2129 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002130 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002131 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002132 }
2133
Alberto Garciaa8003ec2018-09-06 12:37:01 +03002134 if (!writethrough != blk_enable_write_cache(blk) &&
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002135 blk_get_attached_dev(blk))
2136 {
2137 error_report("Cannot change cache.writeback: Device attached");
2138 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002139 return -EBUSY;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002140 }
2141
Kevin Wolff3adefb2017-09-22 14:50:12 +02002142 if (!(flags & BDRV_O_RDWR)) {
2143 uint64_t orig_perm, orig_shared_perm;
2144
2145 bdrv_drain(bs);
2146
2147 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2148 blk_set_perm(blk,
2149 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2150 orig_shared_perm,
2151 &error_abort);
2152 }
2153
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002154 qopts = qemu_opts_find(&reopen_opts, NULL);
Alberto Garciadc900c32018-11-12 16:00:42 +02002155 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002156 qemu_opts_reset(&reopen_opts);
2157
Alberto Garciadc900c32018-11-12 16:00:42 +02002158 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2159 if (has_rw_option) {
2160 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2161 qobject_unref(opts);
2162 return -EINVAL;
2163 }
2164 } else {
2165 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2166 }
2167
2168 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2169 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2170 if (has_cache_option) {
2171 error_report("Cannot set both -c and the cache options");
2172 qobject_unref(opts);
2173 return -EINVAL;
2174 }
2175 } else {
2176 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2177 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2178 }
2179
Kevin Wolf1a63a902017-12-06 20:24:44 +01002180 bdrv_subtree_drained_begin(bs);
Alberto Garcia077e8e22019-03-12 18:48:44 +02002181 brq = bdrv_reopen_queue(NULL, bs, opts, true);
Alberto Garcia5019aec2019-03-12 18:48:50 +02002182 bdrv_reopen_multiple(brq, &local_err);
Kevin Wolf1a63a902017-12-06 20:24:44 +01002183 bdrv_subtree_drained_end(bs);
2184
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002185 if (local_err) {
2186 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02002187 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002188 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002189
2190 blk_set_enable_write_cache(blk, !writethrough);
2191 return 0;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002192}
2193
Max Reitzb32d7a32018-05-09 21:42:59 +02002194static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002195{
2196 int ret;
2197
Max Reitz4c7b7e92015-02-05 13:58:22 -05002198 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002199 if (ret < 0) {
2200 printf("Could not set breakpoint: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002201 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002202 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002203
2204 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002205}
2206
Max Reitzb32d7a32018-05-09 21:42:59 +02002207static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002208{
2209 int ret;
2210
Max Reitz4c7b7e92015-02-05 13:58:22 -05002211 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002212 if (ret < 0) {
2213 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002214 return ret;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002215 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002216
2217 return 0;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002218}
2219
Kevin Wolf797ac582013-06-05 14:19:31 +02002220static const cmdinfo_t break_cmd = {
2221 .name = "break",
2222 .argmin = 2,
2223 .argmax = 2,
2224 .cfunc = break_f,
2225 .args = "event tag",
2226 .oneline = "sets a breakpoint on event and tags the stopped "
2227 "request as tag",
2228};
2229
Fam Zheng4cc70e92013-11-20 10:01:54 +08002230static const cmdinfo_t remove_break_cmd = {
2231 .name = "remove_break",
2232 .argmin = 1,
2233 .argmax = 1,
2234 .cfunc = remove_break_f,
2235 .args = "tag",
2236 .oneline = "remove a breakpoint by tag",
2237};
2238
Max Reitzb32d7a32018-05-09 21:42:59 +02002239static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002240{
2241 int ret;
2242
Max Reitz4c7b7e92015-02-05 13:58:22 -05002243 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002244 if (ret < 0) {
2245 printf("Could not resume request: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002246 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002247 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002248
2249 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002250}
2251
2252static const cmdinfo_t resume_cmd = {
2253 .name = "resume",
2254 .argmin = 1,
2255 .argmax = 1,
2256 .cfunc = resume_f,
2257 .args = "tag",
2258 .oneline = "resumes the request tagged as tag",
2259};
2260
Max Reitzb32d7a32018-05-09 21:42:59 +02002261static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002262{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002263 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2264 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002265 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002266 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002267}
2268
2269static const cmdinfo_t wait_break_cmd = {
2270 .name = "wait_break",
2271 .argmin = 1,
2272 .argmax = 1,
2273 .cfunc = wait_break_f,
2274 .args = "tag",
2275 .oneline = "waits for the suspension of a request",
2276};
2277
Max Reitzb32d7a32018-05-09 21:42:59 +02002278static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002279{
2280 abort();
2281}
2282
2283static const cmdinfo_t abort_cmd = {
2284 .name = "abort",
2285 .cfunc = abort_f,
2286 .flags = CMD_NOFILE_OK,
2287 .oneline = "simulate a program crash using abort(3)",
2288};
2289
Max Reitz0e82dc72014-12-08 10:48:10 +01002290static void sigraise_help(void)
2291{
2292 printf(
2293"\n"
2294" raises the given signal\n"
2295"\n"
2296" Example:\n"
2297" 'sigraise %i' - raises SIGTERM\n"
2298"\n"
2299" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2300" given to sigraise.\n"
2301"\n", SIGTERM);
2302}
2303
Max Reitzb32d7a32018-05-09 21:42:59 +02002304static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002305
2306static const cmdinfo_t sigraise_cmd = {
2307 .name = "sigraise",
2308 .cfunc = sigraise_f,
2309 .argmin = 1,
2310 .argmax = 1,
2311 .flags = CMD_NOFILE_OK,
2312 .args = "signal",
2313 .oneline = "raises a signal",
2314 .help = sigraise_help,
2315};
2316
Max Reitzb32d7a32018-05-09 21:42:59 +02002317static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002318{
John Snow9b0beaf2015-11-05 18:53:02 -05002319 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002320 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002321 print_cvtnum_err(sig, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002322 return sig;
John Snow9b0beaf2015-11-05 18:53:02 -05002323 } else if (sig > NSIG) {
2324 printf("signal argument '%s' is too large to be a valid signal\n",
2325 argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002326 return -EINVAL;
Max Reitz0e82dc72014-12-08 10:48:10 +01002327 }
2328
2329 /* Using raise() to kill this process does not necessarily flush all open
2330 * streams. At least stdout and stderr (although the latter should be
2331 * non-buffered anyway) should be flushed, though. */
2332 fflush(stdout);
2333 fflush(stderr);
2334
2335 raise(sig);
Max Reitzb32d7a32018-05-09 21:42:59 +02002336
2337 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002338}
2339
Kevin Wolfcd33d022014-01-15 15:39:10 +01002340static void sleep_cb(void *opaque)
2341{
2342 bool *expired = opaque;
2343 *expired = true;
2344}
2345
Max Reitzb32d7a32018-05-09 21:42:59 +02002346static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002347{
2348 char *endptr;
2349 long ms;
2350 struct QEMUTimer *timer;
2351 bool expired = false;
2352
2353 ms = strtol(argv[1], &endptr, 0);
2354 if (ms < 0 || *endptr != '\0') {
2355 printf("%s is not a valid number\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002356 return -EINVAL;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002357 }
2358
2359 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2360 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2361
2362 while (!expired) {
2363 main_loop_wait(false);
2364 }
2365
2366 timer_free(timer);
Max Reitzb32d7a32018-05-09 21:42:59 +02002367 return 0;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002368}
2369
2370static const cmdinfo_t sleep_cmd = {
2371 .name = "sleep",
2372 .argmin = 1,
2373 .argmax = 1,
2374 .cfunc = sleep_f,
2375 .flags = CMD_NOFILE_OK,
2376 .oneline = "waits for the given value in milliseconds",
2377};
2378
Kevin Wolff18a8342013-06-05 14:19:33 +02002379static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2380{
2381 if (cmd) {
2382 printf("%s ", cmd);
2383 } else {
2384 printf("%s ", ct->name);
2385 if (ct->altname) {
2386 printf("(or %s) ", ct->altname);
2387 }
2388 }
2389
2390 if (ct->args) {
2391 printf("%s ", ct->args);
2392 }
2393 printf("-- %s\n", ct->oneline);
2394}
2395
2396static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2397{
2398 help_oneline(cmd, ct);
2399 if (ct->help) {
2400 ct->help();
2401 }
2402}
2403
2404static void help_all(void)
2405{
2406 const cmdinfo_t *ct;
2407
2408 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2409 help_oneline(ct->name, ct);
2410 }
2411 printf("\nUse 'help commandname' for extended help.\n");
2412}
2413
Max Reitzb32d7a32018-05-09 21:42:59 +02002414static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002415{
2416 const cmdinfo_t *ct;
2417
2418 if (argc == 1) {
2419 help_all();
Max Reitzb32d7a32018-05-09 21:42:59 +02002420 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002421 }
2422
2423 ct = find_command(argv[1]);
2424 if (ct == NULL) {
2425 printf("command %s not found\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002426 return -EINVAL;
Kevin Wolff18a8342013-06-05 14:19:33 +02002427 }
2428
2429 help_onecmd(argv[1], ct);
Max Reitzb32d7a32018-05-09 21:42:59 +02002430 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002431}
2432
2433static const cmdinfo_t help_cmd = {
2434 .name = "help",
2435 .altname = "?",
2436 .cfunc = help_f,
2437 .argmin = 0,
2438 .argmax = 1,
2439 .flags = CMD_FLAG_GLOBAL,
2440 .args = "[command]",
2441 .oneline = "help for one or all commands",
2442};
2443
Max Reitzb32d7a32018-05-09 21:42:59 +02002444int qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002445{
Paolo Bonzini15afd942016-10-27 12:49:03 +02002446 AioContext *ctx;
Kevin Wolfdd583292013-06-05 14:19:32 +02002447 char *input;
2448 const cmdinfo_t *ct;
2449 char **v;
2450 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +02002451 int ret = 0;
Kevin Wolfdd583292013-06-05 14:19:32 +02002452
2453 input = g_strdup(cmd);
2454 v = breakline(input, &c);
2455 if (c) {
2456 ct = find_command(v[0]);
2457 if (ct) {
Paolo Bonzini15afd942016-10-27 12:49:03 +02002458 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2459 aio_context_acquire(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02002460 ret = command(blk, ct, c, v);
Paolo Bonzini15afd942016-10-27 12:49:03 +02002461 aio_context_release(ctx);
Kevin Wolfdd583292013-06-05 14:19:32 +02002462 } else {
2463 fprintf(stderr, "command \"%s\" not found\n", v[0]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002464 ret = -EINVAL;
Kevin Wolfdd583292013-06-05 14:19:32 +02002465 }
2466 }
2467 g_free(input);
2468 g_free(v);
Max Reitzb32d7a32018-05-09 21:42:59 +02002469
2470 return ret;
Kevin Wolfdd583292013-06-05 14:19:32 +02002471}
2472
Kevin Wolf797ac582013-06-05 14:19:31 +02002473static void __attribute((constructor)) init_qemuio_commands(void)
2474{
2475 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002476 qemuio_add_command(&help_cmd);
2477 qemuio_add_command(&read_cmd);
2478 qemuio_add_command(&readv_cmd);
2479 qemuio_add_command(&write_cmd);
2480 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002481 qemuio_add_command(&aio_read_cmd);
2482 qemuio_add_command(&aio_write_cmd);
2483 qemuio_add_command(&aio_flush_cmd);
2484 qemuio_add_command(&flush_cmd);
2485 qemuio_add_command(&truncate_cmd);
2486 qemuio_add_command(&length_cmd);
2487 qemuio_add_command(&info_cmd);
2488 qemuio_add_command(&discard_cmd);
2489 qemuio_add_command(&alloc_cmd);
2490 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002491 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002492 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002493 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002494 qemuio_add_command(&resume_cmd);
2495 qemuio_add_command(&wait_break_cmd);
2496 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002497 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002498 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002499}