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