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