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