blob: 382faa8a2a1768efbd54529cdfbb48683c6d70e9 [file] [log] [blame]
Kevin Wolf797ac582013-06-05 14:19:31 +02001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * 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"
Fam Zhenga91f9582015-01-30 10:49:42 +080021#include "sysemu/block-backend.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020022#include "qemu/cutils.h"
Kevin Wolf797ac582013-06-05 14:19:31 +020023
24#define CMD_NOFILE_OK 0x01
25
Stefan Weilf9883882014-03-05 22:23:00 +010026bool qemuio_misalign;
Kevin Wolf797ac582013-06-05 14:19:31 +020027
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020028static cmdinfo_t *cmdtab;
29static int ncmds;
30
31static int compare_cmdname(const void *a, const void *b)
32{
33 return strcmp(((const cmdinfo_t *)a)->name,
34 ((const cmdinfo_t *)b)->name);
35}
36
37void qemuio_add_command(const cmdinfo_t *ci)
38{
Markus Armbruster02c4f262014-08-19 10:31:09 +020039 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020040 cmdtab[ncmds - 1] = *ci;
41 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
42}
43
44int qemuio_command_usage(const cmdinfo_t *ci)
45{
46 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
47 return 0;
48}
49
Max Reitz4c7b7e92015-02-05 13:58:22 -050050static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020051{
52 if (ct->flags & CMD_FLAG_GLOBAL) {
53 return 1;
54 }
Max Reitz4c7b7e92015-02-05 13:58:22 -050055 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020056 fprintf(stderr, "no file open, try 'help open'\n");
57 return 0;
58 }
59 return 1;
60}
61
Max Reitz4c7b7e92015-02-05 13:58:22 -050062static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
Kevin Wolf3d219942013-06-05 14:19:39 +020063 char **argv)
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020064{
65 char *cmd = argv[0];
66
Max Reitz4c7b7e92015-02-05 13:58:22 -050067 if (!init_check_command(blk, ct)) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020068 return 0;
69 }
70
71 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
72 if (ct->argmax == -1) {
73 fprintf(stderr,
74 "bad argument count %d to %s, expected at least %d arguments\n",
75 argc-1, cmd, ct->argmin);
76 } else if (ct->argmin == ct->argmax) {
77 fprintf(stderr,
78 "bad argument count %d to %s, expected %d arguments\n",
79 argc-1, cmd, ct->argmin);
80 } else {
81 fprintf(stderr,
82 "bad argument count %d to %s, expected between %d and %d arguments\n",
83 argc-1, cmd, ct->argmin, ct->argmax);
84 }
85 return 0;
86 }
87 optind = 0;
Max Reitz4c7b7e92015-02-05 13:58:22 -050088 return ct->cfunc(blk, argc, argv);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +020089}
90
91static const cmdinfo_t *find_command(const char *cmd)
92{
93 cmdinfo_t *ct;
94
95 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
96 if (strcmp(ct->name, cmd) == 0 ||
97 (ct->altname && strcmp(ct->altname, cmd) == 0))
98 {
99 return (const cmdinfo_t *)ct;
100 }
101 }
102 return NULL;
103}
104
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100105/* Invoke fn() for commands with a matching prefix */
106void qemuio_complete_command(const char *input,
107 void (*fn)(const char *cmd, void *opaque),
108 void *opaque)
109{
110 cmdinfo_t *ct;
111 size_t input_len = strlen(input);
112
113 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
114 if (strncmp(input, ct->name, input_len) == 0) {
115 fn(ct->name, opaque);
116 }
117 }
118}
119
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200120static char **breakline(char *input, int *count)
121{
122 int c = 0;
123 char *p;
Markus Armbruster5839e532014-08-19 10:31:08 +0200124 char **rval = g_new0(char *, 1);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200125
126 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
127 if (!*p) {
128 continue;
129 }
130 c++;
Markus Armbruster08193dd2014-08-19 10:31:10 +0200131 rval = g_renew(char *, rval, (c + 1));
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200132 rval[c - 1] = p;
133 rval[c] = NULL;
134 }
135 *count = c;
136 return rval;
137}
138
Kevin Wolf797ac582013-06-05 14:19:31 +0200139static int64_t cvtnum(const char *s)
140{
141 char *end;
John Snowef5a7882015-11-05 18:53:03 -0500142 int64_t ret;
143
144 ret = qemu_strtosz_suffix(s, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
145 if (*end != '\0') {
146 /* Detritus at the end of the string */
147 return -EINVAL;
148 }
149 return ret;
Kevin Wolf797ac582013-06-05 14:19:31 +0200150}
151
John Snowa9ecfa02015-11-05 18:53:04 -0500152static void print_cvtnum_err(int64_t rc, const char *arg)
153{
154 switch (rc) {
155 case -EINVAL:
156 printf("Parsing error: non-numeric argument,"
157 " or extraneous/unrecognized suffix -- %s\n", arg);
158 break;
159 case -ERANGE:
160 printf("Parsing error: argument too large -- %s\n", arg);
161 break;
162 default:
163 printf("Parsing error: %s\n", arg);
164 }
165}
166
Kevin Wolf0b613882013-06-05 14:19:38 +0200167#define EXABYTES(x) ((long long)(x) << 60)
168#define PETABYTES(x) ((long long)(x) << 50)
169#define TERABYTES(x) ((long long)(x) << 40)
170#define GIGABYTES(x) ((long long)(x) << 30)
171#define MEGABYTES(x) ((long long)(x) << 20)
172#define KILOBYTES(x) ((long long)(x) << 10)
173
174#define TO_EXABYTES(x) ((x) / EXABYTES(1))
175#define TO_PETABYTES(x) ((x) / PETABYTES(1))
176#define TO_TERABYTES(x) ((x) / TERABYTES(1))
177#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
178#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
179#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
180
181static void cvtstr(double value, char *str, size_t size)
182{
183 char *trim;
184 const char *suffix;
185
186 if (value >= EXABYTES(1)) {
187 suffix = " EiB";
188 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
189 } else if (value >= PETABYTES(1)) {
190 suffix = " PiB";
191 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
192 } else if (value >= TERABYTES(1)) {
193 suffix = " TiB";
194 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
195 } else if (value >= GIGABYTES(1)) {
196 suffix = " GiB";
197 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
198 } else if (value >= MEGABYTES(1)) {
199 suffix = " MiB";
200 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
201 } else if (value >= KILOBYTES(1)) {
202 suffix = " KiB";
203 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
204 } else {
205 suffix = " bytes";
206 snprintf(str, size - 6, "%f", value);
207 }
208
209 trim = strstr(str, ".000");
210 if (trim) {
211 strcpy(trim, suffix);
212 } else {
213 strcat(str, suffix);
214 }
215}
216
217
218
219static struct timeval tsub(struct timeval t1, struct timeval t2)
220{
221 t1.tv_usec -= t2.tv_usec;
222 if (t1.tv_usec < 0) {
223 t1.tv_usec += 1000000;
224 t1.tv_sec--;
225 }
226 t1.tv_sec -= t2.tv_sec;
227 return t1;
228}
229
230static double tdiv(double value, struct timeval tv)
231{
232 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
233}
234
235#define HOURS(sec) ((sec) / (60 * 60))
236#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
237#define SECONDS(sec) ((sec) % 60)
238
239enum {
240 DEFAULT_TIME = 0x0,
241 TERSE_FIXED_TIME = 0x1,
242 VERBOSE_FIXED_TIME = 0x2,
243};
244
245static void timestr(struct timeval *tv, char *ts, size_t size, int format)
246{
247 double usec = (double)tv->tv_usec / 1000000.0;
248
249 if (format & TERSE_FIXED_TIME) {
250 if (!HOURS(tv->tv_sec)) {
251 snprintf(ts, size, "%u:%02u.%02u",
252 (unsigned int) MINUTES(tv->tv_sec),
253 (unsigned int) SECONDS(tv->tv_sec),
254 (unsigned int) (usec * 100));
255 return;
256 }
257 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
258 }
259
260 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
261 snprintf(ts, size, "%u:%02u:%02u.%02u",
262 (unsigned int) HOURS(tv->tv_sec),
263 (unsigned int) MINUTES(tv->tv_sec),
264 (unsigned int) SECONDS(tv->tv_sec),
265 (unsigned int) (usec * 100));
266 } else {
267 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
268 }
269}
270
Kevin Wolf797ac582013-06-05 14:19:31 +0200271/*
272 * Parse the pattern argument to various sub-commands.
273 *
274 * Because the pattern is used as an argument to memset it must evaluate
275 * to an unsigned integer that fits into a single byte.
276 */
277static int parse_pattern(const char *arg)
278{
279 char *endptr = NULL;
280 long pattern;
281
282 pattern = strtol(arg, &endptr, 0);
283 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
284 printf("%s is not a valid pattern byte\n", arg);
285 return -1;
286 }
287
288 return pattern;
289}
290
291/*
292 * Memory allocation helpers.
293 *
294 * Make sure memory is aligned by default, or purposefully misaligned if
295 * that is specified on the command line.
296 */
297
298#define MISALIGN_OFFSET 16
Max Reitz4c7b7e92015-02-05 13:58:22 -0500299static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
Kevin Wolf797ac582013-06-05 14:19:31 +0200300{
301 void *buf;
302
303 if (qemuio_misalign) {
304 len += MISALIGN_OFFSET;
305 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500306 buf = blk_blockalign(blk, len);
Kevin Wolf797ac582013-06-05 14:19:31 +0200307 memset(buf, pattern, len);
308 if (qemuio_misalign) {
309 buf += MISALIGN_OFFSET;
310 }
311 return buf;
312}
313
314static void qemu_io_free(void *p)
315{
316 if (qemuio_misalign) {
317 p -= MISALIGN_OFFSET;
318 }
319 qemu_vfree(p);
320}
321
John Snow9b0beaf2015-11-05 18:53:02 -0500322static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
Kevin Wolf797ac582013-06-05 14:19:31 +0200323{
John Snow9b0beaf2015-11-05 18:53:02 -0500324 uint64_t i;
325 int j;
Kevin Wolf797ac582013-06-05 14:19:31 +0200326 const uint8_t *p;
327
328 for (i = 0, p = buffer; i < len; i += 16) {
329 const uint8_t *s = p;
330
331 printf("%08" PRIx64 ": ", offset + i);
332 for (j = 0; j < 16 && i + j < len; j++, p++) {
333 printf("%02x ", *p);
334 }
335 printf(" ");
336 for (j = 0; j < 16 && i + j < len; j++, s++) {
337 if (isalnum(*s)) {
338 printf("%c", *s);
339 } else {
340 printf(".");
341 }
342 }
343 printf("\n");
344 }
345}
346
347static void print_report(const char *op, struct timeval *t, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500348 int64_t count, int64_t total, int cnt, int Cflag)
Kevin Wolf797ac582013-06-05 14:19:31 +0200349{
350 char s1[64], s2[64], ts[64];
351
352 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
353 if (!Cflag) {
354 cvtstr((double)total, s1, sizeof(s1));
355 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
John Snow9b0beaf2015-11-05 18:53:02 -0500356 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200357 op, total, count, offset);
358 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
359 s1, cnt, ts, s2, tdiv((double)cnt, *t));
360 } else {/* bytes,ops,time,bytes/sec,ops/sec */
John Snow9b0beaf2015-11-05 18:53:02 -0500361 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200362 total, cnt, ts,
363 tdiv((double)total, *t),
364 tdiv((double)cnt, *t));
365 }
366}
367
368/*
369 * Parse multiple length statements for vectored I/O, and construct an I/O
370 * vector matching it.
371 */
372static void *
Max Reitz4c7b7e92015-02-05 13:58:22 -0500373create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200374 int pattern)
375{
376 size_t *sizes = g_new0(size_t, nr_iov);
377 size_t count = 0;
378 void *buf = NULL;
379 void *p;
380 int i;
381
382 for (i = 0; i < nr_iov; i++) {
383 char *arg = argv[i];
384 int64_t len;
385
386 len = cvtnum(arg);
387 if (len < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500388 print_cvtnum_err(len, arg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200389 goto fail;
390 }
391
392 /* should be SIZE_T_MAX, but that doesn't exist */
393 if (len > INT_MAX) {
John Snowa9ecfa02015-11-05 18:53:04 -0500394 printf("Argument '%s' exceeds maximum size %d\n", arg, INT_MAX);
Kevin Wolf797ac582013-06-05 14:19:31 +0200395 goto fail;
396 }
397
398 if (len & 0x1ff) {
399 printf("length argument %" PRId64
400 " is not sector aligned\n", len);
401 goto fail;
402 }
403
404 sizes[i] = len;
405 count += len;
406 }
407
408 qemu_iovec_init(qiov, nr_iov);
409
Max Reitz4c7b7e92015-02-05 13:58:22 -0500410 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200411
412 for (i = 0; i < nr_iov; i++) {
413 qemu_iovec_add(qiov, p, sizes[i]);
414 p += sizes[i];
415 }
416
417fail:
418 g_free(sizes);
419 return buf;
420}
421
John Snow9b0beaf2015-11-05 18:53:02 -0500422static int do_read(BlockBackend *blk, char *buf, int64_t offset, int64_t count,
423 int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200424{
425 int ret;
426
John Snow9b0beaf2015-11-05 18:53:02 -0500427 if (count >> 9 > INT_MAX) {
428 return -ERANGE;
429 }
430
Max Reitz4c7b7e92015-02-05 13:58:22 -0500431 ret = blk_read(blk, offset >> 9, (uint8_t *)buf, count >> 9);
Kevin Wolf797ac582013-06-05 14:19:31 +0200432 if (ret < 0) {
433 return ret;
434 }
435 *total = count;
436 return 1;
437}
438
John Snow9b0beaf2015-11-05 18:53:02 -0500439static int do_write(BlockBackend *blk, char *buf, int64_t offset, int64_t count,
440 int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200441{
442 int ret;
443
John Snow9b0beaf2015-11-05 18:53:02 -0500444 if (count >> 9 > INT_MAX) {
445 return -ERANGE;
446 }
447
Max Reitz4c7b7e92015-02-05 13:58:22 -0500448 ret = blk_write(blk, offset >> 9, (uint8_t *)buf, count >> 9);
Kevin Wolf797ac582013-06-05 14:19:31 +0200449 if (ret < 0) {
450 return ret;
451 }
452 *total = count;
453 return 1;
454}
455
John Snow9b0beaf2015-11-05 18:53:02 -0500456static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
457 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200458{
John Snow9b0beaf2015-11-05 18:53:02 -0500459 if (count > INT_MAX) {
460 return -ERANGE;
461 }
462
Max Reitz4c7b7e92015-02-05 13:58:22 -0500463 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200464 if (*total < 0) {
465 return *total;
466 }
467 return 1;
468}
469
John Snow9b0beaf2015-11-05 18:53:02 -0500470static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
471 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200472{
John Snow9b0beaf2015-11-05 18:53:02 -0500473 if (count > INT_MAX) {
474 return -ERANGE;
475 }
476
Max Reitz4c7b7e92015-02-05 13:58:22 -0500477 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200478 if (*total < 0) {
479 return *total;
480 }
481 return 1;
482}
483
484typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500485 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200486 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500487 int64_t count;
488 int64_t *total;
Kevin Wolf797ac582013-06-05 14:19:31 +0200489 int ret;
490 bool done;
491} CoWriteZeroes;
492
493static void coroutine_fn co_write_zeroes_entry(void *opaque)
494{
495 CoWriteZeroes *data = opaque;
496
Max Reitz4c7b7e92015-02-05 13:58:22 -0500497 data->ret = blk_co_write_zeroes(data->blk, data->offset / BDRV_SECTOR_SIZE,
498 data->count / BDRV_SECTOR_SIZE, 0);
Kevin Wolf797ac582013-06-05 14:19:31 +0200499 data->done = true;
500 if (data->ret < 0) {
501 *data->total = data->ret;
502 return;
503 }
504
505 *data->total = data->count;
506}
507
John Snow9b0beaf2015-11-05 18:53:02 -0500508static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int64_t count,
509 int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200510{
511 Coroutine *co;
512 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500513 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200514 .offset = offset,
515 .count = count,
516 .total = total,
517 .done = false,
518 };
519
John Snow9b0beaf2015-11-05 18:53:02 -0500520 if (count >> BDRV_SECTOR_BITS > INT_MAX) {
521 return -ERANGE;
522 }
523
Kevin Wolf797ac582013-06-05 14:19:31 +0200524 co = qemu_coroutine_create(co_write_zeroes_entry);
525 qemu_coroutine_enter(co, &data);
526 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,
John Snow9b0beaf2015-11-05 18:53:02 -0500537 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200538{
539 int ret;
540
John Snow9b0beaf2015-11-05 18:53:02 -0500541 if (count >> 9 > INT_MAX) {
542 return -ERANGE;
543 }
544
Max Reitz4c7b7e92015-02-05 13:58:22 -0500545 ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
Kevin Wolf797ac582013-06-05 14:19:31 +0200546 if (ret < 0) {
547 return ret;
548 }
549 *total = count;
550 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
Max Reitz4c7b7e92015-02-05 13:58:22 -0500592 blk_aio_readv(blk, offset >> 9, qiov, qiov->size >> 9,
593 aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200594 while (async_ret == NOT_DONE) {
595 main_loop_wait(false);
596 }
597
598 *total = qiov->size;
599 return async_ret < 0 ? async_ret : 1;
600}
601
Max Reitz4c7b7e92015-02-05 13:58:22 -0500602static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200603 int64_t offset, int *total)
604{
605 int async_ret = NOT_DONE;
606
Max Reitz4c7b7e92015-02-05 13:58:22 -0500607 blk_aio_writev(blk, offset >> 9, qiov, qiov->size >> 9,
608 aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200609 while (async_ret == NOT_DONE) {
610 main_loop_wait(false);
611 }
612
613 *total = qiov->size;
614 return async_ret < 0 ? async_ret : 1;
615}
616
617struct multiwrite_async_ret {
618 int num_done;
619 int error;
620};
621
622static void multiwrite_cb(void *opaque, int ret)
623{
624 struct multiwrite_async_ret *async_ret = opaque;
625
626 async_ret->num_done++;
627 if (ret < 0) {
628 async_ret->error = ret;
629 }
630}
631
Max Reitz4c7b7e92015-02-05 13:58:22 -0500632static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
Kevin Wolf797ac582013-06-05 14:19:31 +0200633 int num_reqs, int *total)
634{
635 int i, ret;
636 struct multiwrite_async_ret async_ret = {
637 .num_done = 0,
638 .error = 0,
639 };
640
641 *total = 0;
642 for (i = 0; i < num_reqs; i++) {
643 reqs[i].cb = multiwrite_cb;
644 reqs[i].opaque = &async_ret;
645 *total += reqs[i].qiov->size;
646 }
647
Max Reitz4c7b7e92015-02-05 13:58:22 -0500648 ret = blk_aio_multiwrite(blk, reqs, num_reqs);
Kevin Wolf797ac582013-06-05 14:19:31 +0200649 if (ret < 0) {
650 return ret;
651 }
652
653 while (async_ret.num_done < num_reqs) {
654 main_loop_wait(false);
655 }
656
657 return async_ret.error < 0 ? async_ret.error : 1;
658}
659
660static void read_help(void)
661{
662 printf(
663"\n"
664" reads a range of bytes from the given offset\n"
665"\n"
666" Example:\n"
667" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
668"\n"
669" Reads a segment of the currently open file, optionally dumping it to the\n"
670" standard output stream (with -v option) for subsequent inspection.\n"
671" -b, -- read from the VM state rather than the virtual disk\n"
672" -C, -- report statistics in a machine parsable format\n"
673" -l, -- length for pattern verification (only with -P)\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500674" -p, -- use blk_pread to read the file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200675" -P, -- use a pattern to verify read data\n"
676" -q, -- quiet mode, do not show I/O statistics\n"
677" -s, -- start offset for pattern verification (only with -P)\n"
678" -v, -- dump buffer to standard output\n"
679"\n");
680}
681
Max Reitz4c7b7e92015-02-05 13:58:22 -0500682static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200683
684static const cmdinfo_t read_cmd = {
685 .name = "read",
686 .altname = "r",
687 .cfunc = read_f,
688 .argmin = 2,
689 .argmax = -1,
690 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
691 .oneline = "reads a number of bytes at a specified offset",
692 .help = read_help,
693};
694
Max Reitz4c7b7e92015-02-05 13:58:22 -0500695static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200696{
697 struct timeval t1, t2;
698 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
699 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
700 int c, cnt;
701 char *buf;
702 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500703 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200704 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500705 int64_t total = 0;
706 int pattern = 0;
707 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200708
Eric Blakeb062ad82015-05-12 09:10:56 -0600709 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200710 switch (c) {
711 case 'b':
712 bflag = 1;
713 break;
714 case 'C':
715 Cflag = 1;
716 break;
717 case 'l':
718 lflag = 1;
719 pattern_count = cvtnum(optarg);
720 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500721 print_cvtnum_err(pattern_count, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200722 return 0;
723 }
724 break;
725 case 'p':
726 pflag = 1;
727 break;
728 case 'P':
729 Pflag = 1;
730 pattern = parse_pattern(optarg);
731 if (pattern < 0) {
732 return 0;
733 }
734 break;
735 case 'q':
736 qflag = 1;
737 break;
738 case 's':
739 sflag = 1;
740 pattern_offset = cvtnum(optarg);
741 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500742 print_cvtnum_err(pattern_offset, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200743 return 0;
744 }
745 break;
746 case 'v':
747 vflag = 1;
748 break;
749 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200750 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200751 }
752 }
753
754 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200755 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200756 }
757
758 if (bflag && pflag) {
759 printf("-b and -p cannot be specified at the same time\n");
760 return 0;
761 }
762
763 offset = cvtnum(argv[optind]);
764 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500765 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200766 return 0;
767 }
768
769 optind++;
770 count = cvtnum(argv[optind]);
771 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500772 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200773 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -0500774 } else if (count > SIZE_MAX) {
775 printf("length cannot exceed %" PRIu64 ", given %s\n",
776 (uint64_t) SIZE_MAX, argv[optind]);
777 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200778 }
779
780 if (!Pflag && (lflag || sflag)) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200781 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200782 }
783
784 if (!lflag) {
785 pattern_count = count - pattern_offset;
786 }
787
788 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
789 printf("pattern verification range exceeds end of read data\n");
790 return 0;
791 }
792
793 if (!pflag) {
794 if (offset & 0x1ff) {
795 printf("offset %" PRId64 " is not sector aligned\n",
796 offset);
797 return 0;
798 }
799 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -0500800 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200801 count);
802 return 0;
803 }
804 }
805
Max Reitz4c7b7e92015-02-05 13:58:22 -0500806 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200807
808 gettimeofday(&t1, NULL);
809 if (pflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500810 cnt = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200811 } else if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500812 cnt = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200813 } else {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500814 cnt = do_read(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200815 }
816 gettimeofday(&t2, NULL);
817
818 if (cnt < 0) {
819 printf("read failed: %s\n", strerror(-cnt));
820 goto out;
821 }
822
823 if (Pflag) {
824 void *cmp_buf = g_malloc(pattern_count);
825 memset(cmp_buf, pattern, pattern_count);
826 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
827 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500828 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200829 offset + pattern_offset, pattern_count);
830 }
831 g_free(cmp_buf);
832 }
833
834 if (qflag) {
835 goto out;
836 }
837
838 if (vflag) {
839 dump_buffer(buf, offset, count);
840 }
841
842 /* Finally, report back -- -C gives a parsable format */
843 t2 = tsub(t2, t1);
844 print_report("read", &t2, offset, count, total, cnt, Cflag);
845
846out:
847 qemu_io_free(buf);
848
849 return 0;
850}
851
852static void readv_help(void)
853{
854 printf(
855"\n"
856" reads a range of bytes from the given offset into multiple buffers\n"
857"\n"
858" Example:\n"
859" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
860"\n"
861" Reads a segment of the currently open file, optionally dumping it to the\n"
862" standard output stream (with -v option) for subsequent inspection.\n"
863" Uses multiple iovec buffers if more than one byte range is specified.\n"
864" -C, -- report statistics in a machine parsable format\n"
865" -P, -- use a pattern to verify read data\n"
866" -v, -- dump buffer to standard output\n"
867" -q, -- quiet mode, do not show I/O statistics\n"
868"\n");
869}
870
Max Reitz4c7b7e92015-02-05 13:58:22 -0500871static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200872
873static const cmdinfo_t readv_cmd = {
874 .name = "readv",
875 .cfunc = readv_f,
876 .argmin = 2,
877 .argmax = -1,
878 .args = "[-Cqv] [-P pattern ] off len [len..]",
879 .oneline = "reads a number of bytes at a specified offset",
880 .help = readv_help,
881};
882
Max Reitz4c7b7e92015-02-05 13:58:22 -0500883static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200884{
885 struct timeval t1, t2;
886 int Cflag = 0, qflag = 0, vflag = 0;
887 int c, cnt;
888 char *buf;
889 int64_t offset;
890 /* Some compilers get confused and warn if this is not initialized. */
891 int total = 0;
892 int nr_iov;
893 QEMUIOVector qiov;
894 int pattern = 0;
895 int Pflag = 0;
896
Eric Blakeb062ad82015-05-12 09:10:56 -0600897 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200898 switch (c) {
899 case 'C':
900 Cflag = 1;
901 break;
902 case 'P':
903 Pflag = 1;
904 pattern = parse_pattern(optarg);
905 if (pattern < 0) {
906 return 0;
907 }
908 break;
909 case 'q':
910 qflag = 1;
911 break;
912 case 'v':
913 vflag = 1;
914 break;
915 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200916 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200917 }
918 }
919
920 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200921 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200922 }
923
924
925 offset = cvtnum(argv[optind]);
926 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500927 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200928 return 0;
929 }
930 optind++;
931
932 if (offset & 0x1ff) {
933 printf("offset %" PRId64 " is not sector aligned\n",
934 offset);
935 return 0;
936 }
937
938 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500939 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200940 if (buf == NULL) {
941 return 0;
942 }
943
944 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -0500945 cnt = do_aio_readv(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200946 gettimeofday(&t2, NULL);
947
948 if (cnt < 0) {
949 printf("readv failed: %s\n", strerror(-cnt));
950 goto out;
951 }
952
953 if (Pflag) {
954 void *cmp_buf = g_malloc(qiov.size);
955 memset(cmp_buf, pattern, qiov.size);
956 if (memcmp(buf, cmp_buf, qiov.size)) {
957 printf("Pattern verification failed at offset %"
958 PRId64 ", %zd bytes\n", offset, qiov.size);
959 }
960 g_free(cmp_buf);
961 }
962
963 if (qflag) {
964 goto out;
965 }
966
967 if (vflag) {
968 dump_buffer(buf, offset, qiov.size);
969 }
970
971 /* Finally, report back -- -C gives a parsable format */
972 t2 = tsub(t2, t1);
973 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
974
975out:
976 qemu_iovec_destroy(&qiov);
977 qemu_io_free(buf);
978 return 0;
979}
980
981static void write_help(void)
982{
983 printf(
984"\n"
985" writes a range of bytes from the given offset\n"
986"\n"
987" Example:\n"
988" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
989"\n"
990" Writes into a segment of the currently open file, using a buffer\n"
991" filled with a set pattern (0xcdcdcdcd).\n"
992" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500993" -c, -- write compressed data with blk_write_compressed\n"
994" -p, -- use blk_pwrite to write the file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200995" -P, -- use different pattern to fill file\n"
996" -C, -- report statistics in a machine parsable format\n"
997" -q, -- quiet mode, do not show I/O statistics\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500998" -z, -- write zeroes using blk_co_write_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200999"\n");
1000}
1001
Max Reitz4c7b7e92015-02-05 13:58:22 -05001002static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001003
1004static const cmdinfo_t write_cmd = {
1005 .name = "write",
1006 .altname = "w",
1007 .cfunc = write_f,
1008 .argmin = 2,
1009 .argmax = -1,
1010 .args = "[-bcCpqz] [-P pattern ] off len",
1011 .oneline = "writes a number of bytes at a specified offset",
1012 .help = write_help,
1013};
1014
Max Reitz4c7b7e92015-02-05 13:58:22 -05001015static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001016{
1017 struct timeval t1, t2;
1018 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
1019 int cflag = 0;
1020 int c, cnt;
1021 char *buf = NULL;
1022 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -05001023 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001024 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -05001025 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001026 int pattern = 0xcd;
1027
Eric Blakeb062ad82015-05-12 09:10:56 -06001028 while ((c = getopt(argc, argv, "bcCpP:qz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001029 switch (c) {
1030 case 'b':
1031 bflag = 1;
1032 break;
1033 case 'c':
1034 cflag = 1;
1035 break;
1036 case 'C':
1037 Cflag = 1;
1038 break;
1039 case 'p':
1040 pflag = 1;
1041 break;
1042 case 'P':
1043 Pflag = 1;
1044 pattern = parse_pattern(optarg);
1045 if (pattern < 0) {
1046 return 0;
1047 }
1048 break;
1049 case 'q':
1050 qflag = 1;
1051 break;
1052 case 'z':
1053 zflag = 1;
1054 break;
1055 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001056 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001057 }
1058 }
1059
1060 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001061 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001062 }
1063
1064 if (bflag + pflag + zflag > 1) {
1065 printf("-b, -p, or -z cannot be specified at the same time\n");
1066 return 0;
1067 }
1068
1069 if (zflag && Pflag) {
1070 printf("-z and -P cannot be specified at the same time\n");
1071 return 0;
1072 }
1073
1074 offset = cvtnum(argv[optind]);
1075 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001076 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001077 return 0;
1078 }
1079
1080 optind++;
1081 count = cvtnum(argv[optind]);
1082 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001083 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001084 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001085 } else if (count > SIZE_MAX) {
1086 printf("length cannot exceed %" PRIu64 ", given %s\n",
1087 (uint64_t) SIZE_MAX, argv[optind]);
1088 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001089 }
1090
1091 if (!pflag) {
1092 if (offset & 0x1ff) {
1093 printf("offset %" PRId64 " is not sector aligned\n",
1094 offset);
1095 return 0;
1096 }
1097
1098 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -05001099 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001100 count);
1101 return 0;
1102 }
1103 }
1104
1105 if (!zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001106 buf = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001107 }
1108
1109 gettimeofday(&t1, NULL);
1110 if (pflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001111 cnt = do_pwrite(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001112 } else if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001113 cnt = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001114 } else if (zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001115 cnt = do_co_write_zeroes(blk, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001116 } else if (cflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001117 cnt = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001118 } else {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001119 cnt = do_write(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001120 }
1121 gettimeofday(&t2, NULL);
1122
1123 if (cnt < 0) {
1124 printf("write failed: %s\n", strerror(-cnt));
1125 goto out;
1126 }
1127
1128 if (qflag) {
1129 goto out;
1130 }
1131
1132 /* Finally, report back -- -C gives a parsable format */
1133 t2 = tsub(t2, t1);
1134 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1135
1136out:
1137 if (!zflag) {
1138 qemu_io_free(buf);
1139 }
1140
1141 return 0;
1142}
1143
1144static void
1145writev_help(void)
1146{
1147 printf(
1148"\n"
1149" writes a range of bytes from the given offset source from multiple buffers\n"
1150"\n"
1151" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001152" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001153"\n"
1154" Writes into a segment of the currently open file, using a buffer\n"
1155" filled with a set pattern (0xcdcdcdcd).\n"
1156" -P, -- use different pattern to fill file\n"
1157" -C, -- report statistics in a machine parsable format\n"
1158" -q, -- quiet mode, do not show I/O statistics\n"
1159"\n");
1160}
1161
Max Reitz4c7b7e92015-02-05 13:58:22 -05001162static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001163
1164static const cmdinfo_t writev_cmd = {
1165 .name = "writev",
1166 .cfunc = writev_f,
1167 .argmin = 2,
1168 .argmax = -1,
1169 .args = "[-Cq] [-P pattern ] off len [len..]",
1170 .oneline = "writes a number of bytes at a specified offset",
1171 .help = writev_help,
1172};
1173
Max Reitz4c7b7e92015-02-05 13:58:22 -05001174static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001175{
1176 struct timeval t1, t2;
1177 int Cflag = 0, qflag = 0;
1178 int c, cnt;
1179 char *buf;
1180 int64_t offset;
1181 /* Some compilers get confused and warn if this is not initialized. */
1182 int total = 0;
1183 int nr_iov;
1184 int pattern = 0xcd;
1185 QEMUIOVector qiov;
1186
Eric Blakeb062ad82015-05-12 09:10:56 -06001187 while ((c = getopt(argc, argv, "CqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001188 switch (c) {
1189 case 'C':
1190 Cflag = 1;
1191 break;
1192 case 'q':
1193 qflag = 1;
1194 break;
1195 case 'P':
1196 pattern = parse_pattern(optarg);
1197 if (pattern < 0) {
1198 return 0;
1199 }
1200 break;
1201 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001202 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001203 }
1204 }
1205
1206 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001207 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001208 }
1209
1210 offset = cvtnum(argv[optind]);
1211 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001212 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001213 return 0;
1214 }
1215 optind++;
1216
1217 if (offset & 0x1ff) {
1218 printf("offset %" PRId64 " is not sector aligned\n",
1219 offset);
1220 return 0;
1221 }
1222
1223 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001224 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001225 if (buf == NULL) {
1226 return 0;
1227 }
1228
1229 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001230 cnt = do_aio_writev(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001231 gettimeofday(&t2, NULL);
1232
1233 if (cnt < 0) {
1234 printf("writev failed: %s\n", strerror(-cnt));
1235 goto out;
1236 }
1237
1238 if (qflag) {
1239 goto out;
1240 }
1241
1242 /* Finally, report back -- -C gives a parsable format */
1243 t2 = tsub(t2, t1);
1244 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1245out:
1246 qemu_iovec_destroy(&qiov);
1247 qemu_io_free(buf);
1248 return 0;
1249}
1250
1251static void multiwrite_help(void)
1252{
1253 printf(
1254"\n"
1255" writes a range of bytes from the given offset source from multiple buffers,\n"
1256" in a batch of requests that may be merged by qemu\n"
1257"\n"
1258" Example:\n"
1259" 'multiwrite 512 1k 1k ; 4k 1k'\n"
1260" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1261"\n"
1262" Writes into a segment of the currently open file, using a buffer\n"
1263" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1264" by one for each request contained in the multiwrite command.\n"
1265" -P, -- use different pattern to fill file\n"
1266" -C, -- report statistics in a machine parsable format\n"
1267" -q, -- quiet mode, do not show I/O statistics\n"
1268"\n");
1269}
1270
Max Reitz4c7b7e92015-02-05 13:58:22 -05001271static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001272
1273static const cmdinfo_t multiwrite_cmd = {
1274 .name = "multiwrite",
1275 .cfunc = multiwrite_f,
1276 .argmin = 2,
1277 .argmax = -1,
1278 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1279 .oneline = "issues multiple write requests at once",
1280 .help = multiwrite_help,
1281};
1282
Max Reitz4c7b7e92015-02-05 13:58:22 -05001283static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001284{
1285 struct timeval t1, t2;
1286 int Cflag = 0, qflag = 0;
1287 int c, cnt;
1288 char **buf;
1289 int64_t offset, first_offset = 0;
1290 /* Some compilers get confused and warn if this is not initialized. */
1291 int total = 0;
1292 int nr_iov;
1293 int nr_reqs;
1294 int pattern = 0xcd;
1295 QEMUIOVector *qiovs;
1296 int i;
1297 BlockRequest *reqs;
1298
Eric Blakeb062ad82015-05-12 09:10:56 -06001299 while ((c = getopt(argc, argv, "CqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001300 switch (c) {
1301 case 'C':
1302 Cflag = 1;
1303 break;
1304 case 'q':
1305 qflag = 1;
1306 break;
1307 case 'P':
1308 pattern = parse_pattern(optarg);
1309 if (pattern < 0) {
1310 return 0;
1311 }
1312 break;
1313 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001314 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001315 }
1316 }
1317
1318 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001319 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001320 }
1321
1322 nr_reqs = 1;
1323 for (i = optind; i < argc; i++) {
1324 if (!strcmp(argv[i], ";")) {
1325 nr_reqs++;
1326 }
1327 }
1328
Markus Armbruster02c4f262014-08-19 10:31:09 +02001329 reqs = g_new0(BlockRequest, nr_reqs);
1330 buf = g_new0(char *, nr_reqs);
1331 qiovs = g_new(QEMUIOVector, nr_reqs);
Kevin Wolf797ac582013-06-05 14:19:31 +02001332
1333 for (i = 0; i < nr_reqs && optind < argc; i++) {
1334 int j;
1335
1336 /* Read the offset of the request */
1337 offset = cvtnum(argv[optind]);
1338 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001339 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001340 goto out;
1341 }
1342 optind++;
1343
1344 if (offset & 0x1ff) {
1345 printf("offset %lld is not sector aligned\n",
1346 (long long)offset);
1347 goto out;
1348 }
1349
1350 if (i == 0) {
1351 first_offset = offset;
1352 }
1353
1354 /* Read lengths for qiov entries */
1355 for (j = optind; j < argc; j++) {
1356 if (!strcmp(argv[j], ";")) {
1357 break;
1358 }
1359 }
1360
1361 nr_iov = j - optind;
1362
1363 /* Build request */
Max Reitz4c7b7e92015-02-05 13:58:22 -05001364 buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001365 if (buf[i] == NULL) {
1366 goto out;
1367 }
1368
1369 reqs[i].qiov = &qiovs[i];
1370 reqs[i].sector = offset >> 9;
1371 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1372
1373 optind = j + 1;
1374
1375 pattern++;
1376 }
1377
1378 /* If there were empty requests at the end, ignore them */
1379 nr_reqs = i;
1380
1381 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001382 cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001383 gettimeofday(&t2, NULL);
1384
1385 if (cnt < 0) {
1386 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1387 goto out;
1388 }
1389
1390 if (qflag) {
1391 goto out;
1392 }
1393
1394 /* Finally, report back -- -C gives a parsable format */
1395 t2 = tsub(t2, t1);
1396 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1397out:
1398 for (i = 0; i < nr_reqs; i++) {
1399 qemu_io_free(buf[i]);
1400 if (reqs[i].qiov != NULL) {
1401 qemu_iovec_destroy(&qiovs[i]);
1402 }
1403 }
1404 g_free(buf);
1405 g_free(reqs);
1406 g_free(qiovs);
1407 return 0;
1408}
1409
1410struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001411 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001412 QEMUIOVector qiov;
1413 int64_t offset;
1414 char *buf;
1415 int qflag;
1416 int vflag;
1417 int Cflag;
1418 int Pflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001419 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001420 int pattern;
1421 struct timeval t1;
1422};
1423
1424static void aio_write_done(void *opaque, int ret)
1425{
1426 struct aio_ctx *ctx = opaque;
1427 struct timeval t2;
1428
1429 gettimeofday(&t2, NULL);
1430
1431
1432 if (ret < 0) {
1433 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001434 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001435 goto out;
1436 }
1437
Max Reitz4c7b7e92015-02-05 13:58:22 -05001438 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001439
Kevin Wolf797ac582013-06-05 14:19:31 +02001440 if (ctx->qflag) {
1441 goto out;
1442 }
1443
1444 /* Finally, report back -- -C gives a parsable format */
1445 t2 = tsub(t2, ctx->t1);
1446 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1447 ctx->qiov.size, 1, ctx->Cflag);
1448out:
1449 qemu_io_free(ctx->buf);
1450 qemu_iovec_destroy(&ctx->qiov);
1451 g_free(ctx);
1452}
1453
1454static void aio_read_done(void *opaque, int ret)
1455{
1456 struct aio_ctx *ctx = opaque;
1457 struct timeval t2;
1458
1459 gettimeofday(&t2, NULL);
1460
1461 if (ret < 0) {
1462 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001463 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001464 goto out;
1465 }
1466
1467 if (ctx->Pflag) {
1468 void *cmp_buf = g_malloc(ctx->qiov.size);
1469
1470 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1471 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1472 printf("Pattern verification failed at offset %"
1473 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1474 }
1475 g_free(cmp_buf);
1476 }
1477
Max Reitz4c7b7e92015-02-05 13:58:22 -05001478 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001479
Kevin Wolf797ac582013-06-05 14:19:31 +02001480 if (ctx->qflag) {
1481 goto out;
1482 }
1483
1484 if (ctx->vflag) {
1485 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1486 }
1487
1488 /* Finally, report back -- -C gives a parsable format */
1489 t2 = tsub(t2, ctx->t1);
1490 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1491 ctx->qiov.size, 1, ctx->Cflag);
1492out:
1493 qemu_io_free(ctx->buf);
1494 qemu_iovec_destroy(&ctx->qiov);
1495 g_free(ctx);
1496}
1497
1498static void aio_read_help(void)
1499{
1500 printf(
1501"\n"
1502" asynchronously reads a range of bytes from the given offset\n"
1503"\n"
1504" Example:\n"
1505" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1506"\n"
1507" Reads a segment of the currently open file, optionally dumping it to the\n"
1508" standard output stream (with -v option) for subsequent inspection.\n"
1509" The read is performed asynchronously and the aio_flush command must be\n"
1510" used to ensure all outstanding aio requests have been completed.\n"
1511" -C, -- report statistics in a machine parsable format\n"
1512" -P, -- use a pattern to verify read data\n"
1513" -v, -- dump buffer to standard output\n"
1514" -q, -- quiet mode, do not show I/O statistics\n"
1515"\n");
1516}
1517
Max Reitz4c7b7e92015-02-05 13:58:22 -05001518static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001519
1520static const cmdinfo_t aio_read_cmd = {
1521 .name = "aio_read",
1522 .cfunc = aio_read_f,
1523 .argmin = 2,
1524 .argmax = -1,
1525 .args = "[-Cqv] [-P pattern ] off len [len..]",
1526 .oneline = "asynchronously reads a number of bytes",
1527 .help = aio_read_help,
1528};
1529
Max Reitz4c7b7e92015-02-05 13:58:22 -05001530static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001531{
1532 int nr_iov, c;
1533 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1534
Max Reitz4c7b7e92015-02-05 13:58:22 -05001535 ctx->blk = blk;
Eric Blakeb062ad82015-05-12 09:10:56 -06001536 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001537 switch (c) {
1538 case 'C':
1539 ctx->Cflag = 1;
1540 break;
1541 case 'P':
1542 ctx->Pflag = 1;
1543 ctx->pattern = parse_pattern(optarg);
1544 if (ctx->pattern < 0) {
1545 g_free(ctx);
1546 return 0;
1547 }
1548 break;
1549 case 'q':
1550 ctx->qflag = 1;
1551 break;
1552 case 'v':
1553 ctx->vflag = 1;
1554 break;
1555 default:
1556 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001557 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001558 }
1559 }
1560
1561 if (optind > argc - 2) {
1562 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001563 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001564 }
1565
1566 ctx->offset = cvtnum(argv[optind]);
1567 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001568 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001569 g_free(ctx);
1570 return 0;
1571 }
1572 optind++;
1573
1574 if (ctx->offset & 0x1ff) {
1575 printf("offset %" PRId64 " is not sector aligned\n",
1576 ctx->offset);
Alberto Garcia556c2b62015-10-28 17:33:08 +02001577 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001578 g_free(ctx);
1579 return 0;
1580 }
1581
1582 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001583 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001584 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001585 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001586 g_free(ctx);
1587 return 0;
1588 }
1589
1590 gettimeofday(&ctx->t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001591 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1592 BLOCK_ACCT_READ);
1593 blk_aio_readv(blk, ctx->offset >> 9, &ctx->qiov,
1594 ctx->qiov.size >> 9, aio_read_done, ctx);
Kevin Wolf797ac582013-06-05 14:19:31 +02001595 return 0;
1596}
1597
1598static void aio_write_help(void)
1599{
1600 printf(
1601"\n"
1602" asynchronously writes a range of bytes from the given offset source\n"
1603" from multiple buffers\n"
1604"\n"
1605" Example:\n"
1606" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1607"\n"
1608" Writes into a segment of the currently open file, using a buffer\n"
1609" filled with a set pattern (0xcdcdcdcd).\n"
1610" The write is performed asynchronously and the aio_flush command must be\n"
1611" used to ensure all outstanding aio requests have been completed.\n"
1612" -P, -- use different pattern to fill file\n"
1613" -C, -- report statistics in a machine parsable format\n"
1614" -q, -- quiet mode, do not show I/O statistics\n"
1615"\n");
1616}
1617
Max Reitz4c7b7e92015-02-05 13:58:22 -05001618static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001619
1620static const cmdinfo_t aio_write_cmd = {
1621 .name = "aio_write",
1622 .cfunc = aio_write_f,
1623 .argmin = 2,
1624 .argmax = -1,
1625 .args = "[-Cq] [-P pattern ] off len [len..]",
1626 .oneline = "asynchronously writes a number of bytes",
1627 .help = aio_write_help,
1628};
1629
Max Reitz4c7b7e92015-02-05 13:58:22 -05001630static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001631{
1632 int nr_iov, c;
1633 int pattern = 0xcd;
1634 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1635
Max Reitz4c7b7e92015-02-05 13:58:22 -05001636 ctx->blk = blk;
Eric Blakeb062ad82015-05-12 09:10:56 -06001637 while ((c = getopt(argc, argv, "CqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001638 switch (c) {
1639 case 'C':
1640 ctx->Cflag = 1;
1641 break;
1642 case 'q':
1643 ctx->qflag = 1;
1644 break;
1645 case 'P':
1646 pattern = parse_pattern(optarg);
1647 if (pattern < 0) {
1648 g_free(ctx);
1649 return 0;
1650 }
1651 break;
1652 default:
1653 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001654 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001655 }
1656 }
1657
1658 if (optind > argc - 2) {
1659 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001660 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001661 }
1662
1663 ctx->offset = cvtnum(argv[optind]);
1664 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001665 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001666 g_free(ctx);
1667 return 0;
1668 }
1669 optind++;
1670
1671 if (ctx->offset & 0x1ff) {
1672 printf("offset %" PRId64 " is not sector aligned\n",
1673 ctx->offset);
Alberto Garcia556c2b62015-10-28 17:33:08 +02001674 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
Kevin Wolf797ac582013-06-05 14:19:31 +02001675 g_free(ctx);
1676 return 0;
1677 }
1678
1679 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001680 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001681 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001682 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
Kevin Wolf797ac582013-06-05 14:19:31 +02001683 g_free(ctx);
1684 return 0;
1685 }
1686
1687 gettimeofday(&ctx->t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001688 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1689 BLOCK_ACCT_WRITE);
1690 blk_aio_writev(blk, ctx->offset >> 9, &ctx->qiov,
1691 ctx->qiov.size >> 9, aio_write_done, ctx);
Kevin Wolf797ac582013-06-05 14:19:31 +02001692 return 0;
1693}
1694
Max Reitz4c7b7e92015-02-05 13:58:22 -05001695static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001696{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001697 BlockAcctCookie cookie;
1698 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001699 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001700 block_acct_done(blk_get_stats(blk), &cookie);
Kevin Wolf797ac582013-06-05 14:19:31 +02001701 return 0;
1702}
1703
1704static const cmdinfo_t aio_flush_cmd = {
1705 .name = "aio_flush",
1706 .cfunc = aio_flush_f,
1707 .oneline = "completes all outstanding aio requests"
1708};
1709
Max Reitz4c7b7e92015-02-05 13:58:22 -05001710static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001711{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001712 blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001713 return 0;
1714}
1715
1716static const cmdinfo_t flush_cmd = {
1717 .name = "flush",
1718 .altname = "f",
1719 .cfunc = flush_f,
1720 .oneline = "flush all in-core file state to disk",
1721};
1722
Max Reitz4c7b7e92015-02-05 13:58:22 -05001723static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001724{
1725 int64_t offset;
1726 int ret;
1727
1728 offset = cvtnum(argv[1]);
1729 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001730 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001731 return 0;
1732 }
1733
Max Reitz4c7b7e92015-02-05 13:58:22 -05001734 ret = blk_truncate(blk, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02001735 if (ret < 0) {
1736 printf("truncate: %s\n", strerror(-ret));
1737 return 0;
1738 }
1739
1740 return 0;
1741}
1742
1743static const cmdinfo_t truncate_cmd = {
1744 .name = "truncate",
1745 .altname = "t",
1746 .cfunc = truncate_f,
1747 .argmin = 1,
1748 .argmax = 1,
1749 .args = "off",
1750 .oneline = "truncates the current file at the given offset",
1751};
1752
Max Reitz4c7b7e92015-02-05 13:58:22 -05001753static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001754{
1755 int64_t size;
1756 char s1[64];
1757
Max Reitz4c7b7e92015-02-05 13:58:22 -05001758 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001759 if (size < 0) {
1760 printf("getlength: %s\n", strerror(-size));
1761 return 0;
1762 }
1763
1764 cvtstr(size, s1, sizeof(s1));
1765 printf("%s\n", s1);
1766 return 0;
1767}
1768
1769
1770static const cmdinfo_t length_cmd = {
1771 .name = "length",
1772 .altname = "l",
1773 .cfunc = length_f,
1774 .oneline = "gets the length of the current file",
1775};
1776
1777
Max Reitz4c7b7e92015-02-05 13:58:22 -05001778static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001779{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001780 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001781 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001782 ImageInfoSpecific *spec_info;
Kevin Wolf797ac582013-06-05 14:19:31 +02001783 char s1[64], s2[64];
1784 int ret;
1785
1786 if (bs->drv && bs->drv->format_name) {
1787 printf("format name: %s\n", bs->drv->format_name);
1788 }
1789 if (bs->drv && bs->drv->protocol_name) {
1790 printf("format name: %s\n", bs->drv->protocol_name);
1791 }
1792
1793 ret = bdrv_get_info(bs, &bdi);
1794 if (ret) {
1795 return 0;
1796 }
1797
1798 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1799 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1800
1801 printf("cluster size: %s\n", s1);
1802 printf("vm state offset: %s\n", s2);
1803
Max Reitza8d8ecb2013-10-09 10:46:17 +02001804 spec_info = bdrv_get_specific_info(bs);
1805 if (spec_info) {
1806 printf("Format specific information:\n");
1807 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1808 qapi_free_ImageInfoSpecific(spec_info);
1809 }
1810
Kevin Wolf797ac582013-06-05 14:19:31 +02001811 return 0;
1812}
1813
1814
1815
1816static const cmdinfo_t info_cmd = {
1817 .name = "info",
1818 .altname = "i",
1819 .cfunc = info_f,
1820 .oneline = "prints information about the current file",
1821};
1822
1823static void discard_help(void)
1824{
1825 printf(
1826"\n"
1827" discards a range of bytes from the given offset\n"
1828"\n"
1829" Example:\n"
1830" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1831"\n"
1832" Discards a segment of the currently open file.\n"
1833" -C, -- report statistics in a machine parsable format\n"
1834" -q, -- quiet mode, do not show I/O statistics\n"
1835"\n");
1836}
1837
Max Reitz4c7b7e92015-02-05 13:58:22 -05001838static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001839
1840static const cmdinfo_t discard_cmd = {
1841 .name = "discard",
1842 .altname = "d",
1843 .cfunc = discard_f,
1844 .argmin = 2,
1845 .argmax = -1,
1846 .args = "[-Cq] off len",
1847 .oneline = "discards a number of bytes at a specified offset",
1848 .help = discard_help,
1849};
1850
Max Reitz4c7b7e92015-02-05 13:58:22 -05001851static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001852{
1853 struct timeval t1, t2;
1854 int Cflag = 0, qflag = 0;
1855 int c, ret;
John Snow9b0beaf2015-11-05 18:53:02 -05001856 int64_t offset, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001857
Eric Blakeb062ad82015-05-12 09:10:56 -06001858 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001859 switch (c) {
1860 case 'C':
1861 Cflag = 1;
1862 break;
1863 case 'q':
1864 qflag = 1;
1865 break;
1866 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001867 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001868 }
1869 }
1870
1871 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001872 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001873 }
1874
1875 offset = cvtnum(argv[optind]);
1876 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001877 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001878 return 0;
1879 }
1880
1881 optind++;
1882 count = cvtnum(argv[optind]);
1883 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001884 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001885 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001886 } else if (count >> BDRV_SECTOR_BITS > INT_MAX) {
1887 printf("length cannot exceed %"PRIu64", given %s\n",
1888 (uint64_t)INT_MAX << BDRV_SECTOR_BITS,
1889 argv[optind]);
1890 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001891 }
1892
1893 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001894 ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1895 count >> BDRV_SECTOR_BITS);
Kevin Wolf797ac582013-06-05 14:19:31 +02001896 gettimeofday(&t2, NULL);
1897
1898 if (ret < 0) {
1899 printf("discard failed: %s\n", strerror(-ret));
1900 goto out;
1901 }
1902
1903 /* Finally, report back -- -C gives a parsable format */
1904 if (!qflag) {
1905 t2 = tsub(t2, t1);
1906 print_report("discard", &t2, offset, count, count, 1, Cflag);
1907 }
1908
1909out:
1910 return 0;
1911}
1912
Max Reitz4c7b7e92015-02-05 13:58:22 -05001913static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001914{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001915 BlockDriverState *bs = blk_bs(blk);
John Snow9b0beaf2015-11-05 18:53:02 -05001916 int64_t offset, sector_num, nb_sectors, remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001917 char s1[64];
John Snow9b0beaf2015-11-05 18:53:02 -05001918 int num, ret;
1919 int64_t sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001920
1921 offset = cvtnum(argv[1]);
1922 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001923 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001924 return 0;
1925 } else if (offset & 0x1ff) {
1926 printf("offset %" PRId64 " is not sector aligned\n",
1927 offset);
1928 return 0;
1929 }
1930
1931 if (argc == 3) {
1932 nb_sectors = cvtnum(argv[2]);
1933 if (nb_sectors < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001934 print_cvtnum_err(nb_sectors, argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001935 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001936 } else if (nb_sectors > INT_MAX) {
1937 printf("length argument cannot exceed %d, given %s\n",
1938 INT_MAX, argv[2]);
1939 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001940 }
1941 } else {
1942 nb_sectors = 1;
1943 }
1944
1945 remaining = nb_sectors;
1946 sum_alloc = 0;
1947 sector_num = offset >> 9;
1948 while (remaining) {
1949 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001950 if (ret < 0) {
1951 printf("is_allocated failed: %s\n", strerror(-ret));
1952 return 0;
1953 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001954 sector_num += num;
1955 remaining -= num;
1956 if (ret) {
1957 sum_alloc += num;
1958 }
1959 if (num == 0) {
1960 nb_sectors -= remaining;
1961 remaining = 0;
1962 }
1963 }
1964
1965 cvtstr(offset, s1, sizeof(s1));
1966
John Snow9b0beaf2015-11-05 18:53:02 -05001967 printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001968 sum_alloc, nb_sectors, s1);
1969 return 0;
1970}
1971
1972static const cmdinfo_t alloc_cmd = {
1973 .name = "alloc",
1974 .altname = "a",
1975 .argmin = 1,
1976 .argmax = 2,
1977 .cfunc = alloc_f,
1978 .args = "off [sectors]",
1979 .oneline = "checks if a sector is present in the file",
1980};
1981
1982
1983static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1984 int64_t nb_sectors, int64_t *pnum)
1985{
1986 int num, num_checked;
1987 int ret, firstret;
1988
1989 num_checked = MIN(nb_sectors, INT_MAX);
1990 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1991 if (ret < 0) {
1992 return ret;
1993 }
1994
1995 firstret = ret;
1996 *pnum = num;
1997
1998 while (nb_sectors > 0 && ret == firstret) {
1999 sector_num += num;
2000 nb_sectors -= num;
2001
2002 num_checked = MIN(nb_sectors, INT_MAX);
2003 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02002004 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02002005 *pnum += num;
2006 } else {
2007 break;
2008 }
2009 }
2010
2011 return firstret;
2012}
2013
Max Reitz4c7b7e92015-02-05 13:58:22 -05002014static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002015{
2016 int64_t offset;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002017 int64_t nb_sectors, total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02002018 char s1[64];
2019 int64_t num;
2020 int ret;
2021 const char *retstr;
2022
2023 offset = 0;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002024 total_sectors = blk_nb_sectors(blk);
2025 if (total_sectors < 0) {
2026 error_report("Failed to query image length: %s",
2027 strerror(-total_sectors));
2028 return 0;
2029 }
2030
2031 nb_sectors = total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02002032
2033 do {
Max Reitz4c7b7e92015-02-05 13:58:22 -05002034 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002035 if (ret < 0) {
2036 error_report("Failed to get allocation status: %s", strerror(-ret));
2037 return 0;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002038 } else if (!num) {
2039 error_report("Unexpected end of image");
2040 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002041 }
2042
2043 retstr = ret ? " allocated" : "not allocated";
2044 cvtstr(offset << 9ULL, s1, sizeof(s1));
2045 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
2046 "at offset %s (%d)\n",
2047 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
2048
2049 offset += num;
2050 nb_sectors -= num;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002051 } while (offset < total_sectors);
Kevin Wolf797ac582013-06-05 14:19:31 +02002052
2053 return 0;
2054}
2055
2056static const cmdinfo_t map_cmd = {
2057 .name = "map",
2058 .argmin = 0,
2059 .argmax = 0,
2060 .cfunc = map_f,
2061 .args = "",
2062 .oneline = "prints the allocated areas of a file",
2063};
2064
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002065static void reopen_help(void)
2066{
2067 printf(
2068"\n"
2069" Changes the open options of an already opened image\n"
2070"\n"
2071" Example:\n"
2072" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2073"\n"
2074" -r, -- Reopen the image read-only\n"
2075" -c, -- Change the cache mode to the given value\n"
2076" -o, -- Changes block driver options (cf. 'open' command)\n"
2077"\n");
2078}
2079
2080static int reopen_f(BlockBackend *blk, int argc, char **argv);
2081
2082static QemuOptsList reopen_opts = {
2083 .name = "reopen",
2084 .merge_lists = true,
2085 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2086 .desc = {
2087 /* no elements => accept any params */
2088 { /* end of list */ }
2089 },
2090};
2091
2092static const cmdinfo_t reopen_cmd = {
2093 .name = "reopen",
2094 .argmin = 0,
2095 .argmax = -1,
2096 .cfunc = reopen_f,
2097 .args = "[-r] [-c cache] [-o options]",
2098 .oneline = "reopens an image with new options",
2099 .help = reopen_help,
2100};
2101
2102static int reopen_f(BlockBackend *blk, int argc, char **argv)
2103{
2104 BlockDriverState *bs = blk_bs(blk);
2105 QemuOpts *qopts;
2106 QDict *opts;
2107 int c;
2108 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002109 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002110
2111 BlockReopenQueue *brq;
2112 Error *local_err = NULL;
2113
2114 while ((c = getopt(argc, argv, "c:o:r")) != -1) {
2115 switch (c) {
2116 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002117 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002118 error_report("Invalid cache option: %s", optarg);
2119 return 0;
2120 }
2121 break;
2122 case 'o':
2123 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2124 qemu_opts_reset(&reopen_opts);
2125 return 0;
2126 }
2127 break;
2128 case 'r':
2129 flags &= ~BDRV_O_RDWR;
2130 break;
2131 default:
2132 qemu_opts_reset(&reopen_opts);
2133 return qemuio_command_usage(&reopen_cmd);
2134 }
2135 }
2136
2137 if (optind != argc) {
2138 qemu_opts_reset(&reopen_opts);
2139 return qemuio_command_usage(&reopen_cmd);
2140 }
2141
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002142 if (writethrough != blk_enable_write_cache(blk) &&
2143 blk_get_attached_dev(blk))
2144 {
2145 error_report("Cannot change cache.writeback: Device attached");
2146 qemu_opts_reset(&reopen_opts);
2147 return 0;
2148 }
2149
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002150 qopts = qemu_opts_find(&reopen_opts, NULL);
2151 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
2152 qemu_opts_reset(&reopen_opts);
2153
2154 brq = bdrv_reopen_queue(NULL, bs, opts, flags);
2155 bdrv_reopen_multiple(brq, &local_err);
2156 if (local_err) {
2157 error_report_err(local_err);
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002158 } else {
2159 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002160 }
2161
2162 return 0;
2163}
2164
Max Reitz4c7b7e92015-02-05 13:58:22 -05002165static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002166{
2167 int ret;
2168
Max Reitz4c7b7e92015-02-05 13:58:22 -05002169 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002170 if (ret < 0) {
2171 printf("Could not set breakpoint: %s\n", strerror(-ret));
2172 }
2173
2174 return 0;
2175}
2176
Max Reitz4c7b7e92015-02-05 13:58:22 -05002177static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002178{
2179 int ret;
2180
Max Reitz4c7b7e92015-02-05 13:58:22 -05002181 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002182 if (ret < 0) {
2183 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2184 }
2185
2186 return 0;
2187}
2188
Kevin Wolf797ac582013-06-05 14:19:31 +02002189static const cmdinfo_t break_cmd = {
2190 .name = "break",
2191 .argmin = 2,
2192 .argmax = 2,
2193 .cfunc = break_f,
2194 .args = "event tag",
2195 .oneline = "sets a breakpoint on event and tags the stopped "
2196 "request as tag",
2197};
2198
Fam Zheng4cc70e92013-11-20 10:01:54 +08002199static const cmdinfo_t remove_break_cmd = {
2200 .name = "remove_break",
2201 .argmin = 1,
2202 .argmax = 1,
2203 .cfunc = remove_break_f,
2204 .args = "tag",
2205 .oneline = "remove a breakpoint by tag",
2206};
2207
Max Reitz4c7b7e92015-02-05 13:58:22 -05002208static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002209{
2210 int ret;
2211
Max Reitz4c7b7e92015-02-05 13:58:22 -05002212 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002213 if (ret < 0) {
2214 printf("Could not resume request: %s\n", strerror(-ret));
2215 }
2216
2217 return 0;
2218}
2219
2220static const cmdinfo_t resume_cmd = {
2221 .name = "resume",
2222 .argmin = 1,
2223 .argmax = 1,
2224 .cfunc = resume_f,
2225 .args = "tag",
2226 .oneline = "resumes the request tagged as tag",
2227};
2228
Max Reitz4c7b7e92015-02-05 13:58:22 -05002229static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002230{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002231 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2232 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002233 }
2234
2235 return 0;
2236}
2237
2238static const cmdinfo_t wait_break_cmd = {
2239 .name = "wait_break",
2240 .argmin = 1,
2241 .argmax = 1,
2242 .cfunc = wait_break_f,
2243 .args = "tag",
2244 .oneline = "waits for the suspension of a request",
2245};
2246
Max Reitz4c7b7e92015-02-05 13:58:22 -05002247static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002248{
2249 abort();
2250}
2251
2252static const cmdinfo_t abort_cmd = {
2253 .name = "abort",
2254 .cfunc = abort_f,
2255 .flags = CMD_NOFILE_OK,
2256 .oneline = "simulate a program crash using abort(3)",
2257};
2258
Max Reitz0e82dc72014-12-08 10:48:10 +01002259static void sigraise_help(void)
2260{
2261 printf(
2262"\n"
2263" raises the given signal\n"
2264"\n"
2265" Example:\n"
2266" 'sigraise %i' - raises SIGTERM\n"
2267"\n"
2268" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2269" given to sigraise.\n"
2270"\n", SIGTERM);
2271}
2272
Max Reitz4c7b7e92015-02-05 13:58:22 -05002273static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002274
2275static const cmdinfo_t sigraise_cmd = {
2276 .name = "sigraise",
2277 .cfunc = sigraise_f,
2278 .argmin = 1,
2279 .argmax = 1,
2280 .flags = CMD_NOFILE_OK,
2281 .args = "signal",
2282 .oneline = "raises a signal",
2283 .help = sigraise_help,
2284};
2285
Max Reitz4c7b7e92015-02-05 13:58:22 -05002286static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002287{
John Snow9b0beaf2015-11-05 18:53:02 -05002288 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002289 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002290 print_cvtnum_err(sig, argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002291 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05002292 } else if (sig > NSIG) {
2293 printf("signal argument '%s' is too large to be a valid signal\n",
2294 argv[1]);
2295 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002296 }
2297
2298 /* Using raise() to kill this process does not necessarily flush all open
2299 * streams. At least stdout and stderr (although the latter should be
2300 * non-buffered anyway) should be flushed, though. */
2301 fflush(stdout);
2302 fflush(stderr);
2303
2304 raise(sig);
2305 return 0;
2306}
2307
Kevin Wolfcd33d022014-01-15 15:39:10 +01002308static void sleep_cb(void *opaque)
2309{
2310 bool *expired = opaque;
2311 *expired = true;
2312}
2313
Max Reitz4c7b7e92015-02-05 13:58:22 -05002314static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002315{
2316 char *endptr;
2317 long ms;
2318 struct QEMUTimer *timer;
2319 bool expired = false;
2320
2321 ms = strtol(argv[1], &endptr, 0);
2322 if (ms < 0 || *endptr != '\0') {
2323 printf("%s is not a valid number\n", argv[1]);
2324 return 0;
2325 }
2326
2327 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2328 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2329
2330 while (!expired) {
2331 main_loop_wait(false);
2332 }
2333
2334 timer_free(timer);
2335
2336 return 0;
2337}
2338
2339static const cmdinfo_t sleep_cmd = {
2340 .name = "sleep",
2341 .argmin = 1,
2342 .argmax = 1,
2343 .cfunc = sleep_f,
2344 .flags = CMD_NOFILE_OK,
2345 .oneline = "waits for the given value in milliseconds",
2346};
2347
Kevin Wolff18a8342013-06-05 14:19:33 +02002348static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2349{
2350 if (cmd) {
2351 printf("%s ", cmd);
2352 } else {
2353 printf("%s ", ct->name);
2354 if (ct->altname) {
2355 printf("(or %s) ", ct->altname);
2356 }
2357 }
2358
2359 if (ct->args) {
2360 printf("%s ", ct->args);
2361 }
2362 printf("-- %s\n", ct->oneline);
2363}
2364
2365static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2366{
2367 help_oneline(cmd, ct);
2368 if (ct->help) {
2369 ct->help();
2370 }
2371}
2372
2373static void help_all(void)
2374{
2375 const cmdinfo_t *ct;
2376
2377 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2378 help_oneline(ct->name, ct);
2379 }
2380 printf("\nUse 'help commandname' for extended help.\n");
2381}
2382
Max Reitz4c7b7e92015-02-05 13:58:22 -05002383static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002384{
2385 const cmdinfo_t *ct;
2386
2387 if (argc == 1) {
2388 help_all();
2389 return 0;
2390 }
2391
2392 ct = find_command(argv[1]);
2393 if (ct == NULL) {
2394 printf("command %s not found\n", argv[1]);
2395 return 0;
2396 }
2397
2398 help_onecmd(argv[1], ct);
2399 return 0;
2400}
2401
2402static const cmdinfo_t help_cmd = {
2403 .name = "help",
2404 .altname = "?",
2405 .cfunc = help_f,
2406 .argmin = 0,
2407 .argmax = 1,
2408 .flags = CMD_FLAG_GLOBAL,
2409 .args = "[command]",
2410 .oneline = "help for one or all commands",
2411};
2412
Max Reitz4c7b7e92015-02-05 13:58:22 -05002413bool qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002414{
2415 char *input;
2416 const cmdinfo_t *ct;
2417 char **v;
2418 int c;
2419 bool done = false;
2420
2421 input = g_strdup(cmd);
2422 v = breakline(input, &c);
2423 if (c) {
2424 ct = find_command(v[0]);
2425 if (ct) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05002426 done = command(blk, ct, c, v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002427 } else {
2428 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2429 }
2430 }
2431 g_free(input);
2432 g_free(v);
2433
2434 return done;
2435}
2436
Kevin Wolf797ac582013-06-05 14:19:31 +02002437static void __attribute((constructor)) init_qemuio_commands(void)
2438{
2439 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002440 qemuio_add_command(&help_cmd);
2441 qemuio_add_command(&read_cmd);
2442 qemuio_add_command(&readv_cmd);
2443 qemuio_add_command(&write_cmd);
2444 qemuio_add_command(&writev_cmd);
2445 qemuio_add_command(&multiwrite_cmd);
2446 qemuio_add_command(&aio_read_cmd);
2447 qemuio_add_command(&aio_write_cmd);
2448 qemuio_add_command(&aio_flush_cmd);
2449 qemuio_add_command(&flush_cmd);
2450 qemuio_add_command(&truncate_cmd);
2451 qemuio_add_command(&length_cmd);
2452 qemuio_add_command(&info_cmd);
2453 qemuio_add_command(&discard_cmd);
2454 qemuio_add_command(&alloc_cmd);
2455 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002456 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002457 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002458 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002459 qemuio_add_command(&resume_cmd);
2460 qemuio_add_command(&wait_break_cmd);
2461 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002462 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002463 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002464}