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