1797ac58cSKevin Wolf /* 2797ac58cSKevin Wolf * Command line utility to exercise the QEMU I/O path. 3797ac58cSKevin Wolf * 4797ac58cSKevin Wolf * Copyright (C) 2009 Red Hat, Inc. 5797ac58cSKevin Wolf * Copyright (c) 2003-2005 Silicon Graphics, Inc. 6797ac58cSKevin Wolf * 7797ac58cSKevin Wolf * This work is licensed under the terms of the GNU GPL, version 2 or later. 8797ac58cSKevin Wolf * See the COPYING file in the top-level directory. 9797ac58cSKevin Wolf */ 10797ac58cSKevin Wolf 113d21994fSKevin Wolf #include "qemu-io.h" 124c7b7e9bSMax Reitz #include "sysemu/block-backend.h" 134c7b7e9bSMax Reitz #include "block/block.h" 144c7b7e9bSMax Reitz #include "block/block_int.h" /* for info_f() */ 15a8d8ecb7SMax Reitz #include "block/qapi.h" 16d49b6836SMarkus Armbruster #include "qemu/error-report.h" 176a1751b7SAlex Bligh #include "qemu/main-loop.h" 18cd33d02aSKevin Wolf #include "qemu/timer.h" 19a91f9584SFam Zheng #include "sysemu/block-backend.h" 20797ac58cSKevin Wolf 21797ac58cSKevin Wolf #define CMD_NOFILE_OK 0x01 22797ac58cSKevin Wolf 23f9883880SStefan Weil bool qemuio_misalign; 24797ac58cSKevin Wolf 25c2cdf5c5SKevin Wolf static cmdinfo_t *cmdtab; 26c2cdf5c5SKevin Wolf static int ncmds; 27c2cdf5c5SKevin Wolf 28c2cdf5c5SKevin Wolf static int compare_cmdname(const void *a, const void *b) 29c2cdf5c5SKevin Wolf { 30c2cdf5c5SKevin Wolf return strcmp(((const cmdinfo_t *)a)->name, 31c2cdf5c5SKevin Wolf ((const cmdinfo_t *)b)->name); 32c2cdf5c5SKevin Wolf } 33c2cdf5c5SKevin Wolf 34c2cdf5c5SKevin Wolf void qemuio_add_command(const cmdinfo_t *ci) 35c2cdf5c5SKevin Wolf { 3602c4f26bSMarkus Armbruster cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds); 37c2cdf5c5SKevin Wolf cmdtab[ncmds - 1] = *ci; 38c2cdf5c5SKevin Wolf qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname); 39c2cdf5c5SKevin Wolf } 40c2cdf5c5SKevin Wolf 41c2cdf5c5SKevin Wolf int qemuio_command_usage(const cmdinfo_t *ci) 42c2cdf5c5SKevin Wolf { 43c2cdf5c5SKevin Wolf printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline); 44c2cdf5c5SKevin Wolf return 0; 45c2cdf5c5SKevin Wolf } 46c2cdf5c5SKevin Wolf 474c7b7e9bSMax Reitz static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct) 48c2cdf5c5SKevin Wolf { 49c2cdf5c5SKevin Wolf if (ct->flags & CMD_FLAG_GLOBAL) { 50c2cdf5c5SKevin Wolf return 1; 51c2cdf5c5SKevin Wolf } 524c7b7e9bSMax Reitz if (!(ct->flags & CMD_NOFILE_OK) && !blk) { 53c2cdf5c5SKevin Wolf fprintf(stderr, "no file open, try 'help open'\n"); 54c2cdf5c5SKevin Wolf return 0; 55c2cdf5c5SKevin Wolf } 56c2cdf5c5SKevin Wolf return 1; 57c2cdf5c5SKevin Wolf } 58c2cdf5c5SKevin Wolf 594c7b7e9bSMax Reitz static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc, 603d21994fSKevin Wolf char **argv) 61c2cdf5c5SKevin Wolf { 62c2cdf5c5SKevin Wolf char *cmd = argv[0]; 63c2cdf5c5SKevin Wolf 644c7b7e9bSMax Reitz if (!init_check_command(blk, ct)) { 65c2cdf5c5SKevin Wolf return 0; 66c2cdf5c5SKevin Wolf } 67c2cdf5c5SKevin Wolf 68c2cdf5c5SKevin Wolf if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) { 69c2cdf5c5SKevin Wolf if (ct->argmax == -1) { 70c2cdf5c5SKevin Wolf fprintf(stderr, 71c2cdf5c5SKevin Wolf "bad argument count %d to %s, expected at least %d arguments\n", 72c2cdf5c5SKevin Wolf argc-1, cmd, ct->argmin); 73c2cdf5c5SKevin Wolf } else if (ct->argmin == ct->argmax) { 74c2cdf5c5SKevin Wolf fprintf(stderr, 75c2cdf5c5SKevin Wolf "bad argument count %d to %s, expected %d arguments\n", 76c2cdf5c5SKevin Wolf argc-1, cmd, ct->argmin); 77c2cdf5c5SKevin Wolf } else { 78c2cdf5c5SKevin Wolf fprintf(stderr, 79c2cdf5c5SKevin Wolf "bad argument count %d to %s, expected between %d and %d arguments\n", 80c2cdf5c5SKevin Wolf argc-1, cmd, ct->argmin, ct->argmax); 81c2cdf5c5SKevin Wolf } 82c2cdf5c5SKevin Wolf return 0; 83c2cdf5c5SKevin Wolf } 84c2cdf5c5SKevin Wolf optind = 0; 854c7b7e9bSMax Reitz return ct->cfunc(blk, argc, argv); 86c2cdf5c5SKevin Wolf } 87c2cdf5c5SKevin Wolf 88c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd) 89c2cdf5c5SKevin Wolf { 90c2cdf5c5SKevin Wolf cmdinfo_t *ct; 91c2cdf5c5SKevin Wolf 92c2cdf5c5SKevin Wolf for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 93c2cdf5c5SKevin Wolf if (strcmp(ct->name, cmd) == 0 || 94c2cdf5c5SKevin Wolf (ct->altname && strcmp(ct->altname, cmd) == 0)) 95c2cdf5c5SKevin Wolf { 96c2cdf5c5SKevin Wolf return (const cmdinfo_t *)ct; 97c2cdf5c5SKevin Wolf } 98c2cdf5c5SKevin Wolf } 99c2cdf5c5SKevin Wolf return NULL; 100c2cdf5c5SKevin Wolf } 101c2cdf5c5SKevin Wolf 1024694020dSStefan Hajnoczi /* Invoke fn() for commands with a matching prefix */ 1034694020dSStefan Hajnoczi void qemuio_complete_command(const char *input, 1044694020dSStefan Hajnoczi void (*fn)(const char *cmd, void *opaque), 1054694020dSStefan Hajnoczi void *opaque) 1064694020dSStefan Hajnoczi { 1074694020dSStefan Hajnoczi cmdinfo_t *ct; 1084694020dSStefan Hajnoczi size_t input_len = strlen(input); 1094694020dSStefan Hajnoczi 1104694020dSStefan Hajnoczi for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 1114694020dSStefan Hajnoczi if (strncmp(input, ct->name, input_len) == 0) { 1124694020dSStefan Hajnoczi fn(ct->name, opaque); 1134694020dSStefan Hajnoczi } 1144694020dSStefan Hajnoczi } 1154694020dSStefan Hajnoczi } 1164694020dSStefan Hajnoczi 117c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count) 118c2cdf5c5SKevin Wolf { 119c2cdf5c5SKevin Wolf int c = 0; 120c2cdf5c5SKevin Wolf char *p; 1215839e53bSMarkus Armbruster char **rval = g_new0(char *, 1); 122c2cdf5c5SKevin Wolf 123c2cdf5c5SKevin Wolf while (rval && (p = qemu_strsep(&input, " ")) != NULL) { 124c2cdf5c5SKevin Wolf if (!*p) { 125c2cdf5c5SKevin Wolf continue; 126c2cdf5c5SKevin Wolf } 127c2cdf5c5SKevin Wolf c++; 12808193dd5SMarkus Armbruster rval = g_renew(char *, rval, (c + 1)); 129c2cdf5c5SKevin Wolf rval[c - 1] = p; 130c2cdf5c5SKevin Wolf rval[c] = NULL; 131c2cdf5c5SKevin Wolf } 132c2cdf5c5SKevin Wolf *count = c; 133c2cdf5c5SKevin Wolf return rval; 134c2cdf5c5SKevin Wolf } 135c2cdf5c5SKevin Wolf 136797ac58cSKevin Wolf static int64_t cvtnum(const char *s) 137797ac58cSKevin Wolf { 138797ac58cSKevin Wolf char *end; 1394677bb40SMarc-André Lureau return qemu_strtosz_suffix(s, &end, QEMU_STRTOSZ_DEFSUFFIX_B); 140797ac58cSKevin Wolf } 141797ac58cSKevin Wolf 1420b613881SKevin Wolf #define EXABYTES(x) ((long long)(x) << 60) 1430b613881SKevin Wolf #define PETABYTES(x) ((long long)(x) << 50) 1440b613881SKevin Wolf #define TERABYTES(x) ((long long)(x) << 40) 1450b613881SKevin Wolf #define GIGABYTES(x) ((long long)(x) << 30) 1460b613881SKevin Wolf #define MEGABYTES(x) ((long long)(x) << 20) 1470b613881SKevin Wolf #define KILOBYTES(x) ((long long)(x) << 10) 1480b613881SKevin Wolf 1490b613881SKevin Wolf #define TO_EXABYTES(x) ((x) / EXABYTES(1)) 1500b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1)) 1510b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1)) 1520b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1)) 1530b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1)) 1540b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1)) 1550b613881SKevin Wolf 1560b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size) 1570b613881SKevin Wolf { 1580b613881SKevin Wolf char *trim; 1590b613881SKevin Wolf const char *suffix; 1600b613881SKevin Wolf 1610b613881SKevin Wolf if (value >= EXABYTES(1)) { 1620b613881SKevin Wolf suffix = " EiB"; 1630b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_EXABYTES(value)); 1640b613881SKevin Wolf } else if (value >= PETABYTES(1)) { 1650b613881SKevin Wolf suffix = " PiB"; 1660b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_PETABYTES(value)); 1670b613881SKevin Wolf } else if (value >= TERABYTES(1)) { 1680b613881SKevin Wolf suffix = " TiB"; 1690b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_TERABYTES(value)); 1700b613881SKevin Wolf } else if (value >= GIGABYTES(1)) { 1710b613881SKevin Wolf suffix = " GiB"; 1720b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value)); 1730b613881SKevin Wolf } else if (value >= MEGABYTES(1)) { 1740b613881SKevin Wolf suffix = " MiB"; 1750b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value)); 1760b613881SKevin Wolf } else if (value >= KILOBYTES(1)) { 1770b613881SKevin Wolf suffix = " KiB"; 1780b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value)); 1790b613881SKevin Wolf } else { 1800b613881SKevin Wolf suffix = " bytes"; 1810b613881SKevin Wolf snprintf(str, size - 6, "%f", value); 1820b613881SKevin Wolf } 1830b613881SKevin Wolf 1840b613881SKevin Wolf trim = strstr(str, ".000"); 1850b613881SKevin Wolf if (trim) { 1860b613881SKevin Wolf strcpy(trim, suffix); 1870b613881SKevin Wolf } else { 1880b613881SKevin Wolf strcat(str, suffix); 1890b613881SKevin Wolf } 1900b613881SKevin Wolf } 1910b613881SKevin Wolf 1920b613881SKevin Wolf 1930b613881SKevin Wolf 1940b613881SKevin Wolf static struct timeval tsub(struct timeval t1, struct timeval t2) 1950b613881SKevin Wolf { 1960b613881SKevin Wolf t1.tv_usec -= t2.tv_usec; 1970b613881SKevin Wolf if (t1.tv_usec < 0) { 1980b613881SKevin Wolf t1.tv_usec += 1000000; 1990b613881SKevin Wolf t1.tv_sec--; 2000b613881SKevin Wolf } 2010b613881SKevin Wolf t1.tv_sec -= t2.tv_sec; 2020b613881SKevin Wolf return t1; 2030b613881SKevin Wolf } 2040b613881SKevin Wolf 2050b613881SKevin Wolf static double tdiv(double value, struct timeval tv) 2060b613881SKevin Wolf { 2070b613881SKevin Wolf return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0)); 2080b613881SKevin Wolf } 2090b613881SKevin Wolf 2100b613881SKevin Wolf #define HOURS(sec) ((sec) / (60 * 60)) 2110b613881SKevin Wolf #define MINUTES(sec) (((sec) % (60 * 60)) / 60) 2120b613881SKevin Wolf #define SECONDS(sec) ((sec) % 60) 2130b613881SKevin Wolf 2140b613881SKevin Wolf enum { 2150b613881SKevin Wolf DEFAULT_TIME = 0x0, 2160b613881SKevin Wolf TERSE_FIXED_TIME = 0x1, 2170b613881SKevin Wolf VERBOSE_FIXED_TIME = 0x2, 2180b613881SKevin Wolf }; 2190b613881SKevin Wolf 2200b613881SKevin Wolf static void timestr(struct timeval *tv, char *ts, size_t size, int format) 2210b613881SKevin Wolf { 2220b613881SKevin Wolf double usec = (double)tv->tv_usec / 1000000.0; 2230b613881SKevin Wolf 2240b613881SKevin Wolf if (format & TERSE_FIXED_TIME) { 2250b613881SKevin Wolf if (!HOURS(tv->tv_sec)) { 2260b613881SKevin Wolf snprintf(ts, size, "%u:%02u.%02u", 2270b613881SKevin Wolf (unsigned int) MINUTES(tv->tv_sec), 2280b613881SKevin Wolf (unsigned int) SECONDS(tv->tv_sec), 2290b613881SKevin Wolf (unsigned int) (usec * 100)); 2300b613881SKevin Wolf return; 2310b613881SKevin Wolf } 2320b613881SKevin Wolf format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */ 2330b613881SKevin Wolf } 2340b613881SKevin Wolf 2350b613881SKevin Wolf if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) { 2360b613881SKevin Wolf snprintf(ts, size, "%u:%02u:%02u.%02u", 2370b613881SKevin Wolf (unsigned int) HOURS(tv->tv_sec), 2380b613881SKevin Wolf (unsigned int) MINUTES(tv->tv_sec), 2390b613881SKevin Wolf (unsigned int) SECONDS(tv->tv_sec), 2400b613881SKevin Wolf (unsigned int) (usec * 100)); 2410b613881SKevin Wolf } else { 2420b613881SKevin Wolf snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000)); 2430b613881SKevin Wolf } 2440b613881SKevin Wolf } 2450b613881SKevin Wolf 246797ac58cSKevin Wolf /* 247797ac58cSKevin Wolf * Parse the pattern argument to various sub-commands. 248797ac58cSKevin Wolf * 249797ac58cSKevin Wolf * Because the pattern is used as an argument to memset it must evaluate 250797ac58cSKevin Wolf * to an unsigned integer that fits into a single byte. 251797ac58cSKevin Wolf */ 252797ac58cSKevin Wolf static int parse_pattern(const char *arg) 253797ac58cSKevin Wolf { 254797ac58cSKevin Wolf char *endptr = NULL; 255797ac58cSKevin Wolf long pattern; 256797ac58cSKevin Wolf 257797ac58cSKevin Wolf pattern = strtol(arg, &endptr, 0); 258797ac58cSKevin Wolf if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') { 259797ac58cSKevin Wolf printf("%s is not a valid pattern byte\n", arg); 260797ac58cSKevin Wolf return -1; 261797ac58cSKevin Wolf } 262797ac58cSKevin Wolf 263797ac58cSKevin Wolf return pattern; 264797ac58cSKevin Wolf } 265797ac58cSKevin Wolf 266797ac58cSKevin Wolf /* 267797ac58cSKevin Wolf * Memory allocation helpers. 268797ac58cSKevin Wolf * 269797ac58cSKevin Wolf * Make sure memory is aligned by default, or purposefully misaligned if 270797ac58cSKevin Wolf * that is specified on the command line. 271797ac58cSKevin Wolf */ 272797ac58cSKevin Wolf 273797ac58cSKevin Wolf #define MISALIGN_OFFSET 16 2744c7b7e9bSMax Reitz static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern) 275797ac58cSKevin Wolf { 276797ac58cSKevin Wolf void *buf; 277797ac58cSKevin Wolf 278797ac58cSKevin Wolf if (qemuio_misalign) { 279797ac58cSKevin Wolf len += MISALIGN_OFFSET; 280797ac58cSKevin Wolf } 2814c7b7e9bSMax Reitz buf = blk_blockalign(blk, len); 282797ac58cSKevin Wolf memset(buf, pattern, len); 283797ac58cSKevin Wolf if (qemuio_misalign) { 284797ac58cSKevin Wolf buf += MISALIGN_OFFSET; 285797ac58cSKevin Wolf } 286797ac58cSKevin Wolf return buf; 287797ac58cSKevin Wolf } 288797ac58cSKevin Wolf 289797ac58cSKevin Wolf static void qemu_io_free(void *p) 290797ac58cSKevin Wolf { 291797ac58cSKevin Wolf if (qemuio_misalign) { 292797ac58cSKevin Wolf p -= MISALIGN_OFFSET; 293797ac58cSKevin Wolf } 294797ac58cSKevin Wolf qemu_vfree(p); 295797ac58cSKevin Wolf } 296797ac58cSKevin Wolf 297*9b0beaf3SJohn Snow static void dump_buffer(const void *buffer, int64_t offset, int64_t len) 298797ac58cSKevin Wolf { 299*9b0beaf3SJohn Snow uint64_t i; 300*9b0beaf3SJohn Snow int j; 301797ac58cSKevin Wolf const uint8_t *p; 302797ac58cSKevin Wolf 303797ac58cSKevin Wolf for (i = 0, p = buffer; i < len; i += 16) { 304797ac58cSKevin Wolf const uint8_t *s = p; 305797ac58cSKevin Wolf 306797ac58cSKevin Wolf printf("%08" PRIx64 ": ", offset + i); 307797ac58cSKevin Wolf for (j = 0; j < 16 && i + j < len; j++, p++) { 308797ac58cSKevin Wolf printf("%02x ", *p); 309797ac58cSKevin Wolf } 310797ac58cSKevin Wolf printf(" "); 311797ac58cSKevin Wolf for (j = 0; j < 16 && i + j < len; j++, s++) { 312797ac58cSKevin Wolf if (isalnum(*s)) { 313797ac58cSKevin Wolf printf("%c", *s); 314797ac58cSKevin Wolf } else { 315797ac58cSKevin Wolf printf("."); 316797ac58cSKevin Wolf } 317797ac58cSKevin Wolf } 318797ac58cSKevin Wolf printf("\n"); 319797ac58cSKevin Wolf } 320797ac58cSKevin Wolf } 321797ac58cSKevin Wolf 322797ac58cSKevin Wolf static void print_report(const char *op, struct timeval *t, int64_t offset, 323*9b0beaf3SJohn Snow int64_t count, int64_t total, int cnt, int Cflag) 324797ac58cSKevin Wolf { 325797ac58cSKevin Wolf char s1[64], s2[64], ts[64]; 326797ac58cSKevin Wolf 327797ac58cSKevin Wolf timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0); 328797ac58cSKevin Wolf if (!Cflag) { 329797ac58cSKevin Wolf cvtstr((double)total, s1, sizeof(s1)); 330797ac58cSKevin Wolf cvtstr(tdiv((double)total, *t), s2, sizeof(s2)); 331*9b0beaf3SJohn Snow printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n", 332797ac58cSKevin Wolf op, total, count, offset); 333797ac58cSKevin Wolf printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n", 334797ac58cSKevin Wolf s1, cnt, ts, s2, tdiv((double)cnt, *t)); 335797ac58cSKevin Wolf } else {/* bytes,ops,time,bytes/sec,ops/sec */ 336*9b0beaf3SJohn Snow printf("%"PRId64",%d,%s,%.3f,%.3f\n", 337797ac58cSKevin Wolf total, cnt, ts, 338797ac58cSKevin Wolf tdiv((double)total, *t), 339797ac58cSKevin Wolf tdiv((double)cnt, *t)); 340797ac58cSKevin Wolf } 341797ac58cSKevin Wolf } 342797ac58cSKevin Wolf 343797ac58cSKevin Wolf /* 344797ac58cSKevin Wolf * Parse multiple length statements for vectored I/O, and construct an I/O 345797ac58cSKevin Wolf * vector matching it. 346797ac58cSKevin Wolf */ 347797ac58cSKevin Wolf static void * 3484c7b7e9bSMax Reitz create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov, 349797ac58cSKevin Wolf int pattern) 350797ac58cSKevin Wolf { 351797ac58cSKevin Wolf size_t *sizes = g_new0(size_t, nr_iov); 352797ac58cSKevin Wolf size_t count = 0; 353797ac58cSKevin Wolf void *buf = NULL; 354797ac58cSKevin Wolf void *p; 355797ac58cSKevin Wolf int i; 356797ac58cSKevin Wolf 357797ac58cSKevin Wolf for (i = 0; i < nr_iov; i++) { 358797ac58cSKevin Wolf char *arg = argv[i]; 359797ac58cSKevin Wolf int64_t len; 360797ac58cSKevin Wolf 361797ac58cSKevin Wolf len = cvtnum(arg); 362797ac58cSKevin Wolf if (len < 0) { 363797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", arg); 364797ac58cSKevin Wolf goto fail; 365797ac58cSKevin Wolf } 366797ac58cSKevin Wolf 367797ac58cSKevin Wolf /* should be SIZE_T_MAX, but that doesn't exist */ 368797ac58cSKevin Wolf if (len > INT_MAX) { 369797ac58cSKevin Wolf printf("too large length argument -- %s\n", arg); 370797ac58cSKevin Wolf goto fail; 371797ac58cSKevin Wolf } 372797ac58cSKevin Wolf 373797ac58cSKevin Wolf if (len & 0x1ff) { 374797ac58cSKevin Wolf printf("length argument %" PRId64 375797ac58cSKevin Wolf " is not sector aligned\n", len); 376797ac58cSKevin Wolf goto fail; 377797ac58cSKevin Wolf } 378797ac58cSKevin Wolf 379797ac58cSKevin Wolf sizes[i] = len; 380797ac58cSKevin Wolf count += len; 381797ac58cSKevin Wolf } 382797ac58cSKevin Wolf 383797ac58cSKevin Wolf qemu_iovec_init(qiov, nr_iov); 384797ac58cSKevin Wolf 3854c7b7e9bSMax Reitz buf = p = qemu_io_alloc(blk, count, pattern); 386797ac58cSKevin Wolf 387797ac58cSKevin Wolf for (i = 0; i < nr_iov; i++) { 388797ac58cSKevin Wolf qemu_iovec_add(qiov, p, sizes[i]); 389797ac58cSKevin Wolf p += sizes[i]; 390797ac58cSKevin Wolf } 391797ac58cSKevin Wolf 392797ac58cSKevin Wolf fail: 393797ac58cSKevin Wolf g_free(sizes); 394797ac58cSKevin Wolf return buf; 395797ac58cSKevin Wolf } 396797ac58cSKevin Wolf 397*9b0beaf3SJohn Snow static int do_read(BlockBackend *blk, char *buf, int64_t offset, int64_t count, 398*9b0beaf3SJohn Snow int64_t *total) 399797ac58cSKevin Wolf { 400797ac58cSKevin Wolf int ret; 401797ac58cSKevin Wolf 402*9b0beaf3SJohn Snow if (count >> 9 > INT_MAX) { 403*9b0beaf3SJohn Snow return -ERANGE; 404*9b0beaf3SJohn Snow } 405*9b0beaf3SJohn Snow 4064c7b7e9bSMax Reitz ret = blk_read(blk, offset >> 9, (uint8_t *)buf, count >> 9); 407797ac58cSKevin Wolf if (ret < 0) { 408797ac58cSKevin Wolf return ret; 409797ac58cSKevin Wolf } 410797ac58cSKevin Wolf *total = count; 411797ac58cSKevin Wolf return 1; 412797ac58cSKevin Wolf } 413797ac58cSKevin Wolf 414*9b0beaf3SJohn Snow static int do_write(BlockBackend *blk, char *buf, int64_t offset, int64_t count, 415*9b0beaf3SJohn Snow int64_t *total) 416797ac58cSKevin Wolf { 417797ac58cSKevin Wolf int ret; 418797ac58cSKevin Wolf 419*9b0beaf3SJohn Snow if (count >> 9 > INT_MAX) { 420*9b0beaf3SJohn Snow return -ERANGE; 421*9b0beaf3SJohn Snow } 422*9b0beaf3SJohn Snow 4234c7b7e9bSMax Reitz ret = blk_write(blk, offset >> 9, (uint8_t *)buf, count >> 9); 424797ac58cSKevin Wolf if (ret < 0) { 425797ac58cSKevin Wolf return ret; 426797ac58cSKevin Wolf } 427797ac58cSKevin Wolf *total = count; 428797ac58cSKevin Wolf return 1; 429797ac58cSKevin Wolf } 430797ac58cSKevin Wolf 431*9b0beaf3SJohn Snow static int do_pread(BlockBackend *blk, char *buf, int64_t offset, 432*9b0beaf3SJohn Snow int64_t count, int64_t *total) 433797ac58cSKevin Wolf { 434*9b0beaf3SJohn Snow if (count > INT_MAX) { 435*9b0beaf3SJohn Snow return -ERANGE; 436*9b0beaf3SJohn Snow } 437*9b0beaf3SJohn Snow 4384c7b7e9bSMax Reitz *total = blk_pread(blk, offset, (uint8_t *)buf, count); 439797ac58cSKevin Wolf if (*total < 0) { 440797ac58cSKevin Wolf return *total; 441797ac58cSKevin Wolf } 442797ac58cSKevin Wolf return 1; 443797ac58cSKevin Wolf } 444797ac58cSKevin Wolf 445*9b0beaf3SJohn Snow static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset, 446*9b0beaf3SJohn Snow int64_t count, int64_t *total) 447797ac58cSKevin Wolf { 448*9b0beaf3SJohn Snow if (count > INT_MAX) { 449*9b0beaf3SJohn Snow return -ERANGE; 450*9b0beaf3SJohn Snow } 451*9b0beaf3SJohn Snow 4524c7b7e9bSMax Reitz *total = blk_pwrite(blk, offset, (uint8_t *)buf, count); 453797ac58cSKevin Wolf if (*total < 0) { 454797ac58cSKevin Wolf return *total; 455797ac58cSKevin Wolf } 456797ac58cSKevin Wolf return 1; 457797ac58cSKevin Wolf } 458797ac58cSKevin Wolf 459797ac58cSKevin Wolf typedef struct { 4604c7b7e9bSMax Reitz BlockBackend *blk; 461797ac58cSKevin Wolf int64_t offset; 462*9b0beaf3SJohn Snow int64_t count; 463*9b0beaf3SJohn Snow int64_t *total; 464797ac58cSKevin Wolf int ret; 465797ac58cSKevin Wolf bool done; 466797ac58cSKevin Wolf } CoWriteZeroes; 467797ac58cSKevin Wolf 468797ac58cSKevin Wolf static void coroutine_fn co_write_zeroes_entry(void *opaque) 469797ac58cSKevin Wolf { 470797ac58cSKevin Wolf CoWriteZeroes *data = opaque; 471797ac58cSKevin Wolf 4724c7b7e9bSMax Reitz data->ret = blk_co_write_zeroes(data->blk, data->offset / BDRV_SECTOR_SIZE, 473aa7bfbffSPeter Lieven data->count / BDRV_SECTOR_SIZE, 0); 474797ac58cSKevin Wolf data->done = true; 475797ac58cSKevin Wolf if (data->ret < 0) { 476797ac58cSKevin Wolf *data->total = data->ret; 477797ac58cSKevin Wolf return; 478797ac58cSKevin Wolf } 479797ac58cSKevin Wolf 480797ac58cSKevin Wolf *data->total = data->count; 481797ac58cSKevin Wolf } 482797ac58cSKevin Wolf 483*9b0beaf3SJohn Snow static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int64_t count, 484*9b0beaf3SJohn Snow int64_t *total) 485797ac58cSKevin Wolf { 486797ac58cSKevin Wolf Coroutine *co; 487797ac58cSKevin Wolf CoWriteZeroes data = { 4884c7b7e9bSMax Reitz .blk = blk, 489797ac58cSKevin Wolf .offset = offset, 490797ac58cSKevin Wolf .count = count, 491797ac58cSKevin Wolf .total = total, 492797ac58cSKevin Wolf .done = false, 493797ac58cSKevin Wolf }; 494797ac58cSKevin Wolf 495*9b0beaf3SJohn Snow if (count >> BDRV_SECTOR_BITS > INT_MAX) { 496*9b0beaf3SJohn Snow return -ERANGE; 497*9b0beaf3SJohn Snow } 498*9b0beaf3SJohn Snow 499797ac58cSKevin Wolf co = qemu_coroutine_create(co_write_zeroes_entry); 500797ac58cSKevin Wolf qemu_coroutine_enter(co, &data); 501797ac58cSKevin Wolf while (!data.done) { 5024c7b7e9bSMax Reitz aio_poll(blk_get_aio_context(blk), true); 503797ac58cSKevin Wolf } 504797ac58cSKevin Wolf if (data.ret < 0) { 505797ac58cSKevin Wolf return data.ret; 506797ac58cSKevin Wolf } else { 507797ac58cSKevin Wolf return 1; 508797ac58cSKevin Wolf } 509797ac58cSKevin Wolf } 510797ac58cSKevin Wolf 5114c7b7e9bSMax Reitz static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset, 512*9b0beaf3SJohn Snow int64_t count, int64_t *total) 513797ac58cSKevin Wolf { 514797ac58cSKevin Wolf int ret; 515797ac58cSKevin Wolf 516*9b0beaf3SJohn Snow if (count >> 9 > INT_MAX) { 517*9b0beaf3SJohn Snow return -ERANGE; 518*9b0beaf3SJohn Snow } 519*9b0beaf3SJohn Snow 5204c7b7e9bSMax Reitz ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9); 521797ac58cSKevin Wolf if (ret < 0) { 522797ac58cSKevin Wolf return ret; 523797ac58cSKevin Wolf } 524797ac58cSKevin Wolf *total = count; 525797ac58cSKevin Wolf return 1; 526797ac58cSKevin Wolf } 527797ac58cSKevin Wolf 5284c7b7e9bSMax Reitz static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset, 529*9b0beaf3SJohn Snow int64_t count, int64_t *total) 530797ac58cSKevin Wolf { 531*9b0beaf3SJohn Snow if (count > INT_MAX) { 532*9b0beaf3SJohn Snow return -ERANGE; 533*9b0beaf3SJohn Snow } 534*9b0beaf3SJohn Snow 5354c7b7e9bSMax Reitz *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count); 536797ac58cSKevin Wolf if (*total < 0) { 537797ac58cSKevin Wolf return *total; 538797ac58cSKevin Wolf } 539797ac58cSKevin Wolf return 1; 540797ac58cSKevin Wolf } 541797ac58cSKevin Wolf 5424c7b7e9bSMax Reitz static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset, 543*9b0beaf3SJohn Snow int64_t count, int64_t *total) 544797ac58cSKevin Wolf { 545*9b0beaf3SJohn Snow if (count > INT_MAX) { 546*9b0beaf3SJohn Snow return -ERANGE; 547*9b0beaf3SJohn Snow } 548*9b0beaf3SJohn Snow 5494c7b7e9bSMax Reitz *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count); 550797ac58cSKevin Wolf if (*total < 0) { 551797ac58cSKevin Wolf return *total; 552797ac58cSKevin Wolf } 553797ac58cSKevin Wolf return 1; 554797ac58cSKevin Wolf } 555797ac58cSKevin Wolf 556797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff 557797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret) 558797ac58cSKevin Wolf { 559797ac58cSKevin Wolf *(int *)opaque = ret; 560797ac58cSKevin Wolf } 561797ac58cSKevin Wolf 5624c7b7e9bSMax Reitz static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov, 563797ac58cSKevin Wolf int64_t offset, int *total) 564797ac58cSKevin Wolf { 565797ac58cSKevin Wolf int async_ret = NOT_DONE; 566797ac58cSKevin Wolf 5674c7b7e9bSMax Reitz blk_aio_readv(blk, offset >> 9, qiov, qiov->size >> 9, 568797ac58cSKevin Wolf aio_rw_done, &async_ret); 569797ac58cSKevin Wolf while (async_ret == NOT_DONE) { 570797ac58cSKevin Wolf main_loop_wait(false); 571797ac58cSKevin Wolf } 572797ac58cSKevin Wolf 573797ac58cSKevin Wolf *total = qiov->size; 574797ac58cSKevin Wolf return async_ret < 0 ? async_ret : 1; 575797ac58cSKevin Wolf } 576797ac58cSKevin Wolf 5774c7b7e9bSMax Reitz static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov, 578797ac58cSKevin Wolf int64_t offset, int *total) 579797ac58cSKevin Wolf { 580797ac58cSKevin Wolf int async_ret = NOT_DONE; 581797ac58cSKevin Wolf 5824c7b7e9bSMax Reitz blk_aio_writev(blk, offset >> 9, qiov, qiov->size >> 9, 583797ac58cSKevin Wolf aio_rw_done, &async_ret); 584797ac58cSKevin Wolf while (async_ret == NOT_DONE) { 585797ac58cSKevin Wolf main_loop_wait(false); 586797ac58cSKevin Wolf } 587797ac58cSKevin Wolf 588797ac58cSKevin Wolf *total = qiov->size; 589797ac58cSKevin Wolf return async_ret < 0 ? async_ret : 1; 590797ac58cSKevin Wolf } 591797ac58cSKevin Wolf 592797ac58cSKevin Wolf struct multiwrite_async_ret { 593797ac58cSKevin Wolf int num_done; 594797ac58cSKevin Wolf int error; 595797ac58cSKevin Wolf }; 596797ac58cSKevin Wolf 597797ac58cSKevin Wolf static void multiwrite_cb(void *opaque, int ret) 598797ac58cSKevin Wolf { 599797ac58cSKevin Wolf struct multiwrite_async_ret *async_ret = opaque; 600797ac58cSKevin Wolf 601797ac58cSKevin Wolf async_ret->num_done++; 602797ac58cSKevin Wolf if (ret < 0) { 603797ac58cSKevin Wolf async_ret->error = ret; 604797ac58cSKevin Wolf } 605797ac58cSKevin Wolf } 606797ac58cSKevin Wolf 6074c7b7e9bSMax Reitz static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs, 608797ac58cSKevin Wolf int num_reqs, int *total) 609797ac58cSKevin Wolf { 610797ac58cSKevin Wolf int i, ret; 611797ac58cSKevin Wolf struct multiwrite_async_ret async_ret = { 612797ac58cSKevin Wolf .num_done = 0, 613797ac58cSKevin Wolf .error = 0, 614797ac58cSKevin Wolf }; 615797ac58cSKevin Wolf 616797ac58cSKevin Wolf *total = 0; 617797ac58cSKevin Wolf for (i = 0; i < num_reqs; i++) { 618797ac58cSKevin Wolf reqs[i].cb = multiwrite_cb; 619797ac58cSKevin Wolf reqs[i].opaque = &async_ret; 620797ac58cSKevin Wolf *total += reqs[i].qiov->size; 621797ac58cSKevin Wolf } 622797ac58cSKevin Wolf 6234c7b7e9bSMax Reitz ret = blk_aio_multiwrite(blk, reqs, num_reqs); 624797ac58cSKevin Wolf if (ret < 0) { 625797ac58cSKevin Wolf return ret; 626797ac58cSKevin Wolf } 627797ac58cSKevin Wolf 628797ac58cSKevin Wolf while (async_ret.num_done < num_reqs) { 629797ac58cSKevin Wolf main_loop_wait(false); 630797ac58cSKevin Wolf } 631797ac58cSKevin Wolf 632797ac58cSKevin Wolf return async_ret.error < 0 ? async_ret.error : 1; 633797ac58cSKevin Wolf } 634797ac58cSKevin Wolf 635797ac58cSKevin Wolf static void read_help(void) 636797ac58cSKevin Wolf { 637797ac58cSKevin Wolf printf( 638797ac58cSKevin Wolf "\n" 639797ac58cSKevin Wolf " reads a range of bytes from the given offset\n" 640797ac58cSKevin Wolf "\n" 641797ac58cSKevin Wolf " Example:\n" 642797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n" 643797ac58cSKevin Wolf "\n" 644797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 645797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 646797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n" 647797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 648797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n" 6494c7b7e9bSMax Reitz " -p, -- use blk_pread to read the file\n" 650797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 651797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 652797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n" 653797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 654797ac58cSKevin Wolf "\n"); 655797ac58cSKevin Wolf } 656797ac58cSKevin Wolf 6574c7b7e9bSMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv); 658797ac58cSKevin Wolf 659797ac58cSKevin Wolf static const cmdinfo_t read_cmd = { 660797ac58cSKevin Wolf .name = "read", 661797ac58cSKevin Wolf .altname = "r", 662797ac58cSKevin Wolf .cfunc = read_f, 663797ac58cSKevin Wolf .argmin = 2, 664797ac58cSKevin Wolf .argmax = -1, 665797ac58cSKevin Wolf .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len", 666797ac58cSKevin Wolf .oneline = "reads a number of bytes at a specified offset", 667797ac58cSKevin Wolf .help = read_help, 668797ac58cSKevin Wolf }; 669797ac58cSKevin Wolf 6704c7b7e9bSMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv) 671797ac58cSKevin Wolf { 672797ac58cSKevin Wolf struct timeval t1, t2; 673797ac58cSKevin Wolf int Cflag = 0, pflag = 0, qflag = 0, vflag = 0; 674797ac58cSKevin Wolf int Pflag = 0, sflag = 0, lflag = 0, bflag = 0; 675797ac58cSKevin Wolf int c, cnt; 676797ac58cSKevin Wolf char *buf; 677797ac58cSKevin Wolf int64_t offset; 678*9b0beaf3SJohn Snow int64_t count; 679797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 680*9b0beaf3SJohn Snow int64_t total = 0; 681*9b0beaf3SJohn Snow int pattern = 0; 682*9b0beaf3SJohn Snow int64_t pattern_offset = 0, pattern_count = 0; 683797ac58cSKevin Wolf 684b062ad86SEric Blake while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) { 685797ac58cSKevin Wolf switch (c) { 686797ac58cSKevin Wolf case 'b': 687797ac58cSKevin Wolf bflag = 1; 688797ac58cSKevin Wolf break; 689797ac58cSKevin Wolf case 'C': 690797ac58cSKevin Wolf Cflag = 1; 691797ac58cSKevin Wolf break; 692797ac58cSKevin Wolf case 'l': 693797ac58cSKevin Wolf lflag = 1; 694797ac58cSKevin Wolf pattern_count = cvtnum(optarg); 695797ac58cSKevin Wolf if (pattern_count < 0) { 696797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", optarg); 697797ac58cSKevin Wolf return 0; 698797ac58cSKevin Wolf } 699797ac58cSKevin Wolf break; 700797ac58cSKevin Wolf case 'p': 701797ac58cSKevin Wolf pflag = 1; 702797ac58cSKevin Wolf break; 703797ac58cSKevin Wolf case 'P': 704797ac58cSKevin Wolf Pflag = 1; 705797ac58cSKevin Wolf pattern = parse_pattern(optarg); 706797ac58cSKevin Wolf if (pattern < 0) { 707797ac58cSKevin Wolf return 0; 708797ac58cSKevin Wolf } 709797ac58cSKevin Wolf break; 710797ac58cSKevin Wolf case 'q': 711797ac58cSKevin Wolf qflag = 1; 712797ac58cSKevin Wolf break; 713797ac58cSKevin Wolf case 's': 714797ac58cSKevin Wolf sflag = 1; 715797ac58cSKevin Wolf pattern_offset = cvtnum(optarg); 716797ac58cSKevin Wolf if (pattern_offset < 0) { 717797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", optarg); 718797ac58cSKevin Wolf return 0; 719797ac58cSKevin Wolf } 720797ac58cSKevin Wolf break; 721797ac58cSKevin Wolf case 'v': 722797ac58cSKevin Wolf vflag = 1; 723797ac58cSKevin Wolf break; 724797ac58cSKevin Wolf default: 725c2cdf5c5SKevin Wolf return qemuio_command_usage(&read_cmd); 726797ac58cSKevin Wolf } 727797ac58cSKevin Wolf } 728797ac58cSKevin Wolf 729797ac58cSKevin Wolf if (optind != argc - 2) { 730c2cdf5c5SKevin Wolf return qemuio_command_usage(&read_cmd); 731797ac58cSKevin Wolf } 732797ac58cSKevin Wolf 733797ac58cSKevin Wolf if (bflag && pflag) { 734797ac58cSKevin Wolf printf("-b and -p cannot be specified at the same time\n"); 735797ac58cSKevin Wolf return 0; 736797ac58cSKevin Wolf } 737797ac58cSKevin Wolf 738797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 739797ac58cSKevin Wolf if (offset < 0) { 740797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 741797ac58cSKevin Wolf return 0; 742797ac58cSKevin Wolf } 743797ac58cSKevin Wolf 744797ac58cSKevin Wolf optind++; 745797ac58cSKevin Wolf count = cvtnum(argv[optind]); 746797ac58cSKevin Wolf if (count < 0) { 747797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 748797ac58cSKevin Wolf return 0; 749*9b0beaf3SJohn Snow } else if (count > SIZE_MAX) { 750*9b0beaf3SJohn Snow printf("length cannot exceed %" PRIu64 ", given %s\n", 751*9b0beaf3SJohn Snow (uint64_t) SIZE_MAX, argv[optind]); 752*9b0beaf3SJohn Snow return 0; 753797ac58cSKevin Wolf } 754797ac58cSKevin Wolf 755797ac58cSKevin Wolf if (!Pflag && (lflag || sflag)) { 756c2cdf5c5SKevin Wolf return qemuio_command_usage(&read_cmd); 757797ac58cSKevin Wolf } 758797ac58cSKevin Wolf 759797ac58cSKevin Wolf if (!lflag) { 760797ac58cSKevin Wolf pattern_count = count - pattern_offset; 761797ac58cSKevin Wolf } 762797ac58cSKevin Wolf 763797ac58cSKevin Wolf if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) { 764797ac58cSKevin Wolf printf("pattern verification range exceeds end of read data\n"); 765797ac58cSKevin Wolf return 0; 766797ac58cSKevin Wolf } 767797ac58cSKevin Wolf 768797ac58cSKevin Wolf if (!pflag) { 769797ac58cSKevin Wolf if (offset & 0x1ff) { 770797ac58cSKevin Wolf printf("offset %" PRId64 " is not sector aligned\n", 771797ac58cSKevin Wolf offset); 772797ac58cSKevin Wolf return 0; 773797ac58cSKevin Wolf } 774797ac58cSKevin Wolf if (count & 0x1ff) { 775*9b0beaf3SJohn Snow printf("count %"PRId64" is not sector aligned\n", 776797ac58cSKevin Wolf count); 777797ac58cSKevin Wolf return 0; 778797ac58cSKevin Wolf } 779797ac58cSKevin Wolf } 780797ac58cSKevin Wolf 7814c7b7e9bSMax Reitz buf = qemu_io_alloc(blk, count, 0xab); 782797ac58cSKevin Wolf 783797ac58cSKevin Wolf gettimeofday(&t1, NULL); 784797ac58cSKevin Wolf if (pflag) { 7854c7b7e9bSMax Reitz cnt = do_pread(blk, buf, offset, count, &total); 786797ac58cSKevin Wolf } else if (bflag) { 7874c7b7e9bSMax Reitz cnt = do_load_vmstate(blk, buf, offset, count, &total); 788797ac58cSKevin Wolf } else { 7894c7b7e9bSMax Reitz cnt = do_read(blk, buf, offset, count, &total); 790797ac58cSKevin Wolf } 791797ac58cSKevin Wolf gettimeofday(&t2, NULL); 792797ac58cSKevin Wolf 793797ac58cSKevin Wolf if (cnt < 0) { 794797ac58cSKevin Wolf printf("read failed: %s\n", strerror(-cnt)); 795797ac58cSKevin Wolf goto out; 796797ac58cSKevin Wolf } 797797ac58cSKevin Wolf 798797ac58cSKevin Wolf if (Pflag) { 799797ac58cSKevin Wolf void *cmp_buf = g_malloc(pattern_count); 800797ac58cSKevin Wolf memset(cmp_buf, pattern, pattern_count); 801797ac58cSKevin Wolf if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) { 802797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 803*9b0beaf3SJohn Snow PRId64 ", %"PRId64" bytes\n", 804797ac58cSKevin Wolf offset + pattern_offset, pattern_count); 805797ac58cSKevin Wolf } 806797ac58cSKevin Wolf g_free(cmp_buf); 807797ac58cSKevin Wolf } 808797ac58cSKevin Wolf 809797ac58cSKevin Wolf if (qflag) { 810797ac58cSKevin Wolf goto out; 811797ac58cSKevin Wolf } 812797ac58cSKevin Wolf 813797ac58cSKevin Wolf if (vflag) { 814797ac58cSKevin Wolf dump_buffer(buf, offset, count); 815797ac58cSKevin Wolf } 816797ac58cSKevin Wolf 817797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 818797ac58cSKevin Wolf t2 = tsub(t2, t1); 819797ac58cSKevin Wolf print_report("read", &t2, offset, count, total, cnt, Cflag); 820797ac58cSKevin Wolf 821797ac58cSKevin Wolf out: 822797ac58cSKevin Wolf qemu_io_free(buf); 823797ac58cSKevin Wolf 824797ac58cSKevin Wolf return 0; 825797ac58cSKevin Wolf } 826797ac58cSKevin Wolf 827797ac58cSKevin Wolf static void readv_help(void) 828797ac58cSKevin Wolf { 829797ac58cSKevin Wolf printf( 830797ac58cSKevin Wolf "\n" 831797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n" 832797ac58cSKevin Wolf "\n" 833797ac58cSKevin Wolf " Example:\n" 834797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" 835797ac58cSKevin Wolf "\n" 836797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 837797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 838797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n" 839797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 840797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 841797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 842797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 843797ac58cSKevin Wolf "\n"); 844797ac58cSKevin Wolf } 845797ac58cSKevin Wolf 8464c7b7e9bSMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv); 847797ac58cSKevin Wolf 848797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = { 849797ac58cSKevin Wolf .name = "readv", 850797ac58cSKevin Wolf .cfunc = readv_f, 851797ac58cSKevin Wolf .argmin = 2, 852797ac58cSKevin Wolf .argmax = -1, 853797ac58cSKevin Wolf .args = "[-Cqv] [-P pattern ] off len [len..]", 854797ac58cSKevin Wolf .oneline = "reads a number of bytes at a specified offset", 855797ac58cSKevin Wolf .help = readv_help, 856797ac58cSKevin Wolf }; 857797ac58cSKevin Wolf 8584c7b7e9bSMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv) 859797ac58cSKevin Wolf { 860797ac58cSKevin Wolf struct timeval t1, t2; 861797ac58cSKevin Wolf int Cflag = 0, qflag = 0, vflag = 0; 862797ac58cSKevin Wolf int c, cnt; 863797ac58cSKevin Wolf char *buf; 864797ac58cSKevin Wolf int64_t offset; 865797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 866797ac58cSKevin Wolf int total = 0; 867797ac58cSKevin Wolf int nr_iov; 868797ac58cSKevin Wolf QEMUIOVector qiov; 869797ac58cSKevin Wolf int pattern = 0; 870797ac58cSKevin Wolf int Pflag = 0; 871797ac58cSKevin Wolf 872b062ad86SEric Blake while ((c = getopt(argc, argv, "CP:qv")) != -1) { 873797ac58cSKevin Wolf switch (c) { 874797ac58cSKevin Wolf case 'C': 875797ac58cSKevin Wolf Cflag = 1; 876797ac58cSKevin Wolf break; 877797ac58cSKevin Wolf case 'P': 878797ac58cSKevin Wolf Pflag = 1; 879797ac58cSKevin Wolf pattern = parse_pattern(optarg); 880797ac58cSKevin Wolf if (pattern < 0) { 881797ac58cSKevin Wolf return 0; 882797ac58cSKevin Wolf } 883797ac58cSKevin Wolf break; 884797ac58cSKevin Wolf case 'q': 885797ac58cSKevin Wolf qflag = 1; 886797ac58cSKevin Wolf break; 887797ac58cSKevin Wolf case 'v': 888797ac58cSKevin Wolf vflag = 1; 889797ac58cSKevin Wolf break; 890797ac58cSKevin Wolf default: 891c2cdf5c5SKevin Wolf return qemuio_command_usage(&readv_cmd); 892797ac58cSKevin Wolf } 893797ac58cSKevin Wolf } 894797ac58cSKevin Wolf 895797ac58cSKevin Wolf if (optind > argc - 2) { 896c2cdf5c5SKevin Wolf return qemuio_command_usage(&readv_cmd); 897797ac58cSKevin Wolf } 898797ac58cSKevin Wolf 899797ac58cSKevin Wolf 900797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 901797ac58cSKevin Wolf if (offset < 0) { 902797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 903797ac58cSKevin Wolf return 0; 904797ac58cSKevin Wolf } 905797ac58cSKevin Wolf optind++; 906797ac58cSKevin Wolf 907797ac58cSKevin Wolf if (offset & 0x1ff) { 908797ac58cSKevin Wolf printf("offset %" PRId64 " is not sector aligned\n", 909797ac58cSKevin Wolf offset); 910797ac58cSKevin Wolf return 0; 911797ac58cSKevin Wolf } 912797ac58cSKevin Wolf 913797ac58cSKevin Wolf nr_iov = argc - optind; 9144c7b7e9bSMax Reitz buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab); 915797ac58cSKevin Wolf if (buf == NULL) { 916797ac58cSKevin Wolf return 0; 917797ac58cSKevin Wolf } 918797ac58cSKevin Wolf 919797ac58cSKevin Wolf gettimeofday(&t1, NULL); 9204c7b7e9bSMax Reitz cnt = do_aio_readv(blk, &qiov, offset, &total); 921797ac58cSKevin Wolf gettimeofday(&t2, NULL); 922797ac58cSKevin Wolf 923797ac58cSKevin Wolf if (cnt < 0) { 924797ac58cSKevin Wolf printf("readv failed: %s\n", strerror(-cnt)); 925797ac58cSKevin Wolf goto out; 926797ac58cSKevin Wolf } 927797ac58cSKevin Wolf 928797ac58cSKevin Wolf if (Pflag) { 929797ac58cSKevin Wolf void *cmp_buf = g_malloc(qiov.size); 930797ac58cSKevin Wolf memset(cmp_buf, pattern, qiov.size); 931797ac58cSKevin Wolf if (memcmp(buf, cmp_buf, qiov.size)) { 932797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 933797ac58cSKevin Wolf PRId64 ", %zd bytes\n", offset, qiov.size); 934797ac58cSKevin Wolf } 935797ac58cSKevin Wolf g_free(cmp_buf); 936797ac58cSKevin Wolf } 937797ac58cSKevin Wolf 938797ac58cSKevin Wolf if (qflag) { 939797ac58cSKevin Wolf goto out; 940797ac58cSKevin Wolf } 941797ac58cSKevin Wolf 942797ac58cSKevin Wolf if (vflag) { 943797ac58cSKevin Wolf dump_buffer(buf, offset, qiov.size); 944797ac58cSKevin Wolf } 945797ac58cSKevin Wolf 946797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 947797ac58cSKevin Wolf t2 = tsub(t2, t1); 948797ac58cSKevin Wolf print_report("read", &t2, offset, qiov.size, total, cnt, Cflag); 949797ac58cSKevin Wolf 950797ac58cSKevin Wolf out: 951797ac58cSKevin Wolf qemu_iovec_destroy(&qiov); 952797ac58cSKevin Wolf qemu_io_free(buf); 953797ac58cSKevin Wolf return 0; 954797ac58cSKevin Wolf } 955797ac58cSKevin Wolf 956797ac58cSKevin Wolf static void write_help(void) 957797ac58cSKevin Wolf { 958797ac58cSKevin Wolf printf( 959797ac58cSKevin Wolf "\n" 960797ac58cSKevin Wolf " writes a range of bytes from the given offset\n" 961797ac58cSKevin Wolf "\n" 962797ac58cSKevin Wolf " Example:\n" 963797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n" 964797ac58cSKevin Wolf "\n" 965797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 966797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 967797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n" 9684c7b7e9bSMax Reitz " -c, -- write compressed data with blk_write_compressed\n" 9694c7b7e9bSMax Reitz " -p, -- use blk_pwrite to write the file\n" 970797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 971797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 972797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 9734c7b7e9bSMax Reitz " -z, -- write zeroes using blk_co_write_zeroes\n" 974797ac58cSKevin Wolf "\n"); 975797ac58cSKevin Wolf } 976797ac58cSKevin Wolf 9774c7b7e9bSMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv); 978797ac58cSKevin Wolf 979797ac58cSKevin Wolf static const cmdinfo_t write_cmd = { 980797ac58cSKevin Wolf .name = "write", 981797ac58cSKevin Wolf .altname = "w", 982797ac58cSKevin Wolf .cfunc = write_f, 983797ac58cSKevin Wolf .argmin = 2, 984797ac58cSKevin Wolf .argmax = -1, 985797ac58cSKevin Wolf .args = "[-bcCpqz] [-P pattern ] off len", 986797ac58cSKevin Wolf .oneline = "writes a number of bytes at a specified offset", 987797ac58cSKevin Wolf .help = write_help, 988797ac58cSKevin Wolf }; 989797ac58cSKevin Wolf 9904c7b7e9bSMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv) 991797ac58cSKevin Wolf { 992797ac58cSKevin Wolf struct timeval t1, t2; 993797ac58cSKevin Wolf int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0; 994797ac58cSKevin Wolf int cflag = 0; 995797ac58cSKevin Wolf int c, cnt; 996797ac58cSKevin Wolf char *buf = NULL; 997797ac58cSKevin Wolf int64_t offset; 998*9b0beaf3SJohn Snow int64_t count; 999797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 1000*9b0beaf3SJohn Snow int64_t total = 0; 1001797ac58cSKevin Wolf int pattern = 0xcd; 1002797ac58cSKevin Wolf 1003b062ad86SEric Blake while ((c = getopt(argc, argv, "bcCpP:qz")) != -1) { 1004797ac58cSKevin Wolf switch (c) { 1005797ac58cSKevin Wolf case 'b': 1006797ac58cSKevin Wolf bflag = 1; 1007797ac58cSKevin Wolf break; 1008797ac58cSKevin Wolf case 'c': 1009797ac58cSKevin Wolf cflag = 1; 1010797ac58cSKevin Wolf break; 1011797ac58cSKevin Wolf case 'C': 1012797ac58cSKevin Wolf Cflag = 1; 1013797ac58cSKevin Wolf break; 1014797ac58cSKevin Wolf case 'p': 1015797ac58cSKevin Wolf pflag = 1; 1016797ac58cSKevin Wolf break; 1017797ac58cSKevin Wolf case 'P': 1018797ac58cSKevin Wolf Pflag = 1; 1019797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1020797ac58cSKevin Wolf if (pattern < 0) { 1021797ac58cSKevin Wolf return 0; 1022797ac58cSKevin Wolf } 1023797ac58cSKevin Wolf break; 1024797ac58cSKevin Wolf case 'q': 1025797ac58cSKevin Wolf qflag = 1; 1026797ac58cSKevin Wolf break; 1027797ac58cSKevin Wolf case 'z': 1028797ac58cSKevin Wolf zflag = 1; 1029797ac58cSKevin Wolf break; 1030797ac58cSKevin Wolf default: 1031c2cdf5c5SKevin Wolf return qemuio_command_usage(&write_cmd); 1032797ac58cSKevin Wolf } 1033797ac58cSKevin Wolf } 1034797ac58cSKevin Wolf 1035797ac58cSKevin Wolf if (optind != argc - 2) { 1036c2cdf5c5SKevin Wolf return qemuio_command_usage(&write_cmd); 1037797ac58cSKevin Wolf } 1038797ac58cSKevin Wolf 1039797ac58cSKevin Wolf if (bflag + pflag + zflag > 1) { 1040797ac58cSKevin Wolf printf("-b, -p, or -z cannot be specified at the same time\n"); 1041797ac58cSKevin Wolf return 0; 1042797ac58cSKevin Wolf } 1043797ac58cSKevin Wolf 1044797ac58cSKevin Wolf if (zflag && Pflag) { 1045797ac58cSKevin Wolf printf("-z and -P cannot be specified at the same time\n"); 1046797ac58cSKevin Wolf return 0; 1047797ac58cSKevin Wolf } 1048797ac58cSKevin Wolf 1049797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1050797ac58cSKevin Wolf if (offset < 0) { 1051797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 1052797ac58cSKevin Wolf return 0; 1053797ac58cSKevin Wolf } 1054797ac58cSKevin Wolf 1055797ac58cSKevin Wolf optind++; 1056797ac58cSKevin Wolf count = cvtnum(argv[optind]); 1057797ac58cSKevin Wolf if (count < 0) { 1058797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 1059797ac58cSKevin Wolf return 0; 1060*9b0beaf3SJohn Snow } else if (count > SIZE_MAX) { 1061*9b0beaf3SJohn Snow printf("length cannot exceed %" PRIu64 ", given %s\n", 1062*9b0beaf3SJohn Snow (uint64_t) SIZE_MAX, argv[optind]); 1063*9b0beaf3SJohn Snow return 0; 1064797ac58cSKevin Wolf } 1065797ac58cSKevin Wolf 1066797ac58cSKevin Wolf if (!pflag) { 1067797ac58cSKevin Wolf if (offset & 0x1ff) { 1068797ac58cSKevin Wolf printf("offset %" PRId64 " is not sector aligned\n", 1069797ac58cSKevin Wolf offset); 1070797ac58cSKevin Wolf return 0; 1071797ac58cSKevin Wolf } 1072797ac58cSKevin Wolf 1073797ac58cSKevin Wolf if (count & 0x1ff) { 1074*9b0beaf3SJohn Snow printf("count %"PRId64" is not sector aligned\n", 1075797ac58cSKevin Wolf count); 1076797ac58cSKevin Wolf return 0; 1077797ac58cSKevin Wolf } 1078797ac58cSKevin Wolf } 1079797ac58cSKevin Wolf 1080797ac58cSKevin Wolf if (!zflag) { 10814c7b7e9bSMax Reitz buf = qemu_io_alloc(blk, count, pattern); 1082797ac58cSKevin Wolf } 1083797ac58cSKevin Wolf 1084797ac58cSKevin Wolf gettimeofday(&t1, NULL); 1085797ac58cSKevin Wolf if (pflag) { 10864c7b7e9bSMax Reitz cnt = do_pwrite(blk, buf, offset, count, &total); 1087797ac58cSKevin Wolf } else if (bflag) { 10884c7b7e9bSMax Reitz cnt = do_save_vmstate(blk, buf, offset, count, &total); 1089797ac58cSKevin Wolf } else if (zflag) { 10904c7b7e9bSMax Reitz cnt = do_co_write_zeroes(blk, offset, count, &total); 1091797ac58cSKevin Wolf } else if (cflag) { 10924c7b7e9bSMax Reitz cnt = do_write_compressed(blk, buf, offset, count, &total); 1093797ac58cSKevin Wolf } else { 10944c7b7e9bSMax Reitz cnt = do_write(blk, buf, offset, count, &total); 1095797ac58cSKevin Wolf } 1096797ac58cSKevin Wolf gettimeofday(&t2, NULL); 1097797ac58cSKevin Wolf 1098797ac58cSKevin Wolf if (cnt < 0) { 1099797ac58cSKevin Wolf printf("write failed: %s\n", strerror(-cnt)); 1100797ac58cSKevin Wolf goto out; 1101797ac58cSKevin Wolf } 1102797ac58cSKevin Wolf 1103797ac58cSKevin Wolf if (qflag) { 1104797ac58cSKevin Wolf goto out; 1105797ac58cSKevin Wolf } 1106797ac58cSKevin Wolf 1107797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1108797ac58cSKevin Wolf t2 = tsub(t2, t1); 1109797ac58cSKevin Wolf print_report("wrote", &t2, offset, count, total, cnt, Cflag); 1110797ac58cSKevin Wolf 1111797ac58cSKevin Wolf out: 1112797ac58cSKevin Wolf if (!zflag) { 1113797ac58cSKevin Wolf qemu_io_free(buf); 1114797ac58cSKevin Wolf } 1115797ac58cSKevin Wolf 1116797ac58cSKevin Wolf return 0; 1117797ac58cSKevin Wolf } 1118797ac58cSKevin Wolf 1119797ac58cSKevin Wolf static void 1120797ac58cSKevin Wolf writev_help(void) 1121797ac58cSKevin Wolf { 1122797ac58cSKevin Wolf printf( 1123797ac58cSKevin Wolf "\n" 1124797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n" 1125797ac58cSKevin Wolf "\n" 1126797ac58cSKevin Wolf " Example:\n" 11276e6507c0SMaria Kustova " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" 1128797ac58cSKevin Wolf "\n" 1129797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1130797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1131797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 1132797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1133797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1134797ac58cSKevin Wolf "\n"); 1135797ac58cSKevin Wolf } 1136797ac58cSKevin Wolf 11374c7b7e9bSMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv); 1138797ac58cSKevin Wolf 1139797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = { 1140797ac58cSKevin Wolf .name = "writev", 1141797ac58cSKevin Wolf .cfunc = writev_f, 1142797ac58cSKevin Wolf .argmin = 2, 1143797ac58cSKevin Wolf .argmax = -1, 1144797ac58cSKevin Wolf .args = "[-Cq] [-P pattern ] off len [len..]", 1145797ac58cSKevin Wolf .oneline = "writes a number of bytes at a specified offset", 1146797ac58cSKevin Wolf .help = writev_help, 1147797ac58cSKevin Wolf }; 1148797ac58cSKevin Wolf 11494c7b7e9bSMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv) 1150797ac58cSKevin Wolf { 1151797ac58cSKevin Wolf struct timeval t1, t2; 1152797ac58cSKevin Wolf int Cflag = 0, qflag = 0; 1153797ac58cSKevin Wolf int c, cnt; 1154797ac58cSKevin Wolf char *buf; 1155797ac58cSKevin Wolf int64_t offset; 1156797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 1157797ac58cSKevin Wolf int total = 0; 1158797ac58cSKevin Wolf int nr_iov; 1159797ac58cSKevin Wolf int pattern = 0xcd; 1160797ac58cSKevin Wolf QEMUIOVector qiov; 1161797ac58cSKevin Wolf 1162b062ad86SEric Blake while ((c = getopt(argc, argv, "CqP:")) != -1) { 1163797ac58cSKevin Wolf switch (c) { 1164797ac58cSKevin Wolf case 'C': 1165797ac58cSKevin Wolf Cflag = 1; 1166797ac58cSKevin Wolf break; 1167797ac58cSKevin Wolf case 'q': 1168797ac58cSKevin Wolf qflag = 1; 1169797ac58cSKevin Wolf break; 1170797ac58cSKevin Wolf case 'P': 1171797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1172797ac58cSKevin Wolf if (pattern < 0) { 1173797ac58cSKevin Wolf return 0; 1174797ac58cSKevin Wolf } 1175797ac58cSKevin Wolf break; 1176797ac58cSKevin Wolf default: 1177c2cdf5c5SKevin Wolf return qemuio_command_usage(&writev_cmd); 1178797ac58cSKevin Wolf } 1179797ac58cSKevin Wolf } 1180797ac58cSKevin Wolf 1181797ac58cSKevin Wolf if (optind > argc - 2) { 1182c2cdf5c5SKevin Wolf return qemuio_command_usage(&writev_cmd); 1183797ac58cSKevin Wolf } 1184797ac58cSKevin Wolf 1185797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1186797ac58cSKevin Wolf if (offset < 0) { 1187797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 1188797ac58cSKevin Wolf return 0; 1189797ac58cSKevin Wolf } 1190797ac58cSKevin Wolf optind++; 1191797ac58cSKevin Wolf 1192797ac58cSKevin Wolf if (offset & 0x1ff) { 1193797ac58cSKevin Wolf printf("offset %" PRId64 " is not sector aligned\n", 1194797ac58cSKevin Wolf offset); 1195797ac58cSKevin Wolf return 0; 1196797ac58cSKevin Wolf } 1197797ac58cSKevin Wolf 1198797ac58cSKevin Wolf nr_iov = argc - optind; 11994c7b7e9bSMax Reitz buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern); 1200797ac58cSKevin Wolf if (buf == NULL) { 1201797ac58cSKevin Wolf return 0; 1202797ac58cSKevin Wolf } 1203797ac58cSKevin Wolf 1204797ac58cSKevin Wolf gettimeofday(&t1, NULL); 12054c7b7e9bSMax Reitz cnt = do_aio_writev(blk, &qiov, offset, &total); 1206797ac58cSKevin Wolf gettimeofday(&t2, NULL); 1207797ac58cSKevin Wolf 1208797ac58cSKevin Wolf if (cnt < 0) { 1209797ac58cSKevin Wolf printf("writev failed: %s\n", strerror(-cnt)); 1210797ac58cSKevin Wolf goto out; 1211797ac58cSKevin Wolf } 1212797ac58cSKevin Wolf 1213797ac58cSKevin Wolf if (qflag) { 1214797ac58cSKevin Wolf goto out; 1215797ac58cSKevin Wolf } 1216797ac58cSKevin Wolf 1217797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1218797ac58cSKevin Wolf t2 = tsub(t2, t1); 1219797ac58cSKevin Wolf print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag); 1220797ac58cSKevin Wolf out: 1221797ac58cSKevin Wolf qemu_iovec_destroy(&qiov); 1222797ac58cSKevin Wolf qemu_io_free(buf); 1223797ac58cSKevin Wolf return 0; 1224797ac58cSKevin Wolf } 1225797ac58cSKevin Wolf 1226797ac58cSKevin Wolf static void multiwrite_help(void) 1227797ac58cSKevin Wolf { 1228797ac58cSKevin Wolf printf( 1229797ac58cSKevin Wolf "\n" 1230797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers,\n" 1231797ac58cSKevin Wolf " in a batch of requests that may be merged by qemu\n" 1232797ac58cSKevin Wolf "\n" 1233797ac58cSKevin Wolf " Example:\n" 1234797ac58cSKevin Wolf " 'multiwrite 512 1k 1k ; 4k 1k'\n" 1235797ac58cSKevin Wolf " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n" 1236797ac58cSKevin Wolf "\n" 1237797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1238797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n" 1239797ac58cSKevin Wolf " by one for each request contained in the multiwrite command.\n" 1240797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 1241797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1242797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1243797ac58cSKevin Wolf "\n"); 1244797ac58cSKevin Wolf } 1245797ac58cSKevin Wolf 12464c7b7e9bSMax Reitz static int multiwrite_f(BlockBackend *blk, int argc, char **argv); 1247797ac58cSKevin Wolf 1248797ac58cSKevin Wolf static const cmdinfo_t multiwrite_cmd = { 1249797ac58cSKevin Wolf .name = "multiwrite", 1250797ac58cSKevin Wolf .cfunc = multiwrite_f, 1251797ac58cSKevin Wolf .argmin = 2, 1252797ac58cSKevin Wolf .argmax = -1, 1253797ac58cSKevin Wolf .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]", 1254797ac58cSKevin Wolf .oneline = "issues multiple write requests at once", 1255797ac58cSKevin Wolf .help = multiwrite_help, 1256797ac58cSKevin Wolf }; 1257797ac58cSKevin Wolf 12584c7b7e9bSMax Reitz static int multiwrite_f(BlockBackend *blk, int argc, char **argv) 1259797ac58cSKevin Wolf { 1260797ac58cSKevin Wolf struct timeval t1, t2; 1261797ac58cSKevin Wolf int Cflag = 0, qflag = 0; 1262797ac58cSKevin Wolf int c, cnt; 1263797ac58cSKevin Wolf char **buf; 1264797ac58cSKevin Wolf int64_t offset, first_offset = 0; 1265797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 1266797ac58cSKevin Wolf int total = 0; 1267797ac58cSKevin Wolf int nr_iov; 1268797ac58cSKevin Wolf int nr_reqs; 1269797ac58cSKevin Wolf int pattern = 0xcd; 1270797ac58cSKevin Wolf QEMUIOVector *qiovs; 1271797ac58cSKevin Wolf int i; 1272797ac58cSKevin Wolf BlockRequest *reqs; 1273797ac58cSKevin Wolf 1274b062ad86SEric Blake while ((c = getopt(argc, argv, "CqP:")) != -1) { 1275797ac58cSKevin Wolf switch (c) { 1276797ac58cSKevin Wolf case 'C': 1277797ac58cSKevin Wolf Cflag = 1; 1278797ac58cSKevin Wolf break; 1279797ac58cSKevin Wolf case 'q': 1280797ac58cSKevin Wolf qflag = 1; 1281797ac58cSKevin Wolf break; 1282797ac58cSKevin Wolf case 'P': 1283797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1284797ac58cSKevin Wolf if (pattern < 0) { 1285797ac58cSKevin Wolf return 0; 1286797ac58cSKevin Wolf } 1287797ac58cSKevin Wolf break; 1288797ac58cSKevin Wolf default: 1289c2cdf5c5SKevin Wolf return qemuio_command_usage(&writev_cmd); 1290797ac58cSKevin Wolf } 1291797ac58cSKevin Wolf } 1292797ac58cSKevin Wolf 1293797ac58cSKevin Wolf if (optind > argc - 2) { 1294c2cdf5c5SKevin Wolf return qemuio_command_usage(&writev_cmd); 1295797ac58cSKevin Wolf } 1296797ac58cSKevin Wolf 1297797ac58cSKevin Wolf nr_reqs = 1; 1298797ac58cSKevin Wolf for (i = optind; i < argc; i++) { 1299797ac58cSKevin Wolf if (!strcmp(argv[i], ";")) { 1300797ac58cSKevin Wolf nr_reqs++; 1301797ac58cSKevin Wolf } 1302797ac58cSKevin Wolf } 1303797ac58cSKevin Wolf 130402c4f26bSMarkus Armbruster reqs = g_new0(BlockRequest, nr_reqs); 130502c4f26bSMarkus Armbruster buf = g_new0(char *, nr_reqs); 130602c4f26bSMarkus Armbruster qiovs = g_new(QEMUIOVector, nr_reqs); 1307797ac58cSKevin Wolf 1308797ac58cSKevin Wolf for (i = 0; i < nr_reqs && optind < argc; i++) { 1309797ac58cSKevin Wolf int j; 1310797ac58cSKevin Wolf 1311797ac58cSKevin Wolf /* Read the offset of the request */ 1312797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1313797ac58cSKevin Wolf if (offset < 0) { 1314797ac58cSKevin Wolf printf("non-numeric offset argument -- %s\n", argv[optind]); 1315797ac58cSKevin Wolf goto out; 1316797ac58cSKevin Wolf } 1317797ac58cSKevin Wolf optind++; 1318797ac58cSKevin Wolf 1319797ac58cSKevin Wolf if (offset & 0x1ff) { 1320797ac58cSKevin Wolf printf("offset %lld is not sector aligned\n", 1321797ac58cSKevin Wolf (long long)offset); 1322797ac58cSKevin Wolf goto out; 1323797ac58cSKevin Wolf } 1324797ac58cSKevin Wolf 1325797ac58cSKevin Wolf if (i == 0) { 1326797ac58cSKevin Wolf first_offset = offset; 1327797ac58cSKevin Wolf } 1328797ac58cSKevin Wolf 1329797ac58cSKevin Wolf /* Read lengths for qiov entries */ 1330797ac58cSKevin Wolf for (j = optind; j < argc; j++) { 1331797ac58cSKevin Wolf if (!strcmp(argv[j], ";")) { 1332797ac58cSKevin Wolf break; 1333797ac58cSKevin Wolf } 1334797ac58cSKevin Wolf } 1335797ac58cSKevin Wolf 1336797ac58cSKevin Wolf nr_iov = j - optind; 1337797ac58cSKevin Wolf 1338797ac58cSKevin Wolf /* Build request */ 13394c7b7e9bSMax Reitz buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern); 1340797ac58cSKevin Wolf if (buf[i] == NULL) { 1341797ac58cSKevin Wolf goto out; 1342797ac58cSKevin Wolf } 1343797ac58cSKevin Wolf 1344797ac58cSKevin Wolf reqs[i].qiov = &qiovs[i]; 1345797ac58cSKevin Wolf reqs[i].sector = offset >> 9; 1346797ac58cSKevin Wolf reqs[i].nb_sectors = reqs[i].qiov->size >> 9; 1347797ac58cSKevin Wolf 1348797ac58cSKevin Wolf optind = j + 1; 1349797ac58cSKevin Wolf 1350797ac58cSKevin Wolf pattern++; 1351797ac58cSKevin Wolf } 1352797ac58cSKevin Wolf 1353797ac58cSKevin Wolf /* If there were empty requests at the end, ignore them */ 1354797ac58cSKevin Wolf nr_reqs = i; 1355797ac58cSKevin Wolf 1356797ac58cSKevin Wolf gettimeofday(&t1, NULL); 13574c7b7e9bSMax Reitz cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total); 1358797ac58cSKevin Wolf gettimeofday(&t2, NULL); 1359797ac58cSKevin Wolf 1360797ac58cSKevin Wolf if (cnt < 0) { 1361797ac58cSKevin Wolf printf("aio_multiwrite failed: %s\n", strerror(-cnt)); 1362797ac58cSKevin Wolf goto out; 1363797ac58cSKevin Wolf } 1364797ac58cSKevin Wolf 1365797ac58cSKevin Wolf if (qflag) { 1366797ac58cSKevin Wolf goto out; 1367797ac58cSKevin Wolf } 1368797ac58cSKevin Wolf 1369797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1370797ac58cSKevin Wolf t2 = tsub(t2, t1); 1371797ac58cSKevin Wolf print_report("wrote", &t2, first_offset, total, total, cnt, Cflag); 1372797ac58cSKevin Wolf out: 1373797ac58cSKevin Wolf for (i = 0; i < nr_reqs; i++) { 1374797ac58cSKevin Wolf qemu_io_free(buf[i]); 1375797ac58cSKevin Wolf if (reqs[i].qiov != NULL) { 1376797ac58cSKevin Wolf qemu_iovec_destroy(&qiovs[i]); 1377797ac58cSKevin Wolf } 1378797ac58cSKevin Wolf } 1379797ac58cSKevin Wolf g_free(buf); 1380797ac58cSKevin Wolf g_free(reqs); 1381797ac58cSKevin Wolf g_free(qiovs); 1382797ac58cSKevin Wolf return 0; 1383797ac58cSKevin Wolf } 1384797ac58cSKevin Wolf 1385797ac58cSKevin Wolf struct aio_ctx { 13864c7b7e9bSMax Reitz BlockBackend *blk; 1387797ac58cSKevin Wolf QEMUIOVector qiov; 1388797ac58cSKevin Wolf int64_t offset; 1389797ac58cSKevin Wolf char *buf; 1390797ac58cSKevin Wolf int qflag; 1391797ac58cSKevin Wolf int vflag; 1392797ac58cSKevin Wolf int Cflag; 1393797ac58cSKevin Wolf int Pflag; 1394a91f9584SFam Zheng BlockAcctCookie acct; 1395797ac58cSKevin Wolf int pattern; 1396797ac58cSKevin Wolf struct timeval t1; 1397797ac58cSKevin Wolf }; 1398797ac58cSKevin Wolf 1399797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret) 1400797ac58cSKevin Wolf { 1401797ac58cSKevin Wolf struct aio_ctx *ctx = opaque; 1402797ac58cSKevin Wolf struct timeval t2; 1403797ac58cSKevin Wolf 1404797ac58cSKevin Wolf gettimeofday(&t2, NULL); 1405797ac58cSKevin Wolf 1406797ac58cSKevin Wolf 1407797ac58cSKevin Wolf if (ret < 0) { 1408797ac58cSKevin Wolf printf("aio_write failed: %s\n", strerror(-ret)); 1409797ac58cSKevin Wolf goto out; 1410797ac58cSKevin Wolf } 1411797ac58cSKevin Wolf 14124c7b7e9bSMax Reitz block_acct_done(blk_get_stats(ctx->blk), &ctx->acct); 1413a91f9584SFam Zheng 1414797ac58cSKevin Wolf if (ctx->qflag) { 1415797ac58cSKevin Wolf goto out; 1416797ac58cSKevin Wolf } 1417797ac58cSKevin Wolf 1418797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1419797ac58cSKevin Wolf t2 = tsub(t2, ctx->t1); 1420797ac58cSKevin Wolf print_report("wrote", &t2, ctx->offset, ctx->qiov.size, 1421797ac58cSKevin Wolf ctx->qiov.size, 1, ctx->Cflag); 1422797ac58cSKevin Wolf out: 1423797ac58cSKevin Wolf qemu_io_free(ctx->buf); 1424797ac58cSKevin Wolf qemu_iovec_destroy(&ctx->qiov); 1425797ac58cSKevin Wolf g_free(ctx); 1426797ac58cSKevin Wolf } 1427797ac58cSKevin Wolf 1428797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret) 1429797ac58cSKevin Wolf { 1430797ac58cSKevin Wolf struct aio_ctx *ctx = opaque; 1431797ac58cSKevin Wolf struct timeval t2; 1432797ac58cSKevin Wolf 1433797ac58cSKevin Wolf gettimeofday(&t2, NULL); 1434797ac58cSKevin Wolf 1435797ac58cSKevin Wolf if (ret < 0) { 1436797ac58cSKevin Wolf printf("readv failed: %s\n", strerror(-ret)); 1437797ac58cSKevin Wolf goto out; 1438797ac58cSKevin Wolf } 1439797ac58cSKevin Wolf 1440797ac58cSKevin Wolf if (ctx->Pflag) { 1441797ac58cSKevin Wolf void *cmp_buf = g_malloc(ctx->qiov.size); 1442797ac58cSKevin Wolf 1443797ac58cSKevin Wolf memset(cmp_buf, ctx->pattern, ctx->qiov.size); 1444797ac58cSKevin Wolf if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) { 1445797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 1446797ac58cSKevin Wolf PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size); 1447797ac58cSKevin Wolf } 1448797ac58cSKevin Wolf g_free(cmp_buf); 1449797ac58cSKevin Wolf } 1450797ac58cSKevin Wolf 14514c7b7e9bSMax Reitz block_acct_done(blk_get_stats(ctx->blk), &ctx->acct); 1452a91f9584SFam Zheng 1453797ac58cSKevin Wolf if (ctx->qflag) { 1454797ac58cSKevin Wolf goto out; 1455797ac58cSKevin Wolf } 1456797ac58cSKevin Wolf 1457797ac58cSKevin Wolf if (ctx->vflag) { 1458797ac58cSKevin Wolf dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); 1459797ac58cSKevin Wolf } 1460797ac58cSKevin Wolf 1461797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1462797ac58cSKevin Wolf t2 = tsub(t2, ctx->t1); 1463797ac58cSKevin Wolf print_report("read", &t2, ctx->offset, ctx->qiov.size, 1464797ac58cSKevin Wolf ctx->qiov.size, 1, ctx->Cflag); 1465797ac58cSKevin Wolf out: 1466797ac58cSKevin Wolf qemu_io_free(ctx->buf); 1467797ac58cSKevin Wolf qemu_iovec_destroy(&ctx->qiov); 1468797ac58cSKevin Wolf g_free(ctx); 1469797ac58cSKevin Wolf } 1470797ac58cSKevin Wolf 1471797ac58cSKevin Wolf static void aio_read_help(void) 1472797ac58cSKevin Wolf { 1473797ac58cSKevin Wolf printf( 1474797ac58cSKevin Wolf "\n" 1475797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n" 1476797ac58cSKevin Wolf "\n" 1477797ac58cSKevin Wolf " Example:\n" 1478797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" 1479797ac58cSKevin Wolf "\n" 1480797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 1481797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 1482797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n" 1483797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n" 1484797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1485797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 1486797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 1487797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1488797ac58cSKevin Wolf "\n"); 1489797ac58cSKevin Wolf } 1490797ac58cSKevin Wolf 14914c7b7e9bSMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv); 1492797ac58cSKevin Wolf 1493797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = { 1494797ac58cSKevin Wolf .name = "aio_read", 1495797ac58cSKevin Wolf .cfunc = aio_read_f, 1496797ac58cSKevin Wolf .argmin = 2, 1497797ac58cSKevin Wolf .argmax = -1, 1498797ac58cSKevin Wolf .args = "[-Cqv] [-P pattern ] off len [len..]", 1499797ac58cSKevin Wolf .oneline = "asynchronously reads a number of bytes", 1500797ac58cSKevin Wolf .help = aio_read_help, 1501797ac58cSKevin Wolf }; 1502797ac58cSKevin Wolf 15034c7b7e9bSMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv) 1504797ac58cSKevin Wolf { 1505797ac58cSKevin Wolf int nr_iov, c; 1506797ac58cSKevin Wolf struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); 1507797ac58cSKevin Wolf 15084c7b7e9bSMax Reitz ctx->blk = blk; 1509b062ad86SEric Blake while ((c = getopt(argc, argv, "CP:qv")) != -1) { 1510797ac58cSKevin Wolf switch (c) { 1511797ac58cSKevin Wolf case 'C': 1512797ac58cSKevin Wolf ctx->Cflag = 1; 1513797ac58cSKevin Wolf break; 1514797ac58cSKevin Wolf case 'P': 1515797ac58cSKevin Wolf ctx->Pflag = 1; 1516797ac58cSKevin Wolf ctx->pattern = parse_pattern(optarg); 1517797ac58cSKevin Wolf if (ctx->pattern < 0) { 1518797ac58cSKevin Wolf g_free(ctx); 1519797ac58cSKevin Wolf return 0; 1520797ac58cSKevin Wolf } 1521797ac58cSKevin Wolf break; 1522797ac58cSKevin Wolf case 'q': 1523797ac58cSKevin Wolf ctx->qflag = 1; 1524797ac58cSKevin Wolf break; 1525797ac58cSKevin Wolf case 'v': 1526797ac58cSKevin Wolf ctx->vflag = 1; 1527797ac58cSKevin Wolf break; 1528797ac58cSKevin Wolf default: 1529797ac58cSKevin Wolf g_free(ctx); 1530c2cdf5c5SKevin Wolf return qemuio_command_usage(&aio_read_cmd); 1531797ac58cSKevin Wolf } 1532797ac58cSKevin Wolf } 1533797ac58cSKevin Wolf 1534797ac58cSKevin Wolf if (optind > argc - 2) { 1535797ac58cSKevin Wolf g_free(ctx); 1536c2cdf5c5SKevin Wolf return qemuio_command_usage(&aio_read_cmd); 1537797ac58cSKevin Wolf } 1538797ac58cSKevin Wolf 1539797ac58cSKevin Wolf ctx->offset = cvtnum(argv[optind]); 1540797ac58cSKevin Wolf if (ctx->offset < 0) { 1541797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 1542797ac58cSKevin Wolf g_free(ctx); 1543797ac58cSKevin Wolf return 0; 1544797ac58cSKevin Wolf } 1545797ac58cSKevin Wolf optind++; 1546797ac58cSKevin Wolf 1547797ac58cSKevin Wolf if (ctx->offset & 0x1ff) { 1548797ac58cSKevin Wolf printf("offset %" PRId64 " is not sector aligned\n", 1549797ac58cSKevin Wolf ctx->offset); 1550797ac58cSKevin Wolf g_free(ctx); 1551797ac58cSKevin Wolf return 0; 1552797ac58cSKevin Wolf } 1553797ac58cSKevin Wolf 1554797ac58cSKevin Wolf nr_iov = argc - optind; 15554c7b7e9bSMax Reitz ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab); 1556797ac58cSKevin Wolf if (ctx->buf == NULL) { 1557797ac58cSKevin Wolf g_free(ctx); 1558797ac58cSKevin Wolf return 0; 1559797ac58cSKevin Wolf } 1560797ac58cSKevin Wolf 1561797ac58cSKevin Wolf gettimeofday(&ctx->t1, NULL); 15624c7b7e9bSMax Reitz block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size, 15634c7b7e9bSMax Reitz BLOCK_ACCT_READ); 15644c7b7e9bSMax Reitz blk_aio_readv(blk, ctx->offset >> 9, &ctx->qiov, 1565797ac58cSKevin Wolf ctx->qiov.size >> 9, aio_read_done, ctx); 1566797ac58cSKevin Wolf return 0; 1567797ac58cSKevin Wolf } 1568797ac58cSKevin Wolf 1569797ac58cSKevin Wolf static void aio_write_help(void) 1570797ac58cSKevin Wolf { 1571797ac58cSKevin Wolf printf( 1572797ac58cSKevin Wolf "\n" 1573797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n" 1574797ac58cSKevin Wolf " from multiple buffers\n" 1575797ac58cSKevin Wolf "\n" 1576797ac58cSKevin Wolf " Example:\n" 1577797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" 1578797ac58cSKevin Wolf "\n" 1579797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1580797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1581797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n" 1582797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n" 1583797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 1584797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1585797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1586797ac58cSKevin Wolf "\n"); 1587797ac58cSKevin Wolf } 1588797ac58cSKevin Wolf 15894c7b7e9bSMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv); 1590797ac58cSKevin Wolf 1591797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = { 1592797ac58cSKevin Wolf .name = "aio_write", 1593797ac58cSKevin Wolf .cfunc = aio_write_f, 1594797ac58cSKevin Wolf .argmin = 2, 1595797ac58cSKevin Wolf .argmax = -1, 1596797ac58cSKevin Wolf .args = "[-Cq] [-P pattern ] off len [len..]", 1597797ac58cSKevin Wolf .oneline = "asynchronously writes a number of bytes", 1598797ac58cSKevin Wolf .help = aio_write_help, 1599797ac58cSKevin Wolf }; 1600797ac58cSKevin Wolf 16014c7b7e9bSMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv) 1602797ac58cSKevin Wolf { 1603797ac58cSKevin Wolf int nr_iov, c; 1604797ac58cSKevin Wolf int pattern = 0xcd; 1605797ac58cSKevin Wolf struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); 1606797ac58cSKevin Wolf 16074c7b7e9bSMax Reitz ctx->blk = blk; 1608b062ad86SEric Blake while ((c = getopt(argc, argv, "CqP:")) != -1) { 1609797ac58cSKevin Wolf switch (c) { 1610797ac58cSKevin Wolf case 'C': 1611797ac58cSKevin Wolf ctx->Cflag = 1; 1612797ac58cSKevin Wolf break; 1613797ac58cSKevin Wolf case 'q': 1614797ac58cSKevin Wolf ctx->qflag = 1; 1615797ac58cSKevin Wolf break; 1616797ac58cSKevin Wolf case 'P': 1617797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1618797ac58cSKevin Wolf if (pattern < 0) { 1619797ac58cSKevin Wolf g_free(ctx); 1620797ac58cSKevin Wolf return 0; 1621797ac58cSKevin Wolf } 1622797ac58cSKevin Wolf break; 1623797ac58cSKevin Wolf default: 1624797ac58cSKevin Wolf g_free(ctx); 1625c2cdf5c5SKevin Wolf return qemuio_command_usage(&aio_write_cmd); 1626797ac58cSKevin Wolf } 1627797ac58cSKevin Wolf } 1628797ac58cSKevin Wolf 1629797ac58cSKevin Wolf if (optind > argc - 2) { 1630797ac58cSKevin Wolf g_free(ctx); 1631c2cdf5c5SKevin Wolf return qemuio_command_usage(&aio_write_cmd); 1632797ac58cSKevin Wolf } 1633797ac58cSKevin Wolf 1634797ac58cSKevin Wolf ctx->offset = cvtnum(argv[optind]); 1635797ac58cSKevin Wolf if (ctx->offset < 0) { 1636797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 1637797ac58cSKevin Wolf g_free(ctx); 1638797ac58cSKevin Wolf return 0; 1639797ac58cSKevin Wolf } 1640797ac58cSKevin Wolf optind++; 1641797ac58cSKevin Wolf 1642797ac58cSKevin Wolf if (ctx->offset & 0x1ff) { 1643797ac58cSKevin Wolf printf("offset %" PRId64 " is not sector aligned\n", 1644797ac58cSKevin Wolf ctx->offset); 1645797ac58cSKevin Wolf g_free(ctx); 1646797ac58cSKevin Wolf return 0; 1647797ac58cSKevin Wolf } 1648797ac58cSKevin Wolf 1649797ac58cSKevin Wolf nr_iov = argc - optind; 16504c7b7e9bSMax Reitz ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, pattern); 1651797ac58cSKevin Wolf if (ctx->buf == NULL) { 1652797ac58cSKevin Wolf g_free(ctx); 1653797ac58cSKevin Wolf return 0; 1654797ac58cSKevin Wolf } 1655797ac58cSKevin Wolf 1656797ac58cSKevin Wolf gettimeofday(&ctx->t1, NULL); 16574c7b7e9bSMax Reitz block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size, 16584c7b7e9bSMax Reitz BLOCK_ACCT_WRITE); 16594c7b7e9bSMax Reitz blk_aio_writev(blk, ctx->offset >> 9, &ctx->qiov, 1660797ac58cSKevin Wolf ctx->qiov.size >> 9, aio_write_done, ctx); 1661797ac58cSKevin Wolf return 0; 1662797ac58cSKevin Wolf } 1663797ac58cSKevin Wolf 16644c7b7e9bSMax Reitz static int aio_flush_f(BlockBackend *blk, int argc, char **argv) 1665797ac58cSKevin Wolf { 16664c7b7e9bSMax Reitz blk_drain_all(); 1667797ac58cSKevin Wolf return 0; 1668797ac58cSKevin Wolf } 1669797ac58cSKevin Wolf 1670797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = { 1671797ac58cSKevin Wolf .name = "aio_flush", 1672797ac58cSKevin Wolf .cfunc = aio_flush_f, 1673797ac58cSKevin Wolf .oneline = "completes all outstanding aio requests" 1674797ac58cSKevin Wolf }; 1675797ac58cSKevin Wolf 16764c7b7e9bSMax Reitz static int flush_f(BlockBackend *blk, int argc, char **argv) 1677797ac58cSKevin Wolf { 16784c7b7e9bSMax Reitz blk_flush(blk); 1679797ac58cSKevin Wolf return 0; 1680797ac58cSKevin Wolf } 1681797ac58cSKevin Wolf 1682797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = { 1683797ac58cSKevin Wolf .name = "flush", 1684797ac58cSKevin Wolf .altname = "f", 1685797ac58cSKevin Wolf .cfunc = flush_f, 1686797ac58cSKevin Wolf .oneline = "flush all in-core file state to disk", 1687797ac58cSKevin Wolf }; 1688797ac58cSKevin Wolf 16894c7b7e9bSMax Reitz static int truncate_f(BlockBackend *blk, int argc, char **argv) 1690797ac58cSKevin Wolf { 1691797ac58cSKevin Wolf int64_t offset; 1692797ac58cSKevin Wolf int ret; 1693797ac58cSKevin Wolf 1694797ac58cSKevin Wolf offset = cvtnum(argv[1]); 1695797ac58cSKevin Wolf if (offset < 0) { 1696797ac58cSKevin Wolf printf("non-numeric truncate argument -- %s\n", argv[1]); 1697797ac58cSKevin Wolf return 0; 1698797ac58cSKevin Wolf } 1699797ac58cSKevin Wolf 17004c7b7e9bSMax Reitz ret = blk_truncate(blk, offset); 1701797ac58cSKevin Wolf if (ret < 0) { 1702797ac58cSKevin Wolf printf("truncate: %s\n", strerror(-ret)); 1703797ac58cSKevin Wolf return 0; 1704797ac58cSKevin Wolf } 1705797ac58cSKevin Wolf 1706797ac58cSKevin Wolf return 0; 1707797ac58cSKevin Wolf } 1708797ac58cSKevin Wolf 1709797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = { 1710797ac58cSKevin Wolf .name = "truncate", 1711797ac58cSKevin Wolf .altname = "t", 1712797ac58cSKevin Wolf .cfunc = truncate_f, 1713797ac58cSKevin Wolf .argmin = 1, 1714797ac58cSKevin Wolf .argmax = 1, 1715797ac58cSKevin Wolf .args = "off", 1716797ac58cSKevin Wolf .oneline = "truncates the current file at the given offset", 1717797ac58cSKevin Wolf }; 1718797ac58cSKevin Wolf 17194c7b7e9bSMax Reitz static int length_f(BlockBackend *blk, int argc, char **argv) 1720797ac58cSKevin Wolf { 1721797ac58cSKevin Wolf int64_t size; 1722797ac58cSKevin Wolf char s1[64]; 1723797ac58cSKevin Wolf 17244c7b7e9bSMax Reitz size = blk_getlength(blk); 1725797ac58cSKevin Wolf if (size < 0) { 1726797ac58cSKevin Wolf printf("getlength: %s\n", strerror(-size)); 1727797ac58cSKevin Wolf return 0; 1728797ac58cSKevin Wolf } 1729797ac58cSKevin Wolf 1730797ac58cSKevin Wolf cvtstr(size, s1, sizeof(s1)); 1731797ac58cSKevin Wolf printf("%s\n", s1); 1732797ac58cSKevin Wolf return 0; 1733797ac58cSKevin Wolf } 1734797ac58cSKevin Wolf 1735797ac58cSKevin Wolf 1736797ac58cSKevin Wolf static const cmdinfo_t length_cmd = { 1737797ac58cSKevin Wolf .name = "length", 1738797ac58cSKevin Wolf .altname = "l", 1739797ac58cSKevin Wolf .cfunc = length_f, 1740797ac58cSKevin Wolf .oneline = "gets the length of the current file", 1741797ac58cSKevin Wolf }; 1742797ac58cSKevin Wolf 1743797ac58cSKevin Wolf 17444c7b7e9bSMax Reitz static int info_f(BlockBackend *blk, int argc, char **argv) 1745797ac58cSKevin Wolf { 17464c7b7e9bSMax Reitz BlockDriverState *bs = blk_bs(blk); 1747797ac58cSKevin Wolf BlockDriverInfo bdi; 1748a8d8ecb7SMax Reitz ImageInfoSpecific *spec_info; 1749797ac58cSKevin Wolf char s1[64], s2[64]; 1750797ac58cSKevin Wolf int ret; 1751797ac58cSKevin Wolf 1752797ac58cSKevin Wolf if (bs->drv && bs->drv->format_name) { 1753797ac58cSKevin Wolf printf("format name: %s\n", bs->drv->format_name); 1754797ac58cSKevin Wolf } 1755797ac58cSKevin Wolf if (bs->drv && bs->drv->protocol_name) { 1756797ac58cSKevin Wolf printf("format name: %s\n", bs->drv->protocol_name); 1757797ac58cSKevin Wolf } 1758797ac58cSKevin Wolf 1759797ac58cSKevin Wolf ret = bdrv_get_info(bs, &bdi); 1760797ac58cSKevin Wolf if (ret) { 1761797ac58cSKevin Wolf return 0; 1762797ac58cSKevin Wolf } 1763797ac58cSKevin Wolf 1764797ac58cSKevin Wolf cvtstr(bdi.cluster_size, s1, sizeof(s1)); 1765797ac58cSKevin Wolf cvtstr(bdi.vm_state_offset, s2, sizeof(s2)); 1766797ac58cSKevin Wolf 1767797ac58cSKevin Wolf printf("cluster size: %s\n", s1); 1768797ac58cSKevin Wolf printf("vm state offset: %s\n", s2); 1769797ac58cSKevin Wolf 1770a8d8ecb7SMax Reitz spec_info = bdrv_get_specific_info(bs); 1771a8d8ecb7SMax Reitz if (spec_info) { 1772a8d8ecb7SMax Reitz printf("Format specific information:\n"); 1773a8d8ecb7SMax Reitz bdrv_image_info_specific_dump(fprintf, stdout, spec_info); 1774a8d8ecb7SMax Reitz qapi_free_ImageInfoSpecific(spec_info); 1775a8d8ecb7SMax Reitz } 1776a8d8ecb7SMax Reitz 1777797ac58cSKevin Wolf return 0; 1778797ac58cSKevin Wolf } 1779797ac58cSKevin Wolf 1780797ac58cSKevin Wolf 1781797ac58cSKevin Wolf 1782797ac58cSKevin Wolf static const cmdinfo_t info_cmd = { 1783797ac58cSKevin Wolf .name = "info", 1784797ac58cSKevin Wolf .altname = "i", 1785797ac58cSKevin Wolf .cfunc = info_f, 1786797ac58cSKevin Wolf .oneline = "prints information about the current file", 1787797ac58cSKevin Wolf }; 1788797ac58cSKevin Wolf 1789797ac58cSKevin Wolf static void discard_help(void) 1790797ac58cSKevin Wolf { 1791797ac58cSKevin Wolf printf( 1792797ac58cSKevin Wolf "\n" 1793797ac58cSKevin Wolf " discards a range of bytes from the given offset\n" 1794797ac58cSKevin Wolf "\n" 1795797ac58cSKevin Wolf " Example:\n" 1796797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n" 1797797ac58cSKevin Wolf "\n" 1798797ac58cSKevin Wolf " Discards a segment of the currently open file.\n" 1799797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1800797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1801797ac58cSKevin Wolf "\n"); 1802797ac58cSKevin Wolf } 1803797ac58cSKevin Wolf 18044c7b7e9bSMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv); 1805797ac58cSKevin Wolf 1806797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = { 1807797ac58cSKevin Wolf .name = "discard", 1808797ac58cSKevin Wolf .altname = "d", 1809797ac58cSKevin Wolf .cfunc = discard_f, 1810797ac58cSKevin Wolf .argmin = 2, 1811797ac58cSKevin Wolf .argmax = -1, 1812797ac58cSKevin Wolf .args = "[-Cq] off len", 1813797ac58cSKevin Wolf .oneline = "discards a number of bytes at a specified offset", 1814797ac58cSKevin Wolf .help = discard_help, 1815797ac58cSKevin Wolf }; 1816797ac58cSKevin Wolf 18174c7b7e9bSMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv) 1818797ac58cSKevin Wolf { 1819797ac58cSKevin Wolf struct timeval t1, t2; 1820797ac58cSKevin Wolf int Cflag = 0, qflag = 0; 1821797ac58cSKevin Wolf int c, ret; 1822*9b0beaf3SJohn Snow int64_t offset, count; 1823797ac58cSKevin Wolf 1824b062ad86SEric Blake while ((c = getopt(argc, argv, "Cq")) != -1) { 1825797ac58cSKevin Wolf switch (c) { 1826797ac58cSKevin Wolf case 'C': 1827797ac58cSKevin Wolf Cflag = 1; 1828797ac58cSKevin Wolf break; 1829797ac58cSKevin Wolf case 'q': 1830797ac58cSKevin Wolf qflag = 1; 1831797ac58cSKevin Wolf break; 1832797ac58cSKevin Wolf default: 1833c2cdf5c5SKevin Wolf return qemuio_command_usage(&discard_cmd); 1834797ac58cSKevin Wolf } 1835797ac58cSKevin Wolf } 1836797ac58cSKevin Wolf 1837797ac58cSKevin Wolf if (optind != argc - 2) { 1838c2cdf5c5SKevin Wolf return qemuio_command_usage(&discard_cmd); 1839797ac58cSKevin Wolf } 1840797ac58cSKevin Wolf 1841797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1842797ac58cSKevin Wolf if (offset < 0) { 1843797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 1844797ac58cSKevin Wolf return 0; 1845797ac58cSKevin Wolf } 1846797ac58cSKevin Wolf 1847797ac58cSKevin Wolf optind++; 1848797ac58cSKevin Wolf count = cvtnum(argv[optind]); 1849797ac58cSKevin Wolf if (count < 0) { 1850797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[optind]); 1851797ac58cSKevin Wolf return 0; 1852*9b0beaf3SJohn Snow } else if (count >> BDRV_SECTOR_BITS > INT_MAX) { 1853*9b0beaf3SJohn Snow printf("length cannot exceed %"PRIu64", given %s\n", 1854*9b0beaf3SJohn Snow (uint64_t)INT_MAX << BDRV_SECTOR_BITS, 1855*9b0beaf3SJohn Snow argv[optind]); 1856*9b0beaf3SJohn Snow return 0; 1857797ac58cSKevin Wolf } 1858797ac58cSKevin Wolf 1859797ac58cSKevin Wolf gettimeofday(&t1, NULL); 18604c7b7e9bSMax Reitz ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS, 1861797ac58cSKevin Wolf count >> BDRV_SECTOR_BITS); 1862797ac58cSKevin Wolf gettimeofday(&t2, NULL); 1863797ac58cSKevin Wolf 1864797ac58cSKevin Wolf if (ret < 0) { 1865797ac58cSKevin Wolf printf("discard failed: %s\n", strerror(-ret)); 1866797ac58cSKevin Wolf goto out; 1867797ac58cSKevin Wolf } 1868797ac58cSKevin Wolf 1869797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1870797ac58cSKevin Wolf if (!qflag) { 1871797ac58cSKevin Wolf t2 = tsub(t2, t1); 1872797ac58cSKevin Wolf print_report("discard", &t2, offset, count, count, 1, Cflag); 1873797ac58cSKevin Wolf } 1874797ac58cSKevin Wolf 1875797ac58cSKevin Wolf out: 1876797ac58cSKevin Wolf return 0; 1877797ac58cSKevin Wolf } 1878797ac58cSKevin Wolf 18794c7b7e9bSMax Reitz static int alloc_f(BlockBackend *blk, int argc, char **argv) 1880797ac58cSKevin Wolf { 18814c7b7e9bSMax Reitz BlockDriverState *bs = blk_bs(blk); 1882*9b0beaf3SJohn Snow int64_t offset, sector_num, nb_sectors, remaining; 1883797ac58cSKevin Wolf char s1[64]; 1884*9b0beaf3SJohn Snow int num, ret; 1885*9b0beaf3SJohn Snow int64_t sum_alloc; 1886797ac58cSKevin Wolf 1887797ac58cSKevin Wolf offset = cvtnum(argv[1]); 1888797ac58cSKevin Wolf if (offset < 0) { 1889797ac58cSKevin Wolf printf("non-numeric offset argument -- %s\n", argv[1]); 1890797ac58cSKevin Wolf return 0; 1891797ac58cSKevin Wolf } else if (offset & 0x1ff) { 1892797ac58cSKevin Wolf printf("offset %" PRId64 " is not sector aligned\n", 1893797ac58cSKevin Wolf offset); 1894797ac58cSKevin Wolf return 0; 1895797ac58cSKevin Wolf } 1896797ac58cSKevin Wolf 1897797ac58cSKevin Wolf if (argc == 3) { 1898797ac58cSKevin Wolf nb_sectors = cvtnum(argv[2]); 1899797ac58cSKevin Wolf if (nb_sectors < 0) { 1900797ac58cSKevin Wolf printf("non-numeric length argument -- %s\n", argv[2]); 1901797ac58cSKevin Wolf return 0; 1902*9b0beaf3SJohn Snow } else if (nb_sectors > INT_MAX) { 1903*9b0beaf3SJohn Snow printf("length argument cannot exceed %d, given %s\n", 1904*9b0beaf3SJohn Snow INT_MAX, argv[2]); 1905*9b0beaf3SJohn Snow return 0; 1906797ac58cSKevin Wolf } 1907797ac58cSKevin Wolf } else { 1908797ac58cSKevin Wolf nb_sectors = 1; 1909797ac58cSKevin Wolf } 1910797ac58cSKevin Wolf 1911797ac58cSKevin Wolf remaining = nb_sectors; 1912797ac58cSKevin Wolf sum_alloc = 0; 1913797ac58cSKevin Wolf sector_num = offset >> 9; 1914797ac58cSKevin Wolf while (remaining) { 1915797ac58cSKevin Wolf ret = bdrv_is_allocated(bs, sector_num, remaining, &num); 1916d663640cSPaolo Bonzini if (ret < 0) { 1917d663640cSPaolo Bonzini printf("is_allocated failed: %s\n", strerror(-ret)); 1918d663640cSPaolo Bonzini return 0; 1919d663640cSPaolo Bonzini } 1920797ac58cSKevin Wolf sector_num += num; 1921797ac58cSKevin Wolf remaining -= num; 1922797ac58cSKevin Wolf if (ret) { 1923797ac58cSKevin Wolf sum_alloc += num; 1924797ac58cSKevin Wolf } 1925797ac58cSKevin Wolf if (num == 0) { 1926797ac58cSKevin Wolf nb_sectors -= remaining; 1927797ac58cSKevin Wolf remaining = 0; 1928797ac58cSKevin Wolf } 1929797ac58cSKevin Wolf } 1930797ac58cSKevin Wolf 1931797ac58cSKevin Wolf cvtstr(offset, s1, sizeof(s1)); 1932797ac58cSKevin Wolf 1933*9b0beaf3SJohn Snow printf("%"PRId64"/%"PRId64" sectors allocated at offset %s\n", 1934797ac58cSKevin Wolf sum_alloc, nb_sectors, s1); 1935797ac58cSKevin Wolf return 0; 1936797ac58cSKevin Wolf } 1937797ac58cSKevin Wolf 1938797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = { 1939797ac58cSKevin Wolf .name = "alloc", 1940797ac58cSKevin Wolf .altname = "a", 1941797ac58cSKevin Wolf .argmin = 1, 1942797ac58cSKevin Wolf .argmax = 2, 1943797ac58cSKevin Wolf .cfunc = alloc_f, 1944797ac58cSKevin Wolf .args = "off [sectors]", 1945797ac58cSKevin Wolf .oneline = "checks if a sector is present in the file", 1946797ac58cSKevin Wolf }; 1947797ac58cSKevin Wolf 1948797ac58cSKevin Wolf 1949797ac58cSKevin Wolf static int map_is_allocated(BlockDriverState *bs, int64_t sector_num, 1950797ac58cSKevin Wolf int64_t nb_sectors, int64_t *pnum) 1951797ac58cSKevin Wolf { 1952797ac58cSKevin Wolf int num, num_checked; 1953797ac58cSKevin Wolf int ret, firstret; 1954797ac58cSKevin Wolf 1955797ac58cSKevin Wolf num_checked = MIN(nb_sectors, INT_MAX); 1956797ac58cSKevin Wolf ret = bdrv_is_allocated(bs, sector_num, num_checked, &num); 1957797ac58cSKevin Wolf if (ret < 0) { 1958797ac58cSKevin Wolf return ret; 1959797ac58cSKevin Wolf } 1960797ac58cSKevin Wolf 1961797ac58cSKevin Wolf firstret = ret; 1962797ac58cSKevin Wolf *pnum = num; 1963797ac58cSKevin Wolf 1964797ac58cSKevin Wolf while (nb_sectors > 0 && ret == firstret) { 1965797ac58cSKevin Wolf sector_num += num; 1966797ac58cSKevin Wolf nb_sectors -= num; 1967797ac58cSKevin Wolf 1968797ac58cSKevin Wolf num_checked = MIN(nb_sectors, INT_MAX); 1969797ac58cSKevin Wolf ret = bdrv_is_allocated(bs, sector_num, num_checked, &num); 19704b25bbc4SMax Reitz if (ret == firstret && num) { 1971797ac58cSKevin Wolf *pnum += num; 1972797ac58cSKevin Wolf } else { 1973797ac58cSKevin Wolf break; 1974797ac58cSKevin Wolf } 1975797ac58cSKevin Wolf } 1976797ac58cSKevin Wolf 1977797ac58cSKevin Wolf return firstret; 1978797ac58cSKevin Wolf } 1979797ac58cSKevin Wolf 19804c7b7e9bSMax Reitz static int map_f(BlockBackend *blk, int argc, char **argv) 1981797ac58cSKevin Wolf { 1982797ac58cSKevin Wolf int64_t offset; 19834c7b7e9bSMax Reitz int64_t nb_sectors, total_sectors; 1984797ac58cSKevin Wolf char s1[64]; 1985797ac58cSKevin Wolf int64_t num; 1986797ac58cSKevin Wolf int ret; 1987797ac58cSKevin Wolf const char *retstr; 1988797ac58cSKevin Wolf 1989797ac58cSKevin Wolf offset = 0; 19904c7b7e9bSMax Reitz total_sectors = blk_nb_sectors(blk); 19914c7b7e9bSMax Reitz if (total_sectors < 0) { 19924c7b7e9bSMax Reitz error_report("Failed to query image length: %s", 19934c7b7e9bSMax Reitz strerror(-total_sectors)); 19944c7b7e9bSMax Reitz return 0; 19954c7b7e9bSMax Reitz } 19964c7b7e9bSMax Reitz 19974c7b7e9bSMax Reitz nb_sectors = total_sectors; 1998797ac58cSKevin Wolf 1999797ac58cSKevin Wolf do { 20004c7b7e9bSMax Reitz ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num); 2001797ac58cSKevin Wolf if (ret < 0) { 2002797ac58cSKevin Wolf error_report("Failed to get allocation status: %s", strerror(-ret)); 2003797ac58cSKevin Wolf return 0; 20044b25bbc4SMax Reitz } else if (!num) { 20054b25bbc4SMax Reitz error_report("Unexpected end of image"); 20064b25bbc4SMax Reitz return 0; 2007797ac58cSKevin Wolf } 2008797ac58cSKevin Wolf 2009797ac58cSKevin Wolf retstr = ret ? " allocated" : "not allocated"; 2010797ac58cSKevin Wolf cvtstr(offset << 9ULL, s1, sizeof(s1)); 2011797ac58cSKevin Wolf printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s " 2012797ac58cSKevin Wolf "at offset %s (%d)\n", 2013797ac58cSKevin Wolf offset << 9ULL, num, nb_sectors, retstr, s1, ret); 2014797ac58cSKevin Wolf 2015797ac58cSKevin Wolf offset += num; 2016797ac58cSKevin Wolf nb_sectors -= num; 20174c7b7e9bSMax Reitz } while (offset < total_sectors); 2018797ac58cSKevin Wolf 2019797ac58cSKevin Wolf return 0; 2020797ac58cSKevin Wolf } 2021797ac58cSKevin Wolf 2022797ac58cSKevin Wolf static const cmdinfo_t map_cmd = { 2023797ac58cSKevin Wolf .name = "map", 2024797ac58cSKevin Wolf .argmin = 0, 2025797ac58cSKevin Wolf .argmax = 0, 2026797ac58cSKevin Wolf .cfunc = map_f, 2027797ac58cSKevin Wolf .args = "", 2028797ac58cSKevin Wolf .oneline = "prints the allocated areas of a file", 2029797ac58cSKevin Wolf }; 2030797ac58cSKevin Wolf 20315bbd2e59SKevin Wolf static void reopen_help(void) 20325bbd2e59SKevin Wolf { 20335bbd2e59SKevin Wolf printf( 20345bbd2e59SKevin Wolf "\n" 20355bbd2e59SKevin Wolf " Changes the open options of an already opened image\n" 20365bbd2e59SKevin Wolf "\n" 20375bbd2e59SKevin Wolf " Example:\n" 20385bbd2e59SKevin Wolf " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n" 20395bbd2e59SKevin Wolf "\n" 20405bbd2e59SKevin Wolf " -r, -- Reopen the image read-only\n" 20415bbd2e59SKevin Wolf " -c, -- Change the cache mode to the given value\n" 20425bbd2e59SKevin Wolf " -o, -- Changes block driver options (cf. 'open' command)\n" 20435bbd2e59SKevin Wolf "\n"); 20445bbd2e59SKevin Wolf } 20455bbd2e59SKevin Wolf 20465bbd2e59SKevin Wolf static int reopen_f(BlockBackend *blk, int argc, char **argv); 20475bbd2e59SKevin Wolf 20485bbd2e59SKevin Wolf static QemuOptsList reopen_opts = { 20495bbd2e59SKevin Wolf .name = "reopen", 20505bbd2e59SKevin Wolf .merge_lists = true, 20515bbd2e59SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head), 20525bbd2e59SKevin Wolf .desc = { 20535bbd2e59SKevin Wolf /* no elements => accept any params */ 20545bbd2e59SKevin Wolf { /* end of list */ } 20555bbd2e59SKevin Wolf }, 20565bbd2e59SKevin Wolf }; 20575bbd2e59SKevin Wolf 20585bbd2e59SKevin Wolf static const cmdinfo_t reopen_cmd = { 20595bbd2e59SKevin Wolf .name = "reopen", 20605bbd2e59SKevin Wolf .argmin = 0, 20615bbd2e59SKevin Wolf .argmax = -1, 20625bbd2e59SKevin Wolf .cfunc = reopen_f, 20635bbd2e59SKevin Wolf .args = "[-r] [-c cache] [-o options]", 20645bbd2e59SKevin Wolf .oneline = "reopens an image with new options", 20655bbd2e59SKevin Wolf .help = reopen_help, 20665bbd2e59SKevin Wolf }; 20675bbd2e59SKevin Wolf 20685bbd2e59SKevin Wolf static int reopen_f(BlockBackend *blk, int argc, char **argv) 20695bbd2e59SKevin Wolf { 20705bbd2e59SKevin Wolf BlockDriverState *bs = blk_bs(blk); 20715bbd2e59SKevin Wolf QemuOpts *qopts; 20725bbd2e59SKevin Wolf QDict *opts; 20735bbd2e59SKevin Wolf int c; 20745bbd2e59SKevin Wolf int flags = bs->open_flags; 20755bbd2e59SKevin Wolf 20765bbd2e59SKevin Wolf BlockReopenQueue *brq; 20775bbd2e59SKevin Wolf Error *local_err = NULL; 20785bbd2e59SKevin Wolf 20795bbd2e59SKevin Wolf while ((c = getopt(argc, argv, "c:o:r")) != -1) { 20805bbd2e59SKevin Wolf switch (c) { 20815bbd2e59SKevin Wolf case 'c': 20825bbd2e59SKevin Wolf if (bdrv_parse_cache_flags(optarg, &flags) < 0) { 20835bbd2e59SKevin Wolf error_report("Invalid cache option: %s", optarg); 20845bbd2e59SKevin Wolf return 0; 20855bbd2e59SKevin Wolf } 20865bbd2e59SKevin Wolf break; 20875bbd2e59SKevin Wolf case 'o': 20885bbd2e59SKevin Wolf if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) { 20895bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 20905bbd2e59SKevin Wolf return 0; 20915bbd2e59SKevin Wolf } 20925bbd2e59SKevin Wolf break; 20935bbd2e59SKevin Wolf case 'r': 20945bbd2e59SKevin Wolf flags &= ~BDRV_O_RDWR; 20955bbd2e59SKevin Wolf break; 20965bbd2e59SKevin Wolf default: 20975bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 20985bbd2e59SKevin Wolf return qemuio_command_usage(&reopen_cmd); 20995bbd2e59SKevin Wolf } 21005bbd2e59SKevin Wolf } 21015bbd2e59SKevin Wolf 21025bbd2e59SKevin Wolf if (optind != argc) { 21035bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 21045bbd2e59SKevin Wolf return qemuio_command_usage(&reopen_cmd); 21055bbd2e59SKevin Wolf } 21065bbd2e59SKevin Wolf 21075bbd2e59SKevin Wolf qopts = qemu_opts_find(&reopen_opts, NULL); 21085bbd2e59SKevin Wolf opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; 21095bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 21105bbd2e59SKevin Wolf 21115bbd2e59SKevin Wolf brq = bdrv_reopen_queue(NULL, bs, opts, flags); 21125bbd2e59SKevin Wolf bdrv_reopen_multiple(brq, &local_err); 21135bbd2e59SKevin Wolf if (local_err) { 21145bbd2e59SKevin Wolf error_report_err(local_err); 21155bbd2e59SKevin Wolf } 21165bbd2e59SKevin Wolf 21175bbd2e59SKevin Wolf return 0; 21185bbd2e59SKevin Wolf } 21195bbd2e59SKevin Wolf 21204c7b7e9bSMax Reitz static int break_f(BlockBackend *blk, int argc, char **argv) 2121797ac58cSKevin Wolf { 2122797ac58cSKevin Wolf int ret; 2123797ac58cSKevin Wolf 21244c7b7e9bSMax Reitz ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]); 2125797ac58cSKevin Wolf if (ret < 0) { 2126797ac58cSKevin Wolf printf("Could not set breakpoint: %s\n", strerror(-ret)); 2127797ac58cSKevin Wolf } 2128797ac58cSKevin Wolf 2129797ac58cSKevin Wolf return 0; 2130797ac58cSKevin Wolf } 2131797ac58cSKevin Wolf 21324c7b7e9bSMax Reitz static int remove_break_f(BlockBackend *blk, int argc, char **argv) 21334cc70e93SFam Zheng { 21344cc70e93SFam Zheng int ret; 21354cc70e93SFam Zheng 21364c7b7e9bSMax Reitz ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]); 21374cc70e93SFam Zheng if (ret < 0) { 21384cc70e93SFam Zheng printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret)); 21394cc70e93SFam Zheng } 21404cc70e93SFam Zheng 21414cc70e93SFam Zheng return 0; 21424cc70e93SFam Zheng } 21434cc70e93SFam Zheng 2144797ac58cSKevin Wolf static const cmdinfo_t break_cmd = { 2145797ac58cSKevin Wolf .name = "break", 2146797ac58cSKevin Wolf .argmin = 2, 2147797ac58cSKevin Wolf .argmax = 2, 2148797ac58cSKevin Wolf .cfunc = break_f, 2149797ac58cSKevin Wolf .args = "event tag", 2150797ac58cSKevin Wolf .oneline = "sets a breakpoint on event and tags the stopped " 2151797ac58cSKevin Wolf "request as tag", 2152797ac58cSKevin Wolf }; 2153797ac58cSKevin Wolf 21544cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = { 21554cc70e93SFam Zheng .name = "remove_break", 21564cc70e93SFam Zheng .argmin = 1, 21574cc70e93SFam Zheng .argmax = 1, 21584cc70e93SFam Zheng .cfunc = remove_break_f, 21594cc70e93SFam Zheng .args = "tag", 21604cc70e93SFam Zheng .oneline = "remove a breakpoint by tag", 21614cc70e93SFam Zheng }; 21624cc70e93SFam Zheng 21634c7b7e9bSMax Reitz static int resume_f(BlockBackend *blk, int argc, char **argv) 2164797ac58cSKevin Wolf { 2165797ac58cSKevin Wolf int ret; 2166797ac58cSKevin Wolf 21674c7b7e9bSMax Reitz ret = bdrv_debug_resume(blk_bs(blk), argv[1]); 2168797ac58cSKevin Wolf if (ret < 0) { 2169797ac58cSKevin Wolf printf("Could not resume request: %s\n", strerror(-ret)); 2170797ac58cSKevin Wolf } 2171797ac58cSKevin Wolf 2172797ac58cSKevin Wolf return 0; 2173797ac58cSKevin Wolf } 2174797ac58cSKevin Wolf 2175797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = { 2176797ac58cSKevin Wolf .name = "resume", 2177797ac58cSKevin Wolf .argmin = 1, 2178797ac58cSKevin Wolf .argmax = 1, 2179797ac58cSKevin Wolf .cfunc = resume_f, 2180797ac58cSKevin Wolf .args = "tag", 2181797ac58cSKevin Wolf .oneline = "resumes the request tagged as tag", 2182797ac58cSKevin Wolf }; 2183797ac58cSKevin Wolf 21844c7b7e9bSMax Reitz static int wait_break_f(BlockBackend *blk, int argc, char **argv) 2185797ac58cSKevin Wolf { 21864c7b7e9bSMax Reitz while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) { 21874c7b7e9bSMax Reitz aio_poll(blk_get_aio_context(blk), true); 2188797ac58cSKevin Wolf } 2189797ac58cSKevin Wolf 2190797ac58cSKevin Wolf return 0; 2191797ac58cSKevin Wolf } 2192797ac58cSKevin Wolf 2193797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = { 2194797ac58cSKevin Wolf .name = "wait_break", 2195797ac58cSKevin Wolf .argmin = 1, 2196797ac58cSKevin Wolf .argmax = 1, 2197797ac58cSKevin Wolf .cfunc = wait_break_f, 2198797ac58cSKevin Wolf .args = "tag", 2199797ac58cSKevin Wolf .oneline = "waits for the suspension of a request", 2200797ac58cSKevin Wolf }; 2201797ac58cSKevin Wolf 22024c7b7e9bSMax Reitz static int abort_f(BlockBackend *blk, int argc, char **argv) 2203797ac58cSKevin Wolf { 2204797ac58cSKevin Wolf abort(); 2205797ac58cSKevin Wolf } 2206797ac58cSKevin Wolf 2207797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = { 2208797ac58cSKevin Wolf .name = "abort", 2209797ac58cSKevin Wolf .cfunc = abort_f, 2210797ac58cSKevin Wolf .flags = CMD_NOFILE_OK, 2211797ac58cSKevin Wolf .oneline = "simulate a program crash using abort(3)", 2212797ac58cSKevin Wolf }; 2213797ac58cSKevin Wolf 22140e82dc7bSMax Reitz static void sigraise_help(void) 22150e82dc7bSMax Reitz { 22160e82dc7bSMax Reitz printf( 22170e82dc7bSMax Reitz "\n" 22180e82dc7bSMax Reitz " raises the given signal\n" 22190e82dc7bSMax Reitz "\n" 22200e82dc7bSMax Reitz " Example:\n" 22210e82dc7bSMax Reitz " 'sigraise %i' - raises SIGTERM\n" 22220e82dc7bSMax Reitz "\n" 22230e82dc7bSMax Reitz " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n" 22240e82dc7bSMax Reitz " given to sigraise.\n" 22250e82dc7bSMax Reitz "\n", SIGTERM); 22260e82dc7bSMax Reitz } 22270e82dc7bSMax Reitz 22284c7b7e9bSMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv); 22290e82dc7bSMax Reitz 22300e82dc7bSMax Reitz static const cmdinfo_t sigraise_cmd = { 22310e82dc7bSMax Reitz .name = "sigraise", 22320e82dc7bSMax Reitz .cfunc = sigraise_f, 22330e82dc7bSMax Reitz .argmin = 1, 22340e82dc7bSMax Reitz .argmax = 1, 22350e82dc7bSMax Reitz .flags = CMD_NOFILE_OK, 22360e82dc7bSMax Reitz .args = "signal", 22370e82dc7bSMax Reitz .oneline = "raises a signal", 22380e82dc7bSMax Reitz .help = sigraise_help, 22390e82dc7bSMax Reitz }; 22400e82dc7bSMax Reitz 22414c7b7e9bSMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv) 22420e82dc7bSMax Reitz { 2243*9b0beaf3SJohn Snow int64_t sig = cvtnum(argv[1]); 22440e82dc7bSMax Reitz if (sig < 0) { 22450e82dc7bSMax Reitz printf("non-numeric signal number argument -- %s\n", argv[1]); 22460e82dc7bSMax Reitz return 0; 2247*9b0beaf3SJohn Snow } else if (sig > NSIG) { 2248*9b0beaf3SJohn Snow printf("signal argument '%s' is too large to be a valid signal\n", 2249*9b0beaf3SJohn Snow argv[1]); 2250*9b0beaf3SJohn Snow return 0; 22510e82dc7bSMax Reitz } 22520e82dc7bSMax Reitz 22530e82dc7bSMax Reitz /* Using raise() to kill this process does not necessarily flush all open 22540e82dc7bSMax Reitz * streams. At least stdout and stderr (although the latter should be 22550e82dc7bSMax Reitz * non-buffered anyway) should be flushed, though. */ 22560e82dc7bSMax Reitz fflush(stdout); 22570e82dc7bSMax Reitz fflush(stderr); 22580e82dc7bSMax Reitz 22590e82dc7bSMax Reitz raise(sig); 22600e82dc7bSMax Reitz return 0; 22610e82dc7bSMax Reitz } 22620e82dc7bSMax Reitz 2263cd33d02aSKevin Wolf static void sleep_cb(void *opaque) 2264cd33d02aSKevin Wolf { 2265cd33d02aSKevin Wolf bool *expired = opaque; 2266cd33d02aSKevin Wolf *expired = true; 2267cd33d02aSKevin Wolf } 2268cd33d02aSKevin Wolf 22694c7b7e9bSMax Reitz static int sleep_f(BlockBackend *blk, int argc, char **argv) 2270cd33d02aSKevin Wolf { 2271cd33d02aSKevin Wolf char *endptr; 2272cd33d02aSKevin Wolf long ms; 2273cd33d02aSKevin Wolf struct QEMUTimer *timer; 2274cd33d02aSKevin Wolf bool expired = false; 2275cd33d02aSKevin Wolf 2276cd33d02aSKevin Wolf ms = strtol(argv[1], &endptr, 0); 2277cd33d02aSKevin Wolf if (ms < 0 || *endptr != '\0') { 2278cd33d02aSKevin Wolf printf("%s is not a valid number\n", argv[1]); 2279cd33d02aSKevin Wolf return 0; 2280cd33d02aSKevin Wolf } 2281cd33d02aSKevin Wolf 2282cd33d02aSKevin Wolf timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired); 2283cd33d02aSKevin Wolf timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms); 2284cd33d02aSKevin Wolf 2285cd33d02aSKevin Wolf while (!expired) { 2286cd33d02aSKevin Wolf main_loop_wait(false); 2287cd33d02aSKevin Wolf } 2288cd33d02aSKevin Wolf 2289cd33d02aSKevin Wolf timer_free(timer); 2290cd33d02aSKevin Wolf 2291cd33d02aSKevin Wolf return 0; 2292cd33d02aSKevin Wolf } 2293cd33d02aSKevin Wolf 2294cd33d02aSKevin Wolf static const cmdinfo_t sleep_cmd = { 2295cd33d02aSKevin Wolf .name = "sleep", 2296cd33d02aSKevin Wolf .argmin = 1, 2297cd33d02aSKevin Wolf .argmax = 1, 2298cd33d02aSKevin Wolf .cfunc = sleep_f, 2299cd33d02aSKevin Wolf .flags = CMD_NOFILE_OK, 2300cd33d02aSKevin Wolf .oneline = "waits for the given value in milliseconds", 2301cd33d02aSKevin Wolf }; 2302cd33d02aSKevin Wolf 2303f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct) 2304f18a834aSKevin Wolf { 2305f18a834aSKevin Wolf if (cmd) { 2306f18a834aSKevin Wolf printf("%s ", cmd); 2307f18a834aSKevin Wolf } else { 2308f18a834aSKevin Wolf printf("%s ", ct->name); 2309f18a834aSKevin Wolf if (ct->altname) { 2310f18a834aSKevin Wolf printf("(or %s) ", ct->altname); 2311f18a834aSKevin Wolf } 2312f18a834aSKevin Wolf } 2313f18a834aSKevin Wolf 2314f18a834aSKevin Wolf if (ct->args) { 2315f18a834aSKevin Wolf printf("%s ", ct->args); 2316f18a834aSKevin Wolf } 2317f18a834aSKevin Wolf printf("-- %s\n", ct->oneline); 2318f18a834aSKevin Wolf } 2319f18a834aSKevin Wolf 2320f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct) 2321f18a834aSKevin Wolf { 2322f18a834aSKevin Wolf help_oneline(cmd, ct); 2323f18a834aSKevin Wolf if (ct->help) { 2324f18a834aSKevin Wolf ct->help(); 2325f18a834aSKevin Wolf } 2326f18a834aSKevin Wolf } 2327f18a834aSKevin Wolf 2328f18a834aSKevin Wolf static void help_all(void) 2329f18a834aSKevin Wolf { 2330f18a834aSKevin Wolf const cmdinfo_t *ct; 2331f18a834aSKevin Wolf 2332f18a834aSKevin Wolf for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 2333f18a834aSKevin Wolf help_oneline(ct->name, ct); 2334f18a834aSKevin Wolf } 2335f18a834aSKevin Wolf printf("\nUse 'help commandname' for extended help.\n"); 2336f18a834aSKevin Wolf } 2337f18a834aSKevin Wolf 23384c7b7e9bSMax Reitz static int help_f(BlockBackend *blk, int argc, char **argv) 2339f18a834aSKevin Wolf { 2340f18a834aSKevin Wolf const cmdinfo_t *ct; 2341f18a834aSKevin Wolf 2342f18a834aSKevin Wolf if (argc == 1) { 2343f18a834aSKevin Wolf help_all(); 2344f18a834aSKevin Wolf return 0; 2345f18a834aSKevin Wolf } 2346f18a834aSKevin Wolf 2347f18a834aSKevin Wolf ct = find_command(argv[1]); 2348f18a834aSKevin Wolf if (ct == NULL) { 2349f18a834aSKevin Wolf printf("command %s not found\n", argv[1]); 2350f18a834aSKevin Wolf return 0; 2351f18a834aSKevin Wolf } 2352f18a834aSKevin Wolf 2353f18a834aSKevin Wolf help_onecmd(argv[1], ct); 2354f18a834aSKevin Wolf return 0; 2355f18a834aSKevin Wolf } 2356f18a834aSKevin Wolf 2357f18a834aSKevin Wolf static const cmdinfo_t help_cmd = { 2358f18a834aSKevin Wolf .name = "help", 2359f18a834aSKevin Wolf .altname = "?", 2360f18a834aSKevin Wolf .cfunc = help_f, 2361f18a834aSKevin Wolf .argmin = 0, 2362f18a834aSKevin Wolf .argmax = 1, 2363f18a834aSKevin Wolf .flags = CMD_FLAG_GLOBAL, 2364f18a834aSKevin Wolf .args = "[command]", 2365f18a834aSKevin Wolf .oneline = "help for one or all commands", 2366f18a834aSKevin Wolf }; 2367f18a834aSKevin Wolf 23684c7b7e9bSMax Reitz bool qemuio_command(BlockBackend *blk, const char *cmd) 2369dd583296SKevin Wolf { 2370dd583296SKevin Wolf char *input; 2371dd583296SKevin Wolf const cmdinfo_t *ct; 2372dd583296SKevin Wolf char **v; 2373dd583296SKevin Wolf int c; 2374dd583296SKevin Wolf bool done = false; 2375dd583296SKevin Wolf 2376dd583296SKevin Wolf input = g_strdup(cmd); 2377dd583296SKevin Wolf v = breakline(input, &c); 2378dd583296SKevin Wolf if (c) { 2379dd583296SKevin Wolf ct = find_command(v[0]); 2380dd583296SKevin Wolf if (ct) { 23814c7b7e9bSMax Reitz done = command(blk, ct, c, v); 2382dd583296SKevin Wolf } else { 2383dd583296SKevin Wolf fprintf(stderr, "command \"%s\" not found\n", v[0]); 2384dd583296SKevin Wolf } 2385dd583296SKevin Wolf } 2386dd583296SKevin Wolf g_free(input); 2387dd583296SKevin Wolf g_free(v); 2388dd583296SKevin Wolf 2389dd583296SKevin Wolf return done; 2390dd583296SKevin Wolf } 2391dd583296SKevin Wolf 2392797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void) 2393797ac58cSKevin Wolf { 2394797ac58cSKevin Wolf /* initialize commands */ 2395c2cdf5c5SKevin Wolf qemuio_add_command(&help_cmd); 2396c2cdf5c5SKevin Wolf qemuio_add_command(&read_cmd); 2397c2cdf5c5SKevin Wolf qemuio_add_command(&readv_cmd); 2398c2cdf5c5SKevin Wolf qemuio_add_command(&write_cmd); 2399c2cdf5c5SKevin Wolf qemuio_add_command(&writev_cmd); 2400c2cdf5c5SKevin Wolf qemuio_add_command(&multiwrite_cmd); 2401c2cdf5c5SKevin Wolf qemuio_add_command(&aio_read_cmd); 2402c2cdf5c5SKevin Wolf qemuio_add_command(&aio_write_cmd); 2403c2cdf5c5SKevin Wolf qemuio_add_command(&aio_flush_cmd); 2404c2cdf5c5SKevin Wolf qemuio_add_command(&flush_cmd); 2405c2cdf5c5SKevin Wolf qemuio_add_command(&truncate_cmd); 2406c2cdf5c5SKevin Wolf qemuio_add_command(&length_cmd); 2407c2cdf5c5SKevin Wolf qemuio_add_command(&info_cmd); 2408c2cdf5c5SKevin Wolf qemuio_add_command(&discard_cmd); 2409c2cdf5c5SKevin Wolf qemuio_add_command(&alloc_cmd); 2410c2cdf5c5SKevin Wolf qemuio_add_command(&map_cmd); 24115bbd2e59SKevin Wolf qemuio_add_command(&reopen_cmd); 2412c2cdf5c5SKevin Wolf qemuio_add_command(&break_cmd); 24134cc70e93SFam Zheng qemuio_add_command(&remove_break_cmd); 2414c2cdf5c5SKevin Wolf qemuio_add_command(&resume_cmd); 2415c2cdf5c5SKevin Wolf qemuio_add_command(&wait_break_cmd); 2416c2cdf5c5SKevin Wolf qemuio_add_command(&abort_cmd); 2417cd33d02aSKevin Wolf qemuio_add_command(&sleep_cmd); 24180e82dc7bSMax Reitz qemuio_add_command(&sigraise_cmd); 2419797ac58cSKevin Wolf } 2420