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