blob: 7ac1576d4c58d878ea849d11ed085c1f10932916 [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"
Kevin Wolf3d219942013-06-05 14:19:39 +020013#include "qemu-io.h"
Max Reitz4c7b7e92015-02-05 13:58:22 -050014#include "sysemu/block-backend.h"
15#include "block/block.h"
16#include "block/block_int.h" /* for info_f() */
Max Reitza8d8ecb2013-10-09 10:46:17 +020017#include "block/qapi.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Kevin Wolfcd33d022014-01-15 15:39:10 +010020#include "qemu/timer.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020021#include "qemu/cutils.h"
Kevin Wolf797ac582013-06-05 14:19:31 +020022
23#define CMD_NOFILE_OK 0x01
24
Stefan Weilf9883882014-03-05 22:23:00 +010025bool qemuio_misalign;
Kevin Wolf797ac582013-06-05 14:19:31 +020026
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020027static cmdinfo_t *cmdtab;
28static int ncmds;
29
30static int compare_cmdname(const void *a, const void *b)
31{
32 return strcmp(((const cmdinfo_t *)a)->name,
33 ((const cmdinfo_t *)b)->name);
34}
35
36void qemuio_add_command(const cmdinfo_t *ci)
37{
Markus Armbruster02c4f262014-08-19 10:31:09 +020038 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020039 cmdtab[ncmds - 1] = *ci;
40 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
41}
42
43int qemuio_command_usage(const cmdinfo_t *ci)
44{
45 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
46 return 0;
47}
48
Max Reitz4c7b7e92015-02-05 13:58:22 -050049static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020050{
51 if (ct->flags & CMD_FLAG_GLOBAL) {
52 return 1;
53 }
Max Reitz4c7b7e92015-02-05 13:58:22 -050054 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020055 fprintf(stderr, "no file open, try 'help open'\n");
56 return 0;
57 }
58 return 1;
59}
60
Max Reitz4c7b7e92015-02-05 13:58:22 -050061static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
Kevin Wolf3d219942013-06-05 14:19:39 +020062 char **argv)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020063{
64 char *cmd = argv[0];
65
Max Reitz4c7b7e92015-02-05 13:58:22 -050066 if (!init_check_command(blk, ct)) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020067 return 0;
68 }
69
70 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
71 if (ct->argmax == -1) {
72 fprintf(stderr,
73 "bad argument count %d to %s, expected at least %d arguments\n",
74 argc-1, cmd, ct->argmin);
75 } else if (ct->argmin == ct->argmax) {
76 fprintf(stderr,
77 "bad argument count %d to %s, expected %d arguments\n",
78 argc-1, cmd, ct->argmin);
79 } else {
80 fprintf(stderr,
81 "bad argument count %d to %s, expected between %d and %d arguments\n",
82 argc-1, cmd, ct->argmin, ct->argmax);
83 }
84 return 0;
85 }
86 optind = 0;
Max Reitz4c7b7e92015-02-05 13:58:22 -050087 return ct->cfunc(blk, argc, argv);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020088}
89
90static const cmdinfo_t *find_command(const char *cmd)
91{
92 cmdinfo_t *ct;
93
94 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
95 if (strcmp(ct->name, cmd) == 0 ||
96 (ct->altname && strcmp(ct->altname, cmd) == 0))
97 {
98 return (const cmdinfo_t *)ct;
99 }
100 }
101 return NULL;
102}
103
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100104/* Invoke fn() for commands with a matching prefix */
105void qemuio_complete_command(const char *input,
106 void (*fn)(const char *cmd, void *opaque),
107 void *opaque)
108{
109 cmdinfo_t *ct;
110 size_t input_len = strlen(input);
111
112 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
113 if (strncmp(input, ct->name, input_len) == 0) {
114 fn(ct->name, opaque);
115 }
116 }
117}
118
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200119static char **breakline(char *input, int *count)
120{
121 int c = 0;
122 char *p;
Markus Armbruster5839e532014-08-19 10:31:08 +0200123 char **rval = g_new0(char *, 1);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200124
125 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
126 if (!*p) {
127 continue;
128 }
129 c++;
Markus Armbruster08193dd2014-08-19 10:31:10 +0200130 rval = g_renew(char *, rval, (c + 1));
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200131 rval[c - 1] = p;
132 rval[c] = NULL;
133 }
134 *count = c;
135 return rval;
136}
137
Kevin Wolf797ac582013-06-05 14:19:31 +0200138static int64_t cvtnum(const char *s)
139{
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100140 int err;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100141 uint64_t value;
John Snowef5a7882015-11-05 18:53:03 -0500142
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100143 err = qemu_strtosz(s, NULL, &value);
144 if (err < 0) {
145 return err;
146 }
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +0100147 if (value > INT64_MAX) {
148 return -ERANGE;
149 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +0100150 return value;
Kevin Wolf797ac582013-06-05 14:19:31 +0200151}
152
John Snowa9ecfa02015-11-05 18:53:04 -0500153static void print_cvtnum_err(int64_t rc, const char *arg)
154{
155 switch (rc) {
156 case -EINVAL:
157 printf("Parsing error: non-numeric argument,"
158 " or extraneous/unrecognized suffix -- %s\n", arg);
159 break;
160 case -ERANGE:
161 printf("Parsing error: argument too large -- %s\n", arg);
162 break;
163 default:
164 printf("Parsing error: %s\n", arg);
165 }
166}
167
Kevin Wolf0b613882013-06-05 14:19:38 +0200168#define EXABYTES(x) ((long long)(x) << 60)
169#define PETABYTES(x) ((long long)(x) << 50)
170#define TERABYTES(x) ((long long)(x) << 40)
171#define GIGABYTES(x) ((long long)(x) << 30)
172#define MEGABYTES(x) ((long long)(x) << 20)
173#define KILOBYTES(x) ((long long)(x) << 10)
174
175#define TO_EXABYTES(x) ((x) / EXABYTES(1))
176#define TO_PETABYTES(x) ((x) / PETABYTES(1))
177#define TO_TERABYTES(x) ((x) / TERABYTES(1))
178#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
179#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
180#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
181
182static void cvtstr(double value, char *str, size_t size)
183{
184 char *trim;
185 const char *suffix;
186
187 if (value >= EXABYTES(1)) {
188 suffix = " EiB";
189 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
190 } else if (value >= PETABYTES(1)) {
191 suffix = " PiB";
192 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
193 } else if (value >= TERABYTES(1)) {
194 suffix = " TiB";
195 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
196 } else if (value >= GIGABYTES(1)) {
197 suffix = " GiB";
198 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
199 } else if (value >= MEGABYTES(1)) {
200 suffix = " MiB";
201 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
202 } else if (value >= KILOBYTES(1)) {
203 suffix = " KiB";
204 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
205 } else {
206 suffix = " bytes";
207 snprintf(str, size - 6, "%f", value);
208 }
209
210 trim = strstr(str, ".000");
211 if (trim) {
212 strcpy(trim, suffix);
213 } else {
214 strcat(str, suffix);
215 }
216}
217
218
219
220static struct timeval tsub(struct timeval t1, struct timeval t2)
221{
222 t1.tv_usec -= t2.tv_usec;
223 if (t1.tv_usec < 0) {
224 t1.tv_usec += 1000000;
225 t1.tv_sec--;
226 }
227 t1.tv_sec -= t2.tv_sec;
228 return t1;
229}
230
231static double tdiv(double value, struct timeval tv)
232{
233 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
234}
235
236#define HOURS(sec) ((sec) / (60 * 60))
237#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
238#define SECONDS(sec) ((sec) % 60)
239
240enum {
241 DEFAULT_TIME = 0x0,
242 TERSE_FIXED_TIME = 0x1,
243 VERBOSE_FIXED_TIME = 0x2,
244};
245
246static void timestr(struct timeval *tv, char *ts, size_t size, int format)
247{
248 double usec = (double)tv->tv_usec / 1000000.0;
249
250 if (format & TERSE_FIXED_TIME) {
251 if (!HOURS(tv->tv_sec)) {
252 snprintf(ts, size, "%u:%02u.%02u",
253 (unsigned int) MINUTES(tv->tv_sec),
254 (unsigned int) SECONDS(tv->tv_sec),
255 (unsigned int) (usec * 100));
256 return;
257 }
258 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
259 }
260
261 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
262 snprintf(ts, size, "%u:%02u:%02u.%02u",
263 (unsigned int) HOURS(tv->tv_sec),
264 (unsigned int) MINUTES(tv->tv_sec),
265 (unsigned int) SECONDS(tv->tv_sec),
266 (unsigned int) (usec * 100));
267 } else {
268 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
269 }
270}
271
Kevin Wolf797ac582013-06-05 14:19:31 +0200272/*
273 * Parse the pattern argument to various sub-commands.
274 *
275 * Because the pattern is used as an argument to memset it must evaluate
276 * to an unsigned integer that fits into a single byte.
277 */
278static int parse_pattern(const char *arg)
279{
280 char *endptr = NULL;
281 long pattern;
282
283 pattern = strtol(arg, &endptr, 0);
284 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
285 printf("%s is not a valid pattern byte\n", arg);
286 return -1;
287 }
288
289 return pattern;
290}
291
292/*
293 * Memory allocation helpers.
294 *
295 * Make sure memory is aligned by default, or purposefully misaligned if
296 * that is specified on the command line.
297 */
298
299#define MISALIGN_OFFSET 16
Max Reitz4c7b7e92015-02-05 13:58:22 -0500300static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
Kevin Wolf797ac582013-06-05 14:19:31 +0200301{
302 void *buf;
303
304 if (qemuio_misalign) {
305 len += MISALIGN_OFFSET;
306 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500307 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200308 memset(buf, pattern, len);
309 if (qemuio_misalign) {
310 buf += MISALIGN_OFFSET;
311 }
312 return buf;
313}
314
315static void qemu_io_free(void *p)
316{
317 if (qemuio_misalign) {
318 p -= MISALIGN_OFFSET;
319 }
320 qemu_vfree(p);
321}
322
John Snow9b0beaf2015-11-05 18:53:02 -0500323static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200324{
John Snow9b0beaf2015-11-05 18:53:02 -0500325 uint64_t i;
326 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200327 const uint8_t *p;
328
329 for (i = 0, p = buffer; i < len; i += 16) {
330 const uint8_t *s = p;
331
332 printf("%08" PRIx64 ": ", offset + i);
333 for (j = 0; j < 16 && i + j < len; j++, p++) {
334 printf("%02x ", *p);
335 }
336 printf(" ");
337 for (j = 0; j < 16 && i + j < len; j++, s++) {
338 if (isalnum(*s)) {
339 printf("%c", *s);
340 } else {
341 printf(".");
342 }
343 }
344 printf("\n");
345 }
346}
347
348static void print_report(const char *op, struct timeval *t, int64_t offset,
Eric Blakedc388522016-05-07 21:16:42 -0600349 int64_t count, int64_t total, int cnt, bool Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200350{
351 char s1[64], s2[64], ts[64];
352
353 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
354 if (!Cflag) {
355 cvtstr((double)total, s1, sizeof(s1));
356 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500357 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200358 op, total, count, offset);
359 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
360 s1, cnt, ts, s2, tdiv((double)cnt, *t));
361 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500362 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200363 total, cnt, ts,
364 tdiv((double)total, *t),
365 tdiv((double)cnt, *t));
366 }
367}
368
369/*
370 * Parse multiple length statements for vectored I/O, and construct an I/O
371 * vector matching it.
372 */
373static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500374create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200375 int pattern)
376{
377 size_t *sizes = g_new0(size_t, nr_iov);
378 size_t count = 0;
379 void *buf = NULL;
380 void *p;
381 int i;
382
383 for (i = 0; i < nr_iov; i++) {
384 char *arg = argv[i];
385 int64_t len;
386
387 len = cvtnum(arg);
388 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500389 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200390 goto fail;
391 }
392
Alberto Garcia3026c462017-01-31 18:09:54 +0200393 if (len > BDRV_REQUEST_MAX_BYTES) {
394 printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
395 (uint64_t)BDRV_REQUEST_MAX_BYTES);
396 goto fail;
397 }
398
399 if (count > BDRV_REQUEST_MAX_BYTES - len) {
400 printf("The total number of bytes exceed the maximum size %" PRIu64
401 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
Kevin Wolf797ac582013-06-05 14:19:31 +0200402 goto fail;
403 }
404
Kevin Wolf797ac582013-06-05 14:19:31 +0200405 sizes[i] = len;
406 count += len;
407 }
408
409 qemu_iovec_init(qiov, nr_iov);
410
Max Reitz4c7b7e92015-02-05 13:58:22 -0500411 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200412
413 for (i = 0; i < nr_iov; i++) {
414 qemu_iovec_add(qiov, p, sizes[i]);
415 p += sizes[i];
416 }
417
418fail:
419 g_free(sizes);
420 return buf;
421}
422
John Snow9b0beaf2015-11-05 18:53:02 -0500423static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
424 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200425{
John Snow9b0beaf2015-11-05 18:53:02 -0500426 if (count > INT_MAX) {
427 return -ERANGE;
428 }
429
Max Reitz4c7b7e92015-02-05 13:58:22 -0500430 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200431 if (*total < 0) {
432 return *total;
433 }
434 return 1;
435}
436
John Snow9b0beaf2015-11-05 18:53:02 -0500437static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Eric Blake770e0e02016-05-07 21:16:44 -0600438 int64_t count, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200439{
John Snow9b0beaf2015-11-05 18:53:02 -0500440 if (count > INT_MAX) {
441 return -ERANGE;
442 }
443
Eric Blake770e0e02016-05-07 21:16:44 -0600444 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count, flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200445 if (*total < 0) {
446 return *total;
447 }
448 return 1;
449}
450
451typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500452 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200453 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500454 int64_t count;
455 int64_t *total;
Eric Blake770e0e02016-05-07 21:16:44 -0600456 int flags;
Kevin Wolf797ac582013-06-05 14:19:31 +0200457 int ret;
458 bool done;
459} CoWriteZeroes;
460
Eric Blaked004bd52016-05-24 16:25:20 -0600461static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
Kevin Wolf797ac582013-06-05 14:19:31 +0200462{
463 CoWriteZeroes *data = opaque;
464
Eric Blaked004bd52016-05-24 16:25:20 -0600465 data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->count,
466 data->flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200467 data->done = true;
468 if (data->ret < 0) {
469 *data->total = data->ret;
470 return;
471 }
472
473 *data->total = data->count;
474}
475
Eric Blaked004bd52016-05-24 16:25:20 -0600476static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
477 int64_t count, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200478{
479 Coroutine *co;
480 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500481 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200482 .offset = offset,
483 .count = count,
484 .total = total,
Eric Blake770e0e02016-05-07 21:16:44 -0600485 .flags = flags,
Kevin Wolf797ac582013-06-05 14:19:31 +0200486 .done = false,
487 };
488
Max Reitza3674672016-06-20 16:26:22 +0200489 if (count > INT_MAX) {
John Snow9b0beaf2015-11-05 18:53:02 -0500490 return -ERANGE;
491 }
492
Paolo Bonzini0b8b8752016-07-04 19:10:01 +0200493 co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
494 qemu_coroutine_enter(co);
Kevin Wolf797ac582013-06-05 14:19:31 +0200495 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500496 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200497 }
498 if (data.ret < 0) {
499 return data.ret;
500 } else {
501 return 1;
502 }
503}
504
Max Reitz4c7b7e92015-02-05 13:58:22 -0500505static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500506 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200507{
508 int ret;
509
Max Reitza3674672016-06-20 16:26:22 +0200510 if (count >> 9 > BDRV_REQUEST_MAX_SECTORS) {
John Snow9b0beaf2015-11-05 18:53:02 -0500511 return -ERANGE;
512 }
513
Pavel Butsykinfe5c1352016-07-22 11:17:40 +0300514 ret = blk_pwrite_compressed(blk, offset, buf, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200515 if (ret < 0) {
516 return ret;
517 }
518 *total = count;
519 return 1;
520}
521
Max Reitz4c7b7e92015-02-05 13:58:22 -0500522static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500523 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200524{
John Snow9b0beaf2015-11-05 18:53:02 -0500525 if (count > INT_MAX) {
526 return -ERANGE;
527 }
528
Max Reitz4c7b7e92015-02-05 13:58:22 -0500529 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200530 if (*total < 0) {
531 return *total;
532 }
533 return 1;
534}
535
Max Reitz4c7b7e92015-02-05 13:58:22 -0500536static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500537 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200538{
John Snow9b0beaf2015-11-05 18:53:02 -0500539 if (count > INT_MAX) {
540 return -ERANGE;
541 }
542
Max Reitz4c7b7e92015-02-05 13:58:22 -0500543 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200544 if (*total < 0) {
545 return *total;
546 }
547 return 1;
548}
549
550#define NOT_DONE 0x7fffffff
551static void aio_rw_done(void *opaque, int ret)
552{
553 *(int *)opaque = ret;
554}
555
Max Reitz4c7b7e92015-02-05 13:58:22 -0500556static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200557 int64_t offset, int *total)
558{
559 int async_ret = NOT_DONE;
560
Eric Blake7b3f9712016-05-06 10:26:44 -0600561 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200562 while (async_ret == NOT_DONE) {
563 main_loop_wait(false);
564 }
565
566 *total = qiov->size;
567 return async_ret < 0 ? async_ret : 1;
568}
569
Max Reitz4c7b7e92015-02-05 13:58:22 -0500570static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600571 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200572{
573 int async_ret = NOT_DONE;
574
Eric Blake770e0e02016-05-07 21:16:44 -0600575 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200576 while (async_ret == NOT_DONE) {
577 main_loop_wait(false);
578 }
579
580 *total = qiov->size;
581 return async_ret < 0 ? async_ret : 1;
582}
583
Kevin Wolf797ac582013-06-05 14:19:31 +0200584static void read_help(void)
585{
586 printf(
587"\n"
588" reads a range of bytes from the given offset\n"
589"\n"
590" Example:\n"
591" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
592"\n"
593" Reads a segment of the currently open file, optionally dumping it to the\n"
594" standard output stream (with -v option) for subsequent inspection.\n"
595" -b, -- read from the VM state rather than the virtual disk\n"
596" -C, -- report statistics in a machine parsable format\n"
597" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600598" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200599" -P, -- use a pattern to verify read data\n"
600" -q, -- quiet mode, do not show I/O statistics\n"
601" -s, -- start offset for pattern verification (only with -P)\n"
602" -v, -- dump buffer to standard output\n"
603"\n");
604}
605
Max Reitz4c7b7e92015-02-05 13:58:22 -0500606static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200607
608static const cmdinfo_t read_cmd = {
609 .name = "read",
610 .altname = "r",
611 .cfunc = read_f,
612 .argmin = 2,
613 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600614 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200615 .oneline = "reads a number of bytes at a specified offset",
616 .help = read_help,
617};
618
Max Reitz4c7b7e92015-02-05 13:58:22 -0500619static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200620{
621 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600622 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600623 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200624 int c, cnt;
625 char *buf;
626 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500627 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200628 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500629 int64_t total = 0;
630 int pattern = 0;
631 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200632
Eric Blakeb062ad82015-05-12 09:10:56 -0600633 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200634 switch (c) {
635 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600636 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200637 break;
638 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600639 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200640 break;
641 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600642 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200643 pattern_count = cvtnum(optarg);
644 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500645 print_cvtnum_err(pattern_count, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200646 return 0;
647 }
648 break;
649 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600650 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200651 break;
652 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600653 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200654 pattern = parse_pattern(optarg);
655 if (pattern < 0) {
656 return 0;
657 }
658 break;
659 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600660 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200661 break;
662 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600663 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200664 pattern_offset = cvtnum(optarg);
665 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500666 print_cvtnum_err(pattern_offset, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200667 return 0;
668 }
669 break;
670 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600671 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200672 break;
673 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200674 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200675 }
676 }
677
678 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200679 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200680 }
681
Kevin Wolf797ac582013-06-05 14:19:31 +0200682 offset = cvtnum(argv[optind]);
683 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500684 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200685 return 0;
686 }
687
688 optind++;
689 count = cvtnum(argv[optind]);
690 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500691 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200692 return 0;
Alberto Garcia3026c462017-01-31 18:09:54 +0200693 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -0500694 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +0200695 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
John Snow9b0beaf2015-11-05 18:53:02 -0500696 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200697 }
698
699 if (!Pflag && (lflag || sflag)) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200700 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200701 }
702
703 if (!lflag) {
704 pattern_count = count - pattern_offset;
705 }
706
707 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
708 printf("pattern verification range exceeds end of read data\n");
709 return 0;
710 }
711
Eric Blake093ea232016-05-07 21:16:43 -0600712 if (bflag) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200713 if (offset & 0x1ff) {
714 printf("offset %" PRId64 " is not sector aligned\n",
715 offset);
716 return 0;
717 }
718 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -0500719 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200720 count);
721 return 0;
722 }
723 }
724
Max Reitz4c7b7e92015-02-05 13:58:22 -0500725 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200726
727 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -0600728 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500729 cnt = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200730 } else {
Eric Blake7b3f9712016-05-06 10:26:44 -0600731 cnt = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200732 }
733 gettimeofday(&t2, NULL);
734
735 if (cnt < 0) {
736 printf("read failed: %s\n", strerror(-cnt));
737 goto out;
738 }
739
740 if (Pflag) {
741 void *cmp_buf = g_malloc(pattern_count);
742 memset(cmp_buf, pattern, pattern_count);
743 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
744 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500745 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200746 offset + pattern_offset, pattern_count);
747 }
748 g_free(cmp_buf);
749 }
750
751 if (qflag) {
752 goto out;
753 }
754
755 if (vflag) {
756 dump_buffer(buf, offset, count);
757 }
758
759 /* Finally, report back -- -C gives a parsable format */
760 t2 = tsub(t2, t1);
761 print_report("read", &t2, offset, count, total, cnt, Cflag);
762
763out:
764 qemu_io_free(buf);
765
766 return 0;
767}
768
769static void readv_help(void)
770{
771 printf(
772"\n"
773" reads a range of bytes from the given offset into multiple buffers\n"
774"\n"
775" Example:\n"
776" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
777"\n"
778" Reads a segment of the currently open file, optionally dumping it to the\n"
779" standard output stream (with -v option) for subsequent inspection.\n"
780" Uses multiple iovec buffers if more than one byte range is specified.\n"
781" -C, -- report statistics in a machine parsable format\n"
782" -P, -- use a pattern to verify read data\n"
783" -v, -- dump buffer to standard output\n"
784" -q, -- quiet mode, do not show I/O statistics\n"
785"\n");
786}
787
Max Reitz4c7b7e92015-02-05 13:58:22 -0500788static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200789
790static const cmdinfo_t readv_cmd = {
791 .name = "readv",
792 .cfunc = readv_f,
793 .argmin = 2,
794 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600795 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200796 .oneline = "reads a number of bytes at a specified offset",
797 .help = readv_help,
798};
799
Max Reitz4c7b7e92015-02-05 13:58:22 -0500800static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200801{
802 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600803 bool Cflag = false, qflag = false, vflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200804 int c, cnt;
805 char *buf;
806 int64_t offset;
807 /* Some compilers get confused and warn if this is not initialized. */
808 int total = 0;
809 int nr_iov;
810 QEMUIOVector qiov;
811 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600812 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200813
Eric Blakeb062ad82015-05-12 09:10:56 -0600814 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200815 switch (c) {
816 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600817 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200818 break;
819 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600820 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200821 pattern = parse_pattern(optarg);
822 if (pattern < 0) {
823 return 0;
824 }
825 break;
826 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600827 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200828 break;
829 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600830 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200831 break;
832 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200833 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200834 }
835 }
836
837 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200838 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200839 }
840
841
842 offset = cvtnum(argv[optind]);
843 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500844 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200845 return 0;
846 }
847 optind++;
848
Kevin Wolf797ac582013-06-05 14:19:31 +0200849 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500850 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200851 if (buf == NULL) {
852 return 0;
853 }
854
855 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -0500856 cnt = do_aio_readv(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200857 gettimeofday(&t2, NULL);
858
859 if (cnt < 0) {
860 printf("readv failed: %s\n", strerror(-cnt));
861 goto out;
862 }
863
864 if (Pflag) {
865 void *cmp_buf = g_malloc(qiov.size);
866 memset(cmp_buf, pattern, qiov.size);
867 if (memcmp(buf, cmp_buf, qiov.size)) {
868 printf("Pattern verification failed at offset %"
869 PRId64 ", %zd bytes\n", offset, qiov.size);
870 }
871 g_free(cmp_buf);
872 }
873
874 if (qflag) {
875 goto out;
876 }
877
878 if (vflag) {
879 dump_buffer(buf, offset, qiov.size);
880 }
881
882 /* Finally, report back -- -C gives a parsable format */
883 t2 = tsub(t2, t1);
884 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
885
886out:
887 qemu_iovec_destroy(&qiov);
888 qemu_io_free(buf);
889 return 0;
890}
891
892static void write_help(void)
893{
894 printf(
895"\n"
896" writes a range of bytes from the given offset\n"
897"\n"
898" Example:\n"
899" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
900"\n"
901" Writes into a segment of the currently open file, using a buffer\n"
902" filled with a set pattern (0xcdcdcdcd).\n"
903" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500904" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -0600905" -f, -- use Force Unit Access semantics\n"
Eric Blake093ea232016-05-07 21:16:43 -0600906" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200907" -P, -- use different pattern to fill file\n"
908" -C, -- report statistics in a machine parsable format\n"
909" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -0600910" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -0600911" -z, -- write zeroes using blk_co_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200912"\n");
913}
914
Max Reitz4c7b7e92015-02-05 13:58:22 -0500915static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200916
917static const cmdinfo_t write_cmd = {
918 .name = "write",
919 .altname = "w",
920 .cfunc = write_f,
921 .argmin = 2,
922 .argmax = -1,
Eric Blakec2e001c2016-05-07 21:16:45 -0600923 .args = "[-bcCfquz] [-P pattern] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200924 .oneline = "writes a number of bytes at a specified offset",
925 .help = write_help,
926};
927
Max Reitz4c7b7e92015-02-05 13:58:22 -0500928static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200929{
930 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600931 bool Cflag = false, qflag = false, bflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600932 bool Pflag = false, zflag = false, cflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -0600933 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200934 int c, cnt;
935 char *buf = NULL;
936 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500937 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200938 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500939 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200940 int pattern = 0xcd;
941
Eric Blakec2e001c2016-05-07 21:16:45 -0600942 while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200943 switch (c) {
944 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600945 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200946 break;
947 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -0600948 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200949 break;
950 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600951 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200952 break;
Eric Blake770e0e02016-05-07 21:16:44 -0600953 case 'f':
954 flags |= BDRV_REQ_FUA;
955 break;
Kevin Wolf797ac582013-06-05 14:19:31 +0200956 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600957 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200958 break;
959 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600960 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200961 pattern = parse_pattern(optarg);
962 if (pattern < 0) {
963 return 0;
964 }
965 break;
966 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600967 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200968 break;
Eric Blakec2e001c2016-05-07 21:16:45 -0600969 case 'u':
970 flags |= BDRV_REQ_MAY_UNMAP;
971 break;
Kevin Wolf797ac582013-06-05 14:19:31 +0200972 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -0600973 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200974 break;
975 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200976 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200977 }
978 }
979
980 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200981 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200982 }
983
Eric Blake093ea232016-05-07 21:16:43 -0600984 if (bflag && zflag) {
985 printf("-b and -z cannot be specified at the same time\n");
Kevin Wolf797ac582013-06-05 14:19:31 +0200986 return 0;
987 }
988
Eric Blake770e0e02016-05-07 21:16:44 -0600989 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
990 printf("-f and -b or -c cannot be specified at the same time\n");
991 return 0;
992 }
993
Eric Blakec2e001c2016-05-07 21:16:45 -0600994 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
995 printf("-u requires -z to be specified\n");
996 return 0;
997 }
998
Kevin Wolf797ac582013-06-05 14:19:31 +0200999 if (zflag && Pflag) {
1000 printf("-z and -P cannot be specified at the same time\n");
1001 return 0;
1002 }
1003
1004 offset = cvtnum(argv[optind]);
1005 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001006 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001007 return 0;
1008 }
1009
1010 optind++;
1011 count = cvtnum(argv[optind]);
1012 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001013 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001014 return 0;
Alberto Garcia3026c462017-01-31 18:09:54 +02001015 } else if (count > BDRV_REQUEST_MAX_BYTES) {
John Snow9b0beaf2015-11-05 18:53:02 -05001016 printf("length cannot exceed %" PRIu64 ", given %s\n",
Alberto Garcia3026c462017-01-31 18:09:54 +02001017 (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
John Snow9b0beaf2015-11-05 18:53:02 -05001018 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001019 }
1020
Eric Blake093ea232016-05-07 21:16:43 -06001021 if (bflag || cflag) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001022 if (offset & 0x1ff) {
1023 printf("offset %" PRId64 " is not sector aligned\n",
1024 offset);
1025 return 0;
1026 }
1027
1028 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -05001029 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001030 count);
1031 return 0;
1032 }
1033 }
1034
1035 if (!zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001036 buf = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001037 }
1038
1039 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -06001040 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001041 cnt = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001042 } else if (zflag) {
Eric Blaked004bd52016-05-24 16:25:20 -06001043 cnt = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001044 } else if (cflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001045 cnt = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001046 } else {
Eric Blake770e0e02016-05-07 21:16:44 -06001047 cnt = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001048 }
1049 gettimeofday(&t2, NULL);
1050
1051 if (cnt < 0) {
1052 printf("write failed: %s\n", strerror(-cnt));
1053 goto out;
1054 }
1055
1056 if (qflag) {
1057 goto out;
1058 }
1059
1060 /* Finally, report back -- -C gives a parsable format */
1061 t2 = tsub(t2, t1);
1062 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1063
1064out:
1065 if (!zflag) {
1066 qemu_io_free(buf);
1067 }
1068
1069 return 0;
1070}
1071
1072static void
1073writev_help(void)
1074{
1075 printf(
1076"\n"
1077" writes a range of bytes from the given offset source from multiple buffers\n"
1078"\n"
1079" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001080" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001081"\n"
1082" Writes into a segment of the currently open file, using a buffer\n"
1083" filled with a set pattern (0xcdcdcdcd).\n"
1084" -P, -- use different pattern to fill file\n"
1085" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001086" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001087" -q, -- quiet mode, do not show I/O statistics\n"
1088"\n");
1089}
1090
Max Reitz4c7b7e92015-02-05 13:58:22 -05001091static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001092
1093static const cmdinfo_t writev_cmd = {
1094 .name = "writev",
1095 .cfunc = writev_f,
1096 .argmin = 2,
1097 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001098 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001099 .oneline = "writes a number of bytes at a specified offset",
1100 .help = writev_help,
1101};
1102
Max Reitz4c7b7e92015-02-05 13:58:22 -05001103static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001104{
1105 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001106 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001107 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001108 int c, cnt;
1109 char *buf;
1110 int64_t offset;
1111 /* Some compilers get confused and warn if this is not initialized. */
1112 int total = 0;
1113 int nr_iov;
1114 int pattern = 0xcd;
1115 QEMUIOVector qiov;
1116
Eric Blake4ca1d342016-05-16 10:43:01 -06001117 while ((c = getopt(argc, argv, "CfqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001118 switch (c) {
1119 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001120 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001121 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001122 case 'f':
1123 flags |= BDRV_REQ_FUA;
1124 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001125 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001126 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001127 break;
1128 case 'P':
1129 pattern = parse_pattern(optarg);
1130 if (pattern < 0) {
1131 return 0;
1132 }
1133 break;
1134 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001135 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001136 }
1137 }
1138
1139 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001140 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001141 }
1142
1143 offset = cvtnum(argv[optind]);
1144 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001145 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001146 return 0;
1147 }
1148 optind++;
1149
Kevin Wolf797ac582013-06-05 14:19:31 +02001150 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001151 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001152 if (buf == NULL) {
1153 return 0;
1154 }
1155
1156 gettimeofday(&t1, NULL);
Eric Blake770e0e02016-05-07 21:16:44 -06001157 cnt = do_aio_writev(blk, &qiov, offset, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001158 gettimeofday(&t2, NULL);
1159
1160 if (cnt < 0) {
1161 printf("writev failed: %s\n", strerror(-cnt));
1162 goto out;
1163 }
1164
1165 if (qflag) {
1166 goto out;
1167 }
1168
1169 /* Finally, report back -- -C gives a parsable format */
1170 t2 = tsub(t2, t1);
1171 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1172out:
1173 qemu_iovec_destroy(&qiov);
1174 qemu_io_free(buf);
1175 return 0;
1176}
1177
Kevin Wolf797ac582013-06-05 14:19:31 +02001178struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001179 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001180 QEMUIOVector qiov;
1181 int64_t offset;
1182 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001183 bool qflag;
1184 bool vflag;
1185 bool Cflag;
1186 bool Pflag;
1187 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001188 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001189 int pattern;
1190 struct timeval t1;
1191};
1192
1193static void aio_write_done(void *opaque, int ret)
1194{
1195 struct aio_ctx *ctx = opaque;
1196 struct timeval t2;
1197
1198 gettimeofday(&t2, NULL);
1199
1200
1201 if (ret < 0) {
1202 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001203 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001204 goto out;
1205 }
1206
Max Reitz4c7b7e92015-02-05 13:58:22 -05001207 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001208
Kevin Wolf797ac582013-06-05 14:19:31 +02001209 if (ctx->qflag) {
1210 goto out;
1211 }
1212
1213 /* Finally, report back -- -C gives a parsable format */
1214 t2 = tsub(t2, ctx->t1);
1215 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1216 ctx->qiov.size, 1, ctx->Cflag);
1217out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001218 if (!ctx->zflag) {
1219 qemu_io_free(ctx->buf);
1220 qemu_iovec_destroy(&ctx->qiov);
1221 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001222 g_free(ctx);
1223}
1224
1225static void aio_read_done(void *opaque, int ret)
1226{
1227 struct aio_ctx *ctx = opaque;
1228 struct timeval t2;
1229
1230 gettimeofday(&t2, NULL);
1231
1232 if (ret < 0) {
1233 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001234 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001235 goto out;
1236 }
1237
1238 if (ctx->Pflag) {
1239 void *cmp_buf = g_malloc(ctx->qiov.size);
1240
1241 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1242 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1243 printf("Pattern verification failed at offset %"
1244 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1245 }
1246 g_free(cmp_buf);
1247 }
1248
Max Reitz4c7b7e92015-02-05 13:58:22 -05001249 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001250
Kevin Wolf797ac582013-06-05 14:19:31 +02001251 if (ctx->qflag) {
1252 goto out;
1253 }
1254
1255 if (ctx->vflag) {
1256 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1257 }
1258
1259 /* Finally, report back -- -C gives a parsable format */
1260 t2 = tsub(t2, ctx->t1);
1261 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1262 ctx->qiov.size, 1, ctx->Cflag);
1263out:
1264 qemu_io_free(ctx->buf);
1265 qemu_iovec_destroy(&ctx->qiov);
1266 g_free(ctx);
1267}
1268
1269static void aio_read_help(void)
1270{
1271 printf(
1272"\n"
1273" asynchronously reads a range of bytes from the given offset\n"
1274"\n"
1275" Example:\n"
1276" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1277"\n"
1278" Reads a segment of the currently open file, optionally dumping it to the\n"
1279" standard output stream (with -v option) for subsequent inspection.\n"
1280" The read is performed asynchronously and the aio_flush command must be\n"
1281" used to ensure all outstanding aio requests have been completed.\n"
1282" -C, -- report statistics in a machine parsable format\n"
1283" -P, -- use a pattern to verify read data\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001284" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001285" -v, -- dump buffer to standard output\n"
1286" -q, -- quiet mode, do not show I/O statistics\n"
1287"\n");
1288}
1289
Max Reitz4c7b7e92015-02-05 13:58:22 -05001290static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001291
1292static const cmdinfo_t aio_read_cmd = {
1293 .name = "aio_read",
1294 .cfunc = aio_read_f,
1295 .argmin = 2,
1296 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001297 .args = "[-Ciqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001298 .oneline = "asynchronously reads a number of bytes",
1299 .help = aio_read_help,
1300};
1301
Max Reitz4c7b7e92015-02-05 13:58:22 -05001302static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001303{
1304 int nr_iov, c;
1305 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1306
Max Reitz4c7b7e92015-02-05 13:58:22 -05001307 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001308 while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001309 switch (c) {
1310 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001311 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001312 break;
1313 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001314 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001315 ctx->pattern = parse_pattern(optarg);
1316 if (ctx->pattern < 0) {
1317 g_free(ctx);
1318 return 0;
1319 }
1320 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001321 case 'i':
1322 printf("injecting invalid read request\n");
1323 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1324 g_free(ctx);
1325 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001326 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001327 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001328 break;
1329 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001330 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001331 break;
1332 default:
1333 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001334 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001335 }
1336 }
1337
1338 if (optind > argc - 2) {
1339 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001340 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001341 }
1342
1343 ctx->offset = cvtnum(argv[optind]);
1344 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001345 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001346 g_free(ctx);
1347 return 0;
1348 }
1349 optind++;
1350
Kevin Wolf797ac582013-06-05 14:19:31 +02001351 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001352 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001353 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001354 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001355 g_free(ctx);
1356 return 0;
1357 }
1358
1359 gettimeofday(&ctx->t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001360 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1361 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001362 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Kevin Wolf797ac582013-06-05 14:19:31 +02001363 return 0;
1364}
1365
1366static void aio_write_help(void)
1367{
1368 printf(
1369"\n"
1370" asynchronously writes a range of bytes from the given offset source\n"
1371" from multiple buffers\n"
1372"\n"
1373" Example:\n"
1374" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1375"\n"
1376" Writes into a segment of the currently open file, using a buffer\n"
1377" filled with a set pattern (0xcdcdcdcd).\n"
1378" The write is performed asynchronously and the aio_flush command must be\n"
1379" used to ensure all outstanding aio requests have been completed.\n"
1380" -P, -- use different pattern to fill file\n"
1381" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001382" -f, -- use Force Unit Access semantics\n"
Eric Blake37546ff2016-05-16 10:43:03 -06001383" -i, -- treat request as invalid, for exercising stats\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001384" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001385" -u, -- with -z, allow unmapping\n"
Eric Blaked004bd52016-05-24 16:25:20 -06001386" -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001387"\n");
1388}
1389
Max Reitz4c7b7e92015-02-05 13:58:22 -05001390static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001391
1392static const cmdinfo_t aio_write_cmd = {
1393 .name = "aio_write",
1394 .cfunc = aio_write_f,
1395 .argmin = 2,
1396 .argmax = -1,
Eric Blake37546ff2016-05-16 10:43:03 -06001397 .args = "[-Cfiquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001398 .oneline = "asynchronously writes a number of bytes",
1399 .help = aio_write_help,
1400};
1401
Max Reitz4c7b7e92015-02-05 13:58:22 -05001402static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001403{
1404 int nr_iov, c;
1405 int pattern = 0xcd;
1406 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001407 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001408
Max Reitz4c7b7e92015-02-05 13:58:22 -05001409 ctx->blk = blk;
Eric Blake37546ff2016-05-16 10:43:03 -06001410 while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001411 switch (c) {
1412 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001413 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001414 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001415 case 'f':
1416 flags |= BDRV_REQ_FUA;
1417 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001418 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001419 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001420 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001421 case 'u':
1422 flags |= BDRV_REQ_MAY_UNMAP;
1423 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001424 case 'P':
1425 pattern = parse_pattern(optarg);
1426 if (pattern < 0) {
1427 g_free(ctx);
1428 return 0;
1429 }
1430 break;
Eric Blake37546ff2016-05-16 10:43:03 -06001431 case 'i':
1432 printf("injecting invalid write request\n");
1433 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1434 g_free(ctx);
1435 return 0;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001436 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001437 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001438 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001439 default:
1440 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001441 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001442 }
1443 }
1444
1445 if (optind > argc - 2) {
1446 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001447 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001448 }
1449
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001450 if (ctx->zflag && optind != argc - 2) {
1451 printf("-z supports only a single length parameter\n");
1452 g_free(ctx);
1453 return 0;
1454 }
1455
Eric Blakec2e001c2016-05-07 21:16:45 -06001456 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1457 printf("-u requires -z to be specified\n");
Eric Blake4ca1d342016-05-16 10:43:01 -06001458 g_free(ctx);
Eric Blakec2e001c2016-05-07 21:16:45 -06001459 return 0;
1460 }
1461
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001462 if (ctx->zflag && ctx->Pflag) {
1463 printf("-z and -P cannot be specified at the same time\n");
1464 g_free(ctx);
1465 return 0;
1466 }
1467
Kevin Wolf797ac582013-06-05 14:19:31 +02001468 ctx->offset = cvtnum(argv[optind]);
1469 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001470 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001471 g_free(ctx);
1472 return 0;
1473 }
1474 optind++;
1475
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001476 if (ctx->zflag) {
1477 int64_t count = cvtnum(argv[optind]);
1478 if (count < 0) {
1479 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001480 g_free(ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001481 return 0;
1482 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001483
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001484 ctx->qiov.size = count;
Eric Blaked004bd52016-05-24 16:25:20 -06001485 blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1486 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001487 } else {
1488 nr_iov = argc - optind;
1489 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1490 pattern);
1491 if (ctx->buf == NULL) {
1492 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1493 g_free(ctx);
1494 return 0;
1495 }
1496
1497 gettimeofday(&ctx->t1, NULL);
1498 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1499 BLOCK_ACCT_WRITE);
1500
Eric Blake770e0e02016-05-07 21:16:44 -06001501 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1502 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001503 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001504 return 0;
1505}
1506
Max Reitz4c7b7e92015-02-05 13:58:22 -05001507static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001508{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001509 BlockAcctCookie cookie;
1510 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001511 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001512 block_acct_done(blk_get_stats(blk), &cookie);
Kevin Wolf797ac582013-06-05 14:19:31 +02001513 return 0;
1514}
1515
1516static const cmdinfo_t aio_flush_cmd = {
1517 .name = "aio_flush",
1518 .cfunc = aio_flush_f,
1519 .oneline = "completes all outstanding aio requests"
1520};
1521
Max Reitz4c7b7e92015-02-05 13:58:22 -05001522static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001523{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001524 blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001525 return 0;
1526}
1527
1528static const cmdinfo_t flush_cmd = {
1529 .name = "flush",
1530 .altname = "f",
1531 .cfunc = flush_f,
1532 .oneline = "flush all in-core file state to disk",
1533};
1534
Max Reitz4c7b7e92015-02-05 13:58:22 -05001535static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001536{
1537 int64_t offset;
1538 int ret;
1539
1540 offset = cvtnum(argv[1]);
1541 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001542 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001543 return 0;
1544 }
1545
Max Reitz4c7b7e92015-02-05 13:58:22 -05001546 ret = blk_truncate(blk, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02001547 if (ret < 0) {
1548 printf("truncate: %s\n", strerror(-ret));
1549 return 0;
1550 }
1551
1552 return 0;
1553}
1554
1555static const cmdinfo_t truncate_cmd = {
1556 .name = "truncate",
1557 .altname = "t",
1558 .cfunc = truncate_f,
1559 .argmin = 1,
1560 .argmax = 1,
1561 .args = "off",
1562 .oneline = "truncates the current file at the given offset",
1563};
1564
Max Reitz4c7b7e92015-02-05 13:58:22 -05001565static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001566{
1567 int64_t size;
1568 char s1[64];
1569
Max Reitz4c7b7e92015-02-05 13:58:22 -05001570 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001571 if (size < 0) {
1572 printf("getlength: %s\n", strerror(-size));
1573 return 0;
1574 }
1575
1576 cvtstr(size, s1, sizeof(s1));
1577 printf("%s\n", s1);
1578 return 0;
1579}
1580
1581
1582static const cmdinfo_t length_cmd = {
1583 .name = "length",
1584 .altname = "l",
1585 .cfunc = length_f,
1586 .oneline = "gets the length of the current file",
1587};
1588
1589
Max Reitz4c7b7e92015-02-05 13:58:22 -05001590static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001591{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001592 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001593 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001594 ImageInfoSpecific *spec_info;
Kevin Wolf797ac582013-06-05 14:19:31 +02001595 char s1[64], s2[64];
1596 int ret;
1597
1598 if (bs->drv && bs->drv->format_name) {
1599 printf("format name: %s\n", bs->drv->format_name);
1600 }
1601 if (bs->drv && bs->drv->protocol_name) {
1602 printf("format name: %s\n", bs->drv->protocol_name);
1603 }
1604
1605 ret = bdrv_get_info(bs, &bdi);
1606 if (ret) {
1607 return 0;
1608 }
1609
1610 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1611 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1612
1613 printf("cluster size: %s\n", s1);
1614 printf("vm state offset: %s\n", s2);
1615
Max Reitza8d8ecb2013-10-09 10:46:17 +02001616 spec_info = bdrv_get_specific_info(bs);
1617 if (spec_info) {
1618 printf("Format specific information:\n");
1619 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1620 qapi_free_ImageInfoSpecific(spec_info);
1621 }
1622
Kevin Wolf797ac582013-06-05 14:19:31 +02001623 return 0;
1624}
1625
1626
1627
1628static const cmdinfo_t info_cmd = {
1629 .name = "info",
1630 .altname = "i",
1631 .cfunc = info_f,
1632 .oneline = "prints information about the current file",
1633};
1634
1635static void discard_help(void)
1636{
1637 printf(
1638"\n"
1639" discards a range of bytes from the given offset\n"
1640"\n"
1641" Example:\n"
1642" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1643"\n"
1644" Discards a segment of the currently open file.\n"
1645" -C, -- report statistics in a machine parsable format\n"
1646" -q, -- quiet mode, do not show I/O statistics\n"
1647"\n");
1648}
1649
Max Reitz4c7b7e92015-02-05 13:58:22 -05001650static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001651
1652static const cmdinfo_t discard_cmd = {
1653 .name = "discard",
1654 .altname = "d",
1655 .cfunc = discard_f,
1656 .argmin = 2,
1657 .argmax = -1,
1658 .args = "[-Cq] off len",
1659 .oneline = "discards a number of bytes at a specified offset",
1660 .help = discard_help,
1661};
1662
Max Reitz4c7b7e92015-02-05 13:58:22 -05001663static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001664{
1665 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001666 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001667 int c, ret;
John Snow9b0beaf2015-11-05 18:53:02 -05001668 int64_t offset, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001669
Eric Blakeb062ad82015-05-12 09:10:56 -06001670 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001671 switch (c) {
1672 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001673 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001674 break;
1675 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001676 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001677 break;
1678 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001679 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001680 }
1681 }
1682
1683 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001684 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001685 }
1686
1687 offset = cvtnum(argv[optind]);
1688 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001689 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001690 return 0;
1691 }
1692
1693 optind++;
1694 count = cvtnum(argv[optind]);
1695 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001696 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001697 return 0;
Max Reitza3674672016-06-20 16:26:22 +02001698 } else if (count >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
John Snow9b0beaf2015-11-05 18:53:02 -05001699 printf("length cannot exceed %"PRIu64", given %s\n",
Max Reitza3674672016-06-20 16:26:22 +02001700 (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
John Snow9b0beaf2015-11-05 18:53:02 -05001701 argv[optind]);
1702 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001703 }
1704
1705 gettimeofday(&t1, NULL);
Eric Blake1c6c4bb2016-07-15 17:22:54 -06001706 ret = blk_pdiscard(blk, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +02001707 gettimeofday(&t2, NULL);
1708
1709 if (ret < 0) {
1710 printf("discard failed: %s\n", strerror(-ret));
1711 goto out;
1712 }
1713
1714 /* Finally, report back -- -C gives a parsable format */
1715 if (!qflag) {
1716 t2 = tsub(t2, t1);
1717 print_report("discard", &t2, offset, count, count, 1, Cflag);
1718 }
1719
1720out:
1721 return 0;
1722}
1723
Max Reitz4c7b7e92015-02-05 13:58:22 -05001724static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001725{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001726 BlockDriverState *bs = blk_bs(blk);
John Snow9b0beaf2015-11-05 18:53:02 -05001727 int64_t offset, sector_num, nb_sectors, remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001728 char s1[64];
John Snow9b0beaf2015-11-05 18:53:02 -05001729 int num, ret;
1730 int64_t sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001731
1732 offset = cvtnum(argv[1]);
1733 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001734 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001735 return 0;
1736 } else if (offset & 0x1ff) {
1737 printf("offset %" PRId64 " is not sector aligned\n",
1738 offset);
1739 return 0;
1740 }
1741
1742 if (argc == 3) {
1743 nb_sectors = cvtnum(argv[2]);
1744 if (nb_sectors < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001745 print_cvtnum_err(nb_sectors, argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001746 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001747 } else if (nb_sectors > INT_MAX) {
1748 printf("length argument cannot exceed %d, given %s\n",
1749 INT_MAX, argv[2]);
1750 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001751 }
1752 } else {
1753 nb_sectors = 1;
1754 }
1755
1756 remaining = nb_sectors;
1757 sum_alloc = 0;
1758 sector_num = offset >> 9;
1759 while (remaining) {
1760 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001761 if (ret < 0) {
1762 printf("is_allocated failed: %s\n", strerror(-ret));
1763 return 0;
1764 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001765 sector_num += num;
1766 remaining -= num;
1767 if (ret) {
1768 sum_alloc += num;
1769 }
1770 if (num == 0) {
1771 nb_sectors -= remaining;
1772 remaining = 0;
1773 }
1774 }
1775
1776 cvtstr(offset, s1, sizeof(s1));
1777
John Snow9b0beaf2015-11-05 18:53:02 -05001778 printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001779 sum_alloc, nb_sectors, s1);
1780 return 0;
1781}
1782
1783static const cmdinfo_t alloc_cmd = {
1784 .name = "alloc",
1785 .altname = "a",
1786 .argmin = 1,
1787 .argmax = 2,
1788 .cfunc = alloc_f,
1789 .args = "off [sectors]",
1790 .oneline = "checks if a sector is present in the file",
1791};
1792
1793
1794static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1795 int64_t nb_sectors, int64_t *pnum)
1796{
1797 int num, num_checked;
1798 int ret, firstret;
1799
1800 num_checked = MIN(nb_sectors, INT_MAX);
1801 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1802 if (ret < 0) {
1803 return ret;
1804 }
1805
1806 firstret = ret;
1807 *pnum = num;
1808
1809 while (nb_sectors > 0 && ret == firstret) {
1810 sector_num += num;
1811 nb_sectors -= num;
1812
1813 num_checked = MIN(nb_sectors, INT_MAX);
1814 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02001815 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001816 *pnum += num;
1817 } else {
1818 break;
1819 }
1820 }
1821
1822 return firstret;
1823}
1824
Max Reitz4c7b7e92015-02-05 13:58:22 -05001825static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001826{
1827 int64_t offset;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001828 int64_t nb_sectors, total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02001829 char s1[64];
1830 int64_t num;
1831 int ret;
1832 const char *retstr;
1833
1834 offset = 0;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001835 total_sectors = blk_nb_sectors(blk);
1836 if (total_sectors < 0) {
1837 error_report("Failed to query image length: %s",
1838 strerror(-total_sectors));
1839 return 0;
1840 }
1841
1842 nb_sectors = total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02001843
1844 do {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001845 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02001846 if (ret < 0) {
1847 error_report("Failed to get allocation status: %s", strerror(-ret));
1848 return 0;
Max Reitz4b25bbc2014-10-22 17:00:16 +02001849 } else if (!num) {
1850 error_report("Unexpected end of image");
1851 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001852 }
1853
1854 retstr = ret ? " allocated" : "not allocated";
1855 cvtstr(offset << 9ULL, s1, sizeof(s1));
1856 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1857 "at offset %s (%d)\n",
1858 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1859
1860 offset += num;
1861 nb_sectors -= num;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001862 } while (offset < total_sectors);
Kevin Wolf797ac582013-06-05 14:19:31 +02001863
1864 return 0;
1865}
1866
1867static const cmdinfo_t map_cmd = {
1868 .name = "map",
1869 .argmin = 0,
1870 .argmax = 0,
1871 .cfunc = map_f,
1872 .args = "",
1873 .oneline = "prints the allocated areas of a file",
1874};
1875
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001876static void reopen_help(void)
1877{
1878 printf(
1879"\n"
1880" Changes the open options of an already opened image\n"
1881"\n"
1882" Example:\n"
1883" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1884"\n"
1885" -r, -- Reopen the image read-only\n"
1886" -c, -- Change the cache mode to the given value\n"
1887" -o, -- Changes block driver options (cf. 'open' command)\n"
1888"\n");
1889}
1890
1891static int reopen_f(BlockBackend *blk, int argc, char **argv);
1892
1893static QemuOptsList reopen_opts = {
1894 .name = "reopen",
1895 .merge_lists = true,
1896 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
1897 .desc = {
1898 /* no elements => accept any params */
1899 { /* end of list */ }
1900 },
1901};
1902
1903static const cmdinfo_t reopen_cmd = {
1904 .name = "reopen",
1905 .argmin = 0,
1906 .argmax = -1,
1907 .cfunc = reopen_f,
1908 .args = "[-r] [-c cache] [-o options]",
1909 .oneline = "reopens an image with new options",
1910 .help = reopen_help,
1911};
1912
1913static int reopen_f(BlockBackend *blk, int argc, char **argv)
1914{
1915 BlockDriverState *bs = blk_bs(blk);
1916 QemuOpts *qopts;
1917 QDict *opts;
1918 int c;
1919 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001920 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001921
1922 BlockReopenQueue *brq;
1923 Error *local_err = NULL;
1924
1925 while ((c = getopt(argc, argv, "c:o:r")) != -1) {
1926 switch (c) {
1927 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001928 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001929 error_report("Invalid cache option: %s", optarg);
1930 return 0;
1931 }
1932 break;
1933 case 'o':
1934 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
1935 qemu_opts_reset(&reopen_opts);
1936 return 0;
1937 }
1938 break;
1939 case 'r':
1940 flags &= ~BDRV_O_RDWR;
1941 break;
1942 default:
1943 qemu_opts_reset(&reopen_opts);
1944 return qemuio_command_usage(&reopen_cmd);
1945 }
1946 }
1947
1948 if (optind != argc) {
1949 qemu_opts_reset(&reopen_opts);
1950 return qemuio_command_usage(&reopen_cmd);
1951 }
1952
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001953 if (writethrough != blk_enable_write_cache(blk) &&
1954 blk_get_attached_dev(blk))
1955 {
1956 error_report("Cannot change cache.writeback: Device attached");
1957 qemu_opts_reset(&reopen_opts);
1958 return 0;
1959 }
1960
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001961 qopts = qemu_opts_find(&reopen_opts, NULL);
1962 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
1963 qemu_opts_reset(&reopen_opts);
1964
1965 brq = bdrv_reopen_queue(NULL, bs, opts, flags);
Paolo Bonzini720150f2016-10-27 12:49:02 +02001966 bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001967 if (local_err) {
1968 error_report_err(local_err);
Kevin Wolf19dbecd2016-03-18 15:36:31 +01001969 } else {
1970 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01001971 }
1972
1973 return 0;
1974}
1975
Max Reitz4c7b7e92015-02-05 13:58:22 -05001976static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001977{
1978 int ret;
1979
Max Reitz4c7b7e92015-02-05 13:58:22 -05001980 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001981 if (ret < 0) {
1982 printf("Could not set breakpoint: %s\n", strerror(-ret));
1983 }
1984
1985 return 0;
1986}
1987
Max Reitz4c7b7e92015-02-05 13:58:22 -05001988static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08001989{
1990 int ret;
1991
Max Reitz4c7b7e92015-02-05 13:58:22 -05001992 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08001993 if (ret < 0) {
1994 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
1995 }
1996
1997 return 0;
1998}
1999
Kevin Wolf797ac582013-06-05 14:19:31 +02002000static const cmdinfo_t break_cmd = {
2001 .name = "break",
2002 .argmin = 2,
2003 .argmax = 2,
2004 .cfunc = break_f,
2005 .args = "event tag",
2006 .oneline = "sets a breakpoint on event and tags the stopped "
2007 "request as tag",
2008};
2009
Fam Zheng4cc70e92013-11-20 10:01:54 +08002010static const cmdinfo_t remove_break_cmd = {
2011 .name = "remove_break",
2012 .argmin = 1,
2013 .argmax = 1,
2014 .cfunc = remove_break_f,
2015 .args = "tag",
2016 .oneline = "remove a breakpoint by tag",
2017};
2018
Max Reitz4c7b7e92015-02-05 13:58:22 -05002019static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002020{
2021 int ret;
2022
Max Reitz4c7b7e92015-02-05 13:58:22 -05002023 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002024 if (ret < 0) {
2025 printf("Could not resume request: %s\n", strerror(-ret));
2026 }
2027
2028 return 0;
2029}
2030
2031static const cmdinfo_t resume_cmd = {
2032 .name = "resume",
2033 .argmin = 1,
2034 .argmax = 1,
2035 .cfunc = resume_f,
2036 .args = "tag",
2037 .oneline = "resumes the request tagged as tag",
2038};
2039
Max Reitz4c7b7e92015-02-05 13:58:22 -05002040static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002041{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002042 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2043 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002044 }
2045
2046 return 0;
2047}
2048
2049static const cmdinfo_t wait_break_cmd = {
2050 .name = "wait_break",
2051 .argmin = 1,
2052 .argmax = 1,
2053 .cfunc = wait_break_f,
2054 .args = "tag",
2055 .oneline = "waits for the suspension of a request",
2056};
2057
Max Reitz4c7b7e92015-02-05 13:58:22 -05002058static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002059{
2060 abort();
2061}
2062
2063static const cmdinfo_t abort_cmd = {
2064 .name = "abort",
2065 .cfunc = abort_f,
2066 .flags = CMD_NOFILE_OK,
2067 .oneline = "simulate a program crash using abort(3)",
2068};
2069
Max Reitz0e82dc72014-12-08 10:48:10 +01002070static void sigraise_help(void)
2071{
2072 printf(
2073"\n"
2074" raises the given signal\n"
2075"\n"
2076" Example:\n"
2077" 'sigraise %i' - raises SIGTERM\n"
2078"\n"
2079" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2080" given to sigraise.\n"
2081"\n", SIGTERM);
2082}
2083
Max Reitz4c7b7e92015-02-05 13:58:22 -05002084static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002085
2086static const cmdinfo_t sigraise_cmd = {
2087 .name = "sigraise",
2088 .cfunc = sigraise_f,
2089 .argmin = 1,
2090 .argmax = 1,
2091 .flags = CMD_NOFILE_OK,
2092 .args = "signal",
2093 .oneline = "raises a signal",
2094 .help = sigraise_help,
2095};
2096
Max Reitz4c7b7e92015-02-05 13:58:22 -05002097static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002098{
John Snow9b0beaf2015-11-05 18:53:02 -05002099 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002100 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002101 print_cvtnum_err(sig, argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002102 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05002103 } else if (sig > NSIG) {
2104 printf("signal argument '%s' is too large to be a valid signal\n",
2105 argv[1]);
2106 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002107 }
2108
2109 /* Using raise() to kill this process does not necessarily flush all open
2110 * streams. At least stdout and stderr (although the latter should be
2111 * non-buffered anyway) should be flushed, though. */
2112 fflush(stdout);
2113 fflush(stderr);
2114
2115 raise(sig);
2116 return 0;
2117}
2118
Kevin Wolfcd33d022014-01-15 15:39:10 +01002119static void sleep_cb(void *opaque)
2120{
2121 bool *expired = opaque;
2122 *expired = true;
2123}
2124
Max Reitz4c7b7e92015-02-05 13:58:22 -05002125static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002126{
2127 char *endptr;
2128 long ms;
2129 struct QEMUTimer *timer;
2130 bool expired = false;
2131
2132 ms = strtol(argv[1], &endptr, 0);
2133 if (ms < 0 || *endptr != '\0') {
2134 printf("%s is not a valid number\n", argv[1]);
2135 return 0;
2136 }
2137
2138 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2139 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2140
2141 while (!expired) {
2142 main_loop_wait(false);
2143 }
2144
2145 timer_free(timer);
2146
2147 return 0;
2148}
2149
2150static const cmdinfo_t sleep_cmd = {
2151 .name = "sleep",
2152 .argmin = 1,
2153 .argmax = 1,
2154 .cfunc = sleep_f,
2155 .flags = CMD_NOFILE_OK,
2156 .oneline = "waits for the given value in milliseconds",
2157};
2158
Kevin Wolff18a8342013-06-05 14:19:33 +02002159static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2160{
2161 if (cmd) {
2162 printf("%s ", cmd);
2163 } else {
2164 printf("%s ", ct->name);
2165 if (ct->altname) {
2166 printf("(or %s) ", ct->altname);
2167 }
2168 }
2169
2170 if (ct->args) {
2171 printf("%s ", ct->args);
2172 }
2173 printf("-- %s\n", ct->oneline);
2174}
2175
2176static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2177{
2178 help_oneline(cmd, ct);
2179 if (ct->help) {
2180 ct->help();
2181 }
2182}
2183
2184static void help_all(void)
2185{
2186 const cmdinfo_t *ct;
2187
2188 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2189 help_oneline(ct->name, ct);
2190 }
2191 printf("\nUse 'help commandname' for extended help.\n");
2192}
2193
Max Reitz4c7b7e92015-02-05 13:58:22 -05002194static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002195{
2196 const cmdinfo_t *ct;
2197
2198 if (argc == 1) {
2199 help_all();
2200 return 0;
2201 }
2202
2203 ct = find_command(argv[1]);
2204 if (ct == NULL) {
2205 printf("command %s not found\n", argv[1]);
2206 return 0;
2207 }
2208
2209 help_onecmd(argv[1], ct);
2210 return 0;
2211}
2212
2213static const cmdinfo_t help_cmd = {
2214 .name = "help",
2215 .altname = "?",
2216 .cfunc = help_f,
2217 .argmin = 0,
2218 .argmax = 1,
2219 .flags = CMD_FLAG_GLOBAL,
2220 .args = "[command]",
2221 .oneline = "help for one or all commands",
2222};
2223
Max Reitz4c7b7e92015-02-05 13:58:22 -05002224bool qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002225{
Paolo Bonzini15afd942016-10-27 12:49:03 +02002226 AioContext *ctx;
Kevin Wolfdd583292013-06-05 14:19:32 +02002227 char *input;
2228 const cmdinfo_t *ct;
2229 char **v;
2230 int c;
2231 bool done = false;
2232
2233 input = g_strdup(cmd);
2234 v = breakline(input, &c);
2235 if (c) {
2236 ct = find_command(v[0]);
2237 if (ct) {
Paolo Bonzini15afd942016-10-27 12:49:03 +02002238 ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
2239 aio_context_acquire(ctx);
Max Reitz4c7b7e92015-02-05 13:58:22 -05002240 done = command(blk, ct, c, v);
Paolo Bonzini15afd942016-10-27 12:49:03 +02002241 aio_context_release(ctx);
Kevin Wolfdd583292013-06-05 14:19:32 +02002242 } else {
2243 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2244 }
2245 }
2246 g_free(input);
2247 g_free(v);
2248
2249 return done;
2250}
2251
Kevin Wolf797ac582013-06-05 14:19:31 +02002252static void __attribute((constructor)) init_qemuio_commands(void)
2253{
2254 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002255 qemuio_add_command(&help_cmd);
2256 qemuio_add_command(&read_cmd);
2257 qemuio_add_command(&readv_cmd);
2258 qemuio_add_command(&write_cmd);
2259 qemuio_add_command(&writev_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002260 qemuio_add_command(&aio_read_cmd);
2261 qemuio_add_command(&aio_write_cmd);
2262 qemuio_add_command(&aio_flush_cmd);
2263 qemuio_add_command(&flush_cmd);
2264 qemuio_add_command(&truncate_cmd);
2265 qemuio_add_command(&length_cmd);
2266 qemuio_add_command(&info_cmd);
2267 qemuio_add_command(&discard_cmd);
2268 qemuio_add_command(&alloc_cmd);
2269 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002270 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002271 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002272 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002273 qemuio_add_command(&resume_cmd);
2274 qemuio_add_command(&wait_break_cmd);
2275 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002276 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002277 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002278}