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