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