1797ac58cSKevin Wolf /* 2797ac58cSKevin Wolf * Command line utility to exercise the QEMU I/O path. 3797ac58cSKevin Wolf * 4093ea232SEric Blake * Copyright (C) 2009-2016 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 1180c71a24SPeter Maydell #include "qemu/osdep.h" 12da34e65cSMarkus Armbruster #include "qapi/error.h" 13dc900c35SAlberto Garcia #include "qapi/qmp/qdict.h" 143d21994fSKevin Wolf #include "qemu-io.h" 154c7b7e9bSMax Reitz #include "sysemu/block-backend.h" 164c7b7e9bSMax Reitz #include "block/block.h" 174c7b7e9bSMax Reitz #include "block/block_int.h" /* for info_f() */ 18a8d8ecb7SMax Reitz #include "block/qapi.h" 19d49b6836SMarkus Armbruster #include "qemu/error-report.h" 206a1751b7SAlex Bligh #include "qemu/main-loop.h" 21922a01a0SMarkus Armbruster #include "qemu/option.h" 22cd33d02aSKevin Wolf #include "qemu/timer.h" 23f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 24797ac58cSKevin Wolf 25797ac58cSKevin Wolf #define CMD_NOFILE_OK 0x01 26797ac58cSKevin Wolf 27f9883880SStefan Weil bool qemuio_misalign; 28797ac58cSKevin Wolf 29c2cdf5c5SKevin Wolf static cmdinfo_t *cmdtab; 30c2cdf5c5SKevin Wolf static int ncmds; 31c2cdf5c5SKevin Wolf 32c2cdf5c5SKevin Wolf static int compare_cmdname(const void *a, const void *b) 33c2cdf5c5SKevin Wolf { 34c2cdf5c5SKevin Wolf return strcmp(((const cmdinfo_t *)a)->name, 35c2cdf5c5SKevin Wolf ((const cmdinfo_t *)b)->name); 36c2cdf5c5SKevin Wolf } 37c2cdf5c5SKevin Wolf 38c2cdf5c5SKevin Wolf void qemuio_add_command(const cmdinfo_t *ci) 39c2cdf5c5SKevin Wolf { 406aabeb58SPeter Maydell /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK 416aabeb58SPeter Maydell * flags allow it not to be, so that combination is invalid. 426aabeb58SPeter Maydell * Catch it now rather than letting it manifest as a crash if a 436aabeb58SPeter Maydell * particular set of command line options are used. 446aabeb58SPeter Maydell */ 456aabeb58SPeter Maydell assert(ci->perm == 0 || 466aabeb58SPeter Maydell (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0); 4702c4f26bSMarkus Armbruster cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds); 48c2cdf5c5SKevin Wolf cmdtab[ncmds - 1] = *ci; 49c2cdf5c5SKevin Wolf qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname); 50c2cdf5c5SKevin Wolf } 51c2cdf5c5SKevin Wolf 52b444d0e9SMax Reitz void qemuio_command_usage(const cmdinfo_t *ci) 53c2cdf5c5SKevin Wolf { 54c2cdf5c5SKevin Wolf printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline); 55c2cdf5c5SKevin Wolf } 56c2cdf5c5SKevin Wolf 574c7b7e9bSMax Reitz static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct) 58c2cdf5c5SKevin Wolf { 59c2cdf5c5SKevin Wolf if (ct->flags & CMD_FLAG_GLOBAL) { 60c2cdf5c5SKevin Wolf return 1; 61c2cdf5c5SKevin Wolf } 624c7b7e9bSMax Reitz if (!(ct->flags & CMD_NOFILE_OK) && !blk) { 63c2cdf5c5SKevin Wolf fprintf(stderr, "no file open, try 'help open'\n"); 64c2cdf5c5SKevin Wolf return 0; 65c2cdf5c5SKevin Wolf } 66c2cdf5c5SKevin Wolf return 1; 67c2cdf5c5SKevin Wolf } 68c2cdf5c5SKevin Wolf 69b32d7a39SMax Reitz static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc, 703d21994fSKevin Wolf char **argv) 71c2cdf5c5SKevin Wolf { 72c2cdf5c5SKevin Wolf char *cmd = argv[0]; 73c2cdf5c5SKevin Wolf 744c7b7e9bSMax Reitz if (!init_check_command(blk, ct)) { 75b32d7a39SMax Reitz return -EINVAL; 76c2cdf5c5SKevin Wolf } 77c2cdf5c5SKevin Wolf 78c2cdf5c5SKevin Wolf if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) { 79c2cdf5c5SKevin Wolf if (ct->argmax == -1) { 80c2cdf5c5SKevin Wolf fprintf(stderr, 81c2cdf5c5SKevin Wolf "bad argument count %d to %s, expected at least %d arguments\n", 82c2cdf5c5SKevin Wolf argc-1, cmd, ct->argmin); 83c2cdf5c5SKevin Wolf } else if (ct->argmin == ct->argmax) { 84c2cdf5c5SKevin Wolf fprintf(stderr, 85c2cdf5c5SKevin Wolf "bad argument count %d to %s, expected %d arguments\n", 86c2cdf5c5SKevin Wolf argc-1, cmd, ct->argmin); 87c2cdf5c5SKevin Wolf } else { 88c2cdf5c5SKevin Wolf fprintf(stderr, 89c2cdf5c5SKevin Wolf "bad argument count %d to %s, expected between %d and %d arguments\n", 90c2cdf5c5SKevin Wolf argc-1, cmd, ct->argmin, ct->argmax); 91c2cdf5c5SKevin Wolf } 92b32d7a39SMax Reitz return -EINVAL; 93c2cdf5c5SKevin Wolf } 94887354bdSKevin Wolf 95887354bdSKevin Wolf /* Request additional permissions if necessary for this command. The caller 96887354bdSKevin Wolf * is responsible for restoring the original permissions afterwards if this 97887354bdSKevin Wolf * is what it wants. */ 98887354bdSKevin Wolf if (ct->perm && blk_is_available(blk)) { 99887354bdSKevin Wolf uint64_t orig_perm, orig_shared_perm; 100887354bdSKevin Wolf blk_get_perm(blk, &orig_perm, &orig_shared_perm); 101887354bdSKevin Wolf 102887354bdSKevin Wolf if (ct->perm & ~orig_perm) { 103887354bdSKevin Wolf uint64_t new_perm; 104887354bdSKevin Wolf Error *local_err = NULL; 105887354bdSKevin Wolf int ret; 106887354bdSKevin Wolf 107887354bdSKevin Wolf new_perm = orig_perm | ct->perm; 108887354bdSKevin Wolf 109887354bdSKevin Wolf ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err); 110887354bdSKevin Wolf if (ret < 0) { 111887354bdSKevin Wolf error_report_err(local_err); 112b32d7a39SMax Reitz return ret; 113887354bdSKevin Wolf } 114887354bdSKevin Wolf } 115887354bdSKevin Wolf } 116887354bdSKevin Wolf 117d339d766SRichard W.M. Jones qemu_reset_optind(); 118b32d7a39SMax Reitz return ct->cfunc(blk, argc, argv); 119c2cdf5c5SKevin Wolf } 120c2cdf5c5SKevin Wolf 121c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd) 122c2cdf5c5SKevin Wolf { 123c2cdf5c5SKevin Wolf cmdinfo_t *ct; 124c2cdf5c5SKevin Wolf 125c2cdf5c5SKevin Wolf for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 126c2cdf5c5SKevin Wolf if (strcmp(ct->name, cmd) == 0 || 127c2cdf5c5SKevin Wolf (ct->altname && strcmp(ct->altname, cmd) == 0)) 128c2cdf5c5SKevin Wolf { 129c2cdf5c5SKevin Wolf return (const cmdinfo_t *)ct; 130c2cdf5c5SKevin Wolf } 131c2cdf5c5SKevin Wolf } 132c2cdf5c5SKevin Wolf return NULL; 133c2cdf5c5SKevin Wolf } 134c2cdf5c5SKevin Wolf 1354694020dSStefan Hajnoczi /* Invoke fn() for commands with a matching prefix */ 1364694020dSStefan Hajnoczi void qemuio_complete_command(const char *input, 1374694020dSStefan Hajnoczi void (*fn)(const char *cmd, void *opaque), 1384694020dSStefan Hajnoczi void *opaque) 1394694020dSStefan Hajnoczi { 1404694020dSStefan Hajnoczi cmdinfo_t *ct; 1414694020dSStefan Hajnoczi size_t input_len = strlen(input); 1424694020dSStefan Hajnoczi 1434694020dSStefan Hajnoczi for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 1444694020dSStefan Hajnoczi if (strncmp(input, ct->name, input_len) == 0) { 1454694020dSStefan Hajnoczi fn(ct->name, opaque); 1464694020dSStefan Hajnoczi } 1474694020dSStefan Hajnoczi } 1484694020dSStefan Hajnoczi } 1494694020dSStefan Hajnoczi 150c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count) 151c2cdf5c5SKevin Wolf { 152c2cdf5c5SKevin Wolf int c = 0; 153c2cdf5c5SKevin Wolf char *p; 1545839e53bSMarkus Armbruster char **rval = g_new0(char *, 1); 155c2cdf5c5SKevin Wolf 156c2cdf5c5SKevin Wolf while (rval && (p = qemu_strsep(&input, " ")) != NULL) { 157c2cdf5c5SKevin Wolf if (!*p) { 158c2cdf5c5SKevin Wolf continue; 159c2cdf5c5SKevin Wolf } 160c2cdf5c5SKevin Wolf c++; 16108193dd5SMarkus Armbruster rval = g_renew(char *, rval, (c + 1)); 162c2cdf5c5SKevin Wolf rval[c - 1] = p; 163c2cdf5c5SKevin Wolf rval[c] = NULL; 164c2cdf5c5SKevin Wolf } 165c2cdf5c5SKevin Wolf *count = c; 166c2cdf5c5SKevin Wolf return rval; 167c2cdf5c5SKevin Wolf } 168c2cdf5c5SKevin Wolf 169797ac58cSKevin Wolf static int64_t cvtnum(const char *s) 170797ac58cSKevin Wolf { 171f17fd4fdSMarkus Armbruster int err; 172f46bfdbfSMarkus Armbruster uint64_t value; 173ef5a7885SJohn Snow 174f17fd4fdSMarkus Armbruster err = qemu_strtosz(s, NULL, &value); 175f17fd4fdSMarkus Armbruster if (err < 0) { 176f17fd4fdSMarkus Armbruster return err; 177f17fd4fdSMarkus Armbruster } 178f46bfdbfSMarkus Armbruster if (value > INT64_MAX) { 179f46bfdbfSMarkus Armbruster return -ERANGE; 180f46bfdbfSMarkus Armbruster } 181f17fd4fdSMarkus Armbruster return value; 182797ac58cSKevin Wolf } 183797ac58cSKevin Wolf 184a9ecfa00SJohn Snow static void print_cvtnum_err(int64_t rc, const char *arg) 185a9ecfa00SJohn Snow { 186a9ecfa00SJohn Snow switch (rc) { 187a9ecfa00SJohn Snow case -EINVAL: 188a9ecfa00SJohn Snow printf("Parsing error: non-numeric argument," 189a9ecfa00SJohn Snow " or extraneous/unrecognized suffix -- %s\n", arg); 190a9ecfa00SJohn Snow break; 191a9ecfa00SJohn Snow case -ERANGE: 192a9ecfa00SJohn Snow printf("Parsing error: argument too large -- %s\n", arg); 193a9ecfa00SJohn Snow break; 194a9ecfa00SJohn Snow default: 195a9ecfa00SJohn Snow printf("Parsing error: %s\n", arg); 196a9ecfa00SJohn Snow } 197a9ecfa00SJohn Snow } 198a9ecfa00SJohn Snow 1990b613881SKevin Wolf #define EXABYTES(x) ((long long)(x) << 60) 2000b613881SKevin Wolf #define PETABYTES(x) ((long long)(x) << 50) 2010b613881SKevin Wolf #define TERABYTES(x) ((long long)(x) << 40) 2020b613881SKevin Wolf #define GIGABYTES(x) ((long long)(x) << 30) 2030b613881SKevin Wolf #define MEGABYTES(x) ((long long)(x) << 20) 2040b613881SKevin Wolf #define KILOBYTES(x) ((long long)(x) << 10) 2050b613881SKevin Wolf 2060b613881SKevin Wolf #define TO_EXABYTES(x) ((x) / EXABYTES(1)) 2070b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1)) 2080b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1)) 2090b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1)) 2100b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1)) 2110b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1)) 2120b613881SKevin Wolf 2130b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size) 2140b613881SKevin Wolf { 2150b613881SKevin Wolf char *trim; 2160b613881SKevin Wolf const char *suffix; 2170b613881SKevin Wolf 2180b613881SKevin Wolf if (value >= EXABYTES(1)) { 2190b613881SKevin Wolf suffix = " EiB"; 2200b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_EXABYTES(value)); 2210b613881SKevin Wolf } else if (value >= PETABYTES(1)) { 2220b613881SKevin Wolf suffix = " PiB"; 2230b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_PETABYTES(value)); 2240b613881SKevin Wolf } else if (value >= TERABYTES(1)) { 2250b613881SKevin Wolf suffix = " TiB"; 2260b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_TERABYTES(value)); 2270b613881SKevin Wolf } else if (value >= GIGABYTES(1)) { 2280b613881SKevin Wolf suffix = " GiB"; 2290b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value)); 2300b613881SKevin Wolf } else if (value >= MEGABYTES(1)) { 2310b613881SKevin Wolf suffix = " MiB"; 2320b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value)); 2330b613881SKevin Wolf } else if (value >= KILOBYTES(1)) { 2340b613881SKevin Wolf suffix = " KiB"; 2350b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value)); 2360b613881SKevin Wolf } else { 2370b613881SKevin Wolf suffix = " bytes"; 2380b613881SKevin Wolf snprintf(str, size - 6, "%f", value); 2390b613881SKevin Wolf } 2400b613881SKevin Wolf 2410b613881SKevin Wolf trim = strstr(str, ".000"); 2420b613881SKevin Wolf if (trim) { 2430b613881SKevin Wolf strcpy(trim, suffix); 2440b613881SKevin Wolf } else { 2450b613881SKevin Wolf strcat(str, suffix); 2460b613881SKevin Wolf } 2470b613881SKevin Wolf } 2480b613881SKevin Wolf 2490b613881SKevin Wolf 2500b613881SKevin Wolf 25150290c00SAlex Bennée static struct timespec tsub(struct timespec t1, struct timespec t2) 2520b613881SKevin Wolf { 25350290c00SAlex Bennée t1.tv_nsec -= t2.tv_nsec; 25450290c00SAlex Bennée if (t1.tv_nsec < 0) { 25550290c00SAlex Bennée t1.tv_nsec += NANOSECONDS_PER_SECOND; 2560b613881SKevin Wolf t1.tv_sec--; 2570b613881SKevin Wolf } 2580b613881SKevin Wolf t1.tv_sec -= t2.tv_sec; 2590b613881SKevin Wolf return t1; 2600b613881SKevin Wolf } 2610b613881SKevin Wolf 26250290c00SAlex Bennée static double tdiv(double value, struct timespec tv) 2630b613881SKevin Wolf { 26450290c00SAlex Bennée double seconds = tv.tv_sec + (tv.tv_nsec / 1e9); 26550290c00SAlex Bennée return value / seconds; 2660b613881SKevin Wolf } 2670b613881SKevin Wolf 2680b613881SKevin Wolf #define HOURS(sec) ((sec) / (60 * 60)) 2690b613881SKevin Wolf #define MINUTES(sec) (((sec) % (60 * 60)) / 60) 2700b613881SKevin Wolf #define SECONDS(sec) ((sec) % 60) 2710b613881SKevin Wolf 2720b613881SKevin Wolf enum { 2730b613881SKevin Wolf DEFAULT_TIME = 0x0, 2740b613881SKevin Wolf TERSE_FIXED_TIME = 0x1, 2750b613881SKevin Wolf VERBOSE_FIXED_TIME = 0x2, 2760b613881SKevin Wolf }; 2770b613881SKevin Wolf 27850290c00SAlex Bennée static void timestr(struct timespec *tv, char *ts, size_t size, int format) 2790b613881SKevin Wolf { 28050290c00SAlex Bennée double frac_sec = tv->tv_nsec / 1e9; 2810b613881SKevin Wolf 2820b613881SKevin Wolf if (format & TERSE_FIXED_TIME) { 2830b613881SKevin Wolf if (!HOURS(tv->tv_sec)) { 28450290c00SAlex Bennée snprintf(ts, size, "%u:%05.2f", 2850b613881SKevin Wolf (unsigned int) MINUTES(tv->tv_sec), 28650290c00SAlex Bennée SECONDS(tv->tv_sec) + frac_sec); 2870b613881SKevin Wolf return; 2880b613881SKevin Wolf } 2890b613881SKevin Wolf format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */ 2900b613881SKevin Wolf } 2910b613881SKevin Wolf 2920b613881SKevin Wolf if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) { 29350290c00SAlex Bennée snprintf(ts, size, "%u:%02u:%05.2f", 2940b613881SKevin Wolf (unsigned int) HOURS(tv->tv_sec), 2950b613881SKevin Wolf (unsigned int) MINUTES(tv->tv_sec), 29650290c00SAlex Bennée SECONDS(tv->tv_sec) + frac_sec); 2970b613881SKevin Wolf } else { 29850290c00SAlex Bennée snprintf(ts, size, "%05.2f sec", frac_sec); 2990b613881SKevin Wolf } 3000b613881SKevin Wolf } 3010b613881SKevin Wolf 302797ac58cSKevin Wolf /* 303797ac58cSKevin Wolf * Parse the pattern argument to various sub-commands. 304797ac58cSKevin Wolf * 305797ac58cSKevin Wolf * Because the pattern is used as an argument to memset it must evaluate 306797ac58cSKevin Wolf * to an unsigned integer that fits into a single byte. 307797ac58cSKevin Wolf */ 308797ac58cSKevin Wolf static int parse_pattern(const char *arg) 309797ac58cSKevin Wolf { 310797ac58cSKevin Wolf char *endptr = NULL; 311797ac58cSKevin Wolf long pattern; 312797ac58cSKevin Wolf 313797ac58cSKevin Wolf pattern = strtol(arg, &endptr, 0); 314797ac58cSKevin Wolf if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') { 315797ac58cSKevin Wolf printf("%s is not a valid pattern byte\n", arg); 316797ac58cSKevin Wolf return -1; 317797ac58cSKevin Wolf } 318797ac58cSKevin Wolf 319797ac58cSKevin Wolf return pattern; 320797ac58cSKevin Wolf } 321797ac58cSKevin Wolf 322797ac58cSKevin Wolf /* 323797ac58cSKevin Wolf * Memory allocation helpers. 324797ac58cSKevin Wolf * 325797ac58cSKevin Wolf * Make sure memory is aligned by default, or purposefully misaligned if 326797ac58cSKevin Wolf * that is specified on the command line. 327797ac58cSKevin Wolf */ 328797ac58cSKevin Wolf 329797ac58cSKevin Wolf #define MISALIGN_OFFSET 16 3304c7b7e9bSMax Reitz static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern) 331797ac58cSKevin Wolf { 332797ac58cSKevin Wolf void *buf; 333797ac58cSKevin Wolf 334797ac58cSKevin Wolf if (qemuio_misalign) { 335797ac58cSKevin Wolf len += MISALIGN_OFFSET; 336797ac58cSKevin Wolf } 3374c7b7e9bSMax Reitz buf = blk_blockalign(blk, len); 338797ac58cSKevin Wolf memset(buf, pattern, len); 339797ac58cSKevin Wolf if (qemuio_misalign) { 340797ac58cSKevin Wolf buf += MISALIGN_OFFSET; 341797ac58cSKevin Wolf } 342797ac58cSKevin Wolf return buf; 343797ac58cSKevin Wolf } 344797ac58cSKevin Wolf 345797ac58cSKevin Wolf static void qemu_io_free(void *p) 346797ac58cSKevin Wolf { 347797ac58cSKevin Wolf if (qemuio_misalign) { 348797ac58cSKevin Wolf p -= MISALIGN_OFFSET; 349797ac58cSKevin Wolf } 350797ac58cSKevin Wolf qemu_vfree(p); 351797ac58cSKevin Wolf } 352797ac58cSKevin Wolf 3534d731510SDenis Plotnikov /* 3544d731510SDenis Plotnikov * qemu_io_alloc_from_file() 3554d731510SDenis Plotnikov * 3564d731510SDenis Plotnikov * Allocates the buffer and populates it with the content of the given file 3574d731510SDenis Plotnikov * up to @len bytes. If the file length is less than @len, then the buffer 3584d731510SDenis Plotnikov * is populated with the file content cyclically. 3594d731510SDenis Plotnikov * 3604d731510SDenis Plotnikov * @blk - the block backend where the buffer content is going to be written to 3614d731510SDenis Plotnikov * @len - the buffer length 3624d731510SDenis Plotnikov * @file_name - the file to read the content from 3634d731510SDenis Plotnikov * 3644d731510SDenis Plotnikov * Returns: the buffer pointer on success 3654d731510SDenis Plotnikov * NULL on error 3664d731510SDenis Plotnikov */ 3674d731510SDenis Plotnikov static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len, 3684d731510SDenis Plotnikov const char *file_name) 3694d731510SDenis Plotnikov { 3704d731510SDenis Plotnikov char *buf, *buf_origin; 3714d731510SDenis Plotnikov FILE *f = fopen(file_name, "r"); 3724d731510SDenis Plotnikov int pattern_len; 3734d731510SDenis Plotnikov 3744d731510SDenis Plotnikov if (!f) { 3754d731510SDenis Plotnikov perror(file_name); 3764d731510SDenis Plotnikov return NULL; 3774d731510SDenis Plotnikov } 3784d731510SDenis Plotnikov 3794d731510SDenis Plotnikov if (qemuio_misalign) { 3804d731510SDenis Plotnikov len += MISALIGN_OFFSET; 3814d731510SDenis Plotnikov } 3824d731510SDenis Plotnikov 3834d731510SDenis Plotnikov buf_origin = buf = blk_blockalign(blk, len); 3844d731510SDenis Plotnikov 3854d731510SDenis Plotnikov if (qemuio_misalign) { 3864d731510SDenis Plotnikov buf_origin += MISALIGN_OFFSET; 3874d731510SDenis Plotnikov buf += MISALIGN_OFFSET; 3884d731510SDenis Plotnikov len -= MISALIGN_OFFSET; 3894d731510SDenis Plotnikov } 3904d731510SDenis Plotnikov 3914d731510SDenis Plotnikov pattern_len = fread(buf_origin, 1, len, f); 3924d731510SDenis Plotnikov 3934d731510SDenis Plotnikov if (ferror(f)) { 3944d731510SDenis Plotnikov perror(file_name); 3954d731510SDenis Plotnikov goto error; 3964d731510SDenis Plotnikov } 3974d731510SDenis Plotnikov 3984d731510SDenis Plotnikov if (pattern_len == 0) { 3994d731510SDenis Plotnikov fprintf(stderr, "%s: file is empty\n", file_name); 4004d731510SDenis Plotnikov goto error; 4014d731510SDenis Plotnikov } 4024d731510SDenis Plotnikov 4034d731510SDenis Plotnikov fclose(f); 404*c8e68b43SKevin Wolf f = NULL; 4054d731510SDenis Plotnikov 4064d731510SDenis Plotnikov if (len > pattern_len) { 4074d731510SDenis Plotnikov len -= pattern_len; 4084d731510SDenis Plotnikov buf += pattern_len; 4094d731510SDenis Plotnikov 4104d731510SDenis Plotnikov while (len > 0) { 4114d731510SDenis Plotnikov size_t len_to_copy = MIN(pattern_len, len); 4124d731510SDenis Plotnikov 4134d731510SDenis Plotnikov memcpy(buf, buf_origin, len_to_copy); 4144d731510SDenis Plotnikov 4154d731510SDenis Plotnikov len -= len_to_copy; 4164d731510SDenis Plotnikov buf += len_to_copy; 4174d731510SDenis Plotnikov } 4184d731510SDenis Plotnikov } 4194d731510SDenis Plotnikov 4204d731510SDenis Plotnikov return buf_origin; 4214d731510SDenis Plotnikov 4224d731510SDenis Plotnikov error: 4234d731510SDenis Plotnikov qemu_io_free(buf_origin); 424*c8e68b43SKevin Wolf if (f) { 425*c8e68b43SKevin Wolf fclose(f); 426*c8e68b43SKevin Wolf } 4274d731510SDenis Plotnikov return NULL; 4284d731510SDenis Plotnikov } 4294d731510SDenis Plotnikov 4309b0beaf3SJohn Snow static void dump_buffer(const void *buffer, int64_t offset, int64_t len) 431797ac58cSKevin Wolf { 4329b0beaf3SJohn Snow uint64_t i; 4339b0beaf3SJohn Snow int j; 434797ac58cSKevin Wolf const uint8_t *p; 435797ac58cSKevin Wolf 436797ac58cSKevin Wolf for (i = 0, p = buffer; i < len; i += 16) { 437797ac58cSKevin Wolf const uint8_t *s = p; 438797ac58cSKevin Wolf 439797ac58cSKevin Wolf printf("%08" PRIx64 ": ", offset + i); 440797ac58cSKevin Wolf for (j = 0; j < 16 && i + j < len; j++, p++) { 441797ac58cSKevin Wolf printf("%02x ", *p); 442797ac58cSKevin Wolf } 443797ac58cSKevin Wolf printf(" "); 444797ac58cSKevin Wolf for (j = 0; j < 16 && i + j < len; j++, s++) { 445797ac58cSKevin Wolf if (isalnum(*s)) { 446797ac58cSKevin Wolf printf("%c", *s); 447797ac58cSKevin Wolf } else { 448797ac58cSKevin Wolf printf("."); 449797ac58cSKevin Wolf } 450797ac58cSKevin Wolf } 451797ac58cSKevin Wolf printf("\n"); 452797ac58cSKevin Wolf } 453797ac58cSKevin Wolf } 454797ac58cSKevin Wolf 45550290c00SAlex Bennée static void print_report(const char *op, struct timespec *t, int64_t offset, 456dc38852aSEric Blake int64_t count, int64_t total, int cnt, bool Cflag) 457797ac58cSKevin Wolf { 458797ac58cSKevin Wolf char s1[64], s2[64], ts[64]; 459797ac58cSKevin Wolf 460797ac58cSKevin Wolf timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0); 461797ac58cSKevin Wolf if (!Cflag) { 462797ac58cSKevin Wolf cvtstr((double)total, s1, sizeof(s1)); 463797ac58cSKevin Wolf cvtstr(tdiv((double)total, *t), s2, sizeof(s2)); 4649b0beaf3SJohn Snow printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n", 465797ac58cSKevin Wolf op, total, count, offset); 466797ac58cSKevin Wolf printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n", 467797ac58cSKevin Wolf s1, cnt, ts, s2, tdiv((double)cnt, *t)); 468797ac58cSKevin Wolf } else {/* bytes,ops,time,bytes/sec,ops/sec */ 4699b0beaf3SJohn Snow printf("%"PRId64",%d,%s,%.3f,%.3f\n", 470797ac58cSKevin Wolf total, cnt, ts, 471797ac58cSKevin Wolf tdiv((double)total, *t), 472797ac58cSKevin Wolf tdiv((double)cnt, *t)); 473797ac58cSKevin Wolf } 474797ac58cSKevin Wolf } 475797ac58cSKevin Wolf 476797ac58cSKevin Wolf /* 477797ac58cSKevin Wolf * Parse multiple length statements for vectored I/O, and construct an I/O 478797ac58cSKevin Wolf * vector matching it. 479797ac58cSKevin Wolf */ 480797ac58cSKevin Wolf static void * 4814c7b7e9bSMax Reitz create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov, 482797ac58cSKevin Wolf int pattern) 483797ac58cSKevin Wolf { 484797ac58cSKevin Wolf size_t *sizes = g_new0(size_t, nr_iov); 485797ac58cSKevin Wolf size_t count = 0; 486797ac58cSKevin Wolf void *buf = NULL; 487797ac58cSKevin Wolf void *p; 488797ac58cSKevin Wolf int i; 489797ac58cSKevin Wolf 490797ac58cSKevin Wolf for (i = 0; i < nr_iov; i++) { 491797ac58cSKevin Wolf char *arg = argv[i]; 492797ac58cSKevin Wolf int64_t len; 493797ac58cSKevin Wolf 494797ac58cSKevin Wolf len = cvtnum(arg); 495797ac58cSKevin Wolf if (len < 0) { 496a9ecfa00SJohn Snow print_cvtnum_err(len, arg); 497797ac58cSKevin Wolf goto fail; 498797ac58cSKevin Wolf } 499797ac58cSKevin Wolf 5003026c468SAlberto Garcia if (len > BDRV_REQUEST_MAX_BYTES) { 5013026c468SAlberto Garcia printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg, 5023026c468SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES); 5033026c468SAlberto Garcia goto fail; 5043026c468SAlberto Garcia } 5053026c468SAlberto Garcia 5063026c468SAlberto Garcia if (count > BDRV_REQUEST_MAX_BYTES - len) { 5073026c468SAlberto Garcia printf("The total number of bytes exceed the maximum size %" PRIu64 5083026c468SAlberto Garcia "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES); 509797ac58cSKevin Wolf goto fail; 510797ac58cSKevin Wolf } 511797ac58cSKevin Wolf 512797ac58cSKevin Wolf sizes[i] = len; 513797ac58cSKevin Wolf count += len; 514797ac58cSKevin Wolf } 515797ac58cSKevin Wolf 516797ac58cSKevin Wolf qemu_iovec_init(qiov, nr_iov); 517797ac58cSKevin Wolf 5184c7b7e9bSMax Reitz buf = p = qemu_io_alloc(blk, count, pattern); 519797ac58cSKevin Wolf 520797ac58cSKevin Wolf for (i = 0; i < nr_iov; i++) { 521797ac58cSKevin Wolf qemu_iovec_add(qiov, p, sizes[i]); 522797ac58cSKevin Wolf p += sizes[i]; 523797ac58cSKevin Wolf } 524797ac58cSKevin Wolf 525797ac58cSKevin Wolf fail: 526797ac58cSKevin Wolf g_free(sizes); 527797ac58cSKevin Wolf return buf; 528797ac58cSKevin Wolf } 529797ac58cSKevin Wolf 5309b0beaf3SJohn Snow static int do_pread(BlockBackend *blk, char *buf, int64_t offset, 531f5a5ca79SManos Pitsidianakis int64_t bytes, int64_t *total) 532797ac58cSKevin Wolf { 533f5a5ca79SManos Pitsidianakis if (bytes > INT_MAX) { 5349b0beaf3SJohn Snow return -ERANGE; 5359b0beaf3SJohn Snow } 5369b0beaf3SJohn Snow 537f5a5ca79SManos Pitsidianakis *total = blk_pread(blk, offset, (uint8_t *)buf, bytes); 538797ac58cSKevin Wolf if (*total < 0) { 539797ac58cSKevin Wolf return *total; 540797ac58cSKevin Wolf } 541797ac58cSKevin Wolf return 1; 542797ac58cSKevin Wolf } 543797ac58cSKevin Wolf 5449b0beaf3SJohn Snow static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset, 545f5a5ca79SManos Pitsidianakis int64_t bytes, int flags, int64_t *total) 546797ac58cSKevin Wolf { 547f5a5ca79SManos Pitsidianakis if (bytes > INT_MAX) { 5489b0beaf3SJohn Snow return -ERANGE; 5499b0beaf3SJohn Snow } 5509b0beaf3SJohn Snow 551f5a5ca79SManos Pitsidianakis *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags); 552797ac58cSKevin Wolf if (*total < 0) { 553797ac58cSKevin Wolf return *total; 554797ac58cSKevin Wolf } 555797ac58cSKevin Wolf return 1; 556797ac58cSKevin Wolf } 557797ac58cSKevin Wolf 558797ac58cSKevin Wolf typedef struct { 5594c7b7e9bSMax Reitz BlockBackend *blk; 560797ac58cSKevin Wolf int64_t offset; 561f5a5ca79SManos Pitsidianakis int64_t bytes; 5629b0beaf3SJohn Snow int64_t *total; 563770e0e0eSEric Blake int flags; 564797ac58cSKevin Wolf int ret; 565797ac58cSKevin Wolf bool done; 566797ac58cSKevin Wolf } CoWriteZeroes; 567797ac58cSKevin Wolf 568d004bd52SEric Blake static void coroutine_fn co_pwrite_zeroes_entry(void *opaque) 569797ac58cSKevin Wolf { 570797ac58cSKevin Wolf CoWriteZeroes *data = opaque; 571797ac58cSKevin Wolf 572f5a5ca79SManos Pitsidianakis data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes, 573770e0e0eSEric Blake data->flags); 574797ac58cSKevin Wolf data->done = true; 575797ac58cSKevin Wolf if (data->ret < 0) { 576797ac58cSKevin Wolf *data->total = data->ret; 577797ac58cSKevin Wolf return; 578797ac58cSKevin Wolf } 579797ac58cSKevin Wolf 580f5a5ca79SManos Pitsidianakis *data->total = data->bytes; 581797ac58cSKevin Wolf } 582797ac58cSKevin Wolf 583d004bd52SEric Blake static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset, 584f5a5ca79SManos Pitsidianakis int64_t bytes, int flags, int64_t *total) 585797ac58cSKevin Wolf { 586797ac58cSKevin Wolf Coroutine *co; 587797ac58cSKevin Wolf CoWriteZeroes data = { 5884c7b7e9bSMax Reitz .blk = blk, 589797ac58cSKevin Wolf .offset = offset, 590f5a5ca79SManos Pitsidianakis .bytes = bytes, 591797ac58cSKevin Wolf .total = total, 592770e0e0eSEric Blake .flags = flags, 593797ac58cSKevin Wolf .done = false, 594797ac58cSKevin Wolf }; 595797ac58cSKevin Wolf 596f5a5ca79SManos Pitsidianakis if (bytes > INT_MAX) { 5979b0beaf3SJohn Snow return -ERANGE; 5989b0beaf3SJohn Snow } 5999b0beaf3SJohn Snow 6000b8b8753SPaolo Bonzini co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data); 601324ec3e4SFam Zheng bdrv_coroutine_enter(blk_bs(blk), co); 602797ac58cSKevin Wolf while (!data.done) { 6034c7b7e9bSMax Reitz aio_poll(blk_get_aio_context(blk), true); 604797ac58cSKevin Wolf } 605797ac58cSKevin Wolf if (data.ret < 0) { 606797ac58cSKevin Wolf return data.ret; 607797ac58cSKevin Wolf } else { 608797ac58cSKevin Wolf return 1; 609797ac58cSKevin Wolf } 610797ac58cSKevin Wolf } 611797ac58cSKevin Wolf 6124c7b7e9bSMax Reitz static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset, 613f5a5ca79SManos Pitsidianakis int64_t bytes, int64_t *total) 614797ac58cSKevin Wolf { 615797ac58cSKevin Wolf int ret; 616797ac58cSKevin Wolf 61741ae31e3SAlberto Garcia if (bytes > BDRV_REQUEST_MAX_BYTES) { 6189b0beaf3SJohn Snow return -ERANGE; 6199b0beaf3SJohn Snow } 6209b0beaf3SJohn Snow 621f5a5ca79SManos Pitsidianakis ret = blk_pwrite_compressed(blk, offset, buf, bytes); 622797ac58cSKevin Wolf if (ret < 0) { 623797ac58cSKevin Wolf return ret; 624797ac58cSKevin Wolf } 625f5a5ca79SManos Pitsidianakis *total = bytes; 626797ac58cSKevin Wolf return 1; 627797ac58cSKevin Wolf } 628797ac58cSKevin Wolf 6294c7b7e9bSMax Reitz static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset, 6309b0beaf3SJohn Snow int64_t count, int64_t *total) 631797ac58cSKevin Wolf { 6329b0beaf3SJohn Snow if (count > INT_MAX) { 6339b0beaf3SJohn Snow return -ERANGE; 6349b0beaf3SJohn Snow } 6359b0beaf3SJohn Snow 6364c7b7e9bSMax Reitz *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count); 637797ac58cSKevin Wolf if (*total < 0) { 638797ac58cSKevin Wolf return *total; 639797ac58cSKevin Wolf } 640797ac58cSKevin Wolf return 1; 641797ac58cSKevin Wolf } 642797ac58cSKevin Wolf 6434c7b7e9bSMax Reitz static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset, 6449b0beaf3SJohn Snow int64_t count, int64_t *total) 645797ac58cSKevin Wolf { 6469b0beaf3SJohn Snow if (count > INT_MAX) { 6479b0beaf3SJohn Snow return -ERANGE; 6489b0beaf3SJohn Snow } 6499b0beaf3SJohn Snow 6504c7b7e9bSMax Reitz *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count); 651797ac58cSKevin Wolf if (*total < 0) { 652797ac58cSKevin Wolf return *total; 653797ac58cSKevin Wolf } 654797ac58cSKevin Wolf return 1; 655797ac58cSKevin Wolf } 656797ac58cSKevin Wolf 657797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff 658797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret) 659797ac58cSKevin Wolf { 660797ac58cSKevin Wolf *(int *)opaque = ret; 661797ac58cSKevin Wolf } 662797ac58cSKevin Wolf 6634c7b7e9bSMax Reitz static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov, 664797ac58cSKevin Wolf int64_t offset, int *total) 665797ac58cSKevin Wolf { 666797ac58cSKevin Wolf int async_ret = NOT_DONE; 667797ac58cSKevin Wolf 6687b3f9712SEric Blake blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret); 669797ac58cSKevin Wolf while (async_ret == NOT_DONE) { 670797ac58cSKevin Wolf main_loop_wait(false); 671797ac58cSKevin Wolf } 672797ac58cSKevin Wolf 673797ac58cSKevin Wolf *total = qiov->size; 674797ac58cSKevin Wolf return async_ret < 0 ? async_ret : 1; 675797ac58cSKevin Wolf } 676797ac58cSKevin Wolf 6774c7b7e9bSMax Reitz static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov, 678770e0e0eSEric Blake int64_t offset, int flags, int *total) 679797ac58cSKevin Wolf { 680797ac58cSKevin Wolf int async_ret = NOT_DONE; 681797ac58cSKevin Wolf 682770e0e0eSEric Blake blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret); 683797ac58cSKevin Wolf while (async_ret == NOT_DONE) { 684797ac58cSKevin Wolf main_loop_wait(false); 685797ac58cSKevin Wolf } 686797ac58cSKevin Wolf 687797ac58cSKevin Wolf *total = qiov->size; 688797ac58cSKevin Wolf return async_ret < 0 ? async_ret : 1; 689797ac58cSKevin Wolf } 690797ac58cSKevin Wolf 691797ac58cSKevin Wolf static void read_help(void) 692797ac58cSKevin Wolf { 693797ac58cSKevin Wolf printf( 694797ac58cSKevin Wolf "\n" 695797ac58cSKevin Wolf " reads a range of bytes from the given offset\n" 696797ac58cSKevin Wolf "\n" 697797ac58cSKevin Wolf " Example:\n" 698797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n" 699797ac58cSKevin Wolf "\n" 700797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 701797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 702797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n" 703797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 704797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n" 705093ea232SEric Blake " -p, -- ignored for backwards compatibility\n" 706797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 707797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 708797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n" 709797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 710797ac58cSKevin Wolf "\n"); 711797ac58cSKevin Wolf } 712797ac58cSKevin Wolf 713b32d7a39SMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv); 714797ac58cSKevin Wolf 715797ac58cSKevin Wolf static const cmdinfo_t read_cmd = { 716797ac58cSKevin Wolf .name = "read", 717797ac58cSKevin Wolf .altname = "r", 718797ac58cSKevin Wolf .cfunc = read_f, 719797ac58cSKevin Wolf .argmin = 2, 720797ac58cSKevin Wolf .argmax = -1, 721093ea232SEric Blake .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len", 722797ac58cSKevin Wolf .oneline = "reads a number of bytes at a specified offset", 723797ac58cSKevin Wolf .help = read_help, 724797ac58cSKevin Wolf }; 725797ac58cSKevin Wolf 726b32d7a39SMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv) 727797ac58cSKevin Wolf { 72850290c00SAlex Bennée struct timespec t1, t2; 729093ea232SEric Blake bool Cflag = false, qflag = false, vflag = false; 730dc38852aSEric Blake bool Pflag = false, sflag = false, lflag = false, bflag = false; 731b32d7a39SMax Reitz int c, cnt, ret; 732797ac58cSKevin Wolf char *buf; 733797ac58cSKevin Wolf int64_t offset; 7349b0beaf3SJohn Snow int64_t count; 735797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 7369b0beaf3SJohn Snow int64_t total = 0; 7379b0beaf3SJohn Snow int pattern = 0; 7389b0beaf3SJohn Snow int64_t pattern_offset = 0, pattern_count = 0; 739797ac58cSKevin Wolf 740b062ad86SEric Blake while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) { 741797ac58cSKevin Wolf switch (c) { 742797ac58cSKevin Wolf case 'b': 743dc38852aSEric Blake bflag = true; 744797ac58cSKevin Wolf break; 745797ac58cSKevin Wolf case 'C': 746dc38852aSEric Blake Cflag = true; 747797ac58cSKevin Wolf break; 748797ac58cSKevin Wolf case 'l': 749dc38852aSEric Blake lflag = true; 750797ac58cSKevin Wolf pattern_count = cvtnum(optarg); 751797ac58cSKevin Wolf if (pattern_count < 0) { 752a9ecfa00SJohn Snow print_cvtnum_err(pattern_count, optarg); 753b32d7a39SMax Reitz return pattern_count; 754797ac58cSKevin Wolf } 755797ac58cSKevin Wolf break; 756797ac58cSKevin Wolf case 'p': 757093ea232SEric Blake /* Ignored for backwards compatibility */ 758797ac58cSKevin Wolf break; 759797ac58cSKevin Wolf case 'P': 760dc38852aSEric Blake Pflag = true; 761797ac58cSKevin Wolf pattern = parse_pattern(optarg); 762797ac58cSKevin Wolf if (pattern < 0) { 763b32d7a39SMax Reitz return -EINVAL; 764797ac58cSKevin Wolf } 765797ac58cSKevin Wolf break; 766797ac58cSKevin Wolf case 'q': 767dc38852aSEric Blake qflag = true; 768797ac58cSKevin Wolf break; 769797ac58cSKevin Wolf case 's': 770dc38852aSEric Blake sflag = true; 771797ac58cSKevin Wolf pattern_offset = cvtnum(optarg); 772797ac58cSKevin Wolf if (pattern_offset < 0) { 773a9ecfa00SJohn Snow print_cvtnum_err(pattern_offset, optarg); 774b32d7a39SMax Reitz return pattern_offset; 775797ac58cSKevin Wolf } 776797ac58cSKevin Wolf break; 777797ac58cSKevin Wolf case 'v': 778dc38852aSEric Blake vflag = true; 779797ac58cSKevin Wolf break; 780797ac58cSKevin Wolf default: 781b444d0e9SMax Reitz qemuio_command_usage(&read_cmd); 782b32d7a39SMax Reitz return -EINVAL; 783797ac58cSKevin Wolf } 784797ac58cSKevin Wolf } 785797ac58cSKevin Wolf 786797ac58cSKevin Wolf if (optind != argc - 2) { 787b444d0e9SMax Reitz qemuio_command_usage(&read_cmd); 788b32d7a39SMax Reitz return -EINVAL; 789797ac58cSKevin Wolf } 790797ac58cSKevin Wolf 791797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 792797ac58cSKevin Wolf if (offset < 0) { 793a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 794b32d7a39SMax Reitz return offset; 795797ac58cSKevin Wolf } 796797ac58cSKevin Wolf 797797ac58cSKevin Wolf optind++; 798797ac58cSKevin Wolf count = cvtnum(argv[optind]); 799797ac58cSKevin Wolf if (count < 0) { 800a9ecfa00SJohn Snow print_cvtnum_err(count, argv[optind]); 801b32d7a39SMax Reitz return count; 8023026c468SAlberto Garcia } else if (count > BDRV_REQUEST_MAX_BYTES) { 8039b0beaf3SJohn Snow printf("length cannot exceed %" PRIu64 ", given %s\n", 8043026c468SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]); 805b32d7a39SMax Reitz return -EINVAL; 806797ac58cSKevin Wolf } 807797ac58cSKevin Wolf 808797ac58cSKevin Wolf if (!Pflag && (lflag || sflag)) { 809b444d0e9SMax Reitz qemuio_command_usage(&read_cmd); 810b32d7a39SMax Reitz return -EINVAL; 811797ac58cSKevin Wolf } 812797ac58cSKevin Wolf 813797ac58cSKevin Wolf if (!lflag) { 814797ac58cSKevin Wolf pattern_count = count - pattern_offset; 815797ac58cSKevin Wolf } 816797ac58cSKevin Wolf 817797ac58cSKevin Wolf if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) { 818797ac58cSKevin Wolf printf("pattern verification range exceeds end of read data\n"); 819b32d7a39SMax Reitz return -EINVAL; 820797ac58cSKevin Wolf } 821797ac58cSKevin Wolf 822093ea232SEric Blake if (bflag) { 8231bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) { 8241bce6b4cSEric Blake printf("%" PRId64 " is not a sector-aligned value for 'offset'\n", 825797ac58cSKevin Wolf offset); 826b32d7a39SMax Reitz return -EINVAL; 827797ac58cSKevin Wolf } 8281bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) { 8291bce6b4cSEric Blake printf("%"PRId64" is not a sector-aligned value for 'count'\n", 830797ac58cSKevin Wolf count); 831b32d7a39SMax Reitz return -EINVAL; 832797ac58cSKevin Wolf } 833797ac58cSKevin Wolf } 834797ac58cSKevin Wolf 8354c7b7e9bSMax Reitz buf = qemu_io_alloc(blk, count, 0xab); 836797ac58cSKevin Wolf 83750290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 8387b3f9712SEric Blake if (bflag) { 839b32d7a39SMax Reitz ret = do_load_vmstate(blk, buf, offset, count, &total); 840797ac58cSKevin Wolf } else { 841b32d7a39SMax Reitz ret = do_pread(blk, buf, offset, count, &total); 842797ac58cSKevin Wolf } 84350290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 844797ac58cSKevin Wolf 845b32d7a39SMax Reitz if (ret < 0) { 846b32d7a39SMax Reitz printf("read failed: %s\n", strerror(-ret)); 847797ac58cSKevin Wolf goto out; 848797ac58cSKevin Wolf } 849b32d7a39SMax Reitz cnt = ret; 850b32d7a39SMax Reitz 851b32d7a39SMax Reitz ret = 0; 852797ac58cSKevin Wolf 853797ac58cSKevin Wolf if (Pflag) { 854797ac58cSKevin Wolf void *cmp_buf = g_malloc(pattern_count); 855797ac58cSKevin Wolf memset(cmp_buf, pattern, pattern_count); 856797ac58cSKevin Wolf if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) { 857797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 8589b0beaf3SJohn Snow PRId64 ", %"PRId64" bytes\n", 859797ac58cSKevin Wolf offset + pattern_offset, pattern_count); 860b32d7a39SMax Reitz ret = -EINVAL; 861797ac58cSKevin Wolf } 862797ac58cSKevin Wolf g_free(cmp_buf); 863797ac58cSKevin Wolf } 864797ac58cSKevin Wolf 865797ac58cSKevin Wolf if (qflag) { 866797ac58cSKevin Wolf goto out; 867797ac58cSKevin Wolf } 868797ac58cSKevin Wolf 869797ac58cSKevin Wolf if (vflag) { 870797ac58cSKevin Wolf dump_buffer(buf, offset, count); 871797ac58cSKevin Wolf } 872797ac58cSKevin Wolf 873797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 874797ac58cSKevin Wolf t2 = tsub(t2, t1); 875797ac58cSKevin Wolf print_report("read", &t2, offset, count, total, cnt, Cflag); 876797ac58cSKevin Wolf 877797ac58cSKevin Wolf out: 878797ac58cSKevin Wolf qemu_io_free(buf); 879b32d7a39SMax Reitz return ret; 880797ac58cSKevin Wolf } 881797ac58cSKevin Wolf 882797ac58cSKevin Wolf static void readv_help(void) 883797ac58cSKevin Wolf { 884797ac58cSKevin Wolf printf( 885797ac58cSKevin Wolf "\n" 886797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n" 887797ac58cSKevin Wolf "\n" 888797ac58cSKevin Wolf " Example:\n" 889797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" 890797ac58cSKevin Wolf "\n" 891797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 892797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 893797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n" 894797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 895797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 896797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 897797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 898797ac58cSKevin Wolf "\n"); 899797ac58cSKevin Wolf } 900797ac58cSKevin Wolf 901b32d7a39SMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv); 902797ac58cSKevin Wolf 903797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = { 904797ac58cSKevin Wolf .name = "readv", 905797ac58cSKevin Wolf .cfunc = readv_f, 906797ac58cSKevin Wolf .argmin = 2, 907797ac58cSKevin Wolf .argmax = -1, 908797ac58cSKevin Wolf .args = "[-Cqv] [-P pattern] off len [len..]", 909797ac58cSKevin Wolf .oneline = "reads a number of bytes at a specified offset", 910797ac58cSKevin Wolf .help = readv_help, 911797ac58cSKevin Wolf }; 912797ac58cSKevin Wolf 913b32d7a39SMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv) 914797ac58cSKevin Wolf { 91550290c00SAlex Bennée struct timespec t1, t2; 916dc38852aSEric Blake bool Cflag = false, qflag = false, vflag = false; 917b32d7a39SMax Reitz int c, cnt, ret; 918797ac58cSKevin Wolf char *buf; 919797ac58cSKevin Wolf int64_t offset; 920797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 921797ac58cSKevin Wolf int total = 0; 922797ac58cSKevin Wolf int nr_iov; 923797ac58cSKevin Wolf QEMUIOVector qiov; 924797ac58cSKevin Wolf int pattern = 0; 925dc38852aSEric Blake bool Pflag = false; 926797ac58cSKevin Wolf 927b062ad86SEric Blake while ((c = getopt(argc, argv, "CP:qv")) != -1) { 928797ac58cSKevin Wolf switch (c) { 929797ac58cSKevin Wolf case 'C': 930dc38852aSEric Blake Cflag = true; 931797ac58cSKevin Wolf break; 932797ac58cSKevin Wolf case 'P': 933dc38852aSEric Blake Pflag = true; 934797ac58cSKevin Wolf pattern = parse_pattern(optarg); 935797ac58cSKevin Wolf if (pattern < 0) { 936b32d7a39SMax Reitz return -EINVAL; 937797ac58cSKevin Wolf } 938797ac58cSKevin Wolf break; 939797ac58cSKevin Wolf case 'q': 940dc38852aSEric Blake qflag = true; 941797ac58cSKevin Wolf break; 942797ac58cSKevin Wolf case 'v': 943dc38852aSEric Blake vflag = true; 944797ac58cSKevin Wolf break; 945797ac58cSKevin Wolf default: 946b444d0e9SMax Reitz qemuio_command_usage(&readv_cmd); 947b32d7a39SMax Reitz return -EINVAL; 948797ac58cSKevin Wolf } 949797ac58cSKevin Wolf } 950797ac58cSKevin Wolf 951797ac58cSKevin Wolf if (optind > argc - 2) { 952b444d0e9SMax Reitz qemuio_command_usage(&readv_cmd); 953b32d7a39SMax Reitz return -EINVAL; 954797ac58cSKevin Wolf } 955797ac58cSKevin Wolf 956797ac58cSKevin Wolf 957797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 958797ac58cSKevin Wolf if (offset < 0) { 959a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 960b32d7a39SMax Reitz return offset; 961797ac58cSKevin Wolf } 962797ac58cSKevin Wolf optind++; 963797ac58cSKevin Wolf 964797ac58cSKevin Wolf nr_iov = argc - optind; 9654c7b7e9bSMax Reitz buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab); 966797ac58cSKevin Wolf if (buf == NULL) { 967b32d7a39SMax Reitz return -EINVAL; 968797ac58cSKevin Wolf } 969797ac58cSKevin Wolf 97050290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 971b32d7a39SMax Reitz ret = do_aio_readv(blk, &qiov, offset, &total); 97250290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 973797ac58cSKevin Wolf 974b32d7a39SMax Reitz if (ret < 0) { 975b32d7a39SMax Reitz printf("readv failed: %s\n", strerror(-ret)); 976797ac58cSKevin Wolf goto out; 977797ac58cSKevin Wolf } 978b32d7a39SMax Reitz cnt = ret; 979b32d7a39SMax Reitz 980b32d7a39SMax Reitz ret = 0; 981797ac58cSKevin Wolf 982797ac58cSKevin Wolf if (Pflag) { 983797ac58cSKevin Wolf void *cmp_buf = g_malloc(qiov.size); 984797ac58cSKevin Wolf memset(cmp_buf, pattern, qiov.size); 985797ac58cSKevin Wolf if (memcmp(buf, cmp_buf, qiov.size)) { 986797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 987cf67b692SStefan Weil PRId64 ", %zu bytes\n", offset, qiov.size); 988b32d7a39SMax Reitz ret = -EINVAL; 989797ac58cSKevin Wolf } 990797ac58cSKevin Wolf g_free(cmp_buf); 991797ac58cSKevin Wolf } 992797ac58cSKevin Wolf 993797ac58cSKevin Wolf if (qflag) { 994797ac58cSKevin Wolf goto out; 995797ac58cSKevin Wolf } 996797ac58cSKevin Wolf 997797ac58cSKevin Wolf if (vflag) { 998797ac58cSKevin Wolf dump_buffer(buf, offset, qiov.size); 999797ac58cSKevin Wolf } 1000797ac58cSKevin Wolf 1001797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1002797ac58cSKevin Wolf t2 = tsub(t2, t1); 1003797ac58cSKevin Wolf print_report("read", &t2, offset, qiov.size, total, cnt, Cflag); 1004797ac58cSKevin Wolf 1005797ac58cSKevin Wolf out: 1006797ac58cSKevin Wolf qemu_iovec_destroy(&qiov); 1007797ac58cSKevin Wolf qemu_io_free(buf); 1008b32d7a39SMax Reitz return ret; 1009797ac58cSKevin Wolf } 1010797ac58cSKevin Wolf 1011797ac58cSKevin Wolf static void write_help(void) 1012797ac58cSKevin Wolf { 1013797ac58cSKevin Wolf printf( 1014797ac58cSKevin Wolf "\n" 1015797ac58cSKevin Wolf " writes a range of bytes from the given offset\n" 1016797ac58cSKevin Wolf "\n" 1017797ac58cSKevin Wolf " Example:\n" 1018797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n" 1019797ac58cSKevin Wolf "\n" 1020797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1021797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1022797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n" 10234c7b7e9bSMax Reitz " -c, -- write compressed data with blk_write_compressed\n" 1024770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n" 1025c6e3f520SKevin Wolf " -n, -- with -z, don't allow slow fallback\n" 1026093ea232SEric Blake " -p, -- ignored for backwards compatibility\n" 1027797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 10284d731510SDenis Plotnikov " -s, -- use a pattern file to fill the write buffer\n" 1029797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1030797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1031c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n" 1032d004bd52SEric Blake " -z, -- write zeroes using blk_co_pwrite_zeroes\n" 1033797ac58cSKevin Wolf "\n"); 1034797ac58cSKevin Wolf } 1035797ac58cSKevin Wolf 1036b32d7a39SMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv); 1037797ac58cSKevin Wolf 1038797ac58cSKevin Wolf static const cmdinfo_t write_cmd = { 1039797ac58cSKevin Wolf .name = "write", 1040797ac58cSKevin Wolf .altname = "w", 1041797ac58cSKevin Wolf .cfunc = write_f, 1042887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1043797ac58cSKevin Wolf .argmin = 2, 1044797ac58cSKevin Wolf .argmax = -1, 10454d731510SDenis Plotnikov .args = "[-bcCfnquz] [-P pattern | -s source_file] off len", 1046797ac58cSKevin Wolf .oneline = "writes a number of bytes at a specified offset", 1047797ac58cSKevin Wolf .help = write_help, 1048797ac58cSKevin Wolf }; 1049797ac58cSKevin Wolf 1050b32d7a39SMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv) 1051797ac58cSKevin Wolf { 105250290c00SAlex Bennée struct timespec t1, t2; 1053093ea232SEric Blake bool Cflag = false, qflag = false, bflag = false; 10544d731510SDenis Plotnikov bool Pflag = false, zflag = false, cflag = false, sflag = false; 1055770e0e0eSEric Blake int flags = 0; 1056b32d7a39SMax Reitz int c, cnt, ret; 1057797ac58cSKevin Wolf char *buf = NULL; 1058797ac58cSKevin Wolf int64_t offset; 10599b0beaf3SJohn Snow int64_t count; 1060797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 10619b0beaf3SJohn Snow int64_t total = 0; 1062797ac58cSKevin Wolf int pattern = 0xcd; 10634d731510SDenis Plotnikov const char *file_name = NULL; 1064797ac58cSKevin Wolf 10654d731510SDenis Plotnikov while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) { 1066797ac58cSKevin Wolf switch (c) { 1067797ac58cSKevin Wolf case 'b': 1068dc38852aSEric Blake bflag = true; 1069797ac58cSKevin Wolf break; 1070797ac58cSKevin Wolf case 'c': 1071dc38852aSEric Blake cflag = true; 1072797ac58cSKevin Wolf break; 1073797ac58cSKevin Wolf case 'C': 1074dc38852aSEric Blake Cflag = true; 1075797ac58cSKevin Wolf break; 1076770e0e0eSEric Blake case 'f': 1077770e0e0eSEric Blake flags |= BDRV_REQ_FUA; 1078770e0e0eSEric Blake break; 1079c6e3f520SKevin Wolf case 'n': 1080c6e3f520SKevin Wolf flags |= BDRV_REQ_NO_FALLBACK; 1081c6e3f520SKevin Wolf break; 1082797ac58cSKevin Wolf case 'p': 1083093ea232SEric Blake /* Ignored for backwards compatibility */ 1084797ac58cSKevin Wolf break; 1085797ac58cSKevin Wolf case 'P': 1086dc38852aSEric Blake Pflag = true; 1087797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1088797ac58cSKevin Wolf if (pattern < 0) { 1089b32d7a39SMax Reitz return -EINVAL; 1090797ac58cSKevin Wolf } 1091797ac58cSKevin Wolf break; 1092797ac58cSKevin Wolf case 'q': 1093dc38852aSEric Blake qflag = true; 1094797ac58cSKevin Wolf break; 10954d731510SDenis Plotnikov case 's': 10964d731510SDenis Plotnikov sflag = true; 10974d731510SDenis Plotnikov file_name = optarg; 10984d731510SDenis Plotnikov break; 1099c2e001ccSEric Blake case 'u': 1100c2e001ccSEric Blake flags |= BDRV_REQ_MAY_UNMAP; 1101c2e001ccSEric Blake break; 1102797ac58cSKevin Wolf case 'z': 1103dc38852aSEric Blake zflag = true; 1104797ac58cSKevin Wolf break; 1105797ac58cSKevin Wolf default: 1106b444d0e9SMax Reitz qemuio_command_usage(&write_cmd); 1107b32d7a39SMax Reitz return -EINVAL; 1108797ac58cSKevin Wolf } 1109797ac58cSKevin Wolf } 1110797ac58cSKevin Wolf 1111797ac58cSKevin Wolf if (optind != argc - 2) { 1112b444d0e9SMax Reitz qemuio_command_usage(&write_cmd); 1113b32d7a39SMax Reitz return -EINVAL; 1114797ac58cSKevin Wolf } 1115797ac58cSKevin Wolf 1116093ea232SEric Blake if (bflag && zflag) { 1117093ea232SEric Blake printf("-b and -z cannot be specified at the same time\n"); 1118b32d7a39SMax Reitz return -EINVAL; 1119797ac58cSKevin Wolf } 1120797ac58cSKevin Wolf 1121770e0e0eSEric Blake if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) { 1122770e0e0eSEric Blake printf("-f and -b or -c cannot be specified at the same time\n"); 1123b32d7a39SMax Reitz return -EINVAL; 1124770e0e0eSEric Blake } 1125770e0e0eSEric Blake 1126c6e3f520SKevin Wolf if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) { 1127c6e3f520SKevin Wolf printf("-n requires -z to be specified\n"); 1128c6e3f520SKevin Wolf return -EINVAL; 1129c6e3f520SKevin Wolf } 1130c6e3f520SKevin Wolf 1131c2e001ccSEric Blake if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) { 1132c2e001ccSEric Blake printf("-u requires -z to be specified\n"); 1133b32d7a39SMax Reitz return -EINVAL; 1134c2e001ccSEric Blake } 1135c2e001ccSEric Blake 11364d731510SDenis Plotnikov if (zflag + Pflag + sflag > 1) { 11374d731510SDenis Plotnikov printf("Only one of -z, -P, and -s " 11384d731510SDenis Plotnikov "can be specified at the same time\n"); 1139b32d7a39SMax Reitz return -EINVAL; 1140797ac58cSKevin Wolf } 1141797ac58cSKevin Wolf 1142797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1143797ac58cSKevin Wolf if (offset < 0) { 1144a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 1145b32d7a39SMax Reitz return offset; 1146797ac58cSKevin Wolf } 1147797ac58cSKevin Wolf 1148797ac58cSKevin Wolf optind++; 1149797ac58cSKevin Wolf count = cvtnum(argv[optind]); 1150797ac58cSKevin Wolf if (count < 0) { 1151a9ecfa00SJohn Snow print_cvtnum_err(count, argv[optind]); 1152b32d7a39SMax Reitz return count; 11533026c468SAlberto Garcia } else if (count > BDRV_REQUEST_MAX_BYTES) { 11549b0beaf3SJohn Snow printf("length cannot exceed %" PRIu64 ", given %s\n", 11553026c468SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]); 1156b32d7a39SMax Reitz return -EINVAL; 1157797ac58cSKevin Wolf } 1158797ac58cSKevin Wolf 1159093ea232SEric Blake if (bflag || cflag) { 11601bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) { 11611bce6b4cSEric Blake printf("%" PRId64 " is not a sector-aligned value for 'offset'\n", 1162797ac58cSKevin Wolf offset); 1163b32d7a39SMax Reitz return -EINVAL; 1164797ac58cSKevin Wolf } 1165797ac58cSKevin Wolf 11661bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) { 11671bce6b4cSEric Blake printf("%"PRId64" is not a sector-aligned value for 'count'\n", 1168797ac58cSKevin Wolf count); 1169b32d7a39SMax Reitz return -EINVAL; 1170797ac58cSKevin Wolf } 1171797ac58cSKevin Wolf } 1172797ac58cSKevin Wolf 1173797ac58cSKevin Wolf if (!zflag) { 11744d731510SDenis Plotnikov if (sflag) { 11754d731510SDenis Plotnikov buf = qemu_io_alloc_from_file(blk, count, file_name); 11764d731510SDenis Plotnikov if (!buf) { 11774d731510SDenis Plotnikov return -EINVAL; 11784d731510SDenis Plotnikov } 11794d731510SDenis Plotnikov } else { 11804c7b7e9bSMax Reitz buf = qemu_io_alloc(blk, count, pattern); 1181797ac58cSKevin Wolf } 11824d731510SDenis Plotnikov } 1183797ac58cSKevin Wolf 118450290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 11857b3f9712SEric Blake if (bflag) { 1186b32d7a39SMax Reitz ret = do_save_vmstate(blk, buf, offset, count, &total); 1187797ac58cSKevin Wolf } else if (zflag) { 1188b32d7a39SMax Reitz ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total); 1189797ac58cSKevin Wolf } else if (cflag) { 1190b32d7a39SMax Reitz ret = do_write_compressed(blk, buf, offset, count, &total); 1191797ac58cSKevin Wolf } else { 1192b32d7a39SMax Reitz ret = do_pwrite(blk, buf, offset, count, flags, &total); 1193797ac58cSKevin Wolf } 119450290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1195797ac58cSKevin Wolf 1196b32d7a39SMax Reitz if (ret < 0) { 1197b32d7a39SMax Reitz printf("write failed: %s\n", strerror(-ret)); 1198797ac58cSKevin Wolf goto out; 1199797ac58cSKevin Wolf } 1200b32d7a39SMax Reitz cnt = ret; 1201b32d7a39SMax Reitz 1202b32d7a39SMax Reitz ret = 0; 1203797ac58cSKevin Wolf 1204797ac58cSKevin Wolf if (qflag) { 1205797ac58cSKevin Wolf goto out; 1206797ac58cSKevin Wolf } 1207797ac58cSKevin Wolf 1208797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1209797ac58cSKevin Wolf t2 = tsub(t2, t1); 1210797ac58cSKevin Wolf print_report("wrote", &t2, offset, count, total, cnt, Cflag); 1211797ac58cSKevin Wolf 1212797ac58cSKevin Wolf out: 1213797ac58cSKevin Wolf if (!zflag) { 1214797ac58cSKevin Wolf qemu_io_free(buf); 1215797ac58cSKevin Wolf } 1216b32d7a39SMax Reitz return ret; 1217797ac58cSKevin Wolf } 1218797ac58cSKevin Wolf 1219797ac58cSKevin Wolf static void 1220797ac58cSKevin Wolf writev_help(void) 1221797ac58cSKevin Wolf { 1222797ac58cSKevin Wolf printf( 1223797ac58cSKevin Wolf "\n" 1224797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n" 1225797ac58cSKevin Wolf "\n" 1226797ac58cSKevin Wolf " Example:\n" 12276e6507c0SMaria Kustova " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" 1228797ac58cSKevin Wolf "\n" 1229797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1230797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1231797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 1232797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1233770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n" 1234797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1235797ac58cSKevin Wolf "\n"); 1236797ac58cSKevin Wolf } 1237797ac58cSKevin Wolf 1238b32d7a39SMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv); 1239797ac58cSKevin Wolf 1240797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = { 1241797ac58cSKevin Wolf .name = "writev", 1242797ac58cSKevin Wolf .cfunc = writev_f, 1243887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1244797ac58cSKevin Wolf .argmin = 2, 1245797ac58cSKevin Wolf .argmax = -1, 1246770e0e0eSEric Blake .args = "[-Cfq] [-P pattern] off len [len..]", 1247797ac58cSKevin Wolf .oneline = "writes a number of bytes at a specified offset", 1248797ac58cSKevin Wolf .help = writev_help, 1249797ac58cSKevin Wolf }; 1250797ac58cSKevin Wolf 1251b32d7a39SMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv) 1252797ac58cSKevin Wolf { 125350290c00SAlex Bennée struct timespec t1, t2; 1254dc38852aSEric Blake bool Cflag = false, qflag = false; 1255770e0e0eSEric Blake int flags = 0; 1256b32d7a39SMax Reitz int c, cnt, ret; 1257797ac58cSKevin Wolf char *buf; 1258797ac58cSKevin Wolf int64_t offset; 1259797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 1260797ac58cSKevin Wolf int total = 0; 1261797ac58cSKevin Wolf int nr_iov; 1262797ac58cSKevin Wolf int pattern = 0xcd; 1263797ac58cSKevin Wolf QEMUIOVector qiov; 1264797ac58cSKevin Wolf 12654ca1d340SEric Blake while ((c = getopt(argc, argv, "CfqP:")) != -1) { 1266797ac58cSKevin Wolf switch (c) { 1267797ac58cSKevin Wolf case 'C': 1268dc38852aSEric Blake Cflag = true; 1269797ac58cSKevin Wolf break; 1270770e0e0eSEric Blake case 'f': 1271770e0e0eSEric Blake flags |= BDRV_REQ_FUA; 1272770e0e0eSEric Blake break; 1273797ac58cSKevin Wolf case 'q': 1274dc38852aSEric Blake qflag = true; 1275797ac58cSKevin Wolf break; 1276797ac58cSKevin Wolf case 'P': 1277797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1278797ac58cSKevin Wolf if (pattern < 0) { 1279b32d7a39SMax Reitz return -EINVAL; 1280797ac58cSKevin Wolf } 1281797ac58cSKevin Wolf break; 1282797ac58cSKevin Wolf default: 1283b444d0e9SMax Reitz qemuio_command_usage(&writev_cmd); 1284b32d7a39SMax Reitz return -EINVAL; 1285797ac58cSKevin Wolf } 1286797ac58cSKevin Wolf } 1287797ac58cSKevin Wolf 1288797ac58cSKevin Wolf if (optind > argc - 2) { 1289b444d0e9SMax Reitz qemuio_command_usage(&writev_cmd); 1290b32d7a39SMax Reitz return -EINVAL; 1291797ac58cSKevin Wolf } 1292797ac58cSKevin Wolf 1293797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1294797ac58cSKevin Wolf if (offset < 0) { 1295a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 1296b32d7a39SMax Reitz return offset; 1297797ac58cSKevin Wolf } 1298797ac58cSKevin Wolf optind++; 1299797ac58cSKevin Wolf 1300797ac58cSKevin Wolf nr_iov = argc - optind; 13014c7b7e9bSMax Reitz buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern); 1302797ac58cSKevin Wolf if (buf == NULL) { 1303b32d7a39SMax Reitz return -EINVAL; 1304797ac58cSKevin Wolf } 1305797ac58cSKevin Wolf 130650290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 1307b32d7a39SMax Reitz ret = do_aio_writev(blk, &qiov, offset, flags, &total); 130850290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1309797ac58cSKevin Wolf 1310b32d7a39SMax Reitz if (ret < 0) { 1311b32d7a39SMax Reitz printf("writev failed: %s\n", strerror(-ret)); 1312797ac58cSKevin Wolf goto out; 1313797ac58cSKevin Wolf } 1314b32d7a39SMax Reitz cnt = ret; 1315b32d7a39SMax Reitz 1316b32d7a39SMax Reitz ret = 0; 1317797ac58cSKevin Wolf 1318797ac58cSKevin Wolf if (qflag) { 1319797ac58cSKevin Wolf goto out; 1320797ac58cSKevin Wolf } 1321797ac58cSKevin Wolf 1322797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1323797ac58cSKevin Wolf t2 = tsub(t2, t1); 1324797ac58cSKevin Wolf print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag); 1325797ac58cSKevin Wolf out: 1326797ac58cSKevin Wolf qemu_iovec_destroy(&qiov); 1327797ac58cSKevin Wolf qemu_io_free(buf); 1328b32d7a39SMax Reitz return ret; 1329797ac58cSKevin Wolf } 1330797ac58cSKevin Wolf 1331797ac58cSKevin Wolf struct aio_ctx { 13324c7b7e9bSMax Reitz BlockBackend *blk; 1333797ac58cSKevin Wolf QEMUIOVector qiov; 1334797ac58cSKevin Wolf int64_t offset; 1335797ac58cSKevin Wolf char *buf; 1336dc38852aSEric Blake bool qflag; 1337dc38852aSEric Blake bool vflag; 1338dc38852aSEric Blake bool Cflag; 1339dc38852aSEric Blake bool Pflag; 1340dc38852aSEric Blake bool zflag; 1341a91f9584SFam Zheng BlockAcctCookie acct; 1342797ac58cSKevin Wolf int pattern; 134350290c00SAlex Bennée struct timespec t1; 1344797ac58cSKevin Wolf }; 1345797ac58cSKevin Wolf 1346797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret) 1347797ac58cSKevin Wolf { 1348797ac58cSKevin Wolf struct aio_ctx *ctx = opaque; 134950290c00SAlex Bennée struct timespec t2; 1350797ac58cSKevin Wolf 135150290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1352797ac58cSKevin Wolf 1353797ac58cSKevin Wolf 1354797ac58cSKevin Wolf if (ret < 0) { 1355797ac58cSKevin Wolf printf("aio_write failed: %s\n", strerror(-ret)); 1356556c2b60SAlberto Garcia block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct); 1357797ac58cSKevin Wolf goto out; 1358797ac58cSKevin Wolf } 1359797ac58cSKevin Wolf 13604c7b7e9bSMax Reitz block_acct_done(blk_get_stats(ctx->blk), &ctx->acct); 1361a91f9584SFam Zheng 1362797ac58cSKevin Wolf if (ctx->qflag) { 1363797ac58cSKevin Wolf goto out; 1364797ac58cSKevin Wolf } 1365797ac58cSKevin Wolf 1366797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1367797ac58cSKevin Wolf t2 = tsub(t2, ctx->t1); 1368797ac58cSKevin Wolf print_report("wrote", &t2, ctx->offset, ctx->qiov.size, 1369797ac58cSKevin Wolf ctx->qiov.size, 1, ctx->Cflag); 1370797ac58cSKevin Wolf out: 13715ceb7765SKevin Wolf if (!ctx->zflag) { 1372797ac58cSKevin Wolf qemu_io_free(ctx->buf); 1373797ac58cSKevin Wolf qemu_iovec_destroy(&ctx->qiov); 13745ceb7765SKevin Wolf } 1375797ac58cSKevin Wolf g_free(ctx); 1376797ac58cSKevin Wolf } 1377797ac58cSKevin Wolf 1378797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret) 1379797ac58cSKevin Wolf { 1380797ac58cSKevin Wolf struct aio_ctx *ctx = opaque; 138150290c00SAlex Bennée struct timespec t2; 1382797ac58cSKevin Wolf 138350290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1384797ac58cSKevin Wolf 1385797ac58cSKevin Wolf if (ret < 0) { 1386797ac58cSKevin Wolf printf("readv failed: %s\n", strerror(-ret)); 1387556c2b60SAlberto Garcia block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct); 1388797ac58cSKevin Wolf goto out; 1389797ac58cSKevin Wolf } 1390797ac58cSKevin Wolf 1391797ac58cSKevin Wolf if (ctx->Pflag) { 1392797ac58cSKevin Wolf void *cmp_buf = g_malloc(ctx->qiov.size); 1393797ac58cSKevin Wolf 1394797ac58cSKevin Wolf memset(cmp_buf, ctx->pattern, ctx->qiov.size); 1395797ac58cSKevin Wolf if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) { 1396797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 1397cf67b692SStefan Weil PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size); 1398797ac58cSKevin Wolf } 1399797ac58cSKevin Wolf g_free(cmp_buf); 1400797ac58cSKevin Wolf } 1401797ac58cSKevin Wolf 14024c7b7e9bSMax Reitz block_acct_done(blk_get_stats(ctx->blk), &ctx->acct); 1403a91f9584SFam Zheng 1404797ac58cSKevin Wolf if (ctx->qflag) { 1405797ac58cSKevin Wolf goto out; 1406797ac58cSKevin Wolf } 1407797ac58cSKevin Wolf 1408797ac58cSKevin Wolf if (ctx->vflag) { 1409797ac58cSKevin Wolf dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); 1410797ac58cSKevin Wolf } 1411797ac58cSKevin Wolf 1412797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1413797ac58cSKevin Wolf t2 = tsub(t2, ctx->t1); 1414797ac58cSKevin Wolf print_report("read", &t2, ctx->offset, ctx->qiov.size, 1415797ac58cSKevin Wolf ctx->qiov.size, 1, ctx->Cflag); 1416797ac58cSKevin Wolf out: 1417797ac58cSKevin Wolf qemu_io_free(ctx->buf); 1418797ac58cSKevin Wolf qemu_iovec_destroy(&ctx->qiov); 1419797ac58cSKevin Wolf g_free(ctx); 1420797ac58cSKevin Wolf } 1421797ac58cSKevin Wolf 1422797ac58cSKevin Wolf static void aio_read_help(void) 1423797ac58cSKevin Wolf { 1424797ac58cSKevin Wolf printf( 1425797ac58cSKevin Wolf "\n" 1426797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n" 1427797ac58cSKevin Wolf "\n" 1428797ac58cSKevin Wolf " Example:\n" 1429797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" 1430797ac58cSKevin Wolf "\n" 1431797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 1432797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 1433797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n" 1434797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n" 1435b32d7a39SMax Reitz " Note that due to its asynchronous nature, this command will be\n" 1436b32d7a39SMax Reitz " considered successful once the request is submitted, independently\n" 1437b32d7a39SMax Reitz " of potential I/O errors or pattern mismatches.\n" 1438797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1439797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 144037546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n" 1441797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 1442797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1443797ac58cSKevin Wolf "\n"); 1444797ac58cSKevin Wolf } 1445797ac58cSKevin Wolf 1446b32d7a39SMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv); 1447797ac58cSKevin Wolf 1448797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = { 1449797ac58cSKevin Wolf .name = "aio_read", 1450797ac58cSKevin Wolf .cfunc = aio_read_f, 1451797ac58cSKevin Wolf .argmin = 2, 1452797ac58cSKevin Wolf .argmax = -1, 145337546ff2SEric Blake .args = "[-Ciqv] [-P pattern] off len [len..]", 1454797ac58cSKevin Wolf .oneline = "asynchronously reads a number of bytes", 1455797ac58cSKevin Wolf .help = aio_read_help, 1456797ac58cSKevin Wolf }; 1457797ac58cSKevin Wolf 1458b32d7a39SMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv) 1459797ac58cSKevin Wolf { 1460797ac58cSKevin Wolf int nr_iov, c; 1461797ac58cSKevin Wolf struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); 1462797ac58cSKevin Wolf 14634c7b7e9bSMax Reitz ctx->blk = blk; 146437546ff2SEric Blake while ((c = getopt(argc, argv, "CP:iqv")) != -1) { 1465797ac58cSKevin Wolf switch (c) { 1466797ac58cSKevin Wolf case 'C': 1467dc38852aSEric Blake ctx->Cflag = true; 1468797ac58cSKevin Wolf break; 1469797ac58cSKevin Wolf case 'P': 1470dc38852aSEric Blake ctx->Pflag = true; 1471797ac58cSKevin Wolf ctx->pattern = parse_pattern(optarg); 1472797ac58cSKevin Wolf if (ctx->pattern < 0) { 1473797ac58cSKevin Wolf g_free(ctx); 1474b32d7a39SMax Reitz return -EINVAL; 1475797ac58cSKevin Wolf } 1476797ac58cSKevin Wolf break; 147737546ff2SEric Blake case 'i': 147837546ff2SEric Blake printf("injecting invalid read request\n"); 147937546ff2SEric Blake block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ); 148037546ff2SEric Blake g_free(ctx); 1481b32d7a39SMax Reitz return 0; 1482797ac58cSKevin Wolf case 'q': 1483dc38852aSEric Blake ctx->qflag = true; 1484797ac58cSKevin Wolf break; 1485797ac58cSKevin Wolf case 'v': 1486dc38852aSEric Blake ctx->vflag = true; 1487797ac58cSKevin Wolf break; 1488797ac58cSKevin Wolf default: 1489797ac58cSKevin Wolf g_free(ctx); 1490b444d0e9SMax Reitz qemuio_command_usage(&aio_read_cmd); 1491b32d7a39SMax Reitz return -EINVAL; 1492797ac58cSKevin Wolf } 1493797ac58cSKevin Wolf } 1494797ac58cSKevin Wolf 1495797ac58cSKevin Wolf if (optind > argc - 2) { 1496797ac58cSKevin Wolf g_free(ctx); 1497b444d0e9SMax Reitz qemuio_command_usage(&aio_read_cmd); 1498b32d7a39SMax Reitz return -EINVAL; 1499797ac58cSKevin Wolf } 1500797ac58cSKevin Wolf 1501797ac58cSKevin Wolf ctx->offset = cvtnum(argv[optind]); 1502797ac58cSKevin Wolf if (ctx->offset < 0) { 1503b32d7a39SMax Reitz int ret = ctx->offset; 1504b32d7a39SMax Reitz print_cvtnum_err(ret, argv[optind]); 1505797ac58cSKevin Wolf g_free(ctx); 1506b32d7a39SMax Reitz return ret; 1507797ac58cSKevin Wolf } 1508797ac58cSKevin Wolf optind++; 1509797ac58cSKevin Wolf 1510797ac58cSKevin Wolf nr_iov = argc - optind; 15114c7b7e9bSMax Reitz ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab); 1512797ac58cSKevin Wolf if (ctx->buf == NULL) { 1513556c2b60SAlberto Garcia block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ); 1514797ac58cSKevin Wolf g_free(ctx); 1515b32d7a39SMax Reitz return -EINVAL; 1516797ac58cSKevin Wolf } 1517797ac58cSKevin Wolf 151850290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &ctx->t1); 15194c7b7e9bSMax Reitz block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size, 15204c7b7e9bSMax Reitz BLOCK_ACCT_READ); 15217b3f9712SEric Blake blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx); 1522b32d7a39SMax Reitz return 0; 1523797ac58cSKevin Wolf } 1524797ac58cSKevin Wolf 1525797ac58cSKevin Wolf static void aio_write_help(void) 1526797ac58cSKevin Wolf { 1527797ac58cSKevin Wolf printf( 1528797ac58cSKevin Wolf "\n" 1529797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n" 1530797ac58cSKevin Wolf " from multiple buffers\n" 1531797ac58cSKevin Wolf "\n" 1532797ac58cSKevin Wolf " Example:\n" 1533797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" 1534797ac58cSKevin Wolf "\n" 1535797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1536797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1537797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n" 1538797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n" 1539b32d7a39SMax Reitz " Note that due to its asynchronous nature, this command will be\n" 1540b32d7a39SMax Reitz " considered successful once the request is submitted, independently\n" 1541b32d7a39SMax Reitz " of potential I/O errors or pattern mismatches.\n" 1542797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 1543797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1544770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n" 154537546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n" 1546797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1547c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n" 1548d004bd52SEric Blake " -z, -- write zeroes using blk_aio_pwrite_zeroes\n" 1549797ac58cSKevin Wolf "\n"); 1550797ac58cSKevin Wolf } 1551797ac58cSKevin Wolf 1552b32d7a39SMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv); 1553797ac58cSKevin Wolf 1554797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = { 1555797ac58cSKevin Wolf .name = "aio_write", 1556797ac58cSKevin Wolf .cfunc = aio_write_f, 1557887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1558797ac58cSKevin Wolf .argmin = 2, 1559797ac58cSKevin Wolf .argmax = -1, 156037546ff2SEric Blake .args = "[-Cfiquz] [-P pattern] off len [len..]", 1561797ac58cSKevin Wolf .oneline = "asynchronously writes a number of bytes", 1562797ac58cSKevin Wolf .help = aio_write_help, 1563797ac58cSKevin Wolf }; 1564797ac58cSKevin Wolf 1565b32d7a39SMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv) 1566797ac58cSKevin Wolf { 1567797ac58cSKevin Wolf int nr_iov, c; 1568797ac58cSKevin Wolf int pattern = 0xcd; 1569797ac58cSKevin Wolf struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); 1570770e0e0eSEric Blake int flags = 0; 1571797ac58cSKevin Wolf 15724c7b7e9bSMax Reitz ctx->blk = blk; 157337546ff2SEric Blake while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) { 1574797ac58cSKevin Wolf switch (c) { 1575797ac58cSKevin Wolf case 'C': 1576dc38852aSEric Blake ctx->Cflag = true; 1577797ac58cSKevin Wolf break; 1578770e0e0eSEric Blake case 'f': 1579770e0e0eSEric Blake flags |= BDRV_REQ_FUA; 1580770e0e0eSEric Blake break; 1581797ac58cSKevin Wolf case 'q': 1582dc38852aSEric Blake ctx->qflag = true; 1583797ac58cSKevin Wolf break; 1584c2e001ccSEric Blake case 'u': 1585c2e001ccSEric Blake flags |= BDRV_REQ_MAY_UNMAP; 1586c2e001ccSEric Blake break; 1587797ac58cSKevin Wolf case 'P': 1588797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1589797ac58cSKevin Wolf if (pattern < 0) { 1590797ac58cSKevin Wolf g_free(ctx); 1591b32d7a39SMax Reitz return -EINVAL; 1592797ac58cSKevin Wolf } 1593797ac58cSKevin Wolf break; 159437546ff2SEric Blake case 'i': 159537546ff2SEric Blake printf("injecting invalid write request\n"); 159637546ff2SEric Blake block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE); 159737546ff2SEric Blake g_free(ctx); 1598b32d7a39SMax Reitz return 0; 15995ceb7765SKevin Wolf case 'z': 1600dc38852aSEric Blake ctx->zflag = true; 16015ceb7765SKevin Wolf break; 1602797ac58cSKevin Wolf default: 1603797ac58cSKevin Wolf g_free(ctx); 1604b444d0e9SMax Reitz qemuio_command_usage(&aio_write_cmd); 1605b32d7a39SMax Reitz return -EINVAL; 1606797ac58cSKevin Wolf } 1607797ac58cSKevin Wolf } 1608797ac58cSKevin Wolf 1609797ac58cSKevin Wolf if (optind > argc - 2) { 1610797ac58cSKevin Wolf g_free(ctx); 1611b444d0e9SMax Reitz qemuio_command_usage(&aio_write_cmd); 1612b32d7a39SMax Reitz return -EINVAL; 1613797ac58cSKevin Wolf } 1614797ac58cSKevin Wolf 16155ceb7765SKevin Wolf if (ctx->zflag && optind != argc - 2) { 16165ceb7765SKevin Wolf printf("-z supports only a single length parameter\n"); 16175ceb7765SKevin Wolf g_free(ctx); 1618b32d7a39SMax Reitz return -EINVAL; 16195ceb7765SKevin Wolf } 16205ceb7765SKevin Wolf 1621c2e001ccSEric Blake if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) { 1622c2e001ccSEric Blake printf("-u requires -z to be specified\n"); 16234ca1d340SEric Blake g_free(ctx); 1624b32d7a39SMax Reitz return -EINVAL; 1625c2e001ccSEric Blake } 1626c2e001ccSEric Blake 16275ceb7765SKevin Wolf if (ctx->zflag && ctx->Pflag) { 16285ceb7765SKevin Wolf printf("-z and -P cannot be specified at the same time\n"); 16295ceb7765SKevin Wolf g_free(ctx); 1630b32d7a39SMax Reitz return -EINVAL; 16315ceb7765SKevin Wolf } 16325ceb7765SKevin Wolf 1633797ac58cSKevin Wolf ctx->offset = cvtnum(argv[optind]); 1634797ac58cSKevin Wolf if (ctx->offset < 0) { 1635b32d7a39SMax Reitz int ret = ctx->offset; 1636b32d7a39SMax Reitz print_cvtnum_err(ret, argv[optind]); 1637797ac58cSKevin Wolf g_free(ctx); 1638b32d7a39SMax Reitz return ret; 1639797ac58cSKevin Wolf } 1640797ac58cSKevin Wolf optind++; 1641797ac58cSKevin Wolf 16425ceb7765SKevin Wolf if (ctx->zflag) { 16435ceb7765SKevin Wolf int64_t count = cvtnum(argv[optind]); 16445ceb7765SKevin Wolf if (count < 0) { 16455ceb7765SKevin Wolf print_cvtnum_err(count, argv[optind]); 16460e01b76eSKevin Wolf g_free(ctx); 1647b32d7a39SMax Reitz return count; 16485ceb7765SKevin Wolf } 16495ceb7765SKevin Wolf 16505ceb7765SKevin Wolf ctx->qiov.size = count; 1651d004bd52SEric Blake blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done, 1652770e0e0eSEric Blake ctx); 16535ceb7765SKevin Wolf } else { 1654797ac58cSKevin Wolf nr_iov = argc - optind; 16555ceb7765SKevin Wolf ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 16565ceb7765SKevin Wolf pattern); 1657797ac58cSKevin Wolf if (ctx->buf == NULL) { 1658556c2b60SAlberto Garcia block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE); 1659797ac58cSKevin Wolf g_free(ctx); 1660b32d7a39SMax Reitz return -EINVAL; 1661797ac58cSKevin Wolf } 1662797ac58cSKevin Wolf 166350290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &ctx->t1); 16644c7b7e9bSMax Reitz block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size, 16654c7b7e9bSMax Reitz BLOCK_ACCT_WRITE); 16665ceb7765SKevin Wolf 1667770e0e0eSEric Blake blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done, 1668770e0e0eSEric Blake ctx); 16695ceb7765SKevin Wolf } 1670b32d7a39SMax Reitz 1671b32d7a39SMax Reitz return 0; 1672797ac58cSKevin Wolf } 1673797ac58cSKevin Wolf 1674b32d7a39SMax Reitz static int aio_flush_f(BlockBackend *blk, int argc, char **argv) 1675797ac58cSKevin Wolf { 1676556c2b60SAlberto Garcia BlockAcctCookie cookie; 1677556c2b60SAlberto Garcia block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH); 16784c7b7e9bSMax Reitz blk_drain_all(); 1679556c2b60SAlberto Garcia block_acct_done(blk_get_stats(blk), &cookie); 1680b32d7a39SMax Reitz return 0; 1681797ac58cSKevin Wolf } 1682797ac58cSKevin Wolf 1683797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = { 1684797ac58cSKevin Wolf .name = "aio_flush", 1685797ac58cSKevin Wolf .cfunc = aio_flush_f, 1686797ac58cSKevin Wolf .oneline = "completes all outstanding aio requests" 1687797ac58cSKevin Wolf }; 1688797ac58cSKevin Wolf 1689b32d7a39SMax Reitz static int flush_f(BlockBackend *blk, int argc, char **argv) 1690797ac58cSKevin Wolf { 1691b32d7a39SMax Reitz return blk_flush(blk); 1692797ac58cSKevin Wolf } 1693797ac58cSKevin Wolf 1694797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = { 1695797ac58cSKevin Wolf .name = "flush", 1696797ac58cSKevin Wolf .altname = "f", 1697797ac58cSKevin Wolf .cfunc = flush_f, 1698797ac58cSKevin Wolf .oneline = "flush all in-core file state to disk", 1699797ac58cSKevin Wolf }; 1700797ac58cSKevin Wolf 1701b32d7a39SMax Reitz static int truncate_f(BlockBackend *blk, int argc, char **argv) 1702797ac58cSKevin Wolf { 1703ed3d2ec9SMax Reitz Error *local_err = NULL; 1704797ac58cSKevin Wolf int64_t offset; 1705797ac58cSKevin Wolf int ret; 1706797ac58cSKevin Wolf 1707797ac58cSKevin Wolf offset = cvtnum(argv[1]); 1708797ac58cSKevin Wolf if (offset < 0) { 1709a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[1]); 1710b32d7a39SMax Reitz return offset; 1711797ac58cSKevin Wolf } 1712797ac58cSKevin Wolf 17133a691c50SMax Reitz ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err); 1714797ac58cSKevin Wolf if (ret < 0) { 1715ed3d2ec9SMax Reitz error_report_err(local_err); 1716b32d7a39SMax Reitz return ret; 1717797ac58cSKevin Wolf } 1718b32d7a39SMax Reitz 1719b32d7a39SMax Reitz return 0; 1720797ac58cSKevin Wolf } 1721797ac58cSKevin Wolf 1722797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = { 1723797ac58cSKevin Wolf .name = "truncate", 1724797ac58cSKevin Wolf .altname = "t", 1725797ac58cSKevin Wolf .cfunc = truncate_f, 1726887354bdSKevin Wolf .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE, 1727797ac58cSKevin Wolf .argmin = 1, 1728797ac58cSKevin Wolf .argmax = 1, 1729797ac58cSKevin Wolf .args = "off", 1730797ac58cSKevin Wolf .oneline = "truncates the current file at the given offset", 1731797ac58cSKevin Wolf }; 1732797ac58cSKevin Wolf 1733b32d7a39SMax Reitz static int length_f(BlockBackend *blk, int argc, char **argv) 1734797ac58cSKevin Wolf { 1735797ac58cSKevin Wolf int64_t size; 1736797ac58cSKevin Wolf char s1[64]; 1737797ac58cSKevin Wolf 17384c7b7e9bSMax Reitz size = blk_getlength(blk); 1739797ac58cSKevin Wolf if (size < 0) { 1740797ac58cSKevin Wolf printf("getlength: %s\n", strerror(-size)); 1741b32d7a39SMax Reitz return size; 1742797ac58cSKevin Wolf } 1743797ac58cSKevin Wolf 1744797ac58cSKevin Wolf cvtstr(size, s1, sizeof(s1)); 1745797ac58cSKevin Wolf printf("%s\n", s1); 1746b32d7a39SMax Reitz return 0; 1747797ac58cSKevin Wolf } 1748797ac58cSKevin Wolf 1749797ac58cSKevin Wolf 1750797ac58cSKevin Wolf static const cmdinfo_t length_cmd = { 1751797ac58cSKevin Wolf .name = "length", 1752797ac58cSKevin Wolf .altname = "l", 1753797ac58cSKevin Wolf .cfunc = length_f, 1754797ac58cSKevin Wolf .oneline = "gets the length of the current file", 1755797ac58cSKevin Wolf }; 1756797ac58cSKevin Wolf 1757797ac58cSKevin Wolf 1758b32d7a39SMax Reitz static int info_f(BlockBackend *blk, int argc, char **argv) 1759797ac58cSKevin Wolf { 17604c7b7e9bSMax Reitz BlockDriverState *bs = blk_bs(blk); 1761797ac58cSKevin Wolf BlockDriverInfo bdi; 1762a8d8ecb7SMax Reitz ImageInfoSpecific *spec_info; 17631bf6e9caSAndrey Shinkevich Error *local_err = NULL; 1764797ac58cSKevin Wolf char s1[64], s2[64]; 1765797ac58cSKevin Wolf int ret; 1766797ac58cSKevin Wolf 1767797ac58cSKevin Wolf if (bs->drv && bs->drv->format_name) { 1768797ac58cSKevin Wolf printf("format name: %s\n", bs->drv->format_name); 1769797ac58cSKevin Wolf } 1770797ac58cSKevin Wolf if (bs->drv && bs->drv->protocol_name) { 1771797ac58cSKevin Wolf printf("format name: %s\n", bs->drv->protocol_name); 1772797ac58cSKevin Wolf } 1773797ac58cSKevin Wolf 1774797ac58cSKevin Wolf ret = bdrv_get_info(bs, &bdi); 1775797ac58cSKevin Wolf if (ret) { 1776b32d7a39SMax Reitz return ret; 1777797ac58cSKevin Wolf } 1778797ac58cSKevin Wolf 1779797ac58cSKevin Wolf cvtstr(bdi.cluster_size, s1, sizeof(s1)); 1780797ac58cSKevin Wolf cvtstr(bdi.vm_state_offset, s2, sizeof(s2)); 1781797ac58cSKevin Wolf 1782797ac58cSKevin Wolf printf("cluster size: %s\n", s1); 1783797ac58cSKevin Wolf printf("vm state offset: %s\n", s2); 1784797ac58cSKevin Wolf 17851bf6e9caSAndrey Shinkevich spec_info = bdrv_get_specific_info(bs, &local_err); 17861bf6e9caSAndrey Shinkevich if (local_err) { 17871bf6e9caSAndrey Shinkevich error_report_err(local_err); 17881bf6e9caSAndrey Shinkevich return -EIO; 17891bf6e9caSAndrey Shinkevich } 1790a8d8ecb7SMax Reitz if (spec_info) { 1791a8d8ecb7SMax Reitz printf("Format specific information:\n"); 1792e1ce7d74SMarkus Armbruster bdrv_image_info_specific_dump(spec_info); 1793a8d8ecb7SMax Reitz qapi_free_ImageInfoSpecific(spec_info); 1794a8d8ecb7SMax Reitz } 1795b32d7a39SMax Reitz 1796b32d7a39SMax Reitz return 0; 1797797ac58cSKevin Wolf } 1798797ac58cSKevin Wolf 1799797ac58cSKevin Wolf 1800797ac58cSKevin Wolf 1801797ac58cSKevin Wolf static const cmdinfo_t info_cmd = { 1802797ac58cSKevin Wolf .name = "info", 1803797ac58cSKevin Wolf .altname = "i", 1804797ac58cSKevin Wolf .cfunc = info_f, 1805797ac58cSKevin Wolf .oneline = "prints information about the current file", 1806797ac58cSKevin Wolf }; 1807797ac58cSKevin Wolf 1808797ac58cSKevin Wolf static void discard_help(void) 1809797ac58cSKevin Wolf { 1810797ac58cSKevin Wolf printf( 1811797ac58cSKevin Wolf "\n" 1812797ac58cSKevin Wolf " discards a range of bytes from the given offset\n" 1813797ac58cSKevin Wolf "\n" 1814797ac58cSKevin Wolf " Example:\n" 1815797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n" 1816797ac58cSKevin Wolf "\n" 1817797ac58cSKevin Wolf " Discards a segment of the currently open file.\n" 1818797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1819797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1820797ac58cSKevin Wolf "\n"); 1821797ac58cSKevin Wolf } 1822797ac58cSKevin Wolf 1823b32d7a39SMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv); 1824797ac58cSKevin Wolf 1825797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = { 1826797ac58cSKevin Wolf .name = "discard", 1827797ac58cSKevin Wolf .altname = "d", 1828797ac58cSKevin Wolf .cfunc = discard_f, 1829887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1830797ac58cSKevin Wolf .argmin = 2, 1831797ac58cSKevin Wolf .argmax = -1, 1832797ac58cSKevin Wolf .args = "[-Cq] off len", 1833797ac58cSKevin Wolf .oneline = "discards a number of bytes at a specified offset", 1834797ac58cSKevin Wolf .help = discard_help, 1835797ac58cSKevin Wolf }; 1836797ac58cSKevin Wolf 1837b32d7a39SMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv) 1838797ac58cSKevin Wolf { 183950290c00SAlex Bennée struct timespec t1, t2; 1840dc38852aSEric Blake bool Cflag = false, qflag = false; 1841797ac58cSKevin Wolf int c, ret; 1842f5a5ca79SManos Pitsidianakis int64_t offset, bytes; 1843797ac58cSKevin Wolf 1844b062ad86SEric Blake while ((c = getopt(argc, argv, "Cq")) != -1) { 1845797ac58cSKevin Wolf switch (c) { 1846797ac58cSKevin Wolf case 'C': 1847dc38852aSEric Blake Cflag = true; 1848797ac58cSKevin Wolf break; 1849797ac58cSKevin Wolf case 'q': 1850dc38852aSEric Blake qflag = true; 1851797ac58cSKevin Wolf break; 1852797ac58cSKevin Wolf default: 1853b444d0e9SMax Reitz qemuio_command_usage(&discard_cmd); 1854b32d7a39SMax Reitz return -EINVAL; 1855797ac58cSKevin Wolf } 1856797ac58cSKevin Wolf } 1857797ac58cSKevin Wolf 1858797ac58cSKevin Wolf if (optind != argc - 2) { 1859b444d0e9SMax Reitz qemuio_command_usage(&discard_cmd); 1860b32d7a39SMax Reitz return -EINVAL; 1861797ac58cSKevin Wolf } 1862797ac58cSKevin Wolf 1863797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1864797ac58cSKevin Wolf if (offset < 0) { 1865a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 1866b32d7a39SMax Reitz return offset; 1867797ac58cSKevin Wolf } 1868797ac58cSKevin Wolf 1869797ac58cSKevin Wolf optind++; 1870f5a5ca79SManos Pitsidianakis bytes = cvtnum(argv[optind]); 1871f5a5ca79SManos Pitsidianakis if (bytes < 0) { 1872f5a5ca79SManos Pitsidianakis print_cvtnum_err(bytes, argv[optind]); 1873b32d7a39SMax Reitz return bytes; 187441ae31e3SAlberto Garcia } else if (bytes > BDRV_REQUEST_MAX_BYTES) { 18759b0beaf3SJohn Snow printf("length cannot exceed %"PRIu64", given %s\n", 187641ae31e3SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]); 1877b32d7a39SMax Reitz return -EINVAL; 1878797ac58cSKevin Wolf } 1879797ac58cSKevin Wolf 188050290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 1881f5a5ca79SManos Pitsidianakis ret = blk_pdiscard(blk, offset, bytes); 188250290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1883797ac58cSKevin Wolf 1884797ac58cSKevin Wolf if (ret < 0) { 1885797ac58cSKevin Wolf printf("discard failed: %s\n", strerror(-ret)); 1886b32d7a39SMax Reitz return ret; 1887797ac58cSKevin Wolf } 1888797ac58cSKevin Wolf 1889797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1890797ac58cSKevin Wolf if (!qflag) { 1891797ac58cSKevin Wolf t2 = tsub(t2, t1); 1892f5a5ca79SManos Pitsidianakis print_report("discard", &t2, offset, bytes, bytes, 1, Cflag); 1893797ac58cSKevin Wolf } 1894b32d7a39SMax Reitz 1895b32d7a39SMax Reitz return 0; 1896797ac58cSKevin Wolf } 1897797ac58cSKevin Wolf 1898b32d7a39SMax Reitz static int alloc_f(BlockBackend *blk, int argc, char **argv) 1899797ac58cSKevin Wolf { 19004c7b7e9bSMax Reitz BlockDriverState *bs = blk_bs(blk); 1901d6a644bbSEric Blake int64_t offset, start, remaining, count; 1902797ac58cSKevin Wolf char s1[64]; 1903d6a644bbSEric Blake int ret; 1904d6a644bbSEric Blake int64_t num, sum_alloc; 1905797ac58cSKevin Wolf 1906d6a644bbSEric Blake start = offset = cvtnum(argv[1]); 1907797ac58cSKevin Wolf if (offset < 0) { 1908a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[1]); 1909b32d7a39SMax Reitz return offset; 1910797ac58cSKevin Wolf } 1911797ac58cSKevin Wolf 1912797ac58cSKevin Wolf if (argc == 3) { 19134401fdc7SEric Blake count = cvtnum(argv[2]); 19144401fdc7SEric Blake if (count < 0) { 19154401fdc7SEric Blake print_cvtnum_err(count, argv[2]); 1916b32d7a39SMax Reitz return count; 1917797ac58cSKevin Wolf } 1918797ac58cSKevin Wolf } else { 19194401fdc7SEric Blake count = BDRV_SECTOR_SIZE; 1920797ac58cSKevin Wolf } 1921797ac58cSKevin Wolf 1922d6a644bbSEric Blake remaining = count; 1923797ac58cSKevin Wolf sum_alloc = 0; 1924797ac58cSKevin Wolf while (remaining) { 1925d6a644bbSEric Blake ret = bdrv_is_allocated(bs, offset, remaining, &num); 1926d663640cSPaolo Bonzini if (ret < 0) { 1927d663640cSPaolo Bonzini printf("is_allocated failed: %s\n", strerror(-ret)); 1928b32d7a39SMax Reitz return ret; 1929d663640cSPaolo Bonzini } 1930d6a644bbSEric Blake offset += num; 1931797ac58cSKevin Wolf remaining -= num; 1932797ac58cSKevin Wolf if (ret) { 1933797ac58cSKevin Wolf sum_alloc += num; 1934797ac58cSKevin Wolf } 1935797ac58cSKevin Wolf if (num == 0) { 1936d6a644bbSEric Blake count -= remaining; 1937797ac58cSKevin Wolf remaining = 0; 1938797ac58cSKevin Wolf } 1939797ac58cSKevin Wolf } 1940797ac58cSKevin Wolf 1941d6a644bbSEric Blake cvtstr(start, s1, sizeof(s1)); 1942797ac58cSKevin Wolf 19434401fdc7SEric Blake printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n", 1944d6a644bbSEric Blake sum_alloc, count, s1); 1945b32d7a39SMax Reitz return 0; 1946797ac58cSKevin Wolf } 1947797ac58cSKevin Wolf 1948797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = { 1949797ac58cSKevin Wolf .name = "alloc", 1950797ac58cSKevin Wolf .altname = "a", 1951797ac58cSKevin Wolf .argmin = 1, 1952797ac58cSKevin Wolf .argmax = 2, 1953797ac58cSKevin Wolf .cfunc = alloc_f, 19544401fdc7SEric Blake .args = "offset [count]", 19554401fdc7SEric Blake .oneline = "checks if offset is allocated in the file", 1956797ac58cSKevin Wolf }; 1957797ac58cSKevin Wolf 1958797ac58cSKevin Wolf 1959d6a644bbSEric Blake static int map_is_allocated(BlockDriverState *bs, int64_t offset, 1960d6a644bbSEric Blake int64_t bytes, int64_t *pnum) 1961797ac58cSKevin Wolf { 1962d6a644bbSEric Blake int64_t num; 1963d6a644bbSEric Blake int num_checked; 1964797ac58cSKevin Wolf int ret, firstret; 1965797ac58cSKevin Wolf 1966d6a644bbSEric Blake num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES); 1967d6a644bbSEric Blake ret = bdrv_is_allocated(bs, offset, num_checked, &num); 1968797ac58cSKevin Wolf if (ret < 0) { 1969797ac58cSKevin Wolf return ret; 1970797ac58cSKevin Wolf } 1971797ac58cSKevin Wolf 1972797ac58cSKevin Wolf firstret = ret; 1973797ac58cSKevin Wolf *pnum = num; 1974797ac58cSKevin Wolf 1975d6a644bbSEric Blake while (bytes > 0 && ret == firstret) { 1976d6a644bbSEric Blake offset += num; 1977d6a644bbSEric Blake bytes -= num; 1978797ac58cSKevin Wolf 1979d6a644bbSEric Blake num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES); 1980d6a644bbSEric Blake ret = bdrv_is_allocated(bs, offset, num_checked, &num); 19814b25bbc4SMax Reitz if (ret == firstret && num) { 1982797ac58cSKevin Wolf *pnum += num; 1983797ac58cSKevin Wolf } else { 1984797ac58cSKevin Wolf break; 1985797ac58cSKevin Wolf } 1986797ac58cSKevin Wolf } 1987797ac58cSKevin Wolf 1988797ac58cSKevin Wolf return firstret; 1989797ac58cSKevin Wolf } 1990797ac58cSKevin Wolf 1991b32d7a39SMax Reitz static int map_f(BlockBackend *blk, int argc, char **argv) 1992797ac58cSKevin Wolf { 1993d6a644bbSEric Blake int64_t offset, bytes; 19946f3c90afSEric Blake char s1[64], s2[64]; 1995797ac58cSKevin Wolf int64_t num; 1996797ac58cSKevin Wolf int ret; 1997797ac58cSKevin Wolf const char *retstr; 1998797ac58cSKevin Wolf 1999797ac58cSKevin Wolf offset = 0; 2000d6a644bbSEric Blake bytes = blk_getlength(blk); 2001d6a644bbSEric Blake if (bytes < 0) { 2002d6a644bbSEric Blake error_report("Failed to query image length: %s", strerror(-bytes)); 2003b32d7a39SMax Reitz return bytes; 20044c7b7e9bSMax Reitz } 20054c7b7e9bSMax Reitz 2006d6a644bbSEric Blake while (bytes) { 2007d6a644bbSEric Blake ret = map_is_allocated(blk_bs(blk), offset, bytes, &num); 2008797ac58cSKevin Wolf if (ret < 0) { 2009797ac58cSKevin Wolf error_report("Failed to get allocation status: %s", strerror(-ret)); 2010b32d7a39SMax Reitz return ret; 20114b25bbc4SMax Reitz } else if (!num) { 20124b25bbc4SMax Reitz error_report("Unexpected end of image"); 2013b32d7a39SMax Reitz return -EIO; 2014797ac58cSKevin Wolf } 2015797ac58cSKevin Wolf 2016797ac58cSKevin Wolf retstr = ret ? " allocated" : "not allocated"; 2017d6a644bbSEric Blake cvtstr(num, s1, sizeof(s1)); 2018d6a644bbSEric Blake cvtstr(offset, s2, sizeof(s2)); 20196f3c90afSEric Blake printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n", 2020d6a644bbSEric Blake s1, num, retstr, s2, offset); 2021797ac58cSKevin Wolf 2022797ac58cSKevin Wolf offset += num; 2023d6a644bbSEric Blake bytes -= num; 2024d6a644bbSEric Blake } 2025b32d7a39SMax Reitz 2026b32d7a39SMax Reitz return 0; 2027797ac58cSKevin Wolf } 2028797ac58cSKevin Wolf 2029797ac58cSKevin Wolf static const cmdinfo_t map_cmd = { 2030797ac58cSKevin Wolf .name = "map", 2031797ac58cSKevin Wolf .argmin = 0, 2032797ac58cSKevin Wolf .argmax = 0, 2033797ac58cSKevin Wolf .cfunc = map_f, 2034797ac58cSKevin Wolf .args = "", 2035797ac58cSKevin Wolf .oneline = "prints the allocated areas of a file", 2036797ac58cSKevin Wolf }; 2037797ac58cSKevin Wolf 20385bbd2e59SKevin Wolf static void reopen_help(void) 20395bbd2e59SKevin Wolf { 20405bbd2e59SKevin Wolf printf( 20415bbd2e59SKevin Wolf "\n" 20425bbd2e59SKevin Wolf " Changes the open options of an already opened image\n" 20435bbd2e59SKevin Wolf "\n" 20445bbd2e59SKevin Wolf " Example:\n" 20455bbd2e59SKevin Wolf " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n" 20465bbd2e59SKevin Wolf "\n" 20475bbd2e59SKevin Wolf " -r, -- Reopen the image read-only\n" 2048ea92203cSKevin Wolf " -w, -- Reopen the image read-write\n" 20495bbd2e59SKevin Wolf " -c, -- Change the cache mode to the given value\n" 20505bbd2e59SKevin Wolf " -o, -- Changes block driver options (cf. 'open' command)\n" 20515bbd2e59SKevin Wolf "\n"); 20525bbd2e59SKevin Wolf } 20535bbd2e59SKevin Wolf 2054b32d7a39SMax Reitz static int reopen_f(BlockBackend *blk, int argc, char **argv); 20555bbd2e59SKevin Wolf 20565bbd2e59SKevin Wolf static QemuOptsList reopen_opts = { 20575bbd2e59SKevin Wolf .name = "reopen", 20585bbd2e59SKevin Wolf .merge_lists = true, 20595bbd2e59SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head), 20605bbd2e59SKevin Wolf .desc = { 20615bbd2e59SKevin Wolf /* no elements => accept any params */ 20625bbd2e59SKevin Wolf { /* end of list */ } 20635bbd2e59SKevin Wolf }, 20645bbd2e59SKevin Wolf }; 20655bbd2e59SKevin Wolf 20665bbd2e59SKevin Wolf static const cmdinfo_t reopen_cmd = { 20675bbd2e59SKevin Wolf .name = "reopen", 20685bbd2e59SKevin Wolf .argmin = 0, 20695bbd2e59SKevin Wolf .argmax = -1, 20705bbd2e59SKevin Wolf .cfunc = reopen_f, 2071ea92203cSKevin Wolf .args = "[(-r|-w)] [-c cache] [-o options]", 20725bbd2e59SKevin Wolf .oneline = "reopens an image with new options", 20735bbd2e59SKevin Wolf .help = reopen_help, 20745bbd2e59SKevin Wolf }; 20755bbd2e59SKevin Wolf 2076b32d7a39SMax Reitz static int reopen_f(BlockBackend *blk, int argc, char **argv) 20775bbd2e59SKevin Wolf { 20785bbd2e59SKevin Wolf BlockDriverState *bs = blk_bs(blk); 20795bbd2e59SKevin Wolf QemuOpts *qopts; 20805bbd2e59SKevin Wolf QDict *opts; 20815bbd2e59SKevin Wolf int c; 20825bbd2e59SKevin Wolf int flags = bs->open_flags; 208319dbecdcSKevin Wolf bool writethrough = !blk_enable_write_cache(blk); 2084ea92203cSKevin Wolf bool has_rw_option = false; 2085dc900c35SAlberto Garcia bool has_cache_option = false; 20865bbd2e59SKevin Wolf 20875bbd2e59SKevin Wolf BlockReopenQueue *brq; 20885bbd2e59SKevin Wolf Error *local_err = NULL; 20895bbd2e59SKevin Wolf 2090ea92203cSKevin Wolf while ((c = getopt(argc, argv, "c:o:rw")) != -1) { 20915bbd2e59SKevin Wolf switch (c) { 20925bbd2e59SKevin Wolf case 'c': 209319dbecdcSKevin Wolf if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { 20945bbd2e59SKevin Wolf error_report("Invalid cache option: %s", optarg); 2095b32d7a39SMax Reitz return -EINVAL; 20965bbd2e59SKevin Wolf } 2097dc900c35SAlberto Garcia has_cache_option = true; 20985bbd2e59SKevin Wolf break; 20995bbd2e59SKevin Wolf case 'o': 21005bbd2e59SKevin Wolf if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) { 21015bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 2102b32d7a39SMax Reitz return -EINVAL; 21035bbd2e59SKevin Wolf } 21045bbd2e59SKevin Wolf break; 21055bbd2e59SKevin Wolf case 'r': 2106ea92203cSKevin Wolf if (has_rw_option) { 2107ea92203cSKevin Wolf error_report("Only one -r/-w option may be given"); 2108b32d7a39SMax Reitz return -EINVAL; 2109ea92203cSKevin Wolf } 21105bbd2e59SKevin Wolf flags &= ~BDRV_O_RDWR; 2111ea92203cSKevin Wolf has_rw_option = true; 2112ea92203cSKevin Wolf break; 2113ea92203cSKevin Wolf case 'w': 2114ea92203cSKevin Wolf if (has_rw_option) { 2115ea92203cSKevin Wolf error_report("Only one -r/-w option may be given"); 2116b32d7a39SMax Reitz return -EINVAL; 2117ea92203cSKevin Wolf } 2118ea92203cSKevin Wolf flags |= BDRV_O_RDWR; 2119ea92203cSKevin Wolf has_rw_option = true; 21205bbd2e59SKevin Wolf break; 21215bbd2e59SKevin Wolf default: 21225bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 2123b444d0e9SMax Reitz qemuio_command_usage(&reopen_cmd); 2124b32d7a39SMax Reitz return -EINVAL; 21255bbd2e59SKevin Wolf } 21265bbd2e59SKevin Wolf } 21275bbd2e59SKevin Wolf 21285bbd2e59SKevin Wolf if (optind != argc) { 21295bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 2130b444d0e9SMax Reitz qemuio_command_usage(&reopen_cmd); 2131b32d7a39SMax Reitz return -EINVAL; 21325bbd2e59SKevin Wolf } 21335bbd2e59SKevin Wolf 2134a8003ec4SAlberto Garcia if (!writethrough != blk_enable_write_cache(blk) && 213519dbecdcSKevin Wolf blk_get_attached_dev(blk)) 213619dbecdcSKevin Wolf { 213719dbecdcSKevin Wolf error_report("Cannot change cache.writeback: Device attached"); 213819dbecdcSKevin Wolf qemu_opts_reset(&reopen_opts); 2139b32d7a39SMax Reitz return -EBUSY; 214019dbecdcSKevin Wolf } 214119dbecdcSKevin Wolf 2142f3adefb2SKevin Wolf if (!(flags & BDRV_O_RDWR)) { 2143f3adefb2SKevin Wolf uint64_t orig_perm, orig_shared_perm; 2144f3adefb2SKevin Wolf 2145f3adefb2SKevin Wolf bdrv_drain(bs); 2146f3adefb2SKevin Wolf 2147f3adefb2SKevin Wolf blk_get_perm(blk, &orig_perm, &orig_shared_perm); 2148f3adefb2SKevin Wolf blk_set_perm(blk, 2149f3adefb2SKevin Wolf orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED), 2150f3adefb2SKevin Wolf orig_shared_perm, 2151f3adefb2SKevin Wolf &error_abort); 2152f3adefb2SKevin Wolf } 2153f3adefb2SKevin Wolf 21545bbd2e59SKevin Wolf qopts = qemu_opts_find(&reopen_opts, NULL); 2155dc900c35SAlberto Garcia opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new(); 21565bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 21575bbd2e59SKevin Wolf 2158dc900c35SAlberto Garcia if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) { 2159dc900c35SAlberto Garcia if (has_rw_option) { 2160dc900c35SAlberto Garcia error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'"); 2161dc900c35SAlberto Garcia qobject_unref(opts); 2162dc900c35SAlberto Garcia return -EINVAL; 2163dc900c35SAlberto Garcia } 2164dc900c35SAlberto Garcia } else { 2165dc900c35SAlberto Garcia qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR)); 2166dc900c35SAlberto Garcia } 2167dc900c35SAlberto Garcia 2168dc900c35SAlberto Garcia if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) || 2169dc900c35SAlberto Garcia qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) { 2170dc900c35SAlberto Garcia if (has_cache_option) { 2171dc900c35SAlberto Garcia error_report("Cannot set both -c and the cache options"); 2172dc900c35SAlberto Garcia qobject_unref(opts); 2173dc900c35SAlberto Garcia return -EINVAL; 2174dc900c35SAlberto Garcia } 2175dc900c35SAlberto Garcia } else { 2176dc900c35SAlberto Garcia qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE); 2177dc900c35SAlberto Garcia qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH); 2178dc900c35SAlberto Garcia } 2179dc900c35SAlberto Garcia 21801a63a907SKevin Wolf bdrv_subtree_drained_begin(bs); 2181077e8e20SAlberto Garcia brq = bdrv_reopen_queue(NULL, bs, opts, true); 21825019aeceSAlberto Garcia bdrv_reopen_multiple(brq, &local_err); 21831a63a907SKevin Wolf bdrv_subtree_drained_end(bs); 21841a63a907SKevin Wolf 21855bbd2e59SKevin Wolf if (local_err) { 21865bbd2e59SKevin Wolf error_report_err(local_err); 2187b32d7a39SMax Reitz return -EINVAL; 21885bbd2e59SKevin Wolf } 21895bbd2e59SKevin Wolf 2190b32d7a39SMax Reitz blk_set_enable_write_cache(blk, !writethrough); 2191b32d7a39SMax Reitz return 0; 2192b32d7a39SMax Reitz } 2193b32d7a39SMax Reitz 2194b32d7a39SMax Reitz static int break_f(BlockBackend *blk, int argc, char **argv) 2195797ac58cSKevin Wolf { 2196797ac58cSKevin Wolf int ret; 2197797ac58cSKevin Wolf 21984c7b7e9bSMax Reitz ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]); 2199797ac58cSKevin Wolf if (ret < 0) { 2200797ac58cSKevin Wolf printf("Could not set breakpoint: %s\n", strerror(-ret)); 2201b32d7a39SMax Reitz return ret; 2202797ac58cSKevin Wolf } 2203797ac58cSKevin Wolf 2204b32d7a39SMax Reitz return 0; 2205b32d7a39SMax Reitz } 2206b32d7a39SMax Reitz 2207b32d7a39SMax Reitz static int remove_break_f(BlockBackend *blk, int argc, char **argv) 22084cc70e93SFam Zheng { 22094cc70e93SFam Zheng int ret; 22104cc70e93SFam Zheng 22114c7b7e9bSMax Reitz ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]); 22124cc70e93SFam Zheng if (ret < 0) { 22134cc70e93SFam Zheng printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret)); 2214b32d7a39SMax Reitz return ret; 22154cc70e93SFam Zheng } 2216b32d7a39SMax Reitz 2217b32d7a39SMax Reitz return 0; 22184cc70e93SFam Zheng } 22194cc70e93SFam Zheng 2220797ac58cSKevin Wolf static const cmdinfo_t break_cmd = { 2221797ac58cSKevin Wolf .name = "break", 2222797ac58cSKevin Wolf .argmin = 2, 2223797ac58cSKevin Wolf .argmax = 2, 2224797ac58cSKevin Wolf .cfunc = break_f, 2225797ac58cSKevin Wolf .args = "event tag", 2226797ac58cSKevin Wolf .oneline = "sets a breakpoint on event and tags the stopped " 2227797ac58cSKevin Wolf "request as tag", 2228797ac58cSKevin Wolf }; 2229797ac58cSKevin Wolf 22304cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = { 22314cc70e93SFam Zheng .name = "remove_break", 22324cc70e93SFam Zheng .argmin = 1, 22334cc70e93SFam Zheng .argmax = 1, 22344cc70e93SFam Zheng .cfunc = remove_break_f, 22354cc70e93SFam Zheng .args = "tag", 22364cc70e93SFam Zheng .oneline = "remove a breakpoint by tag", 22374cc70e93SFam Zheng }; 22384cc70e93SFam Zheng 2239b32d7a39SMax Reitz static int resume_f(BlockBackend *blk, int argc, char **argv) 2240797ac58cSKevin Wolf { 2241797ac58cSKevin Wolf int ret; 2242797ac58cSKevin Wolf 22434c7b7e9bSMax Reitz ret = bdrv_debug_resume(blk_bs(blk), argv[1]); 2244797ac58cSKevin Wolf if (ret < 0) { 2245797ac58cSKevin Wolf printf("Could not resume request: %s\n", strerror(-ret)); 2246b32d7a39SMax Reitz return ret; 2247797ac58cSKevin Wolf } 2248b32d7a39SMax Reitz 2249b32d7a39SMax Reitz return 0; 2250797ac58cSKevin Wolf } 2251797ac58cSKevin Wolf 2252797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = { 2253797ac58cSKevin Wolf .name = "resume", 2254797ac58cSKevin Wolf .argmin = 1, 2255797ac58cSKevin Wolf .argmax = 1, 2256797ac58cSKevin Wolf .cfunc = resume_f, 2257797ac58cSKevin Wolf .args = "tag", 2258797ac58cSKevin Wolf .oneline = "resumes the request tagged as tag", 2259797ac58cSKevin Wolf }; 2260797ac58cSKevin Wolf 2261b32d7a39SMax Reitz static int wait_break_f(BlockBackend *blk, int argc, char **argv) 2262797ac58cSKevin Wolf { 22634c7b7e9bSMax Reitz while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) { 22644c7b7e9bSMax Reitz aio_poll(blk_get_aio_context(blk), true); 2265797ac58cSKevin Wolf } 2266b32d7a39SMax Reitz return 0; 2267797ac58cSKevin Wolf } 2268797ac58cSKevin Wolf 2269797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = { 2270797ac58cSKevin Wolf .name = "wait_break", 2271797ac58cSKevin Wolf .argmin = 1, 2272797ac58cSKevin Wolf .argmax = 1, 2273797ac58cSKevin Wolf .cfunc = wait_break_f, 2274797ac58cSKevin Wolf .args = "tag", 2275797ac58cSKevin Wolf .oneline = "waits for the suspension of a request", 2276797ac58cSKevin Wolf }; 2277797ac58cSKevin Wolf 2278b32d7a39SMax Reitz static int abort_f(BlockBackend *blk, int argc, char **argv) 2279797ac58cSKevin Wolf { 2280797ac58cSKevin Wolf abort(); 2281797ac58cSKevin Wolf } 2282797ac58cSKevin Wolf 2283797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = { 2284797ac58cSKevin Wolf .name = "abort", 2285797ac58cSKevin Wolf .cfunc = abort_f, 2286797ac58cSKevin Wolf .flags = CMD_NOFILE_OK, 2287797ac58cSKevin Wolf .oneline = "simulate a program crash using abort(3)", 2288797ac58cSKevin Wolf }; 2289797ac58cSKevin Wolf 22900e82dc7bSMax Reitz static void sigraise_help(void) 22910e82dc7bSMax Reitz { 22920e82dc7bSMax Reitz printf( 22930e82dc7bSMax Reitz "\n" 22940e82dc7bSMax Reitz " raises the given signal\n" 22950e82dc7bSMax Reitz "\n" 22960e82dc7bSMax Reitz " Example:\n" 22970e82dc7bSMax Reitz " 'sigraise %i' - raises SIGTERM\n" 22980e82dc7bSMax Reitz "\n" 22990e82dc7bSMax Reitz " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n" 23000e82dc7bSMax Reitz " given to sigraise.\n" 23010e82dc7bSMax Reitz "\n", SIGTERM); 23020e82dc7bSMax Reitz } 23030e82dc7bSMax Reitz 2304b32d7a39SMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv); 23050e82dc7bSMax Reitz 23060e82dc7bSMax Reitz static const cmdinfo_t sigraise_cmd = { 23070e82dc7bSMax Reitz .name = "sigraise", 23080e82dc7bSMax Reitz .cfunc = sigraise_f, 23090e82dc7bSMax Reitz .argmin = 1, 23100e82dc7bSMax Reitz .argmax = 1, 23110e82dc7bSMax Reitz .flags = CMD_NOFILE_OK, 23120e82dc7bSMax Reitz .args = "signal", 23130e82dc7bSMax Reitz .oneline = "raises a signal", 23140e82dc7bSMax Reitz .help = sigraise_help, 23150e82dc7bSMax Reitz }; 23160e82dc7bSMax Reitz 2317b32d7a39SMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv) 23180e82dc7bSMax Reitz { 23199b0beaf3SJohn Snow int64_t sig = cvtnum(argv[1]); 23200e82dc7bSMax Reitz if (sig < 0) { 2321a9ecfa00SJohn Snow print_cvtnum_err(sig, argv[1]); 2322b32d7a39SMax Reitz return sig; 23239b0beaf3SJohn Snow } else if (sig > NSIG) { 23249b0beaf3SJohn Snow printf("signal argument '%s' is too large to be a valid signal\n", 23259b0beaf3SJohn Snow argv[1]); 2326b32d7a39SMax Reitz return -EINVAL; 23270e82dc7bSMax Reitz } 23280e82dc7bSMax Reitz 23290e82dc7bSMax Reitz /* Using raise() to kill this process does not necessarily flush all open 23300e82dc7bSMax Reitz * streams. At least stdout and stderr (although the latter should be 23310e82dc7bSMax Reitz * non-buffered anyway) should be flushed, though. */ 23320e82dc7bSMax Reitz fflush(stdout); 23330e82dc7bSMax Reitz fflush(stderr); 23340e82dc7bSMax Reitz 23350e82dc7bSMax Reitz raise(sig); 2336b32d7a39SMax Reitz 2337b32d7a39SMax Reitz return 0; 23380e82dc7bSMax Reitz } 23390e82dc7bSMax Reitz 2340cd33d02aSKevin Wolf static void sleep_cb(void *opaque) 2341cd33d02aSKevin Wolf { 2342cd33d02aSKevin Wolf bool *expired = opaque; 2343cd33d02aSKevin Wolf *expired = true; 2344cd33d02aSKevin Wolf } 2345cd33d02aSKevin Wolf 2346b32d7a39SMax Reitz static int sleep_f(BlockBackend *blk, int argc, char **argv) 2347cd33d02aSKevin Wolf { 2348cd33d02aSKevin Wolf char *endptr; 2349cd33d02aSKevin Wolf long ms; 2350cd33d02aSKevin Wolf struct QEMUTimer *timer; 2351cd33d02aSKevin Wolf bool expired = false; 2352cd33d02aSKevin Wolf 2353cd33d02aSKevin Wolf ms = strtol(argv[1], &endptr, 0); 2354cd33d02aSKevin Wolf if (ms < 0 || *endptr != '\0') { 2355cd33d02aSKevin Wolf printf("%s is not a valid number\n", argv[1]); 2356b32d7a39SMax Reitz return -EINVAL; 2357cd33d02aSKevin Wolf } 2358cd33d02aSKevin Wolf 2359cd33d02aSKevin Wolf timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired); 2360cd33d02aSKevin Wolf timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms); 2361cd33d02aSKevin Wolf 2362cd33d02aSKevin Wolf while (!expired) { 2363cd33d02aSKevin Wolf main_loop_wait(false); 2364cd33d02aSKevin Wolf } 2365cd33d02aSKevin Wolf 2366cd33d02aSKevin Wolf timer_free(timer); 2367b32d7a39SMax Reitz return 0; 2368cd33d02aSKevin Wolf } 2369cd33d02aSKevin Wolf 2370cd33d02aSKevin Wolf static const cmdinfo_t sleep_cmd = { 2371cd33d02aSKevin Wolf .name = "sleep", 2372cd33d02aSKevin Wolf .argmin = 1, 2373cd33d02aSKevin Wolf .argmax = 1, 2374cd33d02aSKevin Wolf .cfunc = sleep_f, 2375cd33d02aSKevin Wolf .flags = CMD_NOFILE_OK, 2376cd33d02aSKevin Wolf .oneline = "waits for the given value in milliseconds", 2377cd33d02aSKevin Wolf }; 2378cd33d02aSKevin Wolf 2379f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct) 2380f18a834aSKevin Wolf { 2381f18a834aSKevin Wolf if (cmd) { 2382f18a834aSKevin Wolf printf("%s ", cmd); 2383f18a834aSKevin Wolf } else { 2384f18a834aSKevin Wolf printf("%s ", ct->name); 2385f18a834aSKevin Wolf if (ct->altname) { 2386f18a834aSKevin Wolf printf("(or %s) ", ct->altname); 2387f18a834aSKevin Wolf } 2388f18a834aSKevin Wolf } 2389f18a834aSKevin Wolf 2390f18a834aSKevin Wolf if (ct->args) { 2391f18a834aSKevin Wolf printf("%s ", ct->args); 2392f18a834aSKevin Wolf } 2393f18a834aSKevin Wolf printf("-- %s\n", ct->oneline); 2394f18a834aSKevin Wolf } 2395f18a834aSKevin Wolf 2396f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct) 2397f18a834aSKevin Wolf { 2398f18a834aSKevin Wolf help_oneline(cmd, ct); 2399f18a834aSKevin Wolf if (ct->help) { 2400f18a834aSKevin Wolf ct->help(); 2401f18a834aSKevin Wolf } 2402f18a834aSKevin Wolf } 2403f18a834aSKevin Wolf 2404f18a834aSKevin Wolf static void help_all(void) 2405f18a834aSKevin Wolf { 2406f18a834aSKevin Wolf const cmdinfo_t *ct; 2407f18a834aSKevin Wolf 2408f18a834aSKevin Wolf for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 2409f18a834aSKevin Wolf help_oneline(ct->name, ct); 2410f18a834aSKevin Wolf } 2411f18a834aSKevin Wolf printf("\nUse 'help commandname' for extended help.\n"); 2412f18a834aSKevin Wolf } 2413f18a834aSKevin Wolf 2414b32d7a39SMax Reitz static int help_f(BlockBackend *blk, int argc, char **argv) 2415f18a834aSKevin Wolf { 2416f18a834aSKevin Wolf const cmdinfo_t *ct; 2417f18a834aSKevin Wolf 2418f18a834aSKevin Wolf if (argc == 1) { 2419f18a834aSKevin Wolf help_all(); 2420b32d7a39SMax Reitz return 0; 2421f18a834aSKevin Wolf } 2422f18a834aSKevin Wolf 2423f18a834aSKevin Wolf ct = find_command(argv[1]); 2424f18a834aSKevin Wolf if (ct == NULL) { 2425f18a834aSKevin Wolf printf("command %s not found\n", argv[1]); 2426b32d7a39SMax Reitz return -EINVAL; 2427f18a834aSKevin Wolf } 2428f18a834aSKevin Wolf 2429f18a834aSKevin Wolf help_onecmd(argv[1], ct); 2430b32d7a39SMax Reitz return 0; 2431f18a834aSKevin Wolf } 2432f18a834aSKevin Wolf 2433f18a834aSKevin Wolf static const cmdinfo_t help_cmd = { 2434f18a834aSKevin Wolf .name = "help", 2435f18a834aSKevin Wolf .altname = "?", 2436f18a834aSKevin Wolf .cfunc = help_f, 2437f18a834aSKevin Wolf .argmin = 0, 2438f18a834aSKevin Wolf .argmax = 1, 2439f18a834aSKevin Wolf .flags = CMD_FLAG_GLOBAL, 2440f18a834aSKevin Wolf .args = "[command]", 2441f18a834aSKevin Wolf .oneline = "help for one or all commands", 2442f18a834aSKevin Wolf }; 2443f18a834aSKevin Wolf 2444b32d7a39SMax Reitz int qemuio_command(BlockBackend *blk, const char *cmd) 2445dd583296SKevin Wolf { 244615afd94aSPaolo Bonzini AioContext *ctx; 2447dd583296SKevin Wolf char *input; 2448dd583296SKevin Wolf const cmdinfo_t *ct; 2449dd583296SKevin Wolf char **v; 2450dd583296SKevin Wolf int c; 2451b32d7a39SMax Reitz int ret = 0; 2452dd583296SKevin Wolf 2453dd583296SKevin Wolf input = g_strdup(cmd); 2454dd583296SKevin Wolf v = breakline(input, &c); 2455dd583296SKevin Wolf if (c) { 2456dd583296SKevin Wolf ct = find_command(v[0]); 2457dd583296SKevin Wolf if (ct) { 245815afd94aSPaolo Bonzini ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context(); 245915afd94aSPaolo Bonzini aio_context_acquire(ctx); 2460b32d7a39SMax Reitz ret = command(blk, ct, c, v); 246115afd94aSPaolo Bonzini aio_context_release(ctx); 2462dd583296SKevin Wolf } else { 2463dd583296SKevin Wolf fprintf(stderr, "command \"%s\" not found\n", v[0]); 2464b32d7a39SMax Reitz ret = -EINVAL; 2465dd583296SKevin Wolf } 2466dd583296SKevin Wolf } 2467dd583296SKevin Wolf g_free(input); 2468dd583296SKevin Wolf g_free(v); 2469b32d7a39SMax Reitz 2470b32d7a39SMax Reitz return ret; 2471dd583296SKevin Wolf } 2472dd583296SKevin Wolf 2473797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void) 2474797ac58cSKevin Wolf { 2475797ac58cSKevin Wolf /* initialize commands */ 2476c2cdf5c5SKevin Wolf qemuio_add_command(&help_cmd); 2477c2cdf5c5SKevin Wolf qemuio_add_command(&read_cmd); 2478c2cdf5c5SKevin Wolf qemuio_add_command(&readv_cmd); 2479c2cdf5c5SKevin Wolf qemuio_add_command(&write_cmd); 2480c2cdf5c5SKevin Wolf qemuio_add_command(&writev_cmd); 2481c2cdf5c5SKevin Wolf qemuio_add_command(&aio_read_cmd); 2482c2cdf5c5SKevin Wolf qemuio_add_command(&aio_write_cmd); 2483c2cdf5c5SKevin Wolf qemuio_add_command(&aio_flush_cmd); 2484c2cdf5c5SKevin Wolf qemuio_add_command(&flush_cmd); 2485c2cdf5c5SKevin Wolf qemuio_add_command(&truncate_cmd); 2486c2cdf5c5SKevin Wolf qemuio_add_command(&length_cmd); 2487c2cdf5c5SKevin Wolf qemuio_add_command(&info_cmd); 2488c2cdf5c5SKevin Wolf qemuio_add_command(&discard_cmd); 2489c2cdf5c5SKevin Wolf qemuio_add_command(&alloc_cmd); 2490c2cdf5c5SKevin Wolf qemuio_add_command(&map_cmd); 24915bbd2e59SKevin Wolf qemuio_add_command(&reopen_cmd); 2492c2cdf5c5SKevin Wolf qemuio_add_command(&break_cmd); 24934cc70e93SFam Zheng qemuio_add_command(&remove_break_cmd); 2494c2cdf5c5SKevin Wolf qemuio_add_command(&resume_cmd); 2495c2cdf5c5SKevin Wolf qemuio_add_command(&wait_break_cmd); 2496c2cdf5c5SKevin Wolf qemuio_add_command(&abort_cmd); 2497cd33d02aSKevin Wolf qemuio_add_command(&sleep_cmd); 24980e82dc7bSMax Reitz qemuio_add_command(&sigraise_cmd); 2499797ac58cSKevin Wolf } 2500