]>
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 | */ | |
10 | #include <sys/types.h> | |
11 | #include <stdarg.h> | |
12 | #include <stdio.h> | |
13 | #include <getopt.h> | |
14 | ||
15 | #include "qemu-common.h" | |
16 | #include "block_int.h" | |
17 | #include "cmd.h" | |
18 | ||
19 | #define VERSION "0.0.1" | |
20 | ||
21 | #define CMD_NOFILE_OK 0x01 | |
22 | ||
23 | char *progname; | |
24 | static BlockDriverState *bs; | |
25 | ||
26 | static int misalign; | |
27 | ||
28 | /* | |
29 | * Memory allocation helpers. | |
30 | * | |
31 | * Make sure memory is aligned by default, or purposefully misaligned if | |
32 | * that is specified on the command line. | |
33 | */ | |
34 | ||
35 | #define MISALIGN_OFFSET 16 | |
36 | static void *qemu_io_alloc(size_t len, int pattern) | |
37 | { | |
38 | void *buf; | |
39 | ||
40 | if (misalign) | |
41 | len += MISALIGN_OFFSET; | |
42 | buf = qemu_memalign(512, len); | |
43 | memset(buf, pattern, len); | |
44 | if (misalign) | |
45 | buf += MISALIGN_OFFSET; | |
46 | return buf; | |
47 | } | |
48 | ||
49 | static void qemu_io_free(void *p) | |
50 | { | |
51 | if (misalign) | |
52 | p -= MISALIGN_OFFSET; | |
53 | qemu_vfree(p); | |
54 | } | |
55 | ||
56 | static void | |
3abcdf49 | 57 | dump_buffer(const void *buffer, int64_t offset, int len) |
e3aff4f6 AL |
58 | { |
59 | int i, j; | |
3abcdf49 | 60 | const uint8_t *p; |
e3aff4f6 AL |
61 | |
62 | for (i = 0, p = buffer; i < len; i += 16) { | |
3abcdf49 | 63 | const uint8_t *s = p; |
e3aff4f6 AL |
64 | |
65 | printf("%08llx: ", (unsigned long long)offset + i); | |
66 | for (j = 0; j < 16 && i + j < len; j++, p++) | |
67 | printf("%02x ", *p); | |
68 | printf(" "); | |
69 | for (j = 0; j < 16 && i + j < len; j++, s++) { | |
3abcdf49 | 70 | if (isalnum(*s)) |
e3aff4f6 AL |
71 | printf("%c", *s); |
72 | else | |
73 | printf("."); | |
74 | } | |
75 | printf("\n"); | |
76 | } | |
77 | } | |
78 | ||
79 | static void | |
80 | print_report(const char *op, struct timeval *t, int64_t offset, | |
81 | int count, int total, int cnt, int Cflag) | |
82 | { | |
83 | char s1[64], s2[64], ts[64]; | |
84 | ||
85 | timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0); | |
86 | if (!Cflag) { | |
87 | cvtstr((double)total, s1, sizeof(s1)); | |
88 | cvtstr(tdiv((double)total, *t), s2, sizeof(s2)); | |
89 | printf("%s %d/%d bytes at offset %lld\n", | |
90 | op, total, count, (long long)offset); | |
91 | printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n", | |
92 | s1, cnt, ts, s2, tdiv((double)cnt, *t)); | |
93 | } else {/* bytes,ops,time,bytes/sec,ops/sec */ | |
94 | printf("%d,%d,%s,%.3f,%.3f\n", | |
95 | total, cnt, ts, | |
96 | tdiv((double)total, *t), | |
97 | tdiv((double)cnt, *t)); | |
98 | } | |
99 | } | |
100 | ||
cf57298a CH |
101 | /* |
102 | * Parse multiple length statements for vectored I/O, and construct an I/O | |
103 | * vector matching it. | |
104 | */ | |
105 | static void * | |
106 | create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern) | |
107 | { | |
108 | size_t *sizes = calloc(nr_iov, sizeof(size_t)); | |
109 | size_t count = 0; | |
110 | void *buf, *p; | |
111 | int i; | |
112 | ||
113 | for (i = 0; i < nr_iov; i++) { | |
114 | char *arg = argv[i]; | |
115 | long long len; | |
116 | ||
117 | len = cvtnum(arg); | |
118 | if (len < 0) { | |
119 | printf("non-numeric length argument -- %s\n", arg); | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | /* should be SIZE_T_MAX, but that doesn't exist */ | |
124 | if (len > UINT_MAX) { | |
125 | printf("too large length argument -- %s\n", arg); | |
126 | return NULL; | |
127 | } | |
128 | ||
129 | if (len & 0x1ff) { | |
130 | printf("length argument %lld is not sector aligned\n", | |
131 | len); | |
132 | return NULL; | |
133 | } | |
134 | ||
135 | sizes[i] = len; | |
136 | count += len; | |
137 | } | |
138 | ||
139 | qemu_iovec_init(qiov, nr_iov); | |
140 | ||
141 | buf = p = qemu_io_alloc(count, pattern); | |
142 | ||
143 | for (i = 0; i < nr_iov; i++) { | |
144 | qemu_iovec_add(qiov, p, sizes[i]); | |
145 | p += sizes[i]; | |
146 | } | |
147 | ||
148 | free(sizes); | |
149 | return buf; | |
150 | } | |
151 | ||
e3aff4f6 AL |
152 | static int do_read(char *buf, int64_t offset, int count, int *total) |
153 | { | |
154 | int ret; | |
155 | ||
156 | ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9); | |
157 | if (ret < 0) | |
158 | return ret; | |
159 | *total = count; | |
160 | return 1; | |
161 | } | |
162 | ||
163 | static int do_write(char *buf, int64_t offset, int count, int *total) | |
164 | { | |
165 | int ret; | |
166 | ||
167 | ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9); | |
168 | if (ret < 0) | |
169 | return ret; | |
170 | *total = count; | |
171 | return 1; | |
172 | } | |
173 | ||
174 | static int do_pread(char *buf, int64_t offset, int count, int *total) | |
175 | { | |
176 | *total = bdrv_pread(bs, offset, (uint8_t *)buf, count); | |
177 | if (*total < 0) | |
178 | return *total; | |
179 | return 1; | |
180 | } | |
181 | ||
182 | static int do_pwrite(char *buf, int64_t offset, int count, int *total) | |
183 | { | |
184 | *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count); | |
185 | if (*total < 0) | |
186 | return *total; | |
187 | return 1; | |
188 | } | |
189 | ||
190 | #define NOT_DONE 0x7fffffff | |
191 | static void aio_rw_done(void *opaque, int ret) | |
192 | { | |
193 | *(int *)opaque = ret; | |
194 | } | |
195 | ||
196 | static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total) | |
197 | { | |
198 | BlockDriverAIOCB *acb; | |
199 | int async_ret = NOT_DONE; | |
200 | ||
201 | acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9, | |
202 | aio_rw_done, &async_ret); | |
203 | if (!acb) | |
204 | return -EIO; | |
205 | ||
206 | while (async_ret == NOT_DONE) | |
207 | qemu_aio_wait(); | |
208 | ||
209 | *total = qiov->size; | |
210 | return async_ret < 0 ? async_ret : 1; | |
211 | } | |
212 | ||
213 | static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total) | |
214 | { | |
215 | BlockDriverAIOCB *acb; | |
216 | int async_ret = NOT_DONE; | |
217 | ||
218 | acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9, | |
219 | aio_rw_done, &async_ret); | |
220 | if (!acb) | |
221 | return -EIO; | |
222 | ||
223 | while (async_ret == NOT_DONE) | |
224 | qemu_aio_wait(); | |
225 | ||
7e9bbc9f | 226 | *total = qiov->size; |
e3aff4f6 AL |
227 | return async_ret < 0 ? async_ret : 1; |
228 | } | |
229 | ||
230 | ||
231 | static const cmdinfo_t read_cmd; | |
232 | ||
233 | static void | |
234 | read_help(void) | |
235 | { | |
236 | printf( | |
237 | "\n" | |
238 | " reads a range of bytes from the given offset\n" | |
239 | "\n" | |
240 | " Example:\n" | |
241 | " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n" | |
242 | "\n" | |
243 | " Reads a segment of the currently open file, optionally dumping it to the\n" | |
244 | " standard output stream (with -v option) for subsequent inspection.\n" | |
d9654a58 KW |
245 | " -C, -- report statistics in a machine parsable format\n" |
246 | " -l, -- length for pattern verification (only with -P)\n" | |
e3aff4f6 | 247 | " -p, -- use bdrv_pread to read the file\n" |
c48101ae | 248 | " -P, -- use a pattern to verify read data\n" |
e3aff4f6 | 249 | " -q, -- quite mode, do not show I/O statistics\n" |
d9654a58 KW |
250 | " -s, -- start offset for pattern verification (only with -P)\n" |
251 | " -v, -- dump buffer to standard output\n" | |
e3aff4f6 AL |
252 | "\n"); |
253 | } | |
254 | ||
255 | static int | |
256 | read_f(int argc, char **argv) | |
257 | { | |
258 | struct timeval t1, t2; | |
259 | int Cflag = 0, pflag = 0, qflag = 0, vflag = 0; | |
d9654a58 | 260 | int Pflag = 0, sflag = 0, lflag = 0; |
e3aff4f6 AL |
261 | int c, cnt; |
262 | char *buf; | |
263 | int64_t offset; | |
d4ec5228 PB |
264 | int count; |
265 | /* Some compilers get confused and warn if this is not initialized. */ | |
266 | int total = 0; | |
d9654a58 | 267 | int pattern = 0, pattern_offset = 0, pattern_count = 0; |
e3aff4f6 | 268 | |
d9654a58 | 269 | while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) { |
e3aff4f6 AL |
270 | switch (c) { |
271 | case 'C': | |
272 | Cflag = 1; | |
273 | break; | |
d9654a58 KW |
274 | case 'l': |
275 | lflag = 1; | |
276 | pattern_count = cvtnum(optarg); | |
277 | if (pattern_count < 0) { | |
278 | printf("non-numeric length argument -- %s\n", optarg); | |
279 | return 0; | |
280 | } | |
281 | break; | |
e3aff4f6 AL |
282 | case 'p': |
283 | pflag = 1; | |
284 | break; | |
c48101ae AL |
285 | case 'P': |
286 | Pflag = 1; | |
287 | pattern = atoi(optarg); | |
288 | break; | |
e3aff4f6 AL |
289 | case 'q': |
290 | qflag = 1; | |
291 | break; | |
d9654a58 KW |
292 | case 's': |
293 | sflag = 1; | |
294 | pattern_offset = cvtnum(optarg); | |
295 | if (pattern_offset < 0) { | |
296 | printf("non-numeric length argument -- %s\n", optarg); | |
297 | return 0; | |
298 | } | |
299 | break; | |
e3aff4f6 AL |
300 | case 'v': |
301 | vflag = 1; | |
302 | break; | |
303 | default: | |
304 | return command_usage(&read_cmd); | |
305 | } | |
306 | } | |
307 | ||
308 | if (optind != argc - 2) | |
309 | return command_usage(&read_cmd); | |
310 | ||
311 | offset = cvtnum(argv[optind]); | |
312 | if (offset < 0) { | |
313 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | optind++; | |
318 | count = cvtnum(argv[optind]); | |
319 | if (count < 0) { | |
320 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
321 | return 0; | |
322 | } | |
323 | ||
d9654a58 KW |
324 | if (!Pflag && (lflag || sflag)) { |
325 | return command_usage(&read_cmd); | |
326 | } | |
327 | ||
328 | if (!lflag) { | |
329 | pattern_count = count - pattern_offset; | |
330 | } | |
331 | ||
332 | if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) { | |
333 | printf("pattern verfication range exceeds end of read data\n"); | |
334 | return 0; | |
335 | } | |
336 | ||
e3aff4f6 AL |
337 | if (!pflag) |
338 | if (offset & 0x1ff) { | |
339 | printf("offset %lld is not sector aligned\n", | |
340 | (long long)offset); | |
341 | return 0; | |
342 | ||
343 | if (count & 0x1ff) { | |
344 | printf("count %d is not sector aligned\n", | |
345 | count); | |
346 | return 0; | |
347 | } | |
348 | } | |
349 | ||
350 | buf = qemu_io_alloc(count, 0xab); | |
351 | ||
352 | gettimeofday(&t1, NULL); | |
353 | if (pflag) | |
354 | cnt = do_pread(buf, offset, count, &total); | |
355 | else | |
356 | cnt = do_read(buf, offset, count, &total); | |
357 | gettimeofday(&t2, NULL); | |
358 | ||
359 | if (cnt < 0) { | |
360 | printf("read failed: %s\n", strerror(-cnt)); | |
361 | return 0; | |
362 | } | |
363 | ||
c48101ae | 364 | if (Pflag) { |
d9654a58 KW |
365 | void* cmp_buf = malloc(pattern_count); |
366 | memset(cmp_buf, pattern, pattern_count); | |
367 | if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) { | |
c48101ae AL |
368 | printf("Pattern verification failed at offset %lld, " |
369 | "%d bytes\n", | |
d9654a58 | 370 | (long long) offset + pattern_offset, pattern_count); |
c48101ae AL |
371 | } |
372 | free(cmp_buf); | |
373 | } | |
374 | ||
e3aff4f6 AL |
375 | if (qflag) |
376 | return 0; | |
377 | ||
378 | if (vflag) | |
379 | dump_buffer(buf, offset, count); | |
380 | ||
381 | /* Finally, report back -- -C gives a parsable format */ | |
382 | t2 = tsub(t2, t1); | |
383 | print_report("read", &t2, offset, count, total, cnt, Cflag); | |
384 | ||
385 | qemu_io_free(buf); | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | static const cmdinfo_t read_cmd = { | |
391 | .name = "read", | |
392 | .altname = "r", | |
393 | .cfunc = read_f, | |
394 | .argmin = 2, | |
395 | .argmax = -1, | |
d9654a58 | 396 | .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len", |
e3aff4f6 AL |
397 | .oneline = "reads a number of bytes at a specified offset", |
398 | .help = read_help, | |
399 | }; | |
400 | ||
401 | static const cmdinfo_t readv_cmd; | |
402 | ||
403 | static void | |
404 | readv_help(void) | |
405 | { | |
406 | printf( | |
407 | "\n" | |
408 | " reads a range of bytes from the given offset into multiple buffers\n" | |
409 | "\n" | |
410 | " Example:\n" | |
411 | " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" | |
412 | "\n" | |
413 | " Reads a segment of the currently open file, optionally dumping it to the\n" | |
414 | " standard output stream (with -v option) for subsequent inspection.\n" | |
415 | " Uses multiple iovec buffers if more than one byte range is specified.\n" | |
416 | " -C, -- report statistics in a machine parsable format\n" | |
c48101ae | 417 | " -P, -- use a pattern to verify read data\n" |
e3aff4f6 AL |
418 | " -v, -- dump buffer to standard output\n" |
419 | " -q, -- quite mode, do not show I/O statistics\n" | |
420 | "\n"); | |
421 | } | |
422 | ||
423 | static int | |
424 | readv_f(int argc, char **argv) | |
425 | { | |
426 | struct timeval t1, t2; | |
427 | int Cflag = 0, qflag = 0, vflag = 0; | |
428 | int c, cnt; | |
cf57298a | 429 | char *buf; |
e3aff4f6 | 430 | int64_t offset; |
cf57298a CH |
431 | int total; |
432 | int nr_iov; | |
e3aff4f6 | 433 | QEMUIOVector qiov; |
c48101ae AL |
434 | int pattern = 0; |
435 | int Pflag = 0; | |
e3aff4f6 | 436 | |
c48101ae | 437 | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
e3aff4f6 AL |
438 | switch (c) { |
439 | case 'C': | |
440 | Cflag = 1; | |
441 | break; | |
c48101ae AL |
442 | case 'P': |
443 | Pflag = 1; | |
444 | pattern = atoi(optarg); | |
445 | break; | |
e3aff4f6 AL |
446 | case 'q': |
447 | qflag = 1; | |
448 | break; | |
449 | case 'v': | |
450 | vflag = 1; | |
451 | break; | |
452 | default: | |
453 | return command_usage(&readv_cmd); | |
454 | } | |
455 | } | |
456 | ||
457 | if (optind > argc - 2) | |
458 | return command_usage(&readv_cmd); | |
459 | ||
460 | ||
461 | offset = cvtnum(argv[optind]); | |
462 | if (offset < 0) { | |
463 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
464 | return 0; | |
465 | } | |
466 | optind++; | |
467 | ||
468 | if (offset & 0x1ff) { | |
469 | printf("offset %lld is not sector aligned\n", | |
470 | (long long)offset); | |
471 | return 0; | |
472 | } | |
473 | ||
e3aff4f6 | 474 | nr_iov = argc - optind; |
cf57298a | 475 | buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab); |
e3aff4f6 AL |
476 | |
477 | gettimeofday(&t1, NULL); | |
478 | cnt = do_aio_readv(&qiov, offset, &total); | |
479 | gettimeofday(&t2, NULL); | |
480 | ||
481 | if (cnt < 0) { | |
482 | printf("readv failed: %s\n", strerror(-cnt)); | |
483 | return 0; | |
484 | } | |
485 | ||
c48101ae | 486 | if (Pflag) { |
cf57298a CH |
487 | void* cmp_buf = malloc(qiov.size); |
488 | memset(cmp_buf, pattern, qiov.size); | |
489 | if (memcmp(buf, cmp_buf, qiov.size)) { | |
c48101ae | 490 | printf("Pattern verification failed at offset %lld, " |
cf57298a CH |
491 | "%zd bytes\n", |
492 | (long long) offset, qiov.size); | |
c48101ae AL |
493 | } |
494 | free(cmp_buf); | |
495 | } | |
496 | ||
e3aff4f6 AL |
497 | if (qflag) |
498 | return 0; | |
499 | ||
500 | if (vflag) | |
501 | dump_buffer(buf, offset, qiov.size); | |
502 | ||
503 | /* Finally, report back -- -C gives a parsable format */ | |
504 | t2 = tsub(t2, t1); | |
505 | print_report("read", &t2, offset, qiov.size, total, cnt, Cflag); | |
506 | ||
507 | qemu_io_free(buf); | |
508 | ||
509 | return 0; | |
510 | } | |
511 | ||
512 | static const cmdinfo_t readv_cmd = { | |
513 | .name = "readv", | |
514 | .cfunc = readv_f, | |
515 | .argmin = 2, | |
516 | .argmax = -1, | |
c48101ae | 517 | .args = "[-Cqv] [-P pattern ] off len [len..]", |
e3aff4f6 AL |
518 | .oneline = "reads a number of bytes at a specified offset", |
519 | .help = readv_help, | |
520 | }; | |
521 | ||
522 | static const cmdinfo_t write_cmd; | |
523 | ||
524 | static void | |
525 | write_help(void) | |
526 | { | |
527 | printf( | |
528 | "\n" | |
529 | " writes a range of bytes from the given offset\n" | |
530 | "\n" | |
531 | " Example:\n" | |
532 | " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n" | |
533 | "\n" | |
534 | " Writes into a segment of the currently open file, using a buffer\n" | |
535 | " filled with a set pattern (0xcdcdcdcd).\n" | |
536 | " -p, -- use bdrv_pwrite to write the file\n" | |
537 | " -P, -- use different pattern to fill file\n" | |
538 | " -C, -- report statistics in a machine parsable format\n" | |
539 | " -q, -- quite mode, do not show I/O statistics\n" | |
540 | "\n"); | |
541 | } | |
542 | ||
543 | static int | |
544 | write_f(int argc, char **argv) | |
545 | { | |
546 | struct timeval t1, t2; | |
547 | int Cflag = 0, pflag = 0, qflag = 0; | |
548 | int c, cnt; | |
549 | char *buf; | |
550 | int64_t offset; | |
d4ec5228 PB |
551 | int count; |
552 | /* Some compilers get confused and warn if this is not initialized. */ | |
553 | int total = 0; | |
e3aff4f6 AL |
554 | int pattern = 0xcd; |
555 | ||
556 | while ((c = getopt(argc, argv, "CpP:q")) != EOF) { | |
557 | switch (c) { | |
558 | case 'C': | |
559 | Cflag = 1; | |
560 | break; | |
561 | case 'p': | |
562 | pflag = 1; | |
563 | break; | |
564 | case 'P': | |
565 | pattern = atoi(optarg); | |
566 | break; | |
567 | case 'q': | |
568 | qflag = 1; | |
569 | break; | |
570 | default: | |
571 | return command_usage(&write_cmd); | |
572 | } | |
573 | } | |
574 | ||
575 | if (optind != argc - 2) | |
576 | return command_usage(&write_cmd); | |
577 | ||
578 | offset = cvtnum(argv[optind]); | |
579 | if (offset < 0) { | |
580 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
581 | return 0; | |
582 | } | |
583 | ||
584 | optind++; | |
585 | count = cvtnum(argv[optind]); | |
586 | if (count < 0) { | |
587 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
588 | return 0; | |
589 | } | |
590 | ||
591 | if (!pflag) { | |
592 | if (offset & 0x1ff) { | |
593 | printf("offset %lld is not sector aligned\n", | |
594 | (long long)offset); | |
595 | return 0; | |
596 | } | |
597 | ||
598 | if (count & 0x1ff) { | |
599 | printf("count %d is not sector aligned\n", | |
600 | count); | |
601 | return 0; | |
602 | } | |
603 | } | |
604 | ||
605 | buf = qemu_io_alloc(count, pattern); | |
606 | ||
607 | gettimeofday(&t1, NULL); | |
608 | if (pflag) | |
609 | cnt = do_pwrite(buf, offset, count, &total); | |
610 | else | |
611 | cnt = do_write(buf, offset, count, &total); | |
612 | gettimeofday(&t2, NULL); | |
613 | ||
614 | if (cnt < 0) { | |
615 | printf("write failed: %s\n", strerror(-cnt)); | |
616 | return 0; | |
617 | } | |
618 | ||
619 | if (qflag) | |
620 | return 0; | |
621 | ||
622 | /* Finally, report back -- -C gives a parsable format */ | |
623 | t2 = tsub(t2, t1); | |
624 | print_report("wrote", &t2, offset, count, total, cnt, Cflag); | |
625 | ||
626 | qemu_io_free(buf); | |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
631 | static const cmdinfo_t write_cmd = { | |
632 | .name = "write", | |
633 | .altname = "w", | |
634 | .cfunc = write_f, | |
635 | .argmin = 2, | |
636 | .argmax = -1, | |
637 | .args = "[-aCpq] [-P pattern ] off len", | |
638 | .oneline = "writes a number of bytes at a specified offset", | |
639 | .help = write_help, | |
640 | }; | |
641 | ||
642 | static const cmdinfo_t writev_cmd; | |
643 | ||
644 | static void | |
645 | writev_help(void) | |
646 | { | |
647 | printf( | |
648 | "\n" | |
649 | " writes a range of bytes from the given offset source from multiple buffers\n" | |
650 | "\n" | |
651 | " Example:\n" | |
652 | " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" | |
653 | "\n" | |
654 | " Writes into a segment of the currently open file, using a buffer\n" | |
655 | " filled with a set pattern (0xcdcdcdcd).\n" | |
656 | " -P, -- use different pattern to fill file\n" | |
657 | " -C, -- report statistics in a machine parsable format\n" | |
658 | " -q, -- quite mode, do not show I/O statistics\n" | |
659 | "\n"); | |
660 | } | |
661 | ||
662 | static int | |
663 | writev_f(int argc, char **argv) | |
664 | { | |
665 | struct timeval t1, t2; | |
666 | int Cflag = 0, qflag = 0; | |
667 | int c, cnt; | |
cf57298a | 668 | char *buf; |
e3aff4f6 | 669 | int64_t offset; |
cf57298a CH |
670 | int total; |
671 | int nr_iov; | |
e3aff4f6 AL |
672 | int pattern = 0xcd; |
673 | QEMUIOVector qiov; | |
674 | ||
675 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { | |
676 | switch (c) { | |
677 | case 'C': | |
678 | Cflag = 1; | |
679 | break; | |
680 | case 'q': | |
681 | qflag = 1; | |
682 | break; | |
683 | case 'P': | |
684 | pattern = atoi(optarg); | |
685 | break; | |
686 | default: | |
687 | return command_usage(&writev_cmd); | |
688 | } | |
689 | } | |
690 | ||
691 | if (optind > argc - 2) | |
692 | return command_usage(&writev_cmd); | |
693 | ||
694 | offset = cvtnum(argv[optind]); | |
695 | if (offset < 0) { | |
696 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
697 | return 0; | |
698 | } | |
699 | optind++; | |
700 | ||
701 | if (offset & 0x1ff) { | |
702 | printf("offset %lld is not sector aligned\n", | |
703 | (long long)offset); | |
704 | return 0; | |
705 | } | |
706 | ||
e3aff4f6 | 707 | nr_iov = argc - optind; |
cf57298a | 708 | buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern); |
e3aff4f6 AL |
709 | |
710 | gettimeofday(&t1, NULL); | |
711 | cnt = do_aio_writev(&qiov, offset, &total); | |
712 | gettimeofday(&t2, NULL); | |
713 | ||
714 | if (cnt < 0) { | |
715 | printf("writev failed: %s\n", strerror(-cnt)); | |
716 | return 0; | |
717 | } | |
718 | ||
719 | if (qflag) | |
720 | return 0; | |
721 | ||
722 | /* Finally, report back -- -C gives a parsable format */ | |
723 | t2 = tsub(t2, t1); | |
724 | print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag); | |
725 | ||
726 | qemu_io_free(buf); | |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | static const cmdinfo_t writev_cmd = { | |
732 | .name = "writev", | |
733 | .cfunc = writev_f, | |
734 | .argmin = 2, | |
735 | .argmax = -1, | |
736 | .args = "[-Cq] [-P pattern ] off len [len..]", | |
737 | .oneline = "writes a number of bytes at a specified offset", | |
738 | .help = writev_help, | |
739 | }; | |
740 | ||
95533d5f CH |
741 | struct aio_ctx { |
742 | QEMUIOVector qiov; | |
743 | int64_t offset; | |
744 | char *buf; | |
745 | int qflag; | |
746 | int vflag; | |
747 | int Cflag; | |
748 | int Pflag; | |
749 | int pattern; | |
750 | struct timeval t1; | |
751 | }; | |
752 | ||
753 | static void | |
754 | aio_write_done(void *opaque, int ret) | |
755 | { | |
756 | struct aio_ctx *ctx = opaque; | |
757 | struct timeval t2; | |
95533d5f CH |
758 | |
759 | gettimeofday(&t2, NULL); | |
760 | ||
95533d5f CH |
761 | |
762 | if (ret < 0) { | |
763 | printf("aio_write failed: %s\n", strerror(-ret)); | |
764 | return; | |
765 | } | |
766 | ||
230d4fa4 | 767 | if (ctx->qflag) { |
95533d5f | 768 | return; |
230d4fa4 | 769 | } |
95533d5f CH |
770 | |
771 | /* Finally, report back -- -C gives a parsable format */ | |
772 | t2 = tsub(t2, ctx->t1); | |
230d4fa4 CH |
773 | print_report("wrote", &t2, ctx->offset, ctx->qiov.size, |
774 | ctx->qiov.size, 1, ctx->Cflag); | |
95533d5f CH |
775 | |
776 | qemu_io_free(ctx->buf); | |
777 | free(ctx); | |
778 | } | |
779 | ||
780 | static const cmdinfo_t aio_read_cmd; | |
781 | ||
782 | static void | |
783 | aio_read_done(void *opaque, int ret) | |
784 | { | |
785 | struct aio_ctx *ctx = opaque; | |
786 | struct timeval t2; | |
95533d5f CH |
787 | |
788 | gettimeofday(&t2, NULL); | |
789 | ||
95533d5f CH |
790 | if (ret < 0) { |
791 | printf("readv failed: %s\n", strerror(-ret)); | |
792 | return; | |
793 | } | |
794 | ||
795 | if (ctx->Pflag) { | |
230d4fa4 | 796 | void *cmp_buf = malloc(ctx->qiov.size); |
95533d5f | 797 | |
230d4fa4 CH |
798 | memset(cmp_buf, ctx->pattern, ctx->qiov.size); |
799 | if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) { | |
95533d5f | 800 | printf("Pattern verification failed at offset %lld, " |
230d4fa4 CH |
801 | "%zd bytes\n", |
802 | (long long) ctx->offset, ctx->qiov.size); | |
95533d5f CH |
803 | } |
804 | free(cmp_buf); | |
805 | } | |
806 | ||
230d4fa4 | 807 | if (ctx->qflag) { |
95533d5f | 808 | return; |
230d4fa4 | 809 | } |
95533d5f | 810 | |
230d4fa4 CH |
811 | if (ctx->vflag) { |
812 | dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); | |
813 | } | |
95533d5f CH |
814 | |
815 | /* Finally, report back -- -C gives a parsable format */ | |
816 | t2 = tsub(t2, ctx->t1); | |
230d4fa4 CH |
817 | print_report("read", &t2, ctx->offset, ctx->qiov.size, |
818 | ctx->qiov.size, 1, ctx->Cflag); | |
95533d5f CH |
819 | |
820 | qemu_io_free(ctx->buf); | |
821 | free(ctx); | |
95533d5f CH |
822 | } |
823 | ||
824 | static void | |
825 | aio_read_help(void) | |
826 | { | |
827 | printf( | |
828 | "\n" | |
829 | " asynchronously reads a range of bytes from the given offset\n" | |
830 | "\n" | |
831 | " Example:\n" | |
832 | " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" | |
833 | "\n" | |
834 | " Reads a segment of the currently open file, optionally dumping it to the\n" | |
835 | " standard output stream (with -v option) for subsequent inspection.\n" | |
836 | " The read is performed asynchronously and should the aio_flush command \n" | |
837 | " should be used to ensure all outstanding aio requests have been completed\n" | |
838 | " -C, -- report statistics in a machine parsable format\n" | |
839 | " -P, -- use a pattern to verify read data\n" | |
840 | " -v, -- dump buffer to standard output\n" | |
841 | " -q, -- quite mode, do not show I/O statistics\n" | |
842 | "\n"); | |
843 | } | |
844 | ||
845 | static int | |
846 | aio_read_f(int argc, char **argv) | |
847 | { | |
cf57298a | 848 | int nr_iov, c; |
95533d5f CH |
849 | struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx)); |
850 | BlockDriverAIOCB *acb; | |
851 | ||
95533d5f CH |
852 | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
853 | switch (c) { | |
854 | case 'C': | |
855 | ctx->Cflag = 1; | |
856 | break; | |
857 | case 'P': | |
858 | ctx->Pflag = 1; | |
859 | ctx->pattern = atoi(optarg); | |
860 | break; | |
861 | case 'q': | |
862 | ctx->qflag = 1; | |
863 | break; | |
864 | case 'v': | |
865 | ctx->vflag = 1; | |
866 | break; | |
867 | default: | |
868 | return command_usage(&aio_read_cmd); | |
869 | } | |
870 | } | |
871 | ||
872 | if (optind > argc - 2) | |
873 | return command_usage(&aio_read_cmd); | |
874 | ||
875 | ||
876 | ctx->offset = cvtnum(argv[optind]); | |
877 | if (ctx->offset < 0) { | |
878 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
879 | return 0; | |
880 | } | |
881 | optind++; | |
882 | ||
883 | if (ctx->offset & 0x1ff) { | |
884 | printf("offset %lld is not sector aligned\n", | |
885 | (long long)ctx->offset); | |
886 | return 0; | |
887 | } | |
888 | ||
95533d5f | 889 | nr_iov = argc - optind; |
cf57298a | 890 | ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab); |
95533d5f CH |
891 | |
892 | gettimeofday(&ctx->t1, NULL); | |
893 | acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov, | |
894 | ctx->qiov.size >> 9, aio_read_done, ctx); | |
895 | if (!acb) | |
896 | return -EIO; | |
897 | ||
898 | return 0; | |
899 | } | |
900 | ||
901 | static const cmdinfo_t aio_read_cmd = { | |
902 | .name = "aio_read", | |
903 | .cfunc = aio_read_f, | |
904 | .argmin = 2, | |
905 | .argmax = -1, | |
906 | .args = "[-Cqv] [-P pattern ] off len [len..]", | |
907 | .oneline = "asynchronously reads a number of bytes", | |
908 | .help = aio_read_help, | |
909 | }; | |
910 | ||
911 | static const cmdinfo_t aio_write_cmd; | |
912 | ||
913 | static void | |
914 | aio_write_help(void) | |
915 | { | |
916 | printf( | |
917 | "\n" | |
918 | " asynchronously writes a range of bytes from the given offset source \n" | |
919 | " from multiple buffers\n" | |
920 | "\n" | |
921 | " Example:\n" | |
922 | " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" | |
923 | "\n" | |
924 | " Writes into a segment of the currently open file, using a buffer\n" | |
925 | " filled with a set pattern (0xcdcdcdcd).\n" | |
926 | " The write is performed asynchronously and should the aio_flush command \n" | |
927 | " should be used to ensure all outstanding aio requests have been completed\n" | |
928 | " -P, -- use different pattern to fill file\n" | |
929 | " -C, -- report statistics in a machine parsable format\n" | |
930 | " -q, -- quite mode, do not show I/O statistics\n" | |
931 | "\n"); | |
932 | } | |
933 | ||
934 | ||
935 | static int | |
936 | aio_write_f(int argc, char **argv) | |
937 | { | |
cf57298a | 938 | int nr_iov, c; |
95533d5f CH |
939 | int pattern = 0xcd; |
940 | struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx)); | |
941 | BlockDriverAIOCB *acb; | |
942 | ||
943 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { | |
944 | switch (c) { | |
945 | case 'C': | |
946 | ctx->Cflag = 1; | |
947 | break; | |
948 | case 'q': | |
949 | ctx->qflag = 1; | |
950 | break; | |
951 | case 'P': | |
952 | pattern = atoi(optarg); | |
953 | break; | |
954 | default: | |
955 | return command_usage(&aio_write_cmd); | |
956 | } | |
957 | } | |
958 | ||
959 | if (optind > argc - 2) | |
960 | return command_usage(&aio_write_cmd); | |
961 | ||
962 | ctx->offset = cvtnum(argv[optind]); | |
963 | if (ctx->offset < 0) { | |
964 | printf("non-numeric length argument -- %s\n", argv[optind]); | |
965 | return 0; | |
966 | } | |
967 | optind++; | |
968 | ||
969 | if (ctx->offset & 0x1ff) { | |
970 | printf("offset %lld is not sector aligned\n", | |
971 | (long long)ctx->offset); | |
972 | return 0; | |
973 | } | |
974 | ||
95533d5f | 975 | nr_iov = argc - optind; |
cf57298a | 976 | ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern); |
95533d5f CH |
977 | |
978 | gettimeofday(&ctx->t1, NULL); | |
979 | acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov, | |
980 | ctx->qiov.size >> 9, aio_write_done, ctx); | |
981 | if (!acb) | |
982 | return -EIO; | |
983 | ||
984 | return 0; | |
985 | } | |
986 | ||
987 | static const cmdinfo_t aio_write_cmd = { | |
988 | .name = "aio_write", | |
989 | .cfunc = aio_write_f, | |
990 | .argmin = 2, | |
991 | .argmax = -1, | |
992 | .args = "[-Cq] [-P pattern ] off len [len..]", | |
993 | .oneline = "asynchronously writes a number of bytes", | |
994 | .help = aio_write_help, | |
995 | }; | |
996 | ||
997 | static int | |
998 | aio_flush_f(int argc, char **argv) | |
999 | { | |
1000 | qemu_aio_flush(); | |
1001 | return 0; | |
1002 | } | |
1003 | ||
1004 | static const cmdinfo_t aio_flush_cmd = { | |
1005 | .name = "aio_flush", | |
1006 | .cfunc = aio_flush_f, | |
1007 | .oneline = "completes all outstanding aio requets" | |
1008 | }; | |
1009 | ||
e3aff4f6 AL |
1010 | static int |
1011 | flush_f(int argc, char **argv) | |
1012 | { | |
1013 | bdrv_flush(bs); | |
1014 | return 0; | |
1015 | } | |
1016 | ||
1017 | static const cmdinfo_t flush_cmd = { | |
1018 | .name = "flush", | |
1019 | .altname = "f", | |
1020 | .cfunc = flush_f, | |
1021 | .oneline = "flush all in-core file state to disk", | |
1022 | }; | |
1023 | ||
1024 | static int | |
1025 | truncate_f(int argc, char **argv) | |
1026 | { | |
1027 | int64_t offset; | |
1028 | int ret; | |
1029 | ||
1030 | offset = cvtnum(argv[1]); | |
1031 | if (offset < 0) { | |
1032 | printf("non-numeric truncate argument -- %s\n", argv[1]); | |
1033 | return 0; | |
1034 | } | |
1035 | ||
1036 | ret = bdrv_truncate(bs, offset); | |
1037 | if (ret < 0) { | |
1038 | printf("truncate: %s", strerror(ret)); | |
1039 | return 0; | |
1040 | } | |
1041 | ||
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | static const cmdinfo_t truncate_cmd = { | |
1046 | .name = "truncate", | |
1047 | .altname = "t", | |
1048 | .cfunc = truncate_f, | |
1049 | .argmin = 1, | |
1050 | .argmax = 1, | |
1051 | .args = "off", | |
1052 | .oneline = "truncates the current file at the given offset", | |
1053 | }; | |
1054 | ||
1055 | static int | |
1056 | length_f(int argc, char **argv) | |
1057 | { | |
1058 | int64_t size; | |
1059 | char s1[64]; | |
1060 | ||
1061 | size = bdrv_getlength(bs); | |
1062 | if (size < 0) { | |
1063 | printf("getlength: %s", strerror(size)); | |
1064 | return 0; | |
1065 | } | |
1066 | ||
1067 | cvtstr(size, s1, sizeof(s1)); | |
1068 | printf("%s\n", s1); | |
1069 | return 0; | |
1070 | } | |
1071 | ||
1072 | ||
1073 | static const cmdinfo_t length_cmd = { | |
1074 | .name = "length", | |
1075 | .altname = "l", | |
1076 | .cfunc = length_f, | |
1077 | .oneline = "gets the length of the current file", | |
1078 | }; | |
1079 | ||
1080 | ||
1081 | static int | |
1082 | info_f(int argc, char **argv) | |
1083 | { | |
1084 | BlockDriverInfo bdi; | |
1085 | char s1[64], s2[64]; | |
1086 | int ret; | |
1087 | ||
1088 | if (bs->drv && bs->drv->format_name) | |
1089 | printf("format name: %s\n", bs->drv->format_name); | |
1090 | if (bs->drv && bs->drv->protocol_name) | |
1091 | printf("format name: %s\n", bs->drv->protocol_name); | |
1092 | ||
1093 | ret = bdrv_get_info(bs, &bdi); | |
1094 | if (ret) | |
1095 | return 0; | |
1096 | ||
1097 | cvtstr(bdi.cluster_size, s1, sizeof(s1)); | |
1098 | cvtstr(bdi.vm_state_offset, s2, sizeof(s2)); | |
1099 | ||
1100 | printf("cluster size: %s\n", s1); | |
1101 | printf("vm state offset: %s\n", s2); | |
1102 | ||
1103 | return 0; | |
1104 | } | |
1105 | ||
1106 | ||
1107 | ||
1108 | static const cmdinfo_t info_cmd = { | |
1109 | .name = "info", | |
1110 | .altname = "i", | |
1111 | .cfunc = info_f, | |
1112 | .oneline = "prints information about the current file", | |
1113 | }; | |
1114 | ||
1115 | static int | |
1116 | alloc_f(int argc, char **argv) | |
1117 | { | |
1118 | int64_t offset; | |
1119 | int nb_sectors; | |
1120 | char s1[64]; | |
1121 | int num; | |
1122 | int ret; | |
838ab728 | 1123 | const char *retstr; |
e3aff4f6 AL |
1124 | |
1125 | offset = cvtnum(argv[1]); | |
1126 | if (offset & 0x1ff) { | |
1127 | printf("offset %lld is not sector aligned\n", | |
1128 | (long long)offset); | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | if (argc == 3) | |
1133 | nb_sectors = cvtnum(argv[2]); | |
1134 | else | |
1135 | nb_sectors = 1; | |
1136 | ||
1137 | ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num); | |
e3aff4f6 AL |
1138 | |
1139 | cvtstr(offset, s1, sizeof(s1)); | |
1140 | ||
838ab728 | 1141 | retstr = ret ? "allocated" : "not allocated"; |
e3aff4f6 | 1142 | if (nb_sectors == 1) |
838ab728 | 1143 | printf("sector %s at offset %s\n", retstr, s1); |
e3aff4f6 | 1144 | else |
838ab728 AL |
1145 | printf("%d/%d sectors %s at offset %s\n", |
1146 | num, nb_sectors, retstr, s1); | |
e3aff4f6 AL |
1147 | return 0; |
1148 | } | |
1149 | ||
1150 | static const cmdinfo_t alloc_cmd = { | |
1151 | .name = "alloc", | |
1152 | .altname = "a", | |
1153 | .argmin = 1, | |
1154 | .argmax = 2, | |
1155 | .cfunc = alloc_f, | |
1156 | .args = "off [sectors]", | |
1157 | .oneline = "checks if a sector is present in the file", | |
1158 | }; | |
1159 | ||
1160 | static int | |
1161 | close_f(int argc, char **argv) | |
1162 | { | |
1163 | bdrv_close(bs); | |
1164 | bs = NULL; | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | static const cmdinfo_t close_cmd = { | |
1169 | .name = "close", | |
1170 | .altname = "c", | |
1171 | .cfunc = close_f, | |
1172 | .oneline = "close the current open file", | |
1173 | }; | |
1174 | ||
9c4bab26 | 1175 | static int openfile(char *name, int flags, int growable) |
e3aff4f6 AL |
1176 | { |
1177 | if (bs) { | |
1178 | fprintf(stderr, "file open already, try 'help close'\n"); | |
1179 | return 1; | |
1180 | } | |
1181 | ||
1182 | bs = bdrv_new("hda"); | |
1183 | if (!bs) | |
1184 | return 1; | |
1185 | ||
1186 | if (bdrv_open(bs, name, flags) == -1) { | |
1187 | fprintf(stderr, "%s: can't open device %s\n", progname, name); | |
1188 | bs = NULL; | |
1189 | return 1; | |
1190 | } | |
1191 | ||
9c4bab26 CH |
1192 | |
1193 | if (growable) { | |
1194 | if (!bs->drv || !bs->drv->protocol_name) { | |
1195 | fprintf(stderr, | |
1196 | "%s: only protocols can be opened growable\n", | |
1197 | progname); | |
1198 | return 1; | |
1199 | } | |
1200 | bs->growable = 1; | |
1201 | } | |
1202 | ||
e3aff4f6 AL |
1203 | return 0; |
1204 | } | |
1205 | ||
1206 | static void | |
1207 | open_help(void) | |
1208 | { | |
1209 | printf( | |
1210 | "\n" | |
1211 | " opens a new file in the requested mode\n" | |
1212 | "\n" | |
1213 | " Example:\n" | |
1214 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" | |
1215 | "\n" | |
1216 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" | |
1217 | " -C, -- create new file if it doesn't exist\n" | |
1218 | " -r, -- open file read-only\n" | |
1219 | " -s, -- use snapshot file\n" | |
1220 | " -n, -- disable host cache\n" | |
9c4bab26 | 1221 | " -g, -- allow file to grow (only applies to protocols)" |
e3aff4f6 AL |
1222 | "\n"); |
1223 | } | |
1224 | ||
1225 | static const cmdinfo_t open_cmd; | |
1226 | ||
1227 | static int | |
1228 | open_f(int argc, char **argv) | |
1229 | { | |
1230 | int flags = 0; | |
1231 | int readonly = 0; | |
9c4bab26 | 1232 | int growable = 0; |
e3aff4f6 AL |
1233 | int c; |
1234 | ||
9c4bab26 | 1235 | while ((c = getopt(argc, argv, "snCrg")) != EOF) { |
e3aff4f6 AL |
1236 | switch (c) { |
1237 | case 's': | |
1238 | flags |= BDRV_O_SNAPSHOT; | |
1239 | break; | |
1240 | case 'n': | |
1241 | flags |= BDRV_O_NOCACHE; | |
1242 | break; | |
1243 | case 'C': | |
1244 | flags |= BDRV_O_CREAT; | |
1245 | break; | |
1246 | case 'r': | |
1247 | readonly = 1; | |
1248 | break; | |
9c4bab26 CH |
1249 | case 'g': |
1250 | growable = 1; | |
1251 | break; | |
e3aff4f6 AL |
1252 | default: |
1253 | return command_usage(&open_cmd); | |
1254 | } | |
1255 | } | |
1256 | ||
1257 | if (readonly) | |
1258 | flags |= BDRV_O_RDONLY; | |
1259 | else | |
1260 | flags |= BDRV_O_RDWR; | |
1261 | ||
1262 | if (optind != argc - 1) | |
1263 | return command_usage(&open_cmd); | |
1264 | ||
9c4bab26 | 1265 | return openfile(argv[optind], flags, growable); |
e3aff4f6 AL |
1266 | } |
1267 | ||
1268 | static const cmdinfo_t open_cmd = { | |
1269 | .name = "open", | |
1270 | .altname = "o", | |
1271 | .cfunc = open_f, | |
1272 | .argmin = 1, | |
1273 | .argmax = -1, | |
1274 | .flags = CMD_NOFILE_OK, | |
1275 | .args = "[-Crsn] [path]", | |
1276 | .oneline = "open the file specified by path", | |
1277 | .help = open_help, | |
1278 | }; | |
1279 | ||
1280 | static int | |
1281 | init_args_command( | |
1282 | int index) | |
1283 | { | |
1284 | /* only one device allowed so far */ | |
1285 | if (index >= 1) | |
1286 | return 0; | |
1287 | return ++index; | |
1288 | } | |
1289 | ||
1290 | static int | |
1291 | init_check_command( | |
1292 | const cmdinfo_t *ct) | |
1293 | { | |
1294 | if (ct->flags & CMD_FLAG_GLOBAL) | |
1295 | return 1; | |
1296 | if (!(ct->flags & CMD_NOFILE_OK) && !bs) { | |
1297 | fprintf(stderr, "no file open, try 'help open'\n"); | |
1298 | return 0; | |
1299 | } | |
1300 | return 1; | |
1301 | } | |
1302 | ||
1303 | static void usage(const char *name) | |
1304 | { | |
1305 | printf( | |
1306 | "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n" | |
84844a20 | 1307 | "QEMU Disk exerciser\n" |
e3aff4f6 AL |
1308 | "\n" |
1309 | " -C, --create create new file if it doesn't exist\n" | |
1310 | " -c, --cmd command to execute\n" | |
1311 | " -r, --read-only export read-only\n" | |
1312 | " -s, --snapshot use snapshot file\n" | |
1313 | " -n, --nocache disable host cache\n" | |
1314 | " -m, --misalign misalign allocations for O_DIRECT\n" | |
1315 | " -h, --help display this help and exit\n" | |
1316 | " -V, --version output version information and exit\n" | |
1317 | "\n", | |
1318 | name); | |
1319 | } | |
1320 | ||
1321 | ||
1322 | int main(int argc, char **argv) | |
1323 | { | |
1324 | int readonly = 0; | |
9c4bab26 CH |
1325 | int growable = 0; |
1326 | const char *sopt = "hVc:Crsnmg"; | |
e3aff4f6 AL |
1327 | struct option lopt[] = { |
1328 | { "help", 0, 0, 'h' }, | |
1329 | { "version", 0, 0, 'V' }, | |
1330 | { "offset", 1, 0, 'o' }, | |
1331 | { "cmd", 1, 0, 'c' }, | |
1332 | { "create", 0, 0, 'C' }, | |
1333 | { "read-only", 0, 0, 'r' }, | |
1334 | { "snapshot", 0, 0, 's' }, | |
1335 | { "nocache", 0, 0, 'n' }, | |
1336 | { "misalign", 0, 0, 'm' }, | |
9c4bab26 | 1337 | { "growable", 0, 0, 'g' }, |
e3aff4f6 AL |
1338 | { NULL, 0, 0, 0 } |
1339 | }; | |
1340 | int c; | |
1341 | int opt_index = 0; | |
1342 | int flags = 0; | |
1343 | ||
1344 | progname = basename(argv[0]); | |
1345 | ||
1346 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { | |
1347 | switch (c) { | |
1348 | case 's': | |
1349 | flags |= BDRV_O_SNAPSHOT; | |
1350 | break; | |
1351 | case 'n': | |
1352 | flags |= BDRV_O_NOCACHE; | |
1353 | break; | |
1354 | case 'c': | |
1355 | add_user_command(optarg); | |
1356 | break; | |
1357 | case 'C': | |
1358 | flags |= BDRV_O_CREAT; | |
1359 | break; | |
1360 | case 'r': | |
1361 | readonly = 1; | |
1362 | break; | |
1363 | case 'm': | |
1364 | misalign = 1; | |
1365 | break; | |
9c4bab26 CH |
1366 | case 'g': |
1367 | growable = 1; | |
1368 | break; | |
e3aff4f6 AL |
1369 | case 'V': |
1370 | printf("%s version %s\n", progname, VERSION); | |
1371 | exit(0); | |
1372 | case 'h': | |
1373 | usage(progname); | |
1374 | exit(0); | |
1375 | default: | |
1376 | usage(progname); | |
1377 | exit(1); | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | if ((argc - optind) > 1) { | |
1382 | usage(progname); | |
1383 | exit(1); | |
1384 | } | |
1385 | ||
1386 | bdrv_init(); | |
1387 | ||
1388 | /* initialize commands */ | |
1389 | quit_init(); | |
1390 | help_init(); | |
1391 | add_command(&open_cmd); | |
1392 | add_command(&close_cmd); | |
1393 | add_command(&read_cmd); | |
1394 | add_command(&readv_cmd); | |
1395 | add_command(&write_cmd); | |
1396 | add_command(&writev_cmd); | |
95533d5f CH |
1397 | add_command(&aio_read_cmd); |
1398 | add_command(&aio_write_cmd); | |
1399 | add_command(&aio_flush_cmd); | |
e3aff4f6 AL |
1400 | add_command(&flush_cmd); |
1401 | add_command(&truncate_cmd); | |
1402 | add_command(&length_cmd); | |
1403 | add_command(&info_cmd); | |
1404 | add_command(&alloc_cmd); | |
1405 | ||
1406 | add_args_command(init_args_command); | |
1407 | add_check_command(init_check_command); | |
1408 | ||
1409 | /* open the device */ | |
1410 | if (readonly) | |
1411 | flags |= BDRV_O_RDONLY; | |
1412 | else | |
1413 | flags |= BDRV_O_RDWR; | |
1414 | ||
1415 | if ((argc - optind) == 1) | |
9c4bab26 | 1416 | openfile(argv[optind], flags, growable); |
e3aff4f6 AL |
1417 | command_loop(); |
1418 | ||
95533d5f CH |
1419 | /* |
1420 | * Make sure all outstanding requests get flushed the program exits. | |
1421 | */ | |
1422 | qemu_aio_flush(); | |
1423 | ||
e3aff4f6 AL |
1424 | if (bs) |
1425 | bdrv_close(bs); | |
1426 | return 0; | |
1427 | } |