blob: ee8f56e46aef34b077ce688d6ea3dc7e19c19f4e [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
251static struct timeval tsub(struct timeval t1, struct timeval t2)
252{
253 t1.tv_usec -= t2.tv_usec;
254 if (t1.tv_usec < 0) {
255 t1.tv_usec += 1000000;
256 t1.tv_sec--;
257 }
258 t1.tv_sec -= t2.tv_sec;
259 return t1;
260}
261
262static double tdiv(double value, struct timeval tv)
263{
264 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
265}
266
267#define HOURS(sec) ((sec) / (60 * 60))
268#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
269#define SECONDS(sec) ((sec) % 60)
270
271enum {
272 DEFAULT_TIME = 0x0,
273 TERSE_FIXED_TIME = 0x1,
274 VERBOSE_FIXED_TIME = 0x2,
275};
276
277static void timestr(struct timeval *tv, char *ts, size_t size, int format)
278{
279 double usec = (double)tv->tv_usec / 1000000.0;
280
281 if (format & TERSE_FIXED_TIME) {
282 if (!HOURS(tv->tv_sec)) {
283 snprintf(ts, size, "%u:%02u.%02u",
284 (unsigned int) MINUTES(tv->tv_sec),
285 (unsigned int) SECONDS(tv->tv_sec),
286 (unsigned int) (usec * 100));
287 return;
288 }
289 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
290 }
291
292 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
293 snprintf(ts, size, "%u:%02u:%02u.%02u",
294 (unsigned int) HOURS(tv->tv_sec),
295 (unsigned int) MINUTES(tv->tv_sec),
296 (unsigned int) SECONDS(tv->tv_sec),
297 (unsigned int) (usec * 100));
298 } else {
299 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
300 }
301}
302
Kevin Wolf797ac582013-06-05 14:19:31 +0200303/*
304 * Parse the pattern argument to various sub-commands.
305 *
306 * Because the pattern is used as an argument to memset it must evaluate
307 * to an unsigned integer that fits into a single byte.
308 */
309static int parse_pattern(const char *arg)
310{
311 char *endptr = NULL;
312 long pattern;
313
314 pattern = strtol(arg, &endptr, 0);
315 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
316 printf("%s is not a valid pattern byte\n", arg);
317 return -1;
318 }
319
320 return pattern;
321}
322
323/*
324 * Memory allocation helpers.
325 *
326 * Make sure memory is aligned by default, or purposefully misaligned if
327 * that is specified on the command line.
328 */
329
330#define MISALIGN_OFFSET 16
Max Reitz4c7b7e92015-02-05 13:58:22 -0500331static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
Kevin Wolf797ac582013-06-05 14:19:31 +0200332{
333 void *buf;
334
335 if (qemuio_misalign) {
336 len += MISALIGN_OFFSET;
337 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500338 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200339 memset(buf, pattern, len);
340 if (qemuio_misalign) {
341 buf += MISALIGN_OFFSET;
342 }
343 return buf;
344}
345
346static void qemu_io_free(void *p)
347{
348 if (qemuio_misalign) {
349 p -= MISALIGN_OFFSET;
350 }
351 qemu_vfree(p);
352}
353
John Snow9b0beaf2015-11-05 18:53:02 -0500354static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200355{
John Snow9b0beaf2015-11-05 18:53:02 -0500356 uint64_t i;
357 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200358 const uint8_t *p;
359
360 for (i = 0, p = buffer; i < len; i += 16) {
361 const uint8_t *s = p;
362
363 printf("%08" PRIx64 ": ", offset + i);
364 for (j = 0; j < 16 && i + j < len; j++, p++) {
365 printf("%02x ", *p);
366 }
367 printf(" ");
368 for (j = 0; j < 16 && i + j < len; j++, s++) {
369 if (isalnum(*s)) {
370 printf("%c", *s);
371 } else {
372 printf(".");
373 }
374 }
375 printf("\n");
376 }
377}
378
379static void print_report(const char *op, struct timeval *t, int64_t offset,
Eric Blakedc388522016-05-07 21:16:42 -0600380 int64_t count, int64_t total, int cnt, bool Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200381{
382 char s1[64], s2[64], ts[64];
383
384 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
385 if (!Cflag) {
386 cvtstr((double)total, s1, sizeof(s1));
387 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500388 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200389 op, total, count, offset);
390 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
391 s1, cnt, ts, s2, tdiv((double)cnt, *t));
392 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500393 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200394 total, cnt, ts,
395 tdiv((double)total, *t),
396 tdiv((double)cnt, *t));
397 }
398}
399
400/*
401 * Parse multiple length statements for vectored I/O, and construct an I/O
402 * vector matching it.
403 */
404static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500405create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200406 int pattern)
407{
408 size_t *sizes = g_new0(size_t, nr_iov);
409 size_t count = 0;
410 void *buf = NULL;
411 void *p;
412 int i;
413
414 for (i = 0; i < nr_iov; i++) {
415 char *arg = argv[i];
416 int64_t len;
417
418 len = cvtnum(arg);
419 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500420 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200421 goto fail;
422 }
423
Alberto Garcia3026c462017-01-31 18:09:54 +0200424 if (len > BDRV_REQUEST_MAX_BYTES) {
425 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
426 (uint64_t)BDRV_REQUEST_MAX_BYTES);
427 goto fail;
428 }
429
430 if (count > BDRV_REQUEST_MAX_BYTES - len) {
431 printf("The total number of bytes exceed the maximum size %" PRIu64
432 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
Kevin Wolf797ac582013-06-05 14:19:31 +0200433 goto fail;
434 }
435
Kevin Wolf797ac582013-06-05 14:19:31 +0200436 sizes[i] = len;
437 count += len;
438 }
439
440 qemu_iovec_init(qiov, nr_iov);
441
Max Reitz4c7b7e92015-02-05 13:58:22 -0500442 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200443
444 for (i = 0; i < nr_iov; i++) {
445 qemu_iovec_add(qiov, p, sizes[i]);
446 p += sizes[i];
447 }
448
449fail:
450 g_free(sizes);
451 return buf;
452}
453
John Snow9b0beaf2015-11-05 18:53:02 -0500454static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300455 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200456{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300457 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500458 return -ERANGE;
459 }
460
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300461 *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200462 if (*total < 0) {
463 return *total;
464 }
465 return 1;
466}
467
John Snow9b0beaf2015-11-05 18:53:02 -0500468static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300469 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200470{
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300471 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500472 return -ERANGE;
473 }
474
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300475 *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200476 if (*total < 0) {
477 return *total;
478 }
479 return 1;
480}
481
482typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500483 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200484 int64_t offset;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300485 int64_t bytes;
John Snow9b0beaf2015-11-05 18:53:02 -0500486 int64_t *total;
Eric Blake770e0e02016-05-07 21:16:44 -0600487 int flags;
Kevin Wolf797ac582013-06-05 14:19:31 +0200488 int ret;
489 bool done;
490} CoWriteZeroes;
491
Eric Blaked004bd52016-05-24 16:25:20 -0600492static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
Kevin Wolf797ac582013-06-05 14:19:31 +0200493{
494 CoWriteZeroes *data = opaque;
495
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300496 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
Eric Blaked004bd52016-05-24 16:25:20 -0600497 data->flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200498 data->done = true;
499 if (data->ret < 0) {
500 *data->total = data->ret;
501 return;
502 }
503
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300504 *data->total = data->bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200505}
506
Eric Blaked004bd52016-05-24 16:25:20 -0600507static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300508 int64_t bytes, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200509{
510 Coroutine *co;
511 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500512 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200513 .offset = offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300514 .bytes = bytes,
Kevin Wolf797ac582013-06-05 14:19:31 +0200515 .total = total,
Eric Blake770e0e02016-05-07 21:16:44 -0600516 .flags = flags,
Kevin Wolf797ac582013-06-05 14:19:31 +0200517 .done = false,
518 };
519
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300520 if (bytes > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500521 return -ERANGE;
522 }
523
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200524 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
Fam Zheng324ec3e2017-04-10 20:16:18 +0800525 bdrv_coroutine_enter(blk_bs(blk), co);
Kevin Wolf797ac582013-06-05 14:19:31 +0200526 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500527 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200528 }
529 if (data.ret < 0) {
530 return data.ret;
531 } else {
532 return 1;
533 }
534}
535
Max Reitz4c7b7e92015-02-05 13:58:22 -0500536static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300537 int64_t bytes, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200538{
539 int ret;
540
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300541 if (bytes >> 9 > BDRV_REQUEST_MAX_SECTORS) {
John Snow9b0beaf2015-11-05 18:53:02 -0500542 return -ERANGE;
543 }
544
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300545 ret = blk_pwrite_compressed(blk, offset, buf, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +0200546 if (ret < 0) {
547 return ret;
548 }
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300549 *total = bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +0200550 return 1;
551}
552
Max Reitz4c7b7e92015-02-05 13:58:22 -0500553static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500554 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200555{
John Snow9b0beaf2015-11-05 18:53:02 -0500556 if (count > INT_MAX) {
557 return -ERANGE;
558 }
559
Max Reitz4c7b7e92015-02-05 13:58:22 -0500560 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200561 if (*total < 0) {
562 return *total;
563 }
564 return 1;
565}
566
Max Reitz4c7b7e92015-02-05 13:58:22 -0500567static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500568 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200569{
John Snow9b0beaf2015-11-05 18:53:02 -0500570 if (count > INT_MAX) {
571 return -ERANGE;
572 }
573
Max Reitz4c7b7e92015-02-05 13:58:22 -0500574 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200575 if (*total < 0) {
576 return *total;
577 }
578 return 1;
579}
580
581#define NOT_DONE 0x7fffffff
582static void aio_rw_done(void *opaque, int ret)
583{
584 *(int *)opaque = ret;
585}
586
Max Reitz4c7b7e92015-02-05 13:58:22 -0500587static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200588 int64_t offset, int *total)
589{
590 int async_ret = NOT_DONE;
591
Eric Blake7b3f9712016-05-06 10:26:44 -0600592 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200593 while (async_ret == NOT_DONE) {
594 main_loop_wait(false);
595 }
596
597 *total = qiov->size;
598 return async_ret < 0 ? async_ret : 1;
599}
600
Max Reitz4c7b7e92015-02-05 13:58:22 -0500601static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600602 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200603{
604 int async_ret = NOT_DONE;
605
Eric Blake770e0e02016-05-07 21:16:44 -0600606 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200607 while (async_ret == NOT_DONE) {
608 main_loop_wait(false);
609 }
610
611 *total = qiov->size;
612 return async_ret < 0 ? async_ret : 1;
613}
614
Kevin Wolf797ac582013-06-05 14:19:31 +0200615static void read_help(void)
616{
617 printf(
618"\n"
619" reads a range of bytes from the given offset\n"
620"\n"
621" Example:\n"
622" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
623"\n"
624" Reads a segment of the currently open file, optionally dumping it to the\n"
625" standard output stream (with -v option) for subsequent inspection.\n"
626" -b, -- read from the VM state rather than the virtual disk\n"
627" -C, -- report statistics in a machine parsable format\n"
628" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600629" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200630" -P, -- use a pattern to verify read data\n"
631" -q, -- quiet mode, do not show I/O statistics\n"
632" -s, -- start offset for pattern verification (only with -P)\n"
633" -v, -- dump buffer to standard output\n"
634"\n");
635}
636
Max Reitzb32d7a32018-05-09 21:42:59 +0200637static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200638
639static const cmdinfo_t read_cmd = {
640 .name = "read",
641 .altname = "r",
642 .cfunc = read_f,
643 .argmin = 2,
644 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600645 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200646 .oneline = "reads a number of bytes at a specified offset",
647 .help = read_help,
648};
649
Max Reitzb32d7a32018-05-09 21:42:59 +0200650static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200651{
652 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600653 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600654 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200655 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200656 char *buf;
657 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500658 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200659 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500660 int64_t total = 0;
661 int pattern = 0;
662 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200663
Eric Blakeb062ad82015-05-12 09:10:56 -0600664 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200665 switch (c) {
666 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600667 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200668 break;
669 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600670 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200671 break;
672 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600673 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200674 pattern_count = cvtnum(optarg);
675 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500676 print_cvtnum_err(pattern_count, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200677 return pattern_count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200678 }
679 break;
680 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600681 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200682 break;
683 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600684 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200685 pattern = parse_pattern(optarg);
686 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200687 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200688 }
689 break;
690 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600691 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200692 break;
693 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600694 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200695 pattern_offset = cvtnum(optarg);
696 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500697 print_cvtnum_err(pattern_offset, optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +0200698 return pattern_offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200699 }
700 break;
701 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600702 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200703 break;
704 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200705 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200706 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200707 }
708 }
709
710 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200711 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200712 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200713 }
714
Kevin Wolf797ac582013-06-05 14:19:31 +0200715 offset = cvtnum(argv[optind]);
716 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500717 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200718 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200719 }
720
721 optind++;
722 count = cvtnum(argv[optind]);
723 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500724 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200725 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +0200726 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500727 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200728 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200729 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200730 }
731
732 if (!Pflag && (lflag || sflag)) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200733 qemuio_command_usage(&read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200734 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200735 }
736
737 if (!lflag) {
738 pattern_count = count - pattern_offset;
739 }
740
741 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
742 printf("pattern verification range exceeds end of read data\n");
Max Reitzb32d7a32018-05-09 21:42:59 +0200743 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200744 }
745
Eric Blake093ea232016-05-07 21:16:43 -0600746 if (bflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -0500747 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
748 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200749 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +0200750 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200751 }
Eric Blake1bce6b42017-04-29 14:14:11 -0500752 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
753 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200754 count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200755 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200756 }
757 }
758
Max Reitz4c7b7e92015-02-05 13:58:22 -0500759 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200760
761 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -0600762 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200763 ret = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200764 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +0200765 ret = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200766 }
767 gettimeofday(&t2, NULL);
768
Max Reitzb32d7a32018-05-09 21:42:59 +0200769 if (ret < 0) {
770 printf("read failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200771 goto out;
772 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200773 cnt = ret;
774
775 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200776
777 if (Pflag) {
778 void *cmp_buf = g_malloc(pattern_count);
779 memset(cmp_buf, pattern, pattern_count);
780 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
781 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500782 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200783 offset + pattern_offset, pattern_count);
Max Reitzb32d7a32018-05-09 21:42:59 +0200784 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200785 }
786 g_free(cmp_buf);
787 }
788
789 if (qflag) {
790 goto out;
791 }
792
793 if (vflag) {
794 dump_buffer(buf, offset, count);
795 }
796
797 /* Finally, report back -- -C gives a parsable format */
798 t2 = tsub(t2, t1);
799 print_report("read", &t2, offset, count, total, cnt, Cflag);
800
801out:
802 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +0200803 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200804}
805
806static void readv_help(void)
807{
808 printf(
809"\n"
810" reads a range of bytes from the given offset into multiple buffers\n"
811"\n"
812" Example:\n"
813" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
814"\n"
815" Reads a segment of the currently open file, optionally dumping it to the\n"
816" standard output stream (with -v option) for subsequent inspection.\n"
817" Uses multiple iovec buffers if more than one byte range is specified.\n"
818" -C, -- report statistics in a machine parsable format\n"
819" -P, -- use a pattern to verify read data\n"
820" -v, -- dump buffer to standard output\n"
821" -q, -- quiet mode, do not show I/O statistics\n"
822"\n");
823}
824
Max Reitzb32d7a32018-05-09 21:42:59 +0200825static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200826
827static const cmdinfo_t readv_cmd = {
828 .name = "readv",
829 .cfunc = readv_f,
830 .argmin = 2,
831 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600832 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200833 .oneline = "reads a number of bytes at a specified offset",
834 .help = readv_help,
835};
836
Max Reitzb32d7a32018-05-09 21:42:59 +0200837static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200838{
839 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600840 bool Cflag = false, qflag = false, vflag = false;
Max Reitzb32d7a32018-05-09 21:42:59 +0200841 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200842 char *buf;
843 int64_t offset;
844 /* Some compilers get confused and warn if this is not initialized. */
845 int total = 0;
846 int nr_iov;
847 QEMUIOVector qiov;
848 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600849 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200850
Eric Blakeb062ad82015-05-12 09:10:56 -0600851 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200852 switch (c) {
853 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600854 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200855 break;
856 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600857 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200858 pattern = parse_pattern(optarg);
859 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200860 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200861 }
862 break;
863 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600864 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200865 break;
866 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600867 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200868 break;
869 default:
Max Reitzb444d0e2018-05-09 21:42:58 +0200870 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200871 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200872 }
873 }
874
875 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +0200876 qemuio_command_usage(&readv_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200877 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200878 }
879
880
881 offset = cvtnum(argv[optind]);
882 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500883 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +0200884 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +0200885 }
886 optind++;
887
Kevin Wolf797ac582013-06-05 14:19:31 +0200888 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500889 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200890 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200891 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200892 }
893
894 gettimeofday(&t1, NULL);
Max Reitzb32d7a32018-05-09 21:42:59 +0200895 ret = do_aio_readv(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200896 gettimeofday(&t2, NULL);
897
Max Reitzb32d7a32018-05-09 21:42:59 +0200898 if (ret < 0) {
899 printf("readv failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +0200900 goto out;
901 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200902 cnt = ret;
903
904 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200905
906 if (Pflag) {
907 void *cmp_buf = g_malloc(qiov.size);
908 memset(cmp_buf, pattern, qiov.size);
909 if (memcmp(buf, cmp_buf, qiov.size)) {
910 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +0200911 PRId64 ", %zu bytes\n", offset, qiov.size);
Max Reitzb32d7a32018-05-09 21:42:59 +0200912 ret = -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +0200913 }
914 g_free(cmp_buf);
915 }
916
917 if (qflag) {
918 goto out;
919 }
920
921 if (vflag) {
922 dump_buffer(buf, offset, qiov.size);
923 }
924
925 /* Finally, report back -- -C gives a parsable format */
926 t2 = tsub(t2, t1);
927 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
928
929out:
930 qemu_iovec_destroy(&qiov);
931 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +0200932 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200933}
934
935static void write_help(void)
936{
937 printf(
938"\n"
939" writes a range of bytes from the given offset\n"
940"\n"
941" Example:\n"
942" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
943"\n"
944" Writes into a segment of the currently open file, using a buffer\n"
945" filled with a set pattern (0xcdcdcdcd).\n"
946" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500947" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -0600948" -f, -- use Force Unit Access semantics\n"
Eric Blake093ea232016-05-07 21:16:43 -0600949" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200950" -P, -- use different pattern to fill file\n"
951" -C, -- report statistics in a machine parsable format\n"
952" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -0600953" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -0600954" -z, -- write zeroes using blk_co_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200955"\n");
956}
957
Max Reitzb32d7a32018-05-09 21:42:59 +0200958static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200959
960static const cmdinfo_t write_cmd = {
961 .name = "write",
962 .altname = "w",
963 .cfunc = write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +0100964 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +0200965 .argmin = 2,
966 .argmax = -1,
Eric Blakec2e001c2016-05-07 21:16:45 -0600967 .args = "[-bcCfquz] [-P pattern] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200968 .oneline = "writes a number of bytes at a specified offset",
969 .help = write_help,
970};
971
Max Reitzb32d7a32018-05-09 21:42:59 +0200972static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200973{
974 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600975 bool Cflag = false, qflag = false, bflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600976 bool Pflag = false, zflag = false, cflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -0600977 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +0200978 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200979 char *buf = NULL;
980 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500981 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200982 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500983 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200984 int pattern = 0xcd;
985
Eric Blakec2e001c2016-05-07 21:16:45 -0600986 while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200987 switch (c) {
988 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600989 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200990 break;
991 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -0600992 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200993 break;
994 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600995 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200996 break;
Eric Blake770e0e02016-05-07 21:16:44 -0600997 case 'f':
998 flags |= BDRV_REQ_FUA;
999 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001000 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -06001001 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +02001002 break;
1003 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001004 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001005 pattern = parse_pattern(optarg);
1006 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001007 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001008 }
1009 break;
1010 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001011 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001012 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001013 case 'u':
1014 flags |= BDRV_REQ_MAY_UNMAP;
1015 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001016 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001017 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001018 break;
1019 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001020 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001021 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001022 }
1023 }
1024
1025 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001026 qemuio_command_usage(&write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001027 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001028 }
1029
Eric Blake093ea232016-05-07 21:16:43 -06001030 if (bflag && zflag) {
1031 printf("-b and -z cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001032 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001033 }
1034
Eric Blake770e0e02016-05-07 21:16:44 -06001035 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1036 printf("-f and -b or -c cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001037 return -EINVAL;
Eric Blake770e0e02016-05-07 21:16:44 -06001038 }
1039
Eric Blakec2e001c2016-05-07 21:16:45 -06001040 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1041 printf("-u requires -z to be specified\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001042 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001043 }
1044
Kevin Wolf797ac582013-06-05 14:19:31 +02001045 if (zflag && Pflag) {
1046 printf("-z and -P cannot be specified at the same time\n");
Max Reitzb32d7a32018-05-09 21:42:59 +02001047 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001048 }
1049
1050 offset = cvtnum(argv[optind]);
1051 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001052 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001053 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001054 }
1055
1056 optind++;
1057 count = cvtnum(argv[optind]);
1058 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001059 print_cvtnum_err(count, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001060 return count;
Alberto Garcia3026c462017-01-31 18:09:54 +02001061 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001062 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001063 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001064 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001065 }
1066
Eric Blake093ea232016-05-07 21:16:43 -06001067 if (bflag || cflag) {
Eric Blake1bce6b42017-04-29 14:14:11 -05001068 if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
1069 printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001070 offset);
Max Reitzb32d7a32018-05-09 21:42:59 +02001071 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001072 }
1073
Eric Blake1bce6b42017-04-29 14:14:11 -05001074 if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
1075 printf("%"PRId64" is not a sector-aligned value for 'count'\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001076 count);
Max Reitzb32d7a32018-05-09 21:42:59 +02001077 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001078 }
1079 }
1080
1081 if (!zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001082 buf = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001083 }
1084
1085 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -06001086 if (bflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001087 ret = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001088 } else if (zflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001089 ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001090 } else if (cflag) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001091 ret = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001092 } else {
Max Reitzb32d7a32018-05-09 21:42:59 +02001093 ret = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001094 }
1095 gettimeofday(&t2, NULL);
1096
Max Reitzb32d7a32018-05-09 21:42:59 +02001097 if (ret < 0) {
1098 printf("write failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001099 goto out;
1100 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001101 cnt = ret;
1102
1103 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001104
1105 if (qflag) {
1106 goto out;
1107 }
1108
1109 /* Finally, report back -- -C gives a parsable format */
1110 t2 = tsub(t2, t1);
1111 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1112
1113out:
1114 if (!zflag) {
1115 qemu_io_free(buf);
1116 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001117 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001118}
1119
1120static void
1121writev_help(void)
1122{
1123 printf(
1124"\n"
1125" writes a range of bytes from the given offset source from multiple buffers\n"
1126"\n"
1127" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001128" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001129"\n"
1130" Writes into a segment of the currently open file, using a buffer\n"
1131" filled with a set pattern (0xcdcdcdcd).\n"
1132" -P, -- use different pattern to fill file\n"
1133" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001134" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001135" -q, -- quiet mode, do not show I/O statistics\n"
1136"\n");
1137}
1138
Max Reitzb32d7a32018-05-09 21:42:59 +02001139static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001140
1141static const cmdinfo_t writev_cmd = {
1142 .name = "writev",
1143 .cfunc = writev_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001144 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001145 .argmin = 2,
1146 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001147 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001148 .oneline = "writes a number of bytes at a specified offset",
1149 .help = writev_help,
1150};
1151
Max Reitzb32d7a32018-05-09 21:42:59 +02001152static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001153{
1154 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001155 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001156 int flags = 0;
Max Reitzb32d7a32018-05-09 21:42:59 +02001157 int c, cnt, ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001158 char *buf;
1159 int64_t offset;
1160 /* Some compilers get confused and warn if this is not initialized. */
1161 int total = 0;
1162 int nr_iov;
1163 int pattern = 0xcd;
1164 QEMUIOVector qiov;
1165
Eric Blake4ca1d342016-05-16 10:43:01 -06001166 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001167 switch (c) {
1168 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001169 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001170 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001171 case 'f':
1172 flags |= BDRV_REQ_FUA;
1173 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001174 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001175 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001176 break;
1177 case 'P':
1178 pattern = parse_pattern(optarg);
1179 if (pattern < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001180 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001181 }
1182 break;
1183 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001184 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001185 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001186 }
1187 }
1188
1189 if (optind > argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001190 qemuio_command_usage(&writev_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001191 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001192 }
1193
1194 offset = cvtnum(argv[optind]);
1195 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001196 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001197 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001198 }
1199 optind++;
1200
Kevin Wolf797ac582013-06-05 14:19:31 +02001201 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001202 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001203 if (buf == NULL) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001204 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001205 }
1206
1207 gettimeofday(&t1, NULL);
Max Reitzb32d7a32018-05-09 21:42:59 +02001208 ret = do_aio_writev(blk, &qiov, offset, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001209 gettimeofday(&t2, NULL);
1210
Max Reitzb32d7a32018-05-09 21:42:59 +02001211 if (ret < 0) {
1212 printf("writev failed: %s\n", strerror(-ret));
Kevin Wolf797ac582013-06-05 14:19:31 +02001213 goto out;
1214 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001215 cnt = ret;
1216
1217 ret = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001218
1219 if (qflag) {
1220 goto out;
1221 }
1222
1223 /* Finally, report back -- -C gives a parsable format */
1224 t2 = tsub(t2, t1);
1225 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1226out:
1227 qemu_iovec_destroy(&qiov);
1228 qemu_io_free(buf);
Max Reitzb32d7a32018-05-09 21:42:59 +02001229 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001230}
1231
Kevin Wolf797ac582013-06-05 14:19:31 +02001232struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001233 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001234 QEMUIOVector qiov;
1235 int64_t offset;
1236 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001237 bool qflag;
1238 bool vflag;
1239 bool Cflag;
1240 bool Pflag;
1241 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001242 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001243 int pattern;
1244 struct timeval t1;
1245};
1246
1247static void aio_write_done(void *opaque, int ret)
1248{
1249 struct aio_ctx *ctx = opaque;
1250 struct timeval t2;
1251
1252 gettimeofday(&t2, NULL);
1253
1254
1255 if (ret < 0) {
1256 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001257 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001258 goto out;
1259 }
1260
Max Reitz4c7b7e92015-02-05 13:58:22 -05001261 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001262
Kevin Wolf797ac582013-06-05 14:19:31 +02001263 if (ctx->qflag) {
1264 goto out;
1265 }
1266
1267 /* Finally, report back -- -C gives a parsable format */
1268 t2 = tsub(t2, ctx->t1);
1269 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1270 ctx->qiov.size, 1, ctx->Cflag);
1271out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001272 if (!ctx->zflag) {
1273 qemu_io_free(ctx->buf);
1274 qemu_iovec_destroy(&ctx->qiov);
1275 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001276 g_free(ctx);
1277}
1278
1279static void aio_read_done(void *opaque, int ret)
1280{
1281 struct aio_ctx *ctx = opaque;
1282 struct timeval t2;
1283
1284 gettimeofday(&t2, NULL);
1285
1286 if (ret < 0) {
1287 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001288 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001289 goto out;
1290 }
1291
1292 if (ctx->Pflag) {
1293 void *cmp_buf = g_malloc(ctx->qiov.size);
1294
1295 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1296 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1297 printf("Pattern verification failed at offset %"
Stefan Weilcf67b692018-10-06 20:38:51 +02001298 PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
Kevin Wolf797ac582013-06-05 14:19:31 +02001299 }
1300 g_free(cmp_buf);
1301 }
1302
Max Reitz4c7b7e92015-02-05 13:58:22 -05001303 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001304
Kevin Wolf797ac582013-06-05 14:19:31 +02001305 if (ctx->qflag) {
1306 goto out;
1307 }
1308
1309 if (ctx->vflag) {
1310 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1311 }
1312
1313 /* Finally, report back -- -C gives a parsable format */
1314 t2 = tsub(t2, ctx->t1);
1315 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1316 ctx->qiov.size, 1, ctx->Cflag);
1317out:
1318 qemu_io_free(ctx->buf);
1319 qemu_iovec_destroy(&ctx->qiov);
1320 g_free(ctx);
1321}
1322
1323static void aio_read_help(void)
1324{
1325 printf(
1326"\n"
1327" asynchronously reads a range of bytes from the given offset\n"
1328"\n"
1329" Example:\n"
1330" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1331"\n"
1332" Reads a segment of the currently open file, optionally dumping it to the\n"
1333" standard output stream (with -v option) for subsequent inspection.\n"
1334" The read is performed asynchronously and the aio_flush command must be\n"
1335" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001336" Note that due to its asynchronous nature, this command will be\n"
1337" considered successful once the request is submitted, independently\n"
1338" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001339" -C, -- report statistics in a machine parsable format\n"
1340" -P, -- use a pattern to verify read data\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001341" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001342" -v, -- dump buffer to standard output\n"
1343" -q, -- quiet mode, do not show I/O statistics\n"
1344"\n");
1345}
1346
Max Reitzb32d7a32018-05-09 21:42:59 +02001347static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001348
1349static const cmdinfo_t aio_read_cmd = {
1350 .name = "aio_read",
1351 .cfunc = aio_read_f,
1352 .argmin = 2,
1353 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001354 .args = "[-Ciqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001355 .oneline = "asynchronously reads a number of bytes",
1356 .help = aio_read_help,
1357};
1358
Max Reitzb32d7a32018-05-09 21:42:59 +02001359static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001360{
1361 int nr_iov, c;
1362 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1363
Max Reitz4c7b7e92015-02-05 13:58:22 -05001364 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001365 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001366 switch (c) {
1367 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001368 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001369 break;
1370 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001371 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001372 ctx->pattern = parse_pattern(optarg);
1373 if (ctx->pattern < 0) {
1374 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001375 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001376 }
1377 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001378 case 'i':
1379 printf("injecting invalid read request\n");
1380 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1381 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001382 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001383 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001384 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001385 break;
1386 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001387 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001388 break;
1389 default:
1390 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001391 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001392 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001393 }
1394 }
1395
1396 if (optind > argc - 2) {
1397 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001398 qemuio_command_usage(&aio_read_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001399 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001400 }
1401
1402 ctx->offset = cvtnum(argv[optind]);
1403 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001404 int ret = ctx->offset;
1405 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001406 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001407 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001408 }
1409 optind++;
1410
Kevin Wolf797ac582013-06-05 14:19:31 +02001411 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001412 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001413 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001414 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001415 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001416 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001417 }
1418
1419 gettimeofday(&ctx->t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001420 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1421 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001422 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001423 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001424}
1425
1426static void aio_write_help(void)
1427{
1428 printf(
1429"\n"
1430" asynchronously writes a range of bytes from the given offset source\n"
1431" from multiple buffers\n"
1432"\n"
1433" Example:\n"
1434" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1435"\n"
1436" Writes into a segment of the currently open file, using a buffer\n"
1437" filled with a set pattern (0xcdcdcdcd).\n"
1438" The write is performed asynchronously and the aio_flush command must be\n"
1439" used to ensure all outstanding aio requests have been completed.\n"
Max Reitzb32d7a32018-05-09 21:42:59 +02001440" Note that due to its asynchronous nature, this command will be\n"
1441" considered successful once the request is submitted, independently\n"
1442" of potential I/O errors or pattern mismatches.\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001443" -P, -- use different pattern to fill file\n"
1444" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001445" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001446" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001447" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001448" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001449" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001450"\n");
1451}
1452
Max Reitzb32d7a32018-05-09 21:42:59 +02001453static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001454
1455static const cmdinfo_t aio_write_cmd = {
1456 .name = "aio_write",
1457 .cfunc = aio_write_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001458 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001459 .argmin = 2,
1460 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001461 .args = "[-Cfiquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001462 .oneline = "asynchronously writes a number of bytes",
1463 .help = aio_write_help,
1464};
1465
Max Reitzb32d7a32018-05-09 21:42:59 +02001466static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001467{
1468 int nr_iov, c;
1469 int pattern = 0xcd;
1470 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001471 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001472
Max Reitz4c7b7e92015-02-05 13:58:22 -05001473 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001474 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001475 switch (c) {
1476 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001477 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001478 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001479 case 'f':
1480 flags |= BDRV_REQ_FUA;
1481 break;
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;
Eric Blakec2e001c2016-05-07 21:16:45 -06001485 case 'u':
1486 flags |= BDRV_REQ_MAY_UNMAP;
1487 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001488 case 'P':
1489 pattern = parse_pattern(optarg);
1490 if (pattern < 0) {
1491 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001492 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001493 }
1494 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001495 case 'i':
1496 printf("injecting invalid write request\n");
1497 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1498 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001499 return 0;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001500 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001501 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001502 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001503 default:
1504 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001505 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001506 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001507 }
1508 }
1509
1510 if (optind > argc - 2) {
1511 g_free(ctx);
Max Reitzb444d0e2018-05-09 21:42:58 +02001512 qemuio_command_usage(&aio_write_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001513 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001514 }
1515
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001516 if (ctx->zflag && optind != argc - 2) {
1517 printf("-z supports only a single length parameter\n");
1518 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001519 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001520 }
1521
Eric Blakec2e001c2016-05-07 21:16:45 -06001522 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1523 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001524 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001525 return -EINVAL;
Eric Blakec2e001c2016-05-07 21:16:45 -06001526 }
1527
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001528 if (ctx->zflag && ctx->Pflag) {
1529 printf("-z and -P cannot be specified at the same time\n");
1530 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001531 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001532 }
1533
Kevin Wolf797ac582013-06-05 14:19:31 +02001534 ctx->offset = cvtnum(argv[optind]);
1535 if (ctx->offset < 0) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001536 int ret = ctx->offset;
1537 print_cvtnum_err(ret, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001538 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001539 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001540 }
1541 optind++;
1542
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001543 if (ctx->zflag) {
1544 int64_t count = cvtnum(argv[optind]);
1545 if (count < 0) {
1546 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001547 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001548 return count;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001549 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001550
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001551 ctx->qiov.size = count;
Eric Blaked004bd52016-05-24 16:25:20 -06001552 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1553 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001554 } else {
1555 nr_iov = argc - optind;
1556 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1557 pattern);
1558 if (ctx->buf == NULL) {
1559 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1560 g_free(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02001561 return -EINVAL;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001562 }
1563
1564 gettimeofday(&ctx->t1, NULL);
1565 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1566 BLOCK_ACCT_WRITE);
1567
Eric Blake770e0e02016-05-07 21:16:44 -06001568 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1569 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001570 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001571
1572 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001573}
1574
Max Reitzb32d7a32018-05-09 21:42:59 +02001575static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001576{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001577 BlockAcctCookie cookie;
1578 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001579 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001580 block_acct_done(blk_get_stats(blk), &cookie);
Max Reitzb32d7a32018-05-09 21:42:59 +02001581 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001582}
1583
1584static const cmdinfo_t aio_flush_cmd = {
1585 .name = "aio_flush",
1586 .cfunc = aio_flush_f,
1587 .oneline = "completes all outstanding aio requests"
1588};
1589
Max Reitzb32d7a32018-05-09 21:42:59 +02001590static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001591{
Max Reitzb32d7a32018-05-09 21:42:59 +02001592 return blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001593}
1594
1595static const cmdinfo_t flush_cmd = {
1596 .name = "flush",
1597 .altname = "f",
1598 .cfunc = flush_f,
1599 .oneline = "flush all in-core file state to disk",
1600};
1601
Max Reitzb32d7a32018-05-09 21:42:59 +02001602static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001603{
Max Reitzed3d2ec2017-03-28 22:51:27 +02001604 Error *local_err = NULL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001605 int64_t offset;
1606 int ret;
1607
1608 offset = cvtnum(argv[1]);
1609 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001610 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001611 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001612 }
1613
Max Reitz3a691c52017-06-13 22:20:54 +02001614 ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
Kevin Wolf797ac582013-06-05 14:19:31 +02001615 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001616 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02001617 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001618 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001619
1620 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001621}
1622
1623static const cmdinfo_t truncate_cmd = {
1624 .name = "truncate",
1625 .altname = "t",
1626 .cfunc = truncate_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001627 .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001628 .argmin = 1,
1629 .argmax = 1,
1630 .args = "off",
1631 .oneline = "truncates the current file at the given offset",
1632};
1633
Max Reitzb32d7a32018-05-09 21:42:59 +02001634static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001635{
1636 int64_t size;
1637 char s1[64];
1638
Max Reitz4c7b7e92015-02-05 13:58:22 -05001639 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001640 if (size < 0) {
1641 printf("getlength: %s\n", strerror(-size));
Max Reitzb32d7a32018-05-09 21:42:59 +02001642 return size;
Kevin Wolf797ac582013-06-05 14:19:31 +02001643 }
1644
1645 cvtstr(size, s1, sizeof(s1));
1646 printf("%s\n", s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001647 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001648}
1649
1650
1651static const cmdinfo_t length_cmd = {
1652 .name = "length",
1653 .altname = "l",
1654 .cfunc = length_f,
1655 .oneline = "gets the length of the current file",
1656};
1657
1658
Max Reitzb32d7a32018-05-09 21:42:59 +02001659static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001660{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001661 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001662 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001663 ImageInfoSpecific *spec_info;
Kevin Wolf797ac582013-06-05 14:19:31 +02001664 char s1[64], s2[64];
1665 int ret;
1666
1667 if (bs->drv && bs->drv->format_name) {
1668 printf("format name: %s\n", bs->drv->format_name);
1669 }
1670 if (bs->drv && bs->drv->protocol_name) {
1671 printf("format name: %s\n", bs->drv->protocol_name);
1672 }
1673
1674 ret = bdrv_get_info(bs, &bdi);
1675 if (ret) {
Max Reitzb32d7a32018-05-09 21:42:59 +02001676 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001677 }
1678
1679 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1680 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1681
1682 printf("cluster size: %s\n", s1);
1683 printf("vm state offset: %s\n", s2);
1684
Max Reitza8d8ecb2013-10-09 10:46:17 +02001685 spec_info = bdrv_get_specific_info(bs);
1686 if (spec_info) {
1687 printf("Format specific information:\n");
1688 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1689 qapi_free_ImageInfoSpecific(spec_info);
1690 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001691
1692 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001693}
1694
1695
1696
1697static const cmdinfo_t info_cmd = {
1698 .name = "info",
1699 .altname = "i",
1700 .cfunc = info_f,
1701 .oneline = "prints information about the current file",
1702};
1703
1704static void discard_help(void)
1705{
1706 printf(
1707"\n"
1708" discards a range of bytes from the given offset\n"
1709"\n"
1710" Example:\n"
1711" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1712"\n"
1713" Discards a segment of the currently open file.\n"
1714" -C, -- report statistics in a machine parsable format\n"
1715" -q, -- quiet mode, do not show I/O statistics\n"
1716"\n");
1717}
1718
Max Reitzb32d7a32018-05-09 21:42:59 +02001719static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001720
1721static const cmdinfo_t discard_cmd = {
1722 .name = "discard",
1723 .altname = "d",
1724 .cfunc = discard_f,
Kevin Wolf887354b2017-02-10 16:24:56 +01001725 .perm = BLK_PERM_WRITE,
Kevin Wolf797ac582013-06-05 14:19:31 +02001726 .argmin = 2,
1727 .argmax = -1,
1728 .args = "[-Cq] off len",
1729 .oneline = "discards a number of bytes at a specified offset",
1730 .help = discard_help,
1731};
1732
Max Reitzb32d7a32018-05-09 21:42:59 +02001733static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001734{
1735 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001736 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001737 int c, ret;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001738 int64_t offset, bytes;
Kevin Wolf797ac582013-06-05 14:19:31 +02001739
Eric Blakeb062ad82015-05-12 09:10:56 -06001740 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001741 switch (c) {
1742 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001743 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001744 break;
1745 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001746 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001747 break;
1748 default:
Max Reitzb444d0e2018-05-09 21:42:58 +02001749 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001750 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001751 }
1752 }
1753
1754 if (optind != argc - 2) {
Max Reitzb444d0e2018-05-09 21:42:58 +02001755 qemuio_command_usage(&discard_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02001756 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001757 }
1758
1759 offset = cvtnum(argv[optind]);
1760 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001761 print_cvtnum_err(offset, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001762 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001763 }
1764
1765 optind++;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001766 bytes = cvtnum(argv[optind]);
1767 if (bytes < 0) {
1768 print_cvtnum_err(bytes, argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001769 return bytes;
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001770 } else if (bytes >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
John Snow9b0beaf2015-11-05 18:53:02 -05001771 printf("length cannot exceed %"PRIu64", given %s\n",
Max Reitza3674672016-06-20 16:26:22 +02001772 (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
John Snow9b0beaf2015-11-05 18:53:02 -05001773 argv[optind]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001774 return -EINVAL;
Kevin Wolf797ac582013-06-05 14:19:31 +02001775 }
1776
1777 gettimeofday(&t1, NULL);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001778 ret = blk_pdiscard(blk, offset, bytes);
Kevin Wolf797ac582013-06-05 14:19:31 +02001779 gettimeofday(&t2, NULL);
1780
1781 if (ret < 0) {
1782 printf("discard failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001783 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02001784 }
1785
1786 /* Finally, report back -- -C gives a parsable format */
1787 if (!qflag) {
1788 t2 = tsub(t2, t1);
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +03001789 print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
Kevin Wolf797ac582013-06-05 14:19:31 +02001790 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001791
1792 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001793}
1794
Max Reitzb32d7a32018-05-09 21:42:59 +02001795static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001796{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001797 BlockDriverState *bs = blk_bs(blk);
Eric Blaked6a644b2017-07-07 07:44:57 -05001798 int64_t offset, start, remaining, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001799 char s1[64];
Eric Blaked6a644b2017-07-07 07:44:57 -05001800 int ret;
1801 int64_t num, sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001802
Eric Blaked6a644b2017-07-07 07:44:57 -05001803 start = offset = cvtnum(argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001804 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001805 print_cvtnum_err(offset, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001806 return offset;
Kevin Wolf797ac582013-06-05 14:19:31 +02001807 }
1808
1809 if (argc == 3) {
Eric Blake4401fdc2017-04-29 14:14:12 -05001810 count = cvtnum(argv[2]);
1811 if (count < 0) {
1812 print_cvtnum_err(count, argv[2]);
Max Reitzb32d7a32018-05-09 21:42:59 +02001813 return count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001814 }
1815 } else {
Eric Blake4401fdc2017-04-29 14:14:12 -05001816 count = BDRV_SECTOR_SIZE;
Kevin Wolf797ac582013-06-05 14:19:31 +02001817 }
1818
Eric Blaked6a644b2017-07-07 07:44:57 -05001819 remaining = count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001820 sum_alloc = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001821 while (remaining) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001822 ret = bdrv_is_allocated(bs, offset, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001823 if (ret < 0) {
1824 printf("is_allocated failed: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001825 return ret;
Paolo Bonzinid6636402013-09-04 19:00:25 +02001826 }
Eric Blaked6a644b2017-07-07 07:44:57 -05001827 offset += num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001828 remaining -= num;
1829 if (ret) {
1830 sum_alloc += num;
1831 }
1832 if (num == 0) {
Eric Blaked6a644b2017-07-07 07:44:57 -05001833 count -= remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001834 remaining = 0;
1835 }
1836 }
1837
Eric Blaked6a644b2017-07-07 07:44:57 -05001838 cvtstr(start, s1, sizeof(s1));
Kevin Wolf797ac582013-06-05 14:19:31 +02001839
Eric Blake4401fdc2017-04-29 14:14:12 -05001840 printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001841 sum_alloc, count, s1);
Max Reitzb32d7a32018-05-09 21:42:59 +02001842 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001843}
1844
1845static const cmdinfo_t alloc_cmd = {
1846 .name = "alloc",
1847 .altname = "a",
1848 .argmin = 1,
1849 .argmax = 2,
1850 .cfunc = alloc_f,
Eric Blake4401fdc2017-04-29 14:14:12 -05001851 .args = "offset [count]",
1852 .oneline = "checks if offset is allocated in the file",
Kevin Wolf797ac582013-06-05 14:19:31 +02001853};
1854
1855
Eric Blaked6a644b2017-07-07 07:44:57 -05001856static int map_is_allocated(BlockDriverState *bs, int64_t offset,
1857 int64_t bytes, int64_t *pnum)
Kevin Wolf797ac582013-06-05 14:19:31 +02001858{
Eric Blaked6a644b2017-07-07 07:44:57 -05001859 int64_t num;
1860 int num_checked;
Kevin Wolf797ac582013-06-05 14:19:31 +02001861 int ret, firstret;
1862
Eric Blaked6a644b2017-07-07 07:44:57 -05001863 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1864 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001865 if (ret < 0) {
1866 return ret;
1867 }
1868
1869 firstret = ret;
1870 *pnum = num;
1871
Eric Blaked6a644b2017-07-07 07:44:57 -05001872 while (bytes > 0 && ret == firstret) {
1873 offset += num;
1874 bytes -= num;
Kevin Wolf797ac582013-06-05 14:19:31 +02001875
Eric Blaked6a644b2017-07-07 07:44:57 -05001876 num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
1877 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02001878 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001879 *pnum += num;
1880 } else {
1881 break;
1882 }
1883 }
1884
1885 return firstret;
1886}
1887
Max Reitzb32d7a32018-05-09 21:42:59 +02001888static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001889{
Eric Blaked6a644b2017-07-07 07:44:57 -05001890 int64_t offset, bytes;
Eric Blake6f3c90a2017-04-29 14:14:13 -05001891 char s1[64], s2[64];
Kevin Wolf797ac582013-06-05 14:19:31 +02001892 int64_t num;
1893 int ret;
1894 const char *retstr;
1895
1896 offset = 0;
Eric Blaked6a644b2017-07-07 07:44:57 -05001897 bytes = blk_getlength(blk);
1898 if (bytes < 0) {
1899 error_report("Failed to query image length: %s", strerror(-bytes));
Max Reitzb32d7a32018-05-09 21:42:59 +02001900 return bytes;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001901 }
1902
Eric Blaked6a644b2017-07-07 07:44:57 -05001903 while (bytes) {
1904 ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001905 if (ret < 0) {
1906 error_report("Failed to get allocation status: %s", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02001907 return ret;
Max Reitz4b25bbc2014-10-22 17:00:16 +02001908 } else if (!num) {
1909 error_report("Unexpected end of image");
Max Reitzb32d7a32018-05-09 21:42:59 +02001910 return -EIO;
Kevin Wolf797ac582013-06-05 14:19:31 +02001911 }
1912
1913 retstr = ret ? " allocated" : "not allocated";
Eric Blaked6a644b2017-07-07 07:44:57 -05001914 cvtstr(num, s1, sizeof(s1));
1915 cvtstr(offset, s2, sizeof(s2));
Eric Blake6f3c90a2017-04-29 14:14:13 -05001916 printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
Eric Blaked6a644b2017-07-07 07:44:57 -05001917 s1, num, retstr, s2, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02001918
1919 offset += num;
Eric Blaked6a644b2017-07-07 07:44:57 -05001920 bytes -= num;
1921 }
Max Reitzb32d7a32018-05-09 21:42:59 +02001922
1923 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001924}
1925
1926static const cmdinfo_t map_cmd = {
1927 .name = "map",
1928 .argmin = 0,
1929 .argmax = 0,
1930 .cfunc = map_f,
1931 .args = "",
1932 .oneline = "prints the allocated areas of a file",
1933};
1934
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001935static void reopen_help(void)
1936{
1937 printf(
1938"\n"
1939" Changes the open options of an already opened image\n"
1940"\n"
1941" Example:\n"
1942" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1943"\n"
1944" -r, -- Reopen the image read-only\n"
Kevin Wolfea922032017-08-03 17:03:00 +02001945" -w, -- Reopen the image read-write\n"
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001946" -c, -- Change the cache mode to the given value\n"
1947" -o, -- Changes block driver options (cf. 'open' command)\n"
1948"\n");
1949}
1950
Max Reitzb32d7a32018-05-09 21:42:59 +02001951static int reopen_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001952
1953static QemuOptsList reopen_opts = {
1954 .name = "reopen",
1955 .merge_lists = true,
1956 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
1957 .desc = {
1958 /* no elements => accept any params */
1959 { /* end of list */ }
1960 },
1961};
1962
1963static const cmdinfo_t reopen_cmd = {
1964 .name = "reopen",
1965 .argmin = 0,
1966 .argmax = -1,
1967 .cfunc = reopen_f,
Kevin Wolfea922032017-08-03 17:03:00 +02001968 .args = "[(-r|-w)] [-c cache] [-o options]",
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001969 .oneline = "reopens an image with new options",
1970 .help = reopen_help,
1971};
1972
Max Reitzb32d7a32018-05-09 21:42:59 +02001973static int reopen_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001974{
1975 BlockDriverState *bs = blk_bs(blk);
1976 QemuOpts *qopts;
1977 QDict *opts;
1978 int c;
1979 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001980 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolfea922032017-08-03 17:03:00 +02001981 bool has_rw_option = false;
Alberto Garciadc900c32018-11-12 16:00:42 +02001982 bool has_cache_option = false;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001983
1984 BlockReopenQueue *brq;
1985 Error *local_err = NULL;
1986
Kevin Wolfea922032017-08-03 17:03:00 +02001987 while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001988 switch (c) {
1989 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001990 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001991 error_report("Invalid cache option: %s", optarg);
Max Reitzb32d7a32018-05-09 21:42:59 +02001992 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001993 }
Alberto Garciadc900c32018-11-12 16:00:42 +02001994 has_cache_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001995 break;
1996 case 'o':
1997 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
1998 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02001999 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002000 }
2001 break;
2002 case 'r':
Kevin Wolfea922032017-08-03 17:03:00 +02002003 if (has_rw_option) {
2004 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002005 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002006 }
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002007 flags &= ~BDRV_O_RDWR;
Kevin Wolfea922032017-08-03 17:03:00 +02002008 has_rw_option = true;
2009 break;
2010 case 'w':
2011 if (has_rw_option) {
2012 error_report("Only one -r/-w option may be given");
Max Reitzb32d7a32018-05-09 21:42:59 +02002013 return -EINVAL;
Kevin Wolfea922032017-08-03 17:03:00 +02002014 }
2015 flags |= BDRV_O_RDWR;
2016 has_rw_option = true;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002017 break;
2018 default:
2019 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002020 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002021 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002022 }
2023 }
2024
2025 if (optind != argc) {
2026 qemu_opts_reset(&reopen_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +02002027 qemuio_command_usage(&reopen_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +02002028 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002029 }
2030
Alberto Garciaa8003ec2018-09-06 12:37:01 +03002031 if (!writethrough != blk_enable_write_cache(blk) &&
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002032 blk_get_attached_dev(blk))
2033 {
2034 error_report("Cannot change cache.writeback: Device attached");
2035 qemu_opts_reset(&reopen_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +02002036 return -EBUSY;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002037 }
2038
Kevin Wolff3adefb2017-09-22 14:50:12 +02002039 if (!(flags & BDRV_O_RDWR)) {
2040 uint64_t orig_perm, orig_shared_perm;
2041
2042 bdrv_drain(bs);
2043
2044 blk_get_perm(blk, &orig_perm, &orig_shared_perm);
2045 blk_set_perm(blk,
2046 orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
2047 orig_shared_perm,
2048 &error_abort);
2049 }
2050
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002051 qopts = qemu_opts_find(&reopen_opts, NULL);
Alberto Garciadc900c32018-11-12 16:00:42 +02002052 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002053 qemu_opts_reset(&reopen_opts);
2054
Alberto Garciadc900c32018-11-12 16:00:42 +02002055 if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
2056 if (has_rw_option) {
2057 error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
2058 qobject_unref(opts);
2059 return -EINVAL;
2060 }
2061 } else {
2062 qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
2063 }
2064
2065 if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
2066 qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
2067 if (has_cache_option) {
2068 error_report("Cannot set both -c and the cache options");
2069 qobject_unref(opts);
2070 return -EINVAL;
2071 }
2072 } else {
2073 qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
2074 qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
2075 }
2076
Kevin Wolf1a63a902017-12-06 20:24:44 +01002077 bdrv_subtree_drained_begin(bs);
Alberto Garcia2e891722018-11-12 16:00:44 +02002078 brq = bdrv_reopen_queue(NULL, bs, opts);
Paolo Bonzini720150f2016-10-27 12:49:02 +02002079 bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
Kevin Wolf1a63a902017-12-06 20:24:44 +01002080 bdrv_subtree_drained_end(bs);
2081
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002082 if (local_err) {
2083 error_report_err(local_err);
Max Reitzb32d7a32018-05-09 21:42:59 +02002084 return -EINVAL;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002085 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002086
2087 blk_set_enable_write_cache(blk, !writethrough);
2088 return 0;
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002089}
2090
Max Reitzb32d7a32018-05-09 21:42:59 +02002091static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002092{
2093 int ret;
2094
Max Reitz4c7b7e92015-02-05 13:58:22 -05002095 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002096 if (ret < 0) {
2097 printf("Could not set breakpoint: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002098 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002099 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002100
2101 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002102}
2103
Max Reitzb32d7a32018-05-09 21:42:59 +02002104static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002105{
2106 int ret;
2107
Max Reitz4c7b7e92015-02-05 13:58:22 -05002108 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002109 if (ret < 0) {
2110 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002111 return ret;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002112 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002113
2114 return 0;
Fam Zheng4cc70e92013-11-20 10:01:54 +08002115}
2116
Kevin Wolf797ac582013-06-05 14:19:31 +02002117static const cmdinfo_t break_cmd = {
2118 .name = "break",
2119 .argmin = 2,
2120 .argmax = 2,
2121 .cfunc = break_f,
2122 .args = "event tag",
2123 .oneline = "sets a breakpoint on event and tags the stopped "
2124 "request as tag",
2125};
2126
Fam Zheng4cc70e92013-11-20 10:01:54 +08002127static const cmdinfo_t remove_break_cmd = {
2128 .name = "remove_break",
2129 .argmin = 1,
2130 .argmax = 1,
2131 .cfunc = remove_break_f,
2132 .args = "tag",
2133 .oneline = "remove a breakpoint by tag",
2134};
2135
Max Reitzb32d7a32018-05-09 21:42:59 +02002136static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002137{
2138 int ret;
2139
Max Reitz4c7b7e92015-02-05 13:58:22 -05002140 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002141 if (ret < 0) {
2142 printf("Could not resume request: %s\n", strerror(-ret));
Max Reitzb32d7a32018-05-09 21:42:59 +02002143 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +02002144 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002145
2146 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002147}
2148
2149static const cmdinfo_t resume_cmd = {
2150 .name = "resume",
2151 .argmin = 1,
2152 .argmax = 1,
2153 .cfunc = resume_f,
2154 .args = "tag",
2155 .oneline = "resumes the request tagged as tag",
2156};
2157
Max Reitzb32d7a32018-05-09 21:42:59 +02002158static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002159{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002160 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2161 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002162 }
Max Reitzb32d7a32018-05-09 21:42:59 +02002163 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002164}
2165
2166static const cmdinfo_t wait_break_cmd = {
2167 .name = "wait_break",
2168 .argmin = 1,
2169 .argmax = 1,
2170 .cfunc = wait_break_f,
2171 .args = "tag",
2172 .oneline = "waits for the suspension of a request",
2173};
2174
Max Reitzb32d7a32018-05-09 21:42:59 +02002175static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002176{
2177 abort();
2178}
2179
2180static const cmdinfo_t abort_cmd = {
2181 .name = "abort",
2182 .cfunc = abort_f,
2183 .flags = CMD_NOFILE_OK,
2184 .oneline = "simulate a program crash using abort(3)",
2185};
2186
Max Reitz0e82dc72014-12-08 10:48:10 +01002187static void sigraise_help(void)
2188{
2189 printf(
2190"\n"
2191" raises the given signal\n"
2192"\n"
2193" Example:\n"
2194" 'sigraise %i' - raises SIGTERM\n"
2195"\n"
2196" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2197" given to sigraise.\n"
2198"\n", SIGTERM);
2199}
2200
Max Reitzb32d7a32018-05-09 21:42:59 +02002201static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002202
2203static const cmdinfo_t sigraise_cmd = {
2204 .name = "sigraise",
2205 .cfunc = sigraise_f,
2206 .argmin = 1,
2207 .argmax = 1,
2208 .flags = CMD_NOFILE_OK,
2209 .args = "signal",
2210 .oneline = "raises a signal",
2211 .help = sigraise_help,
2212};
2213
Max Reitzb32d7a32018-05-09 21:42:59 +02002214static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002215{
John Snow9b0beaf2015-11-05 18:53:02 -05002216 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002217 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002218 print_cvtnum_err(sig, argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002219 return sig;
John Snow9b0beaf2015-11-05 18:53:02 -05002220 } else if (sig > NSIG) {
2221 printf("signal argument '%s' is too large to be a valid signal\n",
2222 argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002223 return -EINVAL;
Max Reitz0e82dc72014-12-08 10:48:10 +01002224 }
2225
2226 /* Using raise() to kill this process does not necessarily flush all open
2227 * streams. At least stdout and stderr (although the latter should be
2228 * non-buffered anyway) should be flushed, though. */
2229 fflush(stdout);
2230 fflush(stderr);
2231
2232 raise(sig);
Max Reitzb32d7a32018-05-09 21:42:59 +02002233
2234 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002235}
2236
Kevin Wolfcd33d022014-01-15 15:39:10 +01002237static void sleep_cb(void *opaque)
2238{
2239 bool *expired = opaque;
2240 *expired = true;
2241}
2242
Max Reitzb32d7a32018-05-09 21:42:59 +02002243static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002244{
2245 char *endptr;
2246 long ms;
2247 struct QEMUTimer *timer;
2248 bool expired = false;
2249
2250 ms = strtol(argv[1], &endptr, 0);
2251 if (ms < 0 || *endptr != '\0') {
2252 printf("%s is not a valid number\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002253 return -EINVAL;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002254 }
2255
2256 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2257 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2258
2259 while (!expired) {
2260 main_loop_wait(false);
2261 }
2262
2263 timer_free(timer);
Max Reitzb32d7a32018-05-09 21:42:59 +02002264 return 0;
Kevin Wolfcd33d022014-01-15 15:39:10 +01002265}
2266
2267static const cmdinfo_t sleep_cmd = {
2268 .name = "sleep",
2269 .argmin = 1,
2270 .argmax = 1,
2271 .cfunc = sleep_f,
2272 .flags = CMD_NOFILE_OK,
2273 .oneline = "waits for the given value in milliseconds",
2274};
2275
Kevin Wolff18a8342013-06-05 14:19:33 +02002276static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2277{
2278 if (cmd) {
2279 printf("%s ", cmd);
2280 } else {
2281 printf("%s ", ct->name);
2282 if (ct->altname) {
2283 printf("(or %s) ", ct->altname);
2284 }
2285 }
2286
2287 if (ct->args) {
2288 printf("%s ", ct->args);
2289 }
2290 printf("-- %s\n", ct->oneline);
2291}
2292
2293static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2294{
2295 help_oneline(cmd, ct);
2296 if (ct->help) {
2297 ct->help();
2298 }
2299}
2300
2301static void help_all(void)
2302{
2303 const cmdinfo_t *ct;
2304
2305 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2306 help_oneline(ct->name, ct);
2307 }
2308 printf("\nUse 'help commandname' for extended help.\n");
2309}
2310
Max Reitzb32d7a32018-05-09 21:42:59 +02002311static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002312{
2313 const cmdinfo_t *ct;
2314
2315 if (argc == 1) {
2316 help_all();
Max Reitzb32d7a32018-05-09 21:42:59 +02002317 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002318 }
2319
2320 ct = find_command(argv[1]);
2321 if (ct == NULL) {
2322 printf("command %s not found\n", argv[1]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002323 return -EINVAL;
Kevin Wolff18a8342013-06-05 14:19:33 +02002324 }
2325
2326 help_onecmd(argv[1], ct);
Max Reitzb32d7a32018-05-09 21:42:59 +02002327 return 0;
Kevin Wolff18a8342013-06-05 14:19:33 +02002328}
2329
2330static const cmdinfo_t help_cmd = {
2331 .name = "help",
2332 .altname = "?",
2333 .cfunc = help_f,
2334 .argmin = 0,
2335 .argmax = 1,
2336 .flags = CMD_FLAG_GLOBAL,
2337 .args = "[command]",
2338 .oneline = "help for one or all commands",
2339};
2340
Max Reitzb32d7a32018-05-09 21:42:59 +02002341int qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002342{
Paolo Bonzini15afd942016-10-27 12:49:03 +02002343 AioContext *ctx;
Kevin Wolfdd583292013-06-05 14:19:32 +02002344 char *input;
2345 const cmdinfo_t *ct;
2346 char **v;
2347 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +02002348 int ret = 0;
Kevin Wolfdd583292013-06-05 14:19:32 +02002349
2350 input = g_strdup(cmd);
2351 v = breakline(input, &c);
2352 if (c) {
2353 ct = find_command(v[0]);
2354 if (ct) {
Paolo Bonzini15afd942016-10-27 12:49:03 +02002355 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2356 aio_context_acquire(ctx);
Max Reitzb32d7a32018-05-09 21:42:59 +02002357 ret = command(blk, ct, c, v);
Paolo Bonzini15afd942016-10-27 12:49:03 +02002358 aio_context_release(ctx);
Kevin Wolfdd583292013-06-05 14:19:32 +02002359 } else {
2360 fprintf(stderr, "command \"%s\" not found\n", v[0]);
Max Reitzb32d7a32018-05-09 21:42:59 +02002361 ret = -EINVAL;
Kevin Wolfdd583292013-06-05 14:19:32 +02002362 }
2363 }
2364 g_free(input);
2365 g_free(v);
Max Reitzb32d7a32018-05-09 21:42:59 +02002366
2367 return ret;
Kevin Wolfdd583292013-06-05 14:19:32 +02002368}
2369
Kevin Wolf797ac582013-06-05 14:19:31 +02002370static void __attribute((constructor)) init_qemuio_commands(void)
2371{
2372 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002373 qemuio_add_command(&help_cmd);
2374 qemuio_add_command(&read_cmd);
2375 qemuio_add_command(&readv_cmd);
2376 qemuio_add_command(&write_cmd);
2377 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002378 qemuio_add_command(&aio_read_cmd);
2379 qemuio_add_command(&aio_write_cmd);
2380 qemuio_add_command(&aio_flush_cmd);
2381 qemuio_add_command(&flush_cmd);
2382 qemuio_add_command(&truncate_cmd);
2383 qemuio_add_command(&length_cmd);
2384 qemuio_add_command(&info_cmd);
2385 qemuio_add_command(&discard_cmd);
2386 qemuio_add_command(&alloc_cmd);
2387 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002388 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002389 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002390 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002391 qemuio_add_command(&resume_cmd);
2392 qemuio_add_command(&wait_break_cmd);
2393 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002394 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002395 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002396}