blob: 4a00bc604dfd1c6eea58eeace395d6c11dd0d529 [file] [log] [blame]
Kevin Wolf797ac582013-06-05 14:19:31 +02001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
Eric Blake093ea232016-05-07 21:16:43 -06004 * Copyright (C) 2009-2016 Red Hat, Inc.
Kevin Wolf797ac582013-06-05 14:19:31 +02005 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010012#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020013#include "qemu-io.h"
Max Reitz4c7b7e92015-02-05 13:58:22 -050014#include "sysemu/block-backend.h"
15#include "block/block.h"
16#include "block/block_int.h" /* for info_f() */
Max Reitza8d8ecb2013-10-09 10:46:17 +020017#include "block/qapi.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010018#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010019#include "qemu/main-loop.h"
Kevin Wolfcd33d022014-01-15 15:39:10 +010020#include "qemu/timer.h"
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
Kevin Wolf797ac582013-06-05 14:19:31 +0200398 sizes[i] = len;
399 count += len;
400 }
401
402 qemu_iovec_init(qiov, nr_iov);
403
Max Reitz4c7b7e92015-02-05 13:58:22 -0500404 buf = p = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +0200405
406 for (i = 0; i < nr_iov; i++) {
407 qemu_iovec_add(qiov, p, sizes[i]);
408 p += sizes[i];
409 }
410
411fail:
412 g_free(sizes);
413 return buf;
414}
415
John Snow9b0beaf2015-11-05 18:53:02 -0500416static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
417 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200418{
John Snow9b0beaf2015-11-05 18:53:02 -0500419 if (count > INT_MAX) {
420 return -ERANGE;
421 }
422
Max Reitz4c7b7e92015-02-05 13:58:22 -0500423 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200424 if (*total < 0) {
425 return *total;
426 }
427 return 1;
428}
429
John Snow9b0beaf2015-11-05 18:53:02 -0500430static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
Eric Blake770e0e02016-05-07 21:16:44 -0600431 int64_t count, int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200432{
John Snow9b0beaf2015-11-05 18:53:02 -0500433 if (count > INT_MAX) {
434 return -ERANGE;
435 }
436
Eric Blake770e0e02016-05-07 21:16:44 -0600437 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count, flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200438 if (*total < 0) {
439 return *total;
440 }
441 return 1;
442}
443
444typedef struct {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500445 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +0200446 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500447 int64_t count;
448 int64_t *total;
Eric Blake770e0e02016-05-07 21:16:44 -0600449 int flags;
Kevin Wolf797ac582013-06-05 14:19:31 +0200450 int ret;
451 bool done;
452} CoWriteZeroes;
453
454static void coroutine_fn co_write_zeroes_entry(void *opaque)
455{
456 CoWriteZeroes *data = opaque;
457
Eric Blake770e0e02016-05-07 21:16:44 -0600458 data->ret = blk_co_write_zeroes(data->blk, data->offset, data->count,
459 data->flags);
Kevin Wolf797ac582013-06-05 14:19:31 +0200460 data->done = true;
461 if (data->ret < 0) {
462 *data->total = data->ret;
463 return;
464 }
465
466 *data->total = data->count;
467}
468
John Snow9b0beaf2015-11-05 18:53:02 -0500469static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int64_t count,
Eric Blake770e0e02016-05-07 21:16:44 -0600470 int flags, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200471{
472 Coroutine *co;
473 CoWriteZeroes data = {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500474 .blk = blk,
Kevin Wolf797ac582013-06-05 14:19:31 +0200475 .offset = offset,
476 .count = count,
477 .total = total,
Eric Blake770e0e02016-05-07 21:16:44 -0600478 .flags = flags,
Kevin Wolf797ac582013-06-05 14:19:31 +0200479 .done = false,
480 };
481
John Snow9b0beaf2015-11-05 18:53:02 -0500482 if (count >> BDRV_SECTOR_BITS > INT_MAX) {
483 return -ERANGE;
484 }
485
Kevin Wolf797ac582013-06-05 14:19:31 +0200486 co = qemu_coroutine_create(co_write_zeroes_entry);
487 qemu_coroutine_enter(co, &data);
488 while (!data.done) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500489 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +0200490 }
491 if (data.ret < 0) {
492 return data.ret;
493 } else {
494 return 1;
495 }
496}
497
Max Reitz4c7b7e92015-02-05 13:58:22 -0500498static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500499 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200500{
501 int ret;
502
John Snow9b0beaf2015-11-05 18:53:02 -0500503 if (count >> 9 > INT_MAX) {
504 return -ERANGE;
505 }
506
Max Reitz4c7b7e92015-02-05 13:58:22 -0500507 ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
Kevin Wolf797ac582013-06-05 14:19:31 +0200508 if (ret < 0) {
509 return ret;
510 }
511 *total = count;
512 return 1;
513}
514
Max Reitz4c7b7e92015-02-05 13:58:22 -0500515static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500516 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200517{
John Snow9b0beaf2015-11-05 18:53:02 -0500518 if (count > INT_MAX) {
519 return -ERANGE;
520 }
521
Max Reitz4c7b7e92015-02-05 13:58:22 -0500522 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200523 if (*total < 0) {
524 return *total;
525 }
526 return 1;
527}
528
Max Reitz4c7b7e92015-02-05 13:58:22 -0500529static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
John Snow9b0beaf2015-11-05 18:53:02 -0500530 int64_t count, int64_t *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200531{
John Snow9b0beaf2015-11-05 18:53:02 -0500532 if (count > INT_MAX) {
533 return -ERANGE;
534 }
535
Max Reitz4c7b7e92015-02-05 13:58:22 -0500536 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
Kevin Wolf797ac582013-06-05 14:19:31 +0200537 if (*total < 0) {
538 return *total;
539 }
540 return 1;
541}
542
543#define NOT_DONE 0x7fffffff
544static void aio_rw_done(void *opaque, int ret)
545{
546 *(int *)opaque = ret;
547}
548
Max Reitz4c7b7e92015-02-05 13:58:22 -0500549static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
Kevin Wolf797ac582013-06-05 14:19:31 +0200550 int64_t offset, int *total)
551{
552 int async_ret = NOT_DONE;
553
Eric Blake7b3f9712016-05-06 10:26:44 -0600554 blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200555 while (async_ret == NOT_DONE) {
556 main_loop_wait(false);
557 }
558
559 *total = qiov->size;
560 return async_ret < 0 ? async_ret : 1;
561}
562
Max Reitz4c7b7e92015-02-05 13:58:22 -0500563static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
Eric Blake770e0e02016-05-07 21:16:44 -0600564 int64_t offset, int flags, int *total)
Kevin Wolf797ac582013-06-05 14:19:31 +0200565{
566 int async_ret = NOT_DONE;
567
Eric Blake770e0e02016-05-07 21:16:44 -0600568 blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
Kevin Wolf797ac582013-06-05 14:19:31 +0200569 while (async_ret == NOT_DONE) {
570 main_loop_wait(false);
571 }
572
573 *total = qiov->size;
574 return async_ret < 0 ? async_ret : 1;
575}
576
577struct multiwrite_async_ret {
578 int num_done;
579 int error;
580};
581
582static void multiwrite_cb(void *opaque, int ret)
583{
584 struct multiwrite_async_ret *async_ret = opaque;
585
586 async_ret->num_done++;
587 if (ret < 0) {
588 async_ret->error = ret;
589 }
590}
591
Max Reitz4c7b7e92015-02-05 13:58:22 -0500592static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
Kevin Wolf797ac582013-06-05 14:19:31 +0200593 int num_reqs, int *total)
594{
595 int i, ret;
596 struct multiwrite_async_ret async_ret = {
597 .num_done = 0,
598 .error = 0,
599 };
600
601 *total = 0;
602 for (i = 0; i < num_reqs; i++) {
603 reqs[i].cb = multiwrite_cb;
604 reqs[i].opaque = &async_ret;
605 *total += reqs[i].qiov->size;
606 }
607
Max Reitz4c7b7e92015-02-05 13:58:22 -0500608 ret = blk_aio_multiwrite(blk, reqs, num_reqs);
Kevin Wolf797ac582013-06-05 14:19:31 +0200609 if (ret < 0) {
610 return ret;
611 }
612
613 while (async_ret.num_done < num_reqs) {
614 main_loop_wait(false);
615 }
616
617 return async_ret.error < 0 ? async_ret.error : 1;
618}
619
620static void read_help(void)
621{
622 printf(
623"\n"
624" reads a range of bytes from the given offset\n"
625"\n"
626" Example:\n"
627" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
628"\n"
629" Reads a segment of the currently open file, optionally dumping it to the\n"
630" standard output stream (with -v option) for subsequent inspection.\n"
631" -b, -- read from the VM state rather than the virtual disk\n"
632" -C, -- report statistics in a machine parsable format\n"
633" -l, -- length for pattern verification (only with -P)\n"
Eric Blake093ea232016-05-07 21:16:43 -0600634" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200635" -P, -- use a pattern to verify read data\n"
636" -q, -- quiet mode, do not show I/O statistics\n"
637" -s, -- start offset for pattern verification (only with -P)\n"
638" -v, -- dump buffer to standard output\n"
639"\n");
640}
641
Max Reitz4c7b7e92015-02-05 13:58:22 -0500642static int read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200643
644static const cmdinfo_t read_cmd = {
645 .name = "read",
646 .altname = "r",
647 .cfunc = read_f,
648 .argmin = 2,
649 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600650 .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200651 .oneline = "reads a number of bytes at a specified offset",
652 .help = read_help,
653};
654
Max Reitz4c7b7e92015-02-05 13:58:22 -0500655static int read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200656{
657 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600658 bool Cflag = false, qflag = false, vflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600659 bool Pflag = false, sflag = false, lflag = false, bflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200660 int c, cnt;
661 char *buf;
662 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500663 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200664 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500665 int64_t total = 0;
666 int pattern = 0;
667 int64_t pattern_offset = 0, pattern_count = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200668
Eric Blakeb062ad82015-05-12 09:10:56 -0600669 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200670 switch (c) {
671 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600672 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200673 break;
674 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600675 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200676 break;
677 case 'l':
Eric Blakedc388522016-05-07 21:16:42 -0600678 lflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200679 pattern_count = cvtnum(optarg);
680 if (pattern_count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500681 print_cvtnum_err(pattern_count, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200682 return 0;
683 }
684 break;
685 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600686 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200687 break;
688 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600689 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200690 pattern = parse_pattern(optarg);
691 if (pattern < 0) {
692 return 0;
693 }
694 break;
695 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600696 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200697 break;
698 case 's':
Eric Blakedc388522016-05-07 21:16:42 -0600699 sflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200700 pattern_offset = cvtnum(optarg);
701 if (pattern_offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500702 print_cvtnum_err(pattern_offset, optarg);
Kevin Wolf797ac582013-06-05 14:19:31 +0200703 return 0;
704 }
705 break;
706 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600707 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200708 break;
709 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200710 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200711 }
712 }
713
714 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200715 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200716 }
717
Kevin Wolf797ac582013-06-05 14:19:31 +0200718 offset = cvtnum(argv[optind]);
719 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500720 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200721 return 0;
722 }
723
724 optind++;
725 count = cvtnum(argv[optind]);
726 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500727 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200728 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -0500729 } else if (count > SIZE_MAX) {
730 printf("length cannot exceed %" PRIu64 ", given %s\n",
731 (uint64_t) SIZE_MAX, argv[optind]);
732 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200733 }
734
735 if (!Pflag && (lflag || sflag)) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200736 return qemuio_command_usage(&read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200737 }
738
739 if (!lflag) {
740 pattern_count = count - pattern_offset;
741 }
742
743 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
744 printf("pattern verification range exceeds end of read data\n");
745 return 0;
746 }
747
Eric Blake093ea232016-05-07 21:16:43 -0600748 if (bflag) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200749 if (offset & 0x1ff) {
750 printf("offset %" PRId64 " is not sector aligned\n",
751 offset);
752 return 0;
753 }
754 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -0500755 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200756 count);
757 return 0;
758 }
759 }
760
Max Reitz4c7b7e92015-02-05 13:58:22 -0500761 buf = qemu_io_alloc(blk, count, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200762
763 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -0600764 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500765 cnt = do_load_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200766 } else {
Eric Blake7b3f9712016-05-06 10:26:44 -0600767 cnt = do_pread(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200768 }
769 gettimeofday(&t2, NULL);
770
771 if (cnt < 0) {
772 printf("read failed: %s\n", strerror(-cnt));
773 goto out;
774 }
775
776 if (Pflag) {
777 void *cmp_buf = g_malloc(pattern_count);
778 memset(cmp_buf, pattern, pattern_count);
779 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
780 printf("Pattern verification failed at offset %"
John Snow9b0beaf2015-11-05 18:53:02 -0500781 PRId64 ", %"PRId64" bytes\n",
Kevin Wolf797ac582013-06-05 14:19:31 +0200782 offset + pattern_offset, pattern_count);
783 }
784 g_free(cmp_buf);
785 }
786
787 if (qflag) {
788 goto out;
789 }
790
791 if (vflag) {
792 dump_buffer(buf, offset, count);
793 }
794
795 /* Finally, report back -- -C gives a parsable format */
796 t2 = tsub(t2, t1);
797 print_report("read", &t2, offset, count, total, cnt, Cflag);
798
799out:
800 qemu_io_free(buf);
801
802 return 0;
803}
804
805static void readv_help(void)
806{
807 printf(
808"\n"
809" reads a range of bytes from the given offset into multiple buffers\n"
810"\n"
811" Example:\n"
812" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
813"\n"
814" Reads a segment of the currently open file, optionally dumping it to the\n"
815" standard output stream (with -v option) for subsequent inspection.\n"
816" Uses multiple iovec buffers if more than one byte range is specified.\n"
817" -C, -- report statistics in a machine parsable format\n"
818" -P, -- use a pattern to verify read data\n"
819" -v, -- dump buffer to standard output\n"
820" -q, -- quiet mode, do not show I/O statistics\n"
821"\n");
822}
823
Max Reitz4c7b7e92015-02-05 13:58:22 -0500824static int readv_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200825
826static const cmdinfo_t readv_cmd = {
827 .name = "readv",
828 .cfunc = readv_f,
829 .argmin = 2,
830 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -0600831 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +0200832 .oneline = "reads a number of bytes at a specified offset",
833 .help = readv_help,
834};
835
Max Reitz4c7b7e92015-02-05 13:58:22 -0500836static int readv_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200837{
838 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -0600839 bool Cflag = false, qflag = false, vflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200840 int c, cnt;
841 char *buf;
842 int64_t offset;
843 /* Some compilers get confused and warn if this is not initialized. */
844 int total = 0;
845 int nr_iov;
846 QEMUIOVector qiov;
847 int pattern = 0;
Eric Blakedc388522016-05-07 21:16:42 -0600848 bool Pflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +0200849
Eric Blakeb062ad82015-05-12 09:10:56 -0600850 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200851 switch (c) {
852 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600853 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200854 break;
855 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600856 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200857 pattern = parse_pattern(optarg);
858 if (pattern < 0) {
859 return 0;
860 }
861 break;
862 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -0600863 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200864 break;
865 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -0600866 vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200867 break;
868 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200869 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200870 }
871 }
872
873 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200874 return qemuio_command_usage(&readv_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +0200875 }
876
877
878 offset = cvtnum(argv[optind]);
879 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -0500880 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +0200881 return 0;
882 }
883 optind++;
884
Kevin Wolf797ac582013-06-05 14:19:31 +0200885 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -0500886 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +0200887 if (buf == NULL) {
888 return 0;
889 }
890
891 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -0500892 cnt = do_aio_readv(blk, &qiov, offset, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +0200893 gettimeofday(&t2, NULL);
894
895 if (cnt < 0) {
896 printf("readv failed: %s\n", strerror(-cnt));
897 goto out;
898 }
899
900 if (Pflag) {
901 void *cmp_buf = g_malloc(qiov.size);
902 memset(cmp_buf, pattern, qiov.size);
903 if (memcmp(buf, cmp_buf, qiov.size)) {
904 printf("Pattern verification failed at offset %"
905 PRId64 ", %zd bytes\n", offset, qiov.size);
906 }
907 g_free(cmp_buf);
908 }
909
910 if (qflag) {
911 goto out;
912 }
913
914 if (vflag) {
915 dump_buffer(buf, offset, qiov.size);
916 }
917
918 /* Finally, report back -- -C gives a parsable format */
919 t2 = tsub(t2, t1);
920 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
921
922out:
923 qemu_iovec_destroy(&qiov);
924 qemu_io_free(buf);
925 return 0;
926}
927
928static void write_help(void)
929{
930 printf(
931"\n"
932" writes a range of bytes from the given offset\n"
933"\n"
934" Example:\n"
935" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
936"\n"
937" Writes into a segment of the currently open file, using a buffer\n"
938" filled with a set pattern (0xcdcdcdcd).\n"
939" -b, -- write to the VM state rather than the virtual disk\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500940" -c, -- write compressed data with blk_write_compressed\n"
Eric Blake770e0e02016-05-07 21:16:44 -0600941" -f, -- use Force Unit Access semantics\n"
Eric Blake093ea232016-05-07 21:16:43 -0600942" -p, -- ignored for backwards compatibility\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200943" -P, -- use different pattern to fill file\n"
944" -C, -- report statistics in a machine parsable format\n"
945" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -0600946" -u, -- with -z, allow unmapping\n"
Max Reitz4c7b7e92015-02-05 13:58:22 -0500947" -z, -- write zeroes using blk_co_write_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +0200948"\n");
949}
950
Max Reitz4c7b7e92015-02-05 13:58:22 -0500951static int write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +0200952
953static const cmdinfo_t write_cmd = {
954 .name = "write",
955 .altname = "w",
956 .cfunc = write_f,
957 .argmin = 2,
958 .argmax = -1,
Eric Blakec2e001c2016-05-07 21:16:45 -0600959 .args = "[-bcCfquz] [-P pattern] off len",
Kevin Wolf797ac582013-06-05 14:19:31 +0200960 .oneline = "writes a number of bytes at a specified offset",
961 .help = write_help,
962};
963
Max Reitz4c7b7e92015-02-05 13:58:22 -0500964static int write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +0200965{
966 struct timeval t1, t2;
Eric Blake093ea232016-05-07 21:16:43 -0600967 bool Cflag = false, qflag = false, bflag = false;
Eric Blakedc388522016-05-07 21:16:42 -0600968 bool Pflag = false, zflag = false, cflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -0600969 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200970 int c, cnt;
971 char *buf = NULL;
972 int64_t offset;
John Snow9b0beaf2015-11-05 18:53:02 -0500973 int64_t count;
Kevin Wolf797ac582013-06-05 14:19:31 +0200974 /* Some compilers get confused and warn if this is not initialized. */
John Snow9b0beaf2015-11-05 18:53:02 -0500975 int64_t total = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +0200976 int pattern = 0xcd;
977
Eric Blakec2e001c2016-05-07 21:16:45 -0600978 while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +0200979 switch (c) {
980 case 'b':
Eric Blakedc388522016-05-07 21:16:42 -0600981 bflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200982 break;
983 case 'c':
Eric Blakedc388522016-05-07 21:16:42 -0600984 cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200985 break;
986 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -0600987 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200988 break;
Eric Blake770e0e02016-05-07 21:16:44 -0600989 case 'f':
990 flags |= BDRV_REQ_FUA;
991 break;
Kevin Wolf797ac582013-06-05 14:19:31 +0200992 case 'p':
Eric Blake093ea232016-05-07 21:16:43 -0600993 /* Ignored for backwards compatibility */
Kevin Wolf797ac582013-06-05 14:19:31 +0200994 break;
995 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -0600996 Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +0200997 pattern = parse_pattern(optarg);
998 if (pattern < 0) {
999 return 0;
1000 }
1001 break;
1002 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001003 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001004 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001005 case 'u':
1006 flags |= BDRV_REQ_MAY_UNMAP;
1007 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001008 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001009 zflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001010 break;
1011 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001012 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001013 }
1014 }
1015
1016 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001017 return qemuio_command_usage(&write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001018 }
1019
Eric Blake093ea232016-05-07 21:16:43 -06001020 if (bflag && zflag) {
1021 printf("-b and -z cannot be specified at the same time\n");
Kevin Wolf797ac582013-06-05 14:19:31 +02001022 return 0;
1023 }
1024
Eric Blake770e0e02016-05-07 21:16:44 -06001025 if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
1026 printf("-f and -b or -c cannot be specified at the same time\n");
1027 return 0;
1028 }
1029
Eric Blakec2e001c2016-05-07 21:16:45 -06001030 if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
1031 printf("-u requires -z to be specified\n");
1032 return 0;
1033 }
1034
Kevin Wolf797ac582013-06-05 14:19:31 +02001035 if (zflag && Pflag) {
1036 printf("-z and -P cannot be specified at the same time\n");
1037 return 0;
1038 }
1039
1040 offset = cvtnum(argv[optind]);
1041 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001042 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001043 return 0;
1044 }
1045
1046 optind++;
1047 count = cvtnum(argv[optind]);
1048 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001049 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001050 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001051 } else if (count > SIZE_MAX) {
1052 printf("length cannot exceed %" PRIu64 ", given %s\n",
1053 (uint64_t) SIZE_MAX, argv[optind]);
1054 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001055 }
1056
Eric Blake093ea232016-05-07 21:16:43 -06001057 if (bflag || cflag) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001058 if (offset & 0x1ff) {
1059 printf("offset %" PRId64 " is not sector aligned\n",
1060 offset);
1061 return 0;
1062 }
1063
1064 if (count & 0x1ff) {
John Snow9b0beaf2015-11-05 18:53:02 -05001065 printf("count %"PRId64" is not sector aligned\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001066 count);
1067 return 0;
1068 }
1069 }
1070
1071 if (!zflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001072 buf = qemu_io_alloc(blk, count, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001073 }
1074
1075 gettimeofday(&t1, NULL);
Eric Blake7b3f9712016-05-06 10:26:44 -06001076 if (bflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001077 cnt = do_save_vmstate(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001078 } else if (zflag) {
Eric Blake770e0e02016-05-07 21:16:44 -06001079 cnt = do_co_write_zeroes(blk, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001080 } else if (cflag) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001081 cnt = do_write_compressed(blk, buf, offset, count, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001082 } else {
Eric Blake770e0e02016-05-07 21:16:44 -06001083 cnt = do_pwrite(blk, buf, offset, count, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001084 }
1085 gettimeofday(&t2, NULL);
1086
1087 if (cnt < 0) {
1088 printf("write failed: %s\n", strerror(-cnt));
1089 goto out;
1090 }
1091
1092 if (qflag) {
1093 goto out;
1094 }
1095
1096 /* Finally, report back -- -C gives a parsable format */
1097 t2 = tsub(t2, t1);
1098 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1099
1100out:
1101 if (!zflag) {
1102 qemu_io_free(buf);
1103 }
1104
1105 return 0;
1106}
1107
1108static void
1109writev_help(void)
1110{
1111 printf(
1112"\n"
1113" writes a range of bytes from the given offset source from multiple buffers\n"
1114"\n"
1115" Example:\n"
Maria Kustova6e6507c2014-03-18 09:59:17 +04001116" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001117"\n"
1118" Writes into a segment of the currently open file, using a buffer\n"
1119" filled with a set pattern (0xcdcdcdcd).\n"
1120" -P, -- use different pattern to fill file\n"
1121" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001122" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001123" -q, -- quiet mode, do not show I/O statistics\n"
1124"\n");
1125}
1126
Max Reitz4c7b7e92015-02-05 13:58:22 -05001127static int writev_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001128
1129static const cmdinfo_t writev_cmd = {
1130 .name = "writev",
1131 .cfunc = writev_f,
1132 .argmin = 2,
1133 .argmax = -1,
Eric Blake770e0e02016-05-07 21:16:44 -06001134 .args = "[-Cfq] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001135 .oneline = "writes a number of bytes at a specified offset",
1136 .help = writev_help,
1137};
1138
Max Reitz4c7b7e92015-02-05 13:58:22 -05001139static int writev_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001140{
1141 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001142 bool Cflag = false, qflag = false;
Eric Blake770e0e02016-05-07 21:16:44 -06001143 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001144 int c, cnt;
1145 char *buf;
1146 int64_t offset;
1147 /* Some compilers get confused and warn if this is not initialized. */
1148 int total = 0;
1149 int nr_iov;
1150 int pattern = 0xcd;
1151 QEMUIOVector qiov;
1152
Eric Blakeb062ad82015-05-12 09:10:56 -06001153 while ((c = getopt(argc, argv, "CqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001154 switch (c) {
1155 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001156 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001157 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001158 case 'f':
1159 flags |= BDRV_REQ_FUA;
1160 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001161 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001162 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001163 break;
1164 case 'P':
1165 pattern = parse_pattern(optarg);
1166 if (pattern < 0) {
1167 return 0;
1168 }
1169 break;
1170 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001171 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001172 }
1173 }
1174
1175 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001176 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001177 }
1178
1179 offset = cvtnum(argv[optind]);
1180 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001181 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001182 return 0;
1183 }
1184 optind++;
1185
Kevin Wolf797ac582013-06-05 14:19:31 +02001186 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001187 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001188 if (buf == NULL) {
1189 return 0;
1190 }
1191
1192 gettimeofday(&t1, NULL);
Eric Blake770e0e02016-05-07 21:16:44 -06001193 cnt = do_aio_writev(blk, &qiov, offset, flags, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001194 gettimeofday(&t2, NULL);
1195
1196 if (cnt < 0) {
1197 printf("writev failed: %s\n", strerror(-cnt));
1198 goto out;
1199 }
1200
1201 if (qflag) {
1202 goto out;
1203 }
1204
1205 /* Finally, report back -- -C gives a parsable format */
1206 t2 = tsub(t2, t1);
1207 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1208out:
1209 qemu_iovec_destroy(&qiov);
1210 qemu_io_free(buf);
1211 return 0;
1212}
1213
1214static void multiwrite_help(void)
1215{
1216 printf(
1217"\n"
1218" writes a range of bytes from the given offset source from multiple buffers,\n"
1219" in a batch of requests that may be merged by qemu\n"
1220"\n"
1221" Example:\n"
1222" 'multiwrite 512 1k 1k ; 4k 1k'\n"
1223" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1224"\n"
1225" Writes into a segment of the currently open file, using a buffer\n"
1226" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1227" by one for each request contained in the multiwrite command.\n"
1228" -P, -- use different pattern to fill file\n"
1229" -C, -- report statistics in a machine parsable format\n"
1230" -q, -- quiet mode, do not show I/O statistics\n"
1231"\n");
1232}
1233
Max Reitz4c7b7e92015-02-05 13:58:22 -05001234static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001235
1236static const cmdinfo_t multiwrite_cmd = {
1237 .name = "multiwrite",
1238 .cfunc = multiwrite_f,
1239 .argmin = 2,
1240 .argmax = -1,
1241 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1242 .oneline = "issues multiple write requests at once",
1243 .help = multiwrite_help,
1244};
1245
Max Reitz4c7b7e92015-02-05 13:58:22 -05001246static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001247{
1248 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001249 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001250 int c, cnt;
1251 char **buf;
1252 int64_t offset, first_offset = 0;
1253 /* Some compilers get confused and warn if this is not initialized. */
1254 int total = 0;
1255 int nr_iov;
1256 int nr_reqs;
1257 int pattern = 0xcd;
1258 QEMUIOVector *qiovs;
1259 int i;
1260 BlockRequest *reqs;
1261
Eric Blakeb062ad82015-05-12 09:10:56 -06001262 while ((c = getopt(argc, argv, "CqP:")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001263 switch (c) {
1264 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001265 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001266 break;
1267 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001268 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001269 break;
1270 case 'P':
1271 pattern = parse_pattern(optarg);
1272 if (pattern < 0) {
1273 return 0;
1274 }
1275 break;
1276 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001277 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001278 }
1279 }
1280
1281 if (optind > argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001282 return qemuio_command_usage(&writev_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001283 }
1284
1285 nr_reqs = 1;
1286 for (i = optind; i < argc; i++) {
1287 if (!strcmp(argv[i], ";")) {
1288 nr_reqs++;
1289 }
1290 }
1291
Markus Armbruster02c4f262014-08-19 10:31:09 +02001292 reqs = g_new0(BlockRequest, nr_reqs);
1293 buf = g_new0(char *, nr_reqs);
1294 qiovs = g_new(QEMUIOVector, nr_reqs);
Kevin Wolf797ac582013-06-05 14:19:31 +02001295
1296 for (i = 0; i < nr_reqs && optind < argc; i++) {
1297 int j;
1298
1299 /* Read the offset of the request */
1300 offset = cvtnum(argv[optind]);
1301 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001302 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001303 goto out;
1304 }
1305 optind++;
1306
1307 if (offset & 0x1ff) {
1308 printf("offset %lld is not sector aligned\n",
1309 (long long)offset);
1310 goto out;
1311 }
1312
1313 if (i == 0) {
1314 first_offset = offset;
1315 }
1316
1317 /* Read lengths for qiov entries */
1318 for (j = optind; j < argc; j++) {
1319 if (!strcmp(argv[j], ";")) {
1320 break;
1321 }
1322 }
1323
1324 nr_iov = j - optind;
1325
1326 /* Build request */
Max Reitz4c7b7e92015-02-05 13:58:22 -05001327 buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
Kevin Wolf797ac582013-06-05 14:19:31 +02001328 if (buf[i] == NULL) {
1329 goto out;
1330 }
1331
1332 reqs[i].qiov = &qiovs[i];
1333 reqs[i].sector = offset >> 9;
1334 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1335
1336 optind = j + 1;
1337
1338 pattern++;
1339 }
1340
1341 /* If there were empty requests at the end, ignore them */
1342 nr_reqs = i;
1343
1344 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001345 cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
Kevin Wolf797ac582013-06-05 14:19:31 +02001346 gettimeofday(&t2, NULL);
1347
1348 if (cnt < 0) {
1349 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1350 goto out;
1351 }
1352
1353 if (qflag) {
1354 goto out;
1355 }
1356
1357 /* Finally, report back -- -C gives a parsable format */
1358 t2 = tsub(t2, t1);
1359 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1360out:
1361 for (i = 0; i < nr_reqs; i++) {
1362 qemu_io_free(buf[i]);
1363 if (reqs[i].qiov != NULL) {
1364 qemu_iovec_destroy(&qiovs[i]);
1365 }
1366 }
1367 g_free(buf);
1368 g_free(reqs);
1369 g_free(qiovs);
1370 return 0;
1371}
1372
1373struct aio_ctx {
Max Reitz4c7b7e92015-02-05 13:58:22 -05001374 BlockBackend *blk;
Kevin Wolf797ac582013-06-05 14:19:31 +02001375 QEMUIOVector qiov;
1376 int64_t offset;
1377 char *buf;
Eric Blakedc388522016-05-07 21:16:42 -06001378 bool qflag;
1379 bool vflag;
1380 bool Cflag;
1381 bool Pflag;
1382 bool zflag;
Fam Zhenga91f9582015-01-30 10:49:42 +08001383 BlockAcctCookie acct;
Kevin Wolf797ac582013-06-05 14:19:31 +02001384 int pattern;
1385 struct timeval t1;
1386};
1387
1388static void aio_write_done(void *opaque, int ret)
1389{
1390 struct aio_ctx *ctx = opaque;
1391 struct timeval t2;
1392
1393 gettimeofday(&t2, NULL);
1394
1395
1396 if (ret < 0) {
1397 printf("aio_write failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001398 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001399 goto out;
1400 }
1401
Max Reitz4c7b7e92015-02-05 13:58:22 -05001402 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001403
Kevin Wolf797ac582013-06-05 14:19:31 +02001404 if (ctx->qflag) {
1405 goto out;
1406 }
1407
1408 /* Finally, report back -- -C gives a parsable format */
1409 t2 = tsub(t2, ctx->t1);
1410 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1411 ctx->qiov.size, 1, ctx->Cflag);
1412out:
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001413 if (!ctx->zflag) {
1414 qemu_io_free(ctx->buf);
1415 qemu_iovec_destroy(&ctx->qiov);
1416 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001417 g_free(ctx);
1418}
1419
1420static void aio_read_done(void *opaque, int ret)
1421{
1422 struct aio_ctx *ctx = opaque;
1423 struct timeval t2;
1424
1425 gettimeofday(&t2, NULL);
1426
1427 if (ret < 0) {
1428 printf("readv failed: %s\n", strerror(-ret));
Alberto Garcia556c2b62015-10-28 17:33:08 +02001429 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
Kevin Wolf797ac582013-06-05 14:19:31 +02001430 goto out;
1431 }
1432
1433 if (ctx->Pflag) {
1434 void *cmp_buf = g_malloc(ctx->qiov.size);
1435
1436 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1437 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1438 printf("Pattern verification failed at offset %"
1439 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1440 }
1441 g_free(cmp_buf);
1442 }
1443
Max Reitz4c7b7e92015-02-05 13:58:22 -05001444 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
Fam Zhenga91f9582015-01-30 10:49:42 +08001445
Kevin Wolf797ac582013-06-05 14:19:31 +02001446 if (ctx->qflag) {
1447 goto out;
1448 }
1449
1450 if (ctx->vflag) {
1451 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1452 }
1453
1454 /* Finally, report back -- -C gives a parsable format */
1455 t2 = tsub(t2, ctx->t1);
1456 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1457 ctx->qiov.size, 1, ctx->Cflag);
1458out:
1459 qemu_io_free(ctx->buf);
1460 qemu_iovec_destroy(&ctx->qiov);
1461 g_free(ctx);
1462}
1463
1464static void aio_read_help(void)
1465{
1466 printf(
1467"\n"
1468" asynchronously reads a range of bytes from the given offset\n"
1469"\n"
1470" Example:\n"
1471" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1472"\n"
1473" Reads a segment of the currently open file, optionally dumping it to the\n"
1474" standard output stream (with -v option) for subsequent inspection.\n"
1475" The read is performed asynchronously and the aio_flush command must be\n"
1476" used to ensure all outstanding aio requests have been completed.\n"
1477" -C, -- report statistics in a machine parsable format\n"
1478" -P, -- use a pattern to verify read data\n"
1479" -v, -- dump buffer to standard output\n"
1480" -q, -- quiet mode, do not show I/O statistics\n"
1481"\n");
1482}
1483
Max Reitz4c7b7e92015-02-05 13:58:22 -05001484static int aio_read_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001485
1486static const cmdinfo_t aio_read_cmd = {
1487 .name = "aio_read",
1488 .cfunc = aio_read_f,
1489 .argmin = 2,
1490 .argmax = -1,
Eric Blake093ea232016-05-07 21:16:43 -06001491 .args = "[-Cqv] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001492 .oneline = "asynchronously reads a number of bytes",
1493 .help = aio_read_help,
1494};
1495
Max Reitz4c7b7e92015-02-05 13:58:22 -05001496static int aio_read_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001497{
1498 int nr_iov, c;
1499 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1500
Max Reitz4c7b7e92015-02-05 13:58:22 -05001501 ctx->blk = blk;
Eric Blakeb062ad82015-05-12 09:10:56 -06001502 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001503 switch (c) {
1504 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001505 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001506 break;
1507 case 'P':
Eric Blakedc388522016-05-07 21:16:42 -06001508 ctx->Pflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001509 ctx->pattern = parse_pattern(optarg);
1510 if (ctx->pattern < 0) {
1511 g_free(ctx);
1512 return 0;
1513 }
1514 break;
1515 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001516 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001517 break;
1518 case 'v':
Eric Blakedc388522016-05-07 21:16:42 -06001519 ctx->vflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001520 break;
1521 default:
1522 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001523 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001524 }
1525 }
1526
1527 if (optind > argc - 2) {
1528 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001529 return qemuio_command_usage(&aio_read_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001530 }
1531
1532 ctx->offset = cvtnum(argv[optind]);
1533 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001534 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001535 g_free(ctx);
1536 return 0;
1537 }
1538 optind++;
1539
Kevin Wolf797ac582013-06-05 14:19:31 +02001540 nr_iov = argc - optind;
Max Reitz4c7b7e92015-02-05 13:58:22 -05001541 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolf797ac582013-06-05 14:19:31 +02001542 if (ctx->buf == NULL) {
Alberto Garcia556c2b62015-10-28 17:33:08 +02001543 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
Kevin Wolf797ac582013-06-05 14:19:31 +02001544 g_free(ctx);
1545 return 0;
1546 }
1547
1548 gettimeofday(&ctx->t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001549 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1550 BLOCK_ACCT_READ);
Eric Blake7b3f9712016-05-06 10:26:44 -06001551 blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
Kevin Wolf797ac582013-06-05 14:19:31 +02001552 return 0;
1553}
1554
1555static void aio_write_help(void)
1556{
1557 printf(
1558"\n"
1559" asynchronously writes a range of bytes from the given offset source\n"
1560" from multiple buffers\n"
1561"\n"
1562" Example:\n"
1563" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1564"\n"
1565" Writes into a segment of the currently open file, using a buffer\n"
1566" filled with a set pattern (0xcdcdcdcd).\n"
1567" The write is performed asynchronously and the aio_flush command must be\n"
1568" used to ensure all outstanding aio requests have been completed.\n"
1569" -P, -- use different pattern to fill file\n"
1570" -C, -- report statistics in a machine parsable format\n"
Eric Blake770e0e02016-05-07 21:16:44 -06001571" -f, -- use Force Unit Access semantics\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001572" -q, -- quiet mode, do not show I/O statistics\n"
Eric Blakec2e001c2016-05-07 21:16:45 -06001573" -u, -- with -z, allow unmapping\n"
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001574" -z, -- write zeroes using blk_aio_write_zeroes\n"
Kevin Wolf797ac582013-06-05 14:19:31 +02001575"\n");
1576}
1577
Max Reitz4c7b7e92015-02-05 13:58:22 -05001578static int aio_write_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001579
1580static const cmdinfo_t aio_write_cmd = {
1581 .name = "aio_write",
1582 .cfunc = aio_write_f,
1583 .argmin = 2,
1584 .argmax = -1,
Eric Blakec2e001c2016-05-07 21:16:45 -06001585 .args = "[-Cfquz] [-P pattern] off len [len..]",
Kevin Wolf797ac582013-06-05 14:19:31 +02001586 .oneline = "asynchronously writes a number of bytes",
1587 .help = aio_write_help,
1588};
1589
Max Reitz4c7b7e92015-02-05 13:58:22 -05001590static int aio_write_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001591{
1592 int nr_iov, c;
1593 int pattern = 0xcd;
1594 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Eric Blake770e0e02016-05-07 21:16:44 -06001595 int flags = 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001596
Max Reitz4c7b7e92015-02-05 13:58:22 -05001597 ctx->blk = blk;
Eric Blake770e0e02016-05-07 21:16:44 -06001598 while ((c = getopt(argc, argv, "CfqP:z")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001599 switch (c) {
1600 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001601 ctx->Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001602 break;
Eric Blake770e0e02016-05-07 21:16:44 -06001603 case 'f':
1604 flags |= BDRV_REQ_FUA;
1605 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001606 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001607 ctx->qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001608 break;
Eric Blakec2e001c2016-05-07 21:16:45 -06001609 case 'u':
1610 flags |= BDRV_REQ_MAY_UNMAP;
1611 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001612 case 'P':
1613 pattern = parse_pattern(optarg);
1614 if (pattern < 0) {
1615 g_free(ctx);
1616 return 0;
1617 }
1618 break;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001619 case 'z':
Eric Blakedc388522016-05-07 21:16:42 -06001620 ctx->zflag = true;
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001621 break;
Kevin Wolf797ac582013-06-05 14:19:31 +02001622 default:
1623 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001624 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001625 }
1626 }
1627
1628 if (optind > argc - 2) {
1629 g_free(ctx);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001630 return qemuio_command_usage(&aio_write_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001631 }
1632
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001633 if (ctx->zflag && optind != argc - 2) {
1634 printf("-z supports only a single length parameter\n");
1635 g_free(ctx);
1636 return 0;
1637 }
1638
Eric Blakec2e001c2016-05-07 21:16:45 -06001639 if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
1640 printf("-u requires -z to be specified\n");
1641 return 0;
1642 }
1643
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001644 if (ctx->zflag && ctx->Pflag) {
1645 printf("-z and -P cannot be specified at the same time\n");
1646 g_free(ctx);
1647 return 0;
1648 }
1649
Kevin Wolf797ac582013-06-05 14:19:31 +02001650 ctx->offset = cvtnum(argv[optind]);
1651 if (ctx->offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001652 print_cvtnum_err(ctx->offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001653 g_free(ctx);
1654 return 0;
1655 }
1656 optind++;
1657
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001658 if (ctx->zflag) {
1659 int64_t count = cvtnum(argv[optind]);
1660 if (count < 0) {
1661 print_cvtnum_err(count, argv[optind]);
Kevin Wolf0e01b762016-05-09 12:03:04 +02001662 g_free(ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001663 return 0;
1664 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001665
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001666 ctx->qiov.size = count;
Eric Blake770e0e02016-05-07 21:16:44 -06001667 blk_aio_write_zeroes(blk, ctx->offset, count, flags, aio_write_done,
1668 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001669 } else {
1670 nr_iov = argc - optind;
1671 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1672 pattern);
1673 if (ctx->buf == NULL) {
1674 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1675 g_free(ctx);
1676 return 0;
1677 }
1678
1679 gettimeofday(&ctx->t1, NULL);
1680 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1681 BLOCK_ACCT_WRITE);
1682
Eric Blake770e0e02016-05-07 21:16:44 -06001683 blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
1684 ctx);
Kevin Wolf5ceb7762016-04-13 12:39:39 +02001685 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001686 return 0;
1687}
1688
Max Reitz4c7b7e92015-02-05 13:58:22 -05001689static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001690{
Alberto Garcia556c2b62015-10-28 17:33:08 +02001691 BlockAcctCookie cookie;
1692 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001693 blk_drain_all();
Alberto Garcia556c2b62015-10-28 17:33:08 +02001694 block_acct_done(blk_get_stats(blk), &cookie);
Kevin Wolf797ac582013-06-05 14:19:31 +02001695 return 0;
1696}
1697
1698static const cmdinfo_t aio_flush_cmd = {
1699 .name = "aio_flush",
1700 .cfunc = aio_flush_f,
1701 .oneline = "completes all outstanding aio requests"
1702};
1703
Max Reitz4c7b7e92015-02-05 13:58:22 -05001704static int flush_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001705{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001706 blk_flush(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001707 return 0;
1708}
1709
1710static const cmdinfo_t flush_cmd = {
1711 .name = "flush",
1712 .altname = "f",
1713 .cfunc = flush_f,
1714 .oneline = "flush all in-core file state to disk",
1715};
1716
Max Reitz4c7b7e92015-02-05 13:58:22 -05001717static int truncate_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001718{
1719 int64_t offset;
1720 int ret;
1721
1722 offset = cvtnum(argv[1]);
1723 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001724 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001725 return 0;
1726 }
1727
Max Reitz4c7b7e92015-02-05 13:58:22 -05001728 ret = blk_truncate(blk, offset);
Kevin Wolf797ac582013-06-05 14:19:31 +02001729 if (ret < 0) {
1730 printf("truncate: %s\n", strerror(-ret));
1731 return 0;
1732 }
1733
1734 return 0;
1735}
1736
1737static const cmdinfo_t truncate_cmd = {
1738 .name = "truncate",
1739 .altname = "t",
1740 .cfunc = truncate_f,
1741 .argmin = 1,
1742 .argmax = 1,
1743 .args = "off",
1744 .oneline = "truncates the current file at the given offset",
1745};
1746
Max Reitz4c7b7e92015-02-05 13:58:22 -05001747static int length_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001748{
1749 int64_t size;
1750 char s1[64];
1751
Max Reitz4c7b7e92015-02-05 13:58:22 -05001752 size = blk_getlength(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001753 if (size < 0) {
1754 printf("getlength: %s\n", strerror(-size));
1755 return 0;
1756 }
1757
1758 cvtstr(size, s1, sizeof(s1));
1759 printf("%s\n", s1);
1760 return 0;
1761}
1762
1763
1764static const cmdinfo_t length_cmd = {
1765 .name = "length",
1766 .altname = "l",
1767 .cfunc = length_f,
1768 .oneline = "gets the length of the current file",
1769};
1770
1771
Max Reitz4c7b7e92015-02-05 13:58:22 -05001772static int info_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001773{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001774 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf797ac582013-06-05 14:19:31 +02001775 BlockDriverInfo bdi;
Max Reitza8d8ecb2013-10-09 10:46:17 +02001776 ImageInfoSpecific *spec_info;
Kevin Wolf797ac582013-06-05 14:19:31 +02001777 char s1[64], s2[64];
1778 int ret;
1779
1780 if (bs->drv && bs->drv->format_name) {
1781 printf("format name: %s\n", bs->drv->format_name);
1782 }
1783 if (bs->drv && bs->drv->protocol_name) {
1784 printf("format name: %s\n", bs->drv->protocol_name);
1785 }
1786
1787 ret = bdrv_get_info(bs, &bdi);
1788 if (ret) {
1789 return 0;
1790 }
1791
1792 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1793 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1794
1795 printf("cluster size: %s\n", s1);
1796 printf("vm state offset: %s\n", s2);
1797
Max Reitza8d8ecb2013-10-09 10:46:17 +02001798 spec_info = bdrv_get_specific_info(bs);
1799 if (spec_info) {
1800 printf("Format specific information:\n");
1801 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1802 qapi_free_ImageInfoSpecific(spec_info);
1803 }
1804
Kevin Wolf797ac582013-06-05 14:19:31 +02001805 return 0;
1806}
1807
1808
1809
1810static const cmdinfo_t info_cmd = {
1811 .name = "info",
1812 .altname = "i",
1813 .cfunc = info_f,
1814 .oneline = "prints information about the current file",
1815};
1816
1817static void discard_help(void)
1818{
1819 printf(
1820"\n"
1821" discards a range of bytes from the given offset\n"
1822"\n"
1823" Example:\n"
1824" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1825"\n"
1826" Discards a segment of the currently open file.\n"
1827" -C, -- report statistics in a machine parsable format\n"
1828" -q, -- quiet mode, do not show I/O statistics\n"
1829"\n");
1830}
1831
Max Reitz4c7b7e92015-02-05 13:58:22 -05001832static int discard_f(BlockBackend *blk, int argc, char **argv);
Kevin Wolf797ac582013-06-05 14:19:31 +02001833
1834static const cmdinfo_t discard_cmd = {
1835 .name = "discard",
1836 .altname = "d",
1837 .cfunc = discard_f,
1838 .argmin = 2,
1839 .argmax = -1,
1840 .args = "[-Cq] off len",
1841 .oneline = "discards a number of bytes at a specified offset",
1842 .help = discard_help,
1843};
1844
Max Reitz4c7b7e92015-02-05 13:58:22 -05001845static int discard_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001846{
1847 struct timeval t1, t2;
Eric Blakedc388522016-05-07 21:16:42 -06001848 bool Cflag = false, qflag = false;
Kevin Wolf797ac582013-06-05 14:19:31 +02001849 int c, ret;
John Snow9b0beaf2015-11-05 18:53:02 -05001850 int64_t offset, count;
Kevin Wolf797ac582013-06-05 14:19:31 +02001851
Eric Blakeb062ad82015-05-12 09:10:56 -06001852 while ((c = getopt(argc, argv, "Cq")) != -1) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001853 switch (c) {
1854 case 'C':
Eric Blakedc388522016-05-07 21:16:42 -06001855 Cflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001856 break;
1857 case 'q':
Eric Blakedc388522016-05-07 21:16:42 -06001858 qflag = true;
Kevin Wolf797ac582013-06-05 14:19:31 +02001859 break;
1860 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001861 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001862 }
1863 }
1864
1865 if (optind != argc - 2) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02001866 return qemuio_command_usage(&discard_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02001867 }
1868
1869 offset = cvtnum(argv[optind]);
1870 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001871 print_cvtnum_err(offset, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001872 return 0;
1873 }
1874
1875 optind++;
1876 count = cvtnum(argv[optind]);
1877 if (count < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001878 print_cvtnum_err(count, argv[optind]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001879 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001880 } else if (count >> BDRV_SECTOR_BITS > INT_MAX) {
1881 printf("length cannot exceed %"PRIu64", given %s\n",
1882 (uint64_t)INT_MAX << BDRV_SECTOR_BITS,
1883 argv[optind]);
1884 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001885 }
1886
1887 gettimeofday(&t1, NULL);
Max Reitz4c7b7e92015-02-05 13:58:22 -05001888 ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1889 count >> BDRV_SECTOR_BITS);
Kevin Wolf797ac582013-06-05 14:19:31 +02001890 gettimeofday(&t2, NULL);
1891
1892 if (ret < 0) {
1893 printf("discard failed: %s\n", strerror(-ret));
1894 goto out;
1895 }
1896
1897 /* Finally, report back -- -C gives a parsable format */
1898 if (!qflag) {
1899 t2 = tsub(t2, t1);
1900 print_report("discard", &t2, offset, count, count, 1, Cflag);
1901 }
1902
1903out:
1904 return 0;
1905}
1906
Max Reitz4c7b7e92015-02-05 13:58:22 -05001907static int alloc_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02001908{
Max Reitz4c7b7e92015-02-05 13:58:22 -05001909 BlockDriverState *bs = blk_bs(blk);
John Snow9b0beaf2015-11-05 18:53:02 -05001910 int64_t offset, sector_num, nb_sectors, remaining;
Kevin Wolf797ac582013-06-05 14:19:31 +02001911 char s1[64];
John Snow9b0beaf2015-11-05 18:53:02 -05001912 int num, ret;
1913 int64_t sum_alloc;
Kevin Wolf797ac582013-06-05 14:19:31 +02001914
1915 offset = cvtnum(argv[1]);
1916 if (offset < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001917 print_cvtnum_err(offset, argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001918 return 0;
1919 } else if (offset & 0x1ff) {
1920 printf("offset %" PRId64 " is not sector aligned\n",
1921 offset);
1922 return 0;
1923 }
1924
1925 if (argc == 3) {
1926 nb_sectors = cvtnum(argv[2]);
1927 if (nb_sectors < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05001928 print_cvtnum_err(nb_sectors, argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02001929 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05001930 } else if (nb_sectors > INT_MAX) {
1931 printf("length argument cannot exceed %d, given %s\n",
1932 INT_MAX, argv[2]);
1933 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02001934 }
1935 } else {
1936 nb_sectors = 1;
1937 }
1938
1939 remaining = nb_sectors;
1940 sum_alloc = 0;
1941 sector_num = offset >> 9;
1942 while (remaining) {
1943 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
Paolo Bonzinid6636402013-09-04 19:00:25 +02001944 if (ret < 0) {
1945 printf("is_allocated failed: %s\n", strerror(-ret));
1946 return 0;
1947 }
Kevin Wolf797ac582013-06-05 14:19:31 +02001948 sector_num += num;
1949 remaining -= num;
1950 if (ret) {
1951 sum_alloc += num;
1952 }
1953 if (num == 0) {
1954 nb_sectors -= remaining;
1955 remaining = 0;
1956 }
1957 }
1958
1959 cvtstr(offset, s1, sizeof(s1));
1960
John Snow9b0beaf2015-11-05 18:53:02 -05001961 printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
Kevin Wolf797ac582013-06-05 14:19:31 +02001962 sum_alloc, nb_sectors, s1);
1963 return 0;
1964}
1965
1966static const cmdinfo_t alloc_cmd = {
1967 .name = "alloc",
1968 .altname = "a",
1969 .argmin = 1,
1970 .argmax = 2,
1971 .cfunc = alloc_f,
1972 .args = "off [sectors]",
1973 .oneline = "checks if a sector is present in the file",
1974};
1975
1976
1977static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1978 int64_t nb_sectors, int64_t *pnum)
1979{
1980 int num, num_checked;
1981 int ret, firstret;
1982
1983 num_checked = MIN(nb_sectors, INT_MAX);
1984 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1985 if (ret < 0) {
1986 return ret;
1987 }
1988
1989 firstret = ret;
1990 *pnum = num;
1991
1992 while (nb_sectors > 0 && ret == firstret) {
1993 sector_num += num;
1994 nb_sectors -= num;
1995
1996 num_checked = MIN(nb_sectors, INT_MAX);
1997 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
Max Reitz4b25bbc2014-10-22 17:00:16 +02001998 if (ret == firstret && num) {
Kevin Wolf797ac582013-06-05 14:19:31 +02001999 *pnum += num;
2000 } else {
2001 break;
2002 }
2003 }
2004
2005 return firstret;
2006}
2007
Max Reitz4c7b7e92015-02-05 13:58:22 -05002008static int map_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002009{
2010 int64_t offset;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002011 int64_t nb_sectors, total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02002012 char s1[64];
2013 int64_t num;
2014 int ret;
2015 const char *retstr;
2016
2017 offset = 0;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002018 total_sectors = blk_nb_sectors(blk);
2019 if (total_sectors < 0) {
2020 error_report("Failed to query image length: %s",
2021 strerror(-total_sectors));
2022 return 0;
2023 }
2024
2025 nb_sectors = total_sectors;
Kevin Wolf797ac582013-06-05 14:19:31 +02002026
2027 do {
Max Reitz4c7b7e92015-02-05 13:58:22 -05002028 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
Kevin Wolf797ac582013-06-05 14:19:31 +02002029 if (ret < 0) {
2030 error_report("Failed to get allocation status: %s", strerror(-ret));
2031 return 0;
Max Reitz4b25bbc2014-10-22 17:00:16 +02002032 } else if (!num) {
2033 error_report("Unexpected end of image");
2034 return 0;
Kevin Wolf797ac582013-06-05 14:19:31 +02002035 }
2036
2037 retstr = ret ? " allocated" : "not allocated";
2038 cvtstr(offset << 9ULL, s1, sizeof(s1));
2039 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
2040 "at offset %s (%d)\n",
2041 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
2042
2043 offset += num;
2044 nb_sectors -= num;
Max Reitz4c7b7e92015-02-05 13:58:22 -05002045 } while (offset < total_sectors);
Kevin Wolf797ac582013-06-05 14:19:31 +02002046
2047 return 0;
2048}
2049
2050static const cmdinfo_t map_cmd = {
2051 .name = "map",
2052 .argmin = 0,
2053 .argmax = 0,
2054 .cfunc = map_f,
2055 .args = "",
2056 .oneline = "prints the allocated areas of a file",
2057};
2058
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002059static void reopen_help(void)
2060{
2061 printf(
2062"\n"
2063" Changes the open options of an already opened image\n"
2064"\n"
2065" Example:\n"
2066" 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2067"\n"
2068" -r, -- Reopen the image read-only\n"
2069" -c, -- Change the cache mode to the given value\n"
2070" -o, -- Changes block driver options (cf. 'open' command)\n"
2071"\n");
2072}
2073
2074static int reopen_f(BlockBackend *blk, int argc, char **argv);
2075
2076static QemuOptsList reopen_opts = {
2077 .name = "reopen",
2078 .merge_lists = true,
2079 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2080 .desc = {
2081 /* no elements => accept any params */
2082 { /* end of list */ }
2083 },
2084};
2085
2086static const cmdinfo_t reopen_cmd = {
2087 .name = "reopen",
2088 .argmin = 0,
2089 .argmax = -1,
2090 .cfunc = reopen_f,
2091 .args = "[-r] [-c cache] [-o options]",
2092 .oneline = "reopens an image with new options",
2093 .help = reopen_help,
2094};
2095
2096static int reopen_f(BlockBackend *blk, int argc, char **argv)
2097{
2098 BlockDriverState *bs = blk_bs(blk);
2099 QemuOpts *qopts;
2100 QDict *opts;
2101 int c;
2102 int flags = bs->open_flags;
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002103 bool writethrough = !blk_enable_write_cache(blk);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002104
2105 BlockReopenQueue *brq;
2106 Error *local_err = NULL;
2107
2108 while ((c = getopt(argc, argv, "c:o:r")) != -1) {
2109 switch (c) {
2110 case 'c':
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002111 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002112 error_report("Invalid cache option: %s", optarg);
2113 return 0;
2114 }
2115 break;
2116 case 'o':
2117 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2118 qemu_opts_reset(&reopen_opts);
2119 return 0;
2120 }
2121 break;
2122 case 'r':
2123 flags &= ~BDRV_O_RDWR;
2124 break;
2125 default:
2126 qemu_opts_reset(&reopen_opts);
2127 return qemuio_command_usage(&reopen_cmd);
2128 }
2129 }
2130
2131 if (optind != argc) {
2132 qemu_opts_reset(&reopen_opts);
2133 return qemuio_command_usage(&reopen_cmd);
2134 }
2135
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002136 if (writethrough != blk_enable_write_cache(blk) &&
2137 blk_get_attached_dev(blk))
2138 {
2139 error_report("Cannot change cache.writeback: Device attached");
2140 qemu_opts_reset(&reopen_opts);
2141 return 0;
2142 }
2143
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002144 qopts = qemu_opts_find(&reopen_opts, NULL);
2145 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
2146 qemu_opts_reset(&reopen_opts);
2147
2148 brq = bdrv_reopen_queue(NULL, bs, opts, flags);
2149 bdrv_reopen_multiple(brq, &local_err);
2150 if (local_err) {
2151 error_report_err(local_err);
Kevin Wolf19dbecd2016-03-18 15:36:31 +01002152 } else {
2153 blk_set_enable_write_cache(blk, !writethrough);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002154 }
2155
2156 return 0;
2157}
2158
Max Reitz4c7b7e92015-02-05 13:58:22 -05002159static int break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002160{
2161 int ret;
2162
Max Reitz4c7b7e92015-02-05 13:58:22 -05002163 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002164 if (ret < 0) {
2165 printf("Could not set breakpoint: %s\n", strerror(-ret));
2166 }
2167
2168 return 0;
2169}
2170
Max Reitz4c7b7e92015-02-05 13:58:22 -05002171static int remove_break_f(BlockBackend *blk, int argc, char **argv)
Fam Zheng4cc70e92013-11-20 10:01:54 +08002172{
2173 int ret;
2174
Max Reitz4c7b7e92015-02-05 13:58:22 -05002175 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002176 if (ret < 0) {
2177 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2178 }
2179
2180 return 0;
2181}
2182
Kevin Wolf797ac582013-06-05 14:19:31 +02002183static const cmdinfo_t break_cmd = {
2184 .name = "break",
2185 .argmin = 2,
2186 .argmax = 2,
2187 .cfunc = break_f,
2188 .args = "event tag",
2189 .oneline = "sets a breakpoint on event and tags the stopped "
2190 "request as tag",
2191};
2192
Fam Zheng4cc70e92013-11-20 10:01:54 +08002193static const cmdinfo_t remove_break_cmd = {
2194 .name = "remove_break",
2195 .argmin = 1,
2196 .argmax = 1,
2197 .cfunc = remove_break_f,
2198 .args = "tag",
2199 .oneline = "remove a breakpoint by tag",
2200};
2201
Max Reitz4c7b7e92015-02-05 13:58:22 -05002202static int resume_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002203{
2204 int ret;
2205
Max Reitz4c7b7e92015-02-05 13:58:22 -05002206 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
Kevin Wolf797ac582013-06-05 14:19:31 +02002207 if (ret < 0) {
2208 printf("Could not resume request: %s\n", strerror(-ret));
2209 }
2210
2211 return 0;
2212}
2213
2214static const cmdinfo_t resume_cmd = {
2215 .name = "resume",
2216 .argmin = 1,
2217 .argmax = 1,
2218 .cfunc = resume_f,
2219 .args = "tag",
2220 .oneline = "resumes the request tagged as tag",
2221};
2222
Max Reitz4c7b7e92015-02-05 13:58:22 -05002223static int wait_break_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002224{
Max Reitz4c7b7e92015-02-05 13:58:22 -05002225 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2226 aio_poll(blk_get_aio_context(blk), true);
Kevin Wolf797ac582013-06-05 14:19:31 +02002227 }
2228
2229 return 0;
2230}
2231
2232static const cmdinfo_t wait_break_cmd = {
2233 .name = "wait_break",
2234 .argmin = 1,
2235 .argmax = 1,
2236 .cfunc = wait_break_f,
2237 .args = "tag",
2238 .oneline = "waits for the suspension of a request",
2239};
2240
Max Reitz4c7b7e92015-02-05 13:58:22 -05002241static int abort_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolf797ac582013-06-05 14:19:31 +02002242{
2243 abort();
2244}
2245
2246static const cmdinfo_t abort_cmd = {
2247 .name = "abort",
2248 .cfunc = abort_f,
2249 .flags = CMD_NOFILE_OK,
2250 .oneline = "simulate a program crash using abort(3)",
2251};
2252
Max Reitz0e82dc72014-12-08 10:48:10 +01002253static void sigraise_help(void)
2254{
2255 printf(
2256"\n"
2257" raises the given signal\n"
2258"\n"
2259" Example:\n"
2260" 'sigraise %i' - raises SIGTERM\n"
2261"\n"
2262" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2263" given to sigraise.\n"
2264"\n", SIGTERM);
2265}
2266
Max Reitz4c7b7e92015-02-05 13:58:22 -05002267static int sigraise_f(BlockBackend *blk, int argc, char **argv);
Max Reitz0e82dc72014-12-08 10:48:10 +01002268
2269static const cmdinfo_t sigraise_cmd = {
2270 .name = "sigraise",
2271 .cfunc = sigraise_f,
2272 .argmin = 1,
2273 .argmax = 1,
2274 .flags = CMD_NOFILE_OK,
2275 .args = "signal",
2276 .oneline = "raises a signal",
2277 .help = sigraise_help,
2278};
2279
Max Reitz4c7b7e92015-02-05 13:58:22 -05002280static int sigraise_f(BlockBackend *blk, int argc, char **argv)
Max Reitz0e82dc72014-12-08 10:48:10 +01002281{
John Snow9b0beaf2015-11-05 18:53:02 -05002282 int64_t sig = cvtnum(argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002283 if (sig < 0) {
John Snowa9ecfa02015-11-05 18:53:04 -05002284 print_cvtnum_err(sig, argv[1]);
Max Reitz0e82dc72014-12-08 10:48:10 +01002285 return 0;
John Snow9b0beaf2015-11-05 18:53:02 -05002286 } else if (sig > NSIG) {
2287 printf("signal argument '%s' is too large to be a valid signal\n",
2288 argv[1]);
2289 return 0;
Max Reitz0e82dc72014-12-08 10:48:10 +01002290 }
2291
2292 /* Using raise() to kill this process does not necessarily flush all open
2293 * streams. At least stdout and stderr (although the latter should be
2294 * non-buffered anyway) should be flushed, though. */
2295 fflush(stdout);
2296 fflush(stderr);
2297
2298 raise(sig);
2299 return 0;
2300}
2301
Kevin Wolfcd33d022014-01-15 15:39:10 +01002302static void sleep_cb(void *opaque)
2303{
2304 bool *expired = opaque;
2305 *expired = true;
2306}
2307
Max Reitz4c7b7e92015-02-05 13:58:22 -05002308static int sleep_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfcd33d022014-01-15 15:39:10 +01002309{
2310 char *endptr;
2311 long ms;
2312 struct QEMUTimer *timer;
2313 bool expired = false;
2314
2315 ms = strtol(argv[1], &endptr, 0);
2316 if (ms < 0 || *endptr != '\0') {
2317 printf("%s is not a valid number\n", argv[1]);
2318 return 0;
2319 }
2320
2321 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2322 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2323
2324 while (!expired) {
2325 main_loop_wait(false);
2326 }
2327
2328 timer_free(timer);
2329
2330 return 0;
2331}
2332
2333static const cmdinfo_t sleep_cmd = {
2334 .name = "sleep",
2335 .argmin = 1,
2336 .argmax = 1,
2337 .cfunc = sleep_f,
2338 .flags = CMD_NOFILE_OK,
2339 .oneline = "waits for the given value in milliseconds",
2340};
2341
Kevin Wolff18a8342013-06-05 14:19:33 +02002342static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2343{
2344 if (cmd) {
2345 printf("%s ", cmd);
2346 } else {
2347 printf("%s ", ct->name);
2348 if (ct->altname) {
2349 printf("(or %s) ", ct->altname);
2350 }
2351 }
2352
2353 if (ct->args) {
2354 printf("%s ", ct->args);
2355 }
2356 printf("-- %s\n", ct->oneline);
2357}
2358
2359static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2360{
2361 help_oneline(cmd, ct);
2362 if (ct->help) {
2363 ct->help();
2364 }
2365}
2366
2367static void help_all(void)
2368{
2369 const cmdinfo_t *ct;
2370
2371 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2372 help_oneline(ct->name, ct);
2373 }
2374 printf("\nUse 'help commandname' for extended help.\n");
2375}
2376
Max Reitz4c7b7e92015-02-05 13:58:22 -05002377static int help_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolff18a8342013-06-05 14:19:33 +02002378{
2379 const cmdinfo_t *ct;
2380
2381 if (argc == 1) {
2382 help_all();
2383 return 0;
2384 }
2385
2386 ct = find_command(argv[1]);
2387 if (ct == NULL) {
2388 printf("command %s not found\n", argv[1]);
2389 return 0;
2390 }
2391
2392 help_onecmd(argv[1], ct);
2393 return 0;
2394}
2395
2396static const cmdinfo_t help_cmd = {
2397 .name = "help",
2398 .altname = "?",
2399 .cfunc = help_f,
2400 .argmin = 0,
2401 .argmax = 1,
2402 .flags = CMD_FLAG_GLOBAL,
2403 .args = "[command]",
2404 .oneline = "help for one or all commands",
2405};
2406
Max Reitz4c7b7e92015-02-05 13:58:22 -05002407bool qemuio_command(BlockBackend *blk, const char *cmd)
Kevin Wolfdd583292013-06-05 14:19:32 +02002408{
2409 char *input;
2410 const cmdinfo_t *ct;
2411 char **v;
2412 int c;
2413 bool done = false;
2414
2415 input = g_strdup(cmd);
2416 v = breakline(input, &c);
2417 if (c) {
2418 ct = find_command(v[0]);
2419 if (ct) {
Max Reitz4c7b7e92015-02-05 13:58:22 -05002420 done = command(blk, ct, c, v);
Kevin Wolfdd583292013-06-05 14:19:32 +02002421 } else {
2422 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2423 }
2424 }
2425 g_free(input);
2426 g_free(v);
2427
2428 return done;
2429}
2430
Kevin Wolf797ac582013-06-05 14:19:31 +02002431static void __attribute((constructor)) init_qemuio_commands(void)
2432{
2433 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002434 qemuio_add_command(&help_cmd);
2435 qemuio_add_command(&read_cmd);
2436 qemuio_add_command(&readv_cmd);
2437 qemuio_add_command(&write_cmd);
2438 qemuio_add_command(&writev_cmd);
2439 qemuio_add_command(&multiwrite_cmd);
2440 qemuio_add_command(&aio_read_cmd);
2441 qemuio_add_command(&aio_write_cmd);
2442 qemuio_add_command(&aio_flush_cmd);
2443 qemuio_add_command(&flush_cmd);
2444 qemuio_add_command(&truncate_cmd);
2445 qemuio_add_command(&length_cmd);
2446 qemuio_add_command(&info_cmd);
2447 qemuio_add_command(&discard_cmd);
2448 qemuio_add_command(&alloc_cmd);
2449 qemuio_add_command(&map_cmd);
Kevin Wolf5bbd2e52014-12-08 17:37:28 +01002450 qemuio_add_command(&reopen_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002451 qemuio_add_command(&break_cmd);
Fam Zheng4cc70e92013-11-20 10:01:54 +08002452 qemuio_add_command(&remove_break_cmd);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +02002453 qemuio_add_command(&resume_cmd);
2454 qemuio_add_command(&wait_break_cmd);
2455 qemuio_add_command(&abort_cmd);
Kevin Wolfcd33d022014-01-15 15:39:10 +01002456 qemuio_add_command(&sleep_cmd);
Max Reitz0e82dc72014-12-08 10:48:10 +01002457 qemuio_add_command(&sigraise_cmd);
Kevin Wolf797ac582013-06-05 14:19:31 +02002458}