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 95*8eaf1018SVladimir Sementsov-Ogievskiy /* 96*8eaf1018SVladimir Sementsov-Ogievskiy * Request additional permissions if necessary for this command. The caller 97887354bdSKevin Wolf * is responsible for restoring the original permissions afterwards if this 98*8eaf1018SVladimir Sementsov-Ogievskiy * is what it wants. 99*8eaf1018SVladimir Sementsov-Ogievskiy * 100*8eaf1018SVladimir Sementsov-Ogievskiy * Coverity thinks that blk may be NULL in the following if condition. It's 101*8eaf1018SVladimir Sementsov-Ogievskiy * not so: in init_check_command() we fail if blk is NULL for command with 102*8eaf1018SVladimir Sementsov-Ogievskiy * both CMD_FLAG_GLOBAL and CMD_NOFILE_OK flags unset. And in 103*8eaf1018SVladimir Sementsov-Ogievskiy * qemuio_add_command() we assert that command with non-zero .perm field 104*8eaf1018SVladimir Sementsov-Ogievskiy * doesn't set this flags. So, the following assertion is to silence 105*8eaf1018SVladimir Sementsov-Ogievskiy * Coverity: 106*8eaf1018SVladimir Sementsov-Ogievskiy */ 107*8eaf1018SVladimir Sementsov-Ogievskiy assert(blk || !ct->perm); 108887354bdSKevin Wolf if (ct->perm && blk_is_available(blk)) { 109887354bdSKevin Wolf uint64_t orig_perm, orig_shared_perm; 110887354bdSKevin Wolf blk_get_perm(blk, &orig_perm, &orig_shared_perm); 111887354bdSKevin Wolf 112887354bdSKevin Wolf if (ct->perm & ~orig_perm) { 113887354bdSKevin Wolf uint64_t new_perm; 114887354bdSKevin Wolf Error *local_err = NULL; 115887354bdSKevin Wolf int ret; 116887354bdSKevin Wolf 117887354bdSKevin Wolf new_perm = orig_perm | ct->perm; 118887354bdSKevin Wolf 119887354bdSKevin Wolf ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err); 120887354bdSKevin Wolf if (ret < 0) { 121887354bdSKevin Wolf error_report_err(local_err); 122b32d7a39SMax Reitz return ret; 123887354bdSKevin Wolf } 124887354bdSKevin Wolf } 125887354bdSKevin Wolf } 126887354bdSKevin Wolf 127d339d766SRichard W.M. Jones qemu_reset_optind(); 128b32d7a39SMax Reitz return ct->cfunc(blk, argc, argv); 129c2cdf5c5SKevin Wolf } 130c2cdf5c5SKevin Wolf 131c2cdf5c5SKevin Wolf static const cmdinfo_t *find_command(const char *cmd) 132c2cdf5c5SKevin Wolf { 133c2cdf5c5SKevin Wolf cmdinfo_t *ct; 134c2cdf5c5SKevin Wolf 135c2cdf5c5SKevin Wolf for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 136c2cdf5c5SKevin Wolf if (strcmp(ct->name, cmd) == 0 || 137c2cdf5c5SKevin Wolf (ct->altname && strcmp(ct->altname, cmd) == 0)) 138c2cdf5c5SKevin Wolf { 139c2cdf5c5SKevin Wolf return (const cmdinfo_t *)ct; 140c2cdf5c5SKevin Wolf } 141c2cdf5c5SKevin Wolf } 142c2cdf5c5SKevin Wolf return NULL; 143c2cdf5c5SKevin Wolf } 144c2cdf5c5SKevin Wolf 1454694020dSStefan Hajnoczi /* Invoke fn() for commands with a matching prefix */ 1464694020dSStefan Hajnoczi void qemuio_complete_command(const char *input, 1474694020dSStefan Hajnoczi void (*fn)(const char *cmd, void *opaque), 1484694020dSStefan Hajnoczi void *opaque) 1494694020dSStefan Hajnoczi { 1504694020dSStefan Hajnoczi cmdinfo_t *ct; 1514694020dSStefan Hajnoczi size_t input_len = strlen(input); 1524694020dSStefan Hajnoczi 1534694020dSStefan Hajnoczi for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 1544694020dSStefan Hajnoczi if (strncmp(input, ct->name, input_len) == 0) { 1554694020dSStefan Hajnoczi fn(ct->name, opaque); 1564694020dSStefan Hajnoczi } 1574694020dSStefan Hajnoczi } 1584694020dSStefan Hajnoczi } 1594694020dSStefan Hajnoczi 160c2cdf5c5SKevin Wolf static char **breakline(char *input, int *count) 161c2cdf5c5SKevin Wolf { 162c2cdf5c5SKevin Wolf int c = 0; 163c2cdf5c5SKevin Wolf char *p; 1645839e53bSMarkus Armbruster char **rval = g_new0(char *, 1); 165c2cdf5c5SKevin Wolf 166c2cdf5c5SKevin Wolf while (rval && (p = qemu_strsep(&input, " ")) != NULL) { 167c2cdf5c5SKevin Wolf if (!*p) { 168c2cdf5c5SKevin Wolf continue; 169c2cdf5c5SKevin Wolf } 170c2cdf5c5SKevin Wolf c++; 17108193dd5SMarkus Armbruster rval = g_renew(char *, rval, (c + 1)); 172c2cdf5c5SKevin Wolf rval[c - 1] = p; 173c2cdf5c5SKevin Wolf rval[c] = NULL; 174c2cdf5c5SKevin Wolf } 175c2cdf5c5SKevin Wolf *count = c; 176c2cdf5c5SKevin Wolf return rval; 177c2cdf5c5SKevin Wolf } 178c2cdf5c5SKevin Wolf 179797ac58cSKevin Wolf static int64_t cvtnum(const char *s) 180797ac58cSKevin Wolf { 181f17fd4fdSMarkus Armbruster int err; 182f46bfdbfSMarkus Armbruster uint64_t value; 183ef5a7885SJohn Snow 184f17fd4fdSMarkus Armbruster err = qemu_strtosz(s, NULL, &value); 185f17fd4fdSMarkus Armbruster if (err < 0) { 186f17fd4fdSMarkus Armbruster return err; 187f17fd4fdSMarkus Armbruster } 188f46bfdbfSMarkus Armbruster if (value > INT64_MAX) { 189f46bfdbfSMarkus Armbruster return -ERANGE; 190f46bfdbfSMarkus Armbruster } 191f17fd4fdSMarkus Armbruster return value; 192797ac58cSKevin Wolf } 193797ac58cSKevin Wolf 194a9ecfa00SJohn Snow static void print_cvtnum_err(int64_t rc, const char *arg) 195a9ecfa00SJohn Snow { 196a9ecfa00SJohn Snow switch (rc) { 197a9ecfa00SJohn Snow case -EINVAL: 198a9ecfa00SJohn Snow printf("Parsing error: non-numeric argument," 199a9ecfa00SJohn Snow " or extraneous/unrecognized suffix -- %s\n", arg); 200a9ecfa00SJohn Snow break; 201a9ecfa00SJohn Snow case -ERANGE: 202a9ecfa00SJohn Snow printf("Parsing error: argument too large -- %s\n", arg); 203a9ecfa00SJohn Snow break; 204a9ecfa00SJohn Snow default: 205a9ecfa00SJohn Snow printf("Parsing error: %s\n", arg); 206a9ecfa00SJohn Snow } 207a9ecfa00SJohn Snow } 208a9ecfa00SJohn Snow 2090b613881SKevin Wolf #define EXABYTES(x) ((long long)(x) << 60) 2100b613881SKevin Wolf #define PETABYTES(x) ((long long)(x) << 50) 2110b613881SKevin Wolf #define TERABYTES(x) ((long long)(x) << 40) 2120b613881SKevin Wolf #define GIGABYTES(x) ((long long)(x) << 30) 2130b613881SKevin Wolf #define MEGABYTES(x) ((long long)(x) << 20) 2140b613881SKevin Wolf #define KILOBYTES(x) ((long long)(x) << 10) 2150b613881SKevin Wolf 2160b613881SKevin Wolf #define TO_EXABYTES(x) ((x) / EXABYTES(1)) 2170b613881SKevin Wolf #define TO_PETABYTES(x) ((x) / PETABYTES(1)) 2180b613881SKevin Wolf #define TO_TERABYTES(x) ((x) / TERABYTES(1)) 2190b613881SKevin Wolf #define TO_GIGABYTES(x) ((x) / GIGABYTES(1)) 2200b613881SKevin Wolf #define TO_MEGABYTES(x) ((x) / MEGABYTES(1)) 2210b613881SKevin Wolf #define TO_KILOBYTES(x) ((x) / KILOBYTES(1)) 2220b613881SKevin Wolf 2230b613881SKevin Wolf static void cvtstr(double value, char *str, size_t size) 2240b613881SKevin Wolf { 2250b613881SKevin Wolf char *trim; 2260b613881SKevin Wolf const char *suffix; 2270b613881SKevin Wolf 2280b613881SKevin Wolf if (value >= EXABYTES(1)) { 2290b613881SKevin Wolf suffix = " EiB"; 2300b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_EXABYTES(value)); 2310b613881SKevin Wolf } else if (value >= PETABYTES(1)) { 2320b613881SKevin Wolf suffix = " PiB"; 2330b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_PETABYTES(value)); 2340b613881SKevin Wolf } else if (value >= TERABYTES(1)) { 2350b613881SKevin Wolf suffix = " TiB"; 2360b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_TERABYTES(value)); 2370b613881SKevin Wolf } else if (value >= GIGABYTES(1)) { 2380b613881SKevin Wolf suffix = " GiB"; 2390b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value)); 2400b613881SKevin Wolf } else if (value >= MEGABYTES(1)) { 2410b613881SKevin Wolf suffix = " MiB"; 2420b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value)); 2430b613881SKevin Wolf } else if (value >= KILOBYTES(1)) { 2440b613881SKevin Wolf suffix = " KiB"; 2450b613881SKevin Wolf snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value)); 2460b613881SKevin Wolf } else { 2470b613881SKevin Wolf suffix = " bytes"; 2480b613881SKevin Wolf snprintf(str, size - 6, "%f", value); 2490b613881SKevin Wolf } 2500b613881SKevin Wolf 2510b613881SKevin Wolf trim = strstr(str, ".000"); 2520b613881SKevin Wolf if (trim) { 2530b613881SKevin Wolf strcpy(trim, suffix); 2540b613881SKevin Wolf } else { 2550b613881SKevin Wolf strcat(str, suffix); 2560b613881SKevin Wolf } 2570b613881SKevin Wolf } 2580b613881SKevin Wolf 2590b613881SKevin Wolf 2600b613881SKevin Wolf 26150290c00SAlex Bennée static struct timespec tsub(struct timespec t1, struct timespec t2) 2620b613881SKevin Wolf { 26350290c00SAlex Bennée t1.tv_nsec -= t2.tv_nsec; 26450290c00SAlex Bennée if (t1.tv_nsec < 0) { 26550290c00SAlex Bennée t1.tv_nsec += NANOSECONDS_PER_SECOND; 2660b613881SKevin Wolf t1.tv_sec--; 2670b613881SKevin Wolf } 2680b613881SKevin Wolf t1.tv_sec -= t2.tv_sec; 2690b613881SKevin Wolf return t1; 2700b613881SKevin Wolf } 2710b613881SKevin Wolf 27250290c00SAlex Bennée static double tdiv(double value, struct timespec tv) 2730b613881SKevin Wolf { 27450290c00SAlex Bennée double seconds = tv.tv_sec + (tv.tv_nsec / 1e9); 27550290c00SAlex Bennée return value / seconds; 2760b613881SKevin Wolf } 2770b613881SKevin Wolf 2780b613881SKevin Wolf #define HOURS(sec) ((sec) / (60 * 60)) 2790b613881SKevin Wolf #define MINUTES(sec) (((sec) % (60 * 60)) / 60) 2800b613881SKevin Wolf #define SECONDS(sec) ((sec) % 60) 2810b613881SKevin Wolf 2820b613881SKevin Wolf enum { 2830b613881SKevin Wolf DEFAULT_TIME = 0x0, 2840b613881SKevin Wolf TERSE_FIXED_TIME = 0x1, 2850b613881SKevin Wolf VERBOSE_FIXED_TIME = 0x2, 2860b613881SKevin Wolf }; 2870b613881SKevin Wolf 28850290c00SAlex Bennée static void timestr(struct timespec *tv, char *ts, size_t size, int format) 2890b613881SKevin Wolf { 29050290c00SAlex Bennée double frac_sec = tv->tv_nsec / 1e9; 2910b613881SKevin Wolf 2920b613881SKevin Wolf if (format & TERSE_FIXED_TIME) { 2930b613881SKevin Wolf if (!HOURS(tv->tv_sec)) { 29450290c00SAlex Bennée snprintf(ts, size, "%u:%05.2f", 2950b613881SKevin Wolf (unsigned int) MINUTES(tv->tv_sec), 29650290c00SAlex Bennée SECONDS(tv->tv_sec) + frac_sec); 2970b613881SKevin Wolf return; 2980b613881SKevin Wolf } 2990b613881SKevin Wolf format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */ 3000b613881SKevin Wolf } 3010b613881SKevin Wolf 3020b613881SKevin Wolf if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) { 30350290c00SAlex Bennée snprintf(ts, size, "%u:%02u:%05.2f", 3040b613881SKevin Wolf (unsigned int) HOURS(tv->tv_sec), 3050b613881SKevin Wolf (unsigned int) MINUTES(tv->tv_sec), 30650290c00SAlex Bennée SECONDS(tv->tv_sec) + frac_sec); 3070b613881SKevin Wolf } else { 30850290c00SAlex Bennée snprintf(ts, size, "%05.2f sec", frac_sec); 3090b613881SKevin Wolf } 3100b613881SKevin Wolf } 3110b613881SKevin Wolf 312797ac58cSKevin Wolf /* 313797ac58cSKevin Wolf * Parse the pattern argument to various sub-commands. 314797ac58cSKevin Wolf * 315797ac58cSKevin Wolf * Because the pattern is used as an argument to memset it must evaluate 316797ac58cSKevin Wolf * to an unsigned integer that fits into a single byte. 317797ac58cSKevin Wolf */ 318797ac58cSKevin Wolf static int parse_pattern(const char *arg) 319797ac58cSKevin Wolf { 320797ac58cSKevin Wolf char *endptr = NULL; 321797ac58cSKevin Wolf long pattern; 322797ac58cSKevin Wolf 323797ac58cSKevin Wolf pattern = strtol(arg, &endptr, 0); 324797ac58cSKevin Wolf if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') { 325797ac58cSKevin Wolf printf("%s is not a valid pattern byte\n", arg); 326797ac58cSKevin Wolf return -1; 327797ac58cSKevin Wolf } 328797ac58cSKevin Wolf 329797ac58cSKevin Wolf return pattern; 330797ac58cSKevin Wolf } 331797ac58cSKevin Wolf 332797ac58cSKevin Wolf /* 333797ac58cSKevin Wolf * Memory allocation helpers. 334797ac58cSKevin Wolf * 335797ac58cSKevin Wolf * Make sure memory is aligned by default, or purposefully misaligned if 336797ac58cSKevin Wolf * that is specified on the command line. 337797ac58cSKevin Wolf */ 338797ac58cSKevin Wolf 339797ac58cSKevin Wolf #define MISALIGN_OFFSET 16 3404c7b7e9bSMax Reitz static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern) 341797ac58cSKevin Wolf { 342797ac58cSKevin Wolf void *buf; 343797ac58cSKevin Wolf 344797ac58cSKevin Wolf if (qemuio_misalign) { 345797ac58cSKevin Wolf len += MISALIGN_OFFSET; 346797ac58cSKevin Wolf } 3474c7b7e9bSMax Reitz buf = blk_blockalign(blk, len); 348797ac58cSKevin Wolf memset(buf, pattern, len); 349797ac58cSKevin Wolf if (qemuio_misalign) { 350797ac58cSKevin Wolf buf += MISALIGN_OFFSET; 351797ac58cSKevin Wolf } 352797ac58cSKevin Wolf return buf; 353797ac58cSKevin Wolf } 354797ac58cSKevin Wolf 355797ac58cSKevin Wolf static void qemu_io_free(void *p) 356797ac58cSKevin Wolf { 357797ac58cSKevin Wolf if (qemuio_misalign) { 358797ac58cSKevin Wolf p -= MISALIGN_OFFSET; 359797ac58cSKevin Wolf } 360797ac58cSKevin Wolf qemu_vfree(p); 361797ac58cSKevin Wolf } 362797ac58cSKevin Wolf 3634d731510SDenis Plotnikov /* 3644d731510SDenis Plotnikov * qemu_io_alloc_from_file() 3654d731510SDenis Plotnikov * 3664d731510SDenis Plotnikov * Allocates the buffer and populates it with the content of the given file 3674d731510SDenis Plotnikov * up to @len bytes. If the file length is less than @len, then the buffer 3684d731510SDenis Plotnikov * is populated with the file content cyclically. 3694d731510SDenis Plotnikov * 3704d731510SDenis Plotnikov * @blk - the block backend where the buffer content is going to be written to 3714d731510SDenis Plotnikov * @len - the buffer length 3724d731510SDenis Plotnikov * @file_name - the file to read the content from 3734d731510SDenis Plotnikov * 3744d731510SDenis Plotnikov * Returns: the buffer pointer on success 3754d731510SDenis Plotnikov * NULL on error 3764d731510SDenis Plotnikov */ 3774d731510SDenis Plotnikov static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len, 3784d731510SDenis Plotnikov const char *file_name) 3794d731510SDenis Plotnikov { 3804d731510SDenis Plotnikov char *buf, *buf_origin; 3814d731510SDenis Plotnikov FILE *f = fopen(file_name, "r"); 3824d731510SDenis Plotnikov int pattern_len; 3834d731510SDenis Plotnikov 3844d731510SDenis Plotnikov if (!f) { 3854d731510SDenis Plotnikov perror(file_name); 3864d731510SDenis Plotnikov return NULL; 3874d731510SDenis Plotnikov } 3884d731510SDenis Plotnikov 3894d731510SDenis Plotnikov if (qemuio_misalign) { 3904d731510SDenis Plotnikov len += MISALIGN_OFFSET; 3914d731510SDenis Plotnikov } 3924d731510SDenis Plotnikov 3934d731510SDenis Plotnikov buf_origin = buf = blk_blockalign(blk, len); 3944d731510SDenis Plotnikov 3954d731510SDenis Plotnikov if (qemuio_misalign) { 3964d731510SDenis Plotnikov buf_origin += MISALIGN_OFFSET; 3974d731510SDenis Plotnikov buf += MISALIGN_OFFSET; 3984d731510SDenis Plotnikov len -= MISALIGN_OFFSET; 3994d731510SDenis Plotnikov } 4004d731510SDenis Plotnikov 4014d731510SDenis Plotnikov pattern_len = fread(buf_origin, 1, len, f); 4024d731510SDenis Plotnikov 4034d731510SDenis Plotnikov if (ferror(f)) { 4044d731510SDenis Plotnikov perror(file_name); 4054d731510SDenis Plotnikov goto error; 4064d731510SDenis Plotnikov } 4074d731510SDenis Plotnikov 4084d731510SDenis Plotnikov if (pattern_len == 0) { 4094d731510SDenis Plotnikov fprintf(stderr, "%s: file is empty\n", file_name); 4104d731510SDenis Plotnikov goto error; 4114d731510SDenis Plotnikov } 4124d731510SDenis Plotnikov 4134d731510SDenis Plotnikov fclose(f); 414c8e68b43SKevin Wolf f = NULL; 4154d731510SDenis Plotnikov 4164d731510SDenis Plotnikov if (len > pattern_len) { 4174d731510SDenis Plotnikov len -= pattern_len; 4184d731510SDenis Plotnikov buf += pattern_len; 4194d731510SDenis Plotnikov 4204d731510SDenis Plotnikov while (len > 0) { 4214d731510SDenis Plotnikov size_t len_to_copy = MIN(pattern_len, len); 4224d731510SDenis Plotnikov 4234d731510SDenis Plotnikov memcpy(buf, buf_origin, len_to_copy); 4244d731510SDenis Plotnikov 4254d731510SDenis Plotnikov len -= len_to_copy; 4264d731510SDenis Plotnikov buf += len_to_copy; 4274d731510SDenis Plotnikov } 4284d731510SDenis Plotnikov } 4294d731510SDenis Plotnikov 4304d731510SDenis Plotnikov return buf_origin; 4314d731510SDenis Plotnikov 4324d731510SDenis Plotnikov error: 4334d731510SDenis Plotnikov qemu_io_free(buf_origin); 434c8e68b43SKevin Wolf if (f) { 435c8e68b43SKevin Wolf fclose(f); 436c8e68b43SKevin Wolf } 4374d731510SDenis Plotnikov return NULL; 4384d731510SDenis Plotnikov } 4394d731510SDenis Plotnikov 4409b0beaf3SJohn Snow static void dump_buffer(const void *buffer, int64_t offset, int64_t len) 441797ac58cSKevin Wolf { 4429b0beaf3SJohn Snow uint64_t i; 4439b0beaf3SJohn Snow int j; 444797ac58cSKevin Wolf const uint8_t *p; 445797ac58cSKevin Wolf 446797ac58cSKevin Wolf for (i = 0, p = buffer; i < len; i += 16) { 447797ac58cSKevin Wolf const uint8_t *s = p; 448797ac58cSKevin Wolf 449797ac58cSKevin Wolf printf("%08" PRIx64 ": ", offset + i); 450797ac58cSKevin Wolf for (j = 0; j < 16 && i + j < len; j++, p++) { 451797ac58cSKevin Wolf printf("%02x ", *p); 452797ac58cSKevin Wolf } 453797ac58cSKevin Wolf printf(" "); 454797ac58cSKevin Wolf for (j = 0; j < 16 && i + j < len; j++, s++) { 455797ac58cSKevin Wolf if (isalnum(*s)) { 456797ac58cSKevin Wolf printf("%c", *s); 457797ac58cSKevin Wolf } else { 458797ac58cSKevin Wolf printf("."); 459797ac58cSKevin Wolf } 460797ac58cSKevin Wolf } 461797ac58cSKevin Wolf printf("\n"); 462797ac58cSKevin Wolf } 463797ac58cSKevin Wolf } 464797ac58cSKevin Wolf 46550290c00SAlex Bennée static void print_report(const char *op, struct timespec *t, int64_t offset, 466dc38852aSEric Blake int64_t count, int64_t total, int cnt, bool Cflag) 467797ac58cSKevin Wolf { 468797ac58cSKevin Wolf char s1[64], s2[64], ts[64]; 469797ac58cSKevin Wolf 470797ac58cSKevin Wolf timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0); 471797ac58cSKevin Wolf if (!Cflag) { 472797ac58cSKevin Wolf cvtstr((double)total, s1, sizeof(s1)); 473797ac58cSKevin Wolf cvtstr(tdiv((double)total, *t), s2, sizeof(s2)); 4749b0beaf3SJohn Snow printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n", 475797ac58cSKevin Wolf op, total, count, offset); 476797ac58cSKevin Wolf printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n", 477797ac58cSKevin Wolf s1, cnt, ts, s2, tdiv((double)cnt, *t)); 478797ac58cSKevin Wolf } else {/* bytes,ops,time,bytes/sec,ops/sec */ 4799b0beaf3SJohn Snow printf("%"PRId64",%d,%s,%.3f,%.3f\n", 480797ac58cSKevin Wolf total, cnt, ts, 481797ac58cSKevin Wolf tdiv((double)total, *t), 482797ac58cSKevin Wolf tdiv((double)cnt, *t)); 483797ac58cSKevin Wolf } 484797ac58cSKevin Wolf } 485797ac58cSKevin Wolf 486797ac58cSKevin Wolf /* 487797ac58cSKevin Wolf * Parse multiple length statements for vectored I/O, and construct an I/O 488797ac58cSKevin Wolf * vector matching it. 489797ac58cSKevin Wolf */ 490797ac58cSKevin Wolf static void * 4914c7b7e9bSMax Reitz create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov, 492797ac58cSKevin Wolf int pattern) 493797ac58cSKevin Wolf { 494797ac58cSKevin Wolf size_t *sizes = g_new0(size_t, nr_iov); 495797ac58cSKevin Wolf size_t count = 0; 496797ac58cSKevin Wolf void *buf = NULL; 497797ac58cSKevin Wolf void *p; 498797ac58cSKevin Wolf int i; 499797ac58cSKevin Wolf 500797ac58cSKevin Wolf for (i = 0; i < nr_iov; i++) { 501797ac58cSKevin Wolf char *arg = argv[i]; 502797ac58cSKevin Wolf int64_t len; 503797ac58cSKevin Wolf 504797ac58cSKevin Wolf len = cvtnum(arg); 505797ac58cSKevin Wolf if (len < 0) { 506a9ecfa00SJohn Snow print_cvtnum_err(len, arg); 507797ac58cSKevin Wolf goto fail; 508797ac58cSKevin Wolf } 509797ac58cSKevin Wolf 5103026c468SAlberto Garcia if (len > BDRV_REQUEST_MAX_BYTES) { 5113026c468SAlberto Garcia printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg, 5123026c468SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES); 5133026c468SAlberto Garcia goto fail; 5143026c468SAlberto Garcia } 5153026c468SAlberto Garcia 5163026c468SAlberto Garcia if (count > BDRV_REQUEST_MAX_BYTES - len) { 5173026c468SAlberto Garcia printf("The total number of bytes exceed the maximum size %" PRIu64 5183026c468SAlberto Garcia "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES); 519797ac58cSKevin Wolf goto fail; 520797ac58cSKevin Wolf } 521797ac58cSKevin Wolf 522797ac58cSKevin Wolf sizes[i] = len; 523797ac58cSKevin Wolf count += len; 524797ac58cSKevin Wolf } 525797ac58cSKevin Wolf 526797ac58cSKevin Wolf qemu_iovec_init(qiov, nr_iov); 527797ac58cSKevin Wolf 5284c7b7e9bSMax Reitz buf = p = qemu_io_alloc(blk, count, pattern); 529797ac58cSKevin Wolf 530797ac58cSKevin Wolf for (i = 0; i < nr_iov; i++) { 531797ac58cSKevin Wolf qemu_iovec_add(qiov, p, sizes[i]); 532797ac58cSKevin Wolf p += sizes[i]; 533797ac58cSKevin Wolf } 534797ac58cSKevin Wolf 535797ac58cSKevin Wolf fail: 536797ac58cSKevin Wolf g_free(sizes); 537797ac58cSKevin Wolf return buf; 538797ac58cSKevin Wolf } 539797ac58cSKevin Wolf 5409b0beaf3SJohn Snow static int do_pread(BlockBackend *blk, char *buf, int64_t offset, 541f5a5ca79SManos Pitsidianakis int64_t bytes, int64_t *total) 542797ac58cSKevin Wolf { 543f5a5ca79SManos Pitsidianakis if (bytes > INT_MAX) { 5449b0beaf3SJohn Snow return -ERANGE; 5459b0beaf3SJohn Snow } 5469b0beaf3SJohn Snow 547f5a5ca79SManos Pitsidianakis *total = blk_pread(blk, offset, (uint8_t *)buf, bytes); 548797ac58cSKevin Wolf if (*total < 0) { 549797ac58cSKevin Wolf return *total; 550797ac58cSKevin Wolf } 551797ac58cSKevin Wolf return 1; 552797ac58cSKevin Wolf } 553797ac58cSKevin Wolf 5549b0beaf3SJohn Snow static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset, 555f5a5ca79SManos Pitsidianakis int64_t bytes, int flags, int64_t *total) 556797ac58cSKevin Wolf { 557f5a5ca79SManos Pitsidianakis if (bytes > INT_MAX) { 5589b0beaf3SJohn Snow return -ERANGE; 5599b0beaf3SJohn Snow } 5609b0beaf3SJohn Snow 561f5a5ca79SManos Pitsidianakis *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags); 562797ac58cSKevin Wolf if (*total < 0) { 563797ac58cSKevin Wolf return *total; 564797ac58cSKevin Wolf } 565797ac58cSKevin Wolf return 1; 566797ac58cSKevin Wolf } 567797ac58cSKevin Wolf 568797ac58cSKevin Wolf typedef struct { 5694c7b7e9bSMax Reitz BlockBackend *blk; 570797ac58cSKevin Wolf int64_t offset; 571f5a5ca79SManos Pitsidianakis int64_t bytes; 5729b0beaf3SJohn Snow int64_t *total; 573770e0e0eSEric Blake int flags; 574797ac58cSKevin Wolf int ret; 575797ac58cSKevin Wolf bool done; 576797ac58cSKevin Wolf } CoWriteZeroes; 577797ac58cSKevin Wolf 578d004bd52SEric Blake static void coroutine_fn co_pwrite_zeroes_entry(void *opaque) 579797ac58cSKevin Wolf { 580797ac58cSKevin Wolf CoWriteZeroes *data = opaque; 581797ac58cSKevin Wolf 582f5a5ca79SManos Pitsidianakis data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes, 583770e0e0eSEric Blake data->flags); 584797ac58cSKevin Wolf data->done = true; 585797ac58cSKevin Wolf if (data->ret < 0) { 586797ac58cSKevin Wolf *data->total = data->ret; 587797ac58cSKevin Wolf return; 588797ac58cSKevin Wolf } 589797ac58cSKevin Wolf 590f5a5ca79SManos Pitsidianakis *data->total = data->bytes; 591797ac58cSKevin Wolf } 592797ac58cSKevin Wolf 593d004bd52SEric Blake static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset, 594f5a5ca79SManos Pitsidianakis int64_t bytes, int flags, int64_t *total) 595797ac58cSKevin Wolf { 596797ac58cSKevin Wolf Coroutine *co; 597797ac58cSKevin Wolf CoWriteZeroes data = { 5984c7b7e9bSMax Reitz .blk = blk, 599797ac58cSKevin Wolf .offset = offset, 600f5a5ca79SManos Pitsidianakis .bytes = bytes, 601797ac58cSKevin Wolf .total = total, 602770e0e0eSEric Blake .flags = flags, 603797ac58cSKevin Wolf .done = false, 604797ac58cSKevin Wolf }; 605797ac58cSKevin Wolf 606f5a5ca79SManos Pitsidianakis if (bytes > INT_MAX) { 6079b0beaf3SJohn Snow return -ERANGE; 6089b0beaf3SJohn Snow } 6099b0beaf3SJohn Snow 6100b8b8753SPaolo Bonzini co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data); 611324ec3e4SFam Zheng bdrv_coroutine_enter(blk_bs(blk), co); 612797ac58cSKevin Wolf while (!data.done) { 6134c7b7e9bSMax Reitz aio_poll(blk_get_aio_context(blk), true); 614797ac58cSKevin Wolf } 615797ac58cSKevin Wolf if (data.ret < 0) { 616797ac58cSKevin Wolf return data.ret; 617797ac58cSKevin Wolf } else { 618797ac58cSKevin Wolf return 1; 619797ac58cSKevin Wolf } 620797ac58cSKevin Wolf } 621797ac58cSKevin Wolf 6224c7b7e9bSMax Reitz static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset, 623f5a5ca79SManos Pitsidianakis int64_t bytes, int64_t *total) 624797ac58cSKevin Wolf { 625797ac58cSKevin Wolf int ret; 626797ac58cSKevin Wolf 62741ae31e3SAlberto Garcia if (bytes > BDRV_REQUEST_MAX_BYTES) { 6289b0beaf3SJohn Snow return -ERANGE; 6299b0beaf3SJohn Snow } 6309b0beaf3SJohn Snow 631f5a5ca79SManos Pitsidianakis ret = blk_pwrite_compressed(blk, offset, buf, bytes); 632797ac58cSKevin Wolf if (ret < 0) { 633797ac58cSKevin Wolf return ret; 634797ac58cSKevin Wolf } 635f5a5ca79SManos Pitsidianakis *total = bytes; 636797ac58cSKevin Wolf return 1; 637797ac58cSKevin Wolf } 638797ac58cSKevin Wolf 6394c7b7e9bSMax Reitz static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset, 6409b0beaf3SJohn Snow int64_t count, int64_t *total) 641797ac58cSKevin Wolf { 6429b0beaf3SJohn Snow if (count > INT_MAX) { 6439b0beaf3SJohn Snow return -ERANGE; 6449b0beaf3SJohn Snow } 6459b0beaf3SJohn Snow 6464c7b7e9bSMax Reitz *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count); 647797ac58cSKevin Wolf if (*total < 0) { 648797ac58cSKevin Wolf return *total; 649797ac58cSKevin Wolf } 650797ac58cSKevin Wolf return 1; 651797ac58cSKevin Wolf } 652797ac58cSKevin Wolf 6534c7b7e9bSMax Reitz static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset, 6549b0beaf3SJohn Snow int64_t count, int64_t *total) 655797ac58cSKevin Wolf { 6569b0beaf3SJohn Snow if (count > INT_MAX) { 6579b0beaf3SJohn Snow return -ERANGE; 6589b0beaf3SJohn Snow } 6599b0beaf3SJohn Snow 6604c7b7e9bSMax Reitz *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count); 661797ac58cSKevin Wolf if (*total < 0) { 662797ac58cSKevin Wolf return *total; 663797ac58cSKevin Wolf } 664797ac58cSKevin Wolf return 1; 665797ac58cSKevin Wolf } 666797ac58cSKevin Wolf 667797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff 668797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret) 669797ac58cSKevin Wolf { 670797ac58cSKevin Wolf *(int *)opaque = ret; 671797ac58cSKevin Wolf } 672797ac58cSKevin Wolf 6734c7b7e9bSMax Reitz static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov, 674797ac58cSKevin Wolf int64_t offset, int *total) 675797ac58cSKevin Wolf { 676797ac58cSKevin Wolf int async_ret = NOT_DONE; 677797ac58cSKevin Wolf 6787b3f9712SEric Blake blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret); 679797ac58cSKevin Wolf while (async_ret == NOT_DONE) { 680797ac58cSKevin Wolf main_loop_wait(false); 681797ac58cSKevin Wolf } 682797ac58cSKevin Wolf 683797ac58cSKevin Wolf *total = qiov->size; 684797ac58cSKevin Wolf return async_ret < 0 ? async_ret : 1; 685797ac58cSKevin Wolf } 686797ac58cSKevin Wolf 6874c7b7e9bSMax Reitz static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov, 688770e0e0eSEric Blake int64_t offset, int flags, int *total) 689797ac58cSKevin Wolf { 690797ac58cSKevin Wolf int async_ret = NOT_DONE; 691797ac58cSKevin Wolf 692770e0e0eSEric Blake blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret); 693797ac58cSKevin Wolf while (async_ret == NOT_DONE) { 694797ac58cSKevin Wolf main_loop_wait(false); 695797ac58cSKevin Wolf } 696797ac58cSKevin Wolf 697797ac58cSKevin Wolf *total = qiov->size; 698797ac58cSKevin Wolf return async_ret < 0 ? async_ret : 1; 699797ac58cSKevin Wolf } 700797ac58cSKevin Wolf 701797ac58cSKevin Wolf static void read_help(void) 702797ac58cSKevin Wolf { 703797ac58cSKevin Wolf printf( 704797ac58cSKevin Wolf "\n" 705797ac58cSKevin Wolf " reads a range of bytes from the given offset\n" 706797ac58cSKevin Wolf "\n" 707797ac58cSKevin Wolf " Example:\n" 708797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n" 709797ac58cSKevin Wolf "\n" 710797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 711797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 712797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n" 713797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 714797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n" 715093ea232SEric Blake " -p, -- ignored for backwards compatibility\n" 716797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 717797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 718797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n" 719797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 720797ac58cSKevin Wolf "\n"); 721797ac58cSKevin Wolf } 722797ac58cSKevin Wolf 723b32d7a39SMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv); 724797ac58cSKevin Wolf 725797ac58cSKevin Wolf static const cmdinfo_t read_cmd = { 726797ac58cSKevin Wolf .name = "read", 727797ac58cSKevin Wolf .altname = "r", 728797ac58cSKevin Wolf .cfunc = read_f, 729797ac58cSKevin Wolf .argmin = 2, 730797ac58cSKevin Wolf .argmax = -1, 731093ea232SEric Blake .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len", 732797ac58cSKevin Wolf .oneline = "reads a number of bytes at a specified offset", 733797ac58cSKevin Wolf .help = read_help, 734797ac58cSKevin Wolf }; 735797ac58cSKevin Wolf 736b32d7a39SMax Reitz static int read_f(BlockBackend *blk, int argc, char **argv) 737797ac58cSKevin Wolf { 73850290c00SAlex Bennée struct timespec t1, t2; 739093ea232SEric Blake bool Cflag = false, qflag = false, vflag = false; 740dc38852aSEric Blake bool Pflag = false, sflag = false, lflag = false, bflag = false; 741b32d7a39SMax Reitz int c, cnt, ret; 742797ac58cSKevin Wolf char *buf; 743797ac58cSKevin Wolf int64_t offset; 7449b0beaf3SJohn Snow int64_t count; 745797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 7469b0beaf3SJohn Snow int64_t total = 0; 7479b0beaf3SJohn Snow int pattern = 0; 7489b0beaf3SJohn Snow int64_t pattern_offset = 0, pattern_count = 0; 749797ac58cSKevin Wolf 750b062ad86SEric Blake while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) { 751797ac58cSKevin Wolf switch (c) { 752797ac58cSKevin Wolf case 'b': 753dc38852aSEric Blake bflag = true; 754797ac58cSKevin Wolf break; 755797ac58cSKevin Wolf case 'C': 756dc38852aSEric Blake Cflag = true; 757797ac58cSKevin Wolf break; 758797ac58cSKevin Wolf case 'l': 759dc38852aSEric Blake lflag = true; 760797ac58cSKevin Wolf pattern_count = cvtnum(optarg); 761797ac58cSKevin Wolf if (pattern_count < 0) { 762a9ecfa00SJohn Snow print_cvtnum_err(pattern_count, optarg); 763b32d7a39SMax Reitz return pattern_count; 764797ac58cSKevin Wolf } 765797ac58cSKevin Wolf break; 766797ac58cSKevin Wolf case 'p': 767093ea232SEric Blake /* Ignored for backwards compatibility */ 768797ac58cSKevin Wolf break; 769797ac58cSKevin Wolf case 'P': 770dc38852aSEric Blake Pflag = true; 771797ac58cSKevin Wolf pattern = parse_pattern(optarg); 772797ac58cSKevin Wolf if (pattern < 0) { 773b32d7a39SMax Reitz return -EINVAL; 774797ac58cSKevin Wolf } 775797ac58cSKevin Wolf break; 776797ac58cSKevin Wolf case 'q': 777dc38852aSEric Blake qflag = true; 778797ac58cSKevin Wolf break; 779797ac58cSKevin Wolf case 's': 780dc38852aSEric Blake sflag = true; 781797ac58cSKevin Wolf pattern_offset = cvtnum(optarg); 782797ac58cSKevin Wolf if (pattern_offset < 0) { 783a9ecfa00SJohn Snow print_cvtnum_err(pattern_offset, optarg); 784b32d7a39SMax Reitz return pattern_offset; 785797ac58cSKevin Wolf } 786797ac58cSKevin Wolf break; 787797ac58cSKevin Wolf case 'v': 788dc38852aSEric Blake vflag = true; 789797ac58cSKevin Wolf break; 790797ac58cSKevin Wolf default: 791b444d0e9SMax Reitz qemuio_command_usage(&read_cmd); 792b32d7a39SMax Reitz return -EINVAL; 793797ac58cSKevin Wolf } 794797ac58cSKevin Wolf } 795797ac58cSKevin Wolf 796797ac58cSKevin Wolf if (optind != argc - 2) { 797b444d0e9SMax Reitz qemuio_command_usage(&read_cmd); 798b32d7a39SMax Reitz return -EINVAL; 799797ac58cSKevin Wolf } 800797ac58cSKevin Wolf 801797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 802797ac58cSKevin Wolf if (offset < 0) { 803a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 804b32d7a39SMax Reitz return offset; 805797ac58cSKevin Wolf } 806797ac58cSKevin Wolf 807797ac58cSKevin Wolf optind++; 808797ac58cSKevin Wolf count = cvtnum(argv[optind]); 809797ac58cSKevin Wolf if (count < 0) { 810a9ecfa00SJohn Snow print_cvtnum_err(count, argv[optind]); 811b32d7a39SMax Reitz return count; 8123026c468SAlberto Garcia } else if (count > BDRV_REQUEST_MAX_BYTES) { 8139b0beaf3SJohn Snow printf("length cannot exceed %" PRIu64 ", given %s\n", 8143026c468SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]); 815b32d7a39SMax Reitz return -EINVAL; 816797ac58cSKevin Wolf } 817797ac58cSKevin Wolf 818797ac58cSKevin Wolf if (!Pflag && (lflag || sflag)) { 819b444d0e9SMax Reitz qemuio_command_usage(&read_cmd); 820b32d7a39SMax Reitz return -EINVAL; 821797ac58cSKevin Wolf } 822797ac58cSKevin Wolf 823797ac58cSKevin Wolf if (!lflag) { 824797ac58cSKevin Wolf pattern_count = count - pattern_offset; 825797ac58cSKevin Wolf } 826797ac58cSKevin Wolf 827797ac58cSKevin Wolf if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) { 828797ac58cSKevin Wolf printf("pattern verification range exceeds end of read data\n"); 829b32d7a39SMax Reitz return -EINVAL; 830797ac58cSKevin Wolf } 831797ac58cSKevin Wolf 832093ea232SEric Blake if (bflag) { 8331bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) { 8341bce6b4cSEric Blake printf("%" PRId64 " is not a sector-aligned value for 'offset'\n", 835797ac58cSKevin Wolf offset); 836b32d7a39SMax Reitz return -EINVAL; 837797ac58cSKevin Wolf } 8381bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) { 8391bce6b4cSEric Blake printf("%"PRId64" is not a sector-aligned value for 'count'\n", 840797ac58cSKevin Wolf count); 841b32d7a39SMax Reitz return -EINVAL; 842797ac58cSKevin Wolf } 843797ac58cSKevin Wolf } 844797ac58cSKevin Wolf 8454c7b7e9bSMax Reitz buf = qemu_io_alloc(blk, count, 0xab); 846797ac58cSKevin Wolf 84750290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 8487b3f9712SEric Blake if (bflag) { 849b32d7a39SMax Reitz ret = do_load_vmstate(blk, buf, offset, count, &total); 850797ac58cSKevin Wolf } else { 851b32d7a39SMax Reitz ret = do_pread(blk, buf, offset, count, &total); 852797ac58cSKevin Wolf } 85350290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 854797ac58cSKevin Wolf 855b32d7a39SMax Reitz if (ret < 0) { 856b32d7a39SMax Reitz printf("read failed: %s\n", strerror(-ret)); 857797ac58cSKevin Wolf goto out; 858797ac58cSKevin Wolf } 859b32d7a39SMax Reitz cnt = ret; 860b32d7a39SMax Reitz 861b32d7a39SMax Reitz ret = 0; 862797ac58cSKevin Wolf 863797ac58cSKevin Wolf if (Pflag) { 864797ac58cSKevin Wolf void *cmp_buf = g_malloc(pattern_count); 865797ac58cSKevin Wolf memset(cmp_buf, pattern, pattern_count); 866797ac58cSKevin Wolf if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) { 867797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 8689b0beaf3SJohn Snow PRId64 ", %"PRId64" bytes\n", 869797ac58cSKevin Wolf offset + pattern_offset, pattern_count); 870b32d7a39SMax Reitz ret = -EINVAL; 871797ac58cSKevin Wolf } 872797ac58cSKevin Wolf g_free(cmp_buf); 873797ac58cSKevin Wolf } 874797ac58cSKevin Wolf 875797ac58cSKevin Wolf if (qflag) { 876797ac58cSKevin Wolf goto out; 877797ac58cSKevin Wolf } 878797ac58cSKevin Wolf 879797ac58cSKevin Wolf if (vflag) { 880797ac58cSKevin Wolf dump_buffer(buf, offset, count); 881797ac58cSKevin Wolf } 882797ac58cSKevin Wolf 883797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 884797ac58cSKevin Wolf t2 = tsub(t2, t1); 885797ac58cSKevin Wolf print_report("read", &t2, offset, count, total, cnt, Cflag); 886797ac58cSKevin Wolf 887797ac58cSKevin Wolf out: 888797ac58cSKevin Wolf qemu_io_free(buf); 889b32d7a39SMax Reitz return ret; 890797ac58cSKevin Wolf } 891797ac58cSKevin Wolf 892797ac58cSKevin Wolf static void readv_help(void) 893797ac58cSKevin Wolf { 894797ac58cSKevin Wolf printf( 895797ac58cSKevin Wolf "\n" 896797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n" 897797ac58cSKevin Wolf "\n" 898797ac58cSKevin Wolf " Example:\n" 899797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" 900797ac58cSKevin Wolf "\n" 901797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 902797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 903797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n" 904797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 905797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 906797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 907797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 908797ac58cSKevin Wolf "\n"); 909797ac58cSKevin Wolf } 910797ac58cSKevin Wolf 911b32d7a39SMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv); 912797ac58cSKevin Wolf 913797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = { 914797ac58cSKevin Wolf .name = "readv", 915797ac58cSKevin Wolf .cfunc = readv_f, 916797ac58cSKevin Wolf .argmin = 2, 917797ac58cSKevin Wolf .argmax = -1, 918797ac58cSKevin Wolf .args = "[-Cqv] [-P pattern] off len [len..]", 919797ac58cSKevin Wolf .oneline = "reads a number of bytes at a specified offset", 920797ac58cSKevin Wolf .help = readv_help, 921797ac58cSKevin Wolf }; 922797ac58cSKevin Wolf 923b32d7a39SMax Reitz static int readv_f(BlockBackend *blk, int argc, char **argv) 924797ac58cSKevin Wolf { 92550290c00SAlex Bennée struct timespec t1, t2; 926dc38852aSEric Blake bool Cflag = false, qflag = false, vflag = false; 927b32d7a39SMax Reitz int c, cnt, ret; 928797ac58cSKevin Wolf char *buf; 929797ac58cSKevin Wolf int64_t offset; 930797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 931797ac58cSKevin Wolf int total = 0; 932797ac58cSKevin Wolf int nr_iov; 933797ac58cSKevin Wolf QEMUIOVector qiov; 934797ac58cSKevin Wolf int pattern = 0; 935dc38852aSEric Blake bool Pflag = false; 936797ac58cSKevin Wolf 937b062ad86SEric Blake while ((c = getopt(argc, argv, "CP:qv")) != -1) { 938797ac58cSKevin Wolf switch (c) { 939797ac58cSKevin Wolf case 'C': 940dc38852aSEric Blake Cflag = true; 941797ac58cSKevin Wolf break; 942797ac58cSKevin Wolf case 'P': 943dc38852aSEric Blake Pflag = true; 944797ac58cSKevin Wolf pattern = parse_pattern(optarg); 945797ac58cSKevin Wolf if (pattern < 0) { 946b32d7a39SMax Reitz return -EINVAL; 947797ac58cSKevin Wolf } 948797ac58cSKevin Wolf break; 949797ac58cSKevin Wolf case 'q': 950dc38852aSEric Blake qflag = true; 951797ac58cSKevin Wolf break; 952797ac58cSKevin Wolf case 'v': 953dc38852aSEric Blake vflag = true; 954797ac58cSKevin Wolf break; 955797ac58cSKevin Wolf default: 956b444d0e9SMax Reitz qemuio_command_usage(&readv_cmd); 957b32d7a39SMax Reitz return -EINVAL; 958797ac58cSKevin Wolf } 959797ac58cSKevin Wolf } 960797ac58cSKevin Wolf 961797ac58cSKevin Wolf if (optind > argc - 2) { 962b444d0e9SMax Reitz qemuio_command_usage(&readv_cmd); 963b32d7a39SMax Reitz return -EINVAL; 964797ac58cSKevin Wolf } 965797ac58cSKevin Wolf 966797ac58cSKevin Wolf 967797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 968797ac58cSKevin Wolf if (offset < 0) { 969a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 970b32d7a39SMax Reitz return offset; 971797ac58cSKevin Wolf } 972797ac58cSKevin Wolf optind++; 973797ac58cSKevin Wolf 974797ac58cSKevin Wolf nr_iov = argc - optind; 9754c7b7e9bSMax Reitz buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab); 976797ac58cSKevin Wolf if (buf == NULL) { 977b32d7a39SMax Reitz return -EINVAL; 978797ac58cSKevin Wolf } 979797ac58cSKevin Wolf 98050290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 981b32d7a39SMax Reitz ret = do_aio_readv(blk, &qiov, offset, &total); 98250290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 983797ac58cSKevin Wolf 984b32d7a39SMax Reitz if (ret < 0) { 985b32d7a39SMax Reitz printf("readv failed: %s\n", strerror(-ret)); 986797ac58cSKevin Wolf goto out; 987797ac58cSKevin Wolf } 988b32d7a39SMax Reitz cnt = ret; 989b32d7a39SMax Reitz 990b32d7a39SMax Reitz ret = 0; 991797ac58cSKevin Wolf 992797ac58cSKevin Wolf if (Pflag) { 993797ac58cSKevin Wolf void *cmp_buf = g_malloc(qiov.size); 994797ac58cSKevin Wolf memset(cmp_buf, pattern, qiov.size); 995797ac58cSKevin Wolf if (memcmp(buf, cmp_buf, qiov.size)) { 996797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 997cf67b692SStefan Weil PRId64 ", %zu bytes\n", offset, qiov.size); 998b32d7a39SMax Reitz ret = -EINVAL; 999797ac58cSKevin Wolf } 1000797ac58cSKevin Wolf g_free(cmp_buf); 1001797ac58cSKevin Wolf } 1002797ac58cSKevin Wolf 1003797ac58cSKevin Wolf if (qflag) { 1004797ac58cSKevin Wolf goto out; 1005797ac58cSKevin Wolf } 1006797ac58cSKevin Wolf 1007797ac58cSKevin Wolf if (vflag) { 1008797ac58cSKevin Wolf dump_buffer(buf, offset, qiov.size); 1009797ac58cSKevin Wolf } 1010797ac58cSKevin Wolf 1011797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1012797ac58cSKevin Wolf t2 = tsub(t2, t1); 1013797ac58cSKevin Wolf print_report("read", &t2, offset, qiov.size, total, cnt, Cflag); 1014797ac58cSKevin Wolf 1015797ac58cSKevin Wolf out: 1016797ac58cSKevin Wolf qemu_iovec_destroy(&qiov); 1017797ac58cSKevin Wolf qemu_io_free(buf); 1018b32d7a39SMax Reitz return ret; 1019797ac58cSKevin Wolf } 1020797ac58cSKevin Wolf 1021797ac58cSKevin Wolf static void write_help(void) 1022797ac58cSKevin Wolf { 1023797ac58cSKevin Wolf printf( 1024797ac58cSKevin Wolf "\n" 1025797ac58cSKevin Wolf " writes a range of bytes from the given offset\n" 1026797ac58cSKevin Wolf "\n" 1027797ac58cSKevin Wolf " Example:\n" 1028797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n" 1029797ac58cSKevin Wolf "\n" 1030797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1031797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1032797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n" 10334c7b7e9bSMax Reitz " -c, -- write compressed data with blk_write_compressed\n" 1034770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n" 1035c6e3f520SKevin Wolf " -n, -- with -z, don't allow slow fallback\n" 1036093ea232SEric Blake " -p, -- ignored for backwards compatibility\n" 1037797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 10384d731510SDenis Plotnikov " -s, -- use a pattern file to fill the write buffer\n" 1039797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1040797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1041c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n" 1042d004bd52SEric Blake " -z, -- write zeroes using blk_co_pwrite_zeroes\n" 1043797ac58cSKevin Wolf "\n"); 1044797ac58cSKevin Wolf } 1045797ac58cSKevin Wolf 1046b32d7a39SMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv); 1047797ac58cSKevin Wolf 1048797ac58cSKevin Wolf static const cmdinfo_t write_cmd = { 1049797ac58cSKevin Wolf .name = "write", 1050797ac58cSKevin Wolf .altname = "w", 1051797ac58cSKevin Wolf .cfunc = write_f, 1052887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1053797ac58cSKevin Wolf .argmin = 2, 1054797ac58cSKevin Wolf .argmax = -1, 10554d731510SDenis Plotnikov .args = "[-bcCfnquz] [-P pattern | -s source_file] off len", 1056797ac58cSKevin Wolf .oneline = "writes a number of bytes at a specified offset", 1057797ac58cSKevin Wolf .help = write_help, 1058797ac58cSKevin Wolf }; 1059797ac58cSKevin Wolf 1060b32d7a39SMax Reitz static int write_f(BlockBackend *blk, int argc, char **argv) 1061797ac58cSKevin Wolf { 106250290c00SAlex Bennée struct timespec t1, t2; 1063093ea232SEric Blake bool Cflag = false, qflag = false, bflag = false; 10644d731510SDenis Plotnikov bool Pflag = false, zflag = false, cflag = false, sflag = false; 1065770e0e0eSEric Blake int flags = 0; 1066b32d7a39SMax Reitz int c, cnt, ret; 1067797ac58cSKevin Wolf char *buf = NULL; 1068797ac58cSKevin Wolf int64_t offset; 10699b0beaf3SJohn Snow int64_t count; 1070797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 10719b0beaf3SJohn Snow int64_t total = 0; 1072797ac58cSKevin Wolf int pattern = 0xcd; 10734d731510SDenis Plotnikov const char *file_name = NULL; 1074797ac58cSKevin Wolf 10754d731510SDenis Plotnikov while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) { 1076797ac58cSKevin Wolf switch (c) { 1077797ac58cSKevin Wolf case 'b': 1078dc38852aSEric Blake bflag = true; 1079797ac58cSKevin Wolf break; 1080797ac58cSKevin Wolf case 'c': 1081dc38852aSEric Blake cflag = true; 1082797ac58cSKevin Wolf break; 1083797ac58cSKevin Wolf case 'C': 1084dc38852aSEric Blake Cflag = true; 1085797ac58cSKevin Wolf break; 1086770e0e0eSEric Blake case 'f': 1087770e0e0eSEric Blake flags |= BDRV_REQ_FUA; 1088770e0e0eSEric Blake break; 1089c6e3f520SKevin Wolf case 'n': 1090c6e3f520SKevin Wolf flags |= BDRV_REQ_NO_FALLBACK; 1091c6e3f520SKevin Wolf break; 1092797ac58cSKevin Wolf case 'p': 1093093ea232SEric Blake /* Ignored for backwards compatibility */ 1094797ac58cSKevin Wolf break; 1095797ac58cSKevin Wolf case 'P': 1096dc38852aSEric Blake Pflag = true; 1097797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1098797ac58cSKevin Wolf if (pattern < 0) { 1099b32d7a39SMax Reitz return -EINVAL; 1100797ac58cSKevin Wolf } 1101797ac58cSKevin Wolf break; 1102797ac58cSKevin Wolf case 'q': 1103dc38852aSEric Blake qflag = true; 1104797ac58cSKevin Wolf break; 11054d731510SDenis Plotnikov case 's': 11064d731510SDenis Plotnikov sflag = true; 11074d731510SDenis Plotnikov file_name = optarg; 11084d731510SDenis Plotnikov break; 1109c2e001ccSEric Blake case 'u': 1110c2e001ccSEric Blake flags |= BDRV_REQ_MAY_UNMAP; 1111c2e001ccSEric Blake break; 1112797ac58cSKevin Wolf case 'z': 1113dc38852aSEric Blake zflag = true; 1114797ac58cSKevin Wolf break; 1115797ac58cSKevin Wolf default: 1116b444d0e9SMax Reitz qemuio_command_usage(&write_cmd); 1117b32d7a39SMax Reitz return -EINVAL; 1118797ac58cSKevin Wolf } 1119797ac58cSKevin Wolf } 1120797ac58cSKevin Wolf 1121797ac58cSKevin Wolf if (optind != argc - 2) { 1122b444d0e9SMax Reitz qemuio_command_usage(&write_cmd); 1123b32d7a39SMax Reitz return -EINVAL; 1124797ac58cSKevin Wolf } 1125797ac58cSKevin Wolf 1126093ea232SEric Blake if (bflag && zflag) { 1127093ea232SEric Blake printf("-b and -z cannot be specified at the same time\n"); 1128b32d7a39SMax Reitz return -EINVAL; 1129797ac58cSKevin Wolf } 1130797ac58cSKevin Wolf 1131770e0e0eSEric Blake if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) { 1132770e0e0eSEric Blake printf("-f and -b or -c cannot be specified at the same time\n"); 1133b32d7a39SMax Reitz return -EINVAL; 1134770e0e0eSEric Blake } 1135770e0e0eSEric Blake 1136c6e3f520SKevin Wolf if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) { 1137c6e3f520SKevin Wolf printf("-n requires -z to be specified\n"); 1138c6e3f520SKevin Wolf return -EINVAL; 1139c6e3f520SKevin Wolf } 1140c6e3f520SKevin Wolf 1141c2e001ccSEric Blake if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) { 1142c2e001ccSEric Blake printf("-u requires -z to be specified\n"); 1143b32d7a39SMax Reitz return -EINVAL; 1144c2e001ccSEric Blake } 1145c2e001ccSEric Blake 11464d731510SDenis Plotnikov if (zflag + Pflag + sflag > 1) { 11474d731510SDenis Plotnikov printf("Only one of -z, -P, and -s " 11484d731510SDenis Plotnikov "can be specified at the same time\n"); 1149b32d7a39SMax Reitz return -EINVAL; 1150797ac58cSKevin Wolf } 1151797ac58cSKevin Wolf 1152797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1153797ac58cSKevin Wolf if (offset < 0) { 1154a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 1155b32d7a39SMax Reitz return offset; 1156797ac58cSKevin Wolf } 1157797ac58cSKevin Wolf 1158797ac58cSKevin Wolf optind++; 1159797ac58cSKevin Wolf count = cvtnum(argv[optind]); 1160797ac58cSKevin Wolf if (count < 0) { 1161a9ecfa00SJohn Snow print_cvtnum_err(count, argv[optind]); 1162b32d7a39SMax Reitz return count; 11633026c468SAlberto Garcia } else if (count > BDRV_REQUEST_MAX_BYTES) { 11649b0beaf3SJohn Snow printf("length cannot exceed %" PRIu64 ", given %s\n", 11653026c468SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]); 1166b32d7a39SMax Reitz return -EINVAL; 1167797ac58cSKevin Wolf } 1168797ac58cSKevin Wolf 1169093ea232SEric Blake if (bflag || cflag) { 11701bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) { 11711bce6b4cSEric Blake printf("%" PRId64 " is not a sector-aligned value for 'offset'\n", 1172797ac58cSKevin Wolf offset); 1173b32d7a39SMax Reitz return -EINVAL; 1174797ac58cSKevin Wolf } 1175797ac58cSKevin Wolf 11761bce6b4cSEric Blake if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) { 11771bce6b4cSEric Blake printf("%"PRId64" is not a sector-aligned value for 'count'\n", 1178797ac58cSKevin Wolf count); 1179b32d7a39SMax Reitz return -EINVAL; 1180797ac58cSKevin Wolf } 1181797ac58cSKevin Wolf } 1182797ac58cSKevin Wolf 1183797ac58cSKevin Wolf if (!zflag) { 11844d731510SDenis Plotnikov if (sflag) { 11854d731510SDenis Plotnikov buf = qemu_io_alloc_from_file(blk, count, file_name); 11864d731510SDenis Plotnikov if (!buf) { 11874d731510SDenis Plotnikov return -EINVAL; 11884d731510SDenis Plotnikov } 11894d731510SDenis Plotnikov } else { 11904c7b7e9bSMax Reitz buf = qemu_io_alloc(blk, count, pattern); 1191797ac58cSKevin Wolf } 11924d731510SDenis Plotnikov } 1193797ac58cSKevin Wolf 119450290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 11957b3f9712SEric Blake if (bflag) { 1196b32d7a39SMax Reitz ret = do_save_vmstate(blk, buf, offset, count, &total); 1197797ac58cSKevin Wolf } else if (zflag) { 1198b32d7a39SMax Reitz ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total); 1199797ac58cSKevin Wolf } else if (cflag) { 1200b32d7a39SMax Reitz ret = do_write_compressed(blk, buf, offset, count, &total); 1201797ac58cSKevin Wolf } else { 1202b32d7a39SMax Reitz ret = do_pwrite(blk, buf, offset, count, flags, &total); 1203797ac58cSKevin Wolf } 120450290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1205797ac58cSKevin Wolf 1206b32d7a39SMax Reitz if (ret < 0) { 1207b32d7a39SMax Reitz printf("write failed: %s\n", strerror(-ret)); 1208797ac58cSKevin Wolf goto out; 1209797ac58cSKevin Wolf } 1210b32d7a39SMax Reitz cnt = ret; 1211b32d7a39SMax Reitz 1212b32d7a39SMax Reitz ret = 0; 1213797ac58cSKevin Wolf 1214797ac58cSKevin Wolf if (qflag) { 1215797ac58cSKevin Wolf goto out; 1216797ac58cSKevin Wolf } 1217797ac58cSKevin Wolf 1218797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1219797ac58cSKevin Wolf t2 = tsub(t2, t1); 1220797ac58cSKevin Wolf print_report("wrote", &t2, offset, count, total, cnt, Cflag); 1221797ac58cSKevin Wolf 1222797ac58cSKevin Wolf out: 1223797ac58cSKevin Wolf if (!zflag) { 1224797ac58cSKevin Wolf qemu_io_free(buf); 1225797ac58cSKevin Wolf } 1226b32d7a39SMax Reitz return ret; 1227797ac58cSKevin Wolf } 1228797ac58cSKevin Wolf 1229797ac58cSKevin Wolf static void 1230797ac58cSKevin Wolf writev_help(void) 1231797ac58cSKevin Wolf { 1232797ac58cSKevin Wolf printf( 1233797ac58cSKevin Wolf "\n" 1234797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n" 1235797ac58cSKevin Wolf "\n" 1236797ac58cSKevin Wolf " Example:\n" 12376e6507c0SMaria Kustova " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" 1238797ac58cSKevin Wolf "\n" 1239797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1240797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1241797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 1242797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1243770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n" 1244797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1245797ac58cSKevin Wolf "\n"); 1246797ac58cSKevin Wolf } 1247797ac58cSKevin Wolf 1248b32d7a39SMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv); 1249797ac58cSKevin Wolf 1250797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = { 1251797ac58cSKevin Wolf .name = "writev", 1252797ac58cSKevin Wolf .cfunc = writev_f, 1253887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1254797ac58cSKevin Wolf .argmin = 2, 1255797ac58cSKevin Wolf .argmax = -1, 1256770e0e0eSEric Blake .args = "[-Cfq] [-P pattern] off len [len..]", 1257797ac58cSKevin Wolf .oneline = "writes a number of bytes at a specified offset", 1258797ac58cSKevin Wolf .help = writev_help, 1259797ac58cSKevin Wolf }; 1260797ac58cSKevin Wolf 1261b32d7a39SMax Reitz static int writev_f(BlockBackend *blk, int argc, char **argv) 1262797ac58cSKevin Wolf { 126350290c00SAlex Bennée struct timespec t1, t2; 1264dc38852aSEric Blake bool Cflag = false, qflag = false; 1265770e0e0eSEric Blake int flags = 0; 1266b32d7a39SMax Reitz int c, cnt, ret; 1267797ac58cSKevin Wolf char *buf; 1268797ac58cSKevin Wolf int64_t offset; 1269797ac58cSKevin Wolf /* Some compilers get confused and warn if this is not initialized. */ 1270797ac58cSKevin Wolf int total = 0; 1271797ac58cSKevin Wolf int nr_iov; 1272797ac58cSKevin Wolf int pattern = 0xcd; 1273797ac58cSKevin Wolf QEMUIOVector qiov; 1274797ac58cSKevin Wolf 12754ca1d340SEric Blake while ((c = getopt(argc, argv, "CfqP:")) != -1) { 1276797ac58cSKevin Wolf switch (c) { 1277797ac58cSKevin Wolf case 'C': 1278dc38852aSEric Blake Cflag = true; 1279797ac58cSKevin Wolf break; 1280770e0e0eSEric Blake case 'f': 1281770e0e0eSEric Blake flags |= BDRV_REQ_FUA; 1282770e0e0eSEric Blake break; 1283797ac58cSKevin Wolf case 'q': 1284dc38852aSEric Blake qflag = true; 1285797ac58cSKevin Wolf break; 1286797ac58cSKevin Wolf case 'P': 1287797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1288797ac58cSKevin Wolf if (pattern < 0) { 1289b32d7a39SMax Reitz return -EINVAL; 1290797ac58cSKevin Wolf } 1291797ac58cSKevin Wolf break; 1292797ac58cSKevin Wolf default: 1293b444d0e9SMax Reitz qemuio_command_usage(&writev_cmd); 1294b32d7a39SMax Reitz return -EINVAL; 1295797ac58cSKevin Wolf } 1296797ac58cSKevin Wolf } 1297797ac58cSKevin Wolf 1298797ac58cSKevin Wolf if (optind > argc - 2) { 1299b444d0e9SMax Reitz qemuio_command_usage(&writev_cmd); 1300b32d7a39SMax Reitz return -EINVAL; 1301797ac58cSKevin Wolf } 1302797ac58cSKevin Wolf 1303797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1304797ac58cSKevin Wolf if (offset < 0) { 1305a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 1306b32d7a39SMax Reitz return offset; 1307797ac58cSKevin Wolf } 1308797ac58cSKevin Wolf optind++; 1309797ac58cSKevin Wolf 1310797ac58cSKevin Wolf nr_iov = argc - optind; 13114c7b7e9bSMax Reitz buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern); 1312797ac58cSKevin Wolf if (buf == NULL) { 1313b32d7a39SMax Reitz return -EINVAL; 1314797ac58cSKevin Wolf } 1315797ac58cSKevin Wolf 131650290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 1317b32d7a39SMax Reitz ret = do_aio_writev(blk, &qiov, offset, flags, &total); 131850290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1319797ac58cSKevin Wolf 1320b32d7a39SMax Reitz if (ret < 0) { 1321b32d7a39SMax Reitz printf("writev failed: %s\n", strerror(-ret)); 1322797ac58cSKevin Wolf goto out; 1323797ac58cSKevin Wolf } 1324b32d7a39SMax Reitz cnt = ret; 1325b32d7a39SMax Reitz 1326b32d7a39SMax Reitz ret = 0; 1327797ac58cSKevin Wolf 1328797ac58cSKevin Wolf if (qflag) { 1329797ac58cSKevin Wolf goto out; 1330797ac58cSKevin Wolf } 1331797ac58cSKevin Wolf 1332797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1333797ac58cSKevin Wolf t2 = tsub(t2, t1); 1334797ac58cSKevin Wolf print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag); 1335797ac58cSKevin Wolf out: 1336797ac58cSKevin Wolf qemu_iovec_destroy(&qiov); 1337797ac58cSKevin Wolf qemu_io_free(buf); 1338b32d7a39SMax Reitz return ret; 1339797ac58cSKevin Wolf } 1340797ac58cSKevin Wolf 1341797ac58cSKevin Wolf struct aio_ctx { 13424c7b7e9bSMax Reitz BlockBackend *blk; 1343797ac58cSKevin Wolf QEMUIOVector qiov; 1344797ac58cSKevin Wolf int64_t offset; 1345797ac58cSKevin Wolf char *buf; 1346dc38852aSEric Blake bool qflag; 1347dc38852aSEric Blake bool vflag; 1348dc38852aSEric Blake bool Cflag; 1349dc38852aSEric Blake bool Pflag; 1350dc38852aSEric Blake bool zflag; 1351a91f9584SFam Zheng BlockAcctCookie acct; 1352797ac58cSKevin Wolf int pattern; 135350290c00SAlex Bennée struct timespec t1; 1354797ac58cSKevin Wolf }; 1355797ac58cSKevin Wolf 1356797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret) 1357797ac58cSKevin Wolf { 1358797ac58cSKevin Wolf struct aio_ctx *ctx = opaque; 135950290c00SAlex Bennée struct timespec t2; 1360797ac58cSKevin Wolf 136150290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1362797ac58cSKevin Wolf 1363797ac58cSKevin Wolf 1364797ac58cSKevin Wolf if (ret < 0) { 1365797ac58cSKevin Wolf printf("aio_write failed: %s\n", strerror(-ret)); 1366556c2b60SAlberto Garcia block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct); 1367797ac58cSKevin Wolf goto out; 1368797ac58cSKevin Wolf } 1369797ac58cSKevin Wolf 13704c7b7e9bSMax Reitz block_acct_done(blk_get_stats(ctx->blk), &ctx->acct); 1371a91f9584SFam Zheng 1372797ac58cSKevin Wolf if (ctx->qflag) { 1373797ac58cSKevin Wolf goto out; 1374797ac58cSKevin Wolf } 1375797ac58cSKevin Wolf 1376797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1377797ac58cSKevin Wolf t2 = tsub(t2, ctx->t1); 1378797ac58cSKevin Wolf print_report("wrote", &t2, ctx->offset, ctx->qiov.size, 1379797ac58cSKevin Wolf ctx->qiov.size, 1, ctx->Cflag); 1380797ac58cSKevin Wolf out: 13815ceb7765SKevin Wolf if (!ctx->zflag) { 1382797ac58cSKevin Wolf qemu_io_free(ctx->buf); 1383797ac58cSKevin Wolf qemu_iovec_destroy(&ctx->qiov); 13845ceb7765SKevin Wolf } 1385797ac58cSKevin Wolf g_free(ctx); 1386797ac58cSKevin Wolf } 1387797ac58cSKevin Wolf 1388797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret) 1389797ac58cSKevin Wolf { 1390797ac58cSKevin Wolf struct aio_ctx *ctx = opaque; 139150290c00SAlex Bennée struct timespec t2; 1392797ac58cSKevin Wolf 139350290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1394797ac58cSKevin Wolf 1395797ac58cSKevin Wolf if (ret < 0) { 1396797ac58cSKevin Wolf printf("readv failed: %s\n", strerror(-ret)); 1397556c2b60SAlberto Garcia block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct); 1398797ac58cSKevin Wolf goto out; 1399797ac58cSKevin Wolf } 1400797ac58cSKevin Wolf 1401797ac58cSKevin Wolf if (ctx->Pflag) { 1402797ac58cSKevin Wolf void *cmp_buf = g_malloc(ctx->qiov.size); 1403797ac58cSKevin Wolf 1404797ac58cSKevin Wolf memset(cmp_buf, ctx->pattern, ctx->qiov.size); 1405797ac58cSKevin Wolf if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) { 1406797ac58cSKevin Wolf printf("Pattern verification failed at offset %" 1407cf67b692SStefan Weil PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size); 1408797ac58cSKevin Wolf } 1409797ac58cSKevin Wolf g_free(cmp_buf); 1410797ac58cSKevin Wolf } 1411797ac58cSKevin Wolf 14124c7b7e9bSMax Reitz block_acct_done(blk_get_stats(ctx->blk), &ctx->acct); 1413a91f9584SFam Zheng 1414797ac58cSKevin Wolf if (ctx->qflag) { 1415797ac58cSKevin Wolf goto out; 1416797ac58cSKevin Wolf } 1417797ac58cSKevin Wolf 1418797ac58cSKevin Wolf if (ctx->vflag) { 1419797ac58cSKevin Wolf dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); 1420797ac58cSKevin Wolf } 1421797ac58cSKevin Wolf 1422797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1423797ac58cSKevin Wolf t2 = tsub(t2, ctx->t1); 1424797ac58cSKevin Wolf print_report("read", &t2, ctx->offset, ctx->qiov.size, 1425797ac58cSKevin Wolf ctx->qiov.size, 1, ctx->Cflag); 1426797ac58cSKevin Wolf out: 1427797ac58cSKevin Wolf qemu_io_free(ctx->buf); 1428797ac58cSKevin Wolf qemu_iovec_destroy(&ctx->qiov); 1429797ac58cSKevin Wolf g_free(ctx); 1430797ac58cSKevin Wolf } 1431797ac58cSKevin Wolf 1432797ac58cSKevin Wolf static void aio_read_help(void) 1433797ac58cSKevin Wolf { 1434797ac58cSKevin Wolf printf( 1435797ac58cSKevin Wolf "\n" 1436797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n" 1437797ac58cSKevin Wolf "\n" 1438797ac58cSKevin Wolf " Example:\n" 1439797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" 1440797ac58cSKevin Wolf "\n" 1441797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n" 1442797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n" 1443797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n" 1444797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n" 1445b32d7a39SMax Reitz " Note that due to its asynchronous nature, this command will be\n" 1446b32d7a39SMax Reitz " considered successful once the request is submitted, independently\n" 1447b32d7a39SMax Reitz " of potential I/O errors or pattern mismatches.\n" 1448797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1449797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n" 145037546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n" 1451797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n" 1452797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1453797ac58cSKevin Wolf "\n"); 1454797ac58cSKevin Wolf } 1455797ac58cSKevin Wolf 1456b32d7a39SMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv); 1457797ac58cSKevin Wolf 1458797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = { 1459797ac58cSKevin Wolf .name = "aio_read", 1460797ac58cSKevin Wolf .cfunc = aio_read_f, 1461797ac58cSKevin Wolf .argmin = 2, 1462797ac58cSKevin Wolf .argmax = -1, 146337546ff2SEric Blake .args = "[-Ciqv] [-P pattern] off len [len..]", 1464797ac58cSKevin Wolf .oneline = "asynchronously reads a number of bytes", 1465797ac58cSKevin Wolf .help = aio_read_help, 1466797ac58cSKevin Wolf }; 1467797ac58cSKevin Wolf 1468b32d7a39SMax Reitz static int aio_read_f(BlockBackend *blk, int argc, char **argv) 1469797ac58cSKevin Wolf { 1470797ac58cSKevin Wolf int nr_iov, c; 1471797ac58cSKevin Wolf struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); 1472797ac58cSKevin Wolf 14734c7b7e9bSMax Reitz ctx->blk = blk; 147437546ff2SEric Blake while ((c = getopt(argc, argv, "CP:iqv")) != -1) { 1475797ac58cSKevin Wolf switch (c) { 1476797ac58cSKevin Wolf case 'C': 1477dc38852aSEric Blake ctx->Cflag = true; 1478797ac58cSKevin Wolf break; 1479797ac58cSKevin Wolf case 'P': 1480dc38852aSEric Blake ctx->Pflag = true; 1481797ac58cSKevin Wolf ctx->pattern = parse_pattern(optarg); 1482797ac58cSKevin Wolf if (ctx->pattern < 0) { 1483797ac58cSKevin Wolf g_free(ctx); 1484b32d7a39SMax Reitz return -EINVAL; 1485797ac58cSKevin Wolf } 1486797ac58cSKevin Wolf break; 148737546ff2SEric Blake case 'i': 148837546ff2SEric Blake printf("injecting invalid read request\n"); 148937546ff2SEric Blake block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ); 149037546ff2SEric Blake g_free(ctx); 1491b32d7a39SMax Reitz return 0; 1492797ac58cSKevin Wolf case 'q': 1493dc38852aSEric Blake ctx->qflag = true; 1494797ac58cSKevin Wolf break; 1495797ac58cSKevin Wolf case 'v': 1496dc38852aSEric Blake ctx->vflag = true; 1497797ac58cSKevin Wolf break; 1498797ac58cSKevin Wolf default: 1499797ac58cSKevin Wolf g_free(ctx); 1500b444d0e9SMax Reitz qemuio_command_usage(&aio_read_cmd); 1501b32d7a39SMax Reitz return -EINVAL; 1502797ac58cSKevin Wolf } 1503797ac58cSKevin Wolf } 1504797ac58cSKevin Wolf 1505797ac58cSKevin Wolf if (optind > argc - 2) { 1506797ac58cSKevin Wolf g_free(ctx); 1507b444d0e9SMax Reitz qemuio_command_usage(&aio_read_cmd); 1508b32d7a39SMax Reitz return -EINVAL; 1509797ac58cSKevin Wolf } 1510797ac58cSKevin Wolf 1511797ac58cSKevin Wolf ctx->offset = cvtnum(argv[optind]); 1512797ac58cSKevin Wolf if (ctx->offset < 0) { 1513b32d7a39SMax Reitz int ret = ctx->offset; 1514b32d7a39SMax Reitz print_cvtnum_err(ret, argv[optind]); 1515797ac58cSKevin Wolf g_free(ctx); 1516b32d7a39SMax Reitz return ret; 1517797ac58cSKevin Wolf } 1518797ac58cSKevin Wolf optind++; 1519797ac58cSKevin Wolf 1520797ac58cSKevin Wolf nr_iov = argc - optind; 15214c7b7e9bSMax Reitz ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab); 1522797ac58cSKevin Wolf if (ctx->buf == NULL) { 1523556c2b60SAlberto Garcia block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ); 1524797ac58cSKevin Wolf g_free(ctx); 1525b32d7a39SMax Reitz return -EINVAL; 1526797ac58cSKevin Wolf } 1527797ac58cSKevin Wolf 152850290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &ctx->t1); 15294c7b7e9bSMax Reitz block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size, 15304c7b7e9bSMax Reitz BLOCK_ACCT_READ); 15317b3f9712SEric Blake blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx); 1532b32d7a39SMax Reitz return 0; 1533797ac58cSKevin Wolf } 1534797ac58cSKevin Wolf 1535797ac58cSKevin Wolf static void aio_write_help(void) 1536797ac58cSKevin Wolf { 1537797ac58cSKevin Wolf printf( 1538797ac58cSKevin Wolf "\n" 1539797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n" 1540797ac58cSKevin Wolf " from multiple buffers\n" 1541797ac58cSKevin Wolf "\n" 1542797ac58cSKevin Wolf " Example:\n" 1543797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" 1544797ac58cSKevin Wolf "\n" 1545797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n" 1546797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n" 1547797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n" 1548797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n" 1549b32d7a39SMax Reitz " Note that due to its asynchronous nature, this command will be\n" 1550b32d7a39SMax Reitz " considered successful once the request is submitted, independently\n" 1551b32d7a39SMax Reitz " of potential I/O errors or pattern mismatches.\n" 1552797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n" 1553797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1554770e0e0eSEric Blake " -f, -- use Force Unit Access semantics\n" 155537546ff2SEric Blake " -i, -- treat request as invalid, for exercising stats\n" 1556797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1557c2e001ccSEric Blake " -u, -- with -z, allow unmapping\n" 1558d004bd52SEric Blake " -z, -- write zeroes using blk_aio_pwrite_zeroes\n" 1559797ac58cSKevin Wolf "\n"); 1560797ac58cSKevin Wolf } 1561797ac58cSKevin Wolf 1562b32d7a39SMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv); 1563797ac58cSKevin Wolf 1564797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = { 1565797ac58cSKevin Wolf .name = "aio_write", 1566797ac58cSKevin Wolf .cfunc = aio_write_f, 1567887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1568797ac58cSKevin Wolf .argmin = 2, 1569797ac58cSKevin Wolf .argmax = -1, 157037546ff2SEric Blake .args = "[-Cfiquz] [-P pattern] off len [len..]", 1571797ac58cSKevin Wolf .oneline = "asynchronously writes a number of bytes", 1572797ac58cSKevin Wolf .help = aio_write_help, 1573797ac58cSKevin Wolf }; 1574797ac58cSKevin Wolf 1575b32d7a39SMax Reitz static int aio_write_f(BlockBackend *blk, int argc, char **argv) 1576797ac58cSKevin Wolf { 1577797ac58cSKevin Wolf int nr_iov, c; 1578797ac58cSKevin Wolf int pattern = 0xcd; 1579797ac58cSKevin Wolf struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); 1580770e0e0eSEric Blake int flags = 0; 1581797ac58cSKevin Wolf 15824c7b7e9bSMax Reitz ctx->blk = blk; 158337546ff2SEric Blake while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) { 1584797ac58cSKevin Wolf switch (c) { 1585797ac58cSKevin Wolf case 'C': 1586dc38852aSEric Blake ctx->Cflag = true; 1587797ac58cSKevin Wolf break; 1588770e0e0eSEric Blake case 'f': 1589770e0e0eSEric Blake flags |= BDRV_REQ_FUA; 1590770e0e0eSEric Blake break; 1591797ac58cSKevin Wolf case 'q': 1592dc38852aSEric Blake ctx->qflag = true; 1593797ac58cSKevin Wolf break; 1594c2e001ccSEric Blake case 'u': 1595c2e001ccSEric Blake flags |= BDRV_REQ_MAY_UNMAP; 1596c2e001ccSEric Blake break; 1597797ac58cSKevin Wolf case 'P': 1598797ac58cSKevin Wolf pattern = parse_pattern(optarg); 1599797ac58cSKevin Wolf if (pattern < 0) { 1600797ac58cSKevin Wolf g_free(ctx); 1601b32d7a39SMax Reitz return -EINVAL; 1602797ac58cSKevin Wolf } 1603797ac58cSKevin Wolf break; 160437546ff2SEric Blake case 'i': 160537546ff2SEric Blake printf("injecting invalid write request\n"); 160637546ff2SEric Blake block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE); 160737546ff2SEric Blake g_free(ctx); 1608b32d7a39SMax Reitz return 0; 16095ceb7765SKevin Wolf case 'z': 1610dc38852aSEric Blake ctx->zflag = true; 16115ceb7765SKevin Wolf break; 1612797ac58cSKevin Wolf default: 1613797ac58cSKevin Wolf g_free(ctx); 1614b444d0e9SMax Reitz qemuio_command_usage(&aio_write_cmd); 1615b32d7a39SMax Reitz return -EINVAL; 1616797ac58cSKevin Wolf } 1617797ac58cSKevin Wolf } 1618797ac58cSKevin Wolf 1619797ac58cSKevin Wolf if (optind > argc - 2) { 1620797ac58cSKevin Wolf g_free(ctx); 1621b444d0e9SMax Reitz qemuio_command_usage(&aio_write_cmd); 1622b32d7a39SMax Reitz return -EINVAL; 1623797ac58cSKevin Wolf } 1624797ac58cSKevin Wolf 16255ceb7765SKevin Wolf if (ctx->zflag && optind != argc - 2) { 16265ceb7765SKevin Wolf printf("-z supports only a single length parameter\n"); 16275ceb7765SKevin Wolf g_free(ctx); 1628b32d7a39SMax Reitz return -EINVAL; 16295ceb7765SKevin Wolf } 16305ceb7765SKevin Wolf 1631c2e001ccSEric Blake if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) { 1632c2e001ccSEric Blake printf("-u requires -z to be specified\n"); 16334ca1d340SEric Blake g_free(ctx); 1634b32d7a39SMax Reitz return -EINVAL; 1635c2e001ccSEric Blake } 1636c2e001ccSEric Blake 16375ceb7765SKevin Wolf if (ctx->zflag && ctx->Pflag) { 16385ceb7765SKevin Wolf printf("-z and -P cannot be specified at the same time\n"); 16395ceb7765SKevin Wolf g_free(ctx); 1640b32d7a39SMax Reitz return -EINVAL; 16415ceb7765SKevin Wolf } 16425ceb7765SKevin Wolf 1643797ac58cSKevin Wolf ctx->offset = cvtnum(argv[optind]); 1644797ac58cSKevin Wolf if (ctx->offset < 0) { 1645b32d7a39SMax Reitz int ret = ctx->offset; 1646b32d7a39SMax Reitz print_cvtnum_err(ret, argv[optind]); 1647797ac58cSKevin Wolf g_free(ctx); 1648b32d7a39SMax Reitz return ret; 1649797ac58cSKevin Wolf } 1650797ac58cSKevin Wolf optind++; 1651797ac58cSKevin Wolf 16525ceb7765SKevin Wolf if (ctx->zflag) { 16535ceb7765SKevin Wolf int64_t count = cvtnum(argv[optind]); 16545ceb7765SKevin Wolf if (count < 0) { 16555ceb7765SKevin Wolf print_cvtnum_err(count, argv[optind]); 16560e01b76eSKevin Wolf g_free(ctx); 1657b32d7a39SMax Reitz return count; 16585ceb7765SKevin Wolf } 16595ceb7765SKevin Wolf 16605ceb7765SKevin Wolf ctx->qiov.size = count; 1661d004bd52SEric Blake blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done, 1662770e0e0eSEric Blake ctx); 16635ceb7765SKevin Wolf } else { 1664797ac58cSKevin Wolf nr_iov = argc - optind; 16655ceb7765SKevin Wolf ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 16665ceb7765SKevin Wolf pattern); 1667797ac58cSKevin Wolf if (ctx->buf == NULL) { 1668556c2b60SAlberto Garcia block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE); 1669797ac58cSKevin Wolf g_free(ctx); 1670b32d7a39SMax Reitz return -EINVAL; 1671797ac58cSKevin Wolf } 1672797ac58cSKevin Wolf 167350290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &ctx->t1); 16744c7b7e9bSMax Reitz block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size, 16754c7b7e9bSMax Reitz BLOCK_ACCT_WRITE); 16765ceb7765SKevin Wolf 1677770e0e0eSEric Blake blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done, 1678770e0e0eSEric Blake ctx); 16795ceb7765SKevin Wolf } 1680b32d7a39SMax Reitz 1681b32d7a39SMax Reitz return 0; 1682797ac58cSKevin Wolf } 1683797ac58cSKevin Wolf 1684b32d7a39SMax Reitz static int aio_flush_f(BlockBackend *blk, int argc, char **argv) 1685797ac58cSKevin Wolf { 1686556c2b60SAlberto Garcia BlockAcctCookie cookie; 1687556c2b60SAlberto Garcia block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH); 16884c7b7e9bSMax Reitz blk_drain_all(); 1689556c2b60SAlberto Garcia block_acct_done(blk_get_stats(blk), &cookie); 1690b32d7a39SMax Reitz return 0; 1691797ac58cSKevin Wolf } 1692797ac58cSKevin Wolf 1693797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = { 1694797ac58cSKevin Wolf .name = "aio_flush", 1695797ac58cSKevin Wolf .cfunc = aio_flush_f, 1696797ac58cSKevin Wolf .oneline = "completes all outstanding aio requests" 1697797ac58cSKevin Wolf }; 1698797ac58cSKevin Wolf 1699b32d7a39SMax Reitz static int flush_f(BlockBackend *blk, int argc, char **argv) 1700797ac58cSKevin Wolf { 1701b32d7a39SMax Reitz return blk_flush(blk); 1702797ac58cSKevin Wolf } 1703797ac58cSKevin Wolf 1704797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = { 1705797ac58cSKevin Wolf .name = "flush", 1706797ac58cSKevin Wolf .altname = "f", 1707797ac58cSKevin Wolf .cfunc = flush_f, 1708797ac58cSKevin Wolf .oneline = "flush all in-core file state to disk", 1709797ac58cSKevin Wolf }; 1710797ac58cSKevin Wolf 171142ba0225SVladimir Sementsov-Ogievskiy static int truncate_f(BlockBackend *blk, int argc, char **argv); 171242ba0225SVladimir Sementsov-Ogievskiy static const cmdinfo_t truncate_cmd = { 171342ba0225SVladimir Sementsov-Ogievskiy .name = "truncate", 171442ba0225SVladimir Sementsov-Ogievskiy .altname = "t", 171542ba0225SVladimir Sementsov-Ogievskiy .cfunc = truncate_f, 171642ba0225SVladimir Sementsov-Ogievskiy .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE, 171742ba0225SVladimir Sementsov-Ogievskiy .argmin = 1, 171842ba0225SVladimir Sementsov-Ogievskiy .argmax = 3, 171942ba0225SVladimir Sementsov-Ogievskiy .args = "[-m prealloc_mode] off", 172042ba0225SVladimir Sementsov-Ogievskiy .oneline = "truncates the current file at the given offset", 172142ba0225SVladimir Sementsov-Ogievskiy }; 172242ba0225SVladimir Sementsov-Ogievskiy 1723b32d7a39SMax Reitz static int truncate_f(BlockBackend *blk, int argc, char **argv) 1724797ac58cSKevin Wolf { 1725ed3d2ec9SMax Reitz Error *local_err = NULL; 1726797ac58cSKevin Wolf int64_t offset; 172742ba0225SVladimir Sementsov-Ogievskiy int c, ret; 172842ba0225SVladimir Sementsov-Ogievskiy PreallocMode prealloc = PREALLOC_MODE_OFF; 1729797ac58cSKevin Wolf 173042ba0225SVladimir Sementsov-Ogievskiy while ((c = getopt(argc, argv, "m:")) != -1) { 173142ba0225SVladimir Sementsov-Ogievskiy switch (c) { 173242ba0225SVladimir Sementsov-Ogievskiy case 'm': 173342ba0225SVladimir Sementsov-Ogievskiy prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, 173442ba0225SVladimir Sementsov-Ogievskiy PREALLOC_MODE__MAX, NULL); 173542ba0225SVladimir Sementsov-Ogievskiy if (prealloc == PREALLOC_MODE__MAX) { 173642ba0225SVladimir Sementsov-Ogievskiy error_report("Invalid preallocation mode '%s'", optarg); 173742ba0225SVladimir Sementsov-Ogievskiy return -EINVAL; 173842ba0225SVladimir Sementsov-Ogievskiy } 173942ba0225SVladimir Sementsov-Ogievskiy break; 174042ba0225SVladimir Sementsov-Ogievskiy default: 174142ba0225SVladimir Sementsov-Ogievskiy qemuio_command_usage(&truncate_cmd); 174242ba0225SVladimir Sementsov-Ogievskiy return -EINVAL; 174342ba0225SVladimir Sementsov-Ogievskiy } 174442ba0225SVladimir Sementsov-Ogievskiy } 174542ba0225SVladimir Sementsov-Ogievskiy 174642ba0225SVladimir Sementsov-Ogievskiy offset = cvtnum(argv[optind]); 1747797ac58cSKevin Wolf if (offset < 0) { 1748a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[1]); 1749b32d7a39SMax Reitz return offset; 1750797ac58cSKevin Wolf } 1751797ac58cSKevin Wolf 1752e8d04f92SMax Reitz /* 1753e8d04f92SMax Reitz * qemu-io is a debugging tool, so let us be strict here and pass 1754e8d04f92SMax Reitz * exact=true. It is better to err on the "emit more errors" side 1755e8d04f92SMax Reitz * than to be overly permissive. 1756e8d04f92SMax Reitz */ 175742ba0225SVladimir Sementsov-Ogievskiy ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err); 1758797ac58cSKevin Wolf if (ret < 0) { 1759ed3d2ec9SMax Reitz error_report_err(local_err); 1760b32d7a39SMax Reitz return ret; 1761797ac58cSKevin Wolf } 1762b32d7a39SMax Reitz 1763b32d7a39SMax Reitz return 0; 1764797ac58cSKevin Wolf } 1765797ac58cSKevin Wolf 1766b32d7a39SMax Reitz static int length_f(BlockBackend *blk, int argc, char **argv) 1767797ac58cSKevin Wolf { 1768797ac58cSKevin Wolf int64_t size; 1769797ac58cSKevin Wolf char s1[64]; 1770797ac58cSKevin Wolf 17714c7b7e9bSMax Reitz size = blk_getlength(blk); 1772797ac58cSKevin Wolf if (size < 0) { 1773797ac58cSKevin Wolf printf("getlength: %s\n", strerror(-size)); 1774b32d7a39SMax Reitz return size; 1775797ac58cSKevin Wolf } 1776797ac58cSKevin Wolf 1777797ac58cSKevin Wolf cvtstr(size, s1, sizeof(s1)); 1778797ac58cSKevin Wolf printf("%s\n", s1); 1779b32d7a39SMax Reitz return 0; 1780797ac58cSKevin Wolf } 1781797ac58cSKevin Wolf 1782797ac58cSKevin Wolf 1783797ac58cSKevin Wolf static const cmdinfo_t length_cmd = { 1784797ac58cSKevin Wolf .name = "length", 1785797ac58cSKevin Wolf .altname = "l", 1786797ac58cSKevin Wolf .cfunc = length_f, 1787797ac58cSKevin Wolf .oneline = "gets the length of the current file", 1788797ac58cSKevin Wolf }; 1789797ac58cSKevin Wolf 1790797ac58cSKevin Wolf 1791b32d7a39SMax Reitz static int info_f(BlockBackend *blk, int argc, char **argv) 1792797ac58cSKevin Wolf { 17934c7b7e9bSMax Reitz BlockDriverState *bs = blk_bs(blk); 1794797ac58cSKevin Wolf BlockDriverInfo bdi; 1795a8d8ecb7SMax Reitz ImageInfoSpecific *spec_info; 17961bf6e9caSAndrey Shinkevich Error *local_err = NULL; 1797797ac58cSKevin Wolf char s1[64], s2[64]; 1798797ac58cSKevin Wolf int ret; 1799797ac58cSKevin Wolf 1800797ac58cSKevin Wolf if (bs->drv && bs->drv->format_name) { 1801797ac58cSKevin Wolf printf("format name: %s\n", bs->drv->format_name); 1802797ac58cSKevin Wolf } 1803797ac58cSKevin Wolf if (bs->drv && bs->drv->protocol_name) { 1804797ac58cSKevin Wolf printf("format name: %s\n", bs->drv->protocol_name); 1805797ac58cSKevin Wolf } 1806797ac58cSKevin Wolf 1807797ac58cSKevin Wolf ret = bdrv_get_info(bs, &bdi); 1808797ac58cSKevin Wolf if (ret) { 1809b32d7a39SMax Reitz return ret; 1810797ac58cSKevin Wolf } 1811797ac58cSKevin Wolf 1812797ac58cSKevin Wolf cvtstr(bdi.cluster_size, s1, sizeof(s1)); 1813797ac58cSKevin Wolf cvtstr(bdi.vm_state_offset, s2, sizeof(s2)); 1814797ac58cSKevin Wolf 1815797ac58cSKevin Wolf printf("cluster size: %s\n", s1); 1816797ac58cSKevin Wolf printf("vm state offset: %s\n", s2); 1817797ac58cSKevin Wolf 18181bf6e9caSAndrey Shinkevich spec_info = bdrv_get_specific_info(bs, &local_err); 18191bf6e9caSAndrey Shinkevich if (local_err) { 18201bf6e9caSAndrey Shinkevich error_report_err(local_err); 18211bf6e9caSAndrey Shinkevich return -EIO; 18221bf6e9caSAndrey Shinkevich } 1823a8d8ecb7SMax Reitz if (spec_info) { 1824a8d8ecb7SMax Reitz printf("Format specific information:\n"); 1825e1ce7d74SMarkus Armbruster bdrv_image_info_specific_dump(spec_info); 1826a8d8ecb7SMax Reitz qapi_free_ImageInfoSpecific(spec_info); 1827a8d8ecb7SMax Reitz } 1828b32d7a39SMax Reitz 1829b32d7a39SMax Reitz return 0; 1830797ac58cSKevin Wolf } 1831797ac58cSKevin Wolf 1832797ac58cSKevin Wolf 1833797ac58cSKevin Wolf 1834797ac58cSKevin Wolf static const cmdinfo_t info_cmd = { 1835797ac58cSKevin Wolf .name = "info", 1836797ac58cSKevin Wolf .altname = "i", 1837797ac58cSKevin Wolf .cfunc = info_f, 1838797ac58cSKevin Wolf .oneline = "prints information about the current file", 1839797ac58cSKevin Wolf }; 1840797ac58cSKevin Wolf 1841797ac58cSKevin Wolf static void discard_help(void) 1842797ac58cSKevin Wolf { 1843797ac58cSKevin Wolf printf( 1844797ac58cSKevin Wolf "\n" 1845797ac58cSKevin Wolf " discards a range of bytes from the given offset\n" 1846797ac58cSKevin Wolf "\n" 1847797ac58cSKevin Wolf " Example:\n" 1848797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n" 1849797ac58cSKevin Wolf "\n" 1850797ac58cSKevin Wolf " Discards a segment of the currently open file.\n" 1851797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n" 1852797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n" 1853797ac58cSKevin Wolf "\n"); 1854797ac58cSKevin Wolf } 1855797ac58cSKevin Wolf 1856b32d7a39SMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv); 1857797ac58cSKevin Wolf 1858797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = { 1859797ac58cSKevin Wolf .name = "discard", 1860797ac58cSKevin Wolf .altname = "d", 1861797ac58cSKevin Wolf .cfunc = discard_f, 1862887354bdSKevin Wolf .perm = BLK_PERM_WRITE, 1863797ac58cSKevin Wolf .argmin = 2, 1864797ac58cSKevin Wolf .argmax = -1, 1865797ac58cSKevin Wolf .args = "[-Cq] off len", 1866797ac58cSKevin Wolf .oneline = "discards a number of bytes at a specified offset", 1867797ac58cSKevin Wolf .help = discard_help, 1868797ac58cSKevin Wolf }; 1869797ac58cSKevin Wolf 1870b32d7a39SMax Reitz static int discard_f(BlockBackend *blk, int argc, char **argv) 1871797ac58cSKevin Wolf { 187250290c00SAlex Bennée struct timespec t1, t2; 1873dc38852aSEric Blake bool Cflag = false, qflag = false; 1874797ac58cSKevin Wolf int c, ret; 1875f5a5ca79SManos Pitsidianakis int64_t offset, bytes; 1876797ac58cSKevin Wolf 1877b062ad86SEric Blake while ((c = getopt(argc, argv, "Cq")) != -1) { 1878797ac58cSKevin Wolf switch (c) { 1879797ac58cSKevin Wolf case 'C': 1880dc38852aSEric Blake Cflag = true; 1881797ac58cSKevin Wolf break; 1882797ac58cSKevin Wolf case 'q': 1883dc38852aSEric Blake qflag = true; 1884797ac58cSKevin Wolf break; 1885797ac58cSKevin Wolf default: 1886b444d0e9SMax Reitz qemuio_command_usage(&discard_cmd); 1887b32d7a39SMax Reitz return -EINVAL; 1888797ac58cSKevin Wolf } 1889797ac58cSKevin Wolf } 1890797ac58cSKevin Wolf 1891797ac58cSKevin Wolf if (optind != argc - 2) { 1892b444d0e9SMax Reitz qemuio_command_usage(&discard_cmd); 1893b32d7a39SMax Reitz return -EINVAL; 1894797ac58cSKevin Wolf } 1895797ac58cSKevin Wolf 1896797ac58cSKevin Wolf offset = cvtnum(argv[optind]); 1897797ac58cSKevin Wolf if (offset < 0) { 1898a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[optind]); 1899b32d7a39SMax Reitz return offset; 1900797ac58cSKevin Wolf } 1901797ac58cSKevin Wolf 1902797ac58cSKevin Wolf optind++; 1903f5a5ca79SManos Pitsidianakis bytes = cvtnum(argv[optind]); 1904f5a5ca79SManos Pitsidianakis if (bytes < 0) { 1905f5a5ca79SManos Pitsidianakis print_cvtnum_err(bytes, argv[optind]); 1906b32d7a39SMax Reitz return bytes; 190741ae31e3SAlberto Garcia } else if (bytes > BDRV_REQUEST_MAX_BYTES) { 19089b0beaf3SJohn Snow printf("length cannot exceed %"PRIu64", given %s\n", 190941ae31e3SAlberto Garcia (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]); 1910b32d7a39SMax Reitz return -EINVAL; 1911797ac58cSKevin Wolf } 1912797ac58cSKevin Wolf 191350290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t1); 1914f5a5ca79SManos Pitsidianakis ret = blk_pdiscard(blk, offset, bytes); 191550290c00SAlex Bennée clock_gettime(CLOCK_MONOTONIC, &t2); 1916797ac58cSKevin Wolf 1917797ac58cSKevin Wolf if (ret < 0) { 1918797ac58cSKevin Wolf printf("discard failed: %s\n", strerror(-ret)); 1919b32d7a39SMax Reitz return ret; 1920797ac58cSKevin Wolf } 1921797ac58cSKevin Wolf 1922797ac58cSKevin Wolf /* Finally, report back -- -C gives a parsable format */ 1923797ac58cSKevin Wolf if (!qflag) { 1924797ac58cSKevin Wolf t2 = tsub(t2, t1); 1925f5a5ca79SManos Pitsidianakis print_report("discard", &t2, offset, bytes, bytes, 1, Cflag); 1926797ac58cSKevin Wolf } 1927b32d7a39SMax Reitz 1928b32d7a39SMax Reitz return 0; 1929797ac58cSKevin Wolf } 1930797ac58cSKevin Wolf 1931b32d7a39SMax Reitz static int alloc_f(BlockBackend *blk, int argc, char **argv) 1932797ac58cSKevin Wolf { 19334c7b7e9bSMax Reitz BlockDriverState *bs = blk_bs(blk); 1934d6a644bbSEric Blake int64_t offset, start, remaining, count; 1935797ac58cSKevin Wolf char s1[64]; 1936d6a644bbSEric Blake int ret; 1937d6a644bbSEric Blake int64_t num, sum_alloc; 1938797ac58cSKevin Wolf 1939d6a644bbSEric Blake start = offset = cvtnum(argv[1]); 1940797ac58cSKevin Wolf if (offset < 0) { 1941a9ecfa00SJohn Snow print_cvtnum_err(offset, argv[1]); 1942b32d7a39SMax Reitz return offset; 1943797ac58cSKevin Wolf } 1944797ac58cSKevin Wolf 1945797ac58cSKevin Wolf if (argc == 3) { 19464401fdc7SEric Blake count = cvtnum(argv[2]); 19474401fdc7SEric Blake if (count < 0) { 19484401fdc7SEric Blake print_cvtnum_err(count, argv[2]); 1949b32d7a39SMax Reitz return count; 1950797ac58cSKevin Wolf } 1951797ac58cSKevin Wolf } else { 19524401fdc7SEric Blake count = BDRV_SECTOR_SIZE; 1953797ac58cSKevin Wolf } 1954797ac58cSKevin Wolf 1955d6a644bbSEric Blake remaining = count; 1956797ac58cSKevin Wolf sum_alloc = 0; 1957797ac58cSKevin Wolf while (remaining) { 1958d6a644bbSEric Blake ret = bdrv_is_allocated(bs, offset, remaining, &num); 1959d663640cSPaolo Bonzini if (ret < 0) { 1960d663640cSPaolo Bonzini printf("is_allocated failed: %s\n", strerror(-ret)); 1961b32d7a39SMax Reitz return ret; 1962d663640cSPaolo Bonzini } 1963d6a644bbSEric Blake offset += num; 1964797ac58cSKevin Wolf remaining -= num; 1965797ac58cSKevin Wolf if (ret) { 1966797ac58cSKevin Wolf sum_alloc += num; 1967797ac58cSKevin Wolf } 1968797ac58cSKevin Wolf if (num == 0) { 1969d6a644bbSEric Blake count -= remaining; 1970797ac58cSKevin Wolf remaining = 0; 1971797ac58cSKevin Wolf } 1972797ac58cSKevin Wolf } 1973797ac58cSKevin Wolf 1974d6a644bbSEric Blake cvtstr(start, s1, sizeof(s1)); 1975797ac58cSKevin Wolf 19764401fdc7SEric Blake printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n", 1977d6a644bbSEric Blake sum_alloc, count, s1); 1978b32d7a39SMax Reitz return 0; 1979797ac58cSKevin Wolf } 1980797ac58cSKevin Wolf 1981797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = { 1982797ac58cSKevin Wolf .name = "alloc", 1983797ac58cSKevin Wolf .altname = "a", 1984797ac58cSKevin Wolf .argmin = 1, 1985797ac58cSKevin Wolf .argmax = 2, 1986797ac58cSKevin Wolf .cfunc = alloc_f, 19874401fdc7SEric Blake .args = "offset [count]", 19884401fdc7SEric Blake .oneline = "checks if offset is allocated in the file", 1989797ac58cSKevin Wolf }; 1990797ac58cSKevin Wolf 1991797ac58cSKevin Wolf 1992d6a644bbSEric Blake static int map_is_allocated(BlockDriverState *bs, int64_t offset, 1993d6a644bbSEric Blake int64_t bytes, int64_t *pnum) 1994797ac58cSKevin Wolf { 1995d6a644bbSEric Blake int64_t num; 1996d6a644bbSEric Blake int num_checked; 1997797ac58cSKevin Wolf int ret, firstret; 1998797ac58cSKevin Wolf 1999d6a644bbSEric Blake num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES); 2000d6a644bbSEric Blake ret = bdrv_is_allocated(bs, offset, num_checked, &num); 2001797ac58cSKevin Wolf if (ret < 0) { 2002797ac58cSKevin Wolf return ret; 2003797ac58cSKevin Wolf } 2004797ac58cSKevin Wolf 2005797ac58cSKevin Wolf firstret = ret; 2006797ac58cSKevin Wolf *pnum = num; 2007797ac58cSKevin Wolf 2008d6a644bbSEric Blake while (bytes > 0 && ret == firstret) { 2009d6a644bbSEric Blake offset += num; 2010d6a644bbSEric Blake bytes -= num; 2011797ac58cSKevin Wolf 2012d6a644bbSEric Blake num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES); 2013d6a644bbSEric Blake ret = bdrv_is_allocated(bs, offset, num_checked, &num); 20144b25bbc4SMax Reitz if (ret == firstret && num) { 2015797ac58cSKevin Wolf *pnum += num; 2016797ac58cSKevin Wolf } else { 2017797ac58cSKevin Wolf break; 2018797ac58cSKevin Wolf } 2019797ac58cSKevin Wolf } 2020797ac58cSKevin Wolf 2021797ac58cSKevin Wolf return firstret; 2022797ac58cSKevin Wolf } 2023797ac58cSKevin Wolf 2024b32d7a39SMax Reitz static int map_f(BlockBackend *blk, int argc, char **argv) 2025797ac58cSKevin Wolf { 2026d6a644bbSEric Blake int64_t offset, bytes; 20276f3c90afSEric Blake char s1[64], s2[64]; 2028797ac58cSKevin Wolf int64_t num; 2029797ac58cSKevin Wolf int ret; 2030797ac58cSKevin Wolf const char *retstr; 2031797ac58cSKevin Wolf 2032797ac58cSKevin Wolf offset = 0; 2033d6a644bbSEric Blake bytes = blk_getlength(blk); 2034d6a644bbSEric Blake if (bytes < 0) { 2035d6a644bbSEric Blake error_report("Failed to query image length: %s", strerror(-bytes)); 2036b32d7a39SMax Reitz return bytes; 20374c7b7e9bSMax Reitz } 20384c7b7e9bSMax Reitz 2039d6a644bbSEric Blake while (bytes) { 2040d6a644bbSEric Blake ret = map_is_allocated(blk_bs(blk), offset, bytes, &num); 2041797ac58cSKevin Wolf if (ret < 0) { 2042797ac58cSKevin Wolf error_report("Failed to get allocation status: %s", strerror(-ret)); 2043b32d7a39SMax Reitz return ret; 20444b25bbc4SMax Reitz } else if (!num) { 20454b25bbc4SMax Reitz error_report("Unexpected end of image"); 2046b32d7a39SMax Reitz return -EIO; 2047797ac58cSKevin Wolf } 2048797ac58cSKevin Wolf 2049797ac58cSKevin Wolf retstr = ret ? " allocated" : "not allocated"; 2050d6a644bbSEric Blake cvtstr(num, s1, sizeof(s1)); 2051d6a644bbSEric Blake cvtstr(offset, s2, sizeof(s2)); 20526f3c90afSEric Blake printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n", 2053d6a644bbSEric Blake s1, num, retstr, s2, offset); 2054797ac58cSKevin Wolf 2055797ac58cSKevin Wolf offset += num; 2056d6a644bbSEric Blake bytes -= num; 2057d6a644bbSEric Blake } 2058b32d7a39SMax Reitz 2059b32d7a39SMax Reitz return 0; 2060797ac58cSKevin Wolf } 2061797ac58cSKevin Wolf 2062797ac58cSKevin Wolf static const cmdinfo_t map_cmd = { 2063797ac58cSKevin Wolf .name = "map", 2064797ac58cSKevin Wolf .argmin = 0, 2065797ac58cSKevin Wolf .argmax = 0, 2066797ac58cSKevin Wolf .cfunc = map_f, 2067797ac58cSKevin Wolf .args = "", 2068797ac58cSKevin Wolf .oneline = "prints the allocated areas of a file", 2069797ac58cSKevin Wolf }; 2070797ac58cSKevin Wolf 20715bbd2e59SKevin Wolf static void reopen_help(void) 20725bbd2e59SKevin Wolf { 20735bbd2e59SKevin Wolf printf( 20745bbd2e59SKevin Wolf "\n" 20755bbd2e59SKevin Wolf " Changes the open options of an already opened image\n" 20765bbd2e59SKevin Wolf "\n" 20775bbd2e59SKevin Wolf " Example:\n" 20785bbd2e59SKevin Wolf " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n" 20795bbd2e59SKevin Wolf "\n" 20805bbd2e59SKevin Wolf " -r, -- Reopen the image read-only\n" 2081ea92203cSKevin Wolf " -w, -- Reopen the image read-write\n" 20825bbd2e59SKevin Wolf " -c, -- Change the cache mode to the given value\n" 20835bbd2e59SKevin Wolf " -o, -- Changes block driver options (cf. 'open' command)\n" 20845bbd2e59SKevin Wolf "\n"); 20855bbd2e59SKevin Wolf } 20865bbd2e59SKevin Wolf 2087b32d7a39SMax Reitz static int reopen_f(BlockBackend *blk, int argc, char **argv); 20885bbd2e59SKevin Wolf 20895bbd2e59SKevin Wolf static QemuOptsList reopen_opts = { 20905bbd2e59SKevin Wolf .name = "reopen", 20915bbd2e59SKevin Wolf .merge_lists = true, 20925bbd2e59SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head), 20935bbd2e59SKevin Wolf .desc = { 20945bbd2e59SKevin Wolf /* no elements => accept any params */ 20955bbd2e59SKevin Wolf { /* end of list */ } 20965bbd2e59SKevin Wolf }, 20975bbd2e59SKevin Wolf }; 20985bbd2e59SKevin Wolf 20995bbd2e59SKevin Wolf static const cmdinfo_t reopen_cmd = { 21005bbd2e59SKevin Wolf .name = "reopen", 21015bbd2e59SKevin Wolf .argmin = 0, 21025bbd2e59SKevin Wolf .argmax = -1, 21035bbd2e59SKevin Wolf .cfunc = reopen_f, 2104ea92203cSKevin Wolf .args = "[(-r|-w)] [-c cache] [-o options]", 21055bbd2e59SKevin Wolf .oneline = "reopens an image with new options", 21065bbd2e59SKevin Wolf .help = reopen_help, 21075bbd2e59SKevin Wolf }; 21085bbd2e59SKevin Wolf 2109b32d7a39SMax Reitz static int reopen_f(BlockBackend *blk, int argc, char **argv) 21105bbd2e59SKevin Wolf { 21115bbd2e59SKevin Wolf BlockDriverState *bs = blk_bs(blk); 21125bbd2e59SKevin Wolf QemuOpts *qopts; 21135bbd2e59SKevin Wolf QDict *opts; 21145bbd2e59SKevin Wolf int c; 21155bbd2e59SKevin Wolf int flags = bs->open_flags; 211619dbecdcSKevin Wolf bool writethrough = !blk_enable_write_cache(blk); 2117ea92203cSKevin Wolf bool has_rw_option = false; 2118dc900c35SAlberto Garcia bool has_cache_option = false; 21195bbd2e59SKevin Wolf 21205bbd2e59SKevin Wolf BlockReopenQueue *brq; 21215bbd2e59SKevin Wolf Error *local_err = NULL; 21225bbd2e59SKevin Wolf 2123ea92203cSKevin Wolf while ((c = getopt(argc, argv, "c:o:rw")) != -1) { 21245bbd2e59SKevin Wolf switch (c) { 21255bbd2e59SKevin Wolf case 'c': 212619dbecdcSKevin Wolf if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { 21275bbd2e59SKevin Wolf error_report("Invalid cache option: %s", optarg); 2128b32d7a39SMax Reitz return -EINVAL; 21295bbd2e59SKevin Wolf } 2130dc900c35SAlberto Garcia has_cache_option = true; 21315bbd2e59SKevin Wolf break; 21325bbd2e59SKevin Wolf case 'o': 21335bbd2e59SKevin Wolf if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) { 21345bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 2135b32d7a39SMax Reitz return -EINVAL; 21365bbd2e59SKevin Wolf } 21375bbd2e59SKevin Wolf break; 21385bbd2e59SKevin Wolf case 'r': 2139ea92203cSKevin Wolf if (has_rw_option) { 2140ea92203cSKevin Wolf error_report("Only one -r/-w option may be given"); 2141b32d7a39SMax Reitz return -EINVAL; 2142ea92203cSKevin Wolf } 21435bbd2e59SKevin Wolf flags &= ~BDRV_O_RDWR; 2144ea92203cSKevin Wolf has_rw_option = true; 2145ea92203cSKevin Wolf break; 2146ea92203cSKevin Wolf case 'w': 2147ea92203cSKevin Wolf if (has_rw_option) { 2148ea92203cSKevin Wolf error_report("Only one -r/-w option may be given"); 2149b32d7a39SMax Reitz return -EINVAL; 2150ea92203cSKevin Wolf } 2151ea92203cSKevin Wolf flags |= BDRV_O_RDWR; 2152ea92203cSKevin Wolf has_rw_option = true; 21535bbd2e59SKevin Wolf break; 21545bbd2e59SKevin Wolf default: 21555bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 2156b444d0e9SMax Reitz qemuio_command_usage(&reopen_cmd); 2157b32d7a39SMax Reitz return -EINVAL; 21585bbd2e59SKevin Wolf } 21595bbd2e59SKevin Wolf } 21605bbd2e59SKevin Wolf 21615bbd2e59SKevin Wolf if (optind != argc) { 21625bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 2163b444d0e9SMax Reitz qemuio_command_usage(&reopen_cmd); 2164b32d7a39SMax Reitz return -EINVAL; 21655bbd2e59SKevin Wolf } 21665bbd2e59SKevin Wolf 2167a8003ec4SAlberto Garcia if (!writethrough != blk_enable_write_cache(blk) && 216819dbecdcSKevin Wolf blk_get_attached_dev(blk)) 216919dbecdcSKevin Wolf { 217019dbecdcSKevin Wolf error_report("Cannot change cache.writeback: Device attached"); 217119dbecdcSKevin Wolf qemu_opts_reset(&reopen_opts); 2172b32d7a39SMax Reitz return -EBUSY; 217319dbecdcSKevin Wolf } 217419dbecdcSKevin Wolf 2175f3adefb2SKevin Wolf if (!(flags & BDRV_O_RDWR)) { 2176f3adefb2SKevin Wolf uint64_t orig_perm, orig_shared_perm; 2177f3adefb2SKevin Wolf 2178f3adefb2SKevin Wolf bdrv_drain(bs); 2179f3adefb2SKevin Wolf 2180f3adefb2SKevin Wolf blk_get_perm(blk, &orig_perm, &orig_shared_perm); 2181f3adefb2SKevin Wolf blk_set_perm(blk, 2182f3adefb2SKevin Wolf orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED), 2183f3adefb2SKevin Wolf orig_shared_perm, 2184f3adefb2SKevin Wolf &error_abort); 2185f3adefb2SKevin Wolf } 2186f3adefb2SKevin Wolf 21875bbd2e59SKevin Wolf qopts = qemu_opts_find(&reopen_opts, NULL); 2188dc900c35SAlberto Garcia opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new(); 21895bbd2e59SKevin Wolf qemu_opts_reset(&reopen_opts); 21905bbd2e59SKevin Wolf 2191dc900c35SAlberto Garcia if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) { 2192dc900c35SAlberto Garcia if (has_rw_option) { 2193dc900c35SAlberto Garcia error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'"); 2194dc900c35SAlberto Garcia qobject_unref(opts); 2195dc900c35SAlberto Garcia return -EINVAL; 2196dc900c35SAlberto Garcia } 2197dc900c35SAlberto Garcia } else { 2198dc900c35SAlberto Garcia qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR)); 2199dc900c35SAlberto Garcia } 2200dc900c35SAlberto Garcia 2201dc900c35SAlberto Garcia if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) || 2202dc900c35SAlberto Garcia qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) { 2203dc900c35SAlberto Garcia if (has_cache_option) { 2204dc900c35SAlberto Garcia error_report("Cannot set both -c and the cache options"); 2205dc900c35SAlberto Garcia qobject_unref(opts); 2206dc900c35SAlberto Garcia return -EINVAL; 2207dc900c35SAlberto Garcia } 2208dc900c35SAlberto Garcia } else { 2209dc900c35SAlberto Garcia qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE); 2210dc900c35SAlberto Garcia qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH); 2211dc900c35SAlberto Garcia } 2212dc900c35SAlberto Garcia 22131a63a907SKevin Wolf bdrv_subtree_drained_begin(bs); 2214077e8e20SAlberto Garcia brq = bdrv_reopen_queue(NULL, bs, opts, true); 22155019aeceSAlberto Garcia bdrv_reopen_multiple(brq, &local_err); 22161a63a907SKevin Wolf bdrv_subtree_drained_end(bs); 22171a63a907SKevin Wolf 22185bbd2e59SKevin Wolf if (local_err) { 22195bbd2e59SKevin Wolf error_report_err(local_err); 2220b32d7a39SMax Reitz return -EINVAL; 22215bbd2e59SKevin Wolf } 22225bbd2e59SKevin Wolf 2223b32d7a39SMax Reitz blk_set_enable_write_cache(blk, !writethrough); 2224b32d7a39SMax Reitz return 0; 2225b32d7a39SMax Reitz } 2226b32d7a39SMax Reitz 2227b32d7a39SMax Reitz static int break_f(BlockBackend *blk, int argc, char **argv) 2228797ac58cSKevin Wolf { 2229797ac58cSKevin Wolf int ret; 2230797ac58cSKevin Wolf 22314c7b7e9bSMax Reitz ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]); 2232797ac58cSKevin Wolf if (ret < 0) { 2233797ac58cSKevin Wolf printf("Could not set breakpoint: %s\n", strerror(-ret)); 2234b32d7a39SMax Reitz return ret; 2235797ac58cSKevin Wolf } 2236797ac58cSKevin Wolf 2237b32d7a39SMax Reitz return 0; 2238b32d7a39SMax Reitz } 2239b32d7a39SMax Reitz 2240b32d7a39SMax Reitz static int remove_break_f(BlockBackend *blk, int argc, char **argv) 22414cc70e93SFam Zheng { 22424cc70e93SFam Zheng int ret; 22434cc70e93SFam Zheng 22444c7b7e9bSMax Reitz ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]); 22454cc70e93SFam Zheng if (ret < 0) { 22464cc70e93SFam Zheng printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret)); 2247b32d7a39SMax Reitz return ret; 22484cc70e93SFam Zheng } 2249b32d7a39SMax Reitz 2250b32d7a39SMax Reitz return 0; 22514cc70e93SFam Zheng } 22524cc70e93SFam Zheng 2253797ac58cSKevin Wolf static const cmdinfo_t break_cmd = { 2254797ac58cSKevin Wolf .name = "break", 2255797ac58cSKevin Wolf .argmin = 2, 2256797ac58cSKevin Wolf .argmax = 2, 2257797ac58cSKevin Wolf .cfunc = break_f, 2258797ac58cSKevin Wolf .args = "event tag", 2259797ac58cSKevin Wolf .oneline = "sets a breakpoint on event and tags the stopped " 2260797ac58cSKevin Wolf "request as tag", 2261797ac58cSKevin Wolf }; 2262797ac58cSKevin Wolf 22634cc70e93SFam Zheng static const cmdinfo_t remove_break_cmd = { 22644cc70e93SFam Zheng .name = "remove_break", 22654cc70e93SFam Zheng .argmin = 1, 22664cc70e93SFam Zheng .argmax = 1, 22674cc70e93SFam Zheng .cfunc = remove_break_f, 22684cc70e93SFam Zheng .args = "tag", 22694cc70e93SFam Zheng .oneline = "remove a breakpoint by tag", 22704cc70e93SFam Zheng }; 22714cc70e93SFam Zheng 2272b32d7a39SMax Reitz static int resume_f(BlockBackend *blk, int argc, char **argv) 2273797ac58cSKevin Wolf { 2274797ac58cSKevin Wolf int ret; 2275797ac58cSKevin Wolf 22764c7b7e9bSMax Reitz ret = bdrv_debug_resume(blk_bs(blk), argv[1]); 2277797ac58cSKevin Wolf if (ret < 0) { 2278797ac58cSKevin Wolf printf("Could not resume request: %s\n", strerror(-ret)); 2279b32d7a39SMax Reitz return ret; 2280797ac58cSKevin Wolf } 2281b32d7a39SMax Reitz 2282b32d7a39SMax Reitz return 0; 2283797ac58cSKevin Wolf } 2284797ac58cSKevin Wolf 2285797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = { 2286797ac58cSKevin Wolf .name = "resume", 2287797ac58cSKevin Wolf .argmin = 1, 2288797ac58cSKevin Wolf .argmax = 1, 2289797ac58cSKevin Wolf .cfunc = resume_f, 2290797ac58cSKevin Wolf .args = "tag", 2291797ac58cSKevin Wolf .oneline = "resumes the request tagged as tag", 2292797ac58cSKevin Wolf }; 2293797ac58cSKevin Wolf 2294b32d7a39SMax Reitz static int wait_break_f(BlockBackend *blk, int argc, char **argv) 2295797ac58cSKevin Wolf { 22964c7b7e9bSMax Reitz while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) { 22974c7b7e9bSMax Reitz aio_poll(blk_get_aio_context(blk), true); 2298797ac58cSKevin Wolf } 2299b32d7a39SMax Reitz return 0; 2300797ac58cSKevin Wolf } 2301797ac58cSKevin Wolf 2302797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = { 2303797ac58cSKevin Wolf .name = "wait_break", 2304797ac58cSKevin Wolf .argmin = 1, 2305797ac58cSKevin Wolf .argmax = 1, 2306797ac58cSKevin Wolf .cfunc = wait_break_f, 2307797ac58cSKevin Wolf .args = "tag", 2308797ac58cSKevin Wolf .oneline = "waits for the suspension of a request", 2309797ac58cSKevin Wolf }; 2310797ac58cSKevin Wolf 2311b32d7a39SMax Reitz static int abort_f(BlockBackend *blk, int argc, char **argv) 2312797ac58cSKevin Wolf { 2313797ac58cSKevin Wolf abort(); 2314797ac58cSKevin Wolf } 2315797ac58cSKevin Wolf 2316797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = { 2317797ac58cSKevin Wolf .name = "abort", 2318797ac58cSKevin Wolf .cfunc = abort_f, 2319797ac58cSKevin Wolf .flags = CMD_NOFILE_OK, 2320797ac58cSKevin Wolf .oneline = "simulate a program crash using abort(3)", 2321797ac58cSKevin Wolf }; 2322797ac58cSKevin Wolf 23230e82dc7bSMax Reitz static void sigraise_help(void) 23240e82dc7bSMax Reitz { 23250e82dc7bSMax Reitz printf( 23260e82dc7bSMax Reitz "\n" 23270e82dc7bSMax Reitz " raises the given signal\n" 23280e82dc7bSMax Reitz "\n" 23290e82dc7bSMax Reitz " Example:\n" 23300e82dc7bSMax Reitz " 'sigraise %i' - raises SIGTERM\n" 23310e82dc7bSMax Reitz "\n" 23320e82dc7bSMax Reitz " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n" 23330e82dc7bSMax Reitz " given to sigraise.\n" 23340e82dc7bSMax Reitz "\n", SIGTERM); 23350e82dc7bSMax Reitz } 23360e82dc7bSMax Reitz 2337b32d7a39SMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv); 23380e82dc7bSMax Reitz 23390e82dc7bSMax Reitz static const cmdinfo_t sigraise_cmd = { 23400e82dc7bSMax Reitz .name = "sigraise", 23410e82dc7bSMax Reitz .cfunc = sigraise_f, 23420e82dc7bSMax Reitz .argmin = 1, 23430e82dc7bSMax Reitz .argmax = 1, 23440e82dc7bSMax Reitz .flags = CMD_NOFILE_OK, 23450e82dc7bSMax Reitz .args = "signal", 23460e82dc7bSMax Reitz .oneline = "raises a signal", 23470e82dc7bSMax Reitz .help = sigraise_help, 23480e82dc7bSMax Reitz }; 23490e82dc7bSMax Reitz 2350b32d7a39SMax Reitz static int sigraise_f(BlockBackend *blk, int argc, char **argv) 23510e82dc7bSMax Reitz { 23529b0beaf3SJohn Snow int64_t sig = cvtnum(argv[1]); 23530e82dc7bSMax Reitz if (sig < 0) { 2354a9ecfa00SJohn Snow print_cvtnum_err(sig, argv[1]); 2355b32d7a39SMax Reitz return sig; 23569b0beaf3SJohn Snow } else if (sig > NSIG) { 23579b0beaf3SJohn Snow printf("signal argument '%s' is too large to be a valid signal\n", 23589b0beaf3SJohn Snow argv[1]); 2359b32d7a39SMax Reitz return -EINVAL; 23600e82dc7bSMax Reitz } 23610e82dc7bSMax Reitz 23620e82dc7bSMax Reitz /* Using raise() to kill this process does not necessarily flush all open 23630e82dc7bSMax Reitz * streams. At least stdout and stderr (although the latter should be 23640e82dc7bSMax Reitz * non-buffered anyway) should be flushed, though. */ 23650e82dc7bSMax Reitz fflush(stdout); 23660e82dc7bSMax Reitz fflush(stderr); 23670e82dc7bSMax Reitz 23680e82dc7bSMax Reitz raise(sig); 2369b32d7a39SMax Reitz 2370b32d7a39SMax Reitz return 0; 23710e82dc7bSMax Reitz } 23720e82dc7bSMax Reitz 2373cd33d02aSKevin Wolf static void sleep_cb(void *opaque) 2374cd33d02aSKevin Wolf { 2375cd33d02aSKevin Wolf bool *expired = opaque; 2376cd33d02aSKevin Wolf *expired = true; 2377cd33d02aSKevin Wolf } 2378cd33d02aSKevin Wolf 2379b32d7a39SMax Reitz static int sleep_f(BlockBackend *blk, int argc, char **argv) 2380cd33d02aSKevin Wolf { 2381cd33d02aSKevin Wolf char *endptr; 2382cd33d02aSKevin Wolf long ms; 2383cd33d02aSKevin Wolf struct QEMUTimer *timer; 2384cd33d02aSKevin Wolf bool expired = false; 2385cd33d02aSKevin Wolf 2386cd33d02aSKevin Wolf ms = strtol(argv[1], &endptr, 0); 2387cd33d02aSKevin Wolf if (ms < 0 || *endptr != '\0') { 2388cd33d02aSKevin Wolf printf("%s is not a valid number\n", argv[1]); 2389b32d7a39SMax Reitz return -EINVAL; 2390cd33d02aSKevin Wolf } 2391cd33d02aSKevin Wolf 2392cd33d02aSKevin Wolf timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired); 2393cd33d02aSKevin Wolf timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms); 2394cd33d02aSKevin Wolf 2395cd33d02aSKevin Wolf while (!expired) { 2396cd33d02aSKevin Wolf main_loop_wait(false); 2397cd33d02aSKevin Wolf } 2398cd33d02aSKevin Wolf 2399cd33d02aSKevin Wolf timer_free(timer); 2400b32d7a39SMax Reitz return 0; 2401cd33d02aSKevin Wolf } 2402cd33d02aSKevin Wolf 2403cd33d02aSKevin Wolf static const cmdinfo_t sleep_cmd = { 2404cd33d02aSKevin Wolf .name = "sleep", 2405cd33d02aSKevin Wolf .argmin = 1, 2406cd33d02aSKevin Wolf .argmax = 1, 2407cd33d02aSKevin Wolf .cfunc = sleep_f, 2408cd33d02aSKevin Wolf .flags = CMD_NOFILE_OK, 2409cd33d02aSKevin Wolf .oneline = "waits for the given value in milliseconds", 2410cd33d02aSKevin Wolf }; 2411cd33d02aSKevin Wolf 2412f18a834aSKevin Wolf static void help_oneline(const char *cmd, const cmdinfo_t *ct) 2413f18a834aSKevin Wolf { 2414f18a834aSKevin Wolf printf("%s ", cmd); 2415f18a834aSKevin Wolf 2416f18a834aSKevin Wolf if (ct->args) { 2417f18a834aSKevin Wolf printf("%s ", ct->args); 2418f18a834aSKevin Wolf } 2419f18a834aSKevin Wolf printf("-- %s\n", ct->oneline); 2420f18a834aSKevin Wolf } 2421f18a834aSKevin Wolf 2422f18a834aSKevin Wolf static void help_onecmd(const char *cmd, const cmdinfo_t *ct) 2423f18a834aSKevin Wolf { 2424f18a834aSKevin Wolf help_oneline(cmd, ct); 2425f18a834aSKevin Wolf if (ct->help) { 2426f18a834aSKevin Wolf ct->help(); 2427f18a834aSKevin Wolf } 2428f18a834aSKevin Wolf } 2429f18a834aSKevin Wolf 2430f18a834aSKevin Wolf static void help_all(void) 2431f18a834aSKevin Wolf { 2432f18a834aSKevin Wolf const cmdinfo_t *ct; 2433f18a834aSKevin Wolf 2434f18a834aSKevin Wolf for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { 2435f18a834aSKevin Wolf help_oneline(ct->name, ct); 2436f18a834aSKevin Wolf } 2437f18a834aSKevin Wolf printf("\nUse 'help commandname' for extended help.\n"); 2438f18a834aSKevin Wolf } 2439f18a834aSKevin Wolf 2440b32d7a39SMax Reitz static int help_f(BlockBackend *blk, int argc, char **argv) 2441f18a834aSKevin Wolf { 2442f18a834aSKevin Wolf const cmdinfo_t *ct; 2443f18a834aSKevin Wolf 2444da16f4b8SDr. David Alan Gilbert if (argc < 2) { 2445f18a834aSKevin Wolf help_all(); 2446b32d7a39SMax Reitz return 0; 2447f18a834aSKevin Wolf } 2448f18a834aSKevin Wolf 2449f18a834aSKevin Wolf ct = find_command(argv[1]); 2450f18a834aSKevin Wolf if (ct == NULL) { 2451f18a834aSKevin Wolf printf("command %s not found\n", argv[1]); 2452b32d7a39SMax Reitz return -EINVAL; 2453f18a834aSKevin Wolf } 2454f18a834aSKevin Wolf 2455f18a834aSKevin Wolf help_onecmd(argv[1], ct); 2456b32d7a39SMax Reitz return 0; 2457f18a834aSKevin Wolf } 2458f18a834aSKevin Wolf 2459f18a834aSKevin Wolf static const cmdinfo_t help_cmd = { 2460f18a834aSKevin Wolf .name = "help", 2461f18a834aSKevin Wolf .altname = "?", 2462f18a834aSKevin Wolf .cfunc = help_f, 2463f18a834aSKevin Wolf .argmin = 0, 2464f18a834aSKevin Wolf .argmax = 1, 2465f18a834aSKevin Wolf .flags = CMD_FLAG_GLOBAL, 2466f18a834aSKevin Wolf .args = "[command]", 2467f18a834aSKevin Wolf .oneline = "help for one or all commands", 2468f18a834aSKevin Wolf }; 2469f18a834aSKevin Wolf 247078632a3dSVladimir Sementsov-Ogievskiy /* 247178632a3dSVladimir Sementsov-Ogievskiy * Called with aio context of blk acquired. Or with qemu_get_aio_context() 247278632a3dSVladimir Sementsov-Ogievskiy * context acquired if blk is NULL. 247378632a3dSVladimir Sementsov-Ogievskiy */ 2474b32d7a39SMax Reitz int qemuio_command(BlockBackend *blk, const char *cmd) 2475dd583296SKevin Wolf { 2476dd583296SKevin Wolf char *input; 2477dd583296SKevin Wolf const cmdinfo_t *ct; 2478dd583296SKevin Wolf char **v; 2479dd583296SKevin Wolf int c; 2480b32d7a39SMax Reitz int ret = 0; 2481dd583296SKevin Wolf 2482dd583296SKevin Wolf input = g_strdup(cmd); 2483dd583296SKevin Wolf v = breakline(input, &c); 2484dd583296SKevin Wolf if (c) { 2485dd583296SKevin Wolf ct = find_command(v[0]); 2486dd583296SKevin Wolf if (ct) { 2487b32d7a39SMax Reitz ret = command(blk, ct, c, v); 2488dd583296SKevin Wolf } else { 2489dd583296SKevin Wolf fprintf(stderr, "command \"%s\" not found\n", v[0]); 2490b32d7a39SMax Reitz ret = -EINVAL; 2491dd583296SKevin Wolf } 2492dd583296SKevin Wolf } 2493dd583296SKevin Wolf g_free(input); 2494dd583296SKevin Wolf g_free(v); 2495b32d7a39SMax Reitz 2496b32d7a39SMax Reitz return ret; 2497dd583296SKevin Wolf } 2498dd583296SKevin Wolf 2499797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void) 2500797ac58cSKevin Wolf { 2501797ac58cSKevin Wolf /* initialize commands */ 2502c2cdf5c5SKevin Wolf qemuio_add_command(&help_cmd); 2503c2cdf5c5SKevin Wolf qemuio_add_command(&read_cmd); 2504c2cdf5c5SKevin Wolf qemuio_add_command(&readv_cmd); 2505c2cdf5c5SKevin Wolf qemuio_add_command(&write_cmd); 2506c2cdf5c5SKevin Wolf qemuio_add_command(&writev_cmd); 2507c2cdf5c5SKevin Wolf qemuio_add_command(&aio_read_cmd); 2508c2cdf5c5SKevin Wolf qemuio_add_command(&aio_write_cmd); 2509c2cdf5c5SKevin Wolf qemuio_add_command(&aio_flush_cmd); 2510c2cdf5c5SKevin Wolf qemuio_add_command(&flush_cmd); 2511c2cdf5c5SKevin Wolf qemuio_add_command(&truncate_cmd); 2512c2cdf5c5SKevin Wolf qemuio_add_command(&length_cmd); 2513c2cdf5c5SKevin Wolf qemuio_add_command(&info_cmd); 2514c2cdf5c5SKevin Wolf qemuio_add_command(&discard_cmd); 2515c2cdf5c5SKevin Wolf qemuio_add_command(&alloc_cmd); 2516c2cdf5c5SKevin Wolf qemuio_add_command(&map_cmd); 25175bbd2e59SKevin Wolf qemuio_add_command(&reopen_cmd); 2518c2cdf5c5SKevin Wolf qemuio_add_command(&break_cmd); 25194cc70e93SFam Zheng qemuio_add_command(&remove_break_cmd); 2520c2cdf5c5SKevin Wolf qemuio_add_command(&resume_cmd); 2521c2cdf5c5SKevin Wolf qemuio_add_command(&wait_break_cmd); 2522c2cdf5c5SKevin Wolf qemuio_add_command(&abort_cmd); 2523cd33d02aSKevin Wolf qemuio_add_command(&sleep_cmd); 25240e82dc7bSMax Reitz qemuio_add_command(&sigraise_cmd); 2525797ac58cSKevin Wolf } 2526