blob: e038c0309e3dc78c29b9da99e0fe46c2b472167b [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,
Eric Blakedc388522016-05-07 21:16:42 -0600348 int64_t count, int64_t total, int cnt, bool 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_pread(BlockBackend *blk, char *buf, int64_t offset,
423 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200424{
John Snow9b0beaf2015-11-05 18:53:02 -0500425 if (count > INT_MAX) {
426 return -ERANGE;
427 }
428
Max Reitz4c7b7e92015-02-05 13:58:22 -0500429 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200430 if (*total < 0) {
431 return *total;
432 }
433 return 1;
434}
435
John Snow9b0beaf2015-11-05 18:53:02 -0500436static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
437 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200438{
John Snow9b0beaf2015-11-05 18:53:02 -0500439 if (count > INT_MAX) {
440 return -ERANGE;
441 }
442
Eric Blake8341f002016-05-06 10:26:27 -0600443 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count, 0);
Kevin Wolf797ac582013-06-05 14:19:31 +0200444 if (*total < 0) {
445 return *total;
446 }
447 return 1;
448}
449
450typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500451 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200452 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500453 int64_t count;
454 int64_t *total;
Kevin Wolf797ac582013-06-05 14:19:31 +0200455 int ret;
456 bool done;
457} CoWriteZeroes;
458
459static void coroutine_fn co_write_zeroes_entry(void *opaque)
460{
461 CoWriteZeroes *data = opaque;
462
Eric Blake983a1602016-05-06 10:26:29 -0600463 data->ret = blk_co_write_zeroes(data->blk, data->offset, data->count, 0);
Kevin Wolf797ac582013-06-05 14:19:31 +0200464 data->done = true;
465 if (data->ret < 0) {
466 *data->total = data->ret;
467 return;
468 }
469
470 *data->total = data->count;
471}
472
John Snow9b0beaf2015-11-05 18:53:02 -0500473static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int64_t count,
474 int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200475{
476 Coroutine *co;
477 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500478 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200479 .offset = offset,
480 .count = count,
481 .total = total,
482 .done = false,
483 };
484
John Snow9b0beaf2015-11-05 18:53:02 -0500485 if (count >> BDRV_SECTOR_BITS > INT_MAX) {
486 return -ERANGE;
487 }
488
Kevin Wolf797ac582013-06-05 14:19:31 +0200489 co = qemu_coroutine_create(co_write_zeroes_entry);
490 qemu_coroutine_enter(co, &data);
491 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500492 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200493 }
494 if (data.ret < 0) {
495 return data.ret;
496 } else {
497 return 1;
498 }
499}
500
Max Reitz4c7b7e92015-02-05 13:58:22 -0500501static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500502 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200503{
504 int ret;
505
John Snow9b0beaf2015-11-05 18:53:02 -0500506 if (count >> 9 > INT_MAX) {
507 return -ERANGE;
508 }
509
Max Reitz4c7b7e92015-02-05 13:58:22 -0500510 ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
Kevin Wolf797ac582013-06-05 14:19:31 +0200511 if (ret < 0) {
512 return ret;
513 }
514 *total = count;
515 return 1;
516}
517
Max Reitz4c7b7e92015-02-05 13:58:22 -0500518static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500519 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200520{
John Snow9b0beaf2015-11-05 18:53:02 -0500521 if (count > INT_MAX) {
522 return -ERANGE;
523 }
524
Max Reitz4c7b7e92015-02-05 13:58:22 -0500525 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200526 if (*total < 0) {
527 return *total;
528 }
529 return 1;
530}
531
Max Reitz4c7b7e92015-02-05 13:58:22 -0500532static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500533 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200534{
John Snow9b0beaf2015-11-05 18:53:02 -0500535 if (count > INT_MAX) {
536 return -ERANGE;
537 }
538
Max Reitz4c7b7e92015-02-05 13:58:22 -0500539 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200540 if (*total < 0) {
541 return *total;
542 }
543 return 1;
544}
545
546#define NOT_DONE 0x7fffffff
547static void aio_rw_done(void *opaque, int ret)
548{
549 *(int *)opaque = ret;
550}
551
Max Reitz4c7b7e92015-02-05 13:58:22 -0500552static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200553 int64_t offset, int *total)
554{
555 int async_ret = NOT_DONE;
556
Eric Blake7b3f9712016-05-06 10:26:44 -0600557 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200558 while (async_ret == NOT_DONE) {
559 main_loop_wait(false);
560 }
561
562 *total = qiov->size;
563 return async_ret < 0 ? async_ret : 1;
564}
565
Max Reitz4c7b7e92015-02-05 13:58:22 -0500566static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200567 int64_t offset, int *total)
568{
569 int async_ret = NOT_DONE;
570
Eric Blake7b3f9712016-05-06 10:26:44 -0600571 blk_aio_pwritev(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200572 while (async_ret == NOT_DONE) {
573 main_loop_wait(false);
574 }
575
576 *total = qiov->size;
577 return async_ret < 0 ? async_ret : 1;
578}
579
580struct multiwrite_async_ret {
581 int num_done;
582 int error;
583};
584
585static void multiwrite_cb(void *opaque, int ret)
586{
587 struct multiwrite_async_ret *async_ret = opaque;
588
589 async_ret->num_done++;
590 if (ret < 0) {
591 async_ret->error = ret;
592 }
593}
594
Max Reitz4c7b7e92015-02-05 13:58:22 -0500595static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
Kevin Wolf797ac582013-06-05 14:19:31 +0200596 int num_reqs, int *total)
597{
598 int i, ret;
599 struct multiwrite_async_ret async_ret = {
600 .num_done = 0,
601 .error = 0,
602 };
603
604 *total = 0;
605 for (i = 0; i < num_reqs; i++) {
606 reqs[i].cb = multiwrite_cb;
607 reqs[i].opaque = &async_ret;
608 *total += reqs[i].qiov->size;
609 }
610
Max Reitz4c7b7e92015-02-05 13:58:22 -0500611 ret = blk_aio_multiwrite(blk, reqs, num_reqs);
Kevin Wolf797ac582013-06-05 14:19:31 +0200612 if (ret < 0) {
613 return ret;
614 }
615
616 while (async_ret.num_done < num_reqs) {
617 main_loop_wait(false);
618 }
619
620 return async_ret.error < 0 ? async_ret.error : 1;
621}
622
623static void read_help(void)
624{
625 printf(
626"\n"
627" reads a range of bytes from the given offset\n"
628"\n"
629" Example:\n"
630" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
631"\n"
632" Reads a segment of the currently open file, optionally dumping it to the\n"
633" standard output stream (with -v option) for subsequent inspection.\n"
634" -b, -- read from the VM state rather than the virtual disk\n"
635" -C, -- report statistics in a machine parsable format\n"
636" -l, -- length for pattern verification (only with -P)\n"
Eric Blake7b3f9712016-05-06 10:26:44 -0600637" -p, -- allow unaligned access\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200638" -P, -- use a pattern to verify read data\n"
639" -q, -- quiet mode, do not show I/O statistics\n"
640" -s, -- start offset for pattern verification (only with -P)\n"
641" -v, -- dump buffer to standard output\n"
642"\n");
643}
644
Max Reitz4c7b7e92015-02-05 13:58:22 -0500645static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200646
647static const cmdinfo_t read_cmd = {
648 .name = "read",
649 .altname = "r",
650 .cfunc = read_f,
651 .argmin = 2,
652 .argmax = -1,
653 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
654 .oneline = "reads a number of bytes at a specified offset",
655 .help = read_help,
656};
657
Max Reitz4c7b7e92015-02-05 13:58:22 -0500658static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200659{
660 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600661 bool Cflag = false, pflag = false, qflag = false, vflag = false;
662 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200663 int c, cnt;
664 char *buf;
665 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500666 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200667 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500668 int64_t total = 0;
669 int pattern = 0;
670 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200671
Eric Blakeb062ad82015-05-12 09:10:56 -0600672 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200673 switch (c) {
674 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600675 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200676 break;
677 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600678 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200679 break;
680 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600681 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200682 pattern_count = cvtnum(optarg);
683 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500684 print_cvtnum_err(pattern_count, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200685 return 0;
686 }
687 break;
688 case 'p':
Eric Blakedc388522016-05-07 21:16:42 -0600689 pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200690 break;
691 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600692 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200693 pattern = parse_pattern(optarg);
694 if (pattern < 0) {
695 return 0;
696 }
697 break;
698 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600699 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200700 break;
701 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600702 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200703 pattern_offset = cvtnum(optarg);
704 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500705 print_cvtnum_err(pattern_offset, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200706 return 0;
707 }
708 break;
709 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600710 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200711 break;
712 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200713 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200714 }
715 }
716
717 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200718 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200719 }
720
721 if (bflag && pflag) {
722 printf("-b and -p cannot be specified at the same time\n");
723 return 0;
724 }
725
726 offset = cvtnum(argv[optind]);
727 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500728 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200729 return 0;
730 }
731
732 optind++;
733 count = cvtnum(argv[optind]);
734 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500735 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200736 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -0500737 } else if (count > SIZE_MAX) {
738 printf("length cannot exceed %" PRIu64 ", given %s\n",
739 (uint64_t) SIZE_MAX, argv[optind]);
740 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200741 }
742
743 if (!Pflag && (lflag || sflag)) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200744 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200745 }
746
747 if (!lflag) {
748 pattern_count = count - pattern_offset;
749 }
750
751 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
752 printf("pattern verification range exceeds end of read data\n");
753 return 0;
754 }
755
756 if (!pflag) {
757 if (offset & 0x1ff) {
758 printf("offset %" PRId64 " is not sector aligned\n",
759 offset);
760 return 0;
761 }
762 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -0500763 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200764 count);
765 return 0;
766 }
767 }
768
Max Reitz4c7b7e92015-02-05 13:58:22 -0500769 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200770
771 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -0600772 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500773 cnt = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200774 } else {
Eric Blake7b3f9712016-05-06 10:26:44 -0600775 cnt = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200776 }
777 gettimeofday(&t2, NULL);
778
779 if (cnt < 0) {
780 printf("read failed: %s\n", strerror(-cnt));
781 goto out;
782 }
783
784 if (Pflag) {
785 void *cmp_buf = g_malloc(pattern_count);
786 memset(cmp_buf, pattern, pattern_count);
787 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
788 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500789 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200790 offset + pattern_offset, pattern_count);
791 }
792 g_free(cmp_buf);
793 }
794
795 if (qflag) {
796 goto out;
797 }
798
799 if (vflag) {
800 dump_buffer(buf, offset, count);
801 }
802
803 /* Finally, report back -- -C gives a parsable format */
804 t2 = tsub(t2, t1);
805 print_report("read", &t2, offset, count, total, cnt, Cflag);
806
807out:
808 qemu_io_free(buf);
809
810 return 0;
811}
812
813static void readv_help(void)
814{
815 printf(
816"\n"
817" reads a range of bytes from the given offset into multiple buffers\n"
818"\n"
819" Example:\n"
820" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
821"\n"
822" Reads a segment of the currently open file, optionally dumping it to the\n"
823" standard output stream (with -v option) for subsequent inspection.\n"
824" Uses multiple iovec buffers if more than one byte range is specified.\n"
825" -C, -- report statistics in a machine parsable format\n"
826" -P, -- use a pattern to verify read data\n"
827" -v, -- dump buffer to standard output\n"
828" -q, -- quiet mode, do not show I/O statistics\n"
829"\n");
830}
831
Max Reitz4c7b7e92015-02-05 13:58:22 -0500832static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200833
834static const cmdinfo_t readv_cmd = {
835 .name = "readv",
836 .cfunc = readv_f,
837 .argmin = 2,
838 .argmax = -1,
839 .args = "[-Cqv] [-P pattern ] off len [len..]",
840 .oneline = "reads a number of bytes at a specified offset",
841 .help = readv_help,
842};
843
Max Reitz4c7b7e92015-02-05 13:58:22 -0500844static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200845{
846 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600847 bool Cflag = false, qflag = false, vflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200848 int c, cnt;
849 char *buf;
850 int64_t offset;
851 /* Some compilers get confused and warn if this is not initialized. */
852 int total = 0;
853 int nr_iov;
854 QEMUIOVector qiov;
855 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600856 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200857
Eric Blakeb062ad82015-05-12 09:10:56 -0600858 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200859 switch (c) {
860 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600861 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200862 break;
863 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600864 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200865 pattern = parse_pattern(optarg);
866 if (pattern < 0) {
867 return 0;
868 }
869 break;
870 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600871 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200872 break;
873 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600874 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200875 break;
876 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200877 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200878 }
879 }
880
881 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200882 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200883 }
884
885
886 offset = cvtnum(argv[optind]);
887 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500888 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200889 return 0;
890 }
891 optind++;
892
893 if (offset & 0x1ff) {
894 printf("offset %" PRId64 " is not sector aligned\n",
895 offset);
896 return 0;
897 }
898
899 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500900 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200901 if (buf == NULL) {
902 return 0;
903 }
904
905 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -0500906 cnt = do_aio_readv(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200907 gettimeofday(&t2, NULL);
908
909 if (cnt < 0) {
910 printf("readv failed: %s\n", strerror(-cnt));
911 goto out;
912 }
913
914 if (Pflag) {
915 void *cmp_buf = g_malloc(qiov.size);
916 memset(cmp_buf, pattern, qiov.size);
917 if (memcmp(buf, cmp_buf, qiov.size)) {
918 printf("Pattern verification failed at offset %"
919 PRId64 ", %zd bytes\n", offset, qiov.size);
920 }
921 g_free(cmp_buf);
922 }
923
924 if (qflag) {
925 goto out;
926 }
927
928 if (vflag) {
929 dump_buffer(buf, offset, qiov.size);
930 }
931
932 /* Finally, report back -- -C gives a parsable format */
933 t2 = tsub(t2, t1);
934 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
935
936out:
937 qemu_iovec_destroy(&qiov);
938 qemu_io_free(buf);
939 return 0;
940}
941
942static void write_help(void)
943{
944 printf(
945"\n"
946" writes a range of bytes from the given offset\n"
947"\n"
948" Example:\n"
949" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
950"\n"
951" Writes into a segment of the currently open file, using a buffer\n"
952" filled with a set pattern (0xcdcdcdcd).\n"
953" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500954" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake7b3f9712016-05-06 10:26:44 -0600955" -p, -- allow unaligned access\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200956" -P, -- use different pattern to fill file\n"
957" -C, -- report statistics in a machine parsable format\n"
958" -q, -- quiet mode, do not show I/O statistics\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500959" -z, -- write zeroes using blk_co_write_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200960"\n");
961}
962
Max Reitz4c7b7e92015-02-05 13:58:22 -0500963static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200964
965static const cmdinfo_t write_cmd = {
966 .name = "write",
967 .altname = "w",
968 .cfunc = write_f,
969 .argmin = 2,
970 .argmax = -1,
971 .args = "[-bcCpqz] [-P pattern ] off len",
972 .oneline = "writes a number of bytes at a specified offset",
973 .help = write_help,
974};
975
Max Reitz4c7b7e92015-02-05 13:58:22 -0500976static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200977{
978 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600979 bool Cflag = false, pflag = false, qflag = false, bflag = false;
980 bool Pflag = false, zflag = false, cflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200981 int c, cnt;
982 char *buf = NULL;
983 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500984 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200985 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500986 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200987 int pattern = 0xcd;
988
Eric Blakeb062ad82015-05-12 09:10:56 -0600989 while ((c = getopt(argc, argv, "bcCpP:qz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200990 switch (c) {
991 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600992 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200993 break;
994 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -0600995 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200996 break;
997 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600998 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200999 break;
1000 case 'p':
Eric Blakedc388522016-05-07 21:16:42 -06001001 pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001002 break;
1003 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001004 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001005 pattern = parse_pattern(optarg);
1006 if (pattern < 0) {
1007 return 0;
1008 }
1009 break;
1010 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001011 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001012 break;
1013 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001014 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001015 break;
1016 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001017 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001018 }
1019 }
1020
1021 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001022 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001023 }
1024
1025 if (bflag + pflag + zflag > 1) {
1026 printf("-b, -p, or -z cannot be specified at the same time\n");
1027 return 0;
1028 }
1029
1030 if (zflag && Pflag) {
1031 printf("-z and -P cannot be specified at the same time\n");
1032 return 0;
1033 }
1034
1035 offset = cvtnum(argv[optind]);
1036 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001037 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001038 return 0;
1039 }
1040
1041 optind++;
1042 count = cvtnum(argv[optind]);
1043 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001044 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001045 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001046 } else if (count > SIZE_MAX) {
1047 printf("length cannot exceed %" PRIu64 ", given %s\n",
1048 (uint64_t) SIZE_MAX, argv[optind]);
1049 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001050 }
1051
1052 if (!pflag) {
1053 if (offset & 0x1ff) {
1054 printf("offset %" PRId64 " is not sector aligned\n",
1055 offset);
1056 return 0;
1057 }
1058
1059 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -05001060 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001061 count);
1062 return 0;
1063 }
1064 }
1065
1066 if (!zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001067 buf = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001068 }
1069
1070 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -06001071 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001072 cnt = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001073 } else if (zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001074 cnt = do_co_write_zeroes(blk, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001075 } else if (cflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001076 cnt = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001077 } else {
Eric Blake7b3f9712016-05-06 10:26:44 -06001078 cnt = do_pwrite(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001079 }
1080 gettimeofday(&t2, NULL);
1081
1082 if (cnt < 0) {
1083 printf("write failed: %s\n", strerror(-cnt));
1084 goto out;
1085 }
1086
1087 if (qflag) {
1088 goto out;
1089 }
1090
1091 /* Finally, report back -- -C gives a parsable format */
1092 t2 = tsub(t2, t1);
1093 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1094
1095out:
1096 if (!zflag) {
1097 qemu_io_free(buf);
1098 }
1099
1100 return 0;
1101}
1102
1103static void
1104writev_help(void)
1105{
1106 printf(
1107"\n"
1108" writes a range of bytes from the given offset source from multiple buffers\n"
1109"\n"
1110" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001111" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001112"\n"
1113" Writes into a segment of the currently open file, using a buffer\n"
1114" filled with a set pattern (0xcdcdcdcd).\n"
1115" -P, -- use different pattern to fill file\n"
1116" -C, -- report statistics in a machine parsable format\n"
1117" -q, -- quiet mode, do not show I/O statistics\n"
1118"\n");
1119}
1120
Max Reitz4c7b7e92015-02-05 13:58:22 -05001121static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001122
1123static const cmdinfo_t writev_cmd = {
1124 .name = "writev",
1125 .cfunc = writev_f,
1126 .argmin = 2,
1127 .argmax = -1,
1128 .args = "[-Cq] [-P pattern ] off len [len..]",
1129 .oneline = "writes a number of bytes at a specified offset",
1130 .help = writev_help,
1131};
1132
Max Reitz4c7b7e92015-02-05 13:58:22 -05001133static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001134{
1135 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001136 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001137 int c, cnt;
1138 char *buf;
1139 int64_t offset;
1140 /* Some compilers get confused and warn if this is not initialized. */
1141 int total = 0;
1142 int nr_iov;
1143 int pattern = 0xcd;
1144 QEMUIOVector qiov;
1145
Eric Blakeb062ad82015-05-12 09:10:56 -06001146 while ((c = getopt(argc, argv, "CqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001147 switch (c) {
1148 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001149 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001150 break;
1151 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001152 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001153 break;
1154 case 'P':
1155 pattern = parse_pattern(optarg);
1156 if (pattern < 0) {
1157 return 0;
1158 }
1159 break;
1160 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001161 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001162 }
1163 }
1164
1165 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001166 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001167 }
1168
1169 offset = cvtnum(argv[optind]);
1170 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001171 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001172 return 0;
1173 }
1174 optind++;
1175
1176 if (offset & 0x1ff) {
1177 printf("offset %" PRId64 " is not sector aligned\n",
1178 offset);
1179 return 0;
1180 }
1181
1182 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001183 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001184 if (buf == NULL) {
1185 return 0;
1186 }
1187
1188 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001189 cnt = do_aio_writev(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001190 gettimeofday(&t2, NULL);
1191
1192 if (cnt < 0) {
1193 printf("writev failed: %s\n", strerror(-cnt));
1194 goto out;
1195 }
1196
1197 if (qflag) {
1198 goto out;
1199 }
1200
1201 /* Finally, report back -- -C gives a parsable format */
1202 t2 = tsub(t2, t1);
1203 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1204out:
1205 qemu_iovec_destroy(&qiov);
1206 qemu_io_free(buf);
1207 return 0;
1208}
1209
1210static void multiwrite_help(void)
1211{
1212 printf(
1213"\n"
1214" writes a range of bytes from the given offset source from multiple buffers,\n"
1215" in a batch of requests that may be merged by qemu\n"
1216"\n"
1217" Example:\n"
1218" 'multiwrite 512 1k 1k ; 4k 1k'\n"
1219" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1220"\n"
1221" Writes into a segment of the currently open file, using a buffer\n"
1222" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1223" by one for each request contained in the multiwrite command.\n"
1224" -P, -- use different pattern to fill file\n"
1225" -C, -- report statistics in a machine parsable format\n"
1226" -q, -- quiet mode, do not show I/O statistics\n"
1227"\n");
1228}
1229
Max Reitz4c7b7e92015-02-05 13:58:22 -05001230static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001231
1232static const cmdinfo_t multiwrite_cmd = {
1233 .name = "multiwrite",
1234 .cfunc = multiwrite_f,
1235 .argmin = 2,
1236 .argmax = -1,
1237 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1238 .oneline = "issues multiple write requests at once",
1239 .help = multiwrite_help,
1240};
1241
Max Reitz4c7b7e92015-02-05 13:58:22 -05001242static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001243{
1244 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001245 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001246 int c, cnt;
1247 char **buf;
1248 int64_t offset, first_offset = 0;
1249 /* Some compilers get confused and warn if this is not initialized. */
1250 int total = 0;
1251 int nr_iov;
1252 int nr_reqs;
1253 int pattern = 0xcd;
1254 QEMUIOVector *qiovs;
1255 int i;
1256 BlockRequest *reqs;
1257
Eric Blakeb062ad82015-05-12 09:10:56 -06001258 while ((c = getopt(argc, argv, "CqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001259 switch (c) {
1260 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001261 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001262 break;
1263 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001264 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001265 break;
1266 case 'P':
1267 pattern = parse_pattern(optarg);
1268 if (pattern < 0) {
1269 return 0;
1270 }
1271 break;
1272 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001273 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001274 }
1275 }
1276
1277 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001278 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001279 }
1280
1281 nr_reqs = 1;
1282 for (i = optind; i < argc; i++) {
1283 if (!strcmp(argv[i], ";")) {
1284 nr_reqs++;
1285 }
1286 }
1287
Markus Armbruster02c4f262014-08-19 10:31:09 +02001288 reqs = g_new0(BlockRequest, nr_reqs);
1289 buf = g_new0(char *, nr_reqs);
1290 qiovs = g_new(QEMUIOVector, nr_reqs);
Kevin Wolf797ac582013-06-05 14:19:31 +02001291
1292 for (i = 0; i < nr_reqs && optind < argc; i++) {
1293 int j;
1294
1295 /* Read the offset of the request */
1296 offset = cvtnum(argv[optind]);
1297 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001298 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001299 goto out;
1300 }
1301 optind++;
1302
1303 if (offset & 0x1ff) {
1304 printf("offset %lld is not sector aligned\n",
1305 (long long)offset);
1306 goto out;
1307 }
1308
1309 if (i == 0) {
1310 first_offset = offset;
1311 }
1312
1313 /* Read lengths for qiov entries */
1314 for (j = optind; j < argc; j++) {
1315 if (!strcmp(argv[j], ";")) {
1316 break;
1317 }
1318 }
1319
1320 nr_iov = j - optind;
1321
1322 /* Build request */
Max Reitz4c7b7e92015-02-05 13:58:22 -05001323 buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001324 if (buf[i] == NULL) {
1325 goto out;
1326 }
1327
1328 reqs[i].qiov = &qiovs[i];
1329 reqs[i].sector = offset >> 9;
1330 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1331
1332 optind = j + 1;
1333
1334 pattern++;
1335 }
1336
1337 /* If there were empty requests at the end, ignore them */
1338 nr_reqs = i;
1339
1340 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001341 cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001342 gettimeofday(&t2, NULL);
1343
1344 if (cnt < 0) {
1345 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1346 goto out;
1347 }
1348
1349 if (qflag) {
1350 goto out;
1351 }
1352
1353 /* Finally, report back -- -C gives a parsable format */
1354 t2 = tsub(t2, t1);
1355 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1356out:
1357 for (i = 0; i < nr_reqs; i++) {
1358 qemu_io_free(buf[i]);
1359 if (reqs[i].qiov != NULL) {
1360 qemu_iovec_destroy(&qiovs[i]);
1361 }
1362 }
1363 g_free(buf);
1364 g_free(reqs);
1365 g_free(qiovs);
1366 return 0;
1367}
1368
1369struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001370 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001371 QEMUIOVector qiov;
1372 int64_t offset;
1373 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001374 bool qflag;
1375 bool vflag;
1376 bool Cflag;
1377 bool Pflag;
1378 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001379 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001380 int pattern;
1381 struct timeval t1;
1382};
1383
1384static void aio_write_done(void *opaque, int ret)
1385{
1386 struct aio_ctx *ctx = opaque;
1387 struct timeval t2;
1388
1389 gettimeofday(&t2, NULL);
1390
1391
1392 if (ret < 0) {
1393 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001394 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001395 goto out;
1396 }
1397
Max Reitz4c7b7e92015-02-05 13:58:22 -05001398 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001399
Kevin Wolf797ac582013-06-05 14:19:31 +02001400 if (ctx->qflag) {
1401 goto out;
1402 }
1403
1404 /* Finally, report back -- -C gives a parsable format */
1405 t2 = tsub(t2, ctx->t1);
1406 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1407 ctx->qiov.size, 1, ctx->Cflag);
1408out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001409 if (!ctx->zflag) {
1410 qemu_io_free(ctx->buf);
1411 qemu_iovec_destroy(&ctx->qiov);
1412 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001413 g_free(ctx);
1414}
1415
1416static void aio_read_done(void *opaque, int ret)
1417{
1418 struct aio_ctx *ctx = opaque;
1419 struct timeval t2;
1420
1421 gettimeofday(&t2, NULL);
1422
1423 if (ret < 0) {
1424 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001425 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001426 goto out;
1427 }
1428
1429 if (ctx->Pflag) {
1430 void *cmp_buf = g_malloc(ctx->qiov.size);
1431
1432 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1433 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1434 printf("Pattern verification failed at offset %"
1435 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1436 }
1437 g_free(cmp_buf);
1438 }
1439
Max Reitz4c7b7e92015-02-05 13:58:22 -05001440 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001441
Kevin Wolf797ac582013-06-05 14:19:31 +02001442 if (ctx->qflag) {
1443 goto out;
1444 }
1445
1446 if (ctx->vflag) {
1447 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1448 }
1449
1450 /* Finally, report back -- -C gives a parsable format */
1451 t2 = tsub(t2, ctx->t1);
1452 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1453 ctx->qiov.size, 1, ctx->Cflag);
1454out:
1455 qemu_io_free(ctx->buf);
1456 qemu_iovec_destroy(&ctx->qiov);
1457 g_free(ctx);
1458}
1459
1460static void aio_read_help(void)
1461{
1462 printf(
1463"\n"
1464" asynchronously reads a range of bytes from the given offset\n"
1465"\n"
1466" Example:\n"
1467" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1468"\n"
1469" Reads a segment of the currently open file, optionally dumping it to the\n"
1470" standard output stream (with -v option) for subsequent inspection.\n"
1471" The read is performed asynchronously and the aio_flush command must be\n"
1472" used to ensure all outstanding aio requests have been completed.\n"
1473" -C, -- report statistics in a machine parsable format\n"
1474" -P, -- use a pattern to verify read data\n"
1475" -v, -- dump buffer to standard output\n"
1476" -q, -- quiet mode, do not show I/O statistics\n"
1477"\n");
1478}
1479
Max Reitz4c7b7e92015-02-05 13:58:22 -05001480static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001481
1482static const cmdinfo_t aio_read_cmd = {
1483 .name = "aio_read",
1484 .cfunc = aio_read_f,
1485 .argmin = 2,
1486 .argmax = -1,
1487 .args = "[-Cqv] [-P pattern ] off len [len..]",
1488 .oneline = "asynchronously reads a number of bytes",
1489 .help = aio_read_help,
1490};
1491
Max Reitz4c7b7e92015-02-05 13:58:22 -05001492static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001493{
1494 int nr_iov, c;
1495 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1496
Max Reitz4c7b7e92015-02-05 13:58:22 -05001497 ctx->blk = blk;
Eric Blakeb062ad82015-05-12 09:10:56 -06001498 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001499 switch (c) {
1500 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001501 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001502 break;
1503 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001504 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001505 ctx->pattern = parse_pattern(optarg);
1506 if (ctx->pattern < 0) {
1507 g_free(ctx);
1508 return 0;
1509 }
1510 break;
1511 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001512 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001513 break;
1514 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001515 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001516 break;
1517 default:
1518 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001519 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001520 }
1521 }
1522
1523 if (optind > argc - 2) {
1524 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001525 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001526 }
1527
1528 ctx->offset = cvtnum(argv[optind]);
1529 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001530 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001531 g_free(ctx);
1532 return 0;
1533 }
1534 optind++;
1535
1536 if (ctx->offset & 0x1ff) {
1537 printf("offset %" PRId64 " is not sector aligned\n",
1538 ctx->offset);
Alberto Garcia556c2b62015-10-28 17:33:08 +02001539 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001540 g_free(ctx);
1541 return 0;
1542 }
1543
1544 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001545 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001546 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001547 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001548 g_free(ctx);
1549 return 0;
1550 }
1551
1552 gettimeofday(&ctx->t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001553 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1554 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001555 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Kevin Wolf797ac582013-06-05 14:19:31 +02001556 return 0;
1557}
1558
1559static void aio_write_help(void)
1560{
1561 printf(
1562"\n"
1563" asynchronously writes a range of bytes from the given offset source\n"
1564" from multiple buffers\n"
1565"\n"
1566" Example:\n"
1567" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1568"\n"
1569" Writes into a segment of the currently open file, using a buffer\n"
1570" filled with a set pattern (0xcdcdcdcd).\n"
1571" The write is performed asynchronously and the aio_flush command must be\n"
1572" used to ensure all outstanding aio requests have been completed.\n"
1573" -P, -- use different pattern to fill file\n"
1574" -C, -- report statistics in a machine parsable format\n"
1575" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001576" -z, -- write zeroes using blk_aio_write_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001577"\n");
1578}
1579
Max Reitz4c7b7e92015-02-05 13:58:22 -05001580static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001581
1582static const cmdinfo_t aio_write_cmd = {
1583 .name = "aio_write",
1584 .cfunc = aio_write_f,
1585 .argmin = 2,
1586 .argmax = -1,
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001587 .args = "[-Cqz] [-P pattern ] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001588 .oneline = "asynchronously writes a number of bytes",
1589 .help = aio_write_help,
1590};
1591
Max Reitz4c7b7e92015-02-05 13:58:22 -05001592static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001593{
1594 int nr_iov, c;
1595 int pattern = 0xcd;
1596 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1597
Max Reitz4c7b7e92015-02-05 13:58:22 -05001598 ctx->blk = blk;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001599 while ((c = getopt(argc, argv, "CqP:z")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001600 switch (c) {
1601 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001602 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001603 break;
1604 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001605 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001606 break;
1607 case 'P':
1608 pattern = parse_pattern(optarg);
1609 if (pattern < 0) {
1610 g_free(ctx);
1611 return 0;
1612 }
1613 break;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001614 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001615 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001616 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001617 default:
1618 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001619 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001620 }
1621 }
1622
1623 if (optind > argc - 2) {
1624 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001625 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001626 }
1627
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001628 if (ctx->zflag && optind != argc - 2) {
1629 printf("-z supports only a single length parameter\n");
1630 g_free(ctx);
1631 return 0;
1632 }
1633
1634 if (ctx->zflag && ctx->Pflag) {
1635 printf("-z and -P cannot be specified at the same time\n");
1636 g_free(ctx);
1637 return 0;
1638 }
1639
Kevin Wolf797ac582013-06-05 14:19:31 +02001640 ctx->offset = cvtnum(argv[optind]);
1641 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001642 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001643 g_free(ctx);
1644 return 0;
1645 }
1646 optind++;
1647
1648 if (ctx->offset & 0x1ff) {
1649 printf("offset %" PRId64 " is not sector aligned\n",
1650 ctx->offset);
Alberto Garcia556c2b62015-10-28 17:33:08 +02001651 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
Kevin Wolf797ac582013-06-05 14:19:31 +02001652 g_free(ctx);
1653 return 0;
1654 }
1655
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001656 if (ctx->zflag) {
1657 int64_t count = cvtnum(argv[optind]);
1658 if (count < 0) {
1659 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001660 g_free(ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001661 return 0;
1662 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001663
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001664 ctx->qiov.size = count;
Eric Blake983a1602016-05-06 10:26:29 -06001665 blk_aio_write_zeroes(blk, ctx->offset, count, 0, aio_write_done, ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001666 } else {
1667 nr_iov = argc - optind;
1668 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1669 pattern);
1670 if (ctx->buf == NULL) {
1671 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1672 g_free(ctx);
1673 return 0;
1674 }
1675
1676 gettimeofday(&ctx->t1, NULL);
1677 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1678 BLOCK_ACCT_WRITE);
1679
Eric Blake7b3f9712016-05-06 10:26:44 -06001680 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, 0, aio_write_done, ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001681 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001682 return 0;
1683}
1684
Max Reitz4c7b7e92015-02-05 13:58:22 -05001685static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001686{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001687 BlockAcctCookie cookie;
1688 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001689 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001690 block_acct_done(blk_get_stats(blk), &cookie);
Kevin Wolf797ac582013-06-05 14:19:31 +02001691 return 0;
1692}
1693
1694static const cmdinfo_t aio_flush_cmd = {
1695 .name = "aio_flush",
1696 .cfunc = aio_flush_f,
1697 .oneline = "completes all outstanding aio requests"
1698};
1699
Max Reitz4c7b7e92015-02-05 13:58:22 -05001700static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001701{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001702 blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001703 return 0;
1704}
1705
1706static const cmdinfo_t flush_cmd = {
1707 .name = "flush",
1708 .altname = "f",
1709 .cfunc = flush_f,
1710 .oneline = "flush all in-core file state to disk",
1711};
1712
Max Reitz4c7b7e92015-02-05 13:58:22 -05001713static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001714{
1715 int64_t offset;
1716 int ret;
1717
1718 offset = cvtnum(argv[1]);
1719 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001720 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001721 return 0;
1722 }
1723
Max Reitz4c7b7e92015-02-05 13:58:22 -05001724 ret = blk_truncate(blk, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02001725 if (ret < 0) {
1726 printf("truncate: %s\n", strerror(-ret));
1727 return 0;
1728 }
1729
1730 return 0;
1731}
1732
1733static const cmdinfo_t truncate_cmd = {
1734 .name = "truncate",
1735 .altname = "t",
1736 .cfunc = truncate_f,
1737 .argmin = 1,
1738 .argmax = 1,
1739 .args = "off",
1740 .oneline = "truncates the current file at the given offset",
1741};
1742
Max Reitz4c7b7e92015-02-05 13:58:22 -05001743static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001744{
1745 int64_t size;
1746 char s1[64];
1747
Max Reitz4c7b7e92015-02-05 13:58:22 -05001748 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001749 if (size < 0) {
1750 printf("getlength: %s\n", strerror(-size));
1751 return 0;
1752 }
1753
1754 cvtstr(size, s1, sizeof(s1));
1755 printf("%s\n", s1);
1756 return 0;
1757}
1758
1759
1760static const cmdinfo_t length_cmd = {
1761 .name = "length",
1762 .altname = "l",
1763 .cfunc = length_f,
1764 .oneline = "gets the length of the current file",
1765};
1766
1767
Max Reitz4c7b7e92015-02-05 13:58:22 -05001768static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001769{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001770 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001771 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001772 ImageInfoSpecific *spec_info;
Kevin Wolf797ac582013-06-05 14:19:31 +02001773 char s1[64], s2[64];
1774 int ret;
1775
1776 if (bs->drv && bs->drv->format_name) {
1777 printf("format name: %s\n", bs->drv->format_name);
1778 }
1779 if (bs->drv && bs->drv->protocol_name) {
1780 printf("format name: %s\n", bs->drv->protocol_name);
1781 }
1782
1783 ret = bdrv_get_info(bs, &bdi);
1784 if (ret) {
1785 return 0;
1786 }
1787
1788 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1789 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1790
1791 printf("cluster size: %s\n", s1);
1792 printf("vm state offset: %s\n", s2);
1793
Max Reitza8d8ecb2013-10-09 10:46:17 +02001794 spec_info = bdrv_get_specific_info(bs);
1795 if (spec_info) {
1796 printf("Format specific information:\n");
1797 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1798 qapi_free_ImageInfoSpecific(spec_info);
1799 }
1800
Kevin Wolf797ac582013-06-05 14:19:31 +02001801 return 0;
1802}
1803
1804
1805
1806static const cmdinfo_t info_cmd = {
1807 .name = "info",
1808 .altname = "i",
1809 .cfunc = info_f,
1810 .oneline = "prints information about the current file",
1811};
1812
1813static void discard_help(void)
1814{
1815 printf(
1816"\n"
1817" discards a range of bytes from the given offset\n"
1818"\n"
1819" Example:\n"
1820" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1821"\n"
1822" Discards a segment of the currently open file.\n"
1823" -C, -- report statistics in a machine parsable format\n"
1824" -q, -- quiet mode, do not show I/O statistics\n"
1825"\n");
1826}
1827
Max Reitz4c7b7e92015-02-05 13:58:22 -05001828static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001829
1830static const cmdinfo_t discard_cmd = {
1831 .name = "discard",
1832 .altname = "d",
1833 .cfunc = discard_f,
1834 .argmin = 2,
1835 .argmax = -1,
1836 .args = "[-Cq] off len",
1837 .oneline = "discards a number of bytes at a specified offset",
1838 .help = discard_help,
1839};
1840
Max Reitz4c7b7e92015-02-05 13:58:22 -05001841static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001842{
1843 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001844 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001845 int c, ret;
John Snow9b0beaf2015-11-05 18:53:02 -05001846 int64_t offset, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001847
Eric Blakeb062ad82015-05-12 09:10:56 -06001848 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001849 switch (c) {
1850 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001851 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001852 break;
1853 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001854 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001855 break;
1856 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001857 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001858 }
1859 }
1860
1861 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001862 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001863 }
1864
1865 offset = cvtnum(argv[optind]);
1866 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001867 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001868 return 0;
1869 }
1870
1871 optind++;
1872 count = cvtnum(argv[optind]);
1873 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001874 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001875 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001876 } else if (count >> BDRV_SECTOR_BITS > INT_MAX) {
1877 printf("length cannot exceed %"PRIu64", given %s\n",
1878 (uint64_t)INT_MAX << BDRV_SECTOR_BITS,
1879 argv[optind]);
1880 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001881 }
1882
1883 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001884 ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1885 count >> BDRV_SECTOR_BITS);
Kevin Wolf797ac582013-06-05 14:19:31 +02001886 gettimeofday(&t2, NULL);
1887
1888 if (ret < 0) {
1889 printf("discard failed: %s\n", strerror(-ret));
1890 goto out;
1891 }
1892
1893 /* Finally, report back -- -C gives a parsable format */
1894 if (!qflag) {
1895 t2 = tsub(t2, t1);
1896 print_report("discard", &t2, offset, count, count, 1, Cflag);
1897 }
1898
1899out:
1900 return 0;
1901}
1902
Max Reitz4c7b7e92015-02-05 13:58:22 -05001903static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001904{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001905 BlockDriverState *bs = blk_bs(blk);
John Snow9b0beaf2015-11-05 18:53:02 -05001906 int64_t offset, sector_num, nb_sectors, remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001907 char s1[64];
John Snow9b0beaf2015-11-05 18:53:02 -05001908 int num, ret;
1909 int64_t sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001910
1911 offset = cvtnum(argv[1]);
1912 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001913 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001914 return 0;
1915 } else if (offset & 0x1ff) {
1916 printf("offset %" PRId64 " is not sector aligned\n",
1917 offset);
1918 return 0;
1919 }
1920
1921 if (argc == 3) {
1922 nb_sectors = cvtnum(argv[2]);
1923 if (nb_sectors < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001924 print_cvtnum_err(nb_sectors, argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001925 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001926 } else if (nb_sectors > INT_MAX) {
1927 printf("length argument cannot exceed %d, given %s\n",
1928 INT_MAX, argv[2]);
1929 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001930 }
1931 } else {
1932 nb_sectors = 1;
1933 }
1934
1935 remaining = nb_sectors;
1936 sum_alloc = 0;
1937 sector_num = offset >> 9;
1938 while (remaining) {
1939 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001940 if (ret < 0) {
1941 printf("is_allocated failed: %s\n", strerror(-ret));
1942 return 0;
1943 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001944 sector_num += num;
1945 remaining -= num;
1946 if (ret) {
1947 sum_alloc += num;
1948 }
1949 if (num == 0) {
1950 nb_sectors -= remaining;
1951 remaining = 0;
1952 }
1953 }
1954
1955 cvtstr(offset, s1, sizeof(s1));
1956
John Snow9b0beaf2015-11-05 18:53:02 -05001957 printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001958 sum_alloc, nb_sectors, s1);
1959 return 0;
1960}
1961
1962static const cmdinfo_t alloc_cmd = {
1963 .name = "alloc",
1964 .altname = "a",
1965 .argmin = 1,
1966 .argmax = 2,
1967 .cfunc = alloc_f,
1968 .args = "off [sectors]",
1969 .oneline = "checks if a sector is present in the file",
1970};
1971
1972
1973static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1974 int64_t nb_sectors, int64_t *pnum)
1975{
1976 int num, num_checked;
1977 int ret, firstret;
1978
1979 num_checked = MIN(nb_sectors, INT_MAX);
1980 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1981 if (ret < 0) {
1982 return ret;
1983 }
1984
1985 firstret = ret;
1986 *pnum = num;
1987
1988 while (nb_sectors > 0 && ret == firstret) {
1989 sector_num += num;
1990 nb_sectors -= num;
1991
1992 num_checked = MIN(nb_sectors, INT_MAX);
1993 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02001994 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001995 *pnum += num;
1996 } else {
1997 break;
1998 }
1999 }
2000
2001 return firstret;
2002}
2003
Max Reitz4c7b7e92015-02-05 13:58:22 -05002004static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002005{
2006 int64_t offset;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002007 int64_t nb_sectors, total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02002008 char s1[64];
2009 int64_t num;
2010 int ret;
2011 const char *retstr;
2012
2013 offset = 0;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002014 total_sectors = blk_nb_sectors(blk);
2015 if (total_sectors < 0) {
2016 error_report("Failed to query image length: %s",
2017 strerror(-total_sectors));
2018 return 0;
2019 }
2020
2021 nb_sectors = total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02002022
2023 do {
Max Reitz4c7b7e92015-02-05 13:58:22 -05002024 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002025 if (ret < 0) {
2026 error_report("Failed to get allocation status: %s", strerror(-ret));
2027 return 0;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002028 } else if (!num) {
2029 error_report("Unexpected end of image");
2030 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002031 }
2032
2033 retstr = ret ? " allocated" : "not allocated";
2034 cvtstr(offset << 9ULL, s1, sizeof(s1));
2035 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
2036 "at offset %s (%d)\n",
2037 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
2038
2039 offset += num;
2040 nb_sectors -= num;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002041 } while (offset < total_sectors);
Kevin Wolf797ac582013-06-05 14:19:31 +02002042
2043 return 0;
2044}
2045
2046static const cmdinfo_t map_cmd = {
2047 .name = "map",
2048 .argmin = 0,
2049 .argmax = 0,
2050 .cfunc = map_f,
2051 .args = "",
2052 .oneline = "prints the allocated areas of a file",
2053};
2054
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002055static void reopen_help(void)
2056{
2057 printf(
2058"\n"
2059" Changes the open options of an already opened image\n"
2060"\n"
2061" Example:\n"
2062" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2063"\n"
2064" -r, -- Reopen the image read-only\n"
2065" -c, -- Change the cache mode to the given value\n"
2066" -o, -- Changes block driver options (cf. 'open' command)\n"
2067"\n");
2068}
2069
2070static int reopen_f(BlockBackend *blk, int argc, char **argv);
2071
2072static QemuOptsList reopen_opts = {
2073 .name = "reopen",
2074 .merge_lists = true,
2075 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2076 .desc = {
2077 /* no elements => accept any params */
2078 { /* end of list */ }
2079 },
2080};
2081
2082static const cmdinfo_t reopen_cmd = {
2083 .name = "reopen",
2084 .argmin = 0,
2085 .argmax = -1,
2086 .cfunc = reopen_f,
2087 .args = "[-r] [-c cache] [-o options]",
2088 .oneline = "reopens an image with new options",
2089 .help = reopen_help,
2090};
2091
2092static int reopen_f(BlockBackend *blk, int argc, char **argv)
2093{
2094 BlockDriverState *bs = blk_bs(blk);
2095 QemuOpts *qopts;
2096 QDict *opts;
2097 int c;
2098 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002099 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002100
2101 BlockReopenQueue *brq;
2102 Error *local_err = NULL;
2103
2104 while ((c = getopt(argc, argv, "c:o:r")) != -1) {
2105 switch (c) {
2106 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002107 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002108 error_report("Invalid cache option: %s", optarg);
2109 return 0;
2110 }
2111 break;
2112 case 'o':
2113 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2114 qemu_opts_reset(&reopen_opts);
2115 return 0;
2116 }
2117 break;
2118 case 'r':
2119 flags &= ~BDRV_O_RDWR;
2120 break;
2121 default:
2122 qemu_opts_reset(&reopen_opts);
2123 return qemuio_command_usage(&reopen_cmd);
2124 }
2125 }
2126
2127 if (optind != argc) {
2128 qemu_opts_reset(&reopen_opts);
2129 return qemuio_command_usage(&reopen_cmd);
2130 }
2131
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002132 if (writethrough != blk_enable_write_cache(blk) &&
2133 blk_get_attached_dev(blk))
2134 {
2135 error_report("Cannot change cache.writeback: Device attached");
2136 qemu_opts_reset(&reopen_opts);
2137 return 0;
2138 }
2139
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002140 qopts = qemu_opts_find(&reopen_opts, NULL);
2141 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
2142 qemu_opts_reset(&reopen_opts);
2143
2144 brq = bdrv_reopen_queue(NULL, bs, opts, flags);
2145 bdrv_reopen_multiple(brq, &local_err);
2146 if (local_err) {
2147 error_report_err(local_err);
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002148 } else {
2149 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002150 }
2151
2152 return 0;
2153}
2154
Max Reitz4c7b7e92015-02-05 13:58:22 -05002155static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002156{
2157 int ret;
2158
Max Reitz4c7b7e92015-02-05 13:58:22 -05002159 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002160 if (ret < 0) {
2161 printf("Could not set breakpoint: %s\n", strerror(-ret));
2162 }
2163
2164 return 0;
2165}
2166
Max Reitz4c7b7e92015-02-05 13:58:22 -05002167static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002168{
2169 int ret;
2170
Max Reitz4c7b7e92015-02-05 13:58:22 -05002171 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002172 if (ret < 0) {
2173 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2174 }
2175
2176 return 0;
2177}
2178
Kevin Wolf797ac582013-06-05 14:19:31 +02002179static const cmdinfo_t break_cmd = {
2180 .name = "break",
2181 .argmin = 2,
2182 .argmax = 2,
2183 .cfunc = break_f,
2184 .args = "event tag",
2185 .oneline = "sets a breakpoint on event and tags the stopped "
2186 "request as tag",
2187};
2188
Fam Zheng4cc70e92013-11-20 10:01:54 +08002189static const cmdinfo_t remove_break_cmd = {
2190 .name = "remove_break",
2191 .argmin = 1,
2192 .argmax = 1,
2193 .cfunc = remove_break_f,
2194 .args = "tag",
2195 .oneline = "remove a breakpoint by tag",
2196};
2197
Max Reitz4c7b7e92015-02-05 13:58:22 -05002198static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002199{
2200 int ret;
2201
Max Reitz4c7b7e92015-02-05 13:58:22 -05002202 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002203 if (ret < 0) {
2204 printf("Could not resume request: %s\n", strerror(-ret));
2205 }
2206
2207 return 0;
2208}
2209
2210static const cmdinfo_t resume_cmd = {
2211 .name = "resume",
2212 .argmin = 1,
2213 .argmax = 1,
2214 .cfunc = resume_f,
2215 .args = "tag",
2216 .oneline = "resumes the request tagged as tag",
2217};
2218
Max Reitz4c7b7e92015-02-05 13:58:22 -05002219static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002220{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002221 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2222 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002223 }
2224
2225 return 0;
2226}
2227
2228static const cmdinfo_t wait_break_cmd = {
2229 .name = "wait_break",
2230 .argmin = 1,
2231 .argmax = 1,
2232 .cfunc = wait_break_f,
2233 .args = "tag",
2234 .oneline = "waits for the suspension of a request",
2235};
2236
Max Reitz4c7b7e92015-02-05 13:58:22 -05002237static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002238{
2239 abort();
2240}
2241
2242static const cmdinfo_t abort_cmd = {
2243 .name = "abort",
2244 .cfunc = abort_f,
2245 .flags = CMD_NOFILE_OK,
2246 .oneline = "simulate a program crash using abort(3)",
2247};
2248
Max Reitz0e82dc72014-12-08 10:48:10 +01002249static void sigraise_help(void)
2250{
2251 printf(
2252"\n"
2253" raises the given signal\n"
2254"\n"
2255" Example:\n"
2256" 'sigraise %i' - raises SIGTERM\n"
2257"\n"
2258" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2259" given to sigraise.\n"
2260"\n", SIGTERM);
2261}
2262
Max Reitz4c7b7e92015-02-05 13:58:22 -05002263static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002264
2265static const cmdinfo_t sigraise_cmd = {
2266 .name = "sigraise",
2267 .cfunc = sigraise_f,
2268 .argmin = 1,
2269 .argmax = 1,
2270 .flags = CMD_NOFILE_OK,
2271 .args = "signal",
2272 .oneline = "raises a signal",
2273 .help = sigraise_help,
2274};
2275
Max Reitz4c7b7e92015-02-05 13:58:22 -05002276static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002277{
John Snow9b0beaf2015-11-05 18:53:02 -05002278 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002279 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002280 print_cvtnum_err(sig, argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002281 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05002282 } else if (sig > NSIG) {
2283 printf("signal argument '%s' is too large to be a valid signal\n",
2284 argv[1]);
2285 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002286 }
2287
2288 /* Using raise() to kill this process does not necessarily flush all open
2289 * streams. At least stdout and stderr (although the latter should be
2290 * non-buffered anyway) should be flushed, though. */
2291 fflush(stdout);
2292 fflush(stderr);
2293
2294 raise(sig);
2295 return 0;
2296}
2297
Kevin Wolfcd33d022014-01-15 15:39:10 +01002298static void sleep_cb(void *opaque)
2299{
2300 bool *expired = opaque;
2301 *expired = true;
2302}
2303
Max Reitz4c7b7e92015-02-05 13:58:22 -05002304static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002305{
2306 char *endptr;
2307 long ms;
2308 struct QEMUTimer *timer;
2309 bool expired = false;
2310
2311 ms = strtol(argv[1], &endptr, 0);
2312 if (ms < 0 || *endptr != '\0') {
2313 printf("%s is not a valid number\n", argv[1]);
2314 return 0;
2315 }
2316
2317 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2318 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2319
2320 while (!expired) {
2321 main_loop_wait(false);
2322 }
2323
2324 timer_free(timer);
2325
2326 return 0;
2327}
2328
2329static const cmdinfo_t sleep_cmd = {
2330 .name = "sleep",
2331 .argmin = 1,
2332 .argmax = 1,
2333 .cfunc = sleep_f,
2334 .flags = CMD_NOFILE_OK,
2335 .oneline = "waits for the given value in milliseconds",
2336};
2337
Kevin Wolff18a8342013-06-05 14:19:33 +02002338static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2339{
2340 if (cmd) {
2341 printf("%s ", cmd);
2342 } else {
2343 printf("%s ", ct->name);
2344 if (ct->altname) {
2345 printf("(or %s) ", ct->altname);
2346 }
2347 }
2348
2349 if (ct->args) {
2350 printf("%s ", ct->args);
2351 }
2352 printf("-- %s\n", ct->oneline);
2353}
2354
2355static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2356{
2357 help_oneline(cmd, ct);
2358 if (ct->help) {
2359 ct->help();
2360 }
2361}
2362
2363static void help_all(void)
2364{
2365 const cmdinfo_t *ct;
2366
2367 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2368 help_oneline(ct->name, ct);
2369 }
2370 printf("\nUse 'help commandname' for extended help.\n");
2371}
2372
Max Reitz4c7b7e92015-02-05 13:58:22 -05002373static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002374{
2375 const cmdinfo_t *ct;
2376
2377 if (argc == 1) {
2378 help_all();
2379 return 0;
2380 }
2381
2382 ct = find_command(argv[1]);
2383 if (ct == NULL) {
2384 printf("command %s not found\n", argv[1]);
2385 return 0;
2386 }
2387
2388 help_onecmd(argv[1], ct);
2389 return 0;
2390}
2391
2392static const cmdinfo_t help_cmd = {
2393 .name = "help",
2394 .altname = "?",
2395 .cfunc = help_f,
2396 .argmin = 0,
2397 .argmax = 1,
2398 .flags = CMD_FLAG_GLOBAL,
2399 .args = "[command]",
2400 .oneline = "help for one or all commands",
2401};
2402
Max Reitz4c7b7e92015-02-05 13:58:22 -05002403bool qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002404{
2405 char *input;
2406 const cmdinfo_t *ct;
2407 char **v;
2408 int c;
2409 bool done = false;
2410
2411 input = g_strdup(cmd);
2412 v = breakline(input, &c);
2413 if (c) {
2414 ct = find_command(v[0]);
2415 if (ct) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05002416 done = command(blk, ct, c, v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002417 } else {
2418 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2419 }
2420 }
2421 g_free(input);
2422 g_free(v);
2423
2424 return done;
2425}
2426
Kevin Wolf797ac582013-06-05 14:19:31 +02002427static void __attribute((constructor)) init_qemuio_commands(void)
2428{
2429 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002430 qemuio_add_command(&help_cmd);
2431 qemuio_add_command(&read_cmd);
2432 qemuio_add_command(&readv_cmd);
2433 qemuio_add_command(&write_cmd);
2434 qemuio_add_command(&writev_cmd);
2435 qemuio_add_command(&multiwrite_cmd);
2436 qemuio_add_command(&aio_read_cmd);
2437 qemuio_add_command(&aio_write_cmd);
2438 qemuio_add_command(&aio_flush_cmd);
2439 qemuio_add_command(&flush_cmd);
2440 qemuio_add_command(&truncate_cmd);
2441 qemuio_add_command(&length_cmd);
2442 qemuio_add_command(&info_cmd);
2443 qemuio_add_command(&discard_cmd);
2444 qemuio_add_command(&alloc_cmd);
2445 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002446 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002447 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002448 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002449 qemuio_add_command(&resume_cmd);
2450 qemuio_add_command(&wait_break_cmd);
2451 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002452 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002453 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002454}