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