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