]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-io-cmds.c
include/qemu/osdep.h: Don't include qapi/error.h
[mirror_qemu.git] / qemu-io-cmds.c
1 /*
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
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qemu-io.h"
14 #include "sysemu/block-backend.h"
15 #include "block/block.h"
16 #include "block/block_int.h" /* for info_f() */
17 #include "block/qapi.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/timer.h"
21 #include "sysemu/block-backend.h"
22
23 #define CMD_NOFILE_OK 0x01
24
25 bool qemuio_misalign;
26
27 static cmdinfo_t *cmdtab;
28 static int ncmds;
29
30 static int compare_cmdname(const void *a, const void *b)
31 {
32 return strcmp(((const cmdinfo_t *)a)->name,
33 ((const cmdinfo_t *)b)->name);
34 }
35
36 void qemuio_add_command(const cmdinfo_t *ci)
37 {
38 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
39 cmdtab[ncmds - 1] = *ci;
40 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
41 }
42
43 int qemuio_command_usage(const cmdinfo_t *ci)
44 {
45 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
46 return 0;
47 }
48
49 static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
50 {
51 if (ct->flags & CMD_FLAG_GLOBAL) {
52 return 1;
53 }
54 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
55 fprintf(stderr, "no file open, try 'help open'\n");
56 return 0;
57 }
58 return 1;
59 }
60
61 static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
62 char **argv)
63 {
64 char *cmd = argv[0];
65
66 if (!init_check_command(blk, ct)) {
67 return 0;
68 }
69
70 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
71 if (ct->argmax == -1) {
72 fprintf(stderr,
73 "bad argument count %d to %s, expected at least %d arguments\n",
74 argc-1, cmd, ct->argmin);
75 } else if (ct->argmin == ct->argmax) {
76 fprintf(stderr,
77 "bad argument count %d to %s, expected %d arguments\n",
78 argc-1, cmd, ct->argmin);
79 } else {
80 fprintf(stderr,
81 "bad argument count %d to %s, expected between %d and %d arguments\n",
82 argc-1, cmd, ct->argmin, ct->argmax);
83 }
84 return 0;
85 }
86 optind = 0;
87 return ct->cfunc(blk, argc, argv);
88 }
89
90 static const cmdinfo_t *find_command(const char *cmd)
91 {
92 cmdinfo_t *ct;
93
94 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
95 if (strcmp(ct->name, cmd) == 0 ||
96 (ct->altname && strcmp(ct->altname, cmd) == 0))
97 {
98 return (const cmdinfo_t *)ct;
99 }
100 }
101 return NULL;
102 }
103
104 /* Invoke fn() for commands with a matching prefix */
105 void qemuio_complete_command(const char *input,
106 void (*fn)(const char *cmd, void *opaque),
107 void *opaque)
108 {
109 cmdinfo_t *ct;
110 size_t input_len = strlen(input);
111
112 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
113 if (strncmp(input, ct->name, input_len) == 0) {
114 fn(ct->name, opaque);
115 }
116 }
117 }
118
119 static char **breakline(char *input, int *count)
120 {
121 int c = 0;
122 char *p;
123 char **rval = g_new0(char *, 1);
124
125 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
126 if (!*p) {
127 continue;
128 }
129 c++;
130 rval = g_renew(char *, rval, (c + 1));
131 rval[c - 1] = p;
132 rval[c] = NULL;
133 }
134 *count = c;
135 return rval;
136 }
137
138 static int64_t cvtnum(const char *s)
139 {
140 char *end;
141 int64_t ret;
142
143 ret = qemu_strtosz_suffix(s, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
144 if (*end != '\0') {
145 /* Detritus at the end of the string */
146 return -EINVAL;
147 }
148 return ret;
149 }
150
151 static void print_cvtnum_err(int64_t rc, const char *arg)
152 {
153 switch (rc) {
154 case -EINVAL:
155 printf("Parsing error: non-numeric argument,"
156 " or extraneous/unrecognized suffix -- %s\n", arg);
157 break;
158 case -ERANGE:
159 printf("Parsing error: argument too large -- %s\n", arg);
160 break;
161 default:
162 printf("Parsing error: %s\n", arg);
163 }
164 }
165
166 #define EXABYTES(x) ((long long)(x) << 60)
167 #define PETABYTES(x) ((long long)(x) << 50)
168 #define TERABYTES(x) ((long long)(x) << 40)
169 #define GIGABYTES(x) ((long long)(x) << 30)
170 #define MEGABYTES(x) ((long long)(x) << 20)
171 #define KILOBYTES(x) ((long long)(x) << 10)
172
173 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
174 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
175 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
176 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
177 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
178 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
179
180 static void cvtstr(double value, char *str, size_t size)
181 {
182 char *trim;
183 const char *suffix;
184
185 if (value >= EXABYTES(1)) {
186 suffix = " EiB";
187 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
188 } else if (value >= PETABYTES(1)) {
189 suffix = " PiB";
190 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
191 } else if (value >= TERABYTES(1)) {
192 suffix = " TiB";
193 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
194 } else if (value >= GIGABYTES(1)) {
195 suffix = " GiB";
196 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
197 } else if (value >= MEGABYTES(1)) {
198 suffix = " MiB";
199 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
200 } else if (value >= KILOBYTES(1)) {
201 suffix = " KiB";
202 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
203 } else {
204 suffix = " bytes";
205 snprintf(str, size - 6, "%f", value);
206 }
207
208 trim = strstr(str, ".000");
209 if (trim) {
210 strcpy(trim, suffix);
211 } else {
212 strcat(str, suffix);
213 }
214 }
215
216
217
218 static struct timeval tsub(struct timeval t1, struct timeval t2)
219 {
220 t1.tv_usec -= t2.tv_usec;
221 if (t1.tv_usec < 0) {
222 t1.tv_usec += 1000000;
223 t1.tv_sec--;
224 }
225 t1.tv_sec -= t2.tv_sec;
226 return t1;
227 }
228
229 static double tdiv(double value, struct timeval tv)
230 {
231 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
232 }
233
234 #define HOURS(sec) ((sec) / (60 * 60))
235 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
236 #define SECONDS(sec) ((sec) % 60)
237
238 enum {
239 DEFAULT_TIME = 0x0,
240 TERSE_FIXED_TIME = 0x1,
241 VERBOSE_FIXED_TIME = 0x2,
242 };
243
244 static void timestr(struct timeval *tv, char *ts, size_t size, int format)
245 {
246 double usec = (double)tv->tv_usec / 1000000.0;
247
248 if (format & TERSE_FIXED_TIME) {
249 if (!HOURS(tv->tv_sec)) {
250 snprintf(ts, size, "%u:%02u.%02u",
251 (unsigned int) MINUTES(tv->tv_sec),
252 (unsigned int) SECONDS(tv->tv_sec),
253 (unsigned int) (usec * 100));
254 return;
255 }
256 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
257 }
258
259 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
260 snprintf(ts, size, "%u:%02u:%02u.%02u",
261 (unsigned int) HOURS(tv->tv_sec),
262 (unsigned int) MINUTES(tv->tv_sec),
263 (unsigned int) SECONDS(tv->tv_sec),
264 (unsigned int) (usec * 100));
265 } else {
266 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
267 }
268 }
269
270 /*
271 * Parse the pattern argument to various sub-commands.
272 *
273 * Because the pattern is used as an argument to memset it must evaluate
274 * to an unsigned integer that fits into a single byte.
275 */
276 static int parse_pattern(const char *arg)
277 {
278 char *endptr = NULL;
279 long pattern;
280
281 pattern = strtol(arg, &endptr, 0);
282 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
283 printf("%s is not a valid pattern byte\n", arg);
284 return -1;
285 }
286
287 return pattern;
288 }
289
290 /*
291 * Memory allocation helpers.
292 *
293 * Make sure memory is aligned by default, or purposefully misaligned if
294 * that is specified on the command line.
295 */
296
297 #define MISALIGN_OFFSET 16
298 static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
299 {
300 void *buf;
301
302 if (qemuio_misalign) {
303 len += MISALIGN_OFFSET;
304 }
305 buf = blk_blockalign(blk, len);
306 memset(buf, pattern, len);
307 if (qemuio_misalign) {
308 buf += MISALIGN_OFFSET;
309 }
310 return buf;
311 }
312
313 static void qemu_io_free(void *p)
314 {
315 if (qemuio_misalign) {
316 p -= MISALIGN_OFFSET;
317 }
318 qemu_vfree(p);
319 }
320
321 static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
322 {
323 uint64_t i;
324 int j;
325 const uint8_t *p;
326
327 for (i = 0, p = buffer; i < len; i += 16) {
328 const uint8_t *s = p;
329
330 printf("%08" PRIx64 ": ", offset + i);
331 for (j = 0; j < 16 && i + j < len; j++, p++) {
332 printf("%02x ", *p);
333 }
334 printf(" ");
335 for (j = 0; j < 16 && i + j < len; j++, s++) {
336 if (isalnum(*s)) {
337 printf("%c", *s);
338 } else {
339 printf(".");
340 }
341 }
342 printf("\n");
343 }
344 }
345
346 static void print_report(const char *op, struct timeval *t, int64_t offset,
347 int64_t count, int64_t total, int cnt, int Cflag)
348 {
349 char s1[64], s2[64], ts[64];
350
351 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
352 if (!Cflag) {
353 cvtstr((double)total, s1, sizeof(s1));
354 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
355 printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
356 op, total, count, offset);
357 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
358 s1, cnt, ts, s2, tdiv((double)cnt, *t));
359 } else {/* bytes,ops,time,bytes/sec,ops/sec */
360 printf("%"PRId64",%d,%s,%.3f,%.3f\n",
361 total, cnt, ts,
362 tdiv((double)total, *t),
363 tdiv((double)cnt, *t));
364 }
365 }
366
367 /*
368 * Parse multiple length statements for vectored I/O, and construct an I/O
369 * vector matching it.
370 */
371 static void *
372 create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
373 int pattern)
374 {
375 size_t *sizes = g_new0(size_t, nr_iov);
376 size_t count = 0;
377 void *buf = NULL;
378 void *p;
379 int i;
380
381 for (i = 0; i < nr_iov; i++) {
382 char *arg = argv[i];
383 int64_t len;
384
385 len = cvtnum(arg);
386 if (len < 0) {
387 print_cvtnum_err(len, arg);
388 goto fail;
389 }
390
391 /* should be SIZE_T_MAX, but that doesn't exist */
392 if (len > INT_MAX) {
393 printf("Argument '%s' exceeds maximum size %d\n", arg, INT_MAX);
394 goto fail;
395 }
396
397 if (len & 0x1ff) {
398 printf("length argument %" PRId64
399 " is not sector aligned\n", len);
400 goto fail;
401 }
402
403 sizes[i] = len;
404 count += len;
405 }
406
407 qemu_iovec_init(qiov, nr_iov);
408
409 buf = p = qemu_io_alloc(blk, count, pattern);
410
411 for (i = 0; i < nr_iov; i++) {
412 qemu_iovec_add(qiov, p, sizes[i]);
413 p += sizes[i];
414 }
415
416 fail:
417 g_free(sizes);
418 return buf;
419 }
420
421 static int do_read(BlockBackend *blk, char *buf, int64_t offset, int64_t count,
422 int64_t *total)
423 {
424 int ret;
425
426 if (count >> 9 > INT_MAX) {
427 return -ERANGE;
428 }
429
430 ret = blk_read(blk, offset >> 9, (uint8_t *)buf, count >> 9);
431 if (ret < 0) {
432 return ret;
433 }
434 *total = count;
435 return 1;
436 }
437
438 static int do_write(BlockBackend *blk, char *buf, int64_t offset, int64_t count,
439 int64_t *total)
440 {
441 int ret;
442
443 if (count >> 9 > INT_MAX) {
444 return -ERANGE;
445 }
446
447 ret = blk_write(blk, offset >> 9, (uint8_t *)buf, count >> 9);
448 if (ret < 0) {
449 return ret;
450 }
451 *total = count;
452 return 1;
453 }
454
455 static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
456 int64_t count, int64_t *total)
457 {
458 if (count > INT_MAX) {
459 return -ERANGE;
460 }
461
462 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
463 if (*total < 0) {
464 return *total;
465 }
466 return 1;
467 }
468
469 static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
470 int64_t count, int64_t *total)
471 {
472 if (count > INT_MAX) {
473 return -ERANGE;
474 }
475
476 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count);
477 if (*total < 0) {
478 return *total;
479 }
480 return 1;
481 }
482
483 typedef struct {
484 BlockBackend *blk;
485 int64_t offset;
486 int64_t count;
487 int64_t *total;
488 int ret;
489 bool done;
490 } CoWriteZeroes;
491
492 static void coroutine_fn co_write_zeroes_entry(void *opaque)
493 {
494 CoWriteZeroes *data = opaque;
495
496 data->ret = blk_co_write_zeroes(data->blk, data->offset / BDRV_SECTOR_SIZE,
497 data->count / BDRV_SECTOR_SIZE, 0);
498 data->done = true;
499 if (data->ret < 0) {
500 *data->total = data->ret;
501 return;
502 }
503
504 *data->total = data->count;
505 }
506
507 static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int64_t count,
508 int64_t *total)
509 {
510 Coroutine *co;
511 CoWriteZeroes data = {
512 .blk = blk,
513 .offset = offset,
514 .count = count,
515 .total = total,
516 .done = false,
517 };
518
519 if (count >> BDRV_SECTOR_BITS > INT_MAX) {
520 return -ERANGE;
521 }
522
523 co = qemu_coroutine_create(co_write_zeroes_entry);
524 qemu_coroutine_enter(co, &data);
525 while (!data.done) {
526 aio_poll(blk_get_aio_context(blk), true);
527 }
528 if (data.ret < 0) {
529 return data.ret;
530 } else {
531 return 1;
532 }
533 }
534
535 static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
536 int64_t count, int64_t *total)
537 {
538 int ret;
539
540 if (count >> 9 > INT_MAX) {
541 return -ERANGE;
542 }
543
544 ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
545 if (ret < 0) {
546 return ret;
547 }
548 *total = count;
549 return 1;
550 }
551
552 static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
553 int64_t count, int64_t *total)
554 {
555 if (count > INT_MAX) {
556 return -ERANGE;
557 }
558
559 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
560 if (*total < 0) {
561 return *total;
562 }
563 return 1;
564 }
565
566 static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
567 int64_t count, int64_t *total)
568 {
569 if (count > INT_MAX) {
570 return -ERANGE;
571 }
572
573 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
574 if (*total < 0) {
575 return *total;
576 }
577 return 1;
578 }
579
580 #define NOT_DONE 0x7fffffff
581 static void aio_rw_done(void *opaque, int ret)
582 {
583 *(int *)opaque = ret;
584 }
585
586 static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
587 int64_t offset, int *total)
588 {
589 int async_ret = NOT_DONE;
590
591 blk_aio_readv(blk, offset >> 9, qiov, qiov->size >> 9,
592 aio_rw_done, &async_ret);
593 while (async_ret == NOT_DONE) {
594 main_loop_wait(false);
595 }
596
597 *total = qiov->size;
598 return async_ret < 0 ? async_ret : 1;
599 }
600
601 static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
602 int64_t offset, int *total)
603 {
604 int async_ret = NOT_DONE;
605
606 blk_aio_writev(blk, offset >> 9, qiov, qiov->size >> 9,
607 aio_rw_done, &async_ret);
608 while (async_ret == NOT_DONE) {
609 main_loop_wait(false);
610 }
611
612 *total = qiov->size;
613 return async_ret < 0 ? async_ret : 1;
614 }
615
616 struct multiwrite_async_ret {
617 int num_done;
618 int error;
619 };
620
621 static void multiwrite_cb(void *opaque, int ret)
622 {
623 struct multiwrite_async_ret *async_ret = opaque;
624
625 async_ret->num_done++;
626 if (ret < 0) {
627 async_ret->error = ret;
628 }
629 }
630
631 static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
632 int num_reqs, int *total)
633 {
634 int i, ret;
635 struct multiwrite_async_ret async_ret = {
636 .num_done = 0,
637 .error = 0,
638 };
639
640 *total = 0;
641 for (i = 0; i < num_reqs; i++) {
642 reqs[i].cb = multiwrite_cb;
643 reqs[i].opaque = &async_ret;
644 *total += reqs[i].qiov->size;
645 }
646
647 ret = blk_aio_multiwrite(blk, reqs, num_reqs);
648 if (ret < 0) {
649 return ret;
650 }
651
652 while (async_ret.num_done < num_reqs) {
653 main_loop_wait(false);
654 }
655
656 return async_ret.error < 0 ? async_ret.error : 1;
657 }
658
659 static void read_help(void)
660 {
661 printf(
662 "\n"
663 " reads a range of bytes from the given offset\n"
664 "\n"
665 " Example:\n"
666 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
667 "\n"
668 " Reads a segment of the currently open file, optionally dumping it to the\n"
669 " standard output stream (with -v option) for subsequent inspection.\n"
670 " -b, -- read from the VM state rather than the virtual disk\n"
671 " -C, -- report statistics in a machine parsable format\n"
672 " -l, -- length for pattern verification (only with -P)\n"
673 " -p, -- use blk_pread to read the file\n"
674 " -P, -- use a pattern to verify read data\n"
675 " -q, -- quiet mode, do not show I/O statistics\n"
676 " -s, -- start offset for pattern verification (only with -P)\n"
677 " -v, -- dump buffer to standard output\n"
678 "\n");
679 }
680
681 static int read_f(BlockBackend *blk, int argc, char **argv);
682
683 static const cmdinfo_t read_cmd = {
684 .name = "read",
685 .altname = "r",
686 .cfunc = read_f,
687 .argmin = 2,
688 .argmax = -1,
689 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
690 .oneline = "reads a number of bytes at a specified offset",
691 .help = read_help,
692 };
693
694 static int read_f(BlockBackend *blk, int argc, char **argv)
695 {
696 struct timeval t1, t2;
697 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
698 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
699 int c, cnt;
700 char *buf;
701 int64_t offset;
702 int64_t count;
703 /* Some compilers get confused and warn if this is not initialized. */
704 int64_t total = 0;
705 int pattern = 0;
706 int64_t pattern_offset = 0, pattern_count = 0;
707
708 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
709 switch (c) {
710 case 'b':
711 bflag = 1;
712 break;
713 case 'C':
714 Cflag = 1;
715 break;
716 case 'l':
717 lflag = 1;
718 pattern_count = cvtnum(optarg);
719 if (pattern_count < 0) {
720 print_cvtnum_err(pattern_count, optarg);
721 return 0;
722 }
723 break;
724 case 'p':
725 pflag = 1;
726 break;
727 case 'P':
728 Pflag = 1;
729 pattern = parse_pattern(optarg);
730 if (pattern < 0) {
731 return 0;
732 }
733 break;
734 case 'q':
735 qflag = 1;
736 break;
737 case 's':
738 sflag = 1;
739 pattern_offset = cvtnum(optarg);
740 if (pattern_offset < 0) {
741 print_cvtnum_err(pattern_offset, optarg);
742 return 0;
743 }
744 break;
745 case 'v':
746 vflag = 1;
747 break;
748 default:
749 return qemuio_command_usage(&read_cmd);
750 }
751 }
752
753 if (optind != argc - 2) {
754 return qemuio_command_usage(&read_cmd);
755 }
756
757 if (bflag && pflag) {
758 printf("-b and -p cannot be specified at the same time\n");
759 return 0;
760 }
761
762 offset = cvtnum(argv[optind]);
763 if (offset < 0) {
764 print_cvtnum_err(offset, argv[optind]);
765 return 0;
766 }
767
768 optind++;
769 count = cvtnum(argv[optind]);
770 if (count < 0) {
771 print_cvtnum_err(count, argv[optind]);
772 return 0;
773 } else if (count > SIZE_MAX) {
774 printf("length cannot exceed %" PRIu64 ", given %s\n",
775 (uint64_t) SIZE_MAX, argv[optind]);
776 return 0;
777 }
778
779 if (!Pflag && (lflag || sflag)) {
780 return qemuio_command_usage(&read_cmd);
781 }
782
783 if (!lflag) {
784 pattern_count = count - pattern_offset;
785 }
786
787 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
788 printf("pattern verification range exceeds end of read data\n");
789 return 0;
790 }
791
792 if (!pflag) {
793 if (offset & 0x1ff) {
794 printf("offset %" PRId64 " is not sector aligned\n",
795 offset);
796 return 0;
797 }
798 if (count & 0x1ff) {
799 printf("count %"PRId64" is not sector aligned\n",
800 count);
801 return 0;
802 }
803 }
804
805 buf = qemu_io_alloc(blk, count, 0xab);
806
807 gettimeofday(&t1, NULL);
808 if (pflag) {
809 cnt = do_pread(blk, buf, offset, count, &total);
810 } else if (bflag) {
811 cnt = do_load_vmstate(blk, buf, offset, count, &total);
812 } else {
813 cnt = do_read(blk, buf, offset, count, &total);
814 }
815 gettimeofday(&t2, NULL);
816
817 if (cnt < 0) {
818 printf("read failed: %s\n", strerror(-cnt));
819 goto out;
820 }
821
822 if (Pflag) {
823 void *cmp_buf = g_malloc(pattern_count);
824 memset(cmp_buf, pattern, pattern_count);
825 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
826 printf("Pattern verification failed at offset %"
827 PRId64 ", %"PRId64" bytes\n",
828 offset + pattern_offset, pattern_count);
829 }
830 g_free(cmp_buf);
831 }
832
833 if (qflag) {
834 goto out;
835 }
836
837 if (vflag) {
838 dump_buffer(buf, offset, count);
839 }
840
841 /* Finally, report back -- -C gives a parsable format */
842 t2 = tsub(t2, t1);
843 print_report("read", &t2, offset, count, total, cnt, Cflag);
844
845 out:
846 qemu_io_free(buf);
847
848 return 0;
849 }
850
851 static void readv_help(void)
852 {
853 printf(
854 "\n"
855 " reads a range of bytes from the given offset into multiple buffers\n"
856 "\n"
857 " Example:\n"
858 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
859 "\n"
860 " Reads a segment of the currently open file, optionally dumping it to the\n"
861 " standard output stream (with -v option) for subsequent inspection.\n"
862 " Uses multiple iovec buffers if more than one byte range is specified.\n"
863 " -C, -- report statistics in a machine parsable format\n"
864 " -P, -- use a pattern to verify read data\n"
865 " -v, -- dump buffer to standard output\n"
866 " -q, -- quiet mode, do not show I/O statistics\n"
867 "\n");
868 }
869
870 static int readv_f(BlockBackend *blk, int argc, char **argv);
871
872 static const cmdinfo_t readv_cmd = {
873 .name = "readv",
874 .cfunc = readv_f,
875 .argmin = 2,
876 .argmax = -1,
877 .args = "[-Cqv] [-P pattern ] off len [len..]",
878 .oneline = "reads a number of bytes at a specified offset",
879 .help = readv_help,
880 };
881
882 static int readv_f(BlockBackend *blk, int argc, char **argv)
883 {
884 struct timeval t1, t2;
885 int Cflag = 0, qflag = 0, vflag = 0;
886 int c, cnt;
887 char *buf;
888 int64_t offset;
889 /* Some compilers get confused and warn if this is not initialized. */
890 int total = 0;
891 int nr_iov;
892 QEMUIOVector qiov;
893 int pattern = 0;
894 int Pflag = 0;
895
896 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
897 switch (c) {
898 case 'C':
899 Cflag = 1;
900 break;
901 case 'P':
902 Pflag = 1;
903 pattern = parse_pattern(optarg);
904 if (pattern < 0) {
905 return 0;
906 }
907 break;
908 case 'q':
909 qflag = 1;
910 break;
911 case 'v':
912 vflag = 1;
913 break;
914 default:
915 return qemuio_command_usage(&readv_cmd);
916 }
917 }
918
919 if (optind > argc - 2) {
920 return qemuio_command_usage(&readv_cmd);
921 }
922
923
924 offset = cvtnum(argv[optind]);
925 if (offset < 0) {
926 print_cvtnum_err(offset, argv[optind]);
927 return 0;
928 }
929 optind++;
930
931 if (offset & 0x1ff) {
932 printf("offset %" PRId64 " is not sector aligned\n",
933 offset);
934 return 0;
935 }
936
937 nr_iov = argc - optind;
938 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
939 if (buf == NULL) {
940 return 0;
941 }
942
943 gettimeofday(&t1, NULL);
944 cnt = do_aio_readv(blk, &qiov, offset, &total);
945 gettimeofday(&t2, NULL);
946
947 if (cnt < 0) {
948 printf("readv failed: %s\n", strerror(-cnt));
949 goto out;
950 }
951
952 if (Pflag) {
953 void *cmp_buf = g_malloc(qiov.size);
954 memset(cmp_buf, pattern, qiov.size);
955 if (memcmp(buf, cmp_buf, qiov.size)) {
956 printf("Pattern verification failed at offset %"
957 PRId64 ", %zd bytes\n", offset, qiov.size);
958 }
959 g_free(cmp_buf);
960 }
961
962 if (qflag) {
963 goto out;
964 }
965
966 if (vflag) {
967 dump_buffer(buf, offset, qiov.size);
968 }
969
970 /* Finally, report back -- -C gives a parsable format */
971 t2 = tsub(t2, t1);
972 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
973
974 out:
975 qemu_iovec_destroy(&qiov);
976 qemu_io_free(buf);
977 return 0;
978 }
979
980 static void write_help(void)
981 {
982 printf(
983 "\n"
984 " writes a range of bytes from the given offset\n"
985 "\n"
986 " Example:\n"
987 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
988 "\n"
989 " Writes into a segment of the currently open file, using a buffer\n"
990 " filled with a set pattern (0xcdcdcdcd).\n"
991 " -b, -- write to the VM state rather than the virtual disk\n"
992 " -c, -- write compressed data with blk_write_compressed\n"
993 " -p, -- use blk_pwrite to write the file\n"
994 " -P, -- use different pattern to fill file\n"
995 " -C, -- report statistics in a machine parsable format\n"
996 " -q, -- quiet mode, do not show I/O statistics\n"
997 " -z, -- write zeroes using blk_co_write_zeroes\n"
998 "\n");
999 }
1000
1001 static int write_f(BlockBackend *blk, int argc, char **argv);
1002
1003 static const cmdinfo_t write_cmd = {
1004 .name = "write",
1005 .altname = "w",
1006 .cfunc = write_f,
1007 .argmin = 2,
1008 .argmax = -1,
1009 .args = "[-bcCpqz] [-P pattern ] off len",
1010 .oneline = "writes a number of bytes at a specified offset",
1011 .help = write_help,
1012 };
1013
1014 static int write_f(BlockBackend *blk, int argc, char **argv)
1015 {
1016 struct timeval t1, t2;
1017 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
1018 int cflag = 0;
1019 int c, cnt;
1020 char *buf = NULL;
1021 int64_t offset;
1022 int64_t count;
1023 /* Some compilers get confused and warn if this is not initialized. */
1024 int64_t total = 0;
1025 int pattern = 0xcd;
1026
1027 while ((c = getopt(argc, argv, "bcCpP:qz")) != -1) {
1028 switch (c) {
1029 case 'b':
1030 bflag = 1;
1031 break;
1032 case 'c':
1033 cflag = 1;
1034 break;
1035 case 'C':
1036 Cflag = 1;
1037 break;
1038 case 'p':
1039 pflag = 1;
1040 break;
1041 case 'P':
1042 Pflag = 1;
1043 pattern = parse_pattern(optarg);
1044 if (pattern < 0) {
1045 return 0;
1046 }
1047 break;
1048 case 'q':
1049 qflag = 1;
1050 break;
1051 case 'z':
1052 zflag = 1;
1053 break;
1054 default:
1055 return qemuio_command_usage(&write_cmd);
1056 }
1057 }
1058
1059 if (optind != argc - 2) {
1060 return qemuio_command_usage(&write_cmd);
1061 }
1062
1063 if (bflag + pflag + zflag > 1) {
1064 printf("-b, -p, or -z cannot be specified at the same time\n");
1065 return 0;
1066 }
1067
1068 if (zflag && Pflag) {
1069 printf("-z and -P cannot be specified at the same time\n");
1070 return 0;
1071 }
1072
1073 offset = cvtnum(argv[optind]);
1074 if (offset < 0) {
1075 print_cvtnum_err(offset, argv[optind]);
1076 return 0;
1077 }
1078
1079 optind++;
1080 count = cvtnum(argv[optind]);
1081 if (count < 0) {
1082 print_cvtnum_err(count, argv[optind]);
1083 return 0;
1084 } else if (count > SIZE_MAX) {
1085 printf("length cannot exceed %" PRIu64 ", given %s\n",
1086 (uint64_t) SIZE_MAX, argv[optind]);
1087 return 0;
1088 }
1089
1090 if (!pflag) {
1091 if (offset & 0x1ff) {
1092 printf("offset %" PRId64 " is not sector aligned\n",
1093 offset);
1094 return 0;
1095 }
1096
1097 if (count & 0x1ff) {
1098 printf("count %"PRId64" is not sector aligned\n",
1099 count);
1100 return 0;
1101 }
1102 }
1103
1104 if (!zflag) {
1105 buf = qemu_io_alloc(blk, count, pattern);
1106 }
1107
1108 gettimeofday(&t1, NULL);
1109 if (pflag) {
1110 cnt = do_pwrite(blk, buf, offset, count, &total);
1111 } else if (bflag) {
1112 cnt = do_save_vmstate(blk, buf, offset, count, &total);
1113 } else if (zflag) {
1114 cnt = do_co_write_zeroes(blk, offset, count, &total);
1115 } else if (cflag) {
1116 cnt = do_write_compressed(blk, buf, offset, count, &total);
1117 } else {
1118 cnt = do_write(blk, buf, offset, count, &total);
1119 }
1120 gettimeofday(&t2, NULL);
1121
1122 if (cnt < 0) {
1123 printf("write failed: %s\n", strerror(-cnt));
1124 goto out;
1125 }
1126
1127 if (qflag) {
1128 goto out;
1129 }
1130
1131 /* Finally, report back -- -C gives a parsable format */
1132 t2 = tsub(t2, t1);
1133 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1134
1135 out:
1136 if (!zflag) {
1137 qemu_io_free(buf);
1138 }
1139
1140 return 0;
1141 }
1142
1143 static void
1144 writev_help(void)
1145 {
1146 printf(
1147 "\n"
1148 " writes a range of bytes from the given offset source from multiple buffers\n"
1149 "\n"
1150 " Example:\n"
1151 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1152 "\n"
1153 " Writes into a segment of the currently open file, using a buffer\n"
1154 " filled with a set pattern (0xcdcdcdcd).\n"
1155 " -P, -- use different pattern to fill file\n"
1156 " -C, -- report statistics in a machine parsable format\n"
1157 " -q, -- quiet mode, do not show I/O statistics\n"
1158 "\n");
1159 }
1160
1161 static int writev_f(BlockBackend *blk, int argc, char **argv);
1162
1163 static const cmdinfo_t writev_cmd = {
1164 .name = "writev",
1165 .cfunc = writev_f,
1166 .argmin = 2,
1167 .argmax = -1,
1168 .args = "[-Cq] [-P pattern ] off len [len..]",
1169 .oneline = "writes a number of bytes at a specified offset",
1170 .help = writev_help,
1171 };
1172
1173 static int writev_f(BlockBackend *blk, int argc, char **argv)
1174 {
1175 struct timeval t1, t2;
1176 int Cflag = 0, qflag = 0;
1177 int c, cnt;
1178 char *buf;
1179 int64_t offset;
1180 /* Some compilers get confused and warn if this is not initialized. */
1181 int total = 0;
1182 int nr_iov;
1183 int pattern = 0xcd;
1184 QEMUIOVector qiov;
1185
1186 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1187 switch (c) {
1188 case 'C':
1189 Cflag = 1;
1190 break;
1191 case 'q':
1192 qflag = 1;
1193 break;
1194 case 'P':
1195 pattern = parse_pattern(optarg);
1196 if (pattern < 0) {
1197 return 0;
1198 }
1199 break;
1200 default:
1201 return qemuio_command_usage(&writev_cmd);
1202 }
1203 }
1204
1205 if (optind > argc - 2) {
1206 return qemuio_command_usage(&writev_cmd);
1207 }
1208
1209 offset = cvtnum(argv[optind]);
1210 if (offset < 0) {
1211 print_cvtnum_err(offset, argv[optind]);
1212 return 0;
1213 }
1214 optind++;
1215
1216 if (offset & 0x1ff) {
1217 printf("offset %" PRId64 " is not sector aligned\n",
1218 offset);
1219 return 0;
1220 }
1221
1222 nr_iov = argc - optind;
1223 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1224 if (buf == NULL) {
1225 return 0;
1226 }
1227
1228 gettimeofday(&t1, NULL);
1229 cnt = do_aio_writev(blk, &qiov, offset, &total);
1230 gettimeofday(&t2, NULL);
1231
1232 if (cnt < 0) {
1233 printf("writev failed: %s\n", strerror(-cnt));
1234 goto out;
1235 }
1236
1237 if (qflag) {
1238 goto out;
1239 }
1240
1241 /* Finally, report back -- -C gives a parsable format */
1242 t2 = tsub(t2, t1);
1243 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1244 out:
1245 qemu_iovec_destroy(&qiov);
1246 qemu_io_free(buf);
1247 return 0;
1248 }
1249
1250 static void multiwrite_help(void)
1251 {
1252 printf(
1253 "\n"
1254 " writes a range of bytes from the given offset source from multiple buffers,\n"
1255 " in a batch of requests that may be merged by qemu\n"
1256 "\n"
1257 " Example:\n"
1258 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1259 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1260 "\n"
1261 " Writes into a segment of the currently open file, using a buffer\n"
1262 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1263 " by one for each request contained in the multiwrite command.\n"
1264 " -P, -- use different pattern to fill file\n"
1265 " -C, -- report statistics in a machine parsable format\n"
1266 " -q, -- quiet mode, do not show I/O statistics\n"
1267 "\n");
1268 }
1269
1270 static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
1271
1272 static const cmdinfo_t multiwrite_cmd = {
1273 .name = "multiwrite",
1274 .cfunc = multiwrite_f,
1275 .argmin = 2,
1276 .argmax = -1,
1277 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1278 .oneline = "issues multiple write requests at once",
1279 .help = multiwrite_help,
1280 };
1281
1282 static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
1283 {
1284 struct timeval t1, t2;
1285 int Cflag = 0, qflag = 0;
1286 int c, cnt;
1287 char **buf;
1288 int64_t offset, first_offset = 0;
1289 /* Some compilers get confused and warn if this is not initialized. */
1290 int total = 0;
1291 int nr_iov;
1292 int nr_reqs;
1293 int pattern = 0xcd;
1294 QEMUIOVector *qiovs;
1295 int i;
1296 BlockRequest *reqs;
1297
1298 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1299 switch (c) {
1300 case 'C':
1301 Cflag = 1;
1302 break;
1303 case 'q':
1304 qflag = 1;
1305 break;
1306 case 'P':
1307 pattern = parse_pattern(optarg);
1308 if (pattern < 0) {
1309 return 0;
1310 }
1311 break;
1312 default:
1313 return qemuio_command_usage(&writev_cmd);
1314 }
1315 }
1316
1317 if (optind > argc - 2) {
1318 return qemuio_command_usage(&writev_cmd);
1319 }
1320
1321 nr_reqs = 1;
1322 for (i = optind; i < argc; i++) {
1323 if (!strcmp(argv[i], ";")) {
1324 nr_reqs++;
1325 }
1326 }
1327
1328 reqs = g_new0(BlockRequest, nr_reqs);
1329 buf = g_new0(char *, nr_reqs);
1330 qiovs = g_new(QEMUIOVector, nr_reqs);
1331
1332 for (i = 0; i < nr_reqs && optind < argc; i++) {
1333 int j;
1334
1335 /* Read the offset of the request */
1336 offset = cvtnum(argv[optind]);
1337 if (offset < 0) {
1338 print_cvtnum_err(offset, argv[optind]);
1339 goto out;
1340 }
1341 optind++;
1342
1343 if (offset & 0x1ff) {
1344 printf("offset %lld is not sector aligned\n",
1345 (long long)offset);
1346 goto out;
1347 }
1348
1349 if (i == 0) {
1350 first_offset = offset;
1351 }
1352
1353 /* Read lengths for qiov entries */
1354 for (j = optind; j < argc; j++) {
1355 if (!strcmp(argv[j], ";")) {
1356 break;
1357 }
1358 }
1359
1360 nr_iov = j - optind;
1361
1362 /* Build request */
1363 buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
1364 if (buf[i] == NULL) {
1365 goto out;
1366 }
1367
1368 reqs[i].qiov = &qiovs[i];
1369 reqs[i].sector = offset >> 9;
1370 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1371
1372 optind = j + 1;
1373
1374 pattern++;
1375 }
1376
1377 /* If there were empty requests at the end, ignore them */
1378 nr_reqs = i;
1379
1380 gettimeofday(&t1, NULL);
1381 cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
1382 gettimeofday(&t2, NULL);
1383
1384 if (cnt < 0) {
1385 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1386 goto out;
1387 }
1388
1389 if (qflag) {
1390 goto out;
1391 }
1392
1393 /* Finally, report back -- -C gives a parsable format */
1394 t2 = tsub(t2, t1);
1395 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1396 out:
1397 for (i = 0; i < nr_reqs; i++) {
1398 qemu_io_free(buf[i]);
1399 if (reqs[i].qiov != NULL) {
1400 qemu_iovec_destroy(&qiovs[i]);
1401 }
1402 }
1403 g_free(buf);
1404 g_free(reqs);
1405 g_free(qiovs);
1406 return 0;
1407 }
1408
1409 struct aio_ctx {
1410 BlockBackend *blk;
1411 QEMUIOVector qiov;
1412 int64_t offset;
1413 char *buf;
1414 int qflag;
1415 int vflag;
1416 int Cflag;
1417 int Pflag;
1418 BlockAcctCookie acct;
1419 int pattern;
1420 struct timeval t1;
1421 };
1422
1423 static void aio_write_done(void *opaque, int ret)
1424 {
1425 struct aio_ctx *ctx = opaque;
1426 struct timeval t2;
1427
1428 gettimeofday(&t2, NULL);
1429
1430
1431 if (ret < 0) {
1432 printf("aio_write failed: %s\n", strerror(-ret));
1433 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1434 goto out;
1435 }
1436
1437 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1438
1439 if (ctx->qflag) {
1440 goto out;
1441 }
1442
1443 /* Finally, report back -- -C gives a parsable format */
1444 t2 = tsub(t2, ctx->t1);
1445 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1446 ctx->qiov.size, 1, ctx->Cflag);
1447 out:
1448 qemu_io_free(ctx->buf);
1449 qemu_iovec_destroy(&ctx->qiov);
1450 g_free(ctx);
1451 }
1452
1453 static void aio_read_done(void *opaque, int ret)
1454 {
1455 struct aio_ctx *ctx = opaque;
1456 struct timeval t2;
1457
1458 gettimeofday(&t2, NULL);
1459
1460 if (ret < 0) {
1461 printf("readv failed: %s\n", strerror(-ret));
1462 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1463 goto out;
1464 }
1465
1466 if (ctx->Pflag) {
1467 void *cmp_buf = g_malloc(ctx->qiov.size);
1468
1469 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1470 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1471 printf("Pattern verification failed at offset %"
1472 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1473 }
1474 g_free(cmp_buf);
1475 }
1476
1477 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1478
1479 if (ctx->qflag) {
1480 goto out;
1481 }
1482
1483 if (ctx->vflag) {
1484 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1485 }
1486
1487 /* Finally, report back -- -C gives a parsable format */
1488 t2 = tsub(t2, ctx->t1);
1489 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1490 ctx->qiov.size, 1, ctx->Cflag);
1491 out:
1492 qemu_io_free(ctx->buf);
1493 qemu_iovec_destroy(&ctx->qiov);
1494 g_free(ctx);
1495 }
1496
1497 static void aio_read_help(void)
1498 {
1499 printf(
1500 "\n"
1501 " asynchronously reads a range of bytes from the given offset\n"
1502 "\n"
1503 " Example:\n"
1504 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1505 "\n"
1506 " Reads a segment of the currently open file, optionally dumping it to the\n"
1507 " standard output stream (with -v option) for subsequent inspection.\n"
1508 " The read is performed asynchronously and the aio_flush command must be\n"
1509 " used to ensure all outstanding aio requests have been completed.\n"
1510 " -C, -- report statistics in a machine parsable format\n"
1511 " -P, -- use a pattern to verify read data\n"
1512 " -v, -- dump buffer to standard output\n"
1513 " -q, -- quiet mode, do not show I/O statistics\n"
1514 "\n");
1515 }
1516
1517 static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1518
1519 static const cmdinfo_t aio_read_cmd = {
1520 .name = "aio_read",
1521 .cfunc = aio_read_f,
1522 .argmin = 2,
1523 .argmax = -1,
1524 .args = "[-Cqv] [-P pattern ] off len [len..]",
1525 .oneline = "asynchronously reads a number of bytes",
1526 .help = aio_read_help,
1527 };
1528
1529 static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1530 {
1531 int nr_iov, c;
1532 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1533
1534 ctx->blk = blk;
1535 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
1536 switch (c) {
1537 case 'C':
1538 ctx->Cflag = 1;
1539 break;
1540 case 'P':
1541 ctx->Pflag = 1;
1542 ctx->pattern = parse_pattern(optarg);
1543 if (ctx->pattern < 0) {
1544 g_free(ctx);
1545 return 0;
1546 }
1547 break;
1548 case 'q':
1549 ctx->qflag = 1;
1550 break;
1551 case 'v':
1552 ctx->vflag = 1;
1553 break;
1554 default:
1555 g_free(ctx);
1556 return qemuio_command_usage(&aio_read_cmd);
1557 }
1558 }
1559
1560 if (optind > argc - 2) {
1561 g_free(ctx);
1562 return qemuio_command_usage(&aio_read_cmd);
1563 }
1564
1565 ctx->offset = cvtnum(argv[optind]);
1566 if (ctx->offset < 0) {
1567 print_cvtnum_err(ctx->offset, argv[optind]);
1568 g_free(ctx);
1569 return 0;
1570 }
1571 optind++;
1572
1573 if (ctx->offset & 0x1ff) {
1574 printf("offset %" PRId64 " is not sector aligned\n",
1575 ctx->offset);
1576 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1577 g_free(ctx);
1578 return 0;
1579 }
1580
1581 nr_iov = argc - optind;
1582 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1583 if (ctx->buf == NULL) {
1584 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1585 g_free(ctx);
1586 return 0;
1587 }
1588
1589 gettimeofday(&ctx->t1, NULL);
1590 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1591 BLOCK_ACCT_READ);
1592 blk_aio_readv(blk, ctx->offset >> 9, &ctx->qiov,
1593 ctx->qiov.size >> 9, aio_read_done, ctx);
1594 return 0;
1595 }
1596
1597 static void aio_write_help(void)
1598 {
1599 printf(
1600 "\n"
1601 " asynchronously writes a range of bytes from the given offset source\n"
1602 " from multiple buffers\n"
1603 "\n"
1604 " Example:\n"
1605 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1606 "\n"
1607 " Writes into a segment of the currently open file, using a buffer\n"
1608 " filled with a set pattern (0xcdcdcdcd).\n"
1609 " The write is performed asynchronously and the aio_flush command must be\n"
1610 " used to ensure all outstanding aio requests have been completed.\n"
1611 " -P, -- use different pattern to fill file\n"
1612 " -C, -- report statistics in a machine parsable format\n"
1613 " -q, -- quiet mode, do not show I/O statistics\n"
1614 "\n");
1615 }
1616
1617 static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1618
1619 static const cmdinfo_t aio_write_cmd = {
1620 .name = "aio_write",
1621 .cfunc = aio_write_f,
1622 .argmin = 2,
1623 .argmax = -1,
1624 .args = "[-Cq] [-P pattern ] off len [len..]",
1625 .oneline = "asynchronously writes a number of bytes",
1626 .help = aio_write_help,
1627 };
1628
1629 static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1630 {
1631 int nr_iov, c;
1632 int pattern = 0xcd;
1633 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1634
1635 ctx->blk = blk;
1636 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1637 switch (c) {
1638 case 'C':
1639 ctx->Cflag = 1;
1640 break;
1641 case 'q':
1642 ctx->qflag = 1;
1643 break;
1644 case 'P':
1645 pattern = parse_pattern(optarg);
1646 if (pattern < 0) {
1647 g_free(ctx);
1648 return 0;
1649 }
1650 break;
1651 default:
1652 g_free(ctx);
1653 return qemuio_command_usage(&aio_write_cmd);
1654 }
1655 }
1656
1657 if (optind > argc - 2) {
1658 g_free(ctx);
1659 return qemuio_command_usage(&aio_write_cmd);
1660 }
1661
1662 ctx->offset = cvtnum(argv[optind]);
1663 if (ctx->offset < 0) {
1664 print_cvtnum_err(ctx->offset, argv[optind]);
1665 g_free(ctx);
1666 return 0;
1667 }
1668 optind++;
1669
1670 if (ctx->offset & 0x1ff) {
1671 printf("offset %" PRId64 " is not sector aligned\n",
1672 ctx->offset);
1673 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1674 g_free(ctx);
1675 return 0;
1676 }
1677
1678 nr_iov = argc - optind;
1679 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, pattern);
1680 if (ctx->buf == NULL) {
1681 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1682 g_free(ctx);
1683 return 0;
1684 }
1685
1686 gettimeofday(&ctx->t1, NULL);
1687 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1688 BLOCK_ACCT_WRITE);
1689 blk_aio_writev(blk, ctx->offset >> 9, &ctx->qiov,
1690 ctx->qiov.size >> 9, aio_write_done, ctx);
1691 return 0;
1692 }
1693
1694 static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1695 {
1696 BlockAcctCookie cookie;
1697 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
1698 blk_drain_all();
1699 block_acct_done(blk_get_stats(blk), &cookie);
1700 return 0;
1701 }
1702
1703 static const cmdinfo_t aio_flush_cmd = {
1704 .name = "aio_flush",
1705 .cfunc = aio_flush_f,
1706 .oneline = "completes all outstanding aio requests"
1707 };
1708
1709 static int flush_f(BlockBackend *blk, int argc, char **argv)
1710 {
1711 blk_flush(blk);
1712 return 0;
1713 }
1714
1715 static const cmdinfo_t flush_cmd = {
1716 .name = "flush",
1717 .altname = "f",
1718 .cfunc = flush_f,
1719 .oneline = "flush all in-core file state to disk",
1720 };
1721
1722 static int truncate_f(BlockBackend *blk, int argc, char **argv)
1723 {
1724 int64_t offset;
1725 int ret;
1726
1727 offset = cvtnum(argv[1]);
1728 if (offset < 0) {
1729 print_cvtnum_err(offset, argv[1]);
1730 return 0;
1731 }
1732
1733 ret = blk_truncate(blk, offset);
1734 if (ret < 0) {
1735 printf("truncate: %s\n", strerror(-ret));
1736 return 0;
1737 }
1738
1739 return 0;
1740 }
1741
1742 static const cmdinfo_t truncate_cmd = {
1743 .name = "truncate",
1744 .altname = "t",
1745 .cfunc = truncate_f,
1746 .argmin = 1,
1747 .argmax = 1,
1748 .args = "off",
1749 .oneline = "truncates the current file at the given offset",
1750 };
1751
1752 static int length_f(BlockBackend *blk, int argc, char **argv)
1753 {
1754 int64_t size;
1755 char s1[64];
1756
1757 size = blk_getlength(blk);
1758 if (size < 0) {
1759 printf("getlength: %s\n", strerror(-size));
1760 return 0;
1761 }
1762
1763 cvtstr(size, s1, sizeof(s1));
1764 printf("%s\n", s1);
1765 return 0;
1766 }
1767
1768
1769 static const cmdinfo_t length_cmd = {
1770 .name = "length",
1771 .altname = "l",
1772 .cfunc = length_f,
1773 .oneline = "gets the length of the current file",
1774 };
1775
1776
1777 static int info_f(BlockBackend *blk, int argc, char **argv)
1778 {
1779 BlockDriverState *bs = blk_bs(blk);
1780 BlockDriverInfo bdi;
1781 ImageInfoSpecific *spec_info;
1782 char s1[64], s2[64];
1783 int ret;
1784
1785 if (bs->drv && bs->drv->format_name) {
1786 printf("format name: %s\n", bs->drv->format_name);
1787 }
1788 if (bs->drv && bs->drv->protocol_name) {
1789 printf("format name: %s\n", bs->drv->protocol_name);
1790 }
1791
1792 ret = bdrv_get_info(bs, &bdi);
1793 if (ret) {
1794 return 0;
1795 }
1796
1797 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1798 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1799
1800 printf("cluster size: %s\n", s1);
1801 printf("vm state offset: %s\n", s2);
1802
1803 spec_info = bdrv_get_specific_info(bs);
1804 if (spec_info) {
1805 printf("Format specific information:\n");
1806 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1807 qapi_free_ImageInfoSpecific(spec_info);
1808 }
1809
1810 return 0;
1811 }
1812
1813
1814
1815 static const cmdinfo_t info_cmd = {
1816 .name = "info",
1817 .altname = "i",
1818 .cfunc = info_f,
1819 .oneline = "prints information about the current file",
1820 };
1821
1822 static void discard_help(void)
1823 {
1824 printf(
1825 "\n"
1826 " discards a range of bytes from the given offset\n"
1827 "\n"
1828 " Example:\n"
1829 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1830 "\n"
1831 " Discards a segment of the currently open file.\n"
1832 " -C, -- report statistics in a machine parsable format\n"
1833 " -q, -- quiet mode, do not show I/O statistics\n"
1834 "\n");
1835 }
1836
1837 static int discard_f(BlockBackend *blk, int argc, char **argv);
1838
1839 static const cmdinfo_t discard_cmd = {
1840 .name = "discard",
1841 .altname = "d",
1842 .cfunc = discard_f,
1843 .argmin = 2,
1844 .argmax = -1,
1845 .args = "[-Cq] off len",
1846 .oneline = "discards a number of bytes at a specified offset",
1847 .help = discard_help,
1848 };
1849
1850 static int discard_f(BlockBackend *blk, int argc, char **argv)
1851 {
1852 struct timeval t1, t2;
1853 int Cflag = 0, qflag = 0;
1854 int c, ret;
1855 int64_t offset, count;
1856
1857 while ((c = getopt(argc, argv, "Cq")) != -1) {
1858 switch (c) {
1859 case 'C':
1860 Cflag = 1;
1861 break;
1862 case 'q':
1863 qflag = 1;
1864 break;
1865 default:
1866 return qemuio_command_usage(&discard_cmd);
1867 }
1868 }
1869
1870 if (optind != argc - 2) {
1871 return qemuio_command_usage(&discard_cmd);
1872 }
1873
1874 offset = cvtnum(argv[optind]);
1875 if (offset < 0) {
1876 print_cvtnum_err(offset, argv[optind]);
1877 return 0;
1878 }
1879
1880 optind++;
1881 count = cvtnum(argv[optind]);
1882 if (count < 0) {
1883 print_cvtnum_err(count, argv[optind]);
1884 return 0;
1885 } else if (count >> BDRV_SECTOR_BITS > INT_MAX) {
1886 printf("length cannot exceed %"PRIu64", given %s\n",
1887 (uint64_t)INT_MAX << BDRV_SECTOR_BITS,
1888 argv[optind]);
1889 return 0;
1890 }
1891
1892 gettimeofday(&t1, NULL);
1893 ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1894 count >> BDRV_SECTOR_BITS);
1895 gettimeofday(&t2, NULL);
1896
1897 if (ret < 0) {
1898 printf("discard failed: %s\n", strerror(-ret));
1899 goto out;
1900 }
1901
1902 /* Finally, report back -- -C gives a parsable format */
1903 if (!qflag) {
1904 t2 = tsub(t2, t1);
1905 print_report("discard", &t2, offset, count, count, 1, Cflag);
1906 }
1907
1908 out:
1909 return 0;
1910 }
1911
1912 static int alloc_f(BlockBackend *blk, int argc, char **argv)
1913 {
1914 BlockDriverState *bs = blk_bs(blk);
1915 int64_t offset, sector_num, nb_sectors, remaining;
1916 char s1[64];
1917 int num, ret;
1918 int64_t sum_alloc;
1919
1920 offset = cvtnum(argv[1]);
1921 if (offset < 0) {
1922 print_cvtnum_err(offset, argv[1]);
1923 return 0;
1924 } else if (offset & 0x1ff) {
1925 printf("offset %" PRId64 " is not sector aligned\n",
1926 offset);
1927 return 0;
1928 }
1929
1930 if (argc == 3) {
1931 nb_sectors = cvtnum(argv[2]);
1932 if (nb_sectors < 0) {
1933 print_cvtnum_err(nb_sectors, argv[2]);
1934 return 0;
1935 } else if (nb_sectors > INT_MAX) {
1936 printf("length argument cannot exceed %d, given %s\n",
1937 INT_MAX, argv[2]);
1938 return 0;
1939 }
1940 } else {
1941 nb_sectors = 1;
1942 }
1943
1944 remaining = nb_sectors;
1945 sum_alloc = 0;
1946 sector_num = offset >> 9;
1947 while (remaining) {
1948 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1949 if (ret < 0) {
1950 printf("is_allocated failed: %s\n", strerror(-ret));
1951 return 0;
1952 }
1953 sector_num += num;
1954 remaining -= num;
1955 if (ret) {
1956 sum_alloc += num;
1957 }
1958 if (num == 0) {
1959 nb_sectors -= remaining;
1960 remaining = 0;
1961 }
1962 }
1963
1964 cvtstr(offset, s1, sizeof(s1));
1965
1966 printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
1967 sum_alloc, nb_sectors, s1);
1968 return 0;
1969 }
1970
1971 static const cmdinfo_t alloc_cmd = {
1972 .name = "alloc",
1973 .altname = "a",
1974 .argmin = 1,
1975 .argmax = 2,
1976 .cfunc = alloc_f,
1977 .args = "off [sectors]",
1978 .oneline = "checks if a sector is present in the file",
1979 };
1980
1981
1982 static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1983 int64_t nb_sectors, int64_t *pnum)
1984 {
1985 int num, num_checked;
1986 int ret, firstret;
1987
1988 num_checked = MIN(nb_sectors, INT_MAX);
1989 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1990 if (ret < 0) {
1991 return ret;
1992 }
1993
1994 firstret = ret;
1995 *pnum = num;
1996
1997 while (nb_sectors > 0 && ret == firstret) {
1998 sector_num += num;
1999 nb_sectors -= num;
2000
2001 num_checked = MIN(nb_sectors, INT_MAX);
2002 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
2003 if (ret == firstret && num) {
2004 *pnum += num;
2005 } else {
2006 break;
2007 }
2008 }
2009
2010 return firstret;
2011 }
2012
2013 static int map_f(BlockBackend *blk, int argc, char **argv)
2014 {
2015 int64_t offset;
2016 int64_t nb_sectors, total_sectors;
2017 char s1[64];
2018 int64_t num;
2019 int ret;
2020 const char *retstr;
2021
2022 offset = 0;
2023 total_sectors = blk_nb_sectors(blk);
2024 if (total_sectors < 0) {
2025 error_report("Failed to query image length: %s",
2026 strerror(-total_sectors));
2027 return 0;
2028 }
2029
2030 nb_sectors = total_sectors;
2031
2032 do {
2033 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
2034 if (ret < 0) {
2035 error_report("Failed to get allocation status: %s", strerror(-ret));
2036 return 0;
2037 } else if (!num) {
2038 error_report("Unexpected end of image");
2039 return 0;
2040 }
2041
2042 retstr = ret ? " allocated" : "not allocated";
2043 cvtstr(offset << 9ULL, s1, sizeof(s1));
2044 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
2045 "at offset %s (%d)\n",
2046 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
2047
2048 offset += num;
2049 nb_sectors -= num;
2050 } while (offset < total_sectors);
2051
2052 return 0;
2053 }
2054
2055 static const cmdinfo_t map_cmd = {
2056 .name = "map",
2057 .argmin = 0,
2058 .argmax = 0,
2059 .cfunc = map_f,
2060 .args = "",
2061 .oneline = "prints the allocated areas of a file",
2062 };
2063
2064 static void reopen_help(void)
2065 {
2066 printf(
2067 "\n"
2068 " Changes the open options of an already opened image\n"
2069 "\n"
2070 " Example:\n"
2071 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2072 "\n"
2073 " -r, -- Reopen the image read-only\n"
2074 " -c, -- Change the cache mode to the given value\n"
2075 " -o, -- Changes block driver options (cf. 'open' command)\n"
2076 "\n");
2077 }
2078
2079 static int reopen_f(BlockBackend *blk, int argc, char **argv);
2080
2081 static QemuOptsList reopen_opts = {
2082 .name = "reopen",
2083 .merge_lists = true,
2084 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2085 .desc = {
2086 /* no elements => accept any params */
2087 { /* end of list */ }
2088 },
2089 };
2090
2091 static const cmdinfo_t reopen_cmd = {
2092 .name = "reopen",
2093 .argmin = 0,
2094 .argmax = -1,
2095 .cfunc = reopen_f,
2096 .args = "[-r] [-c cache] [-o options]",
2097 .oneline = "reopens an image with new options",
2098 .help = reopen_help,
2099 };
2100
2101 static int reopen_f(BlockBackend *blk, int argc, char **argv)
2102 {
2103 BlockDriverState *bs = blk_bs(blk);
2104 QemuOpts *qopts;
2105 QDict *opts;
2106 int c;
2107 int flags = bs->open_flags;
2108
2109 BlockReopenQueue *brq;
2110 Error *local_err = NULL;
2111
2112 while ((c = getopt(argc, argv, "c:o:r")) != -1) {
2113 switch (c) {
2114 case 'c':
2115 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
2116 error_report("Invalid cache option: %s", optarg);
2117 return 0;
2118 }
2119 break;
2120 case 'o':
2121 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2122 qemu_opts_reset(&reopen_opts);
2123 return 0;
2124 }
2125 break;
2126 case 'r':
2127 flags &= ~BDRV_O_RDWR;
2128 break;
2129 default:
2130 qemu_opts_reset(&reopen_opts);
2131 return qemuio_command_usage(&reopen_cmd);
2132 }
2133 }
2134
2135 if (optind != argc) {
2136 qemu_opts_reset(&reopen_opts);
2137 return qemuio_command_usage(&reopen_cmd);
2138 }
2139
2140 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);
2148 }
2149
2150 return 0;
2151 }
2152
2153 static int break_f(BlockBackend *blk, int argc, char **argv)
2154 {
2155 int ret;
2156
2157 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2158 if (ret < 0) {
2159 printf("Could not set breakpoint: %s\n", strerror(-ret));
2160 }
2161
2162 return 0;
2163 }
2164
2165 static int remove_break_f(BlockBackend *blk, int argc, char **argv)
2166 {
2167 int ret;
2168
2169 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
2170 if (ret < 0) {
2171 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2172 }
2173
2174 return 0;
2175 }
2176
2177 static const cmdinfo_t break_cmd = {
2178 .name = "break",
2179 .argmin = 2,
2180 .argmax = 2,
2181 .cfunc = break_f,
2182 .args = "event tag",
2183 .oneline = "sets a breakpoint on event and tags the stopped "
2184 "request as tag",
2185 };
2186
2187 static const cmdinfo_t remove_break_cmd = {
2188 .name = "remove_break",
2189 .argmin = 1,
2190 .argmax = 1,
2191 .cfunc = remove_break_f,
2192 .args = "tag",
2193 .oneline = "remove a breakpoint by tag",
2194 };
2195
2196 static int resume_f(BlockBackend *blk, int argc, char **argv)
2197 {
2198 int ret;
2199
2200 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2201 if (ret < 0) {
2202 printf("Could not resume request: %s\n", strerror(-ret));
2203 }
2204
2205 return 0;
2206 }
2207
2208 static const cmdinfo_t resume_cmd = {
2209 .name = "resume",
2210 .argmin = 1,
2211 .argmax = 1,
2212 .cfunc = resume_f,
2213 .args = "tag",
2214 .oneline = "resumes the request tagged as tag",
2215 };
2216
2217 static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2218 {
2219 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2220 aio_poll(blk_get_aio_context(blk), true);
2221 }
2222
2223 return 0;
2224 }
2225
2226 static const cmdinfo_t wait_break_cmd = {
2227 .name = "wait_break",
2228 .argmin = 1,
2229 .argmax = 1,
2230 .cfunc = wait_break_f,
2231 .args = "tag",
2232 .oneline = "waits for the suspension of a request",
2233 };
2234
2235 static int abort_f(BlockBackend *blk, int argc, char **argv)
2236 {
2237 abort();
2238 }
2239
2240 static const cmdinfo_t abort_cmd = {
2241 .name = "abort",
2242 .cfunc = abort_f,
2243 .flags = CMD_NOFILE_OK,
2244 .oneline = "simulate a program crash using abort(3)",
2245 };
2246
2247 static void sigraise_help(void)
2248 {
2249 printf(
2250 "\n"
2251 " raises the given signal\n"
2252 "\n"
2253 " Example:\n"
2254 " 'sigraise %i' - raises SIGTERM\n"
2255 "\n"
2256 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2257 " given to sigraise.\n"
2258 "\n", SIGTERM);
2259 }
2260
2261 static int sigraise_f(BlockBackend *blk, int argc, char **argv);
2262
2263 static const cmdinfo_t sigraise_cmd = {
2264 .name = "sigraise",
2265 .cfunc = sigraise_f,
2266 .argmin = 1,
2267 .argmax = 1,
2268 .flags = CMD_NOFILE_OK,
2269 .args = "signal",
2270 .oneline = "raises a signal",
2271 .help = sigraise_help,
2272 };
2273
2274 static int sigraise_f(BlockBackend *blk, int argc, char **argv)
2275 {
2276 int64_t sig = cvtnum(argv[1]);
2277 if (sig < 0) {
2278 print_cvtnum_err(sig, argv[1]);
2279 return 0;
2280 } else if (sig > NSIG) {
2281 printf("signal argument '%s' is too large to be a valid signal\n",
2282 argv[1]);
2283 return 0;
2284 }
2285
2286 /* Using raise() to kill this process does not necessarily flush all open
2287 * streams. At least stdout and stderr (although the latter should be
2288 * non-buffered anyway) should be flushed, though. */
2289 fflush(stdout);
2290 fflush(stderr);
2291
2292 raise(sig);
2293 return 0;
2294 }
2295
2296 static void sleep_cb(void *opaque)
2297 {
2298 bool *expired = opaque;
2299 *expired = true;
2300 }
2301
2302 static int sleep_f(BlockBackend *blk, int argc, char **argv)
2303 {
2304 char *endptr;
2305 long ms;
2306 struct QEMUTimer *timer;
2307 bool expired = false;
2308
2309 ms = strtol(argv[1], &endptr, 0);
2310 if (ms < 0 || *endptr != '\0') {
2311 printf("%s is not a valid number\n", argv[1]);
2312 return 0;
2313 }
2314
2315 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2316 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2317
2318 while (!expired) {
2319 main_loop_wait(false);
2320 }
2321
2322 timer_free(timer);
2323
2324 return 0;
2325 }
2326
2327 static const cmdinfo_t sleep_cmd = {
2328 .name = "sleep",
2329 .argmin = 1,
2330 .argmax = 1,
2331 .cfunc = sleep_f,
2332 .flags = CMD_NOFILE_OK,
2333 .oneline = "waits for the given value in milliseconds",
2334 };
2335
2336 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2337 {
2338 if (cmd) {
2339 printf("%s ", cmd);
2340 } else {
2341 printf("%s ", ct->name);
2342 if (ct->altname) {
2343 printf("(or %s) ", ct->altname);
2344 }
2345 }
2346
2347 if (ct->args) {
2348 printf("%s ", ct->args);
2349 }
2350 printf("-- %s\n", ct->oneline);
2351 }
2352
2353 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2354 {
2355 help_oneline(cmd, ct);
2356 if (ct->help) {
2357 ct->help();
2358 }
2359 }
2360
2361 static void help_all(void)
2362 {
2363 const cmdinfo_t *ct;
2364
2365 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2366 help_oneline(ct->name, ct);
2367 }
2368 printf("\nUse 'help commandname' for extended help.\n");
2369 }
2370
2371 static int help_f(BlockBackend *blk, int argc, char **argv)
2372 {
2373 const cmdinfo_t *ct;
2374
2375 if (argc == 1) {
2376 help_all();
2377 return 0;
2378 }
2379
2380 ct = find_command(argv[1]);
2381 if (ct == NULL) {
2382 printf("command %s not found\n", argv[1]);
2383 return 0;
2384 }
2385
2386 help_onecmd(argv[1], ct);
2387 return 0;
2388 }
2389
2390 static const cmdinfo_t help_cmd = {
2391 .name = "help",
2392 .altname = "?",
2393 .cfunc = help_f,
2394 .argmin = 0,
2395 .argmax = 1,
2396 .flags = CMD_FLAG_GLOBAL,
2397 .args = "[command]",
2398 .oneline = "help for one or all commands",
2399 };
2400
2401 bool qemuio_command(BlockBackend *blk, const char *cmd)
2402 {
2403 char *input;
2404 const cmdinfo_t *ct;
2405 char **v;
2406 int c;
2407 bool done = false;
2408
2409 input = g_strdup(cmd);
2410 v = breakline(input, &c);
2411 if (c) {
2412 ct = find_command(v[0]);
2413 if (ct) {
2414 done = command(blk, ct, c, v);
2415 } else {
2416 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2417 }
2418 }
2419 g_free(input);
2420 g_free(v);
2421
2422 return done;
2423 }
2424
2425 static void __attribute((constructor)) init_qemuio_commands(void)
2426 {
2427 /* initialize commands */
2428 qemuio_add_command(&help_cmd);
2429 qemuio_add_command(&read_cmd);
2430 qemuio_add_command(&readv_cmd);
2431 qemuio_add_command(&write_cmd);
2432 qemuio_add_command(&writev_cmd);
2433 qemuio_add_command(&multiwrite_cmd);
2434 qemuio_add_command(&aio_read_cmd);
2435 qemuio_add_command(&aio_write_cmd);
2436 qemuio_add_command(&aio_flush_cmd);
2437 qemuio_add_command(&flush_cmd);
2438 qemuio_add_command(&truncate_cmd);
2439 qemuio_add_command(&length_cmd);
2440 qemuio_add_command(&info_cmd);
2441 qemuio_add_command(&discard_cmd);
2442 qemuio_add_command(&alloc_cmd);
2443 qemuio_add_command(&map_cmd);
2444 qemuio_add_command(&reopen_cmd);
2445 qemuio_add_command(&break_cmd);
2446 qemuio_add_command(&remove_break_cmd);
2447 qemuio_add_command(&resume_cmd);
2448 qemuio_add_command(&wait_break_cmd);
2449 qemuio_add_command(&abort_cmd);
2450 qemuio_add_command(&sleep_cmd);
2451 qemuio_add_command(&sigraise_cmd);
2452 }