]>
Commit | Line | Data |
---|---|---|
e3aff4f6 AL |
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 | */ | |
c32d766a | 10 | #include <sys/time.h> |
e3aff4f6 AL |
11 | #include <sys/types.h> |
12 | #include <stdarg.h> | |
13 | #include <stdio.h> | |
14 | #include <getopt.h> | |
c32d766a | 15 | #include <libgen.h> |
e3aff4f6 AL |
16 | |
17 | #include "qemu-common.h" | |
1de7afc9 | 18 | #include "qemu/main-loop.h" |
737e150e | 19 | #include "block/block_int.h" |
e3aff4f6 | 20 | #include "cmd.h" |
d7bb72c8 | 21 | #include "trace/control.h" |
e3aff4f6 AL |
22 | |
23 | #define VERSION "0.0.1" | |
24 | ||
43642b38 | 25 | #define CMD_NOFILE_OK 0x01 |
e3aff4f6 AL |
26 | |
27 | char *progname; | |
e3aff4f6 | 28 | |
734c3b85 | 29 | BlockDriverState *qemuio_bs; |
e3aff4f6 AL |
30 | static int misalign; |
31 | ||
b6e356aa KW |
32 | static int64_t cvtnum(const char *s) |
33 | { | |
34 | char *end; | |
35 | return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B); | |
36 | } | |
37 | ||
cf070d7e CH |
38 | /* |
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 | { | |
43642b38 DN |
46 | char *endptr = NULL; |
47 | long pattern; | |
cf070d7e | 48 | |
43642b38 DN |
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 | } | |
cf070d7e | 54 | |
43642b38 | 55 | return pattern; |
cf070d7e CH |
56 | } |
57 | ||
e3aff4f6 AL |
58 | /* |
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 | ||
43642b38 | 65 | #define MISALIGN_OFFSET 16 |
734c3b85 | 66 | static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern) |
e3aff4f6 | 67 | { |
43642b38 DN |
68 | void *buf; |
69 | ||
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; | |
e3aff4f6 AL |
79 | } |
80 | ||
81 | static void qemu_io_free(void *p) | |
82 | { | |
43642b38 DN |
83 | if (misalign) { |
84 | p -= MISALIGN_OFFSET; | |
85 | } | |
86 | qemu_vfree(p); | |
e3aff4f6 AL |
87 | } |
88 | ||
43642b38 | 89 | static void dump_buffer(const void *buffer, int64_t offset, int len) |
e3aff4f6 | 90 | { |
43642b38 DN |
91 | int i, j; |
92 | const uint8_t *p; | |
93 | ||
94 | for (i = 0, p = buffer; i < len; i += 16) { | |
95 | const uint8_t *s = p; | |
96 | ||
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 | } | |
e3aff4f6 AL |
111 | } |
112 | ||
43642b38 DN |
113 | static void print_report(const char *op, struct timeval *t, int64_t offset, |
114 | int count, int total, int cnt, int Cflag) | |
e3aff4f6 | 115 | { |
43642b38 DN |
116 | char s1[64], s2[64], ts[64]; |
117 | ||
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 | } | |
e3aff4f6 AL |
132 | } |
133 | ||
cf57298a CH |
134 | /* |
135 | * Parse multiple length statements for vectored I/O, and construct an I/O | |
136 | * vector matching it. | |
137 | */ | |
138 | static void * | |
734c3b85 KW |
139 | create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov, |
140 | int pattern) | |
cf57298a | 141 | { |
031380d8 | 142 | size_t *sizes = g_new0(size_t, nr_iov); |
43642b38 DN |
143 | size_t count = 0; |
144 | void *buf = NULL; | |
145 | void *p; | |
146 | int i; | |
147 | ||
148 | for (i = 0; i < nr_iov; i++) { | |
149 | char *arg = argv[i]; | |
150 | int64_t len; | |
151 | ||
152 | len = cvtnum(arg); | |
153 | if (len < 0) { | |
154 | printf("non-numeric length argument -- %s\n", arg); | |
155 | goto fail; | |
156 | } | |
157 | ||
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 | } | |
163 | ||
164 | if (len & 0x1ff) { | |
165 | printf("length argument %" PRId64 | |
166 | " is not sector aligned\n", len); | |
167 | goto fail; | |
168 | } | |
169 | ||
170 | sizes[i] = len; | |
171 | count += len; | |
172 | } | |
173 | ||
174 | qemu_iovec_init(qiov, nr_iov); | |
175 | ||
734c3b85 | 176 | buf = p = qemu_io_alloc(bs, count, pattern); |
43642b38 DN |
177 | |
178 | for (i = 0; i < nr_iov; i++) { | |
179 | qemu_iovec_add(qiov, p, sizes[i]); | |
180 | p += sizes[i]; | |
181 | } | |
cf57298a | 182 | |
40a0d7c3 | 183 | fail: |
031380d8 | 184 | g_free(sizes); |
43642b38 | 185 | return buf; |
cf57298a CH |
186 | } |
187 | ||
734c3b85 KW |
188 | static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count, |
189 | int *total) | |
e3aff4f6 | 190 | { |
43642b38 | 191 | int ret; |
e3aff4f6 | 192 | |
43642b38 DN |
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; | |
e3aff4f6 AL |
199 | } |
200 | ||
734c3b85 KW |
201 | static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count, |
202 | int *total) | |
e3aff4f6 | 203 | { |
43642b38 | 204 | int ret; |
e3aff4f6 | 205 | |
43642b38 DN |
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; | |
e3aff4f6 AL |
212 | } |
213 | ||
734c3b85 KW |
214 | static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count, |
215 | int *total) | |
e3aff4f6 | 216 | { |
43642b38 DN |
217 | *total = bdrv_pread(bs, offset, (uint8_t *)buf, count); |
218 | if (*total < 0) { | |
219 | return *total; | |
220 | } | |
221 | return 1; | |
e3aff4f6 AL |
222 | } |
223 | ||
734c3b85 KW |
224 | static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count, |
225 | int *total) | |
e3aff4f6 | 226 | { |
43642b38 DN |
227 | *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count); |
228 | if (*total < 0) { | |
229 | return *total; | |
230 | } | |
231 | return 1; | |
e3aff4f6 AL |
232 | } |
233 | ||
71b58b82 | 234 | typedef struct { |
734c3b85 | 235 | BlockDriverState *bs; |
71b58b82 SH |
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 | ||
734c3b85 | 247 | data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE, |
71b58b82 SH |
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 | ||
734c3b85 KW |
258 | static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count, |
259 | int *total) | |
71b58b82 SH |
260 | { |
261 | Coroutine *co; | |
262 | CoWriteZeroes data = { | |
734c3b85 | 263 | .bs = bs, |
71b58b82 SH |
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 | ||
734c3b85 KW |
282 | static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset, |
283 | int count, int *total) | |
791bfa35 KW |
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 | ||
734c3b85 KW |
295 | static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset, |
296 | int count, int *total) | |
ca94dbc7 | 297 | { |
43642b38 DN |
298 | *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count); |
299 | if (*total < 0) { | |
300 | return *total; | |
301 | } | |
302 | return 1; | |
ca94dbc7 KW |
303 | } |
304 | ||
734c3b85 KW |
305 | static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset, |
306 | int count, int *total) | |
ca94dbc7 | 307 | { |
43642b38 DN |
308 | *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count); |
309 | if (*total < 0) { | |
310 | return *total; | |
311 | } | |
312 | return 1; | |
ca94dbc7 KW |
313 | } |
314 | ||
e3aff4f6 AL |
315 | #define NOT_DONE 0x7fffffff |
316 | static void aio_rw_done(void *opaque, int ret) | |
317 | { | |
43642b38 | 318 | *(int *)opaque = ret; |
e3aff4f6 AL |
319 | } |
320 | ||
734c3b85 KW |
321 | static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov, |
322 | int64_t offset, int *total) | |
e3aff4f6 | 323 | { |
43642b38 | 324 | int async_ret = NOT_DONE; |
e3aff4f6 | 325 | |
ad54ae80 PB |
326 | bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9, |
327 | aio_rw_done, &async_ret); | |
43642b38 | 328 | while (async_ret == NOT_DONE) { |
a5a5238e | 329 | main_loop_wait(false); |
43642b38 | 330 | } |
e3aff4f6 | 331 | |
43642b38 DN |
332 | *total = qiov->size; |
333 | return async_ret < 0 ? async_ret : 1; | |
e3aff4f6 AL |
334 | } |
335 | ||
734c3b85 KW |
336 | static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov, |
337 | int64_t offset, int *total) | |
e3aff4f6 | 338 | { |
43642b38 | 339 | int async_ret = NOT_DONE; |
e3aff4f6 | 340 | |
ad54ae80 PB |
341 | bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9, |
342 | aio_rw_done, &async_ret); | |
43642b38 | 343 | while (async_ret == NOT_DONE) { |
a5a5238e | 344 | main_loop_wait(false); |
43642b38 | 345 | } |
e3aff4f6 | 346 | |
43642b38 DN |
347 | *total = qiov->size; |
348 | return async_ret < 0 ? async_ret : 1; | |
e3aff4f6 AL |
349 | } |
350 | ||
776cbbbd | 351 | struct multiwrite_async_ret { |
43642b38 DN |
352 | int num_done; |
353 | int error; | |
776cbbbd KW |
354 | }; |
355 | ||
356 | static void multiwrite_cb(void *opaque, int ret) | |
357 | { | |
43642b38 | 358 | struct multiwrite_async_ret *async_ret = opaque; |
776cbbbd | 359 | |
43642b38 DN |
360 | async_ret->num_done++; |
361 | if (ret < 0) { | |
362 | async_ret->error = ret; | |
363 | } | |
776cbbbd KW |
364 | } |
365 | ||
734c3b85 KW |
366 | static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs, |
367 | int num_reqs, int *total) | |
776cbbbd | 368 | { |
43642b38 DN |
369 | int i, ret; |
370 | struct multiwrite_async_ret async_ret = { | |
371 | .num_done = 0, | |
372 | .error = 0, | |
373 | }; | |
374 | ||
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 | } | |
381 | ||
382 | ret = bdrv_aio_multiwrite(bs, reqs, num_reqs); | |
383 | if (ret < 0) { | |
384 | return ret; | |
385 | } | |
386 | ||
387 | while (async_ret.num_done < num_reqs) { | |
a5a5238e | 388 | main_loop_wait(false); |
43642b38 DN |
389 | } |
390 | ||
391 | return async_ret.error < 0 ? async_ret.error : 1; | |
776cbbbd | 392 | } |
e3aff4f6 | 393 | |
43642b38 | 394 | static void read_help(void) |
e3aff4f6 | 395 | { |
43642b38 | 396 | printf( |
e3aff4f6 AL |
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" | |
ca94dbc7 | 405 | " -b, -- read from the VM state rather than the virtual disk\n" |
d9654a58 KW |
406 | " -C, -- report statistics in a machine parsable format\n" |
407 | " -l, -- length for pattern verification (only with -P)\n" | |
e3aff4f6 | 408 | " -p, -- use bdrv_pread to read the file\n" |
c48101ae | 409 | " -P, -- use a pattern to verify read data\n" |
095343ad | 410 | " -q, -- quiet mode, do not show I/O statistics\n" |
d9654a58 KW |
411 | " -s, -- start offset for pattern verification (only with -P)\n" |
412 | " -v, -- dump buffer to standard output\n" | |
e3aff4f6 AL |
413 | "\n"); |
414 | } | |
415 | ||
734c3b85 | 416 | static int read_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
417 | |
418 | static const cmdinfo_t read_cmd = { | |
43642b38 DN |
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, | |
22a2bdcb BS |
427 | }; |
428 | ||
734c3b85 | 429 | static int read_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 430 | { |
43642b38 DN |
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; | |
441 | ||
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 | } | |
486 | ||
487 | if (optind != argc - 2) { | |
488 | return command_usage(&read_cmd); | |
489 | } | |
490 | ||
491 | if (bflag && pflag) { | |
492 | printf("-b and -p cannot be specified at the same time\n"); | |
493 | return 0; | |
494 | } | |
495 | ||
496 | offset = cvtnum(argv[optind]); | |
497 | if (offset < 0) { | |
498 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
499 | return 0; | |
500 | } | |
501 | ||
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 | } | |
e3aff4f6 | 508 | |
d9654a58 KW |
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)) { | |
07f35073 | 518 | printf("pattern verification range exceeds end of read data\n"); |
d9654a58 KW |
519 | return 0; |
520 | } | |
521 | ||
5afc8b3d | 522 | if (!pflag) { |
43642b38 DN |
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 | } | |
5afc8b3d | 533 | } |
43642b38 | 534 | |
734c3b85 | 535 | buf = qemu_io_alloc(bs, count, 0xab); |
43642b38 DN |
536 | |
537 | gettimeofday(&t1, NULL); | |
538 | if (pflag) { | |
734c3b85 | 539 | cnt = do_pread(bs, buf, offset, count, &total); |
43642b38 | 540 | } else if (bflag) { |
734c3b85 | 541 | cnt = do_load_vmstate(bs, buf, offset, count, &total); |
43642b38 | 542 | } else { |
734c3b85 | 543 | cnt = do_read(bs, buf, offset, count, &total); |
43642b38 DN |
544 | } |
545 | gettimeofday(&t2, NULL); | |
546 | ||
547 | if (cnt < 0) { | |
548 | printf("read failed: %s\n", strerror(-cnt)); | |
549 | goto out; | |
550 | } | |
551 | ||
552 | if (Pflag) { | |
031380d8 | 553 | void *cmp_buf = g_malloc(pattern_count); |
43642b38 DN |
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 | } | |
031380d8 | 560 | g_free(cmp_buf); |
43642b38 DN |
561 | } |
562 | ||
563 | if (qflag) { | |
564 | goto out; | |
565 | } | |
566 | ||
567 | if (vflag) { | |
568 | dump_buffer(buf, offset, count); | |
569 | } | |
570 | ||
571 | /* Finally, report back -- -C gives a parsable format */ | |
572 | t2 = tsub(t2, t1); | |
573 | print_report("read", &t2, offset, count, total, cnt, Cflag); | |
e3aff4f6 | 574 | |
7d8abfcb | 575 | out: |
43642b38 | 576 | qemu_io_free(buf); |
e3aff4f6 | 577 | |
43642b38 | 578 | return 0; |
e3aff4f6 AL |
579 | } |
580 | ||
43642b38 | 581 | static void readv_help(void) |
e3aff4f6 | 582 | { |
43642b38 | 583 | printf( |
e3aff4f6 AL |
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" | |
c48101ae | 594 | " -P, -- use a pattern to verify read data\n" |
e3aff4f6 | 595 | " -v, -- dump buffer to standard output\n" |
095343ad | 596 | " -q, -- quiet mode, do not show I/O statistics\n" |
e3aff4f6 AL |
597 | "\n"); |
598 | } | |
599 | ||
734c3b85 | 600 | static int readv_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
601 | |
602 | static const cmdinfo_t readv_cmd = { | |
43642b38 DN |
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, | |
22a2bdcb BS |
610 | }; |
611 | ||
734c3b85 | 612 | static int readv_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 613 | { |
43642b38 DN |
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; | |
625 | ||
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 | } | |
648 | ||
649 | if (optind > argc - 2) { | |
650 | return command_usage(&readv_cmd); | |
651 | } | |
652 | ||
653 | ||
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++; | |
660 | ||
661 | if (offset & 0x1ff) { | |
662 | printf("offset %" PRId64 " is not sector aligned\n", | |
663 | offset); | |
664 | return 0; | |
665 | } | |
666 | ||
667 | nr_iov = argc - optind; | |
734c3b85 | 668 | buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab); |
f2360620 KW |
669 | if (buf == NULL) { |
670 | return 0; | |
671 | } | |
43642b38 DN |
672 | |
673 | gettimeofday(&t1, NULL); | |
734c3b85 | 674 | cnt = do_aio_readv(bs, &qiov, offset, &total); |
43642b38 DN |
675 | gettimeofday(&t2, NULL); |
676 | ||
677 | if (cnt < 0) { | |
678 | printf("readv failed: %s\n", strerror(-cnt)); | |
679 | goto out; | |
680 | } | |
681 | ||
682 | if (Pflag) { | |
031380d8 | 683 | void *cmp_buf = g_malloc(qiov.size); |
43642b38 DN |
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 | } | |
031380d8 | 689 | g_free(cmp_buf); |
43642b38 DN |
690 | } |
691 | ||
692 | if (qflag) { | |
693 | goto out; | |
694 | } | |
695 | ||
696 | if (vflag) { | |
697 | dump_buffer(buf, offset, qiov.size); | |
698 | } | |
699 | ||
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); | |
e3aff4f6 | 703 | |
7d8abfcb | 704 | out: |
9e559533 | 705 | qemu_iovec_destroy(&qiov); |
43642b38 DN |
706 | qemu_io_free(buf); |
707 | return 0; | |
e3aff4f6 AL |
708 | } |
709 | ||
43642b38 | 710 | static void write_help(void) |
e3aff4f6 | 711 | { |
43642b38 | 712 | printf( |
e3aff4f6 AL |
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" | |
ca94dbc7 | 721 | " -b, -- write to the VM state rather than the virtual disk\n" |
791bfa35 | 722 | " -c, -- write compressed data with bdrv_write_compressed\n" |
e3aff4f6 AL |
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" | |
095343ad | 726 | " -q, -- quiet mode, do not show I/O statistics\n" |
71b58b82 | 727 | " -z, -- write zeroes using bdrv_co_write_zeroes\n" |
e3aff4f6 AL |
728 | "\n"); |
729 | } | |
730 | ||
734c3b85 | 731 | static int write_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
732 | |
733 | static const cmdinfo_t write_cmd = { | |
43642b38 DN |
734 | .name = "write", |
735 | .altname = "w", | |
736 | .cfunc = write_f, | |
737 | .argmin = 2, | |
738 | .argmax = -1, | |
791bfa35 | 739 | .args = "[-bcCpqz] [-P pattern ] off len", |
43642b38 DN |
740 | .oneline = "writes a number of bytes at a specified offset", |
741 | .help = write_help, | |
22a2bdcb BS |
742 | }; |
743 | ||
734c3b85 | 744 | static int write_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 745 | { |
43642b38 | 746 | struct timeval t1, t2; |
71b58b82 | 747 | int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0; |
791bfa35 | 748 | int cflag = 0; |
43642b38 | 749 | int c, cnt; |
71b58b82 | 750 | char *buf = NULL; |
43642b38 DN |
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; | |
756 | ||
791bfa35 | 757 | while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) { |
43642b38 DN |
758 | switch (c) { |
759 | case 'b': | |
760 | bflag = 1; | |
761 | break; | |
791bfa35 KW |
762 | case 'c': |
763 | cflag = 1; | |
764 | break; | |
43642b38 DN |
765 | case 'C': |
766 | Cflag = 1; | |
767 | break; | |
768 | case 'p': | |
769 | pflag = 1; | |
770 | break; | |
771 | case 'P': | |
71b58b82 | 772 | Pflag = 1; |
43642b38 DN |
773 | pattern = parse_pattern(optarg); |
774 | if (pattern < 0) { | |
775 | return 0; | |
776 | } | |
777 | break; | |
778 | case 'q': | |
779 | qflag = 1; | |
780 | break; | |
71b58b82 SH |
781 | case 'z': |
782 | zflag = 1; | |
783 | break; | |
43642b38 DN |
784 | default: |
785 | return command_usage(&write_cmd); | |
786 | } | |
787 | } | |
788 | ||
789 | if (optind != argc - 2) { | |
790 | return command_usage(&write_cmd); | |
791 | } | |
792 | ||
71b58b82 SH |
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"); | |
43642b38 DN |
800 | return 0; |
801 | } | |
802 | ||
803 | offset = cvtnum(argv[optind]); | |
804 | if (offset < 0) { | |
805 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
806 | return 0; | |
807 | } | |
808 | ||
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 | } | |
815 | ||
816 | if (!pflag) { | |
817 | if (offset & 0x1ff) { | |
818 | printf("offset %" PRId64 " is not sector aligned\n", | |
819 | offset); | |
820 | return 0; | |
821 | } | |
822 | ||
823 | if (count & 0x1ff) { | |
824 | printf("count %d is not sector aligned\n", | |
825 | count); | |
826 | return 0; | |
827 | } | |
828 | } | |
829 | ||
71b58b82 | 830 | if (!zflag) { |
734c3b85 | 831 | buf = qemu_io_alloc(bs, count, pattern); |
71b58b82 | 832 | } |
43642b38 DN |
833 | |
834 | gettimeofday(&t1, NULL); | |
835 | if (pflag) { | |
734c3b85 | 836 | cnt = do_pwrite(bs, buf, offset, count, &total); |
43642b38 | 837 | } else if (bflag) { |
734c3b85 | 838 | cnt = do_save_vmstate(bs, buf, offset, count, &total); |
71b58b82 | 839 | } else if (zflag) { |
734c3b85 | 840 | cnt = do_co_write_zeroes(bs, offset, count, &total); |
791bfa35 | 841 | } else if (cflag) { |
734c3b85 | 842 | cnt = do_write_compressed(bs, buf, offset, count, &total); |
43642b38 | 843 | } else { |
734c3b85 | 844 | cnt = do_write(bs, buf, offset, count, &total); |
43642b38 DN |
845 | } |
846 | gettimeofday(&t2, NULL); | |
847 | ||
848 | if (cnt < 0) { | |
849 | printf("write failed: %s\n", strerror(-cnt)); | |
850 | goto out; | |
851 | } | |
852 | ||
853 | if (qflag) { | |
854 | goto out; | |
855 | } | |
856 | ||
857 | /* Finally, report back -- -C gives a parsable format */ | |
858 | t2 = tsub(t2, t1); | |
859 | print_report("wrote", &t2, offset, count, total, cnt, Cflag); | |
e3aff4f6 | 860 | |
7d8abfcb | 861 | out: |
71b58b82 SH |
862 | if (!zflag) { |
863 | qemu_io_free(buf); | |
864 | } | |
e3aff4f6 | 865 | |
43642b38 | 866 | return 0; |
e3aff4f6 AL |
867 | } |
868 | ||
e3aff4f6 AL |
869 | static void |
870 | writev_help(void) | |
871 | { | |
43642b38 | 872 | printf( |
e3aff4f6 AL |
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" | |
095343ad | 883 | " -q, -- quiet mode, do not show I/O statistics\n" |
e3aff4f6 AL |
884 | "\n"); |
885 | } | |
886 | ||
734c3b85 | 887 | static int writev_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
888 | |
889 | static const cmdinfo_t writev_cmd = { | |
43642b38 DN |
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, | |
22a2bdcb BS |
897 | }; |
898 | ||
734c3b85 | 899 | static int writev_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 900 | { |
43642b38 DN |
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; | |
911 | ||
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 | } | |
930 | ||
931 | if (optind > argc - 2) { | |
932 | return command_usage(&writev_cmd); | |
933 | } | |
934 | ||
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++; | |
941 | ||
942 | if (offset & 0x1ff) { | |
943 | printf("offset %" PRId64 " is not sector aligned\n", | |
944 | offset); | |
945 | return 0; | |
946 | } | |
947 | ||
948 | nr_iov = argc - optind; | |
734c3b85 | 949 | buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern); |
f2360620 KW |
950 | if (buf == NULL) { |
951 | return 0; | |
952 | } | |
43642b38 DN |
953 | |
954 | gettimeofday(&t1, NULL); | |
734c3b85 | 955 | cnt = do_aio_writev(bs, &qiov, offset, &total); |
43642b38 DN |
956 | gettimeofday(&t2, NULL); |
957 | ||
958 | if (cnt < 0) { | |
959 | printf("writev failed: %s\n", strerror(-cnt)); | |
960 | goto out; | |
961 | } | |
962 | ||
963 | if (qflag) { | |
964 | goto out; | |
965 | } | |
966 | ||
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); | |
7d8abfcb | 970 | out: |
9e559533 | 971 | qemu_iovec_destroy(&qiov); |
43642b38 DN |
972 | qemu_io_free(buf); |
973 | return 0; | |
e3aff4f6 AL |
974 | } |
975 | ||
43642b38 | 976 | static void multiwrite_help(void) |
776cbbbd | 977 | { |
43642b38 | 978 | printf( |
776cbbbd KW |
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" | |
b2bedb21 | 984 | " 'multiwrite 512 1k 1k ; 4k 1k'\n" |
776cbbbd KW |
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 | ||
734c3b85 | 996 | static int multiwrite_f(BlockDriverState *bs, int argc, char **argv); |
776cbbbd KW |
997 | |
998 | static const cmdinfo_t multiwrite_cmd = { | |
43642b38 DN |
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, | |
776cbbbd KW |
1006 | }; |
1007 | ||
734c3b85 | 1008 | static int multiwrite_f(BlockDriverState *bs, int argc, char **argv) |
776cbbbd | 1009 | { |
43642b38 DN |
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; | |
1023 | ||
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 | } | |
1042 | ||
1043 | if (optind > argc - 2) { | |
1044 | return command_usage(&writev_cmd); | |
1045 | } | |
1046 | ||
1047 | nr_reqs = 1; | |
1048 | for (i = optind; i < argc; i++) { | |
1049 | if (!strcmp(argv[i], ";")) { | |
1050 | nr_reqs++; | |
1051 | } | |
1052 | } | |
1053 | ||
f2360620 KW |
1054 | reqs = g_malloc0(nr_reqs * sizeof(*reqs)); |
1055 | buf = g_malloc0(nr_reqs * sizeof(*buf)); | |
7267c094 | 1056 | qiovs = g_malloc(nr_reqs * sizeof(*qiovs)); |
43642b38 | 1057 | |
67403dbb | 1058 | for (i = 0; i < nr_reqs && optind < argc; i++) { |
43642b38 DN |
1059 | int j; |
1060 | ||
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]); | |
67403dbb | 1065 | goto out; |
43642b38 DN |
1066 | } |
1067 | optind++; | |
1068 | ||
1069 | if (offset & 0x1ff) { | |
1070 | printf("offset %lld is not sector aligned\n", | |
1071 | (long long)offset); | |
67403dbb | 1072 | goto out; |
43642b38 | 1073 | } |
776cbbbd KW |
1074 | |
1075 | if (i == 0) { | |
1076 | first_offset = offset; | |
1077 | } | |
1078 | ||
43642b38 DN |
1079 | /* Read lengths for qiov entries */ |
1080 | for (j = optind; j < argc; j++) { | |
1081 | if (!strcmp(argv[j], ";")) { | |
1082 | break; | |
1083 | } | |
1084 | } | |
776cbbbd | 1085 | |
43642b38 | 1086 | nr_iov = j - optind; |
776cbbbd | 1087 | |
43642b38 | 1088 | /* Build request */ |
734c3b85 | 1089 | buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern); |
f2360620 KW |
1090 | if (buf[i] == NULL) { |
1091 | goto out; | |
1092 | } | |
1093 | ||
43642b38 | 1094 | reqs[i].qiov = &qiovs[i]; |
43642b38 DN |
1095 | reqs[i].sector = offset >> 9; |
1096 | reqs[i].nb_sectors = reqs[i].qiov->size >> 9; | |
776cbbbd | 1097 | |
43642b38 | 1098 | optind = j + 1; |
776cbbbd | 1099 | |
43642b38 DN |
1100 | pattern++; |
1101 | } | |
776cbbbd | 1102 | |
67403dbb KW |
1103 | /* If there were empty requests at the end, ignore them */ |
1104 | nr_reqs = i; | |
1105 | ||
43642b38 | 1106 | gettimeofday(&t1, NULL); |
734c3b85 | 1107 | cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total); |
43642b38 | 1108 | gettimeofday(&t2, NULL); |
776cbbbd | 1109 | |
43642b38 DN |
1110 | if (cnt < 0) { |
1111 | printf("aio_multiwrite failed: %s\n", strerror(-cnt)); | |
1112 | goto out; | |
1113 | } | |
776cbbbd | 1114 | |
43642b38 DN |
1115 | if (qflag) { |
1116 | goto out; | |
1117 | } | |
776cbbbd | 1118 | |
43642b38 DN |
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); | |
776cbbbd | 1122 | out: |
43642b38 DN |
1123 | for (i = 0; i < nr_reqs; i++) { |
1124 | qemu_io_free(buf[i]); | |
f2360620 KW |
1125 | if (reqs[i].qiov != NULL) { |
1126 | qemu_iovec_destroy(&qiovs[i]); | |
1127 | } | |
43642b38 | 1128 | } |
7267c094 AL |
1129 | g_free(buf); |
1130 | g_free(reqs); | |
1131 | g_free(qiovs); | |
43642b38 | 1132 | return 0; |
776cbbbd KW |
1133 | } |
1134 | ||
95533d5f | 1135 | struct aio_ctx { |
43642b38 DN |
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; | |
95533d5f CH |
1145 | }; |
1146 | ||
43642b38 | 1147 | static void aio_write_done(void *opaque, int ret) |
95533d5f | 1148 | { |
43642b38 DN |
1149 | struct aio_ctx *ctx = opaque; |
1150 | struct timeval t2; | |
95533d5f | 1151 | |
43642b38 | 1152 | gettimeofday(&t2, NULL); |
95533d5f | 1153 | |
95533d5f | 1154 | |
43642b38 DN |
1155 | if (ret < 0) { |
1156 | printf("aio_write failed: %s\n", strerror(-ret)); | |
1157 | goto out; | |
1158 | } | |
95533d5f | 1159 | |
43642b38 DN |
1160 | if (ctx->qflag) { |
1161 | goto out; | |
1162 | } | |
95533d5f | 1163 | |
43642b38 DN |
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); | |
7d8abfcb | 1168 | out: |
43642b38 | 1169 | qemu_io_free(ctx->buf); |
9e559533 | 1170 | qemu_iovec_destroy(&ctx->qiov); |
031380d8 | 1171 | g_free(ctx); |
95533d5f CH |
1172 | } |
1173 | ||
43642b38 | 1174 | static void aio_read_done(void *opaque, int ret) |
95533d5f | 1175 | { |
43642b38 DN |
1176 | struct aio_ctx *ctx = opaque; |
1177 | struct timeval t2; | |
1178 | ||
1179 | gettimeofday(&t2, NULL); | |
1180 | ||
1181 | if (ret < 0) { | |
1182 | printf("readv failed: %s\n", strerror(-ret)); | |
1183 | goto out; | |
1184 | } | |
1185 | ||
1186 | if (ctx->Pflag) { | |
031380d8 | 1187 | void *cmp_buf = g_malloc(ctx->qiov.size); |
43642b38 DN |
1188 | |
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 | } | |
031380d8 | 1194 | g_free(cmp_buf); |
43642b38 DN |
1195 | } |
1196 | ||
1197 | if (ctx->qflag) { | |
1198 | goto out; | |
1199 | } | |
1200 | ||
1201 | if (ctx->vflag) { | |
1202 | dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); | |
1203 | } | |
1204 | ||
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); | |
7d8abfcb | 1209 | out: |
43642b38 | 1210 | qemu_io_free(ctx->buf); |
9e559533 | 1211 | qemu_iovec_destroy(&ctx->qiov); |
031380d8 | 1212 | g_free(ctx); |
95533d5f CH |
1213 | } |
1214 | ||
43642b38 | 1215 | static void aio_read_help(void) |
95533d5f | 1216 | { |
43642b38 | 1217 | printf( |
95533d5f CH |
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" | |
e432cef9 | 1226 | " The read is performed asynchronously and the aio_flush command must be\n" |
96bab41d | 1227 | " used to ensure all outstanding aio requests have been completed.\n" |
95533d5f CH |
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" | |
095343ad | 1231 | " -q, -- quiet mode, do not show I/O statistics\n" |
95533d5f CH |
1232 | "\n"); |
1233 | } | |
1234 | ||
734c3b85 | 1235 | static int aio_read_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
1236 | |
1237 | static const cmdinfo_t aio_read_cmd = { | |
43642b38 DN |
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, | |
22a2bdcb BS |
1245 | }; |
1246 | ||
734c3b85 | 1247 | static int aio_read_f(BlockDriverState *bs, int argc, char **argv) |
95533d5f | 1248 | { |
43642b38 | 1249 | int nr_iov, c; |
031380d8 | 1250 | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
43642b38 DN |
1251 | |
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) { | |
031380d8 | 1261 | g_free(ctx); |
43642b38 DN |
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: | |
031380d8 | 1272 | g_free(ctx); |
43642b38 DN |
1273 | return command_usage(&aio_read_cmd); |
1274 | } | |
1275 | } | |
1276 | ||
1277 | if (optind > argc - 2) { | |
031380d8 | 1278 | g_free(ctx); |
43642b38 DN |
1279 | return command_usage(&aio_read_cmd); |
1280 | } | |
1281 | ||
1282 | ctx->offset = cvtnum(argv[optind]); | |
1283 | if (ctx->offset < 0) { | |
1284 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
031380d8 | 1285 | g_free(ctx); |
43642b38 DN |
1286 | return 0; |
1287 | } | |
1288 | optind++; | |
1289 | ||
1290 | if (ctx->offset & 0x1ff) { | |
1291 | printf("offset %" PRId64 " is not sector aligned\n", | |
1292 | ctx->offset); | |
031380d8 | 1293 | g_free(ctx); |
43642b38 DN |
1294 | return 0; |
1295 | } | |
1296 | ||
1297 | nr_iov = argc - optind; | |
734c3b85 | 1298 | ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab); |
f2360620 | 1299 | if (ctx->buf == NULL) { |
031380d8 | 1300 | g_free(ctx); |
f2360620 KW |
1301 | return 0; |
1302 | } | |
43642b38 DN |
1303 | |
1304 | gettimeofday(&ctx->t1, NULL); | |
ad54ae80 PB |
1305 | bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov, |
1306 | ctx->qiov.size >> 9, aio_read_done, ctx); | |
43642b38 | 1307 | return 0; |
95533d5f CH |
1308 | } |
1309 | ||
43642b38 | 1310 | static void aio_write_help(void) |
95533d5f | 1311 | { |
43642b38 | 1312 | printf( |
95533d5f | 1313 | "\n" |
43642b38 | 1314 | " asynchronously writes a range of bytes from the given offset source\n" |
95533d5f CH |
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" | |
e432cef9 | 1322 | " The write is performed asynchronously and the aio_flush command must be\n" |
96bab41d | 1323 | " used to ensure all outstanding aio requests have been completed.\n" |
95533d5f CH |
1324 | " -P, -- use different pattern to fill file\n" |
1325 | " -C, -- report statistics in a machine parsable format\n" | |
095343ad | 1326 | " -q, -- quiet mode, do not show I/O statistics\n" |
95533d5f CH |
1327 | "\n"); |
1328 | } | |
1329 | ||
734c3b85 | 1330 | static int aio_write_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
1331 | |
1332 | static const cmdinfo_t aio_write_cmd = { | |
43642b38 DN |
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, | |
22a2bdcb | 1340 | }; |
95533d5f | 1341 | |
734c3b85 | 1342 | static int aio_write_f(BlockDriverState *bs, int argc, char **argv) |
95533d5f | 1343 | { |
43642b38 DN |
1344 | int nr_iov, c; |
1345 | int pattern = 0xcd; | |
031380d8 | 1346 | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
43642b38 DN |
1347 | |
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) { | |
031380d8 | 1359 | g_free(ctx); |
43642b38 DN |
1360 | return 0; |
1361 | } | |
1362 | break; | |
1363 | default: | |
031380d8 | 1364 | g_free(ctx); |
43642b38 DN |
1365 | return command_usage(&aio_write_cmd); |
1366 | } | |
1367 | } | |
1368 | ||
1369 | if (optind > argc - 2) { | |
031380d8 | 1370 | g_free(ctx); |
43642b38 DN |
1371 | return command_usage(&aio_write_cmd); |
1372 | } | |
1373 | ||
1374 | ctx->offset = cvtnum(argv[optind]); | |
1375 | if (ctx->offset < 0) { | |
1376 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
031380d8 | 1377 | g_free(ctx); |
43642b38 DN |
1378 | return 0; |
1379 | } | |
1380 | optind++; | |
1381 | ||
1382 | if (ctx->offset & 0x1ff) { | |
1383 | printf("offset %" PRId64 " is not sector aligned\n", | |
1384 | ctx->offset); | |
031380d8 | 1385 | g_free(ctx); |
43642b38 DN |
1386 | return 0; |
1387 | } | |
1388 | ||
1389 | nr_iov = argc - optind; | |
734c3b85 | 1390 | ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern); |
f2360620 | 1391 | if (ctx->buf == NULL) { |
031380d8 | 1392 | g_free(ctx); |
f2360620 KW |
1393 | return 0; |
1394 | } | |
43642b38 DN |
1395 | |
1396 | gettimeofday(&ctx->t1, NULL); | |
ad54ae80 PB |
1397 | bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov, |
1398 | ctx->qiov.size >> 9, aio_write_done, ctx); | |
43642b38 | 1399 | return 0; |
95533d5f CH |
1400 | } |
1401 | ||
734c3b85 | 1402 | static int aio_flush_f(BlockDriverState *bs, int argc, char **argv) |
95533d5f | 1403 | { |
e7c8b094 | 1404 | bdrv_drain_all(); |
43642b38 | 1405 | return 0; |
95533d5f CH |
1406 | } |
1407 | ||
1408 | static const cmdinfo_t aio_flush_cmd = { | |
43642b38 DN |
1409 | .name = "aio_flush", |
1410 | .cfunc = aio_flush_f, | |
1411 | .oneline = "completes all outstanding aio requests" | |
95533d5f CH |
1412 | }; |
1413 | ||
734c3b85 | 1414 | static int flush_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 1415 | { |
43642b38 DN |
1416 | bdrv_flush(bs); |
1417 | return 0; | |
e3aff4f6 AL |
1418 | } |
1419 | ||
1420 | static const cmdinfo_t flush_cmd = { | |
43642b38 DN |
1421 | .name = "flush", |
1422 | .altname = "f", | |
1423 | .cfunc = flush_f, | |
1424 | .oneline = "flush all in-core file state to disk", | |
e3aff4f6 AL |
1425 | }; |
1426 | ||
734c3b85 | 1427 | static int truncate_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 1428 | { |
43642b38 DN |
1429 | int64_t offset; |
1430 | int ret; | |
1431 | ||
1432 | offset = cvtnum(argv[1]); | |
1433 | if (offset < 0) { | |
1434 | printf("non-numeric truncate argument -- %s\n", argv[1]); | |
1435 | return 0; | |
1436 | } | |
1437 | ||
1438 | ret = bdrv_truncate(bs, offset); | |
1439 | if (ret < 0) { | |
1440 | printf("truncate: %s\n", strerror(-ret)); | |
1441 | return 0; | |
1442 | } | |
1443 | ||
1444 | return 0; | |
e3aff4f6 AL |
1445 | } |
1446 | ||
1447 | static const cmdinfo_t truncate_cmd = { | |
43642b38 DN |
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", | |
e3aff4f6 AL |
1455 | }; |
1456 | ||
734c3b85 | 1457 | static int length_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 1458 | { |
43642b38 DN |
1459 | int64_t size; |
1460 | char s1[64]; | |
1461 | ||
1462 | size = bdrv_getlength(bs); | |
1463 | if (size < 0) { | |
1464 | printf("getlength: %s\n", strerror(-size)); | |
1465 | return 0; | |
1466 | } | |
1467 | ||
1468 | cvtstr(size, s1, sizeof(s1)); | |
1469 | printf("%s\n", s1); | |
1470 | return 0; | |
e3aff4f6 AL |
1471 | } |
1472 | ||
1473 | ||
1474 | static const cmdinfo_t length_cmd = { | |
43642b38 DN |
1475 | .name = "length", |
1476 | .altname = "l", | |
1477 | .cfunc = length_f, | |
1478 | .oneline = "gets the length of the current file", | |
e3aff4f6 AL |
1479 | }; |
1480 | ||
1481 | ||
734c3b85 | 1482 | static int info_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 1483 | { |
43642b38 DN |
1484 | BlockDriverInfo bdi; |
1485 | char s1[64], s2[64]; | |
1486 | int ret; | |
e3aff4f6 | 1487 | |
43642b38 DN |
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 | } | |
e3aff4f6 | 1494 | |
43642b38 DN |
1495 | ret = bdrv_get_info(bs, &bdi); |
1496 | if (ret) { | |
1497 | return 0; | |
1498 | } | |
e3aff4f6 | 1499 | |
43642b38 DN |
1500 | cvtstr(bdi.cluster_size, s1, sizeof(s1)); |
1501 | cvtstr(bdi.vm_state_offset, s2, sizeof(s2)); | |
e3aff4f6 | 1502 | |
43642b38 DN |
1503 | printf("cluster size: %s\n", s1); |
1504 | printf("vm state offset: %s\n", s2); | |
e3aff4f6 | 1505 | |
43642b38 | 1506 | return 0; |
e3aff4f6 AL |
1507 | } |
1508 | ||
1509 | ||
1510 | ||
1511 | static const cmdinfo_t info_cmd = { | |
43642b38 DN |
1512 | .name = "info", |
1513 | .altname = "i", | |
1514 | .cfunc = info_f, | |
1515 | .oneline = "prints information about the current file", | |
e3aff4f6 AL |
1516 | }; |
1517 | ||
43642b38 | 1518 | static void discard_help(void) |
edff5db1 | 1519 | { |
43642b38 | 1520 | printf( |
edff5db1 SH |
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" | |
095343ad | 1529 | " -q, -- quiet mode, do not show I/O statistics\n" |
edff5db1 SH |
1530 | "\n"); |
1531 | } | |
1532 | ||
734c3b85 | 1533 | static int discard_f(BlockDriverState *bs, int argc, char **argv); |
edff5db1 SH |
1534 | |
1535 | static const cmdinfo_t discard_cmd = { | |
43642b38 DN |
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, | |
edff5db1 SH |
1544 | }; |
1545 | ||
734c3b85 | 1546 | static int discard_f(BlockDriverState *bs, int argc, char **argv) |
edff5db1 | 1547 | { |
43642b38 DN |
1548 | struct timeval t1, t2; |
1549 | int Cflag = 0, qflag = 0; | |
1550 | int c, ret; | |
1551 | int64_t offset; | |
1552 | int count; | |
1553 | ||
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 | } | |
1566 | ||
1567 | if (optind != argc - 2) { | |
1568 | return command_usage(&discard_cmd); | |
1569 | } | |
1570 | ||
1571 | offset = cvtnum(argv[optind]); | |
1572 | if (offset < 0) { | |
1573 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
1574 | return 0; | |
1575 | } | |
1576 | ||
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 | } | |
1583 | ||
1584 | gettimeofday(&t1, NULL); | |
1585 | ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS, | |
1586 | count >> BDRV_SECTOR_BITS); | |
1587 | gettimeofday(&t2, NULL); | |
1588 | ||
1589 | if (ret < 0) { | |
1590 | printf("discard failed: %s\n", strerror(-ret)); | |
1591 | goto out; | |
1592 | } | |
1593 | ||
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 | } | |
edff5db1 SH |
1599 | |
1600 | out: | |
43642b38 | 1601 | return 0; |
edff5db1 SH |
1602 | } |
1603 | ||
734c3b85 | 1604 | static int alloc_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 1605 | { |
cc785c34 | 1606 | int64_t offset, sector_num; |
43642b38 DN |
1607 | int nb_sectors, remaining; |
1608 | char s1[64]; | |
1609 | int num, sum_alloc; | |
1610 | int ret; | |
1611 | ||
1612 | offset = cvtnum(argv[1]); | |
cf49a6a0 KW |
1613 | if (offset < 0) { |
1614 | printf("non-numeric offset argument -- %s\n", argv[1]); | |
1615 | return 0; | |
1616 | } else if (offset & 0x1ff) { | |
43642b38 DN |
1617 | printf("offset %" PRId64 " is not sector aligned\n", |
1618 | offset); | |
1619 | return 0; | |
1620 | } | |
1621 | ||
1622 | if (argc == 3) { | |
1623 | nb_sectors = cvtnum(argv[2]); | |
cf49a6a0 KW |
1624 | if (nb_sectors < 0) { |
1625 | printf("non-numeric length argument -- %s\n", argv[2]); | |
1626 | return 0; | |
1627 | } | |
43642b38 DN |
1628 | } else { |
1629 | nb_sectors = 1; | |
1630 | } | |
1631 | ||
1632 | remaining = nb_sectors; | |
1633 | sum_alloc = 0; | |
cc785c34 | 1634 | sector_num = offset >> 9; |
43642b38 | 1635 | while (remaining) { |
cc785c34 PB |
1636 | ret = bdrv_is_allocated(bs, sector_num, remaining, &num); |
1637 | sector_num += num; | |
43642b38 DN |
1638 | remaining -= num; |
1639 | if (ret) { | |
1640 | sum_alloc += num; | |
1641 | } | |
cc785c34 PB |
1642 | if (num == 0) { |
1643 | nb_sectors -= remaining; | |
1644 | remaining = 0; | |
1645 | } | |
43642b38 DN |
1646 | } |
1647 | ||
1648 | cvtstr(offset, s1, sizeof(s1)); | |
1649 | ||
1650 | printf("%d/%d sectors allocated at offset %s\n", | |
1651 | sum_alloc, nb_sectors, s1); | |
1652 | return 0; | |
e3aff4f6 AL |
1653 | } |
1654 | ||
1655 | static const cmdinfo_t alloc_cmd = { | |
43642b38 DN |
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", | |
e3aff4f6 AL |
1663 | }; |
1664 | ||
a00e81e9 | 1665 | |
734c3b85 KW |
1666 | static int map_is_allocated(BlockDriverState *bs, int64_t sector_num, |
1667 | int64_t nb_sectors, int64_t *pnum) | |
a00e81e9 KW |
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 | ||
734c3b85 | 1697 | static int map_f(BlockDriverState *bs, int argc, char **argv) |
191c2890 | 1698 | { |
43642b38 DN |
1699 | int64_t offset; |
1700 | int64_t nb_sectors; | |
1701 | char s1[64]; | |
a00e81e9 | 1702 | int64_t num; |
43642b38 DN |
1703 | int ret; |
1704 | const char *retstr; | |
1705 | ||
1706 | offset = 0; | |
1707 | nb_sectors = bs->total_sectors; | |
1708 | ||
1709 | do { | |
734c3b85 | 1710 | ret = map_is_allocated(bs, offset, nb_sectors, &num); |
a00e81e9 KW |
1711 | if (ret < 0) { |
1712 | error_report("Failed to get allocation status: %s", strerror(-ret)); | |
1713 | return 0; | |
1714 | } | |
1715 | ||
43642b38 DN |
1716 | retstr = ret ? " allocated" : "not allocated"; |
1717 | cvtstr(offset << 9ULL, s1, sizeof(s1)); | |
a00e81e9 KW |
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); | |
43642b38 DN |
1721 | |
1722 | offset += num; | |
1723 | nb_sectors -= num; | |
1724 | } while (offset < bs->total_sectors); | |
1725 | ||
1726 | return 0; | |
191c2890 KW |
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 | ||
734c3b85 | 1738 | static int break_f(BlockDriverState *bs, int argc, char **argv) |
41c695c7 KW |
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 | ||
734c3b85 | 1760 | static int resume_f(BlockDriverState *bs, int argc, char **argv) |
41c695c7 KW |
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 | ||
734c3b85 | 1781 | static int wait_break_f(BlockDriverState *bs, int argc, char **argv) |
41c695c7 KW |
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 | ||
734c3b85 | 1799 | static int abort_f(BlockDriverState *bs, int argc, char **argv) |
e01c30d3 SH |
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 | }; | |
191c2890 | 1810 | |
734c3b85 | 1811 | static int close_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 1812 | { |
b4657855 | 1813 | bdrv_delete(bs); |
734c3b85 | 1814 | qemuio_bs = NULL; |
43642b38 | 1815 | return 0; |
e3aff4f6 AL |
1816 | } |
1817 | ||
1818 | static const cmdinfo_t close_cmd = { | |
43642b38 DN |
1819 | .name = "close", |
1820 | .altname = "c", | |
1821 | .cfunc = close_f, | |
1822 | .oneline = "close the current open file", | |
e3aff4f6 AL |
1823 | }; |
1824 | ||
9c4bab26 | 1825 | static int openfile(char *name, int flags, int growable) |
e3aff4f6 | 1826 | { |
734c3b85 | 1827 | if (qemuio_bs) { |
43642b38 DN |
1828 | fprintf(stderr, "file open already, try 'help close'\n"); |
1829 | return 1; | |
1830 | } | |
1831 | ||
1832 | if (growable) { | |
734c3b85 | 1833 | if (bdrv_file_open(&qemuio_bs, name, NULL, flags)) { |
43642b38 DN |
1834 | fprintf(stderr, "%s: can't open device %s\n", progname, name); |
1835 | return 1; | |
1836 | } | |
1837 | } else { | |
734c3b85 | 1838 | qemuio_bs = bdrv_new("hda"); |
43642b38 | 1839 | |
734c3b85 | 1840 | if (bdrv_open(qemuio_bs, name, NULL, flags, NULL) < 0) { |
43642b38 | 1841 | fprintf(stderr, "%s: can't open device %s\n", progname, name); |
734c3b85 KW |
1842 | bdrv_delete(qemuio_bs); |
1843 | qemuio_bs = NULL; | |
43642b38 DN |
1844 | return 1; |
1845 | } | |
1846 | } | |
1847 | ||
1848 | return 0; | |
e3aff4f6 AL |
1849 | } |
1850 | ||
43642b38 | 1851 | static void open_help(void) |
e3aff4f6 | 1852 | { |
43642b38 | 1853 | printf( |
e3aff4f6 AL |
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" | |
e3aff4f6 AL |
1861 | " -r, -- open file read-only\n" |
1862 | " -s, -- use snapshot file\n" | |
1863 | " -n, -- disable host cache\n" | |
9c4bab26 | 1864 | " -g, -- allow file to grow (only applies to protocols)" |
e3aff4f6 AL |
1865 | "\n"); |
1866 | } | |
1867 | ||
734c3b85 | 1868 | static int open_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
1869 | |
1870 | static const cmdinfo_t open_cmd = { | |
43642b38 DN |
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, | |
22a2bdcb | 1880 | }; |
e3aff4f6 | 1881 | |
734c3b85 | 1882 | static int open_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 1883 | { |
43642b38 DN |
1884 | int flags = 0; |
1885 | int readonly = 0; | |
1886 | int growable = 0; | |
1887 | int c; | |
1888 | ||
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); | |
f5edb014 | 1905 | } |
43642b38 DN |
1906 | } |
1907 | ||
1908 | if (!readonly) { | |
1909 | flags |= BDRV_O_RDWR; | |
1910 | } | |
e3aff4f6 | 1911 | |
43642b38 DN |
1912 | if (optind != argc - 1) { |
1913 | return command_usage(&open_cmd); | |
1914 | } | |
e3aff4f6 | 1915 | |
43642b38 | 1916 | return openfile(argv[optind], flags, growable); |
e3aff4f6 AL |
1917 | } |
1918 | ||
734c3b85 | 1919 | static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct) |
e3aff4f6 | 1920 | { |
43642b38 DN |
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; | |
e3aff4f6 AL |
1929 | } |
1930 | ||
1931 | static void usage(const char *name) | |
1932 | { | |
43642b38 | 1933 | printf( |
9a2d77ad | 1934 | "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n" |
84844a20 | 1935 | "QEMU Disk exerciser\n" |
e3aff4f6 | 1936 | "\n" |
e3aff4f6 AL |
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" | |
1db6947d | 1941 | " -g, --growable allow file to grow (only applies to protocols)\n" |
e3aff4f6 | 1942 | " -m, --misalign misalign allocations for O_DIRECT\n" |
5c6c3a6c | 1943 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
592fa070 | 1944 | " -t, --cache=MODE use the given cache mode for the image\n" |
d7bb72c8 | 1945 | " -T, --trace FILE enable trace events listed in the given file\n" |
e3aff4f6 AL |
1946 | " -h, --help display this help and exit\n" |
1947 | " -V, --version output version information and exit\n" | |
1948 | "\n", | |
43642b38 | 1949 | name); |
e3aff4f6 AL |
1950 | } |
1951 | ||
1952 | ||
1953 | int main(int argc, char **argv) | |
1954 | { | |
43642b38 DN |
1955 | int readonly = 0; |
1956 | int growable = 0; | |
9e8f1835 | 1957 | const char *sopt = "hVc:d:rsnmgkt:T:"; |
43642b38 DN |
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' }, | |
9e8f1835 | 1969 | { "discard", 1, NULL, 'd' }, |
592fa070 | 1970 | { "cache", 1, NULL, 't' }, |
d7bb72c8 | 1971 | { "trace", 1, NULL, 'T' }, |
43642b38 DN |
1972 | { NULL, 0, NULL, 0 } |
1973 | }; | |
1974 | int c; | |
1975 | int opt_index = 0; | |
9e8f1835 | 1976 | int flags = BDRV_O_UNMAP; |
43642b38 DN |
1977 | |
1978 | progname = basename(argv[0]); | |
1979 | ||
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; | |
9e8f1835 PB |
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; | |
43642b38 DN |
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; | |
592fa070 KW |
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; | |
d7bb72c8 SH |
2015 | case 'T': |
2016 | if (!trace_backend_init(optarg, NULL)) { | |
2017 | exit(1); /* error message will have been printed */ | |
2018 | } | |
2019 | break; | |
43642b38 DN |
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); | |
f5edb014 | 2029 | } |
43642b38 DN |
2030 | } |
2031 | ||
2032 | if ((argc - optind) > 1) { | |
2033 | usage(progname); | |
2034 | exit(1); | |
2035 | } | |
e3aff4f6 | 2036 | |
a57d1143 | 2037 | qemu_init_main_loop(); |
2592c59a | 2038 | bdrv_init(); |
a57d1143 | 2039 | |
43642b38 DN |
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); | |
41c695c7 KW |
2060 | add_command(&break_cmd); |
2061 | add_command(&resume_cmd); | |
2062 | add_command(&wait_break_cmd); | |
e01c30d3 | 2063 | add_command(&abort_cmd); |
43642b38 | 2064 | |
43642b38 DN |
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(); | |
e3aff4f6 | 2076 | |
43642b38 | 2077 | /* |
922453bc | 2078 | * Make sure all outstanding requests complete before the program exits. |
43642b38 | 2079 | */ |
922453bc | 2080 | bdrv_drain_all(); |
95533d5f | 2081 | |
734c3b85 KW |
2082 | if (qemuio_bs) { |
2083 | bdrv_delete(qemuio_bs); | |
43642b38 DN |
2084 | } |
2085 | return 0; | |
e3aff4f6 | 2086 | } |