aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Command line utility to exercise the QEMU I/O path. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat, Inc. |
| 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 | */ |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 10 | #include <sys/time.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | #include <stdarg.h> |
| 13 | #include <stdio.h> |
| 14 | #include <getopt.h> |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 15 | #include <libgen.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 16 | |
| 17 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/main-loop.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 19 | #include "block/block_int.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 20 | #include "cmd.h" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 21 | #include "trace/control.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 22 | |
| 23 | #define VERSION "0.0.1" |
| 24 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 25 | #define CMD_NOFILE_OK 0x01 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 26 | |
| 27 | char *progname; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 28 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 29 | BlockDriverState *qemuio_bs; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 30 | static int misalign; |
| 31 | |
Kevin Wolf | b6e356a | 2013-06-05 14:19:28 +0200 | [diff] [blame] | 32 | static int64_t cvtnum(const char *s) |
| 33 | { |
| 34 | char *end; |
| 35 | return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B); |
| 36 | } |
| 37 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 38 | /* |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 39 | * Parse the pattern argument to various sub-commands. |
| 40 | * |
| 41 | * Because the pattern is used as an argument to memset it must evaluate |
| 42 | * to an unsigned integer that fits into a single byte. |
| 43 | */ |
| 44 | static int parse_pattern(const char *arg) |
| 45 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 46 | char *endptr = NULL; |
| 47 | long pattern; |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 48 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 49 | pattern = strtol(arg, &endptr, 0); |
| 50 | if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') { |
| 51 | printf("%s is not a valid pattern byte\n", arg); |
| 52 | return -1; |
| 53 | } |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 54 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 55 | return pattern; |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 59 | * Memory allocation helpers. |
| 60 | * |
| 61 | * Make sure memory is aligned by default, or purposefully misaligned if |
| 62 | * that is specified on the command line. |
| 63 | */ |
| 64 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 65 | #define MISALIGN_OFFSET 16 |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 66 | static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 67 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 68 | void *buf; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 69 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 70 | if (misalign) { |
| 71 | len += MISALIGN_OFFSET; |
| 72 | } |
| 73 | buf = qemu_blockalign(bs, len); |
| 74 | memset(buf, pattern, len); |
| 75 | if (misalign) { |
| 76 | buf += MISALIGN_OFFSET; |
| 77 | } |
| 78 | return buf; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void qemu_io_free(void *p) |
| 82 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 83 | if (misalign) { |
| 84 | p -= MISALIGN_OFFSET; |
| 85 | } |
| 86 | qemu_vfree(p); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 89 | static void dump_buffer(const void *buffer, int64_t offset, int len) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 90 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 91 | int i, j; |
| 92 | const uint8_t *p; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 93 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 94 | for (i = 0, p = buffer; i < len; i += 16) { |
| 95 | const uint8_t *s = p; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 96 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 97 | printf("%08" PRIx64 ": ", offset + i); |
| 98 | for (j = 0; j < 16 && i + j < len; j++, p++) { |
| 99 | printf("%02x ", *p); |
| 100 | } |
| 101 | printf(" "); |
| 102 | for (j = 0; j < 16 && i + j < len; j++, s++) { |
| 103 | if (isalnum(*s)) { |
| 104 | printf("%c", *s); |
| 105 | } else { |
| 106 | printf("."); |
| 107 | } |
| 108 | } |
| 109 | printf("\n"); |
| 110 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 113 | static void print_report(const char *op, struct timeval *t, int64_t offset, |
| 114 | int count, int total, int cnt, int Cflag) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 115 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 116 | char s1[64], s2[64], ts[64]; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 117 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 118 | timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0); |
| 119 | if (!Cflag) { |
| 120 | cvtstr((double)total, s1, sizeof(s1)); |
| 121 | cvtstr(tdiv((double)total, *t), s2, sizeof(s2)); |
| 122 | printf("%s %d/%d bytes at offset %" PRId64 "\n", |
| 123 | op, total, count, offset); |
| 124 | printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n", |
| 125 | s1, cnt, ts, s2, tdiv((double)cnt, *t)); |
| 126 | } else {/* bytes,ops,time,bytes/sec,ops/sec */ |
| 127 | printf("%d,%d,%s,%.3f,%.3f\n", |
| 128 | total, cnt, ts, |
| 129 | tdiv((double)total, *t), |
| 130 | tdiv((double)cnt, *t)); |
| 131 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 134 | /* |
| 135 | * Parse multiple length statements for vectored I/O, and construct an I/O |
| 136 | * vector matching it. |
| 137 | */ |
| 138 | static void * |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 139 | create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov, |
| 140 | int pattern) |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 141 | { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 142 | size_t *sizes = g_new0(size_t, nr_iov); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 143 | size_t count = 0; |
| 144 | void *buf = NULL; |
| 145 | void *p; |
| 146 | int i; |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 147 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 148 | for (i = 0; i < nr_iov; i++) { |
| 149 | char *arg = argv[i]; |
| 150 | int64_t len; |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 151 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 152 | len = cvtnum(arg); |
| 153 | if (len < 0) { |
| 154 | printf("non-numeric length argument -- %s\n", arg); |
| 155 | goto fail; |
| 156 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 157 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 158 | /* should be SIZE_T_MAX, but that doesn't exist */ |
| 159 | if (len > INT_MAX) { |
| 160 | printf("too large length argument -- %s\n", arg); |
| 161 | goto fail; |
| 162 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 163 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 164 | if (len & 0x1ff) { |
| 165 | printf("length argument %" PRId64 |
| 166 | " is not sector aligned\n", len); |
| 167 | goto fail; |
| 168 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 169 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 170 | sizes[i] = len; |
| 171 | count += len; |
| 172 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 173 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 174 | qemu_iovec_init(qiov, nr_iov); |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 175 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 176 | buf = p = qemu_io_alloc(bs, count, pattern); |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 177 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 178 | for (i = 0; i < nr_iov; i++) { |
| 179 | qemu_iovec_add(qiov, p, sizes[i]); |
| 180 | p += sizes[i]; |
| 181 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 182 | |
Kevin Wolf | 40a0d7c | 2009-11-18 10:42:59 +0100 | [diff] [blame] | 183 | fail: |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 184 | g_free(sizes); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 185 | return buf; |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 188 | static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count, |
| 189 | int *total) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 190 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 191 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 192 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 193 | ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
| 194 | if (ret < 0) { |
| 195 | return ret; |
| 196 | } |
| 197 | *total = count; |
| 198 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 201 | static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count, |
| 202 | int *total) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 203 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 204 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 205 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 206 | ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
| 207 | if (ret < 0) { |
| 208 | return ret; |
| 209 | } |
| 210 | *total = count; |
| 211 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 214 | static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count, |
| 215 | int *total) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 216 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 217 | *total = bdrv_pread(bs, offset, (uint8_t *)buf, count); |
| 218 | if (*total < 0) { |
| 219 | return *total; |
| 220 | } |
| 221 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 224 | static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count, |
| 225 | int *total) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 226 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 227 | *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count); |
| 228 | if (*total < 0) { |
| 229 | return *total; |
| 230 | } |
| 231 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 234 | typedef struct { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 235 | BlockDriverState *bs; |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 236 | int64_t offset; |
| 237 | int count; |
| 238 | int *total; |
| 239 | int ret; |
| 240 | bool done; |
| 241 | } CoWriteZeroes; |
| 242 | |
| 243 | static void coroutine_fn co_write_zeroes_entry(void *opaque) |
| 244 | { |
| 245 | CoWriteZeroes *data = opaque; |
| 246 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 247 | data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE, |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 248 | data->count / BDRV_SECTOR_SIZE); |
| 249 | data->done = true; |
| 250 | if (data->ret < 0) { |
| 251 | *data->total = data->ret; |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | *data->total = data->count; |
| 256 | } |
| 257 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 258 | static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count, |
| 259 | int *total) |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 260 | { |
| 261 | Coroutine *co; |
| 262 | CoWriteZeroes data = { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 263 | .bs = bs, |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 264 | .offset = offset, |
| 265 | .count = count, |
| 266 | .total = total, |
| 267 | .done = false, |
| 268 | }; |
| 269 | |
| 270 | co = qemu_coroutine_create(co_write_zeroes_entry); |
| 271 | qemu_coroutine_enter(co, &data); |
| 272 | while (!data.done) { |
| 273 | qemu_aio_wait(); |
| 274 | } |
| 275 | if (data.ret < 0) { |
| 276 | return data.ret; |
| 277 | } else { |
| 278 | return 1; |
| 279 | } |
| 280 | } |
| 281 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 282 | static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset, |
| 283 | int count, int *total) |
Kevin Wolf | 791bfa3 | 2012-12-04 16:35:12 +0100 | [diff] [blame] | 284 | { |
| 285 | int ret; |
| 286 | |
| 287 | ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
| 288 | if (ret < 0) { |
| 289 | return ret; |
| 290 | } |
| 291 | *total = count; |
| 292 | return 1; |
| 293 | } |
| 294 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 295 | static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset, |
| 296 | int count, int *total) |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 297 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 298 | *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count); |
| 299 | if (*total < 0) { |
| 300 | return *total; |
| 301 | } |
| 302 | return 1; |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 305 | static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset, |
| 306 | int count, int *total) |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 307 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 308 | *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count); |
| 309 | if (*total < 0) { |
| 310 | return *total; |
| 311 | } |
| 312 | return 1; |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 313 | } |
| 314 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 315 | #define NOT_DONE 0x7fffffff |
| 316 | static void aio_rw_done(void *opaque, int ret) |
| 317 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 318 | *(int *)opaque = ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 321 | static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov, |
| 322 | int64_t offset, int *total) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 323 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 324 | int async_ret = NOT_DONE; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 325 | |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 326 | bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9, |
| 327 | aio_rw_done, &async_ret); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 328 | while (async_ret == NOT_DONE) { |
Paolo Bonzini | a5a5238 | 2012-04-12 14:00:51 +0200 | [diff] [blame] | 329 | main_loop_wait(false); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 330 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 331 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 332 | *total = qiov->size; |
| 333 | return async_ret < 0 ? async_ret : 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 336 | static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov, |
| 337 | int64_t offset, int *total) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 338 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 339 | int async_ret = NOT_DONE; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 340 | |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 341 | bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9, |
| 342 | aio_rw_done, &async_ret); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 343 | while (async_ret == NOT_DONE) { |
Paolo Bonzini | a5a5238 | 2012-04-12 14:00:51 +0200 | [diff] [blame] | 344 | main_loop_wait(false); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 345 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 346 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 347 | *total = qiov->size; |
| 348 | return async_ret < 0 ? async_ret : 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 351 | struct multiwrite_async_ret { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 352 | int num_done; |
| 353 | int error; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | static void multiwrite_cb(void *opaque, int ret) |
| 357 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 358 | struct multiwrite_async_ret *async_ret = opaque; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 359 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 360 | async_ret->num_done++; |
| 361 | if (ret < 0) { |
| 362 | async_ret->error = ret; |
| 363 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 366 | static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs, |
| 367 | int num_reqs, int *total) |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 368 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 369 | int i, ret; |
| 370 | struct multiwrite_async_ret async_ret = { |
| 371 | .num_done = 0, |
| 372 | .error = 0, |
| 373 | }; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 374 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 375 | *total = 0; |
| 376 | for (i = 0; i < num_reqs; i++) { |
| 377 | reqs[i].cb = multiwrite_cb; |
| 378 | reqs[i].opaque = &async_ret; |
| 379 | *total += reqs[i].qiov->size; |
| 380 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 381 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 382 | ret = bdrv_aio_multiwrite(bs, reqs, num_reqs); |
| 383 | if (ret < 0) { |
| 384 | return ret; |
| 385 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 386 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 387 | while (async_ret.num_done < num_reqs) { |
Paolo Bonzini | a5a5238 | 2012-04-12 14:00:51 +0200 | [diff] [blame] | 388 | main_loop_wait(false); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 389 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 390 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 391 | return async_ret.error < 0 ? async_ret.error : 1; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 392 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 393 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 394 | static void read_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 395 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 396 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 397 | "\n" |
| 398 | " reads a range of bytes from the given offset\n" |
| 399 | "\n" |
| 400 | " Example:\n" |
| 401 | " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n" |
| 402 | "\n" |
| 403 | " Reads a segment of the currently open file, optionally dumping it to the\n" |
| 404 | " standard output stream (with -v option) for subsequent inspection.\n" |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 405 | " -b, -- read from the VM state rather than the virtual disk\n" |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 406 | " -C, -- report statistics in a machine parsable format\n" |
| 407 | " -l, -- length for pattern verification (only with -P)\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 408 | " -p, -- use bdrv_pread to read the file\n" |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 409 | " -P, -- use a pattern to verify read data\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 410 | " -q, -- quiet mode, do not show I/O statistics\n" |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 411 | " -s, -- start offset for pattern verification (only with -P)\n" |
| 412 | " -v, -- dump buffer to standard output\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 413 | "\n"); |
| 414 | } |
| 415 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 416 | static int read_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 417 | |
| 418 | static const cmdinfo_t read_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 419 | .name = "read", |
| 420 | .altname = "r", |
| 421 | .cfunc = read_f, |
| 422 | .argmin = 2, |
| 423 | .argmax = -1, |
| 424 | .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len", |
| 425 | .oneline = "reads a number of bytes at a specified offset", |
| 426 | .help = read_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 429 | static int read_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 430 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 431 | struct timeval t1, t2; |
| 432 | int Cflag = 0, pflag = 0, qflag = 0, vflag = 0; |
| 433 | int Pflag = 0, sflag = 0, lflag = 0, bflag = 0; |
| 434 | int c, cnt; |
| 435 | char *buf; |
| 436 | int64_t offset; |
| 437 | int count; |
| 438 | /* Some compilers get confused and warn if this is not initialized. */ |
| 439 | int total = 0; |
| 440 | int pattern = 0, pattern_offset = 0, pattern_count = 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 441 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 442 | while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) { |
| 443 | switch (c) { |
| 444 | case 'b': |
| 445 | bflag = 1; |
| 446 | break; |
| 447 | case 'C': |
| 448 | Cflag = 1; |
| 449 | break; |
| 450 | case 'l': |
| 451 | lflag = 1; |
| 452 | pattern_count = cvtnum(optarg); |
| 453 | if (pattern_count < 0) { |
| 454 | printf("non-numeric length argument -- %s\n", optarg); |
| 455 | return 0; |
| 456 | } |
| 457 | break; |
| 458 | case 'p': |
| 459 | pflag = 1; |
| 460 | break; |
| 461 | case 'P': |
| 462 | Pflag = 1; |
| 463 | pattern = parse_pattern(optarg); |
| 464 | if (pattern < 0) { |
| 465 | return 0; |
| 466 | } |
| 467 | break; |
| 468 | case 'q': |
| 469 | qflag = 1; |
| 470 | break; |
| 471 | case 's': |
| 472 | sflag = 1; |
| 473 | pattern_offset = cvtnum(optarg); |
| 474 | if (pattern_offset < 0) { |
| 475 | printf("non-numeric length argument -- %s\n", optarg); |
| 476 | return 0; |
| 477 | } |
| 478 | break; |
| 479 | case 'v': |
| 480 | vflag = 1; |
| 481 | break; |
| 482 | default: |
| 483 | return command_usage(&read_cmd); |
| 484 | } |
| 485 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 486 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 487 | if (optind != argc - 2) { |
| 488 | return command_usage(&read_cmd); |
| 489 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 490 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 491 | if (bflag && pflag) { |
| 492 | printf("-b and -p cannot be specified at the same time\n"); |
| 493 | return 0; |
| 494 | } |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 495 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 496 | offset = cvtnum(argv[optind]); |
| 497 | if (offset < 0) { |
| 498 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 499 | return 0; |
| 500 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 501 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 502 | optind++; |
| 503 | count = cvtnum(argv[optind]); |
| 504 | if (count < 0) { |
| 505 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 506 | return 0; |
| 507 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 508 | |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 509 | if (!Pflag && (lflag || sflag)) { |
| 510 | return command_usage(&read_cmd); |
| 511 | } |
| 512 | |
| 513 | if (!lflag) { |
| 514 | pattern_count = count - pattern_offset; |
| 515 | } |
| 516 | |
| 517 | if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) { |
Dong Xu Wang | 07f3507 | 2011-11-22 18:06:26 +0800 | [diff] [blame] | 518 | printf("pattern verification range exceeds end of read data\n"); |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Devin Nakamura | 5afc8b3 | 2011-07-11 11:20:25 -0400 | [diff] [blame] | 522 | if (!pflag) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 523 | if (offset & 0x1ff) { |
| 524 | printf("offset %" PRId64 " is not sector aligned\n", |
| 525 | offset); |
| 526 | return 0; |
| 527 | } |
| 528 | if (count & 0x1ff) { |
| 529 | printf("count %d is not sector aligned\n", |
| 530 | count); |
| 531 | return 0; |
| 532 | } |
Devin Nakamura | 5afc8b3 | 2011-07-11 11:20:25 -0400 | [diff] [blame] | 533 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 534 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 535 | buf = qemu_io_alloc(bs, count, 0xab); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 536 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 537 | gettimeofday(&t1, NULL); |
| 538 | if (pflag) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 539 | cnt = do_pread(bs, buf, offset, count, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 540 | } else if (bflag) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 541 | cnt = do_load_vmstate(bs, buf, offset, count, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 542 | } else { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 543 | cnt = do_read(bs, buf, offset, count, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 544 | } |
| 545 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 546 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 547 | if (cnt < 0) { |
| 548 | printf("read failed: %s\n", strerror(-cnt)); |
| 549 | goto out; |
| 550 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 551 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 552 | if (Pflag) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 553 | void *cmp_buf = g_malloc(pattern_count); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 554 | memset(cmp_buf, pattern, pattern_count); |
| 555 | if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) { |
| 556 | printf("Pattern verification failed at offset %" |
| 557 | PRId64 ", %d bytes\n", |
| 558 | offset + pattern_offset, pattern_count); |
| 559 | } |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 560 | g_free(cmp_buf); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 561 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 562 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 563 | if (qflag) { |
| 564 | goto out; |
| 565 | } |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 566 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 567 | if (vflag) { |
| 568 | dump_buffer(buf, offset, count); |
| 569 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 570 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 571 | /* Finally, report back -- -C gives a parsable format */ |
| 572 | t2 = tsub(t2, t1); |
| 573 | print_report("read", &t2, offset, count, total, cnt, Cflag); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 574 | |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 575 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 576 | qemu_io_free(buf); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 577 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 578 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 581 | static void readv_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 582 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 583 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 584 | "\n" |
| 585 | " reads a range of bytes from the given offset into multiple buffers\n" |
| 586 | "\n" |
| 587 | " Example:\n" |
| 588 | " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" |
| 589 | "\n" |
| 590 | " Reads a segment of the currently open file, optionally dumping it to the\n" |
| 591 | " standard output stream (with -v option) for subsequent inspection.\n" |
| 592 | " Uses multiple iovec buffers if more than one byte range is specified.\n" |
| 593 | " -C, -- report statistics in a machine parsable format\n" |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 594 | " -P, -- use a pattern to verify read data\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 595 | " -v, -- dump buffer to standard output\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 596 | " -q, -- quiet mode, do not show I/O statistics\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 597 | "\n"); |
| 598 | } |
| 599 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 600 | static int readv_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 601 | |
| 602 | static const cmdinfo_t readv_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 603 | .name = "readv", |
| 604 | .cfunc = readv_f, |
| 605 | .argmin = 2, |
| 606 | .argmax = -1, |
| 607 | .args = "[-Cqv] [-P pattern ] off len [len..]", |
| 608 | .oneline = "reads a number of bytes at a specified offset", |
| 609 | .help = readv_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 610 | }; |
| 611 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 612 | static int readv_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 613 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 614 | struct timeval t1, t2; |
| 615 | int Cflag = 0, qflag = 0, vflag = 0; |
| 616 | int c, cnt; |
| 617 | char *buf; |
| 618 | int64_t offset; |
| 619 | /* Some compilers get confused and warn if this is not initialized. */ |
| 620 | int total = 0; |
| 621 | int nr_iov; |
| 622 | QEMUIOVector qiov; |
| 623 | int pattern = 0; |
| 624 | int Pflag = 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 625 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 626 | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
| 627 | switch (c) { |
| 628 | case 'C': |
| 629 | Cflag = 1; |
| 630 | break; |
| 631 | case 'P': |
| 632 | Pflag = 1; |
| 633 | pattern = parse_pattern(optarg); |
| 634 | if (pattern < 0) { |
| 635 | return 0; |
| 636 | } |
| 637 | break; |
| 638 | case 'q': |
| 639 | qflag = 1; |
| 640 | break; |
| 641 | case 'v': |
| 642 | vflag = 1; |
| 643 | break; |
| 644 | default: |
| 645 | return command_usage(&readv_cmd); |
| 646 | } |
| 647 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 648 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 649 | if (optind > argc - 2) { |
| 650 | return command_usage(&readv_cmd); |
| 651 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 652 | |
| 653 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 654 | offset = cvtnum(argv[optind]); |
| 655 | if (offset < 0) { |
| 656 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 657 | return 0; |
| 658 | } |
| 659 | optind++; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 660 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 661 | if (offset & 0x1ff) { |
| 662 | printf("offset %" PRId64 " is not sector aligned\n", |
| 663 | offset); |
| 664 | return 0; |
| 665 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 666 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 667 | nr_iov = argc - optind; |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 668 | buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 669 | if (buf == NULL) { |
| 670 | return 0; |
| 671 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 672 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 673 | gettimeofday(&t1, NULL); |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 674 | cnt = do_aio_readv(bs, &qiov, offset, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 675 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 676 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 677 | if (cnt < 0) { |
| 678 | printf("readv failed: %s\n", strerror(-cnt)); |
| 679 | goto out; |
| 680 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 681 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 682 | if (Pflag) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 683 | void *cmp_buf = g_malloc(qiov.size); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 684 | memset(cmp_buf, pattern, qiov.size); |
| 685 | if (memcmp(buf, cmp_buf, qiov.size)) { |
| 686 | printf("Pattern verification failed at offset %" |
| 687 | PRId64 ", %zd bytes\n", offset, qiov.size); |
| 688 | } |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 689 | g_free(cmp_buf); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 690 | } |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 691 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 692 | if (qflag) { |
| 693 | goto out; |
| 694 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 695 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 696 | if (vflag) { |
| 697 | dump_buffer(buf, offset, qiov.size); |
| 698 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 699 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 700 | /* Finally, report back -- -C gives a parsable format */ |
| 701 | t2 = tsub(t2, t1); |
| 702 | print_report("read", &t2, offset, qiov.size, total, cnt, Cflag); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 703 | |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 704 | out: |
Kevin Wolf | 9e55953 | 2012-07-02 15:13:53 +0200 | [diff] [blame] | 705 | qemu_iovec_destroy(&qiov); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 706 | qemu_io_free(buf); |
| 707 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 710 | static void write_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 711 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 712 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 713 | "\n" |
| 714 | " writes a range of bytes from the given offset\n" |
| 715 | "\n" |
| 716 | " Example:\n" |
| 717 | " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n" |
| 718 | "\n" |
| 719 | " Writes into a segment of the currently open file, using a buffer\n" |
| 720 | " filled with a set pattern (0xcdcdcdcd).\n" |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 721 | " -b, -- write to the VM state rather than the virtual disk\n" |
Kevin Wolf | 791bfa3 | 2012-12-04 16:35:12 +0100 | [diff] [blame] | 722 | " -c, -- write compressed data with bdrv_write_compressed\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 723 | " -p, -- use bdrv_pwrite to write the file\n" |
| 724 | " -P, -- use different pattern to fill file\n" |
| 725 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 726 | " -q, -- quiet mode, do not show I/O statistics\n" |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 727 | " -z, -- write zeroes using bdrv_co_write_zeroes\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 728 | "\n"); |
| 729 | } |
| 730 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 731 | static int write_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 732 | |
| 733 | static const cmdinfo_t write_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 734 | .name = "write", |
| 735 | .altname = "w", |
| 736 | .cfunc = write_f, |
| 737 | .argmin = 2, |
| 738 | .argmax = -1, |
Kevin Wolf | 791bfa3 | 2012-12-04 16:35:12 +0100 | [diff] [blame] | 739 | .args = "[-bcCpqz] [-P pattern ] off len", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 740 | .oneline = "writes a number of bytes at a specified offset", |
| 741 | .help = write_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 742 | }; |
| 743 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 744 | static int write_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 745 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 746 | struct timeval t1, t2; |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 747 | int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0; |
Kevin Wolf | 791bfa3 | 2012-12-04 16:35:12 +0100 | [diff] [blame] | 748 | int cflag = 0; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 749 | int c, cnt; |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 750 | char *buf = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 751 | int64_t offset; |
| 752 | int count; |
| 753 | /* Some compilers get confused and warn if this is not initialized. */ |
| 754 | int total = 0; |
| 755 | int pattern = 0xcd; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 756 | |
Kevin Wolf | 791bfa3 | 2012-12-04 16:35:12 +0100 | [diff] [blame] | 757 | while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 758 | switch (c) { |
| 759 | case 'b': |
| 760 | bflag = 1; |
| 761 | break; |
Kevin Wolf | 791bfa3 | 2012-12-04 16:35:12 +0100 | [diff] [blame] | 762 | case 'c': |
| 763 | cflag = 1; |
| 764 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 765 | case 'C': |
| 766 | Cflag = 1; |
| 767 | break; |
| 768 | case 'p': |
| 769 | pflag = 1; |
| 770 | break; |
| 771 | case 'P': |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 772 | Pflag = 1; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 773 | pattern = parse_pattern(optarg); |
| 774 | if (pattern < 0) { |
| 775 | return 0; |
| 776 | } |
| 777 | break; |
| 778 | case 'q': |
| 779 | qflag = 1; |
| 780 | break; |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 781 | case 'z': |
| 782 | zflag = 1; |
| 783 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 784 | default: |
| 785 | return command_usage(&write_cmd); |
| 786 | } |
| 787 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 788 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 789 | if (optind != argc - 2) { |
| 790 | return command_usage(&write_cmd); |
| 791 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 792 | |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 793 | if (bflag + pflag + zflag > 1) { |
| 794 | printf("-b, -p, or -z cannot be specified at the same time\n"); |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | if (zflag && Pflag) { |
| 799 | printf("-z and -P cannot be specified at the same time\n"); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 800 | return 0; |
| 801 | } |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 802 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 803 | offset = cvtnum(argv[optind]); |
| 804 | if (offset < 0) { |
| 805 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 806 | return 0; |
| 807 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 808 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 809 | optind++; |
| 810 | count = cvtnum(argv[optind]); |
| 811 | if (count < 0) { |
| 812 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 813 | return 0; |
| 814 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 815 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 816 | if (!pflag) { |
| 817 | if (offset & 0x1ff) { |
| 818 | printf("offset %" PRId64 " is not sector aligned\n", |
| 819 | offset); |
| 820 | return 0; |
| 821 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 822 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 823 | if (count & 0x1ff) { |
| 824 | printf("count %d is not sector aligned\n", |
| 825 | count); |
| 826 | return 0; |
| 827 | } |
| 828 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 829 | |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 830 | if (!zflag) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 831 | buf = qemu_io_alloc(bs, count, pattern); |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 832 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 833 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 834 | gettimeofday(&t1, NULL); |
| 835 | if (pflag) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 836 | cnt = do_pwrite(bs, buf, offset, count, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 837 | } else if (bflag) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 838 | cnt = do_save_vmstate(bs, buf, offset, count, &total); |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 839 | } else if (zflag) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 840 | cnt = do_co_write_zeroes(bs, offset, count, &total); |
Kevin Wolf | 791bfa3 | 2012-12-04 16:35:12 +0100 | [diff] [blame] | 841 | } else if (cflag) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 842 | cnt = do_write_compressed(bs, buf, offset, count, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 843 | } else { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 844 | cnt = do_write(bs, buf, offset, count, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 845 | } |
| 846 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 847 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 848 | if (cnt < 0) { |
| 849 | printf("write failed: %s\n", strerror(-cnt)); |
| 850 | goto out; |
| 851 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 852 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 853 | if (qflag) { |
| 854 | goto out; |
| 855 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 856 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 857 | /* Finally, report back -- -C gives a parsable format */ |
| 858 | t2 = tsub(t2, t1); |
| 859 | print_report("wrote", &t2, offset, count, total, cnt, Cflag); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 860 | |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 861 | out: |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 862 | if (!zflag) { |
| 863 | qemu_io_free(buf); |
| 864 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 865 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 866 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 867 | } |
| 868 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 869 | static void |
| 870 | writev_help(void) |
| 871 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 872 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 873 | "\n" |
| 874 | " writes a range of bytes from the given offset source from multiple buffers\n" |
| 875 | "\n" |
| 876 | " Example:\n" |
| 877 | " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" |
| 878 | "\n" |
| 879 | " Writes into a segment of the currently open file, using a buffer\n" |
| 880 | " filled with a set pattern (0xcdcdcdcd).\n" |
| 881 | " -P, -- use different pattern to fill file\n" |
| 882 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 883 | " -q, -- quiet mode, do not show I/O statistics\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 884 | "\n"); |
| 885 | } |
| 886 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 887 | static int writev_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 888 | |
| 889 | static const cmdinfo_t writev_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 890 | .name = "writev", |
| 891 | .cfunc = writev_f, |
| 892 | .argmin = 2, |
| 893 | .argmax = -1, |
| 894 | .args = "[-Cq] [-P pattern ] off len [len..]", |
| 895 | .oneline = "writes a number of bytes at a specified offset", |
| 896 | .help = writev_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 897 | }; |
| 898 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 899 | static int writev_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 900 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 901 | struct timeval t1, t2; |
| 902 | int Cflag = 0, qflag = 0; |
| 903 | int c, cnt; |
| 904 | char *buf; |
| 905 | int64_t offset; |
| 906 | /* Some compilers get confused and warn if this is not initialized. */ |
| 907 | int total = 0; |
| 908 | int nr_iov; |
| 909 | int pattern = 0xcd; |
| 910 | QEMUIOVector qiov; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 911 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 912 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
| 913 | switch (c) { |
| 914 | case 'C': |
| 915 | Cflag = 1; |
| 916 | break; |
| 917 | case 'q': |
| 918 | qflag = 1; |
| 919 | break; |
| 920 | case 'P': |
| 921 | pattern = parse_pattern(optarg); |
| 922 | if (pattern < 0) { |
| 923 | return 0; |
| 924 | } |
| 925 | break; |
| 926 | default: |
| 927 | return command_usage(&writev_cmd); |
| 928 | } |
| 929 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 930 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 931 | if (optind > argc - 2) { |
| 932 | return command_usage(&writev_cmd); |
| 933 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 934 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 935 | offset = cvtnum(argv[optind]); |
| 936 | if (offset < 0) { |
| 937 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 938 | return 0; |
| 939 | } |
| 940 | optind++; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 941 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 942 | if (offset & 0x1ff) { |
| 943 | printf("offset %" PRId64 " is not sector aligned\n", |
| 944 | offset); |
| 945 | return 0; |
| 946 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 947 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 948 | nr_iov = argc - optind; |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 949 | buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 950 | if (buf == NULL) { |
| 951 | return 0; |
| 952 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 953 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 954 | gettimeofday(&t1, NULL); |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 955 | cnt = do_aio_writev(bs, &qiov, offset, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 956 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 957 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 958 | if (cnt < 0) { |
| 959 | printf("writev failed: %s\n", strerror(-cnt)); |
| 960 | goto out; |
| 961 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 962 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 963 | if (qflag) { |
| 964 | goto out; |
| 965 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 966 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 967 | /* Finally, report back -- -C gives a parsable format */ |
| 968 | t2 = tsub(t2, t1); |
| 969 | print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag); |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 970 | out: |
Kevin Wolf | 9e55953 | 2012-07-02 15:13:53 +0200 | [diff] [blame] | 971 | qemu_iovec_destroy(&qiov); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 972 | qemu_io_free(buf); |
| 973 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 976 | static void multiwrite_help(void) |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 977 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 978 | printf( |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 979 | "\n" |
| 980 | " writes a range of bytes from the given offset source from multiple buffers,\n" |
| 981 | " in a batch of requests that may be merged by qemu\n" |
| 982 | "\n" |
| 983 | " Example:\n" |
Stefan Weil | b2bedb2 | 2011-09-12 22:33:01 +0200 | [diff] [blame] | 984 | " 'multiwrite 512 1k 1k ; 4k 1k'\n" |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 985 | " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n" |
| 986 | "\n" |
| 987 | " Writes into a segment of the currently open file, using a buffer\n" |
| 988 | " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n" |
| 989 | " by one for each request contained in the multiwrite command.\n" |
| 990 | " -P, -- use different pattern to fill file\n" |
| 991 | " -C, -- report statistics in a machine parsable format\n" |
| 992 | " -q, -- quiet mode, do not show I/O statistics\n" |
| 993 | "\n"); |
| 994 | } |
| 995 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 996 | static int multiwrite_f(BlockDriverState *bs, int argc, char **argv); |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 997 | |
| 998 | static const cmdinfo_t multiwrite_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 999 | .name = "multiwrite", |
| 1000 | .cfunc = multiwrite_f, |
| 1001 | .argmin = 2, |
| 1002 | .argmax = -1, |
| 1003 | .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]", |
| 1004 | .oneline = "issues multiple write requests at once", |
| 1005 | .help = multiwrite_help, |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1006 | }; |
| 1007 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1008 | static int multiwrite_f(BlockDriverState *bs, int argc, char **argv) |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1009 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1010 | struct timeval t1, t2; |
| 1011 | int Cflag = 0, qflag = 0; |
| 1012 | int c, cnt; |
| 1013 | char **buf; |
| 1014 | int64_t offset, first_offset = 0; |
| 1015 | /* Some compilers get confused and warn if this is not initialized. */ |
| 1016 | int total = 0; |
| 1017 | int nr_iov; |
| 1018 | int nr_reqs; |
| 1019 | int pattern = 0xcd; |
| 1020 | QEMUIOVector *qiovs; |
| 1021 | int i; |
| 1022 | BlockRequest *reqs; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1023 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1024 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
| 1025 | switch (c) { |
| 1026 | case 'C': |
| 1027 | Cflag = 1; |
| 1028 | break; |
| 1029 | case 'q': |
| 1030 | qflag = 1; |
| 1031 | break; |
| 1032 | case 'P': |
| 1033 | pattern = parse_pattern(optarg); |
| 1034 | if (pattern < 0) { |
| 1035 | return 0; |
| 1036 | } |
| 1037 | break; |
| 1038 | default: |
| 1039 | return command_usage(&writev_cmd); |
| 1040 | } |
| 1041 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1042 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1043 | if (optind > argc - 2) { |
| 1044 | return command_usage(&writev_cmd); |
| 1045 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1046 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1047 | nr_reqs = 1; |
| 1048 | for (i = optind; i < argc; i++) { |
| 1049 | if (!strcmp(argv[i], ";")) { |
| 1050 | nr_reqs++; |
| 1051 | } |
| 1052 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1053 | |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1054 | reqs = g_malloc0(nr_reqs * sizeof(*reqs)); |
| 1055 | buf = g_malloc0(nr_reqs * sizeof(*buf)); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1056 | qiovs = g_malloc(nr_reqs * sizeof(*qiovs)); |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1057 | |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1058 | for (i = 0; i < nr_reqs && optind < argc; i++) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1059 | int j; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1060 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1061 | /* Read the offset of the request */ |
| 1062 | offset = cvtnum(argv[optind]); |
| 1063 | if (offset < 0) { |
| 1064 | printf("non-numeric offset argument -- %s\n", argv[optind]); |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1065 | goto out; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1066 | } |
| 1067 | optind++; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1068 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1069 | if (offset & 0x1ff) { |
| 1070 | printf("offset %lld is not sector aligned\n", |
| 1071 | (long long)offset); |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1072 | goto out; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1073 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1074 | |
| 1075 | if (i == 0) { |
| 1076 | first_offset = offset; |
| 1077 | } |
| 1078 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1079 | /* Read lengths for qiov entries */ |
| 1080 | for (j = optind; j < argc; j++) { |
| 1081 | if (!strcmp(argv[j], ";")) { |
| 1082 | break; |
| 1083 | } |
| 1084 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1085 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1086 | nr_iov = j - optind; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1087 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1088 | /* Build request */ |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1089 | buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1090 | if (buf[i] == NULL) { |
| 1091 | goto out; |
| 1092 | } |
| 1093 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1094 | reqs[i].qiov = &qiovs[i]; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1095 | reqs[i].sector = offset >> 9; |
| 1096 | reqs[i].nb_sectors = reqs[i].qiov->size >> 9; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1097 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1098 | optind = j + 1; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1099 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1100 | pattern++; |
| 1101 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1102 | |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1103 | /* If there were empty requests at the end, ignore them */ |
| 1104 | nr_reqs = i; |
| 1105 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1106 | gettimeofday(&t1, NULL); |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1107 | cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1108 | gettimeofday(&t2, NULL); |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1109 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1110 | if (cnt < 0) { |
| 1111 | printf("aio_multiwrite failed: %s\n", strerror(-cnt)); |
| 1112 | goto out; |
| 1113 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1114 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1115 | if (qflag) { |
| 1116 | goto out; |
| 1117 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1118 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1119 | /* Finally, report back -- -C gives a parsable format */ |
| 1120 | t2 = tsub(t2, t1); |
| 1121 | print_report("wrote", &t2, first_offset, total, total, cnt, Cflag); |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1122 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1123 | for (i = 0; i < nr_reqs; i++) { |
| 1124 | qemu_io_free(buf[i]); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1125 | if (reqs[i].qiov != NULL) { |
| 1126 | qemu_iovec_destroy(&qiovs[i]); |
| 1127 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1128 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1129 | g_free(buf); |
| 1130 | g_free(reqs); |
| 1131 | g_free(qiovs); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1132 | return 0; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1135 | struct aio_ctx { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1136 | QEMUIOVector qiov; |
| 1137 | int64_t offset; |
| 1138 | char *buf; |
| 1139 | int qflag; |
| 1140 | int vflag; |
| 1141 | int Cflag; |
| 1142 | int Pflag; |
| 1143 | int pattern; |
| 1144 | struct timeval t1; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1145 | }; |
| 1146 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1147 | static void aio_write_done(void *opaque, int ret) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1148 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1149 | struct aio_ctx *ctx = opaque; |
| 1150 | struct timeval t2; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1151 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1152 | gettimeofday(&t2, NULL); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1153 | |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1154 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1155 | if (ret < 0) { |
| 1156 | printf("aio_write failed: %s\n", strerror(-ret)); |
| 1157 | goto out; |
| 1158 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1159 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1160 | if (ctx->qflag) { |
| 1161 | goto out; |
| 1162 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1163 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1164 | /* Finally, report back -- -C gives a parsable format */ |
| 1165 | t2 = tsub(t2, ctx->t1); |
| 1166 | print_report("wrote", &t2, ctx->offset, ctx->qiov.size, |
| 1167 | ctx->qiov.size, 1, ctx->Cflag); |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 1168 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1169 | qemu_io_free(ctx->buf); |
Kevin Wolf | 9e55953 | 2012-07-02 15:13:53 +0200 | [diff] [blame] | 1170 | qemu_iovec_destroy(&ctx->qiov); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1171 | g_free(ctx); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1174 | static void aio_read_done(void *opaque, int ret) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1175 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1176 | struct aio_ctx *ctx = opaque; |
| 1177 | struct timeval t2; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1178 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1179 | gettimeofday(&t2, NULL); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1180 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1181 | if (ret < 0) { |
| 1182 | printf("readv failed: %s\n", strerror(-ret)); |
| 1183 | goto out; |
| 1184 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1185 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1186 | if (ctx->Pflag) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1187 | void *cmp_buf = g_malloc(ctx->qiov.size); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1188 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1189 | memset(cmp_buf, ctx->pattern, ctx->qiov.size); |
| 1190 | if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) { |
| 1191 | printf("Pattern verification failed at offset %" |
| 1192 | PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size); |
| 1193 | } |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1194 | g_free(cmp_buf); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1195 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1196 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1197 | if (ctx->qflag) { |
| 1198 | goto out; |
| 1199 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1200 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1201 | if (ctx->vflag) { |
| 1202 | dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); |
| 1203 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1204 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1205 | /* Finally, report back -- -C gives a parsable format */ |
| 1206 | t2 = tsub(t2, ctx->t1); |
| 1207 | print_report("read", &t2, ctx->offset, ctx->qiov.size, |
| 1208 | ctx->qiov.size, 1, ctx->Cflag); |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 1209 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1210 | qemu_io_free(ctx->buf); |
Kevin Wolf | 9e55953 | 2012-07-02 15:13:53 +0200 | [diff] [blame] | 1211 | qemu_iovec_destroy(&ctx->qiov); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1212 | g_free(ctx); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1215 | static void aio_read_help(void) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1216 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1217 | printf( |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1218 | "\n" |
| 1219 | " asynchronously reads a range of bytes from the given offset\n" |
| 1220 | "\n" |
| 1221 | " Example:\n" |
| 1222 | " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" |
| 1223 | "\n" |
| 1224 | " Reads a segment of the currently open file, optionally dumping it to the\n" |
| 1225 | " standard output stream (with -v option) for subsequent inspection.\n" |
Christoph Hellwig | e432cef | 2010-03-28 12:19:31 +0200 | [diff] [blame] | 1226 | " The read is performed asynchronously and the aio_flush command must be\n" |
Laszlo Ersek | 96bab41 | 2012-01-24 21:13:28 +0100 | [diff] [blame] | 1227 | " used to ensure all outstanding aio requests have been completed.\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1228 | " -C, -- report statistics in a machine parsable format\n" |
| 1229 | " -P, -- use a pattern to verify read data\n" |
| 1230 | " -v, -- dump buffer to standard output\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 1231 | " -q, -- quiet mode, do not show I/O statistics\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1232 | "\n"); |
| 1233 | } |
| 1234 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1235 | static int aio_read_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1236 | |
| 1237 | static const cmdinfo_t aio_read_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1238 | .name = "aio_read", |
| 1239 | .cfunc = aio_read_f, |
| 1240 | .argmin = 2, |
| 1241 | .argmax = -1, |
| 1242 | .args = "[-Cqv] [-P pattern ] off len [len..]", |
| 1243 | .oneline = "asynchronously reads a number of bytes", |
| 1244 | .help = aio_read_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1245 | }; |
| 1246 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1247 | static int aio_read_f(BlockDriverState *bs, int argc, char **argv) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1248 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1249 | int nr_iov, c; |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1250 | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1251 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1252 | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
| 1253 | switch (c) { |
| 1254 | case 'C': |
| 1255 | ctx->Cflag = 1; |
| 1256 | break; |
| 1257 | case 'P': |
| 1258 | ctx->Pflag = 1; |
| 1259 | ctx->pattern = parse_pattern(optarg); |
| 1260 | if (ctx->pattern < 0) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1261 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | break; |
| 1265 | case 'q': |
| 1266 | ctx->qflag = 1; |
| 1267 | break; |
| 1268 | case 'v': |
| 1269 | ctx->vflag = 1; |
| 1270 | break; |
| 1271 | default: |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1272 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1273 | return command_usage(&aio_read_cmd); |
| 1274 | } |
| 1275 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1276 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1277 | if (optind > argc - 2) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1278 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1279 | return command_usage(&aio_read_cmd); |
| 1280 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1281 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1282 | ctx->offset = cvtnum(argv[optind]); |
| 1283 | if (ctx->offset < 0) { |
| 1284 | printf("non-numeric length argument -- %s\n", argv[optind]); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1285 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1286 | return 0; |
| 1287 | } |
| 1288 | optind++; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1289 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1290 | if (ctx->offset & 0x1ff) { |
| 1291 | printf("offset %" PRId64 " is not sector aligned\n", |
| 1292 | ctx->offset); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1293 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1294 | return 0; |
| 1295 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1296 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1297 | nr_iov = argc - optind; |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1298 | ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1299 | if (ctx->buf == NULL) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1300 | g_free(ctx); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1301 | return 0; |
| 1302 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1303 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1304 | gettimeofday(&ctx->t1, NULL); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 1305 | bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov, |
| 1306 | ctx->qiov.size >> 9, aio_read_done, ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1307 | return 0; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1310 | static void aio_write_help(void) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1311 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1312 | printf( |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1313 | "\n" |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1314 | " asynchronously writes a range of bytes from the given offset source\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1315 | " from multiple buffers\n" |
| 1316 | "\n" |
| 1317 | " Example:\n" |
| 1318 | " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" |
| 1319 | "\n" |
| 1320 | " Writes into a segment of the currently open file, using a buffer\n" |
| 1321 | " filled with a set pattern (0xcdcdcdcd).\n" |
Christoph Hellwig | e432cef | 2010-03-28 12:19:31 +0200 | [diff] [blame] | 1322 | " The write is performed asynchronously and the aio_flush command must be\n" |
Laszlo Ersek | 96bab41 | 2012-01-24 21:13:28 +0100 | [diff] [blame] | 1323 | " used to ensure all outstanding aio requests have been completed.\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1324 | " -P, -- use different pattern to fill file\n" |
| 1325 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 1326 | " -q, -- quiet mode, do not show I/O statistics\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1327 | "\n"); |
| 1328 | } |
| 1329 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1330 | static int aio_write_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1331 | |
| 1332 | static const cmdinfo_t aio_write_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1333 | .name = "aio_write", |
| 1334 | .cfunc = aio_write_f, |
| 1335 | .argmin = 2, |
| 1336 | .argmax = -1, |
| 1337 | .args = "[-Cq] [-P pattern ] off len [len..]", |
| 1338 | .oneline = "asynchronously writes a number of bytes", |
| 1339 | .help = aio_write_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1340 | }; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1341 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1342 | static int aio_write_f(BlockDriverState *bs, int argc, char **argv) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1343 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1344 | int nr_iov, c; |
| 1345 | int pattern = 0xcd; |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1346 | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1347 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1348 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
| 1349 | switch (c) { |
| 1350 | case 'C': |
| 1351 | ctx->Cflag = 1; |
| 1352 | break; |
| 1353 | case 'q': |
| 1354 | ctx->qflag = 1; |
| 1355 | break; |
| 1356 | case 'P': |
| 1357 | pattern = parse_pattern(optarg); |
| 1358 | if (pattern < 0) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1359 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | break; |
| 1363 | default: |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1364 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1365 | return command_usage(&aio_write_cmd); |
| 1366 | } |
| 1367 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1368 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1369 | if (optind > argc - 2) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1370 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1371 | return command_usage(&aio_write_cmd); |
| 1372 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1373 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1374 | ctx->offset = cvtnum(argv[optind]); |
| 1375 | if (ctx->offset < 0) { |
| 1376 | printf("non-numeric length argument -- %s\n", argv[optind]); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1377 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1378 | return 0; |
| 1379 | } |
| 1380 | optind++; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1381 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1382 | if (ctx->offset & 0x1ff) { |
| 1383 | printf("offset %" PRId64 " is not sector aligned\n", |
| 1384 | ctx->offset); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1385 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1388 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1389 | nr_iov = argc - optind; |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1390 | ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1391 | if (ctx->buf == NULL) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1392 | g_free(ctx); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1395 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1396 | gettimeofday(&ctx->t1, NULL); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 1397 | bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov, |
| 1398 | ctx->qiov.size >> 9, aio_write_done, ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1399 | return 0; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1402 | static int aio_flush_f(BlockDriverState *bs, int argc, char **argv) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1403 | { |
Kevin Wolf | e7c8b09 | 2012-11-13 16:41:22 +0100 | [diff] [blame] | 1404 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1405 | return 0; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | static const cmdinfo_t aio_flush_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1409 | .name = "aio_flush", |
| 1410 | .cfunc = aio_flush_f, |
| 1411 | .oneline = "completes all outstanding aio requests" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1412 | }; |
| 1413 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1414 | static int flush_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1415 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1416 | bdrv_flush(bs); |
| 1417 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static const cmdinfo_t flush_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1421 | .name = "flush", |
| 1422 | .altname = "f", |
| 1423 | .cfunc = flush_f, |
| 1424 | .oneline = "flush all in-core file state to disk", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1425 | }; |
| 1426 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1427 | static int truncate_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1428 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1429 | int64_t offset; |
| 1430 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1431 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1432 | offset = cvtnum(argv[1]); |
| 1433 | if (offset < 0) { |
| 1434 | printf("non-numeric truncate argument -- %s\n", argv[1]); |
| 1435 | return 0; |
| 1436 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1437 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1438 | ret = bdrv_truncate(bs, offset); |
| 1439 | if (ret < 0) { |
| 1440 | printf("truncate: %s\n", strerror(-ret)); |
| 1441 | return 0; |
| 1442 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1443 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1444 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | static const cmdinfo_t truncate_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1448 | .name = "truncate", |
| 1449 | .altname = "t", |
| 1450 | .cfunc = truncate_f, |
| 1451 | .argmin = 1, |
| 1452 | .argmax = 1, |
| 1453 | .args = "off", |
| 1454 | .oneline = "truncates the current file at the given offset", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1455 | }; |
| 1456 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1457 | static int length_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1458 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1459 | int64_t size; |
| 1460 | char s1[64]; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1461 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1462 | size = bdrv_getlength(bs); |
| 1463 | if (size < 0) { |
| 1464 | printf("getlength: %s\n", strerror(-size)); |
| 1465 | return 0; |
| 1466 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1467 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1468 | cvtstr(size, s1, sizeof(s1)); |
| 1469 | printf("%s\n", s1); |
| 1470 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | static const cmdinfo_t length_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1475 | .name = "length", |
| 1476 | .altname = "l", |
| 1477 | .cfunc = length_f, |
| 1478 | .oneline = "gets the length of the current file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1479 | }; |
| 1480 | |
| 1481 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1482 | static int info_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1483 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1484 | BlockDriverInfo bdi; |
| 1485 | char s1[64], s2[64]; |
| 1486 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1487 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1488 | if (bs->drv && bs->drv->format_name) { |
| 1489 | printf("format name: %s\n", bs->drv->format_name); |
| 1490 | } |
| 1491 | if (bs->drv && bs->drv->protocol_name) { |
| 1492 | printf("format name: %s\n", bs->drv->protocol_name); |
| 1493 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1494 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1495 | ret = bdrv_get_info(bs, &bdi); |
| 1496 | if (ret) { |
| 1497 | return 0; |
| 1498 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1499 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1500 | cvtstr(bdi.cluster_size, s1, sizeof(s1)); |
| 1501 | cvtstr(bdi.vm_state_offset, s2, sizeof(s2)); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1502 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1503 | printf("cluster size: %s\n", s1); |
| 1504 | printf("vm state offset: %s\n", s2); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1505 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1506 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | static const cmdinfo_t info_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1512 | .name = "info", |
| 1513 | .altname = "i", |
| 1514 | .cfunc = info_f, |
| 1515 | .oneline = "prints information about the current file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1516 | }; |
| 1517 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1518 | static void discard_help(void) |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1519 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1520 | printf( |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1521 | "\n" |
| 1522 | " discards a range of bytes from the given offset\n" |
| 1523 | "\n" |
| 1524 | " Example:\n" |
| 1525 | " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n" |
| 1526 | "\n" |
| 1527 | " Discards a segment of the currently open file.\n" |
| 1528 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 1529 | " -q, -- quiet mode, do not show I/O statistics\n" |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1530 | "\n"); |
| 1531 | } |
| 1532 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1533 | static int discard_f(BlockDriverState *bs, int argc, char **argv); |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1534 | |
| 1535 | static const cmdinfo_t discard_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1536 | .name = "discard", |
| 1537 | .altname = "d", |
| 1538 | .cfunc = discard_f, |
| 1539 | .argmin = 2, |
| 1540 | .argmax = -1, |
| 1541 | .args = "[-Cq] off len", |
| 1542 | .oneline = "discards a number of bytes at a specified offset", |
| 1543 | .help = discard_help, |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1544 | }; |
| 1545 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1546 | static int discard_f(BlockDriverState *bs, int argc, char **argv) |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1547 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1548 | struct timeval t1, t2; |
| 1549 | int Cflag = 0, qflag = 0; |
| 1550 | int c, ret; |
| 1551 | int64_t offset; |
| 1552 | int count; |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1553 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1554 | while ((c = getopt(argc, argv, "Cq")) != EOF) { |
| 1555 | switch (c) { |
| 1556 | case 'C': |
| 1557 | Cflag = 1; |
| 1558 | break; |
| 1559 | case 'q': |
| 1560 | qflag = 1; |
| 1561 | break; |
| 1562 | default: |
| 1563 | return command_usage(&discard_cmd); |
| 1564 | } |
| 1565 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1566 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1567 | if (optind != argc - 2) { |
| 1568 | return command_usage(&discard_cmd); |
| 1569 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1570 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1571 | offset = cvtnum(argv[optind]); |
| 1572 | if (offset < 0) { |
| 1573 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 1574 | return 0; |
| 1575 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1576 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1577 | optind++; |
| 1578 | count = cvtnum(argv[optind]); |
| 1579 | if (count < 0) { |
| 1580 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 1581 | return 0; |
| 1582 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1583 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1584 | gettimeofday(&t1, NULL); |
| 1585 | ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS, |
| 1586 | count >> BDRV_SECTOR_BITS); |
| 1587 | gettimeofday(&t2, NULL); |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1588 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1589 | if (ret < 0) { |
| 1590 | printf("discard failed: %s\n", strerror(-ret)); |
| 1591 | goto out; |
| 1592 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1593 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1594 | /* Finally, report back -- -C gives a parsable format */ |
| 1595 | if (!qflag) { |
| 1596 | t2 = tsub(t2, t1); |
| 1597 | print_report("discard", &t2, offset, count, count, 1, Cflag); |
| 1598 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1599 | |
| 1600 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1601 | return 0; |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1604 | static int alloc_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1605 | { |
Paolo Bonzini | cc785c3 | 2012-05-08 16:51:52 +0200 | [diff] [blame] | 1606 | int64_t offset, sector_num; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1607 | int nb_sectors, remaining; |
| 1608 | char s1[64]; |
| 1609 | int num, sum_alloc; |
| 1610 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1611 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1612 | offset = cvtnum(argv[1]); |
Kevin Wolf | cf49a6a | 2013-06-05 14:19:29 +0200 | [diff] [blame] | 1613 | if (offset < 0) { |
| 1614 | printf("non-numeric offset argument -- %s\n", argv[1]); |
| 1615 | return 0; |
| 1616 | } else if (offset & 0x1ff) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1617 | printf("offset %" PRId64 " is not sector aligned\n", |
| 1618 | offset); |
| 1619 | return 0; |
| 1620 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1621 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1622 | if (argc == 3) { |
| 1623 | nb_sectors = cvtnum(argv[2]); |
Kevin Wolf | cf49a6a | 2013-06-05 14:19:29 +0200 | [diff] [blame] | 1624 | if (nb_sectors < 0) { |
| 1625 | printf("non-numeric length argument -- %s\n", argv[2]); |
| 1626 | return 0; |
| 1627 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1628 | } else { |
| 1629 | nb_sectors = 1; |
| 1630 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1631 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1632 | remaining = nb_sectors; |
| 1633 | sum_alloc = 0; |
Paolo Bonzini | cc785c3 | 2012-05-08 16:51:52 +0200 | [diff] [blame] | 1634 | sector_num = offset >> 9; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1635 | while (remaining) { |
Paolo Bonzini | cc785c3 | 2012-05-08 16:51:52 +0200 | [diff] [blame] | 1636 | ret = bdrv_is_allocated(bs, sector_num, remaining, &num); |
| 1637 | sector_num += num; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1638 | remaining -= num; |
| 1639 | if (ret) { |
| 1640 | sum_alloc += num; |
| 1641 | } |
Paolo Bonzini | cc785c3 | 2012-05-08 16:51:52 +0200 | [diff] [blame] | 1642 | if (num == 0) { |
| 1643 | nb_sectors -= remaining; |
| 1644 | remaining = 0; |
| 1645 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1646 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1647 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1648 | cvtstr(offset, s1, sizeof(s1)); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1649 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1650 | printf("%d/%d sectors allocated at offset %s\n", |
| 1651 | sum_alloc, nb_sectors, s1); |
| 1652 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | static const cmdinfo_t alloc_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1656 | .name = "alloc", |
| 1657 | .altname = "a", |
| 1658 | .argmin = 1, |
| 1659 | .argmax = 2, |
| 1660 | .cfunc = alloc_f, |
| 1661 | .args = "off [sectors]", |
| 1662 | .oneline = "checks if a sector is present in the file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1663 | }; |
| 1664 | |
Kevin Wolf | a00e81e | 2013-05-13 15:31:34 +0200 | [diff] [blame] | 1665 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1666 | static int map_is_allocated(BlockDriverState *bs, int64_t sector_num, |
| 1667 | int64_t nb_sectors, int64_t *pnum) |
Kevin Wolf | a00e81e | 2013-05-13 15:31:34 +0200 | [diff] [blame] | 1668 | { |
| 1669 | int num, num_checked; |
| 1670 | int ret, firstret; |
| 1671 | |
| 1672 | num_checked = MIN(nb_sectors, INT_MAX); |
| 1673 | ret = bdrv_is_allocated(bs, sector_num, num_checked, &num); |
| 1674 | if (ret < 0) { |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
| 1678 | firstret = ret; |
| 1679 | *pnum = num; |
| 1680 | |
| 1681 | while (nb_sectors > 0 && ret == firstret) { |
| 1682 | sector_num += num; |
| 1683 | nb_sectors -= num; |
| 1684 | |
| 1685 | num_checked = MIN(nb_sectors, INT_MAX); |
| 1686 | ret = bdrv_is_allocated(bs, sector_num, num_checked, &num); |
| 1687 | if (ret == firstret) { |
| 1688 | *pnum += num; |
| 1689 | } else { |
| 1690 | break; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | return firstret; |
| 1695 | } |
| 1696 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1697 | static int map_f(BlockDriverState *bs, int argc, char **argv) |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1698 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1699 | int64_t offset; |
| 1700 | int64_t nb_sectors; |
| 1701 | char s1[64]; |
Kevin Wolf | a00e81e | 2013-05-13 15:31:34 +0200 | [diff] [blame] | 1702 | int64_t num; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1703 | int ret; |
| 1704 | const char *retstr; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1705 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1706 | offset = 0; |
| 1707 | nb_sectors = bs->total_sectors; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1708 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1709 | do { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1710 | ret = map_is_allocated(bs, offset, nb_sectors, &num); |
Kevin Wolf | a00e81e | 2013-05-13 15:31:34 +0200 | [diff] [blame] | 1711 | if (ret < 0) { |
| 1712 | error_report("Failed to get allocation status: %s", strerror(-ret)); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1716 | retstr = ret ? " allocated" : "not allocated"; |
| 1717 | cvtstr(offset << 9ULL, s1, sizeof(s1)); |
Kevin Wolf | a00e81e | 2013-05-13 15:31:34 +0200 | [diff] [blame] | 1718 | printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s " |
| 1719 | "at offset %s (%d)\n", |
| 1720 | offset << 9ULL, num, nb_sectors, retstr, s1, ret); |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1721 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1722 | offset += num; |
| 1723 | nb_sectors -= num; |
| 1724 | } while (offset < bs->total_sectors); |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1725 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1726 | return 0; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | static const cmdinfo_t map_cmd = { |
| 1730 | .name = "map", |
| 1731 | .argmin = 0, |
| 1732 | .argmax = 0, |
| 1733 | .cfunc = map_f, |
| 1734 | .args = "", |
| 1735 | .oneline = "prints the allocated areas of a file", |
| 1736 | }; |
| 1737 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1738 | static int break_f(BlockDriverState *bs, int argc, char **argv) |
Kevin Wolf | 41c695c | 2012-12-06 14:32:58 +0100 | [diff] [blame] | 1739 | { |
| 1740 | int ret; |
| 1741 | |
| 1742 | ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]); |
| 1743 | if (ret < 0) { |
| 1744 | printf("Could not set breakpoint: %s\n", strerror(-ret)); |
| 1745 | } |
| 1746 | |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | static const cmdinfo_t break_cmd = { |
| 1751 | .name = "break", |
| 1752 | .argmin = 2, |
| 1753 | .argmax = 2, |
| 1754 | .cfunc = break_f, |
| 1755 | .args = "event tag", |
| 1756 | .oneline = "sets a breakpoint on event and tags the stopped " |
| 1757 | "request as tag", |
| 1758 | }; |
| 1759 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1760 | static int resume_f(BlockDriverState *bs, int argc, char **argv) |
Kevin Wolf | 41c695c | 2012-12-06 14:32:58 +0100 | [diff] [blame] | 1761 | { |
| 1762 | int ret; |
| 1763 | |
| 1764 | ret = bdrv_debug_resume(bs, argv[1]); |
| 1765 | if (ret < 0) { |
| 1766 | printf("Could not resume request: %s\n", strerror(-ret)); |
| 1767 | } |
| 1768 | |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | static const cmdinfo_t resume_cmd = { |
| 1773 | .name = "resume", |
| 1774 | .argmin = 1, |
| 1775 | .argmax = 1, |
| 1776 | .cfunc = resume_f, |
| 1777 | .args = "tag", |
| 1778 | .oneline = "resumes the request tagged as tag", |
| 1779 | }; |
| 1780 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1781 | static int wait_break_f(BlockDriverState *bs, int argc, char **argv) |
Kevin Wolf | 41c695c | 2012-12-06 14:32:58 +0100 | [diff] [blame] | 1782 | { |
| 1783 | while (!bdrv_debug_is_suspended(bs, argv[1])) { |
| 1784 | qemu_aio_wait(); |
| 1785 | } |
| 1786 | |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | static const cmdinfo_t wait_break_cmd = { |
| 1791 | .name = "wait_break", |
| 1792 | .argmin = 1, |
| 1793 | .argmax = 1, |
| 1794 | .cfunc = wait_break_f, |
| 1795 | .args = "tag", |
| 1796 | .oneline = "waits for the suspension of a request", |
| 1797 | }; |
| 1798 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1799 | static int abort_f(BlockDriverState *bs, int argc, char **argv) |
Stefan Hajnoczi | e01c30d | 2012-07-27 09:05:23 +0100 | [diff] [blame] | 1800 | { |
| 1801 | abort(); |
| 1802 | } |
| 1803 | |
| 1804 | static const cmdinfo_t abort_cmd = { |
| 1805 | .name = "abort", |
| 1806 | .cfunc = abort_f, |
| 1807 | .flags = CMD_NOFILE_OK, |
| 1808 | .oneline = "simulate a program crash using abort(3)", |
| 1809 | }; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1810 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1811 | static int close_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1812 | { |
Stefan Hajnoczi | b465785 | 2011-10-27 10:54:26 +0100 | [diff] [blame] | 1813 | bdrv_delete(bs); |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1814 | qemuio_bs = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1815 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | static const cmdinfo_t close_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1819 | .name = "close", |
| 1820 | .altname = "c", |
| 1821 | .cfunc = close_f, |
| 1822 | .oneline = "close the current open file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1823 | }; |
| 1824 | |
Christoph Hellwig | 9c4bab2 | 2009-07-10 13:33:47 +0200 | [diff] [blame] | 1825 | static int openfile(char *name, int flags, int growable) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1826 | { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1827 | if (qemuio_bs) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1828 | fprintf(stderr, "file open already, try 'help close'\n"); |
| 1829 | return 1; |
| 1830 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1831 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1832 | if (growable) { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1833 | if (bdrv_file_open(&qemuio_bs, name, NULL, flags)) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1834 | fprintf(stderr, "%s: can't open device %s\n", progname, name); |
| 1835 | return 1; |
| 1836 | } |
| 1837 | } else { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1838 | qemuio_bs = bdrv_new("hda"); |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 1839 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1840 | if (bdrv_open(qemuio_bs, name, NULL, flags, NULL) < 0) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1841 | fprintf(stderr, "%s: can't open device %s\n", progname, name); |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1842 | bdrv_delete(qemuio_bs); |
| 1843 | qemuio_bs = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1844 | return 1; |
| 1845 | } |
| 1846 | } |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 1847 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1848 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1851 | static void open_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1852 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1853 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1854 | "\n" |
| 1855 | " opens a new file in the requested mode\n" |
| 1856 | "\n" |
| 1857 | " Example:\n" |
| 1858 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" |
| 1859 | "\n" |
| 1860 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1861 | " -r, -- open file read-only\n" |
| 1862 | " -s, -- use snapshot file\n" |
| 1863 | " -n, -- disable host cache\n" |
Christoph Hellwig | 9c4bab2 | 2009-07-10 13:33:47 +0200 | [diff] [blame] | 1864 | " -g, -- allow file to grow (only applies to protocols)" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1865 | "\n"); |
| 1866 | } |
| 1867 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1868 | static int open_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1869 | |
| 1870 | static const cmdinfo_t open_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1871 | .name = "open", |
| 1872 | .altname = "o", |
| 1873 | .cfunc = open_f, |
| 1874 | .argmin = 1, |
| 1875 | .argmax = -1, |
| 1876 | .flags = CMD_NOFILE_OK, |
| 1877 | .args = "[-Crsn] [path]", |
| 1878 | .oneline = "open the file specified by path", |
| 1879 | .help = open_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1880 | }; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1881 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1882 | static int open_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1883 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1884 | int flags = 0; |
| 1885 | int readonly = 0; |
| 1886 | int growable = 0; |
| 1887 | int c; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1888 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1889 | while ((c = getopt(argc, argv, "snrg")) != EOF) { |
| 1890 | switch (c) { |
| 1891 | case 's': |
| 1892 | flags |= BDRV_O_SNAPSHOT; |
| 1893 | break; |
| 1894 | case 'n': |
| 1895 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 1896 | break; |
| 1897 | case 'r': |
| 1898 | readonly = 1; |
| 1899 | break; |
| 1900 | case 'g': |
| 1901 | growable = 1; |
| 1902 | break; |
| 1903 | default: |
| 1904 | return command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 1905 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1906 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1907 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1908 | if (!readonly) { |
| 1909 | flags |= BDRV_O_RDWR; |
| 1910 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1911 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1912 | if (optind != argc - 1) { |
| 1913 | return command_usage(&open_cmd); |
| 1914 | } |
| 1915 | |
| 1916 | return openfile(argv[optind], flags, growable); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 1919 | static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1920 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1921 | if (ct->flags & CMD_FLAG_GLOBAL) { |
| 1922 | return 1; |
| 1923 | } |
| 1924 | if (!(ct->flags & CMD_NOFILE_OK) && !bs) { |
| 1925 | fprintf(stderr, "no file open, try 'help open'\n"); |
| 1926 | return 0; |
| 1927 | } |
| 1928 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | static void usage(const char *name) |
| 1932 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1933 | printf( |
Christoph Hellwig | 9a2d77a | 2010-01-20 18:13:42 +0100 | [diff] [blame] | 1934 | "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 1935 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1936 | "\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1937 | " -c, --cmd command to execute\n" |
| 1938 | " -r, --read-only export read-only\n" |
| 1939 | " -s, --snapshot use snapshot file\n" |
| 1940 | " -n, --nocache disable host cache\n" |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 1941 | " -g, --growable allow file to grow (only applies to protocols)\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1942 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 1943 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 1944 | " -t, --cache=MODE use the given cache mode for the image\n" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 1945 | " -T, --trace FILE enable trace events listed in the given file\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1946 | " -h, --help display this help and exit\n" |
| 1947 | " -V, --version output version information and exit\n" |
| 1948 | "\n", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1949 | name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | |
| 1953 | int main(int argc, char **argv) |
| 1954 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1955 | int readonly = 0; |
| 1956 | int growable = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 1957 | const char *sopt = "hVc:d:rsnmgkt:T:"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1958 | const struct option lopt[] = { |
| 1959 | { "help", 0, NULL, 'h' }, |
| 1960 | { "version", 0, NULL, 'V' }, |
| 1961 | { "offset", 1, NULL, 'o' }, |
| 1962 | { "cmd", 1, NULL, 'c' }, |
| 1963 | { "read-only", 0, NULL, 'r' }, |
| 1964 | { "snapshot", 0, NULL, 's' }, |
| 1965 | { "nocache", 0, NULL, 'n' }, |
| 1966 | { "misalign", 0, NULL, 'm' }, |
| 1967 | { "growable", 0, NULL, 'g' }, |
| 1968 | { "native-aio", 0, NULL, 'k' }, |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 1969 | { "discard", 1, NULL, 'd' }, |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 1970 | { "cache", 1, NULL, 't' }, |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 1971 | { "trace", 1, NULL, 'T' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1972 | { NULL, 0, NULL, 0 } |
| 1973 | }; |
| 1974 | int c; |
| 1975 | int opt_index = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 1976 | int flags = BDRV_O_UNMAP; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1977 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1978 | progname = basename(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1979 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1980 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 1981 | switch (c) { |
| 1982 | case 's': |
| 1983 | flags |= BDRV_O_SNAPSHOT; |
| 1984 | break; |
| 1985 | case 'n': |
| 1986 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 1987 | break; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 1988 | case 'd': |
| 1989 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 1990 | error_report("Invalid discard option: %s", optarg); |
| 1991 | exit(1); |
| 1992 | } |
| 1993 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1994 | case 'c': |
| 1995 | add_user_command(optarg); |
| 1996 | break; |
| 1997 | case 'r': |
| 1998 | readonly = 1; |
| 1999 | break; |
| 2000 | case 'm': |
| 2001 | misalign = 1; |
| 2002 | break; |
| 2003 | case 'g': |
| 2004 | growable = 1; |
| 2005 | break; |
| 2006 | case 'k': |
| 2007 | flags |= BDRV_O_NATIVE_AIO; |
| 2008 | break; |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 2009 | case 't': |
| 2010 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { |
| 2011 | error_report("Invalid cache option: %s", optarg); |
| 2012 | exit(1); |
| 2013 | } |
| 2014 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 2015 | case 'T': |
| 2016 | if (!trace_backend_init(optarg, NULL)) { |
| 2017 | exit(1); /* error message will have been printed */ |
| 2018 | } |
| 2019 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2020 | case 'V': |
| 2021 | printf("%s version %s\n", progname, VERSION); |
| 2022 | exit(0); |
| 2023 | case 'h': |
| 2024 | usage(progname); |
| 2025 | exit(0); |
| 2026 | default: |
| 2027 | usage(progname); |
| 2028 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 2029 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2030 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 2031 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2032 | if ((argc - optind) > 1) { |
| 2033 | usage(progname); |
| 2034 | exit(1); |
| 2035 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 2036 | |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 2037 | qemu_init_main_loop(); |
Paolo Bonzini | 2592c59 | 2012-11-03 18:10:17 +0100 | [diff] [blame] | 2038 | bdrv_init(); |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 2039 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2040 | /* initialize commands */ |
| 2041 | quit_init(); |
| 2042 | help_init(); |
| 2043 | add_command(&open_cmd); |
| 2044 | add_command(&close_cmd); |
| 2045 | add_command(&read_cmd); |
| 2046 | add_command(&readv_cmd); |
| 2047 | add_command(&write_cmd); |
| 2048 | add_command(&writev_cmd); |
| 2049 | add_command(&multiwrite_cmd); |
| 2050 | add_command(&aio_read_cmd); |
| 2051 | add_command(&aio_write_cmd); |
| 2052 | add_command(&aio_flush_cmd); |
| 2053 | add_command(&flush_cmd); |
| 2054 | add_command(&truncate_cmd); |
| 2055 | add_command(&length_cmd); |
| 2056 | add_command(&info_cmd); |
| 2057 | add_command(&discard_cmd); |
| 2058 | add_command(&alloc_cmd); |
| 2059 | add_command(&map_cmd); |
Kevin Wolf | 41c695c | 2012-12-06 14:32:58 +0100 | [diff] [blame] | 2060 | add_command(&break_cmd); |
| 2061 | add_command(&resume_cmd); |
| 2062 | add_command(&wait_break_cmd); |
Stefan Hajnoczi | e01c30d | 2012-07-27 09:05:23 +0100 | [diff] [blame] | 2063 | add_command(&abort_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2064 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2065 | add_check_command(init_check_command); |
| 2066 | |
| 2067 | /* open the device */ |
| 2068 | if (!readonly) { |
| 2069 | flags |= BDRV_O_RDWR; |
| 2070 | } |
| 2071 | |
| 2072 | if ((argc - optind) == 1) { |
| 2073 | openfile(argv[optind], flags, growable); |
| 2074 | } |
| 2075 | command_loop(); |
| 2076 | |
| 2077 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 2078 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2079 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 2080 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2081 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame^] | 2082 | if (qemuio_bs) { |
| 2083 | bdrv_delete(qemuio_bs); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 2084 | } |
| 2085 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 2086 | } |