onenand: Switch to byte-based block access
[qemu/ar7.git] / qemu-io-cmds.c
blob3040a4b12e8dfce73bdf43eb765095b80679c4b9
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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 */
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"
24 #define CMD_NOFILE_OK 0x01
26 bool qemuio_misalign;
28 static cmdinfo_t *cmdtab;
29 static int ncmds;
31 static int compare_cmdname(const void *a, const void *b)
33 return strcmp(((const cmdinfo_t *)a)->name,
34 ((const cmdinfo_t *)b)->name);
37 void qemuio_add_command(const cmdinfo_t *ci)
39 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
40 cmdtab[ncmds - 1] = *ci;
41 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
44 int qemuio_command_usage(const cmdinfo_t *ci)
46 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
47 return 0;
50 static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
52 if (ct->flags & CMD_FLAG_GLOBAL) {
53 return 1;
55 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
56 fprintf(stderr, "no file open, try 'help open'\n");
57 return 0;
59 return 1;
62 static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
63 char **argv)
65 char *cmd = argv[0];
67 if (!init_check_command(blk, ct)) {
68 return 0;
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);
85 return 0;
87 optind = 0;
88 return ct->cfunc(blk, argc, argv);
91 static const cmdinfo_t *find_command(const char *cmd)
93 cmdinfo_t *ct;
95 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
96 if (strcmp(ct->name, cmd) == 0 ||
97 (ct->altname && strcmp(ct->altname, cmd) == 0))
99 return (const cmdinfo_t *)ct;
102 return NULL;
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)
110 cmdinfo_t *ct;
111 size_t input_len = strlen(input);
113 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
114 if (strncmp(input, ct->name, input_len) == 0) {
115 fn(ct->name, opaque);
120 static char **breakline(char *input, int *count)
122 int c = 0;
123 char *p;
124 char **rval = g_new0(char *, 1);
126 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
127 if (!*p) {
128 continue;
130 c++;
131 rval = g_renew(char *, rval, (c + 1));
132 rval[c - 1] = p;
133 rval[c] = NULL;
135 *count = c;
136 return rval;
139 static int64_t cvtnum(const char *s)
141 char *end;
142 int64_t ret;
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;
149 return ret;
152 static void print_cvtnum_err(int64_t rc, const char *arg)
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);
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)
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))
181 static void cvtstr(double value, char *str, size_t size)
183 char *trim;
184 const char *suffix;
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);
209 trim = strstr(str, ".000");
210 if (trim) {
211 strcpy(trim, suffix);
212 } else {
213 strcat(str, suffix);
219 static struct timeval tsub(struct timeval t1, struct timeval t2)
221 t1.tv_usec -= t2.tv_usec;
222 if (t1.tv_usec < 0) {
223 t1.tv_usec += 1000000;
224 t1.tv_sec--;
226 t1.tv_sec -= t2.tv_sec;
227 return t1;
230 static double tdiv(double value, struct timeval tv)
232 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
235 #define HOURS(sec) ((sec) / (60 * 60))
236 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
237 #define SECONDS(sec) ((sec) % 60)
239 enum {
240 DEFAULT_TIME = 0x0,
241 TERSE_FIXED_TIME = 0x1,
242 VERBOSE_FIXED_TIME = 0x2,
245 static void timestr(struct timeval *tv, char *ts, size_t size, int format)
247 double usec = (double)tv->tv_usec / 1000000.0;
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;
257 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
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));
272 * Parse the pattern argument to various sub-commands.
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.
277 static int parse_pattern(const char *arg)
279 char *endptr = NULL;
280 long pattern;
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;
288 return pattern;
292 * Memory allocation helpers.
294 * Make sure memory is aligned by default, or purposefully misaligned if
295 * that is specified on the command line.
298 #define MISALIGN_OFFSET 16
299 static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
301 void *buf;
303 if (qemuio_misalign) {
304 len += MISALIGN_OFFSET;
306 buf = blk_blockalign(blk, len);
307 memset(buf, pattern, len);
308 if (qemuio_misalign) {
309 buf += MISALIGN_OFFSET;
311 return buf;
314 static void qemu_io_free(void *p)
316 if (qemuio_misalign) {
317 p -= MISALIGN_OFFSET;
319 qemu_vfree(p);
322 static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
324 uint64_t i;
325 int j;
326 const uint8_t *p;
328 for (i = 0, p = buffer; i < len; i += 16) {
329 const uint8_t *s = p;
331 printf("%08" PRIx64 ": ", offset + i);
332 for (j = 0; j < 16 && i + j < len; j++, p++) {
333 printf("%02x ", *p);
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(".");
343 printf("\n");
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)
350 char s1[64], s2[64], ts[64];
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));
369 * Parse multiple length statements for vectored I/O, and construct an I/O
370 * vector matching it.
372 static void *
373 create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
374 int pattern)
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;
382 for (i = 0; i < nr_iov; i++) {
383 char *arg = argv[i];
384 int64_t len;
386 len = cvtnum(arg);
387 if (len < 0) {
388 print_cvtnum_err(len, arg);
389 goto fail;
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;
398 if (len & 0x1ff) {
399 printf("length argument %" PRId64
400 " is not sector aligned\n", len);
401 goto fail;
404 sizes[i] = len;
405 count += len;
408 qemu_iovec_init(qiov, nr_iov);
410 buf = p = qemu_io_alloc(blk, count, pattern);
412 for (i = 0; i < nr_iov; i++) {
413 qemu_iovec_add(qiov, p, sizes[i]);
414 p += sizes[i];
417 fail:
418 g_free(sizes);
419 return buf;
422 static int do_read(BlockBackend *blk, char *buf, int64_t offset, int64_t count,
423 int64_t *total)
425 int ret;
427 if (count >> 9 > INT_MAX) {
428 return -ERANGE;
431 ret = blk_read(blk, offset >> 9, (uint8_t *)buf, count >> 9);
432 if (ret < 0) {
433 return ret;
435 *total = count;
436 return 1;
439 static int do_write(BlockBackend *blk, char *buf, int64_t offset, int64_t count,
440 int64_t *total)
442 int ret;
444 if (count >> 9 > INT_MAX) {
445 return -ERANGE;
448 ret = blk_write(blk, offset >> 9, (uint8_t *)buf, count >> 9);
449 if (ret < 0) {
450 return ret;
452 *total = count;
453 return 1;
456 static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
457 int64_t count, int64_t *total)
459 if (count > INT_MAX) {
460 return -ERANGE;
463 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
464 if (*total < 0) {
465 return *total;
467 return 1;
470 static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
471 int64_t count, int64_t *total)
473 if (count > INT_MAX) {
474 return -ERANGE;
477 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count, 0);
478 if (*total < 0) {
479 return *total;
481 return 1;
484 typedef struct {
485 BlockBackend *blk;
486 int64_t offset;
487 int64_t count;
488 int64_t *total;
489 int ret;
490 bool done;
491 } CoWriteZeroes;
493 static void coroutine_fn co_write_zeroes_entry(void *opaque)
495 CoWriteZeroes *data = opaque;
497 data->ret = blk_co_write_zeroes(data->blk, data->offset, data->count, 0);
498 data->done = true;
499 if (data->ret < 0) {
500 *data->total = data->ret;
501 return;
504 *data->total = data->count;
507 static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int64_t count,
508 int64_t *total)
510 Coroutine *co;
511 CoWriteZeroes data = {
512 .blk = blk,
513 .offset = offset,
514 .count = count,
515 .total = total,
516 .done = false,
519 if (count >> BDRV_SECTOR_BITS > INT_MAX) {
520 return -ERANGE;
523 co = qemu_coroutine_create(co_write_zeroes_entry);
524 qemu_coroutine_enter(co, &data);
525 while (!data.done) {
526 aio_poll(blk_get_aio_context(blk), true);
528 if (data.ret < 0) {
529 return data.ret;
530 } else {
531 return 1;
535 static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
536 int64_t count, int64_t *total)
538 int ret;
540 if (count >> 9 > INT_MAX) {
541 return -ERANGE;
544 ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
545 if (ret < 0) {
546 return ret;
548 *total = count;
549 return 1;
552 static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
553 int64_t count, int64_t *total)
555 if (count > INT_MAX) {
556 return -ERANGE;
559 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
560 if (*total < 0) {
561 return *total;
563 return 1;
566 static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
567 int64_t count, int64_t *total)
569 if (count > INT_MAX) {
570 return -ERANGE;
573 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
574 if (*total < 0) {
575 return *total;
577 return 1;
580 #define NOT_DONE 0x7fffffff
581 static void aio_rw_done(void *opaque, int ret)
583 *(int *)opaque = ret;
586 static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
587 int64_t offset, int *total)
589 int async_ret = NOT_DONE;
591 blk_aio_readv(blk, offset >> 9, qiov, qiov->size >> 9,
592 aio_rw_done, &async_ret);
593 while (async_ret == NOT_DONE) {
594 main_loop_wait(false);
597 *total = qiov->size;
598 return async_ret < 0 ? async_ret : 1;
601 static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
602 int64_t offset, int *total)
604 int async_ret = NOT_DONE;
606 blk_aio_writev(blk, offset >> 9, qiov, qiov->size >> 9,
607 aio_rw_done, &async_ret);
608 while (async_ret == NOT_DONE) {
609 main_loop_wait(false);
612 *total = qiov->size;
613 return async_ret < 0 ? async_ret : 1;
616 struct multiwrite_async_ret {
617 int num_done;
618 int error;
621 static void multiwrite_cb(void *opaque, int ret)
623 struct multiwrite_async_ret *async_ret = opaque;
625 async_ret->num_done++;
626 if (ret < 0) {
627 async_ret->error = ret;
631 static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
632 int num_reqs, int *total)
634 int i, ret;
635 struct multiwrite_async_ret async_ret = {
636 .num_done = 0,
637 .error = 0,
640 *total = 0;
641 for (i = 0; i < num_reqs; i++) {
642 reqs[i].cb = multiwrite_cb;
643 reqs[i].opaque = &async_ret;
644 *total += reqs[i].qiov->size;
647 ret = blk_aio_multiwrite(blk, reqs, num_reqs);
648 if (ret < 0) {
649 return ret;
652 while (async_ret.num_done < num_reqs) {
653 main_loop_wait(false);
656 return async_ret.error < 0 ? async_ret.error : 1;
659 static void read_help(void)
661 printf(
662 "\n"
663 " reads a range of bytes from the given offset\n"
664 "\n"
665 " Example:\n"
666 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
667 "\n"
668 " Reads a segment of the currently open file, optionally dumping it to the\n"
669 " standard output stream (with -v option) for subsequent inspection.\n"
670 " -b, -- read from the VM state rather than the virtual disk\n"
671 " -C, -- report statistics in a machine parsable format\n"
672 " -l, -- length for pattern verification (only with -P)\n"
673 " -p, -- use blk_pread to read the file\n"
674 " -P, -- use a pattern to verify read data\n"
675 " -q, -- quiet mode, do not show I/O statistics\n"
676 " -s, -- start offset for pattern verification (only with -P)\n"
677 " -v, -- dump buffer to standard output\n"
678 "\n");
681 static int read_f(BlockBackend *blk, int argc, char **argv);
683 static const cmdinfo_t read_cmd = {
684 .name = "read",
685 .altname = "r",
686 .cfunc = read_f,
687 .argmin = 2,
688 .argmax = -1,
689 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
690 .oneline = "reads a number of bytes at a specified offset",
691 .help = read_help,
694 static int read_f(BlockBackend *blk, int argc, char **argv)
696 struct timeval t1, t2;
697 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
698 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
699 int c, cnt;
700 char *buf;
701 int64_t offset;
702 int64_t count;
703 /* Some compilers get confused and warn if this is not initialized. */
704 int64_t total = 0;
705 int pattern = 0;
706 int64_t pattern_offset = 0, pattern_count = 0;
708 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
709 switch (c) {
710 case 'b':
711 bflag = 1;
712 break;
713 case 'C':
714 Cflag = 1;
715 break;
716 case 'l':
717 lflag = 1;
718 pattern_count = cvtnum(optarg);
719 if (pattern_count < 0) {
720 print_cvtnum_err(pattern_count, optarg);
721 return 0;
723 break;
724 case 'p':
725 pflag = 1;
726 break;
727 case 'P':
728 Pflag = 1;
729 pattern = parse_pattern(optarg);
730 if (pattern < 0) {
731 return 0;
733 break;
734 case 'q':
735 qflag = 1;
736 break;
737 case 's':
738 sflag = 1;
739 pattern_offset = cvtnum(optarg);
740 if (pattern_offset < 0) {
741 print_cvtnum_err(pattern_offset, optarg);
742 return 0;
744 break;
745 case 'v':
746 vflag = 1;
747 break;
748 default:
749 return qemuio_command_usage(&read_cmd);
753 if (optind != argc - 2) {
754 return qemuio_command_usage(&read_cmd);
757 if (bflag && pflag) {
758 printf("-b and -p cannot be specified at the same time\n");
759 return 0;
762 offset = cvtnum(argv[optind]);
763 if (offset < 0) {
764 print_cvtnum_err(offset, argv[optind]);
765 return 0;
768 optind++;
769 count = cvtnum(argv[optind]);
770 if (count < 0) {
771 print_cvtnum_err(count, argv[optind]);
772 return 0;
773 } else if (count > SIZE_MAX) {
774 printf("length cannot exceed %" PRIu64 ", given %s\n",
775 (uint64_t) SIZE_MAX, argv[optind]);
776 return 0;
779 if (!Pflag && (lflag || sflag)) {
780 return qemuio_command_usage(&read_cmd);
783 if (!lflag) {
784 pattern_count = count - pattern_offset;
787 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
788 printf("pattern verification range exceeds end of read data\n");
789 return 0;
792 if (!pflag) {
793 if (offset & 0x1ff) {
794 printf("offset %" PRId64 " is not sector aligned\n",
795 offset);
796 return 0;
798 if (count & 0x1ff) {
799 printf("count %"PRId64" is not sector aligned\n",
800 count);
801 return 0;
805 buf = qemu_io_alloc(blk, count, 0xab);
807 gettimeofday(&t1, NULL);
808 if (pflag) {
809 cnt = do_pread(blk, buf, offset, count, &total);
810 } else if (bflag) {
811 cnt = do_load_vmstate(blk, buf, offset, count, &total);
812 } else {
813 cnt = do_read(blk, buf, offset, count, &total);
815 gettimeofday(&t2, NULL);
817 if (cnt < 0) {
818 printf("read failed: %s\n", strerror(-cnt));
819 goto out;
822 if (Pflag) {
823 void *cmp_buf = g_malloc(pattern_count);
824 memset(cmp_buf, pattern, pattern_count);
825 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
826 printf("Pattern verification failed at offset %"
827 PRId64 ", %"PRId64" bytes\n",
828 offset + pattern_offset, pattern_count);
830 g_free(cmp_buf);
833 if (qflag) {
834 goto out;
837 if (vflag) {
838 dump_buffer(buf, offset, count);
841 /* Finally, report back -- -C gives a parsable format */
842 t2 = tsub(t2, t1);
843 print_report("read", &t2, offset, count, total, cnt, Cflag);
845 out:
846 qemu_io_free(buf);
848 return 0;
851 static void readv_help(void)
853 printf(
854 "\n"
855 " reads a range of bytes from the given offset into multiple buffers\n"
856 "\n"
857 " Example:\n"
858 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
859 "\n"
860 " Reads a segment of the currently open file, optionally dumping it to the\n"
861 " standard output stream (with -v option) for subsequent inspection.\n"
862 " Uses multiple iovec buffers if more than one byte range is specified.\n"
863 " -C, -- report statistics in a machine parsable format\n"
864 " -P, -- use a pattern to verify read data\n"
865 " -v, -- dump buffer to standard output\n"
866 " -q, -- quiet mode, do not show I/O statistics\n"
867 "\n");
870 static int readv_f(BlockBackend *blk, int argc, char **argv);
872 static const cmdinfo_t readv_cmd = {
873 .name = "readv",
874 .cfunc = readv_f,
875 .argmin = 2,
876 .argmax = -1,
877 .args = "[-Cqv] [-P pattern ] off len [len..]",
878 .oneline = "reads a number of bytes at a specified offset",
879 .help = readv_help,
882 static int readv_f(BlockBackend *blk, int argc, char **argv)
884 struct timeval t1, t2;
885 int Cflag = 0, qflag = 0, vflag = 0;
886 int c, cnt;
887 char *buf;
888 int64_t offset;
889 /* Some compilers get confused and warn if this is not initialized. */
890 int total = 0;
891 int nr_iov;
892 QEMUIOVector qiov;
893 int pattern = 0;
894 int Pflag = 0;
896 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
897 switch (c) {
898 case 'C':
899 Cflag = 1;
900 break;
901 case 'P':
902 Pflag = 1;
903 pattern = parse_pattern(optarg);
904 if (pattern < 0) {
905 return 0;
907 break;
908 case 'q':
909 qflag = 1;
910 break;
911 case 'v':
912 vflag = 1;
913 break;
914 default:
915 return qemuio_command_usage(&readv_cmd);
919 if (optind > argc - 2) {
920 return qemuio_command_usage(&readv_cmd);
924 offset = cvtnum(argv[optind]);
925 if (offset < 0) {
926 print_cvtnum_err(offset, argv[optind]);
927 return 0;
929 optind++;
931 if (offset & 0x1ff) {
932 printf("offset %" PRId64 " is not sector aligned\n",
933 offset);
934 return 0;
937 nr_iov = argc - optind;
938 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
939 if (buf == NULL) {
940 return 0;
943 gettimeofday(&t1, NULL);
944 cnt = do_aio_readv(blk, &qiov, offset, &total);
945 gettimeofday(&t2, NULL);
947 if (cnt < 0) {
948 printf("readv failed: %s\n", strerror(-cnt));
949 goto out;
952 if (Pflag) {
953 void *cmp_buf = g_malloc(qiov.size);
954 memset(cmp_buf, pattern, qiov.size);
955 if (memcmp(buf, cmp_buf, qiov.size)) {
956 printf("Pattern verification failed at offset %"
957 PRId64 ", %zd bytes\n", offset, qiov.size);
959 g_free(cmp_buf);
962 if (qflag) {
963 goto out;
966 if (vflag) {
967 dump_buffer(buf, offset, qiov.size);
970 /* Finally, report back -- -C gives a parsable format */
971 t2 = tsub(t2, t1);
972 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
974 out:
975 qemu_iovec_destroy(&qiov);
976 qemu_io_free(buf);
977 return 0;
980 static void write_help(void)
982 printf(
983 "\n"
984 " writes a range of bytes from the given offset\n"
985 "\n"
986 " Example:\n"
987 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
988 "\n"
989 " Writes into a segment of the currently open file, using a buffer\n"
990 " filled with a set pattern (0xcdcdcdcd).\n"
991 " -b, -- write to the VM state rather than the virtual disk\n"
992 " -c, -- write compressed data with blk_write_compressed\n"
993 " -p, -- use blk_pwrite to write the file\n"
994 " -P, -- use different pattern to fill file\n"
995 " -C, -- report statistics in a machine parsable format\n"
996 " -q, -- quiet mode, do not show I/O statistics\n"
997 " -z, -- write zeroes using blk_co_write_zeroes\n"
998 "\n");
1001 static int write_f(BlockBackend *blk, int argc, char **argv);
1003 static const cmdinfo_t write_cmd = {
1004 .name = "write",
1005 .altname = "w",
1006 .cfunc = write_f,
1007 .argmin = 2,
1008 .argmax = -1,
1009 .args = "[-bcCpqz] [-P pattern ] off len",
1010 .oneline = "writes a number of bytes at a specified offset",
1011 .help = write_help,
1014 static int write_f(BlockBackend *blk, int argc, char **argv)
1016 struct timeval t1, t2;
1017 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
1018 int cflag = 0;
1019 int c, cnt;
1020 char *buf = NULL;
1021 int64_t offset;
1022 int64_t count;
1023 /* Some compilers get confused and warn if this is not initialized. */
1024 int64_t total = 0;
1025 int pattern = 0xcd;
1027 while ((c = getopt(argc, argv, "bcCpP:qz")) != -1) {
1028 switch (c) {
1029 case 'b':
1030 bflag = 1;
1031 break;
1032 case 'c':
1033 cflag = 1;
1034 break;
1035 case 'C':
1036 Cflag = 1;
1037 break;
1038 case 'p':
1039 pflag = 1;
1040 break;
1041 case 'P':
1042 Pflag = 1;
1043 pattern = parse_pattern(optarg);
1044 if (pattern < 0) {
1045 return 0;
1047 break;
1048 case 'q':
1049 qflag = 1;
1050 break;
1051 case 'z':
1052 zflag = 1;
1053 break;
1054 default:
1055 return qemuio_command_usage(&write_cmd);
1059 if (optind != argc - 2) {
1060 return qemuio_command_usage(&write_cmd);
1063 if (bflag + pflag + zflag > 1) {
1064 printf("-b, -p, or -z cannot be specified at the same time\n");
1065 return 0;
1068 if (zflag && Pflag) {
1069 printf("-z and -P cannot be specified at the same time\n");
1070 return 0;
1073 offset = cvtnum(argv[optind]);
1074 if (offset < 0) {
1075 print_cvtnum_err(offset, argv[optind]);
1076 return 0;
1079 optind++;
1080 count = cvtnum(argv[optind]);
1081 if (count < 0) {
1082 print_cvtnum_err(count, argv[optind]);
1083 return 0;
1084 } else if (count > SIZE_MAX) {
1085 printf("length cannot exceed %" PRIu64 ", given %s\n",
1086 (uint64_t) SIZE_MAX, argv[optind]);
1087 return 0;
1090 if (!pflag) {
1091 if (offset & 0x1ff) {
1092 printf("offset %" PRId64 " is not sector aligned\n",
1093 offset);
1094 return 0;
1097 if (count & 0x1ff) {
1098 printf("count %"PRId64" is not sector aligned\n",
1099 count);
1100 return 0;
1104 if (!zflag) {
1105 buf = qemu_io_alloc(blk, count, pattern);
1108 gettimeofday(&t1, NULL);
1109 if (pflag) {
1110 cnt = do_pwrite(blk, buf, offset, count, &total);
1111 } else if (bflag) {
1112 cnt = do_save_vmstate(blk, buf, offset, count, &total);
1113 } else if (zflag) {
1114 cnt = do_co_write_zeroes(blk, offset, count, &total);
1115 } else if (cflag) {
1116 cnt = do_write_compressed(blk, buf, offset, count, &total);
1117 } else {
1118 cnt = do_write(blk, buf, offset, count, &total);
1120 gettimeofday(&t2, NULL);
1122 if (cnt < 0) {
1123 printf("write failed: %s\n", strerror(-cnt));
1124 goto out;
1127 if (qflag) {
1128 goto out;
1131 /* Finally, report back -- -C gives a parsable format */
1132 t2 = tsub(t2, t1);
1133 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1135 out:
1136 if (!zflag) {
1137 qemu_io_free(buf);
1140 return 0;
1143 static void
1144 writev_help(void)
1146 printf(
1147 "\n"
1148 " writes a range of bytes from the given offset source from multiple buffers\n"
1149 "\n"
1150 " Example:\n"
1151 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1152 "\n"
1153 " Writes into a segment of the currently open file, using a buffer\n"
1154 " filled with a set pattern (0xcdcdcdcd).\n"
1155 " -P, -- use different pattern to fill file\n"
1156 " -C, -- report statistics in a machine parsable format\n"
1157 " -q, -- quiet mode, do not show I/O statistics\n"
1158 "\n");
1161 static int writev_f(BlockBackend *blk, int argc, char **argv);
1163 static const cmdinfo_t writev_cmd = {
1164 .name = "writev",
1165 .cfunc = writev_f,
1166 .argmin = 2,
1167 .argmax = -1,
1168 .args = "[-Cq] [-P pattern ] off len [len..]",
1169 .oneline = "writes a number of bytes at a specified offset",
1170 .help = writev_help,
1173 static int writev_f(BlockBackend *blk, int argc, char **argv)
1175 struct timeval t1, t2;
1176 int Cflag = 0, qflag = 0;
1177 int c, cnt;
1178 char *buf;
1179 int64_t offset;
1180 /* Some compilers get confused and warn if this is not initialized. */
1181 int total = 0;
1182 int nr_iov;
1183 int pattern = 0xcd;
1184 QEMUIOVector qiov;
1186 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1187 switch (c) {
1188 case 'C':
1189 Cflag = 1;
1190 break;
1191 case 'q':
1192 qflag = 1;
1193 break;
1194 case 'P':
1195 pattern = parse_pattern(optarg);
1196 if (pattern < 0) {
1197 return 0;
1199 break;
1200 default:
1201 return qemuio_command_usage(&writev_cmd);
1205 if (optind > argc - 2) {
1206 return qemuio_command_usage(&writev_cmd);
1209 offset = cvtnum(argv[optind]);
1210 if (offset < 0) {
1211 print_cvtnum_err(offset, argv[optind]);
1212 return 0;
1214 optind++;
1216 if (offset & 0x1ff) {
1217 printf("offset %" PRId64 " is not sector aligned\n",
1218 offset);
1219 return 0;
1222 nr_iov = argc - optind;
1223 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1224 if (buf == NULL) {
1225 return 0;
1228 gettimeofday(&t1, NULL);
1229 cnt = do_aio_writev(blk, &qiov, offset, &total);
1230 gettimeofday(&t2, NULL);
1232 if (cnt < 0) {
1233 printf("writev failed: %s\n", strerror(-cnt));
1234 goto out;
1237 if (qflag) {
1238 goto out;
1241 /* Finally, report back -- -C gives a parsable format */
1242 t2 = tsub(t2, t1);
1243 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1244 out:
1245 qemu_iovec_destroy(&qiov);
1246 qemu_io_free(buf);
1247 return 0;
1250 static void multiwrite_help(void)
1252 printf(
1253 "\n"
1254 " writes a range of bytes from the given offset source from multiple buffers,\n"
1255 " in a batch of requests that may be merged by qemu\n"
1256 "\n"
1257 " Example:\n"
1258 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1259 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1260 "\n"
1261 " Writes into a segment of the currently open file, using a buffer\n"
1262 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1263 " by one for each request contained in the multiwrite command.\n"
1264 " -P, -- use different pattern to fill file\n"
1265 " -C, -- report statistics in a machine parsable format\n"
1266 " -q, -- quiet mode, do not show I/O statistics\n"
1267 "\n");
1270 static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
1272 static const cmdinfo_t multiwrite_cmd = {
1273 .name = "multiwrite",
1274 .cfunc = multiwrite_f,
1275 .argmin = 2,
1276 .argmax = -1,
1277 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1278 .oneline = "issues multiple write requests at once",
1279 .help = multiwrite_help,
1282 static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
1284 struct timeval t1, t2;
1285 int Cflag = 0, qflag = 0;
1286 int c, cnt;
1287 char **buf;
1288 int64_t offset, first_offset = 0;
1289 /* Some compilers get confused and warn if this is not initialized. */
1290 int total = 0;
1291 int nr_iov;
1292 int nr_reqs;
1293 int pattern = 0xcd;
1294 QEMUIOVector *qiovs;
1295 int i;
1296 BlockRequest *reqs;
1298 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1299 switch (c) {
1300 case 'C':
1301 Cflag = 1;
1302 break;
1303 case 'q':
1304 qflag = 1;
1305 break;
1306 case 'P':
1307 pattern = parse_pattern(optarg);
1308 if (pattern < 0) {
1309 return 0;
1311 break;
1312 default:
1313 return qemuio_command_usage(&writev_cmd);
1317 if (optind > argc - 2) {
1318 return qemuio_command_usage(&writev_cmd);
1321 nr_reqs = 1;
1322 for (i = optind; i < argc; i++) {
1323 if (!strcmp(argv[i], ";")) {
1324 nr_reqs++;
1328 reqs = g_new0(BlockRequest, nr_reqs);
1329 buf = g_new0(char *, nr_reqs);
1330 qiovs = g_new(QEMUIOVector, nr_reqs);
1332 for (i = 0; i < nr_reqs && optind < argc; i++) {
1333 int j;
1335 /* Read the offset of the request */
1336 offset = cvtnum(argv[optind]);
1337 if (offset < 0) {
1338 print_cvtnum_err(offset, argv[optind]);
1339 goto out;
1341 optind++;
1343 if (offset & 0x1ff) {
1344 printf("offset %lld is not sector aligned\n",
1345 (long long)offset);
1346 goto out;
1349 if (i == 0) {
1350 first_offset = offset;
1353 /* Read lengths for qiov entries */
1354 for (j = optind; j < argc; j++) {
1355 if (!strcmp(argv[j], ";")) {
1356 break;
1360 nr_iov = j - optind;
1362 /* Build request */
1363 buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
1364 if (buf[i] == NULL) {
1365 goto out;
1368 reqs[i].qiov = &qiovs[i];
1369 reqs[i].sector = offset >> 9;
1370 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1372 optind = j + 1;
1374 pattern++;
1377 /* If there were empty requests at the end, ignore them */
1378 nr_reqs = i;
1380 gettimeofday(&t1, NULL);
1381 cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
1382 gettimeofday(&t2, NULL);
1384 if (cnt < 0) {
1385 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1386 goto out;
1389 if (qflag) {
1390 goto out;
1393 /* Finally, report back -- -C gives a parsable format */
1394 t2 = tsub(t2, t1);
1395 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1396 out:
1397 for (i = 0; i < nr_reqs; i++) {
1398 qemu_io_free(buf[i]);
1399 if (reqs[i].qiov != NULL) {
1400 qemu_iovec_destroy(&qiovs[i]);
1403 g_free(buf);
1404 g_free(reqs);
1405 g_free(qiovs);
1406 return 0;
1409 struct aio_ctx {
1410 BlockBackend *blk;
1411 QEMUIOVector qiov;
1412 int64_t offset;
1413 char *buf;
1414 int qflag;
1415 int vflag;
1416 int Cflag;
1417 int Pflag;
1418 int zflag;
1419 BlockAcctCookie acct;
1420 int pattern;
1421 struct timeval t1;
1424 static void aio_write_done(void *opaque, int ret)
1426 struct aio_ctx *ctx = opaque;
1427 struct timeval t2;
1429 gettimeofday(&t2, NULL);
1432 if (ret < 0) {
1433 printf("aio_write failed: %s\n", strerror(-ret));
1434 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1435 goto out;
1438 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1440 if (ctx->qflag) {
1441 goto out;
1444 /* Finally, report back -- -C gives a parsable format */
1445 t2 = tsub(t2, ctx->t1);
1446 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1447 ctx->qiov.size, 1, ctx->Cflag);
1448 out:
1449 if (!ctx->zflag) {
1450 qemu_io_free(ctx->buf);
1451 qemu_iovec_destroy(&ctx->qiov);
1453 g_free(ctx);
1456 static void aio_read_done(void *opaque, int ret)
1458 struct aio_ctx *ctx = opaque;
1459 struct timeval t2;
1461 gettimeofday(&t2, NULL);
1463 if (ret < 0) {
1464 printf("readv failed: %s\n", strerror(-ret));
1465 block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
1466 goto out;
1469 if (ctx->Pflag) {
1470 void *cmp_buf = g_malloc(ctx->qiov.size);
1472 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1473 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1474 printf("Pattern verification failed at offset %"
1475 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1477 g_free(cmp_buf);
1480 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1482 if (ctx->qflag) {
1483 goto out;
1486 if (ctx->vflag) {
1487 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1490 /* Finally, report back -- -C gives a parsable format */
1491 t2 = tsub(t2, ctx->t1);
1492 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1493 ctx->qiov.size, 1, ctx->Cflag);
1494 out:
1495 qemu_io_free(ctx->buf);
1496 qemu_iovec_destroy(&ctx->qiov);
1497 g_free(ctx);
1500 static void aio_read_help(void)
1502 printf(
1503 "\n"
1504 " asynchronously reads a range of bytes from the given offset\n"
1505 "\n"
1506 " Example:\n"
1507 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1508 "\n"
1509 " Reads a segment of the currently open file, optionally dumping it to the\n"
1510 " standard output stream (with -v option) for subsequent inspection.\n"
1511 " The read is performed asynchronously and the aio_flush command must be\n"
1512 " used to ensure all outstanding aio requests have been completed.\n"
1513 " -C, -- report statistics in a machine parsable format\n"
1514 " -P, -- use a pattern to verify read data\n"
1515 " -v, -- dump buffer to standard output\n"
1516 " -q, -- quiet mode, do not show I/O statistics\n"
1517 "\n");
1520 static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1522 static const cmdinfo_t aio_read_cmd = {
1523 .name = "aio_read",
1524 .cfunc = aio_read_f,
1525 .argmin = 2,
1526 .argmax = -1,
1527 .args = "[-Cqv] [-P pattern ] off len [len..]",
1528 .oneline = "asynchronously reads a number of bytes",
1529 .help = aio_read_help,
1532 static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1534 int nr_iov, c;
1535 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1537 ctx->blk = blk;
1538 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
1539 switch (c) {
1540 case 'C':
1541 ctx->Cflag = 1;
1542 break;
1543 case 'P':
1544 ctx->Pflag = 1;
1545 ctx->pattern = parse_pattern(optarg);
1546 if (ctx->pattern < 0) {
1547 g_free(ctx);
1548 return 0;
1550 break;
1551 case 'q':
1552 ctx->qflag = 1;
1553 break;
1554 case 'v':
1555 ctx->vflag = 1;
1556 break;
1557 default:
1558 g_free(ctx);
1559 return qemuio_command_usage(&aio_read_cmd);
1563 if (optind > argc - 2) {
1564 g_free(ctx);
1565 return qemuio_command_usage(&aio_read_cmd);
1568 ctx->offset = cvtnum(argv[optind]);
1569 if (ctx->offset < 0) {
1570 print_cvtnum_err(ctx->offset, argv[optind]);
1571 g_free(ctx);
1572 return 0;
1574 optind++;
1576 if (ctx->offset & 0x1ff) {
1577 printf("offset %" PRId64 " is not sector aligned\n",
1578 ctx->offset);
1579 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1580 g_free(ctx);
1581 return 0;
1584 nr_iov = argc - optind;
1585 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1586 if (ctx->buf == NULL) {
1587 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
1588 g_free(ctx);
1589 return 0;
1592 gettimeofday(&ctx->t1, NULL);
1593 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1594 BLOCK_ACCT_READ);
1595 blk_aio_readv(blk, ctx->offset >> 9, &ctx->qiov,
1596 ctx->qiov.size >> 9, aio_read_done, ctx);
1597 return 0;
1600 static void aio_write_help(void)
1602 printf(
1603 "\n"
1604 " asynchronously writes a range of bytes from the given offset source\n"
1605 " from multiple buffers\n"
1606 "\n"
1607 " Example:\n"
1608 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1609 "\n"
1610 " Writes into a segment of the currently open file, using a buffer\n"
1611 " filled with a set pattern (0xcdcdcdcd).\n"
1612 " The write is performed asynchronously and the aio_flush command must be\n"
1613 " used to ensure all outstanding aio requests have been completed.\n"
1614 " -P, -- use different pattern to fill file\n"
1615 " -C, -- report statistics in a machine parsable format\n"
1616 " -q, -- quiet mode, do not show I/O statistics\n"
1617 " -z, -- write zeroes using blk_aio_write_zeroes\n"
1618 "\n");
1621 static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1623 static const cmdinfo_t aio_write_cmd = {
1624 .name = "aio_write",
1625 .cfunc = aio_write_f,
1626 .argmin = 2,
1627 .argmax = -1,
1628 .args = "[-Cqz] [-P pattern ] off len [len..]",
1629 .oneline = "asynchronously writes a number of bytes",
1630 .help = aio_write_help,
1633 static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1635 int nr_iov, c;
1636 int pattern = 0xcd;
1637 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1639 ctx->blk = blk;
1640 while ((c = getopt(argc, argv, "CqP:z")) != -1) {
1641 switch (c) {
1642 case 'C':
1643 ctx->Cflag = 1;
1644 break;
1645 case 'q':
1646 ctx->qflag = 1;
1647 break;
1648 case 'P':
1649 pattern = parse_pattern(optarg);
1650 if (pattern < 0) {
1651 g_free(ctx);
1652 return 0;
1654 break;
1655 case 'z':
1656 ctx->zflag = 1;
1657 break;
1658 default:
1659 g_free(ctx);
1660 return qemuio_command_usage(&aio_write_cmd);
1664 if (optind > argc - 2) {
1665 g_free(ctx);
1666 return qemuio_command_usage(&aio_write_cmd);
1669 if (ctx->zflag && optind != argc - 2) {
1670 printf("-z supports only a single length parameter\n");
1671 g_free(ctx);
1672 return 0;
1675 if (ctx->zflag && ctx->Pflag) {
1676 printf("-z and -P cannot be specified at the same time\n");
1677 g_free(ctx);
1678 return 0;
1681 ctx->offset = cvtnum(argv[optind]);
1682 if (ctx->offset < 0) {
1683 print_cvtnum_err(ctx->offset, argv[optind]);
1684 g_free(ctx);
1685 return 0;
1687 optind++;
1689 if (ctx->offset & 0x1ff) {
1690 printf("offset %" PRId64 " is not sector aligned\n",
1691 ctx->offset);
1692 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1693 g_free(ctx);
1694 return 0;
1697 if (ctx->zflag) {
1698 int64_t count = cvtnum(argv[optind]);
1699 if (count < 0) {
1700 print_cvtnum_err(count, argv[optind]);
1701 g_free(ctx);
1702 return 0;
1705 ctx->qiov.size = count;
1706 blk_aio_write_zeroes(blk, ctx->offset, count, 0, aio_write_done, ctx);
1707 } else {
1708 nr_iov = argc - optind;
1709 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
1710 pattern);
1711 if (ctx->buf == NULL) {
1712 block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
1713 g_free(ctx);
1714 return 0;
1717 gettimeofday(&ctx->t1, NULL);
1718 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1719 BLOCK_ACCT_WRITE);
1721 blk_aio_writev(blk, ctx->offset >> 9, &ctx->qiov,
1722 ctx->qiov.size >> 9, aio_write_done, ctx);
1724 return 0;
1727 static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1729 BlockAcctCookie cookie;
1730 block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
1731 blk_drain_all();
1732 block_acct_done(blk_get_stats(blk), &cookie);
1733 return 0;
1736 static const cmdinfo_t aio_flush_cmd = {
1737 .name = "aio_flush",
1738 .cfunc = aio_flush_f,
1739 .oneline = "completes all outstanding aio requests"
1742 static int flush_f(BlockBackend *blk, int argc, char **argv)
1744 blk_flush(blk);
1745 return 0;
1748 static const cmdinfo_t flush_cmd = {
1749 .name = "flush",
1750 .altname = "f",
1751 .cfunc = flush_f,
1752 .oneline = "flush all in-core file state to disk",
1755 static int truncate_f(BlockBackend *blk, int argc, char **argv)
1757 int64_t offset;
1758 int ret;
1760 offset = cvtnum(argv[1]);
1761 if (offset < 0) {
1762 print_cvtnum_err(offset, argv[1]);
1763 return 0;
1766 ret = blk_truncate(blk, offset);
1767 if (ret < 0) {
1768 printf("truncate: %s\n", strerror(-ret));
1769 return 0;
1772 return 0;
1775 static const cmdinfo_t truncate_cmd = {
1776 .name = "truncate",
1777 .altname = "t",
1778 .cfunc = truncate_f,
1779 .argmin = 1,
1780 .argmax = 1,
1781 .args = "off",
1782 .oneline = "truncates the current file at the given offset",
1785 static int length_f(BlockBackend *blk, int argc, char **argv)
1787 int64_t size;
1788 char s1[64];
1790 size = blk_getlength(blk);
1791 if (size < 0) {
1792 printf("getlength: %s\n", strerror(-size));
1793 return 0;
1796 cvtstr(size, s1, sizeof(s1));
1797 printf("%s\n", s1);
1798 return 0;
1802 static const cmdinfo_t length_cmd = {
1803 .name = "length",
1804 .altname = "l",
1805 .cfunc = length_f,
1806 .oneline = "gets the length of the current file",
1810 static int info_f(BlockBackend *blk, int argc, char **argv)
1812 BlockDriverState *bs = blk_bs(blk);
1813 BlockDriverInfo bdi;
1814 ImageInfoSpecific *spec_info;
1815 char s1[64], s2[64];
1816 int ret;
1818 if (bs->drv && bs->drv->format_name) {
1819 printf("format name: %s\n", bs->drv->format_name);
1821 if (bs->drv && bs->drv->protocol_name) {
1822 printf("format name: %s\n", bs->drv->protocol_name);
1825 ret = bdrv_get_info(bs, &bdi);
1826 if (ret) {
1827 return 0;
1830 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1831 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1833 printf("cluster size: %s\n", s1);
1834 printf("vm state offset: %s\n", s2);
1836 spec_info = bdrv_get_specific_info(bs);
1837 if (spec_info) {
1838 printf("Format specific information:\n");
1839 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1840 qapi_free_ImageInfoSpecific(spec_info);
1843 return 0;
1848 static const cmdinfo_t info_cmd = {
1849 .name = "info",
1850 .altname = "i",
1851 .cfunc = info_f,
1852 .oneline = "prints information about the current file",
1855 static void discard_help(void)
1857 printf(
1858 "\n"
1859 " discards a range of bytes from the given offset\n"
1860 "\n"
1861 " Example:\n"
1862 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1863 "\n"
1864 " Discards a segment of the currently open file.\n"
1865 " -C, -- report statistics in a machine parsable format\n"
1866 " -q, -- quiet mode, do not show I/O statistics\n"
1867 "\n");
1870 static int discard_f(BlockBackend *blk, int argc, char **argv);
1872 static const cmdinfo_t discard_cmd = {
1873 .name = "discard",
1874 .altname = "d",
1875 .cfunc = discard_f,
1876 .argmin = 2,
1877 .argmax = -1,
1878 .args = "[-Cq] off len",
1879 .oneline = "discards a number of bytes at a specified offset",
1880 .help = discard_help,
1883 static int discard_f(BlockBackend *blk, int argc, char **argv)
1885 struct timeval t1, t2;
1886 int Cflag = 0, qflag = 0;
1887 int c, ret;
1888 int64_t offset, count;
1890 while ((c = getopt(argc, argv, "Cq")) != -1) {
1891 switch (c) {
1892 case 'C':
1893 Cflag = 1;
1894 break;
1895 case 'q':
1896 qflag = 1;
1897 break;
1898 default:
1899 return qemuio_command_usage(&discard_cmd);
1903 if (optind != argc - 2) {
1904 return qemuio_command_usage(&discard_cmd);
1907 offset = cvtnum(argv[optind]);
1908 if (offset < 0) {
1909 print_cvtnum_err(offset, argv[optind]);
1910 return 0;
1913 optind++;
1914 count = cvtnum(argv[optind]);
1915 if (count < 0) {
1916 print_cvtnum_err(count, argv[optind]);
1917 return 0;
1918 } else if (count >> BDRV_SECTOR_BITS > INT_MAX) {
1919 printf("length cannot exceed %"PRIu64", given %s\n",
1920 (uint64_t)INT_MAX << BDRV_SECTOR_BITS,
1921 argv[optind]);
1922 return 0;
1925 gettimeofday(&t1, NULL);
1926 ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1927 count >> BDRV_SECTOR_BITS);
1928 gettimeofday(&t2, NULL);
1930 if (ret < 0) {
1931 printf("discard failed: %s\n", strerror(-ret));
1932 goto out;
1935 /* Finally, report back -- -C gives a parsable format */
1936 if (!qflag) {
1937 t2 = tsub(t2, t1);
1938 print_report("discard", &t2, offset, count, count, 1, Cflag);
1941 out:
1942 return 0;
1945 static int alloc_f(BlockBackend *blk, int argc, char **argv)
1947 BlockDriverState *bs = blk_bs(blk);
1948 int64_t offset, sector_num, nb_sectors, remaining;
1949 char s1[64];
1950 int num, ret;
1951 int64_t sum_alloc;
1953 offset = cvtnum(argv[1]);
1954 if (offset < 0) {
1955 print_cvtnum_err(offset, argv[1]);
1956 return 0;
1957 } else if (offset & 0x1ff) {
1958 printf("offset %" PRId64 " is not sector aligned\n",
1959 offset);
1960 return 0;
1963 if (argc == 3) {
1964 nb_sectors = cvtnum(argv[2]);
1965 if (nb_sectors < 0) {
1966 print_cvtnum_err(nb_sectors, argv[2]);
1967 return 0;
1968 } else if (nb_sectors > INT_MAX) {
1969 printf("length argument cannot exceed %d, given %s\n",
1970 INT_MAX, argv[2]);
1971 return 0;
1973 } else {
1974 nb_sectors = 1;
1977 remaining = nb_sectors;
1978 sum_alloc = 0;
1979 sector_num = offset >> 9;
1980 while (remaining) {
1981 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1982 if (ret < 0) {
1983 printf("is_allocated failed: %s\n", strerror(-ret));
1984 return 0;
1986 sector_num += num;
1987 remaining -= num;
1988 if (ret) {
1989 sum_alloc += num;
1991 if (num == 0) {
1992 nb_sectors -= remaining;
1993 remaining = 0;
1997 cvtstr(offset, s1, sizeof(s1));
1999 printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n",
2000 sum_alloc, nb_sectors, s1);
2001 return 0;
2004 static const cmdinfo_t alloc_cmd = {
2005 .name = "alloc",
2006 .altname = "a",
2007 .argmin = 1,
2008 .argmax = 2,
2009 .cfunc = alloc_f,
2010 .args = "off [sectors]",
2011 .oneline = "checks if a sector is present in the file",
2015 static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
2016 int64_t nb_sectors, int64_t *pnum)
2018 int num, num_checked;
2019 int ret, firstret;
2021 num_checked = MIN(nb_sectors, INT_MAX);
2022 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
2023 if (ret < 0) {
2024 return ret;
2027 firstret = ret;
2028 *pnum = num;
2030 while (nb_sectors > 0 && ret == firstret) {
2031 sector_num += num;
2032 nb_sectors -= num;
2034 num_checked = MIN(nb_sectors, INT_MAX);
2035 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
2036 if (ret == firstret && num) {
2037 *pnum += num;
2038 } else {
2039 break;
2043 return firstret;
2046 static int map_f(BlockBackend *blk, int argc, char **argv)
2048 int64_t offset;
2049 int64_t nb_sectors, total_sectors;
2050 char s1[64];
2051 int64_t num;
2052 int ret;
2053 const char *retstr;
2055 offset = 0;
2056 total_sectors = blk_nb_sectors(blk);
2057 if (total_sectors < 0) {
2058 error_report("Failed to query image length: %s",
2059 strerror(-total_sectors));
2060 return 0;
2063 nb_sectors = total_sectors;
2065 do {
2066 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
2067 if (ret < 0) {
2068 error_report("Failed to get allocation status: %s", strerror(-ret));
2069 return 0;
2070 } else if (!num) {
2071 error_report("Unexpected end of image");
2072 return 0;
2075 retstr = ret ? " allocated" : "not allocated";
2076 cvtstr(offset << 9ULL, s1, sizeof(s1));
2077 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
2078 "at offset %s (%d)\n",
2079 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
2081 offset += num;
2082 nb_sectors -= num;
2083 } while (offset < total_sectors);
2085 return 0;
2088 static const cmdinfo_t map_cmd = {
2089 .name = "map",
2090 .argmin = 0,
2091 .argmax = 0,
2092 .cfunc = map_f,
2093 .args = "",
2094 .oneline = "prints the allocated areas of a file",
2097 static void reopen_help(void)
2099 printf(
2100 "\n"
2101 " Changes the open options of an already opened image\n"
2102 "\n"
2103 " Example:\n"
2104 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2105 "\n"
2106 " -r, -- Reopen the image read-only\n"
2107 " -c, -- Change the cache mode to the given value\n"
2108 " -o, -- Changes block driver options (cf. 'open' command)\n"
2109 "\n");
2112 static int reopen_f(BlockBackend *blk, int argc, char **argv);
2114 static QemuOptsList reopen_opts = {
2115 .name = "reopen",
2116 .merge_lists = true,
2117 .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
2118 .desc = {
2119 /* no elements => accept any params */
2120 { /* end of list */ }
2124 static const cmdinfo_t reopen_cmd = {
2125 .name = "reopen",
2126 .argmin = 0,
2127 .argmax = -1,
2128 .cfunc = reopen_f,
2129 .args = "[-r] [-c cache] [-o options]",
2130 .oneline = "reopens an image with new options",
2131 .help = reopen_help,
2134 static int reopen_f(BlockBackend *blk, int argc, char **argv)
2136 BlockDriverState *bs = blk_bs(blk);
2137 QemuOpts *qopts;
2138 QDict *opts;
2139 int c;
2140 int flags = bs->open_flags;
2141 bool writethrough = !blk_enable_write_cache(blk);
2143 BlockReopenQueue *brq;
2144 Error *local_err = NULL;
2146 while ((c = getopt(argc, argv, "c:o:r")) != -1) {
2147 switch (c) {
2148 case 'c':
2149 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
2150 error_report("Invalid cache option: %s", optarg);
2151 return 0;
2153 break;
2154 case 'o':
2155 if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
2156 qemu_opts_reset(&reopen_opts);
2157 return 0;
2159 break;
2160 case 'r':
2161 flags &= ~BDRV_O_RDWR;
2162 break;
2163 default:
2164 qemu_opts_reset(&reopen_opts);
2165 return qemuio_command_usage(&reopen_cmd);
2169 if (optind != argc) {
2170 qemu_opts_reset(&reopen_opts);
2171 return qemuio_command_usage(&reopen_cmd);
2174 if (writethrough != blk_enable_write_cache(blk) &&
2175 blk_get_attached_dev(blk))
2177 error_report("Cannot change cache.writeback: Device attached");
2178 qemu_opts_reset(&reopen_opts);
2179 return 0;
2182 qopts = qemu_opts_find(&reopen_opts, NULL);
2183 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
2184 qemu_opts_reset(&reopen_opts);
2186 brq = bdrv_reopen_queue(NULL, bs, opts, flags);
2187 bdrv_reopen_multiple(brq, &local_err);
2188 if (local_err) {
2189 error_report_err(local_err);
2190 } else {
2191 blk_set_enable_write_cache(blk, !writethrough);
2194 return 0;
2197 static int break_f(BlockBackend *blk, int argc, char **argv)
2199 int ret;
2201 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
2202 if (ret < 0) {
2203 printf("Could not set breakpoint: %s\n", strerror(-ret));
2206 return 0;
2209 static int remove_break_f(BlockBackend *blk, int argc, char **argv)
2211 int ret;
2213 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
2214 if (ret < 0) {
2215 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2218 return 0;
2221 static const cmdinfo_t break_cmd = {
2222 .name = "break",
2223 .argmin = 2,
2224 .argmax = 2,
2225 .cfunc = break_f,
2226 .args = "event tag",
2227 .oneline = "sets a breakpoint on event and tags the stopped "
2228 "request as tag",
2231 static const cmdinfo_t remove_break_cmd = {
2232 .name = "remove_break",
2233 .argmin = 1,
2234 .argmax = 1,
2235 .cfunc = remove_break_f,
2236 .args = "tag",
2237 .oneline = "remove a breakpoint by tag",
2240 static int resume_f(BlockBackend *blk, int argc, char **argv)
2242 int ret;
2244 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2245 if (ret < 0) {
2246 printf("Could not resume request: %s\n", strerror(-ret));
2249 return 0;
2252 static const cmdinfo_t resume_cmd = {
2253 .name = "resume",
2254 .argmin = 1,
2255 .argmax = 1,
2256 .cfunc = resume_f,
2257 .args = "tag",
2258 .oneline = "resumes the request tagged as tag",
2261 static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2263 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2264 aio_poll(blk_get_aio_context(blk), true);
2267 return 0;
2270 static const cmdinfo_t wait_break_cmd = {
2271 .name = "wait_break",
2272 .argmin = 1,
2273 .argmax = 1,
2274 .cfunc = wait_break_f,
2275 .args = "tag",
2276 .oneline = "waits for the suspension of a request",
2279 static int abort_f(BlockBackend *blk, int argc, char **argv)
2281 abort();
2284 static const cmdinfo_t abort_cmd = {
2285 .name = "abort",
2286 .cfunc = abort_f,
2287 .flags = CMD_NOFILE_OK,
2288 .oneline = "simulate a program crash using abort(3)",
2291 static void sigraise_help(void)
2293 printf(
2294 "\n"
2295 " raises the given signal\n"
2296 "\n"
2297 " Example:\n"
2298 " 'sigraise %i' - raises SIGTERM\n"
2299 "\n"
2300 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2301 " given to sigraise.\n"
2302 "\n", SIGTERM);
2305 static int sigraise_f(BlockBackend *blk, int argc, char **argv);
2307 static const cmdinfo_t sigraise_cmd = {
2308 .name = "sigraise",
2309 .cfunc = sigraise_f,
2310 .argmin = 1,
2311 .argmax = 1,
2312 .flags = CMD_NOFILE_OK,
2313 .args = "signal",
2314 .oneline = "raises a signal",
2315 .help = sigraise_help,
2318 static int sigraise_f(BlockBackend *blk, int argc, char **argv)
2320 int64_t sig = cvtnum(argv[1]);
2321 if (sig < 0) {
2322 print_cvtnum_err(sig, argv[1]);
2323 return 0;
2324 } else if (sig > NSIG) {
2325 printf("signal argument '%s' is too large to be a valid signal\n",
2326 argv[1]);
2327 return 0;
2330 /* Using raise() to kill this process does not necessarily flush all open
2331 * streams. At least stdout and stderr (although the latter should be
2332 * non-buffered anyway) should be flushed, though. */
2333 fflush(stdout);
2334 fflush(stderr);
2336 raise(sig);
2337 return 0;
2340 static void sleep_cb(void *opaque)
2342 bool *expired = opaque;
2343 *expired = true;
2346 static int sleep_f(BlockBackend *blk, int argc, char **argv)
2348 char *endptr;
2349 long ms;
2350 struct QEMUTimer *timer;
2351 bool expired = false;
2353 ms = strtol(argv[1], &endptr, 0);
2354 if (ms < 0 || *endptr != '\0') {
2355 printf("%s is not a valid number\n", argv[1]);
2356 return 0;
2359 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2360 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2362 while (!expired) {
2363 main_loop_wait(false);
2366 timer_free(timer);
2368 return 0;
2371 static const cmdinfo_t sleep_cmd = {
2372 .name = "sleep",
2373 .argmin = 1,
2374 .argmax = 1,
2375 .cfunc = sleep_f,
2376 .flags = CMD_NOFILE_OK,
2377 .oneline = "waits for the given value in milliseconds",
2380 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2382 if (cmd) {
2383 printf("%s ", cmd);
2384 } else {
2385 printf("%s ", ct->name);
2386 if (ct->altname) {
2387 printf("(or %s) ", ct->altname);
2391 if (ct->args) {
2392 printf("%s ", ct->args);
2394 printf("-- %s\n", ct->oneline);
2397 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2399 help_oneline(cmd, ct);
2400 if (ct->help) {
2401 ct->help();
2405 static void help_all(void)
2407 const cmdinfo_t *ct;
2409 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2410 help_oneline(ct->name, ct);
2412 printf("\nUse 'help commandname' for extended help.\n");
2415 static int help_f(BlockBackend *blk, int argc, char **argv)
2417 const cmdinfo_t *ct;
2419 if (argc == 1) {
2420 help_all();
2421 return 0;
2424 ct = find_command(argv[1]);
2425 if (ct == NULL) {
2426 printf("command %s not found\n", argv[1]);
2427 return 0;
2430 help_onecmd(argv[1], ct);
2431 return 0;
2434 static const cmdinfo_t help_cmd = {
2435 .name = "help",
2436 .altname = "?",
2437 .cfunc = help_f,
2438 .argmin = 0,
2439 .argmax = 1,
2440 .flags = CMD_FLAG_GLOBAL,
2441 .args = "[command]",
2442 .oneline = "help for one or all commands",
2445 bool qemuio_command(BlockBackend *blk, const char *cmd)
2447 char *input;
2448 const cmdinfo_t *ct;
2449 char **v;
2450 int c;
2451 bool done = false;
2453 input = g_strdup(cmd);
2454 v = breakline(input, &c);
2455 if (c) {
2456 ct = find_command(v[0]);
2457 if (ct) {
2458 done = command(blk, ct, c, v);
2459 } else {
2460 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2463 g_free(input);
2464 g_free(v);
2466 return done;
2469 static void __attribute((constructor)) init_qemuio_commands(void)
2471 /* initialize commands */
2472 qemuio_add_command(&help_cmd);
2473 qemuio_add_command(&read_cmd);
2474 qemuio_add_command(&readv_cmd);
2475 qemuio_add_command(&write_cmd);
2476 qemuio_add_command(&writev_cmd);
2477 qemuio_add_command(&multiwrite_cmd);
2478 qemuio_add_command(&aio_read_cmd);
2479 qemuio_add_command(&aio_write_cmd);
2480 qemuio_add_command(&aio_flush_cmd);
2481 qemuio_add_command(&flush_cmd);
2482 qemuio_add_command(&truncate_cmd);
2483 qemuio_add_command(&length_cmd);
2484 qemuio_add_command(&info_cmd);
2485 qemuio_add_command(&discard_cmd);
2486 qemuio_add_command(&alloc_cmd);
2487 qemuio_add_command(&map_cmd);
2488 qemuio_add_command(&reopen_cmd);
2489 qemuio_add_command(&break_cmd);
2490 qemuio_add_command(&remove_break_cmd);
2491 qemuio_add_command(&resume_cmd);
2492 qemuio_add_command(&wait_break_cmd);
2493 qemuio_add_command(&abort_cmd);
2494 qemuio_add_command(&sleep_cmd);
2495 qemuio_add_command(&sigraise_cmd);