root / qemu-io-cmds.c @ 216db403
History | View | Annotate | Download (54.3 kB)
1 | 797ac58c | Kevin Wolf | /*
|
---|---|---|---|
2 | 797ac58c | Kevin Wolf | * Command line utility to exercise the QEMU I/O path.
|
3 | 797ac58c | Kevin Wolf | *
|
4 | 797ac58c | Kevin Wolf | * Copyright (C) 2009 Red Hat, Inc.
|
5 | 797ac58c | Kevin Wolf | * Copyright (c) 2003-2005 Silicon Graphics, Inc.
|
6 | 797ac58c | Kevin Wolf | *
|
7 | 797ac58c | Kevin Wolf | * This work is licensed under the terms of the GNU GPL, version 2 or later.
|
8 | 797ac58c | Kevin Wolf | * See the COPYING file in the top-level directory.
|
9 | 797ac58c | Kevin Wolf | */
|
10 | 797ac58c | Kevin Wolf | |
11 | 3d21994f | Kevin Wolf | #include "qemu-io.h" |
12 | 797ac58c | Kevin Wolf | #include "block/block_int.h" |
13 | a8d8ecb7 | Max Reitz | #include "block/qapi.h" |
14 | 6a1751b7 | Alex Bligh | #include "qemu/main-loop.h" |
15 | cd33d02a | Kevin Wolf | #include "qemu/timer.h" |
16 | 797ac58c | Kevin Wolf | |
17 | 797ac58c | Kevin Wolf | #define CMD_NOFILE_OK 0x01 |
18 | 797ac58c | Kevin Wolf | |
19 | 797ac58c | Kevin Wolf | int qemuio_misalign;
|
20 | 797ac58c | Kevin Wolf | |
21 | c2cdf5c5 | Kevin Wolf | static cmdinfo_t *cmdtab;
|
22 | c2cdf5c5 | Kevin Wolf | static int ncmds; |
23 | c2cdf5c5 | Kevin Wolf | |
24 | c2cdf5c5 | Kevin Wolf | static int compare_cmdname(const void *a, const void *b) |
25 | c2cdf5c5 | Kevin Wolf | { |
26 | c2cdf5c5 | Kevin Wolf | return strcmp(((const cmdinfo_t *)a)->name, |
27 | c2cdf5c5 | Kevin Wolf | ((const cmdinfo_t *)b)->name);
|
28 | c2cdf5c5 | Kevin Wolf | } |
29 | c2cdf5c5 | Kevin Wolf | |
30 | c2cdf5c5 | Kevin Wolf | void qemuio_add_command(const cmdinfo_t *ci) |
31 | c2cdf5c5 | Kevin Wolf | { |
32 | c2cdf5c5 | Kevin Wolf | cmdtab = g_realloc(cmdtab, ++ncmds * sizeof(*cmdtab));
|
33 | c2cdf5c5 | Kevin Wolf | cmdtab[ncmds - 1] = *ci;
|
34 | c2cdf5c5 | Kevin Wolf | qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
|
35 | c2cdf5c5 | Kevin Wolf | } |
36 | c2cdf5c5 | Kevin Wolf | |
37 | c2cdf5c5 | Kevin Wolf | int qemuio_command_usage(const cmdinfo_t *ci) |
38 | c2cdf5c5 | Kevin Wolf | { |
39 | c2cdf5c5 | Kevin Wolf | printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
|
40 | c2cdf5c5 | Kevin Wolf | return 0; |
41 | c2cdf5c5 | Kevin Wolf | } |
42 | c2cdf5c5 | Kevin Wolf | |
43 | c2cdf5c5 | Kevin Wolf | static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct) |
44 | c2cdf5c5 | Kevin Wolf | { |
45 | c2cdf5c5 | Kevin Wolf | if (ct->flags & CMD_FLAG_GLOBAL) {
|
46 | c2cdf5c5 | Kevin Wolf | return 1; |
47 | c2cdf5c5 | Kevin Wolf | } |
48 | c2cdf5c5 | Kevin Wolf | if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
|
49 | c2cdf5c5 | Kevin Wolf | fprintf(stderr, "no file open, try 'help open'\n");
|
50 | c2cdf5c5 | Kevin Wolf | return 0; |
51 | c2cdf5c5 | Kevin Wolf | } |
52 | c2cdf5c5 | Kevin Wolf | return 1; |
53 | c2cdf5c5 | Kevin Wolf | } |
54 | c2cdf5c5 | Kevin Wolf | |
55 | 3d21994f | Kevin Wolf | static int command(BlockDriverState *bs, const cmdinfo_t *ct, int argc, |
56 | 3d21994f | Kevin Wolf | char **argv)
|
57 | c2cdf5c5 | Kevin Wolf | { |
58 | c2cdf5c5 | Kevin Wolf | char *cmd = argv[0]; |
59 | c2cdf5c5 | Kevin Wolf | |
60 | 3d21994f | Kevin Wolf | if (!init_check_command(bs, ct)) {
|
61 | c2cdf5c5 | Kevin Wolf | return 0; |
62 | c2cdf5c5 | Kevin Wolf | } |
63 | c2cdf5c5 | Kevin Wolf | |
64 | c2cdf5c5 | Kevin Wolf | if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) { |
65 | c2cdf5c5 | Kevin Wolf | if (ct->argmax == -1) { |
66 | c2cdf5c5 | Kevin Wolf | fprintf(stderr, |
67 | c2cdf5c5 | Kevin Wolf | "bad argument count %d to %s, expected at least %d arguments\n",
|
68 | c2cdf5c5 | Kevin Wolf | argc-1, cmd, ct->argmin);
|
69 | c2cdf5c5 | Kevin Wolf | } else if (ct->argmin == ct->argmax) { |
70 | c2cdf5c5 | Kevin Wolf | fprintf(stderr, |
71 | c2cdf5c5 | Kevin Wolf | "bad argument count %d to %s, expected %d arguments\n",
|
72 | c2cdf5c5 | Kevin Wolf | argc-1, cmd, ct->argmin);
|
73 | c2cdf5c5 | Kevin Wolf | } else {
|
74 | c2cdf5c5 | Kevin Wolf | fprintf(stderr, |
75 | c2cdf5c5 | Kevin Wolf | "bad argument count %d to %s, expected between %d and %d arguments\n",
|
76 | c2cdf5c5 | Kevin Wolf | argc-1, cmd, ct->argmin, ct->argmax);
|
77 | c2cdf5c5 | Kevin Wolf | } |
78 | c2cdf5c5 | Kevin Wolf | return 0; |
79 | c2cdf5c5 | Kevin Wolf | } |
80 | c2cdf5c5 | Kevin Wolf | optind = 0;
|
81 | 3d21994f | Kevin Wolf | return ct->cfunc(bs, argc, argv);
|
82 | c2cdf5c5 | Kevin Wolf | } |
83 | c2cdf5c5 | Kevin Wolf | |
84 | c2cdf5c5 | Kevin Wolf | static const cmdinfo_t *find_command(const char *cmd) |
85 | c2cdf5c5 | Kevin Wolf | { |
86 | c2cdf5c5 | Kevin Wolf | cmdinfo_t *ct; |
87 | c2cdf5c5 | Kevin Wolf | |
88 | c2cdf5c5 | Kevin Wolf | for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
|
89 | c2cdf5c5 | Kevin Wolf | if (strcmp(ct->name, cmd) == 0 || |
90 | c2cdf5c5 | Kevin Wolf | (ct->altname && strcmp(ct->altname, cmd) == 0))
|
91 | c2cdf5c5 | Kevin Wolf | { |
92 | c2cdf5c5 | Kevin Wolf | return (const cmdinfo_t *)ct; |
93 | c2cdf5c5 | Kevin Wolf | } |
94 | c2cdf5c5 | Kevin Wolf | } |
95 | c2cdf5c5 | Kevin Wolf | return NULL; |
96 | c2cdf5c5 | Kevin Wolf | } |
97 | c2cdf5c5 | Kevin Wolf | |
98 | 4694020d | Stefan Hajnoczi | /* Invoke fn() for commands with a matching prefix */
|
99 | 4694020d | Stefan Hajnoczi | void qemuio_complete_command(const char *input, |
100 | 4694020d | Stefan Hajnoczi | void (*fn)(const char *cmd, void *opaque), |
101 | 4694020d | Stefan Hajnoczi | void *opaque)
|
102 | 4694020d | Stefan Hajnoczi | { |
103 | 4694020d | Stefan Hajnoczi | cmdinfo_t *ct; |
104 | 4694020d | Stefan Hajnoczi | size_t input_len = strlen(input); |
105 | 4694020d | Stefan Hajnoczi | |
106 | 4694020d | Stefan Hajnoczi | for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
|
107 | 4694020d | Stefan Hajnoczi | if (strncmp(input, ct->name, input_len) == 0) { |
108 | 4694020d | Stefan Hajnoczi | fn(ct->name, opaque); |
109 | 4694020d | Stefan Hajnoczi | } |
110 | 4694020d | Stefan Hajnoczi | } |
111 | 4694020d | Stefan Hajnoczi | } |
112 | 4694020d | Stefan Hajnoczi | |
113 | c2cdf5c5 | Kevin Wolf | static char **breakline(char *input, int *count) |
114 | c2cdf5c5 | Kevin Wolf | { |
115 | c2cdf5c5 | Kevin Wolf | int c = 0; |
116 | c2cdf5c5 | Kevin Wolf | char *p;
|
117 | c2cdf5c5 | Kevin Wolf | char **rval = g_malloc0(sizeof(char *)); |
118 | c2cdf5c5 | Kevin Wolf | char **tmp;
|
119 | c2cdf5c5 | Kevin Wolf | |
120 | c2cdf5c5 | Kevin Wolf | while (rval && (p = qemu_strsep(&input, " ")) != NULL) { |
121 | c2cdf5c5 | Kevin Wolf | if (!*p) {
|
122 | c2cdf5c5 | Kevin Wolf | continue;
|
123 | c2cdf5c5 | Kevin Wolf | } |
124 | c2cdf5c5 | Kevin Wolf | c++; |
125 | c2cdf5c5 | Kevin Wolf | tmp = g_realloc(rval, sizeof(*rval) * (c + 1)); |
126 | c2cdf5c5 | Kevin Wolf | if (!tmp) {
|
127 | c2cdf5c5 | Kevin Wolf | g_free(rval); |
128 | c2cdf5c5 | Kevin Wolf | rval = NULL;
|
129 | c2cdf5c5 | Kevin Wolf | c = 0;
|
130 | c2cdf5c5 | Kevin Wolf | break;
|
131 | c2cdf5c5 | Kevin Wolf | } else {
|
132 | c2cdf5c5 | Kevin Wolf | rval = tmp; |
133 | c2cdf5c5 | Kevin Wolf | } |
134 | c2cdf5c5 | Kevin Wolf | rval[c - 1] = p;
|
135 | c2cdf5c5 | Kevin Wolf | rval[c] = NULL;
|
136 | c2cdf5c5 | Kevin Wolf | } |
137 | c2cdf5c5 | Kevin Wolf | *count = c; |
138 | c2cdf5c5 | Kevin Wolf | return rval;
|
139 | c2cdf5c5 | Kevin Wolf | } |
140 | c2cdf5c5 | Kevin Wolf | |
141 | 797ac58c | Kevin Wolf | static int64_t cvtnum(const char *s) |
142 | 797ac58c | Kevin Wolf | { |
143 | 797ac58c | Kevin Wolf | char *end;
|
144 | 797ac58c | Kevin Wolf | return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
|
145 | 797ac58c | Kevin Wolf | } |
146 | 797ac58c | Kevin Wolf | |
147 | 0b613881 | Kevin Wolf | #define EXABYTES(x) ((long long)(x) << 60) |
148 | 0b613881 | Kevin Wolf | #define PETABYTES(x) ((long long)(x) << 50) |
149 | 0b613881 | Kevin Wolf | #define TERABYTES(x) ((long long)(x) << 40) |
150 | 0b613881 | Kevin Wolf | #define GIGABYTES(x) ((long long)(x) << 30) |
151 | 0b613881 | Kevin Wolf | #define MEGABYTES(x) ((long long)(x) << 20) |
152 | 0b613881 | Kevin Wolf | #define KILOBYTES(x) ((long long)(x) << 10) |
153 | 0b613881 | Kevin Wolf | |
154 | 0b613881 | Kevin Wolf | #define TO_EXABYTES(x) ((x) / EXABYTES(1)) |
155 | 0b613881 | Kevin Wolf | #define TO_PETABYTES(x) ((x) / PETABYTES(1)) |
156 | 0b613881 | Kevin Wolf | #define TO_TERABYTES(x) ((x) / TERABYTES(1)) |
157 | 0b613881 | Kevin Wolf | #define TO_GIGABYTES(x) ((x) / GIGABYTES(1)) |
158 | 0b613881 | Kevin Wolf | #define TO_MEGABYTES(x) ((x) / MEGABYTES(1)) |
159 | 0b613881 | Kevin Wolf | #define TO_KILOBYTES(x) ((x) / KILOBYTES(1)) |
160 | 0b613881 | Kevin Wolf | |
161 | 0b613881 | Kevin Wolf | static void cvtstr(double value, char *str, size_t size) |
162 | 0b613881 | Kevin Wolf | { |
163 | 0b613881 | Kevin Wolf | char *trim;
|
164 | 0b613881 | Kevin Wolf | const char *suffix; |
165 | 0b613881 | Kevin Wolf | |
166 | 0b613881 | Kevin Wolf | if (value >= EXABYTES(1)) { |
167 | 0b613881 | Kevin Wolf | suffix = " EiB";
|
168 | 0b613881 | Kevin Wolf | snprintf(str, size - 4, "%.3f", TO_EXABYTES(value)); |
169 | 0b613881 | Kevin Wolf | } else if (value >= PETABYTES(1)) { |
170 | 0b613881 | Kevin Wolf | suffix = " PiB";
|
171 | 0b613881 | Kevin Wolf | snprintf(str, size - 4, "%.3f", TO_PETABYTES(value)); |
172 | 0b613881 | Kevin Wolf | } else if (value >= TERABYTES(1)) { |
173 | 0b613881 | Kevin Wolf | suffix = " TiB";
|
174 | 0b613881 | Kevin Wolf | snprintf(str, size - 4, "%.3f", TO_TERABYTES(value)); |
175 | 0b613881 | Kevin Wolf | } else if (value >= GIGABYTES(1)) { |
176 | 0b613881 | Kevin Wolf | suffix = " GiB";
|
177 | 0b613881 | Kevin Wolf | snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value)); |
178 | 0b613881 | Kevin Wolf | } else if (value >= MEGABYTES(1)) { |
179 | 0b613881 | Kevin Wolf | suffix = " MiB";
|
180 | 0b613881 | Kevin Wolf | snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value)); |
181 | 0b613881 | Kevin Wolf | } else if (value >= KILOBYTES(1)) { |
182 | 0b613881 | Kevin Wolf | suffix = " KiB";
|
183 | 0b613881 | Kevin Wolf | snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value)); |
184 | 0b613881 | Kevin Wolf | } else {
|
185 | 0b613881 | Kevin Wolf | suffix = " bytes";
|
186 | 0b613881 | Kevin Wolf | snprintf(str, size - 6, "%f", value); |
187 | 0b613881 | Kevin Wolf | } |
188 | 0b613881 | Kevin Wolf | |
189 | 0b613881 | Kevin Wolf | trim = strstr(str, ".000");
|
190 | 0b613881 | Kevin Wolf | if (trim) {
|
191 | 0b613881 | Kevin Wolf | strcpy(trim, suffix); |
192 | 0b613881 | Kevin Wolf | } else {
|
193 | 0b613881 | Kevin Wolf | strcat(str, suffix); |
194 | 0b613881 | Kevin Wolf | } |
195 | 0b613881 | Kevin Wolf | } |
196 | 0b613881 | Kevin Wolf | |
197 | 0b613881 | Kevin Wolf | |
198 | 0b613881 | Kevin Wolf | |
199 | 0b613881 | Kevin Wolf | static struct timeval tsub(struct timeval t1, struct timeval t2) |
200 | 0b613881 | Kevin Wolf | { |
201 | 0b613881 | Kevin Wolf | t1.tv_usec -= t2.tv_usec; |
202 | 0b613881 | Kevin Wolf | if (t1.tv_usec < 0) { |
203 | 0b613881 | Kevin Wolf | t1.tv_usec += 1000000;
|
204 | 0b613881 | Kevin Wolf | t1.tv_sec--; |
205 | 0b613881 | Kevin Wolf | } |
206 | 0b613881 | Kevin Wolf | t1.tv_sec -= t2.tv_sec; |
207 | 0b613881 | Kevin Wolf | return t1;
|
208 | 0b613881 | Kevin Wolf | } |
209 | 0b613881 | Kevin Wolf | |
210 | 0b613881 | Kevin Wolf | static double tdiv(double value, struct timeval tv) |
211 | 0b613881 | Kevin Wolf | { |
212 | 0b613881 | Kevin Wolf | return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0)); |
213 | 0b613881 | Kevin Wolf | } |
214 | 0b613881 | Kevin Wolf | |
215 | 0b613881 | Kevin Wolf | #define HOURS(sec) ((sec) / (60 * 60)) |
216 | 0b613881 | Kevin Wolf | #define MINUTES(sec) (((sec) % (60 * 60)) / 60) |
217 | 0b613881 | Kevin Wolf | #define SECONDS(sec) ((sec) % 60) |
218 | 0b613881 | Kevin Wolf | |
219 | 0b613881 | Kevin Wolf | enum {
|
220 | 0b613881 | Kevin Wolf | DEFAULT_TIME = 0x0,
|
221 | 0b613881 | Kevin Wolf | TERSE_FIXED_TIME = 0x1,
|
222 | 0b613881 | Kevin Wolf | VERBOSE_FIXED_TIME = 0x2,
|
223 | 0b613881 | Kevin Wolf | }; |
224 | 0b613881 | Kevin Wolf | |
225 | 0b613881 | Kevin Wolf | static void timestr(struct timeval *tv, char *ts, size_t size, int format) |
226 | 0b613881 | Kevin Wolf | { |
227 | 0b613881 | Kevin Wolf | double usec = (double)tv->tv_usec / 1000000.0; |
228 | 0b613881 | Kevin Wolf | |
229 | 0b613881 | Kevin Wolf | if (format & TERSE_FIXED_TIME) {
|
230 | 0b613881 | Kevin Wolf | if (!HOURS(tv->tv_sec)) {
|
231 | 0b613881 | Kevin Wolf | snprintf(ts, size, "%u:%02u.%02u",
|
232 | 0b613881 | Kevin Wolf | (unsigned int) MINUTES(tv->tv_sec), |
233 | 0b613881 | Kevin Wolf | (unsigned int) SECONDS(tv->tv_sec), |
234 | 0b613881 | Kevin Wolf | (unsigned int) (usec * 100)); |
235 | 0b613881 | Kevin Wolf | return;
|
236 | 0b613881 | Kevin Wolf | } |
237 | 0b613881 | Kevin Wolf | format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
|
238 | 0b613881 | Kevin Wolf | } |
239 | 0b613881 | Kevin Wolf | |
240 | 0b613881 | Kevin Wolf | if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
|
241 | 0b613881 | Kevin Wolf | snprintf(ts, size, "%u:%02u:%02u.%02u",
|
242 | 0b613881 | Kevin Wolf | (unsigned int) HOURS(tv->tv_sec), |
243 | 0b613881 | Kevin Wolf | (unsigned int) MINUTES(tv->tv_sec), |
244 | 0b613881 | Kevin Wolf | (unsigned int) SECONDS(tv->tv_sec), |
245 | 0b613881 | Kevin Wolf | (unsigned int) (usec * 100)); |
246 | 0b613881 | Kevin Wolf | } else {
|
247 | 0b613881 | Kevin Wolf | snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000)); |
248 | 0b613881 | Kevin Wolf | } |
249 | 0b613881 | Kevin Wolf | } |
250 | 0b613881 | Kevin Wolf | |
251 | 797ac58c | Kevin Wolf | /*
|
252 | 797ac58c | Kevin Wolf | * Parse the pattern argument to various sub-commands.
|
253 | 797ac58c | Kevin Wolf | *
|
254 | 797ac58c | Kevin Wolf | * Because the pattern is used as an argument to memset it must evaluate
|
255 | 797ac58c | Kevin Wolf | * to an unsigned integer that fits into a single byte.
|
256 | 797ac58c | Kevin Wolf | */
|
257 | 797ac58c | Kevin Wolf | static int parse_pattern(const char *arg) |
258 | 797ac58c | Kevin Wolf | { |
259 | 797ac58c | Kevin Wolf | char *endptr = NULL; |
260 | 797ac58c | Kevin Wolf | long pattern;
|
261 | 797ac58c | Kevin Wolf | |
262 | 797ac58c | Kevin Wolf | pattern = strtol(arg, &endptr, 0);
|
263 | 797ac58c | Kevin Wolf | if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') { |
264 | 797ac58c | Kevin Wolf | printf("%s is not a valid pattern byte\n", arg);
|
265 | 797ac58c | Kevin Wolf | return -1; |
266 | 797ac58c | Kevin Wolf | } |
267 | 797ac58c | Kevin Wolf | |
268 | 797ac58c | Kevin Wolf | return pattern;
|
269 | 797ac58c | Kevin Wolf | } |
270 | 797ac58c | Kevin Wolf | |
271 | 797ac58c | Kevin Wolf | /*
|
272 | 797ac58c | Kevin Wolf | * Memory allocation helpers.
|
273 | 797ac58c | Kevin Wolf | *
|
274 | 797ac58c | Kevin Wolf | * Make sure memory is aligned by default, or purposefully misaligned if
|
275 | 797ac58c | Kevin Wolf | * that is specified on the command line.
|
276 | 797ac58c | Kevin Wolf | */
|
277 | 797ac58c | Kevin Wolf | |
278 | 797ac58c | Kevin Wolf | #define MISALIGN_OFFSET 16 |
279 | 797ac58c | Kevin Wolf | static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern) |
280 | 797ac58c | Kevin Wolf | { |
281 | 797ac58c | Kevin Wolf | void *buf;
|
282 | 797ac58c | Kevin Wolf | |
283 | 797ac58c | Kevin Wolf | if (qemuio_misalign) {
|
284 | 797ac58c | Kevin Wolf | len += MISALIGN_OFFSET; |
285 | 797ac58c | Kevin Wolf | } |
286 | 797ac58c | Kevin Wolf | buf = qemu_blockalign(bs, len); |
287 | 797ac58c | Kevin Wolf | memset(buf, pattern, len); |
288 | 797ac58c | Kevin Wolf | if (qemuio_misalign) {
|
289 | 797ac58c | Kevin Wolf | buf += MISALIGN_OFFSET; |
290 | 797ac58c | Kevin Wolf | } |
291 | 797ac58c | Kevin Wolf | return buf;
|
292 | 797ac58c | Kevin Wolf | } |
293 | 797ac58c | Kevin Wolf | |
294 | 797ac58c | Kevin Wolf | static void qemu_io_free(void *p) |
295 | 797ac58c | Kevin Wolf | { |
296 | 797ac58c | Kevin Wolf | if (qemuio_misalign) {
|
297 | 797ac58c | Kevin Wolf | p -= MISALIGN_OFFSET; |
298 | 797ac58c | Kevin Wolf | } |
299 | 797ac58c | Kevin Wolf | qemu_vfree(p); |
300 | 797ac58c | Kevin Wolf | } |
301 | 797ac58c | Kevin Wolf | |
302 | 797ac58c | Kevin Wolf | static void dump_buffer(const void *buffer, int64_t offset, int len) |
303 | 797ac58c | Kevin Wolf | { |
304 | 797ac58c | Kevin Wolf | int i, j;
|
305 | 797ac58c | Kevin Wolf | const uint8_t *p;
|
306 | 797ac58c | Kevin Wolf | |
307 | 797ac58c | Kevin Wolf | for (i = 0, p = buffer; i < len; i += 16) { |
308 | 797ac58c | Kevin Wolf | const uint8_t *s = p;
|
309 | 797ac58c | Kevin Wolf | |
310 | 797ac58c | Kevin Wolf | printf("%08" PRIx64 ": ", offset + i); |
311 | 797ac58c | Kevin Wolf | for (j = 0; j < 16 && i + j < len; j++, p++) { |
312 | 797ac58c | Kevin Wolf | printf("%02x ", *p);
|
313 | 797ac58c | Kevin Wolf | } |
314 | 797ac58c | Kevin Wolf | printf(" ");
|
315 | 797ac58c | Kevin Wolf | for (j = 0; j < 16 && i + j < len; j++, s++) { |
316 | 797ac58c | Kevin Wolf | if (isalnum(*s)) {
|
317 | 797ac58c | Kevin Wolf | printf("%c", *s);
|
318 | 797ac58c | Kevin Wolf | } else {
|
319 | 797ac58c | Kevin Wolf | printf(".");
|
320 | 797ac58c | Kevin Wolf | } |
321 | 797ac58c | Kevin Wolf | } |
322 | 797ac58c | Kevin Wolf | printf("\n");
|
323 | 797ac58c | Kevin Wolf | } |
324 | 797ac58c | Kevin Wolf | } |
325 | 797ac58c | Kevin Wolf | |
326 | 797ac58c | Kevin Wolf | static void print_report(const char *op, struct timeval *t, int64_t offset, |
327 | 797ac58c | Kevin Wolf | int count, int total, int cnt, int Cflag) |
328 | 797ac58c | Kevin Wolf | { |
329 | 797ac58c | Kevin Wolf | char s1[64], s2[64], ts[64]; |
330 | 797ac58c | Kevin Wolf | |
331 | 797ac58c | Kevin Wolf | timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0); |
332 | 797ac58c | Kevin Wolf | if (!Cflag) {
|
333 | 797ac58c | Kevin Wolf | cvtstr((double)total, s1, sizeof(s1)); |
334 | 797ac58c | Kevin Wolf | cvtstr(tdiv((double)total, *t), s2, sizeof(s2)); |
335 | 797ac58c | Kevin Wolf | printf("%s %d/%d bytes at offset %" PRId64 "\n", |
336 | 797ac58c | Kevin Wolf | op, total, count, offset); |
337 | 797ac58c | Kevin Wolf | printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
|
338 | 797ac58c | Kevin Wolf | s1, cnt, ts, s2, tdiv((double)cnt, *t));
|
339 | 797ac58c | Kevin Wolf | } else {/* bytes,ops,time,bytes/sec,ops/sec */ |
340 | 797ac58c | Kevin Wolf | printf("%d,%d,%s,%.3f,%.3f\n",
|
341 | 797ac58c | Kevin Wolf | total, cnt, ts, |
342 | 797ac58c | Kevin Wolf | tdiv((double)total, *t),
|
343 | 797ac58c | Kevin Wolf | tdiv((double)cnt, *t));
|
344 | 797ac58c | Kevin Wolf | } |
345 | 797ac58c | Kevin Wolf | } |
346 | 797ac58c | Kevin Wolf | |
347 | 797ac58c | Kevin Wolf | /*
|
348 | 797ac58c | Kevin Wolf | * Parse multiple length statements for vectored I/O, and construct an I/O
|
349 | 797ac58c | Kevin Wolf | * vector matching it.
|
350 | 797ac58c | Kevin Wolf | */
|
351 | 797ac58c | Kevin Wolf | static void * |
352 | 797ac58c | Kevin Wolf | create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov, |
353 | 797ac58c | Kevin Wolf | int pattern)
|
354 | 797ac58c | Kevin Wolf | { |
355 | 797ac58c | Kevin Wolf | size_t *sizes = g_new0(size_t, nr_iov); |
356 | 797ac58c | Kevin Wolf | size_t count = 0;
|
357 | 797ac58c | Kevin Wolf | void *buf = NULL; |
358 | 797ac58c | Kevin Wolf | void *p;
|
359 | 797ac58c | Kevin Wolf | int i;
|
360 | 797ac58c | Kevin Wolf | |
361 | 797ac58c | Kevin Wolf | for (i = 0; i < nr_iov; i++) { |
362 | 797ac58c | Kevin Wolf | char *arg = argv[i];
|
363 | 797ac58c | Kevin Wolf | int64_t len; |
364 | 797ac58c | Kevin Wolf | |
365 | 797ac58c | Kevin Wolf | len = cvtnum(arg); |
366 | 797ac58c | Kevin Wolf | if (len < 0) { |
367 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", arg);
|
368 | 797ac58c | Kevin Wolf | goto fail;
|
369 | 797ac58c | Kevin Wolf | } |
370 | 797ac58c | Kevin Wolf | |
371 | 797ac58c | Kevin Wolf | /* should be SIZE_T_MAX, but that doesn't exist */
|
372 | 797ac58c | Kevin Wolf | if (len > INT_MAX) {
|
373 | 797ac58c | Kevin Wolf | printf("too large length argument -- %s\n", arg);
|
374 | 797ac58c | Kevin Wolf | goto fail;
|
375 | 797ac58c | Kevin Wolf | } |
376 | 797ac58c | Kevin Wolf | |
377 | 797ac58c | Kevin Wolf | if (len & 0x1ff) { |
378 | 797ac58c | Kevin Wolf | printf("length argument %" PRId64
|
379 | 797ac58c | Kevin Wolf | " is not sector aligned\n", len);
|
380 | 797ac58c | Kevin Wolf | goto fail;
|
381 | 797ac58c | Kevin Wolf | } |
382 | 797ac58c | Kevin Wolf | |
383 | 797ac58c | Kevin Wolf | sizes[i] = len; |
384 | 797ac58c | Kevin Wolf | count += len; |
385 | 797ac58c | Kevin Wolf | } |
386 | 797ac58c | Kevin Wolf | |
387 | 797ac58c | Kevin Wolf | qemu_iovec_init(qiov, nr_iov); |
388 | 797ac58c | Kevin Wolf | |
389 | 797ac58c | Kevin Wolf | buf = p = qemu_io_alloc(bs, count, pattern); |
390 | 797ac58c | Kevin Wolf | |
391 | 797ac58c | Kevin Wolf | for (i = 0; i < nr_iov; i++) { |
392 | 797ac58c | Kevin Wolf | qemu_iovec_add(qiov, p, sizes[i]); |
393 | 797ac58c | Kevin Wolf | p += sizes[i]; |
394 | 797ac58c | Kevin Wolf | } |
395 | 797ac58c | Kevin Wolf | |
396 | 797ac58c | Kevin Wolf | fail:
|
397 | 797ac58c | Kevin Wolf | g_free(sizes); |
398 | 797ac58c | Kevin Wolf | return buf;
|
399 | 797ac58c | Kevin Wolf | } |
400 | 797ac58c | Kevin Wolf | |
401 | 797ac58c | Kevin Wolf | static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count, |
402 | 797ac58c | Kevin Wolf | int *total)
|
403 | 797ac58c | Kevin Wolf | { |
404 | 797ac58c | Kevin Wolf | int ret;
|
405 | 797ac58c | Kevin Wolf | |
406 | 797ac58c | Kevin Wolf | ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
407 | 797ac58c | Kevin Wolf | if (ret < 0) { |
408 | 797ac58c | Kevin Wolf | return ret;
|
409 | 797ac58c | Kevin Wolf | } |
410 | 797ac58c | Kevin Wolf | *total = count; |
411 | 797ac58c | Kevin Wolf | return 1; |
412 | 797ac58c | Kevin Wolf | } |
413 | 797ac58c | Kevin Wolf | |
414 | 797ac58c | Kevin Wolf | static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count, |
415 | 797ac58c | Kevin Wolf | int *total)
|
416 | 797ac58c | Kevin Wolf | { |
417 | 797ac58c | Kevin Wolf | int ret;
|
418 | 797ac58c | Kevin Wolf | |
419 | 797ac58c | Kevin Wolf | ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
420 | 797ac58c | Kevin Wolf | if (ret < 0) { |
421 | 797ac58c | Kevin Wolf | return ret;
|
422 | 797ac58c | Kevin Wolf | } |
423 | 797ac58c | Kevin Wolf | *total = count; |
424 | 797ac58c | Kevin Wolf | return 1; |
425 | 797ac58c | Kevin Wolf | } |
426 | 797ac58c | Kevin Wolf | |
427 | 797ac58c | Kevin Wolf | static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count, |
428 | 797ac58c | Kevin Wolf | int *total)
|
429 | 797ac58c | Kevin Wolf | { |
430 | 797ac58c | Kevin Wolf | *total = bdrv_pread(bs, offset, (uint8_t *)buf, count); |
431 | 797ac58c | Kevin Wolf | if (*total < 0) { |
432 | 797ac58c | Kevin Wolf | return *total;
|
433 | 797ac58c | Kevin Wolf | } |
434 | 797ac58c | Kevin Wolf | return 1; |
435 | 797ac58c | Kevin Wolf | } |
436 | 797ac58c | Kevin Wolf | |
437 | 797ac58c | Kevin Wolf | static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count, |
438 | 797ac58c | Kevin Wolf | int *total)
|
439 | 797ac58c | Kevin Wolf | { |
440 | 797ac58c | Kevin Wolf | *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count); |
441 | 797ac58c | Kevin Wolf | if (*total < 0) { |
442 | 797ac58c | Kevin Wolf | return *total;
|
443 | 797ac58c | Kevin Wolf | } |
444 | 797ac58c | Kevin Wolf | return 1; |
445 | 797ac58c | Kevin Wolf | } |
446 | 797ac58c | Kevin Wolf | |
447 | 797ac58c | Kevin Wolf | typedef struct { |
448 | 797ac58c | Kevin Wolf | BlockDriverState *bs; |
449 | 797ac58c | Kevin Wolf | int64_t offset; |
450 | 797ac58c | Kevin Wolf | int count;
|
451 | 797ac58c | Kevin Wolf | int *total;
|
452 | 797ac58c | Kevin Wolf | int ret;
|
453 | 797ac58c | Kevin Wolf | bool done;
|
454 | 797ac58c | Kevin Wolf | } CoWriteZeroes; |
455 | 797ac58c | Kevin Wolf | |
456 | 797ac58c | Kevin Wolf | static void coroutine_fn co_write_zeroes_entry(void *opaque) |
457 | 797ac58c | Kevin Wolf | { |
458 | 797ac58c | Kevin Wolf | CoWriteZeroes *data = opaque; |
459 | 797ac58c | Kevin Wolf | |
460 | 797ac58c | Kevin Wolf | data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE, |
461 | aa7bfbff | Peter Lieven | data->count / BDRV_SECTOR_SIZE, 0);
|
462 | 797ac58c | Kevin Wolf | data->done = true;
|
463 | 797ac58c | Kevin Wolf | if (data->ret < 0) { |
464 | 797ac58c | Kevin Wolf | *data->total = data->ret; |
465 | 797ac58c | Kevin Wolf | return;
|
466 | 797ac58c | Kevin Wolf | } |
467 | 797ac58c | Kevin Wolf | |
468 | 797ac58c | Kevin Wolf | *data->total = data->count; |
469 | 797ac58c | Kevin Wolf | } |
470 | 797ac58c | Kevin Wolf | |
471 | 797ac58c | Kevin Wolf | static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count, |
472 | 797ac58c | Kevin Wolf | int *total)
|
473 | 797ac58c | Kevin Wolf | { |
474 | 797ac58c | Kevin Wolf | Coroutine *co; |
475 | 797ac58c | Kevin Wolf | CoWriteZeroes data = { |
476 | 797ac58c | Kevin Wolf | .bs = bs, |
477 | 797ac58c | Kevin Wolf | .offset = offset, |
478 | 797ac58c | Kevin Wolf | .count = count, |
479 | 797ac58c | Kevin Wolf | .total = total, |
480 | 797ac58c | Kevin Wolf | .done = false,
|
481 | 797ac58c | Kevin Wolf | }; |
482 | 797ac58c | Kevin Wolf | |
483 | 797ac58c | Kevin Wolf | co = qemu_coroutine_create(co_write_zeroes_entry); |
484 | 797ac58c | Kevin Wolf | qemu_coroutine_enter(co, &data); |
485 | 797ac58c | Kevin Wolf | while (!data.done) {
|
486 | 797ac58c | Kevin Wolf | qemu_aio_wait(); |
487 | 797ac58c | Kevin Wolf | } |
488 | 797ac58c | Kevin Wolf | if (data.ret < 0) { |
489 | 797ac58c | Kevin Wolf | return data.ret;
|
490 | 797ac58c | Kevin Wolf | } else {
|
491 | 797ac58c | Kevin Wolf | return 1; |
492 | 797ac58c | Kevin Wolf | } |
493 | 797ac58c | Kevin Wolf | } |
494 | 797ac58c | Kevin Wolf | |
495 | 797ac58c | Kevin Wolf | static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset, |
496 | 797ac58c | Kevin Wolf | int count, int *total) |
497 | 797ac58c | Kevin Wolf | { |
498 | 797ac58c | Kevin Wolf | int ret;
|
499 | 797ac58c | Kevin Wolf | |
500 | 797ac58c | Kevin Wolf | ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
501 | 797ac58c | Kevin Wolf | if (ret < 0) { |
502 | 797ac58c | Kevin Wolf | return ret;
|
503 | 797ac58c | Kevin Wolf | } |
504 | 797ac58c | Kevin Wolf | *total = count; |
505 | 797ac58c | Kevin Wolf | return 1; |
506 | 797ac58c | Kevin Wolf | } |
507 | 797ac58c | Kevin Wolf | |
508 | 797ac58c | Kevin Wolf | static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset, |
509 | 797ac58c | Kevin Wolf | int count, int *total) |
510 | 797ac58c | Kevin Wolf | { |
511 | 797ac58c | Kevin Wolf | *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count); |
512 | 797ac58c | Kevin Wolf | if (*total < 0) { |
513 | 797ac58c | Kevin Wolf | return *total;
|
514 | 797ac58c | Kevin Wolf | } |
515 | 797ac58c | Kevin Wolf | return 1; |
516 | 797ac58c | Kevin Wolf | } |
517 | 797ac58c | Kevin Wolf | |
518 | 797ac58c | Kevin Wolf | static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset, |
519 | 797ac58c | Kevin Wolf | int count, int *total) |
520 | 797ac58c | Kevin Wolf | { |
521 | 797ac58c | Kevin Wolf | *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count); |
522 | 797ac58c | Kevin Wolf | if (*total < 0) { |
523 | 797ac58c | Kevin Wolf | return *total;
|
524 | 797ac58c | Kevin Wolf | } |
525 | 797ac58c | Kevin Wolf | return 1; |
526 | 797ac58c | Kevin Wolf | } |
527 | 797ac58c | Kevin Wolf | |
528 | 797ac58c | Kevin Wolf | #define NOT_DONE 0x7fffffff |
529 | 797ac58c | Kevin Wolf | static void aio_rw_done(void *opaque, int ret) |
530 | 797ac58c | Kevin Wolf | { |
531 | 797ac58c | Kevin Wolf | *(int *)opaque = ret;
|
532 | 797ac58c | Kevin Wolf | } |
533 | 797ac58c | Kevin Wolf | |
534 | 797ac58c | Kevin Wolf | static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov, |
535 | 797ac58c | Kevin Wolf | int64_t offset, int *total)
|
536 | 797ac58c | Kevin Wolf | { |
537 | 797ac58c | Kevin Wolf | int async_ret = NOT_DONE;
|
538 | 797ac58c | Kevin Wolf | |
539 | 797ac58c | Kevin Wolf | bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9, |
540 | 797ac58c | Kevin Wolf | aio_rw_done, &async_ret); |
541 | 797ac58c | Kevin Wolf | while (async_ret == NOT_DONE) {
|
542 | 797ac58c | Kevin Wolf | main_loop_wait(false);
|
543 | 797ac58c | Kevin Wolf | } |
544 | 797ac58c | Kevin Wolf | |
545 | 797ac58c | Kevin Wolf | *total = qiov->size; |
546 | 797ac58c | Kevin Wolf | return async_ret < 0 ? async_ret : 1; |
547 | 797ac58c | Kevin Wolf | } |
548 | 797ac58c | Kevin Wolf | |
549 | 797ac58c | Kevin Wolf | static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov, |
550 | 797ac58c | Kevin Wolf | int64_t offset, int *total)
|
551 | 797ac58c | Kevin Wolf | { |
552 | 797ac58c | Kevin Wolf | int async_ret = NOT_DONE;
|
553 | 797ac58c | Kevin Wolf | |
554 | 797ac58c | Kevin Wolf | bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9, |
555 | 797ac58c | Kevin Wolf | aio_rw_done, &async_ret); |
556 | 797ac58c | Kevin Wolf | while (async_ret == NOT_DONE) {
|
557 | 797ac58c | Kevin Wolf | main_loop_wait(false);
|
558 | 797ac58c | Kevin Wolf | } |
559 | 797ac58c | Kevin Wolf | |
560 | 797ac58c | Kevin Wolf | *total = qiov->size; |
561 | 797ac58c | Kevin Wolf | return async_ret < 0 ? async_ret : 1; |
562 | 797ac58c | Kevin Wolf | } |
563 | 797ac58c | Kevin Wolf | |
564 | 797ac58c | Kevin Wolf | struct multiwrite_async_ret {
|
565 | 797ac58c | Kevin Wolf | int num_done;
|
566 | 797ac58c | Kevin Wolf | int error;
|
567 | 797ac58c | Kevin Wolf | }; |
568 | 797ac58c | Kevin Wolf | |
569 | 797ac58c | Kevin Wolf | static void multiwrite_cb(void *opaque, int ret) |
570 | 797ac58c | Kevin Wolf | { |
571 | 797ac58c | Kevin Wolf | struct multiwrite_async_ret *async_ret = opaque;
|
572 | 797ac58c | Kevin Wolf | |
573 | 797ac58c | Kevin Wolf | async_ret->num_done++; |
574 | 797ac58c | Kevin Wolf | if (ret < 0) { |
575 | 797ac58c | Kevin Wolf | async_ret->error = ret; |
576 | 797ac58c | Kevin Wolf | } |
577 | 797ac58c | Kevin Wolf | } |
578 | 797ac58c | Kevin Wolf | |
579 | 797ac58c | Kevin Wolf | static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs, |
580 | 797ac58c | Kevin Wolf | int num_reqs, int *total) |
581 | 797ac58c | Kevin Wolf | { |
582 | 797ac58c | Kevin Wolf | int i, ret;
|
583 | 797ac58c | Kevin Wolf | struct multiwrite_async_ret async_ret = {
|
584 | 797ac58c | Kevin Wolf | .num_done = 0,
|
585 | 797ac58c | Kevin Wolf | .error = 0,
|
586 | 797ac58c | Kevin Wolf | }; |
587 | 797ac58c | Kevin Wolf | |
588 | 797ac58c | Kevin Wolf | *total = 0;
|
589 | 797ac58c | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
590 | 797ac58c | Kevin Wolf | reqs[i].cb = multiwrite_cb; |
591 | 797ac58c | Kevin Wolf | reqs[i].opaque = &async_ret; |
592 | 797ac58c | Kevin Wolf | *total += reqs[i].qiov->size; |
593 | 797ac58c | Kevin Wolf | } |
594 | 797ac58c | Kevin Wolf | |
595 | 797ac58c | Kevin Wolf | ret = bdrv_aio_multiwrite(bs, reqs, num_reqs); |
596 | 797ac58c | Kevin Wolf | if (ret < 0) { |
597 | 797ac58c | Kevin Wolf | return ret;
|
598 | 797ac58c | Kevin Wolf | } |
599 | 797ac58c | Kevin Wolf | |
600 | 797ac58c | Kevin Wolf | while (async_ret.num_done < num_reqs) {
|
601 | 797ac58c | Kevin Wolf | main_loop_wait(false);
|
602 | 797ac58c | Kevin Wolf | } |
603 | 797ac58c | Kevin Wolf | |
604 | 797ac58c | Kevin Wolf | return async_ret.error < 0 ? async_ret.error : 1; |
605 | 797ac58c | Kevin Wolf | } |
606 | 797ac58c | Kevin Wolf | |
607 | 797ac58c | Kevin Wolf | static void read_help(void) |
608 | 797ac58c | Kevin Wolf | { |
609 | 797ac58c | Kevin Wolf | printf( |
610 | 797ac58c | Kevin Wolf | "\n"
|
611 | 797ac58c | Kevin Wolf | " reads a range of bytes from the given offset\n"
|
612 | 797ac58c | Kevin Wolf | "\n"
|
613 | 797ac58c | Kevin Wolf | " Example:\n"
|
614 | 797ac58c | Kevin Wolf | " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
|
615 | 797ac58c | Kevin Wolf | "\n"
|
616 | 797ac58c | Kevin Wolf | " Reads a segment of the currently open file, optionally dumping it to the\n"
|
617 | 797ac58c | Kevin Wolf | " standard output stream (with -v option) for subsequent inspection.\n"
|
618 | 797ac58c | Kevin Wolf | " -b, -- read from the VM state rather than the virtual disk\n"
|
619 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
620 | 797ac58c | Kevin Wolf | " -l, -- length for pattern verification (only with -P)\n"
|
621 | 797ac58c | Kevin Wolf | " -p, -- use bdrv_pread to read the file\n"
|
622 | 797ac58c | Kevin Wolf | " -P, -- use a pattern to verify read data\n"
|
623 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
624 | 797ac58c | Kevin Wolf | " -s, -- start offset for pattern verification (only with -P)\n"
|
625 | 797ac58c | Kevin Wolf | " -v, -- dump buffer to standard output\n"
|
626 | 797ac58c | Kevin Wolf | "\n");
|
627 | 797ac58c | Kevin Wolf | } |
628 | 797ac58c | Kevin Wolf | |
629 | 797ac58c | Kevin Wolf | static int read_f(BlockDriverState *bs, int argc, char **argv); |
630 | 797ac58c | Kevin Wolf | |
631 | 797ac58c | Kevin Wolf | static const cmdinfo_t read_cmd = { |
632 | 797ac58c | Kevin Wolf | .name = "read",
|
633 | 797ac58c | Kevin Wolf | .altname = "r",
|
634 | 797ac58c | Kevin Wolf | .cfunc = read_f, |
635 | 797ac58c | Kevin Wolf | .argmin = 2,
|
636 | 797ac58c | Kevin Wolf | .argmax = -1,
|
637 | 797ac58c | Kevin Wolf | .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
|
638 | 797ac58c | Kevin Wolf | .oneline = "reads a number of bytes at a specified offset",
|
639 | 797ac58c | Kevin Wolf | .help = read_help, |
640 | 797ac58c | Kevin Wolf | }; |
641 | 797ac58c | Kevin Wolf | |
642 | 797ac58c | Kevin Wolf | static int read_f(BlockDriverState *bs, int argc, char **argv) |
643 | 797ac58c | Kevin Wolf | { |
644 | 797ac58c | Kevin Wolf | struct timeval t1, t2;
|
645 | 797ac58c | Kevin Wolf | int Cflag = 0, pflag = 0, qflag = 0, vflag = 0; |
646 | 797ac58c | Kevin Wolf | int Pflag = 0, sflag = 0, lflag = 0, bflag = 0; |
647 | 797ac58c | Kevin Wolf | int c, cnt;
|
648 | 797ac58c | Kevin Wolf | char *buf;
|
649 | 797ac58c | Kevin Wolf | int64_t offset; |
650 | 797ac58c | Kevin Wolf | int count;
|
651 | 797ac58c | Kevin Wolf | /* Some compilers get confused and warn if this is not initialized. */
|
652 | 797ac58c | Kevin Wolf | int total = 0; |
653 | 797ac58c | Kevin Wolf | int pattern = 0, pattern_offset = 0, pattern_count = 0; |
654 | 797ac58c | Kevin Wolf | |
655 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) { |
656 | 797ac58c | Kevin Wolf | switch (c) {
|
657 | 797ac58c | Kevin Wolf | case 'b': |
658 | 797ac58c | Kevin Wolf | bflag = 1;
|
659 | 797ac58c | Kevin Wolf | break;
|
660 | 797ac58c | Kevin Wolf | case 'C': |
661 | 797ac58c | Kevin Wolf | Cflag = 1;
|
662 | 797ac58c | Kevin Wolf | break;
|
663 | 797ac58c | Kevin Wolf | case 'l': |
664 | 797ac58c | Kevin Wolf | lflag = 1;
|
665 | 797ac58c | Kevin Wolf | pattern_count = cvtnum(optarg); |
666 | 797ac58c | Kevin Wolf | if (pattern_count < 0) { |
667 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", optarg);
|
668 | 797ac58c | Kevin Wolf | return 0; |
669 | 797ac58c | Kevin Wolf | } |
670 | 797ac58c | Kevin Wolf | break;
|
671 | 797ac58c | Kevin Wolf | case 'p': |
672 | 797ac58c | Kevin Wolf | pflag = 1;
|
673 | 797ac58c | Kevin Wolf | break;
|
674 | 797ac58c | Kevin Wolf | case 'P': |
675 | 797ac58c | Kevin Wolf | Pflag = 1;
|
676 | 797ac58c | Kevin Wolf | pattern = parse_pattern(optarg); |
677 | 797ac58c | Kevin Wolf | if (pattern < 0) { |
678 | 797ac58c | Kevin Wolf | return 0; |
679 | 797ac58c | Kevin Wolf | } |
680 | 797ac58c | Kevin Wolf | break;
|
681 | 797ac58c | Kevin Wolf | case 'q': |
682 | 797ac58c | Kevin Wolf | qflag = 1;
|
683 | 797ac58c | Kevin Wolf | break;
|
684 | 797ac58c | Kevin Wolf | case 's': |
685 | 797ac58c | Kevin Wolf | sflag = 1;
|
686 | 797ac58c | Kevin Wolf | pattern_offset = cvtnum(optarg); |
687 | 797ac58c | Kevin Wolf | if (pattern_offset < 0) { |
688 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", optarg);
|
689 | 797ac58c | Kevin Wolf | return 0; |
690 | 797ac58c | Kevin Wolf | } |
691 | 797ac58c | Kevin Wolf | break;
|
692 | 797ac58c | Kevin Wolf | case 'v': |
693 | 797ac58c | Kevin Wolf | vflag = 1;
|
694 | 797ac58c | Kevin Wolf | break;
|
695 | 797ac58c | Kevin Wolf | default:
|
696 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&read_cmd);
|
697 | 797ac58c | Kevin Wolf | } |
698 | 797ac58c | Kevin Wolf | } |
699 | 797ac58c | Kevin Wolf | |
700 | 797ac58c | Kevin Wolf | if (optind != argc - 2) { |
701 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&read_cmd);
|
702 | 797ac58c | Kevin Wolf | } |
703 | 797ac58c | Kevin Wolf | |
704 | 797ac58c | Kevin Wolf | if (bflag && pflag) {
|
705 | 797ac58c | Kevin Wolf | printf("-b and -p cannot be specified at the same time\n");
|
706 | 797ac58c | Kevin Wolf | return 0; |
707 | 797ac58c | Kevin Wolf | } |
708 | 797ac58c | Kevin Wolf | |
709 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[optind]); |
710 | 797ac58c | Kevin Wolf | if (offset < 0) { |
711 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
712 | 797ac58c | Kevin Wolf | return 0; |
713 | 797ac58c | Kevin Wolf | } |
714 | 797ac58c | Kevin Wolf | |
715 | 797ac58c | Kevin Wolf | optind++; |
716 | 797ac58c | Kevin Wolf | count = cvtnum(argv[optind]); |
717 | 797ac58c | Kevin Wolf | if (count < 0) { |
718 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
719 | 797ac58c | Kevin Wolf | return 0; |
720 | 797ac58c | Kevin Wolf | } |
721 | 797ac58c | Kevin Wolf | |
722 | 797ac58c | Kevin Wolf | if (!Pflag && (lflag || sflag)) {
|
723 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&read_cmd);
|
724 | 797ac58c | Kevin Wolf | } |
725 | 797ac58c | Kevin Wolf | |
726 | 797ac58c | Kevin Wolf | if (!lflag) {
|
727 | 797ac58c | Kevin Wolf | pattern_count = count - pattern_offset; |
728 | 797ac58c | Kevin Wolf | } |
729 | 797ac58c | Kevin Wolf | |
730 | 797ac58c | Kevin Wolf | if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) { |
731 | 797ac58c | Kevin Wolf | printf("pattern verification range exceeds end of read data\n");
|
732 | 797ac58c | Kevin Wolf | return 0; |
733 | 797ac58c | Kevin Wolf | } |
734 | 797ac58c | Kevin Wolf | |
735 | 797ac58c | Kevin Wolf | if (!pflag) {
|
736 | 797ac58c | Kevin Wolf | if (offset & 0x1ff) { |
737 | 797ac58c | Kevin Wolf | printf("offset %" PRId64 " is not sector aligned\n", |
738 | 797ac58c | Kevin Wolf | offset); |
739 | 797ac58c | Kevin Wolf | return 0; |
740 | 797ac58c | Kevin Wolf | } |
741 | 797ac58c | Kevin Wolf | if (count & 0x1ff) { |
742 | 797ac58c | Kevin Wolf | printf("count %d is not sector aligned\n",
|
743 | 797ac58c | Kevin Wolf | count); |
744 | 797ac58c | Kevin Wolf | return 0; |
745 | 797ac58c | Kevin Wolf | } |
746 | 797ac58c | Kevin Wolf | } |
747 | 797ac58c | Kevin Wolf | |
748 | 797ac58c | Kevin Wolf | buf = qemu_io_alloc(bs, count, 0xab);
|
749 | 797ac58c | Kevin Wolf | |
750 | 797ac58c | Kevin Wolf | gettimeofday(&t1, NULL);
|
751 | 797ac58c | Kevin Wolf | if (pflag) {
|
752 | 797ac58c | Kevin Wolf | cnt = do_pread(bs, buf, offset, count, &total); |
753 | 797ac58c | Kevin Wolf | } else if (bflag) { |
754 | 797ac58c | Kevin Wolf | cnt = do_load_vmstate(bs, buf, offset, count, &total); |
755 | 797ac58c | Kevin Wolf | } else {
|
756 | 797ac58c | Kevin Wolf | cnt = do_read(bs, buf, offset, count, &total); |
757 | 797ac58c | Kevin Wolf | } |
758 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
759 | 797ac58c | Kevin Wolf | |
760 | 797ac58c | Kevin Wolf | if (cnt < 0) { |
761 | 797ac58c | Kevin Wolf | printf("read failed: %s\n", strerror(-cnt));
|
762 | 797ac58c | Kevin Wolf | goto out;
|
763 | 797ac58c | Kevin Wolf | } |
764 | 797ac58c | Kevin Wolf | |
765 | 797ac58c | Kevin Wolf | if (Pflag) {
|
766 | 797ac58c | Kevin Wolf | void *cmp_buf = g_malloc(pattern_count);
|
767 | 797ac58c | Kevin Wolf | memset(cmp_buf, pattern, pattern_count); |
768 | 797ac58c | Kevin Wolf | if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
|
769 | 797ac58c | Kevin Wolf | printf("Pattern verification failed at offset %"
|
770 | 797ac58c | Kevin Wolf | PRId64 ", %d bytes\n",
|
771 | 797ac58c | Kevin Wolf | offset + pattern_offset, pattern_count); |
772 | 797ac58c | Kevin Wolf | } |
773 | 797ac58c | Kevin Wolf | g_free(cmp_buf); |
774 | 797ac58c | Kevin Wolf | } |
775 | 797ac58c | Kevin Wolf | |
776 | 797ac58c | Kevin Wolf | if (qflag) {
|
777 | 797ac58c | Kevin Wolf | goto out;
|
778 | 797ac58c | Kevin Wolf | } |
779 | 797ac58c | Kevin Wolf | |
780 | 797ac58c | Kevin Wolf | if (vflag) {
|
781 | 797ac58c | Kevin Wolf | dump_buffer(buf, offset, count); |
782 | 797ac58c | Kevin Wolf | } |
783 | 797ac58c | Kevin Wolf | |
784 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
785 | 797ac58c | Kevin Wolf | t2 = tsub(t2, t1); |
786 | 797ac58c | Kevin Wolf | print_report("read", &t2, offset, count, total, cnt, Cflag);
|
787 | 797ac58c | Kevin Wolf | |
788 | 797ac58c | Kevin Wolf | out:
|
789 | 797ac58c | Kevin Wolf | qemu_io_free(buf); |
790 | 797ac58c | Kevin Wolf | |
791 | 797ac58c | Kevin Wolf | return 0; |
792 | 797ac58c | Kevin Wolf | } |
793 | 797ac58c | Kevin Wolf | |
794 | 797ac58c | Kevin Wolf | static void readv_help(void) |
795 | 797ac58c | Kevin Wolf | { |
796 | 797ac58c | Kevin Wolf | printf( |
797 | 797ac58c | Kevin Wolf | "\n"
|
798 | 797ac58c | Kevin Wolf | " reads a range of bytes from the given offset into multiple buffers\n"
|
799 | 797ac58c | Kevin Wolf | "\n"
|
800 | 797ac58c | Kevin Wolf | " Example:\n"
|
801 | 797ac58c | Kevin Wolf | " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
|
802 | 797ac58c | Kevin Wolf | "\n"
|
803 | 797ac58c | Kevin Wolf | " Reads a segment of the currently open file, optionally dumping it to the\n"
|
804 | 797ac58c | Kevin Wolf | " standard output stream (with -v option) for subsequent inspection.\n"
|
805 | 797ac58c | Kevin Wolf | " Uses multiple iovec buffers if more than one byte range is specified.\n"
|
806 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
807 | 797ac58c | Kevin Wolf | " -P, -- use a pattern to verify read data\n"
|
808 | 797ac58c | Kevin Wolf | " -v, -- dump buffer to standard output\n"
|
809 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
810 | 797ac58c | Kevin Wolf | "\n");
|
811 | 797ac58c | Kevin Wolf | } |
812 | 797ac58c | Kevin Wolf | |
813 | 797ac58c | Kevin Wolf | static int readv_f(BlockDriverState *bs, int argc, char **argv); |
814 | 797ac58c | Kevin Wolf | |
815 | 797ac58c | Kevin Wolf | static const cmdinfo_t readv_cmd = { |
816 | 797ac58c | Kevin Wolf | .name = "readv",
|
817 | 797ac58c | Kevin Wolf | .cfunc = readv_f, |
818 | 797ac58c | Kevin Wolf | .argmin = 2,
|
819 | 797ac58c | Kevin Wolf | .argmax = -1,
|
820 | 797ac58c | Kevin Wolf | .args = "[-Cqv] [-P pattern ] off len [len..]",
|
821 | 797ac58c | Kevin Wolf | .oneline = "reads a number of bytes at a specified offset",
|
822 | 797ac58c | Kevin Wolf | .help = readv_help, |
823 | 797ac58c | Kevin Wolf | }; |
824 | 797ac58c | Kevin Wolf | |
825 | 797ac58c | Kevin Wolf | static int readv_f(BlockDriverState *bs, int argc, char **argv) |
826 | 797ac58c | Kevin Wolf | { |
827 | 797ac58c | Kevin Wolf | struct timeval t1, t2;
|
828 | 797ac58c | Kevin Wolf | int Cflag = 0, qflag = 0, vflag = 0; |
829 | 797ac58c | Kevin Wolf | int c, cnt;
|
830 | 797ac58c | Kevin Wolf | char *buf;
|
831 | 797ac58c | Kevin Wolf | int64_t offset; |
832 | 797ac58c | Kevin Wolf | /* Some compilers get confused and warn if this is not initialized. */
|
833 | 797ac58c | Kevin Wolf | int total = 0; |
834 | 797ac58c | Kevin Wolf | int nr_iov;
|
835 | 797ac58c | Kevin Wolf | QEMUIOVector qiov; |
836 | 797ac58c | Kevin Wolf | int pattern = 0; |
837 | 797ac58c | Kevin Wolf | int Pflag = 0; |
838 | 797ac58c | Kevin Wolf | |
839 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
840 | 797ac58c | Kevin Wolf | switch (c) {
|
841 | 797ac58c | Kevin Wolf | case 'C': |
842 | 797ac58c | Kevin Wolf | Cflag = 1;
|
843 | 797ac58c | Kevin Wolf | break;
|
844 | 797ac58c | Kevin Wolf | case 'P': |
845 | 797ac58c | Kevin Wolf | Pflag = 1;
|
846 | 797ac58c | Kevin Wolf | pattern = parse_pattern(optarg); |
847 | 797ac58c | Kevin Wolf | if (pattern < 0) { |
848 | 797ac58c | Kevin Wolf | return 0; |
849 | 797ac58c | Kevin Wolf | } |
850 | 797ac58c | Kevin Wolf | break;
|
851 | 797ac58c | Kevin Wolf | case 'q': |
852 | 797ac58c | Kevin Wolf | qflag = 1;
|
853 | 797ac58c | Kevin Wolf | break;
|
854 | 797ac58c | Kevin Wolf | case 'v': |
855 | 797ac58c | Kevin Wolf | vflag = 1;
|
856 | 797ac58c | Kevin Wolf | break;
|
857 | 797ac58c | Kevin Wolf | default:
|
858 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&readv_cmd);
|
859 | 797ac58c | Kevin Wolf | } |
860 | 797ac58c | Kevin Wolf | } |
861 | 797ac58c | Kevin Wolf | |
862 | 797ac58c | Kevin Wolf | if (optind > argc - 2) { |
863 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&readv_cmd);
|
864 | 797ac58c | Kevin Wolf | } |
865 | 797ac58c | Kevin Wolf | |
866 | 797ac58c | Kevin Wolf | |
867 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[optind]); |
868 | 797ac58c | Kevin Wolf | if (offset < 0) { |
869 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
870 | 797ac58c | Kevin Wolf | return 0; |
871 | 797ac58c | Kevin Wolf | } |
872 | 797ac58c | Kevin Wolf | optind++; |
873 | 797ac58c | Kevin Wolf | |
874 | 797ac58c | Kevin Wolf | if (offset & 0x1ff) { |
875 | 797ac58c | Kevin Wolf | printf("offset %" PRId64 " is not sector aligned\n", |
876 | 797ac58c | Kevin Wolf | offset); |
877 | 797ac58c | Kevin Wolf | return 0; |
878 | 797ac58c | Kevin Wolf | } |
879 | 797ac58c | Kevin Wolf | |
880 | 797ac58c | Kevin Wolf | nr_iov = argc - optind; |
881 | 797ac58c | Kevin Wolf | buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
|
882 | 797ac58c | Kevin Wolf | if (buf == NULL) { |
883 | 797ac58c | Kevin Wolf | return 0; |
884 | 797ac58c | Kevin Wolf | } |
885 | 797ac58c | Kevin Wolf | |
886 | 797ac58c | Kevin Wolf | gettimeofday(&t1, NULL);
|
887 | 797ac58c | Kevin Wolf | cnt = do_aio_readv(bs, &qiov, offset, &total); |
888 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
889 | 797ac58c | Kevin Wolf | |
890 | 797ac58c | Kevin Wolf | if (cnt < 0) { |
891 | 797ac58c | Kevin Wolf | printf("readv failed: %s\n", strerror(-cnt));
|
892 | 797ac58c | Kevin Wolf | goto out;
|
893 | 797ac58c | Kevin Wolf | } |
894 | 797ac58c | Kevin Wolf | |
895 | 797ac58c | Kevin Wolf | if (Pflag) {
|
896 | 797ac58c | Kevin Wolf | void *cmp_buf = g_malloc(qiov.size);
|
897 | 797ac58c | Kevin Wolf | memset(cmp_buf, pattern, qiov.size); |
898 | 797ac58c | Kevin Wolf | if (memcmp(buf, cmp_buf, qiov.size)) {
|
899 | 797ac58c | Kevin Wolf | printf("Pattern verification failed at offset %"
|
900 | 797ac58c | Kevin Wolf | PRId64 ", %zd bytes\n", offset, qiov.size);
|
901 | 797ac58c | Kevin Wolf | } |
902 | 797ac58c | Kevin Wolf | g_free(cmp_buf); |
903 | 797ac58c | Kevin Wolf | } |
904 | 797ac58c | Kevin Wolf | |
905 | 797ac58c | Kevin Wolf | if (qflag) {
|
906 | 797ac58c | Kevin Wolf | goto out;
|
907 | 797ac58c | Kevin Wolf | } |
908 | 797ac58c | Kevin Wolf | |
909 | 797ac58c | Kevin Wolf | if (vflag) {
|
910 | 797ac58c | Kevin Wolf | dump_buffer(buf, offset, qiov.size); |
911 | 797ac58c | Kevin Wolf | } |
912 | 797ac58c | Kevin Wolf | |
913 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
914 | 797ac58c | Kevin Wolf | t2 = tsub(t2, t1); |
915 | 797ac58c | Kevin Wolf | print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
|
916 | 797ac58c | Kevin Wolf | |
917 | 797ac58c | Kevin Wolf | out:
|
918 | 797ac58c | Kevin Wolf | qemu_iovec_destroy(&qiov); |
919 | 797ac58c | Kevin Wolf | qemu_io_free(buf); |
920 | 797ac58c | Kevin Wolf | return 0; |
921 | 797ac58c | Kevin Wolf | } |
922 | 797ac58c | Kevin Wolf | |
923 | 797ac58c | Kevin Wolf | static void write_help(void) |
924 | 797ac58c | Kevin Wolf | { |
925 | 797ac58c | Kevin Wolf | printf( |
926 | 797ac58c | Kevin Wolf | "\n"
|
927 | 797ac58c | Kevin Wolf | " writes a range of bytes from the given offset\n"
|
928 | 797ac58c | Kevin Wolf | "\n"
|
929 | 797ac58c | Kevin Wolf | " Example:\n"
|
930 | 797ac58c | Kevin Wolf | " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
|
931 | 797ac58c | Kevin Wolf | "\n"
|
932 | 797ac58c | Kevin Wolf | " Writes into a segment of the currently open file, using a buffer\n"
|
933 | 797ac58c | Kevin Wolf | " filled with a set pattern (0xcdcdcdcd).\n"
|
934 | 797ac58c | Kevin Wolf | " -b, -- write to the VM state rather than the virtual disk\n"
|
935 | 797ac58c | Kevin Wolf | " -c, -- write compressed data with bdrv_write_compressed\n"
|
936 | 797ac58c | Kevin Wolf | " -p, -- use bdrv_pwrite to write the file\n"
|
937 | 797ac58c | Kevin Wolf | " -P, -- use different pattern to fill file\n"
|
938 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
939 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
940 | 797ac58c | Kevin Wolf | " -z, -- write zeroes using bdrv_co_write_zeroes\n"
|
941 | 797ac58c | Kevin Wolf | "\n");
|
942 | 797ac58c | Kevin Wolf | } |
943 | 797ac58c | Kevin Wolf | |
944 | 797ac58c | Kevin Wolf | static int write_f(BlockDriverState *bs, int argc, char **argv); |
945 | 797ac58c | Kevin Wolf | |
946 | 797ac58c | Kevin Wolf | static const cmdinfo_t write_cmd = { |
947 | 797ac58c | Kevin Wolf | .name = "write",
|
948 | 797ac58c | Kevin Wolf | .altname = "w",
|
949 | 797ac58c | Kevin Wolf | .cfunc = write_f, |
950 | 797ac58c | Kevin Wolf | .argmin = 2,
|
951 | 797ac58c | Kevin Wolf | .argmax = -1,
|
952 | 797ac58c | Kevin Wolf | .args = "[-bcCpqz] [-P pattern ] off len",
|
953 | 797ac58c | Kevin Wolf | .oneline = "writes a number of bytes at a specified offset",
|
954 | 797ac58c | Kevin Wolf | .help = write_help, |
955 | 797ac58c | Kevin Wolf | }; |
956 | 797ac58c | Kevin Wolf | |
957 | 797ac58c | Kevin Wolf | static int write_f(BlockDriverState *bs, int argc, char **argv) |
958 | 797ac58c | Kevin Wolf | { |
959 | 797ac58c | Kevin Wolf | struct timeval t1, t2;
|
960 | 797ac58c | Kevin Wolf | int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0; |
961 | 797ac58c | Kevin Wolf | int cflag = 0; |
962 | 797ac58c | Kevin Wolf | int c, cnt;
|
963 | 797ac58c | Kevin Wolf | char *buf = NULL; |
964 | 797ac58c | Kevin Wolf | int64_t offset; |
965 | 797ac58c | Kevin Wolf | int count;
|
966 | 797ac58c | Kevin Wolf | /* Some compilers get confused and warn if this is not initialized. */
|
967 | 797ac58c | Kevin Wolf | int total = 0; |
968 | 797ac58c | Kevin Wolf | int pattern = 0xcd; |
969 | 797ac58c | Kevin Wolf | |
970 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) { |
971 | 797ac58c | Kevin Wolf | switch (c) {
|
972 | 797ac58c | Kevin Wolf | case 'b': |
973 | 797ac58c | Kevin Wolf | bflag = 1;
|
974 | 797ac58c | Kevin Wolf | break;
|
975 | 797ac58c | Kevin Wolf | case 'c': |
976 | 797ac58c | Kevin Wolf | cflag = 1;
|
977 | 797ac58c | Kevin Wolf | break;
|
978 | 797ac58c | Kevin Wolf | case 'C': |
979 | 797ac58c | Kevin Wolf | Cflag = 1;
|
980 | 797ac58c | Kevin Wolf | break;
|
981 | 797ac58c | Kevin Wolf | case 'p': |
982 | 797ac58c | Kevin Wolf | pflag = 1;
|
983 | 797ac58c | Kevin Wolf | break;
|
984 | 797ac58c | Kevin Wolf | case 'P': |
985 | 797ac58c | Kevin Wolf | Pflag = 1;
|
986 | 797ac58c | Kevin Wolf | pattern = parse_pattern(optarg); |
987 | 797ac58c | Kevin Wolf | if (pattern < 0) { |
988 | 797ac58c | Kevin Wolf | return 0; |
989 | 797ac58c | Kevin Wolf | } |
990 | 797ac58c | Kevin Wolf | break;
|
991 | 797ac58c | Kevin Wolf | case 'q': |
992 | 797ac58c | Kevin Wolf | qflag = 1;
|
993 | 797ac58c | Kevin Wolf | break;
|
994 | 797ac58c | Kevin Wolf | case 'z': |
995 | 797ac58c | Kevin Wolf | zflag = 1;
|
996 | 797ac58c | Kevin Wolf | break;
|
997 | 797ac58c | Kevin Wolf | default:
|
998 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&write_cmd);
|
999 | 797ac58c | Kevin Wolf | } |
1000 | 797ac58c | Kevin Wolf | } |
1001 | 797ac58c | Kevin Wolf | |
1002 | 797ac58c | Kevin Wolf | if (optind != argc - 2) { |
1003 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&write_cmd);
|
1004 | 797ac58c | Kevin Wolf | } |
1005 | 797ac58c | Kevin Wolf | |
1006 | 797ac58c | Kevin Wolf | if (bflag + pflag + zflag > 1) { |
1007 | 797ac58c | Kevin Wolf | printf("-b, -p, or -z cannot be specified at the same time\n");
|
1008 | 797ac58c | Kevin Wolf | return 0; |
1009 | 797ac58c | Kevin Wolf | } |
1010 | 797ac58c | Kevin Wolf | |
1011 | 797ac58c | Kevin Wolf | if (zflag && Pflag) {
|
1012 | 797ac58c | Kevin Wolf | printf("-z and -P cannot be specified at the same time\n");
|
1013 | 797ac58c | Kevin Wolf | return 0; |
1014 | 797ac58c | Kevin Wolf | } |
1015 | 797ac58c | Kevin Wolf | |
1016 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[optind]); |
1017 | 797ac58c | Kevin Wolf | if (offset < 0) { |
1018 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
1019 | 797ac58c | Kevin Wolf | return 0; |
1020 | 797ac58c | Kevin Wolf | } |
1021 | 797ac58c | Kevin Wolf | |
1022 | 797ac58c | Kevin Wolf | optind++; |
1023 | 797ac58c | Kevin Wolf | count = cvtnum(argv[optind]); |
1024 | 797ac58c | Kevin Wolf | if (count < 0) { |
1025 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
1026 | 797ac58c | Kevin Wolf | return 0; |
1027 | 797ac58c | Kevin Wolf | } |
1028 | 797ac58c | Kevin Wolf | |
1029 | 797ac58c | Kevin Wolf | if (!pflag) {
|
1030 | 797ac58c | Kevin Wolf | if (offset & 0x1ff) { |
1031 | 797ac58c | Kevin Wolf | printf("offset %" PRId64 " is not sector aligned\n", |
1032 | 797ac58c | Kevin Wolf | offset); |
1033 | 797ac58c | Kevin Wolf | return 0; |
1034 | 797ac58c | Kevin Wolf | } |
1035 | 797ac58c | Kevin Wolf | |
1036 | 797ac58c | Kevin Wolf | if (count & 0x1ff) { |
1037 | 797ac58c | Kevin Wolf | printf("count %d is not sector aligned\n",
|
1038 | 797ac58c | Kevin Wolf | count); |
1039 | 797ac58c | Kevin Wolf | return 0; |
1040 | 797ac58c | Kevin Wolf | } |
1041 | 797ac58c | Kevin Wolf | } |
1042 | 797ac58c | Kevin Wolf | |
1043 | 797ac58c | Kevin Wolf | if (!zflag) {
|
1044 | 797ac58c | Kevin Wolf | buf = qemu_io_alloc(bs, count, pattern); |
1045 | 797ac58c | Kevin Wolf | } |
1046 | 797ac58c | Kevin Wolf | |
1047 | 797ac58c | Kevin Wolf | gettimeofday(&t1, NULL);
|
1048 | 797ac58c | Kevin Wolf | if (pflag) {
|
1049 | 797ac58c | Kevin Wolf | cnt = do_pwrite(bs, buf, offset, count, &total); |
1050 | 797ac58c | Kevin Wolf | } else if (bflag) { |
1051 | 797ac58c | Kevin Wolf | cnt = do_save_vmstate(bs, buf, offset, count, &total); |
1052 | 797ac58c | Kevin Wolf | } else if (zflag) { |
1053 | 797ac58c | Kevin Wolf | cnt = do_co_write_zeroes(bs, offset, count, &total); |
1054 | 797ac58c | Kevin Wolf | } else if (cflag) { |
1055 | 797ac58c | Kevin Wolf | cnt = do_write_compressed(bs, buf, offset, count, &total); |
1056 | 797ac58c | Kevin Wolf | } else {
|
1057 | 797ac58c | Kevin Wolf | cnt = do_write(bs, buf, offset, count, &total); |
1058 | 797ac58c | Kevin Wolf | } |
1059 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
1060 | 797ac58c | Kevin Wolf | |
1061 | 797ac58c | Kevin Wolf | if (cnt < 0) { |
1062 | 797ac58c | Kevin Wolf | printf("write failed: %s\n", strerror(-cnt));
|
1063 | 797ac58c | Kevin Wolf | goto out;
|
1064 | 797ac58c | Kevin Wolf | } |
1065 | 797ac58c | Kevin Wolf | |
1066 | 797ac58c | Kevin Wolf | if (qflag) {
|
1067 | 797ac58c | Kevin Wolf | goto out;
|
1068 | 797ac58c | Kevin Wolf | } |
1069 | 797ac58c | Kevin Wolf | |
1070 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
1071 | 797ac58c | Kevin Wolf | t2 = tsub(t2, t1); |
1072 | 797ac58c | Kevin Wolf | print_report("wrote", &t2, offset, count, total, cnt, Cflag);
|
1073 | 797ac58c | Kevin Wolf | |
1074 | 797ac58c | Kevin Wolf | out:
|
1075 | 797ac58c | Kevin Wolf | if (!zflag) {
|
1076 | 797ac58c | Kevin Wolf | qemu_io_free(buf); |
1077 | 797ac58c | Kevin Wolf | } |
1078 | 797ac58c | Kevin Wolf | |
1079 | 797ac58c | Kevin Wolf | return 0; |
1080 | 797ac58c | Kevin Wolf | } |
1081 | 797ac58c | Kevin Wolf | |
1082 | 797ac58c | Kevin Wolf | static void |
1083 | 797ac58c | Kevin Wolf | writev_help(void)
|
1084 | 797ac58c | Kevin Wolf | { |
1085 | 797ac58c | Kevin Wolf | printf( |
1086 | 797ac58c | Kevin Wolf | "\n"
|
1087 | 797ac58c | Kevin Wolf | " writes a range of bytes from the given offset source from multiple buffers\n"
|
1088 | 797ac58c | Kevin Wolf | "\n"
|
1089 | 797ac58c | Kevin Wolf | " Example:\n"
|
1090 | 797ac58c | Kevin Wolf | " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
|
1091 | 797ac58c | Kevin Wolf | "\n"
|
1092 | 797ac58c | Kevin Wolf | " Writes into a segment of the currently open file, using a buffer\n"
|
1093 | 797ac58c | Kevin Wolf | " filled with a set pattern (0xcdcdcdcd).\n"
|
1094 | 797ac58c | Kevin Wolf | " -P, -- use different pattern to fill file\n"
|
1095 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
1096 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
1097 | 797ac58c | Kevin Wolf | "\n");
|
1098 | 797ac58c | Kevin Wolf | } |
1099 | 797ac58c | Kevin Wolf | |
1100 | 797ac58c | Kevin Wolf | static int writev_f(BlockDriverState *bs, int argc, char **argv); |
1101 | 797ac58c | Kevin Wolf | |
1102 | 797ac58c | Kevin Wolf | static const cmdinfo_t writev_cmd = { |
1103 | 797ac58c | Kevin Wolf | .name = "writev",
|
1104 | 797ac58c | Kevin Wolf | .cfunc = writev_f, |
1105 | 797ac58c | Kevin Wolf | .argmin = 2,
|
1106 | 797ac58c | Kevin Wolf | .argmax = -1,
|
1107 | 797ac58c | Kevin Wolf | .args = "[-Cq] [-P pattern ] off len [len..]",
|
1108 | 797ac58c | Kevin Wolf | .oneline = "writes a number of bytes at a specified offset",
|
1109 | 797ac58c | Kevin Wolf | .help = writev_help, |
1110 | 797ac58c | Kevin Wolf | }; |
1111 | 797ac58c | Kevin Wolf | |
1112 | 797ac58c | Kevin Wolf | static int writev_f(BlockDriverState *bs, int argc, char **argv) |
1113 | 797ac58c | Kevin Wolf | { |
1114 | 797ac58c | Kevin Wolf | struct timeval t1, t2;
|
1115 | 797ac58c | Kevin Wolf | int Cflag = 0, qflag = 0; |
1116 | 797ac58c | Kevin Wolf | int c, cnt;
|
1117 | 797ac58c | Kevin Wolf | char *buf;
|
1118 | 797ac58c | Kevin Wolf | int64_t offset; |
1119 | 797ac58c | Kevin Wolf | /* Some compilers get confused and warn if this is not initialized. */
|
1120 | 797ac58c | Kevin Wolf | int total = 0; |
1121 | 797ac58c | Kevin Wolf | int nr_iov;
|
1122 | 797ac58c | Kevin Wolf | int pattern = 0xcd; |
1123 | 797ac58c | Kevin Wolf | QEMUIOVector qiov; |
1124 | 797ac58c | Kevin Wolf | |
1125 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
1126 | 797ac58c | Kevin Wolf | switch (c) {
|
1127 | 797ac58c | Kevin Wolf | case 'C': |
1128 | 797ac58c | Kevin Wolf | Cflag = 1;
|
1129 | 797ac58c | Kevin Wolf | break;
|
1130 | 797ac58c | Kevin Wolf | case 'q': |
1131 | 797ac58c | Kevin Wolf | qflag = 1;
|
1132 | 797ac58c | Kevin Wolf | break;
|
1133 | 797ac58c | Kevin Wolf | case 'P': |
1134 | 797ac58c | Kevin Wolf | pattern = parse_pattern(optarg); |
1135 | 797ac58c | Kevin Wolf | if (pattern < 0) { |
1136 | 797ac58c | Kevin Wolf | return 0; |
1137 | 797ac58c | Kevin Wolf | } |
1138 | 797ac58c | Kevin Wolf | break;
|
1139 | 797ac58c | Kevin Wolf | default:
|
1140 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&writev_cmd);
|
1141 | 797ac58c | Kevin Wolf | } |
1142 | 797ac58c | Kevin Wolf | } |
1143 | 797ac58c | Kevin Wolf | |
1144 | 797ac58c | Kevin Wolf | if (optind > argc - 2) { |
1145 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&writev_cmd);
|
1146 | 797ac58c | Kevin Wolf | } |
1147 | 797ac58c | Kevin Wolf | |
1148 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[optind]); |
1149 | 797ac58c | Kevin Wolf | if (offset < 0) { |
1150 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
1151 | 797ac58c | Kevin Wolf | return 0; |
1152 | 797ac58c | Kevin Wolf | } |
1153 | 797ac58c | Kevin Wolf | optind++; |
1154 | 797ac58c | Kevin Wolf | |
1155 | 797ac58c | Kevin Wolf | if (offset & 0x1ff) { |
1156 | 797ac58c | Kevin Wolf | printf("offset %" PRId64 " is not sector aligned\n", |
1157 | 797ac58c | Kevin Wolf | offset); |
1158 | 797ac58c | Kevin Wolf | return 0; |
1159 | 797ac58c | Kevin Wolf | } |
1160 | 797ac58c | Kevin Wolf | |
1161 | 797ac58c | Kevin Wolf | nr_iov = argc - optind; |
1162 | 797ac58c | Kevin Wolf | buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern); |
1163 | 797ac58c | Kevin Wolf | if (buf == NULL) { |
1164 | 797ac58c | Kevin Wolf | return 0; |
1165 | 797ac58c | Kevin Wolf | } |
1166 | 797ac58c | Kevin Wolf | |
1167 | 797ac58c | Kevin Wolf | gettimeofday(&t1, NULL);
|
1168 | 797ac58c | Kevin Wolf | cnt = do_aio_writev(bs, &qiov, offset, &total); |
1169 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
1170 | 797ac58c | Kevin Wolf | |
1171 | 797ac58c | Kevin Wolf | if (cnt < 0) { |
1172 | 797ac58c | Kevin Wolf | printf("writev failed: %s\n", strerror(-cnt));
|
1173 | 797ac58c | Kevin Wolf | goto out;
|
1174 | 797ac58c | Kevin Wolf | } |
1175 | 797ac58c | Kevin Wolf | |
1176 | 797ac58c | Kevin Wolf | if (qflag) {
|
1177 | 797ac58c | Kevin Wolf | goto out;
|
1178 | 797ac58c | Kevin Wolf | } |
1179 | 797ac58c | Kevin Wolf | |
1180 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
1181 | 797ac58c | Kevin Wolf | t2 = tsub(t2, t1); |
1182 | 797ac58c | Kevin Wolf | print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
|
1183 | 797ac58c | Kevin Wolf | out:
|
1184 | 797ac58c | Kevin Wolf | qemu_iovec_destroy(&qiov); |
1185 | 797ac58c | Kevin Wolf | qemu_io_free(buf); |
1186 | 797ac58c | Kevin Wolf | return 0; |
1187 | 797ac58c | Kevin Wolf | } |
1188 | 797ac58c | Kevin Wolf | |
1189 | 797ac58c | Kevin Wolf | static void multiwrite_help(void) |
1190 | 797ac58c | Kevin Wolf | { |
1191 | 797ac58c | Kevin Wolf | printf( |
1192 | 797ac58c | Kevin Wolf | "\n"
|
1193 | 797ac58c | Kevin Wolf | " writes a range of bytes from the given offset source from multiple buffers,\n"
|
1194 | 797ac58c | Kevin Wolf | " in a batch of requests that may be merged by qemu\n"
|
1195 | 797ac58c | Kevin Wolf | "\n"
|
1196 | 797ac58c | Kevin Wolf | " Example:\n"
|
1197 | 797ac58c | Kevin Wolf | " 'multiwrite 512 1k 1k ; 4k 1k'\n"
|
1198 | 797ac58c | Kevin Wolf | " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
|
1199 | 797ac58c | Kevin Wolf | "\n"
|
1200 | 797ac58c | Kevin Wolf | " Writes into a segment of the currently open file, using a buffer\n"
|
1201 | 797ac58c | Kevin Wolf | " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
|
1202 | 797ac58c | Kevin Wolf | " by one for each request contained in the multiwrite command.\n"
|
1203 | 797ac58c | Kevin Wolf | " -P, -- use different pattern to fill file\n"
|
1204 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
1205 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
1206 | 797ac58c | Kevin Wolf | "\n");
|
1207 | 797ac58c | Kevin Wolf | } |
1208 | 797ac58c | Kevin Wolf | |
1209 | 797ac58c | Kevin Wolf | static int multiwrite_f(BlockDriverState *bs, int argc, char **argv); |
1210 | 797ac58c | Kevin Wolf | |
1211 | 797ac58c | Kevin Wolf | static const cmdinfo_t multiwrite_cmd = { |
1212 | 797ac58c | Kevin Wolf | .name = "multiwrite",
|
1213 | 797ac58c | Kevin Wolf | .cfunc = multiwrite_f, |
1214 | 797ac58c | Kevin Wolf | .argmin = 2,
|
1215 | 797ac58c | Kevin Wolf | .argmax = -1,
|
1216 | 797ac58c | Kevin Wolf | .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
|
1217 | 797ac58c | Kevin Wolf | .oneline = "issues multiple write requests at once",
|
1218 | 797ac58c | Kevin Wolf | .help = multiwrite_help, |
1219 | 797ac58c | Kevin Wolf | }; |
1220 | 797ac58c | Kevin Wolf | |
1221 | 797ac58c | Kevin Wolf | static int multiwrite_f(BlockDriverState *bs, int argc, char **argv) |
1222 | 797ac58c | Kevin Wolf | { |
1223 | 797ac58c | Kevin Wolf | struct timeval t1, t2;
|
1224 | 797ac58c | Kevin Wolf | int Cflag = 0, qflag = 0; |
1225 | 797ac58c | Kevin Wolf | int c, cnt;
|
1226 | 797ac58c | Kevin Wolf | char **buf;
|
1227 | 797ac58c | Kevin Wolf | int64_t offset, first_offset = 0;
|
1228 | 797ac58c | Kevin Wolf | /* Some compilers get confused and warn if this is not initialized. */
|
1229 | 797ac58c | Kevin Wolf | int total = 0; |
1230 | 797ac58c | Kevin Wolf | int nr_iov;
|
1231 | 797ac58c | Kevin Wolf | int nr_reqs;
|
1232 | 797ac58c | Kevin Wolf | int pattern = 0xcd; |
1233 | 797ac58c | Kevin Wolf | QEMUIOVector *qiovs; |
1234 | 797ac58c | Kevin Wolf | int i;
|
1235 | 797ac58c | Kevin Wolf | BlockRequest *reqs; |
1236 | 797ac58c | Kevin Wolf | |
1237 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
1238 | 797ac58c | Kevin Wolf | switch (c) {
|
1239 | 797ac58c | Kevin Wolf | case 'C': |
1240 | 797ac58c | Kevin Wolf | Cflag = 1;
|
1241 | 797ac58c | Kevin Wolf | break;
|
1242 | 797ac58c | Kevin Wolf | case 'q': |
1243 | 797ac58c | Kevin Wolf | qflag = 1;
|
1244 | 797ac58c | Kevin Wolf | break;
|
1245 | 797ac58c | Kevin Wolf | case 'P': |
1246 | 797ac58c | Kevin Wolf | pattern = parse_pattern(optarg); |
1247 | 797ac58c | Kevin Wolf | if (pattern < 0) { |
1248 | 797ac58c | Kevin Wolf | return 0; |
1249 | 797ac58c | Kevin Wolf | } |
1250 | 797ac58c | Kevin Wolf | break;
|
1251 | 797ac58c | Kevin Wolf | default:
|
1252 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&writev_cmd);
|
1253 | 797ac58c | Kevin Wolf | } |
1254 | 797ac58c | Kevin Wolf | } |
1255 | 797ac58c | Kevin Wolf | |
1256 | 797ac58c | Kevin Wolf | if (optind > argc - 2) { |
1257 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&writev_cmd);
|
1258 | 797ac58c | Kevin Wolf | } |
1259 | 797ac58c | Kevin Wolf | |
1260 | 797ac58c | Kevin Wolf | nr_reqs = 1;
|
1261 | 797ac58c | Kevin Wolf | for (i = optind; i < argc; i++) {
|
1262 | 797ac58c | Kevin Wolf | if (!strcmp(argv[i], ";")) { |
1263 | 797ac58c | Kevin Wolf | nr_reqs++; |
1264 | 797ac58c | Kevin Wolf | } |
1265 | 797ac58c | Kevin Wolf | } |
1266 | 797ac58c | Kevin Wolf | |
1267 | 797ac58c | Kevin Wolf | reqs = g_malloc0(nr_reqs * sizeof(*reqs));
|
1268 | 797ac58c | Kevin Wolf | buf = g_malloc0(nr_reqs * sizeof(*buf));
|
1269 | 797ac58c | Kevin Wolf | qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
|
1270 | 797ac58c | Kevin Wolf | |
1271 | 797ac58c | Kevin Wolf | for (i = 0; i < nr_reqs && optind < argc; i++) { |
1272 | 797ac58c | Kevin Wolf | int j;
|
1273 | 797ac58c | Kevin Wolf | |
1274 | 797ac58c | Kevin Wolf | /* Read the offset of the request */
|
1275 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[optind]); |
1276 | 797ac58c | Kevin Wolf | if (offset < 0) { |
1277 | 797ac58c | Kevin Wolf | printf("non-numeric offset argument -- %s\n", argv[optind]);
|
1278 | 797ac58c | Kevin Wolf | goto out;
|
1279 | 797ac58c | Kevin Wolf | } |
1280 | 797ac58c | Kevin Wolf | optind++; |
1281 | 797ac58c | Kevin Wolf | |
1282 | 797ac58c | Kevin Wolf | if (offset & 0x1ff) { |
1283 | 797ac58c | Kevin Wolf | printf("offset %lld is not sector aligned\n",
|
1284 | 797ac58c | Kevin Wolf | (long long)offset); |
1285 | 797ac58c | Kevin Wolf | goto out;
|
1286 | 797ac58c | Kevin Wolf | } |
1287 | 797ac58c | Kevin Wolf | |
1288 | 797ac58c | Kevin Wolf | if (i == 0) { |
1289 | 797ac58c | Kevin Wolf | first_offset = offset; |
1290 | 797ac58c | Kevin Wolf | } |
1291 | 797ac58c | Kevin Wolf | |
1292 | 797ac58c | Kevin Wolf | /* Read lengths for qiov entries */
|
1293 | 797ac58c | Kevin Wolf | for (j = optind; j < argc; j++) {
|
1294 | 797ac58c | Kevin Wolf | if (!strcmp(argv[j], ";")) { |
1295 | 797ac58c | Kevin Wolf | break;
|
1296 | 797ac58c | Kevin Wolf | } |
1297 | 797ac58c | Kevin Wolf | } |
1298 | 797ac58c | Kevin Wolf | |
1299 | 797ac58c | Kevin Wolf | nr_iov = j - optind; |
1300 | 797ac58c | Kevin Wolf | |
1301 | 797ac58c | Kevin Wolf | /* Build request */
|
1302 | 797ac58c | Kevin Wolf | buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern); |
1303 | 797ac58c | Kevin Wolf | if (buf[i] == NULL) { |
1304 | 797ac58c | Kevin Wolf | goto out;
|
1305 | 797ac58c | Kevin Wolf | } |
1306 | 797ac58c | Kevin Wolf | |
1307 | 797ac58c | Kevin Wolf | reqs[i].qiov = &qiovs[i]; |
1308 | 797ac58c | Kevin Wolf | reqs[i].sector = offset >> 9;
|
1309 | 797ac58c | Kevin Wolf | reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
|
1310 | 797ac58c | Kevin Wolf | |
1311 | 797ac58c | Kevin Wolf | optind = j + 1;
|
1312 | 797ac58c | Kevin Wolf | |
1313 | 797ac58c | Kevin Wolf | pattern++; |
1314 | 797ac58c | Kevin Wolf | } |
1315 | 797ac58c | Kevin Wolf | |
1316 | 797ac58c | Kevin Wolf | /* If there were empty requests at the end, ignore them */
|
1317 | 797ac58c | Kevin Wolf | nr_reqs = i; |
1318 | 797ac58c | Kevin Wolf | |
1319 | 797ac58c | Kevin Wolf | gettimeofday(&t1, NULL);
|
1320 | 797ac58c | Kevin Wolf | cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total); |
1321 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
1322 | 797ac58c | Kevin Wolf | |
1323 | 797ac58c | Kevin Wolf | if (cnt < 0) { |
1324 | 797ac58c | Kevin Wolf | printf("aio_multiwrite failed: %s\n", strerror(-cnt));
|
1325 | 797ac58c | Kevin Wolf | goto out;
|
1326 | 797ac58c | Kevin Wolf | } |
1327 | 797ac58c | Kevin Wolf | |
1328 | 797ac58c | Kevin Wolf | if (qflag) {
|
1329 | 797ac58c | Kevin Wolf | goto out;
|
1330 | 797ac58c | Kevin Wolf | } |
1331 | 797ac58c | Kevin Wolf | |
1332 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
1333 | 797ac58c | Kevin Wolf | t2 = tsub(t2, t1); |
1334 | 797ac58c | Kevin Wolf | print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
|
1335 | 797ac58c | Kevin Wolf | out:
|
1336 | 797ac58c | Kevin Wolf | for (i = 0; i < nr_reqs; i++) { |
1337 | 797ac58c | Kevin Wolf | qemu_io_free(buf[i]); |
1338 | 797ac58c | Kevin Wolf | if (reqs[i].qiov != NULL) { |
1339 | 797ac58c | Kevin Wolf | qemu_iovec_destroy(&qiovs[i]); |
1340 | 797ac58c | Kevin Wolf | } |
1341 | 797ac58c | Kevin Wolf | } |
1342 | 797ac58c | Kevin Wolf | g_free(buf); |
1343 | 797ac58c | Kevin Wolf | g_free(reqs); |
1344 | 797ac58c | Kevin Wolf | g_free(qiovs); |
1345 | 797ac58c | Kevin Wolf | return 0; |
1346 | 797ac58c | Kevin Wolf | } |
1347 | 797ac58c | Kevin Wolf | |
1348 | 797ac58c | Kevin Wolf | struct aio_ctx {
|
1349 | 797ac58c | Kevin Wolf | QEMUIOVector qiov; |
1350 | 797ac58c | Kevin Wolf | int64_t offset; |
1351 | 797ac58c | Kevin Wolf | char *buf;
|
1352 | 797ac58c | Kevin Wolf | int qflag;
|
1353 | 797ac58c | Kevin Wolf | int vflag;
|
1354 | 797ac58c | Kevin Wolf | int Cflag;
|
1355 | 797ac58c | Kevin Wolf | int Pflag;
|
1356 | 797ac58c | Kevin Wolf | int pattern;
|
1357 | 797ac58c | Kevin Wolf | struct timeval t1;
|
1358 | 797ac58c | Kevin Wolf | }; |
1359 | 797ac58c | Kevin Wolf | |
1360 | 797ac58c | Kevin Wolf | static void aio_write_done(void *opaque, int ret) |
1361 | 797ac58c | Kevin Wolf | { |
1362 | 797ac58c | Kevin Wolf | struct aio_ctx *ctx = opaque;
|
1363 | 797ac58c | Kevin Wolf | struct timeval t2;
|
1364 | 797ac58c | Kevin Wolf | |
1365 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
1366 | 797ac58c | Kevin Wolf | |
1367 | 797ac58c | Kevin Wolf | |
1368 | 797ac58c | Kevin Wolf | if (ret < 0) { |
1369 | 797ac58c | Kevin Wolf | printf("aio_write failed: %s\n", strerror(-ret));
|
1370 | 797ac58c | Kevin Wolf | goto out;
|
1371 | 797ac58c | Kevin Wolf | } |
1372 | 797ac58c | Kevin Wolf | |
1373 | 797ac58c | Kevin Wolf | if (ctx->qflag) {
|
1374 | 797ac58c | Kevin Wolf | goto out;
|
1375 | 797ac58c | Kevin Wolf | } |
1376 | 797ac58c | Kevin Wolf | |
1377 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
1378 | 797ac58c | Kevin Wolf | t2 = tsub(t2, ctx->t1); |
1379 | 797ac58c | Kevin Wolf | print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
|
1380 | 797ac58c | Kevin Wolf | ctx->qiov.size, 1, ctx->Cflag);
|
1381 | 797ac58c | Kevin Wolf | out:
|
1382 | 797ac58c | Kevin Wolf | qemu_io_free(ctx->buf); |
1383 | 797ac58c | Kevin Wolf | qemu_iovec_destroy(&ctx->qiov); |
1384 | 797ac58c | Kevin Wolf | g_free(ctx); |
1385 | 797ac58c | Kevin Wolf | } |
1386 | 797ac58c | Kevin Wolf | |
1387 | 797ac58c | Kevin Wolf | static void aio_read_done(void *opaque, int ret) |
1388 | 797ac58c | Kevin Wolf | { |
1389 | 797ac58c | Kevin Wolf | struct aio_ctx *ctx = opaque;
|
1390 | 797ac58c | Kevin Wolf | struct timeval t2;
|
1391 | 797ac58c | Kevin Wolf | |
1392 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
1393 | 797ac58c | Kevin Wolf | |
1394 | 797ac58c | Kevin Wolf | if (ret < 0) { |
1395 | 797ac58c | Kevin Wolf | printf("readv failed: %s\n", strerror(-ret));
|
1396 | 797ac58c | Kevin Wolf | goto out;
|
1397 | 797ac58c | Kevin Wolf | } |
1398 | 797ac58c | Kevin Wolf | |
1399 | 797ac58c | Kevin Wolf | if (ctx->Pflag) {
|
1400 | 797ac58c | Kevin Wolf | void *cmp_buf = g_malloc(ctx->qiov.size);
|
1401 | 797ac58c | Kevin Wolf | |
1402 | 797ac58c | Kevin Wolf | memset(cmp_buf, ctx->pattern, ctx->qiov.size); |
1403 | 797ac58c | Kevin Wolf | if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
|
1404 | 797ac58c | Kevin Wolf | printf("Pattern verification failed at offset %"
|
1405 | 797ac58c | Kevin Wolf | PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
|
1406 | 797ac58c | Kevin Wolf | } |
1407 | 797ac58c | Kevin Wolf | g_free(cmp_buf); |
1408 | 797ac58c | Kevin Wolf | } |
1409 | 797ac58c | Kevin Wolf | |
1410 | 797ac58c | Kevin Wolf | if (ctx->qflag) {
|
1411 | 797ac58c | Kevin Wolf | goto out;
|
1412 | 797ac58c | Kevin Wolf | } |
1413 | 797ac58c | Kevin Wolf | |
1414 | 797ac58c | Kevin Wolf | if (ctx->vflag) {
|
1415 | 797ac58c | Kevin Wolf | dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); |
1416 | 797ac58c | Kevin Wolf | } |
1417 | 797ac58c | Kevin Wolf | |
1418 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
1419 | 797ac58c | Kevin Wolf | t2 = tsub(t2, ctx->t1); |
1420 | 797ac58c | Kevin Wolf | print_report("read", &t2, ctx->offset, ctx->qiov.size,
|
1421 | 797ac58c | Kevin Wolf | ctx->qiov.size, 1, ctx->Cflag);
|
1422 | 797ac58c | Kevin Wolf | out:
|
1423 | 797ac58c | Kevin Wolf | qemu_io_free(ctx->buf); |
1424 | 797ac58c | Kevin Wolf | qemu_iovec_destroy(&ctx->qiov); |
1425 | 797ac58c | Kevin Wolf | g_free(ctx); |
1426 | 797ac58c | Kevin Wolf | } |
1427 | 797ac58c | Kevin Wolf | |
1428 | 797ac58c | Kevin Wolf | static void aio_read_help(void) |
1429 | 797ac58c | Kevin Wolf | { |
1430 | 797ac58c | Kevin Wolf | printf( |
1431 | 797ac58c | Kevin Wolf | "\n"
|
1432 | 797ac58c | Kevin Wolf | " asynchronously reads a range of bytes from the given offset\n"
|
1433 | 797ac58c | Kevin Wolf | "\n"
|
1434 | 797ac58c | Kevin Wolf | " Example:\n"
|
1435 | 797ac58c | Kevin Wolf | " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
|
1436 | 797ac58c | Kevin Wolf | "\n"
|
1437 | 797ac58c | Kevin Wolf | " Reads a segment of the currently open file, optionally dumping it to the\n"
|
1438 | 797ac58c | Kevin Wolf | " standard output stream (with -v option) for subsequent inspection.\n"
|
1439 | 797ac58c | Kevin Wolf | " The read is performed asynchronously and the aio_flush command must be\n"
|
1440 | 797ac58c | Kevin Wolf | " used to ensure all outstanding aio requests have been completed.\n"
|
1441 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
1442 | 797ac58c | Kevin Wolf | " -P, -- use a pattern to verify read data\n"
|
1443 | 797ac58c | Kevin Wolf | " -v, -- dump buffer to standard output\n"
|
1444 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
1445 | 797ac58c | Kevin Wolf | "\n");
|
1446 | 797ac58c | Kevin Wolf | } |
1447 | 797ac58c | Kevin Wolf | |
1448 | 797ac58c | Kevin Wolf | static int aio_read_f(BlockDriverState *bs, int argc, char **argv); |
1449 | 797ac58c | Kevin Wolf | |
1450 | 797ac58c | Kevin Wolf | static const cmdinfo_t aio_read_cmd = { |
1451 | 797ac58c | Kevin Wolf | .name = "aio_read",
|
1452 | 797ac58c | Kevin Wolf | .cfunc = aio_read_f, |
1453 | 797ac58c | Kevin Wolf | .argmin = 2,
|
1454 | 797ac58c | Kevin Wolf | .argmax = -1,
|
1455 | 797ac58c | Kevin Wolf | .args = "[-Cqv] [-P pattern ] off len [len..]",
|
1456 | 797ac58c | Kevin Wolf | .oneline = "asynchronously reads a number of bytes",
|
1457 | 797ac58c | Kevin Wolf | .help = aio_read_help, |
1458 | 797ac58c | Kevin Wolf | }; |
1459 | 797ac58c | Kevin Wolf | |
1460 | 797ac58c | Kevin Wolf | static int aio_read_f(BlockDriverState *bs, int argc, char **argv) |
1461 | 797ac58c | Kevin Wolf | { |
1462 | 797ac58c | Kevin Wolf | int nr_iov, c;
|
1463 | 797ac58c | Kevin Wolf | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
1464 | 797ac58c | Kevin Wolf | |
1465 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
1466 | 797ac58c | Kevin Wolf | switch (c) {
|
1467 | 797ac58c | Kevin Wolf | case 'C': |
1468 | 797ac58c | Kevin Wolf | ctx->Cflag = 1;
|
1469 | 797ac58c | Kevin Wolf | break;
|
1470 | 797ac58c | Kevin Wolf | case 'P': |
1471 | 797ac58c | Kevin Wolf | ctx->Pflag = 1;
|
1472 | 797ac58c | Kevin Wolf | ctx->pattern = parse_pattern(optarg); |
1473 | 797ac58c | Kevin Wolf | if (ctx->pattern < 0) { |
1474 | 797ac58c | Kevin Wolf | g_free(ctx); |
1475 | 797ac58c | Kevin Wolf | return 0; |
1476 | 797ac58c | Kevin Wolf | } |
1477 | 797ac58c | Kevin Wolf | break;
|
1478 | 797ac58c | Kevin Wolf | case 'q': |
1479 | 797ac58c | Kevin Wolf | ctx->qflag = 1;
|
1480 | 797ac58c | Kevin Wolf | break;
|
1481 | 797ac58c | Kevin Wolf | case 'v': |
1482 | 797ac58c | Kevin Wolf | ctx->vflag = 1;
|
1483 | 797ac58c | Kevin Wolf | break;
|
1484 | 797ac58c | Kevin Wolf | default:
|
1485 | 797ac58c | Kevin Wolf | g_free(ctx); |
1486 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&aio_read_cmd);
|
1487 | 797ac58c | Kevin Wolf | } |
1488 | 797ac58c | Kevin Wolf | } |
1489 | 797ac58c | Kevin Wolf | |
1490 | 797ac58c | Kevin Wolf | if (optind > argc - 2) { |
1491 | 797ac58c | Kevin Wolf | g_free(ctx); |
1492 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&aio_read_cmd);
|
1493 | 797ac58c | Kevin Wolf | } |
1494 | 797ac58c | Kevin Wolf | |
1495 | 797ac58c | Kevin Wolf | ctx->offset = cvtnum(argv[optind]); |
1496 | 797ac58c | Kevin Wolf | if (ctx->offset < 0) { |
1497 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
1498 | 797ac58c | Kevin Wolf | g_free(ctx); |
1499 | 797ac58c | Kevin Wolf | return 0; |
1500 | 797ac58c | Kevin Wolf | } |
1501 | 797ac58c | Kevin Wolf | optind++; |
1502 | 797ac58c | Kevin Wolf | |
1503 | 797ac58c | Kevin Wolf | if (ctx->offset & 0x1ff) { |
1504 | 797ac58c | Kevin Wolf | printf("offset %" PRId64 " is not sector aligned\n", |
1505 | 797ac58c | Kevin Wolf | ctx->offset); |
1506 | 797ac58c | Kevin Wolf | g_free(ctx); |
1507 | 797ac58c | Kevin Wolf | return 0; |
1508 | 797ac58c | Kevin Wolf | } |
1509 | 797ac58c | Kevin Wolf | |
1510 | 797ac58c | Kevin Wolf | nr_iov = argc - optind; |
1511 | 797ac58c | Kevin Wolf | ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
|
1512 | 797ac58c | Kevin Wolf | if (ctx->buf == NULL) { |
1513 | 797ac58c | Kevin Wolf | g_free(ctx); |
1514 | 797ac58c | Kevin Wolf | return 0; |
1515 | 797ac58c | Kevin Wolf | } |
1516 | 797ac58c | Kevin Wolf | |
1517 | 797ac58c | Kevin Wolf | gettimeofday(&ctx->t1, NULL);
|
1518 | 797ac58c | Kevin Wolf | bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
|
1519 | 797ac58c | Kevin Wolf | ctx->qiov.size >> 9, aio_read_done, ctx);
|
1520 | 797ac58c | Kevin Wolf | return 0; |
1521 | 797ac58c | Kevin Wolf | } |
1522 | 797ac58c | Kevin Wolf | |
1523 | 797ac58c | Kevin Wolf | static void aio_write_help(void) |
1524 | 797ac58c | Kevin Wolf | { |
1525 | 797ac58c | Kevin Wolf | printf( |
1526 | 797ac58c | Kevin Wolf | "\n"
|
1527 | 797ac58c | Kevin Wolf | " asynchronously writes a range of bytes from the given offset source\n"
|
1528 | 797ac58c | Kevin Wolf | " from multiple buffers\n"
|
1529 | 797ac58c | Kevin Wolf | "\n"
|
1530 | 797ac58c | Kevin Wolf | " Example:\n"
|
1531 | 797ac58c | Kevin Wolf | " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
|
1532 | 797ac58c | Kevin Wolf | "\n"
|
1533 | 797ac58c | Kevin Wolf | " Writes into a segment of the currently open file, using a buffer\n"
|
1534 | 797ac58c | Kevin Wolf | " filled with a set pattern (0xcdcdcdcd).\n"
|
1535 | 797ac58c | Kevin Wolf | " The write is performed asynchronously and the aio_flush command must be\n"
|
1536 | 797ac58c | Kevin Wolf | " used to ensure all outstanding aio requests have been completed.\n"
|
1537 | 797ac58c | Kevin Wolf | " -P, -- use different pattern to fill file\n"
|
1538 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
1539 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
1540 | 797ac58c | Kevin Wolf | "\n");
|
1541 | 797ac58c | Kevin Wolf | } |
1542 | 797ac58c | Kevin Wolf | |
1543 | 797ac58c | Kevin Wolf | static int aio_write_f(BlockDriverState *bs, int argc, char **argv); |
1544 | 797ac58c | Kevin Wolf | |
1545 | 797ac58c | Kevin Wolf | static const cmdinfo_t aio_write_cmd = { |
1546 | 797ac58c | Kevin Wolf | .name = "aio_write",
|
1547 | 797ac58c | Kevin Wolf | .cfunc = aio_write_f, |
1548 | 797ac58c | Kevin Wolf | .argmin = 2,
|
1549 | 797ac58c | Kevin Wolf | .argmax = -1,
|
1550 | 797ac58c | Kevin Wolf | .args = "[-Cq] [-P pattern ] off len [len..]",
|
1551 | 797ac58c | Kevin Wolf | .oneline = "asynchronously writes a number of bytes",
|
1552 | 797ac58c | Kevin Wolf | .help = aio_write_help, |
1553 | 797ac58c | Kevin Wolf | }; |
1554 | 797ac58c | Kevin Wolf | |
1555 | 797ac58c | Kevin Wolf | static int aio_write_f(BlockDriverState *bs, int argc, char **argv) |
1556 | 797ac58c | Kevin Wolf | { |
1557 | 797ac58c | Kevin Wolf | int nr_iov, c;
|
1558 | 797ac58c | Kevin Wolf | int pattern = 0xcd; |
1559 | 797ac58c | Kevin Wolf | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
1560 | 797ac58c | Kevin Wolf | |
1561 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
1562 | 797ac58c | Kevin Wolf | switch (c) {
|
1563 | 797ac58c | Kevin Wolf | case 'C': |
1564 | 797ac58c | Kevin Wolf | ctx->Cflag = 1;
|
1565 | 797ac58c | Kevin Wolf | break;
|
1566 | 797ac58c | Kevin Wolf | case 'q': |
1567 | 797ac58c | Kevin Wolf | ctx->qflag = 1;
|
1568 | 797ac58c | Kevin Wolf | break;
|
1569 | 797ac58c | Kevin Wolf | case 'P': |
1570 | 797ac58c | Kevin Wolf | pattern = parse_pattern(optarg); |
1571 | 797ac58c | Kevin Wolf | if (pattern < 0) { |
1572 | 797ac58c | Kevin Wolf | g_free(ctx); |
1573 | 797ac58c | Kevin Wolf | return 0; |
1574 | 797ac58c | Kevin Wolf | } |
1575 | 797ac58c | Kevin Wolf | break;
|
1576 | 797ac58c | Kevin Wolf | default:
|
1577 | 797ac58c | Kevin Wolf | g_free(ctx); |
1578 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&aio_write_cmd);
|
1579 | 797ac58c | Kevin Wolf | } |
1580 | 797ac58c | Kevin Wolf | } |
1581 | 797ac58c | Kevin Wolf | |
1582 | 797ac58c | Kevin Wolf | if (optind > argc - 2) { |
1583 | 797ac58c | Kevin Wolf | g_free(ctx); |
1584 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&aio_write_cmd);
|
1585 | 797ac58c | Kevin Wolf | } |
1586 | 797ac58c | Kevin Wolf | |
1587 | 797ac58c | Kevin Wolf | ctx->offset = cvtnum(argv[optind]); |
1588 | 797ac58c | Kevin Wolf | if (ctx->offset < 0) { |
1589 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
1590 | 797ac58c | Kevin Wolf | g_free(ctx); |
1591 | 797ac58c | Kevin Wolf | return 0; |
1592 | 797ac58c | Kevin Wolf | } |
1593 | 797ac58c | Kevin Wolf | optind++; |
1594 | 797ac58c | Kevin Wolf | |
1595 | 797ac58c | Kevin Wolf | if (ctx->offset & 0x1ff) { |
1596 | 797ac58c | Kevin Wolf | printf("offset %" PRId64 " is not sector aligned\n", |
1597 | 797ac58c | Kevin Wolf | ctx->offset); |
1598 | 797ac58c | Kevin Wolf | g_free(ctx); |
1599 | 797ac58c | Kevin Wolf | return 0; |
1600 | 797ac58c | Kevin Wolf | } |
1601 | 797ac58c | Kevin Wolf | |
1602 | 797ac58c | Kevin Wolf | nr_iov = argc - optind; |
1603 | 797ac58c | Kevin Wolf | ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern); |
1604 | 797ac58c | Kevin Wolf | if (ctx->buf == NULL) { |
1605 | 797ac58c | Kevin Wolf | g_free(ctx); |
1606 | 797ac58c | Kevin Wolf | return 0; |
1607 | 797ac58c | Kevin Wolf | } |
1608 | 797ac58c | Kevin Wolf | |
1609 | 797ac58c | Kevin Wolf | gettimeofday(&ctx->t1, NULL);
|
1610 | 797ac58c | Kevin Wolf | bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
|
1611 | 797ac58c | Kevin Wolf | ctx->qiov.size >> 9, aio_write_done, ctx);
|
1612 | 797ac58c | Kevin Wolf | return 0; |
1613 | 797ac58c | Kevin Wolf | } |
1614 | 797ac58c | Kevin Wolf | |
1615 | 797ac58c | Kevin Wolf | static int aio_flush_f(BlockDriverState *bs, int argc, char **argv) |
1616 | 797ac58c | Kevin Wolf | { |
1617 | 797ac58c | Kevin Wolf | bdrv_drain_all(); |
1618 | 797ac58c | Kevin Wolf | return 0; |
1619 | 797ac58c | Kevin Wolf | } |
1620 | 797ac58c | Kevin Wolf | |
1621 | 797ac58c | Kevin Wolf | static const cmdinfo_t aio_flush_cmd = { |
1622 | 797ac58c | Kevin Wolf | .name = "aio_flush",
|
1623 | 797ac58c | Kevin Wolf | .cfunc = aio_flush_f, |
1624 | 797ac58c | Kevin Wolf | .oneline = "completes all outstanding aio requests"
|
1625 | 797ac58c | Kevin Wolf | }; |
1626 | 797ac58c | Kevin Wolf | |
1627 | 797ac58c | Kevin Wolf | static int flush_f(BlockDriverState *bs, int argc, char **argv) |
1628 | 797ac58c | Kevin Wolf | { |
1629 | 797ac58c | Kevin Wolf | bdrv_flush(bs); |
1630 | 797ac58c | Kevin Wolf | return 0; |
1631 | 797ac58c | Kevin Wolf | } |
1632 | 797ac58c | Kevin Wolf | |
1633 | 797ac58c | Kevin Wolf | static const cmdinfo_t flush_cmd = { |
1634 | 797ac58c | Kevin Wolf | .name = "flush",
|
1635 | 797ac58c | Kevin Wolf | .altname = "f",
|
1636 | 797ac58c | Kevin Wolf | .cfunc = flush_f, |
1637 | 797ac58c | Kevin Wolf | .oneline = "flush all in-core file state to disk",
|
1638 | 797ac58c | Kevin Wolf | }; |
1639 | 797ac58c | Kevin Wolf | |
1640 | 797ac58c | Kevin Wolf | static int truncate_f(BlockDriverState *bs, int argc, char **argv) |
1641 | 797ac58c | Kevin Wolf | { |
1642 | 797ac58c | Kevin Wolf | int64_t offset; |
1643 | 797ac58c | Kevin Wolf | int ret;
|
1644 | 797ac58c | Kevin Wolf | |
1645 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[1]);
|
1646 | 797ac58c | Kevin Wolf | if (offset < 0) { |
1647 | 797ac58c | Kevin Wolf | printf("non-numeric truncate argument -- %s\n", argv[1]); |
1648 | 797ac58c | Kevin Wolf | return 0; |
1649 | 797ac58c | Kevin Wolf | } |
1650 | 797ac58c | Kevin Wolf | |
1651 | 797ac58c | Kevin Wolf | ret = bdrv_truncate(bs, offset); |
1652 | 797ac58c | Kevin Wolf | if (ret < 0) { |
1653 | 797ac58c | Kevin Wolf | printf("truncate: %s\n", strerror(-ret));
|
1654 | 797ac58c | Kevin Wolf | return 0; |
1655 | 797ac58c | Kevin Wolf | } |
1656 | 797ac58c | Kevin Wolf | |
1657 | 797ac58c | Kevin Wolf | return 0; |
1658 | 797ac58c | Kevin Wolf | } |
1659 | 797ac58c | Kevin Wolf | |
1660 | 797ac58c | Kevin Wolf | static const cmdinfo_t truncate_cmd = { |
1661 | 797ac58c | Kevin Wolf | .name = "truncate",
|
1662 | 797ac58c | Kevin Wolf | .altname = "t",
|
1663 | 797ac58c | Kevin Wolf | .cfunc = truncate_f, |
1664 | 797ac58c | Kevin Wolf | .argmin = 1,
|
1665 | 797ac58c | Kevin Wolf | .argmax = 1,
|
1666 | 797ac58c | Kevin Wolf | .args = "off",
|
1667 | 797ac58c | Kevin Wolf | .oneline = "truncates the current file at the given offset",
|
1668 | 797ac58c | Kevin Wolf | }; |
1669 | 797ac58c | Kevin Wolf | |
1670 | 797ac58c | Kevin Wolf | static int length_f(BlockDriverState *bs, int argc, char **argv) |
1671 | 797ac58c | Kevin Wolf | { |
1672 | 797ac58c | Kevin Wolf | int64_t size; |
1673 | 797ac58c | Kevin Wolf | char s1[64]; |
1674 | 797ac58c | Kevin Wolf | |
1675 | 797ac58c | Kevin Wolf | size = bdrv_getlength(bs); |
1676 | 797ac58c | Kevin Wolf | if (size < 0) { |
1677 | 797ac58c | Kevin Wolf | printf("getlength: %s\n", strerror(-size));
|
1678 | 797ac58c | Kevin Wolf | return 0; |
1679 | 797ac58c | Kevin Wolf | } |
1680 | 797ac58c | Kevin Wolf | |
1681 | 797ac58c | Kevin Wolf | cvtstr(size, s1, sizeof(s1));
|
1682 | 797ac58c | Kevin Wolf | printf("%s\n", s1);
|
1683 | 797ac58c | Kevin Wolf | return 0; |
1684 | 797ac58c | Kevin Wolf | } |
1685 | 797ac58c | Kevin Wolf | |
1686 | 797ac58c | Kevin Wolf | |
1687 | 797ac58c | Kevin Wolf | static const cmdinfo_t length_cmd = { |
1688 | 797ac58c | Kevin Wolf | .name = "length",
|
1689 | 797ac58c | Kevin Wolf | .altname = "l",
|
1690 | 797ac58c | Kevin Wolf | .cfunc = length_f, |
1691 | 797ac58c | Kevin Wolf | .oneline = "gets the length of the current file",
|
1692 | 797ac58c | Kevin Wolf | }; |
1693 | 797ac58c | Kevin Wolf | |
1694 | 797ac58c | Kevin Wolf | |
1695 | 797ac58c | Kevin Wolf | static int info_f(BlockDriverState *bs, int argc, char **argv) |
1696 | 797ac58c | Kevin Wolf | { |
1697 | 797ac58c | Kevin Wolf | BlockDriverInfo bdi; |
1698 | a8d8ecb7 | Max Reitz | ImageInfoSpecific *spec_info; |
1699 | 797ac58c | Kevin Wolf | char s1[64], s2[64]; |
1700 | 797ac58c | Kevin Wolf | int ret;
|
1701 | 797ac58c | Kevin Wolf | |
1702 | 797ac58c | Kevin Wolf | if (bs->drv && bs->drv->format_name) {
|
1703 | 797ac58c | Kevin Wolf | printf("format name: %s\n", bs->drv->format_name);
|
1704 | 797ac58c | Kevin Wolf | } |
1705 | 797ac58c | Kevin Wolf | if (bs->drv && bs->drv->protocol_name) {
|
1706 | 797ac58c | Kevin Wolf | printf("format name: %s\n", bs->drv->protocol_name);
|
1707 | 797ac58c | Kevin Wolf | } |
1708 | 797ac58c | Kevin Wolf | |
1709 | 797ac58c | Kevin Wolf | ret = bdrv_get_info(bs, &bdi); |
1710 | 797ac58c | Kevin Wolf | if (ret) {
|
1711 | 797ac58c | Kevin Wolf | return 0; |
1712 | 797ac58c | Kevin Wolf | } |
1713 | 797ac58c | Kevin Wolf | |
1714 | 797ac58c | Kevin Wolf | cvtstr(bdi.cluster_size, s1, sizeof(s1));
|
1715 | 797ac58c | Kevin Wolf | cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
|
1716 | 797ac58c | Kevin Wolf | |
1717 | 797ac58c | Kevin Wolf | printf("cluster size: %s\n", s1);
|
1718 | 797ac58c | Kevin Wolf | printf("vm state offset: %s\n", s2);
|
1719 | 797ac58c | Kevin Wolf | |
1720 | a8d8ecb7 | Max Reitz | spec_info = bdrv_get_specific_info(bs); |
1721 | a8d8ecb7 | Max Reitz | if (spec_info) {
|
1722 | a8d8ecb7 | Max Reitz | printf("Format specific information:\n");
|
1723 | a8d8ecb7 | Max Reitz | bdrv_image_info_specific_dump(fprintf, stdout, spec_info); |
1724 | a8d8ecb7 | Max Reitz | qapi_free_ImageInfoSpecific(spec_info); |
1725 | a8d8ecb7 | Max Reitz | } |
1726 | a8d8ecb7 | Max Reitz | |
1727 | 797ac58c | Kevin Wolf | return 0; |
1728 | 797ac58c | Kevin Wolf | } |
1729 | 797ac58c | Kevin Wolf | |
1730 | 797ac58c | Kevin Wolf | |
1731 | 797ac58c | Kevin Wolf | |
1732 | 797ac58c | Kevin Wolf | static const cmdinfo_t info_cmd = { |
1733 | 797ac58c | Kevin Wolf | .name = "info",
|
1734 | 797ac58c | Kevin Wolf | .altname = "i",
|
1735 | 797ac58c | Kevin Wolf | .cfunc = info_f, |
1736 | 797ac58c | Kevin Wolf | .oneline = "prints information about the current file",
|
1737 | 797ac58c | Kevin Wolf | }; |
1738 | 797ac58c | Kevin Wolf | |
1739 | 797ac58c | Kevin Wolf | static void discard_help(void) |
1740 | 797ac58c | Kevin Wolf | { |
1741 | 797ac58c | Kevin Wolf | printf( |
1742 | 797ac58c | Kevin Wolf | "\n"
|
1743 | 797ac58c | Kevin Wolf | " discards a range of bytes from the given offset\n"
|
1744 | 797ac58c | Kevin Wolf | "\n"
|
1745 | 797ac58c | Kevin Wolf | " Example:\n"
|
1746 | 797ac58c | Kevin Wolf | " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
|
1747 | 797ac58c | Kevin Wolf | "\n"
|
1748 | 797ac58c | Kevin Wolf | " Discards a segment of the currently open file.\n"
|
1749 | 797ac58c | Kevin Wolf | " -C, -- report statistics in a machine parsable format\n"
|
1750 | 797ac58c | Kevin Wolf | " -q, -- quiet mode, do not show I/O statistics\n"
|
1751 | 797ac58c | Kevin Wolf | "\n");
|
1752 | 797ac58c | Kevin Wolf | } |
1753 | 797ac58c | Kevin Wolf | |
1754 | 797ac58c | Kevin Wolf | static int discard_f(BlockDriverState *bs, int argc, char **argv); |
1755 | 797ac58c | Kevin Wolf | |
1756 | 797ac58c | Kevin Wolf | static const cmdinfo_t discard_cmd = { |
1757 | 797ac58c | Kevin Wolf | .name = "discard",
|
1758 | 797ac58c | Kevin Wolf | .altname = "d",
|
1759 | 797ac58c | Kevin Wolf | .cfunc = discard_f, |
1760 | 797ac58c | Kevin Wolf | .argmin = 2,
|
1761 | 797ac58c | Kevin Wolf | .argmax = -1,
|
1762 | 797ac58c | Kevin Wolf | .args = "[-Cq] off len",
|
1763 | 797ac58c | Kevin Wolf | .oneline = "discards a number of bytes at a specified offset",
|
1764 | 797ac58c | Kevin Wolf | .help = discard_help, |
1765 | 797ac58c | Kevin Wolf | }; |
1766 | 797ac58c | Kevin Wolf | |
1767 | 797ac58c | Kevin Wolf | static int discard_f(BlockDriverState *bs, int argc, char **argv) |
1768 | 797ac58c | Kevin Wolf | { |
1769 | 797ac58c | Kevin Wolf | struct timeval t1, t2;
|
1770 | 797ac58c | Kevin Wolf | int Cflag = 0, qflag = 0; |
1771 | 797ac58c | Kevin Wolf | int c, ret;
|
1772 | 797ac58c | Kevin Wolf | int64_t offset; |
1773 | 797ac58c | Kevin Wolf | int count;
|
1774 | 797ac58c | Kevin Wolf | |
1775 | 797ac58c | Kevin Wolf | while ((c = getopt(argc, argv, "Cq")) != EOF) { |
1776 | 797ac58c | Kevin Wolf | switch (c) {
|
1777 | 797ac58c | Kevin Wolf | case 'C': |
1778 | 797ac58c | Kevin Wolf | Cflag = 1;
|
1779 | 797ac58c | Kevin Wolf | break;
|
1780 | 797ac58c | Kevin Wolf | case 'q': |
1781 | 797ac58c | Kevin Wolf | qflag = 1;
|
1782 | 797ac58c | Kevin Wolf | break;
|
1783 | 797ac58c | Kevin Wolf | default:
|
1784 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&discard_cmd);
|
1785 | 797ac58c | Kevin Wolf | } |
1786 | 797ac58c | Kevin Wolf | } |
1787 | 797ac58c | Kevin Wolf | |
1788 | 797ac58c | Kevin Wolf | if (optind != argc - 2) { |
1789 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&discard_cmd);
|
1790 | 797ac58c | Kevin Wolf | } |
1791 | 797ac58c | Kevin Wolf | |
1792 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[optind]); |
1793 | 797ac58c | Kevin Wolf | if (offset < 0) { |
1794 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
1795 | 797ac58c | Kevin Wolf | return 0; |
1796 | 797ac58c | Kevin Wolf | } |
1797 | 797ac58c | Kevin Wolf | |
1798 | 797ac58c | Kevin Wolf | optind++; |
1799 | 797ac58c | Kevin Wolf | count = cvtnum(argv[optind]); |
1800 | 797ac58c | Kevin Wolf | if (count < 0) { |
1801 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[optind]);
|
1802 | 797ac58c | Kevin Wolf | return 0; |
1803 | 797ac58c | Kevin Wolf | } |
1804 | 797ac58c | Kevin Wolf | |
1805 | 797ac58c | Kevin Wolf | gettimeofday(&t1, NULL);
|
1806 | 797ac58c | Kevin Wolf | ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS, |
1807 | 797ac58c | Kevin Wolf | count >> BDRV_SECTOR_BITS); |
1808 | 797ac58c | Kevin Wolf | gettimeofday(&t2, NULL);
|
1809 | 797ac58c | Kevin Wolf | |
1810 | 797ac58c | Kevin Wolf | if (ret < 0) { |
1811 | 797ac58c | Kevin Wolf | printf("discard failed: %s\n", strerror(-ret));
|
1812 | 797ac58c | Kevin Wolf | goto out;
|
1813 | 797ac58c | Kevin Wolf | } |
1814 | 797ac58c | Kevin Wolf | |
1815 | 797ac58c | Kevin Wolf | /* Finally, report back -- -C gives a parsable format */
|
1816 | 797ac58c | Kevin Wolf | if (!qflag) {
|
1817 | 797ac58c | Kevin Wolf | t2 = tsub(t2, t1); |
1818 | 797ac58c | Kevin Wolf | print_report("discard", &t2, offset, count, count, 1, Cflag); |
1819 | 797ac58c | Kevin Wolf | } |
1820 | 797ac58c | Kevin Wolf | |
1821 | 797ac58c | Kevin Wolf | out:
|
1822 | 797ac58c | Kevin Wolf | return 0; |
1823 | 797ac58c | Kevin Wolf | } |
1824 | 797ac58c | Kevin Wolf | |
1825 | 797ac58c | Kevin Wolf | static int alloc_f(BlockDriverState *bs, int argc, char **argv) |
1826 | 797ac58c | Kevin Wolf | { |
1827 | 797ac58c | Kevin Wolf | int64_t offset, sector_num; |
1828 | 797ac58c | Kevin Wolf | int nb_sectors, remaining;
|
1829 | 797ac58c | Kevin Wolf | char s1[64]; |
1830 | 797ac58c | Kevin Wolf | int num, sum_alloc;
|
1831 | 797ac58c | Kevin Wolf | int ret;
|
1832 | 797ac58c | Kevin Wolf | |
1833 | 797ac58c | Kevin Wolf | offset = cvtnum(argv[1]);
|
1834 | 797ac58c | Kevin Wolf | if (offset < 0) { |
1835 | 797ac58c | Kevin Wolf | printf("non-numeric offset argument -- %s\n", argv[1]); |
1836 | 797ac58c | Kevin Wolf | return 0; |
1837 | 797ac58c | Kevin Wolf | } else if (offset & 0x1ff) { |
1838 | 797ac58c | Kevin Wolf | printf("offset %" PRId64 " is not sector aligned\n", |
1839 | 797ac58c | Kevin Wolf | offset); |
1840 | 797ac58c | Kevin Wolf | return 0; |
1841 | 797ac58c | Kevin Wolf | } |
1842 | 797ac58c | Kevin Wolf | |
1843 | 797ac58c | Kevin Wolf | if (argc == 3) { |
1844 | 797ac58c | Kevin Wolf | nb_sectors = cvtnum(argv[2]);
|
1845 | 797ac58c | Kevin Wolf | if (nb_sectors < 0) { |
1846 | 797ac58c | Kevin Wolf | printf("non-numeric length argument -- %s\n", argv[2]); |
1847 | 797ac58c | Kevin Wolf | return 0; |
1848 | 797ac58c | Kevin Wolf | } |
1849 | 797ac58c | Kevin Wolf | } else {
|
1850 | 797ac58c | Kevin Wolf | nb_sectors = 1;
|
1851 | 797ac58c | Kevin Wolf | } |
1852 | 797ac58c | Kevin Wolf | |
1853 | 797ac58c | Kevin Wolf | remaining = nb_sectors; |
1854 | 797ac58c | Kevin Wolf | sum_alloc = 0;
|
1855 | 797ac58c | Kevin Wolf | sector_num = offset >> 9;
|
1856 | 797ac58c | Kevin Wolf | while (remaining) {
|
1857 | 797ac58c | Kevin Wolf | ret = bdrv_is_allocated(bs, sector_num, remaining, &num); |
1858 | d663640c | Paolo Bonzini | if (ret < 0) { |
1859 | d663640c | Paolo Bonzini | printf("is_allocated failed: %s\n", strerror(-ret));
|
1860 | d663640c | Paolo Bonzini | return 0; |
1861 | d663640c | Paolo Bonzini | } |
1862 | 797ac58c | Kevin Wolf | sector_num += num; |
1863 | 797ac58c | Kevin Wolf | remaining -= num; |
1864 | 797ac58c | Kevin Wolf | if (ret) {
|
1865 | 797ac58c | Kevin Wolf | sum_alloc += num; |
1866 | 797ac58c | Kevin Wolf | } |
1867 | 797ac58c | Kevin Wolf | if (num == 0) { |
1868 | 797ac58c | Kevin Wolf | nb_sectors -= remaining; |
1869 | 797ac58c | Kevin Wolf | remaining = 0;
|
1870 | 797ac58c | Kevin Wolf | } |
1871 | 797ac58c | Kevin Wolf | } |
1872 | 797ac58c | Kevin Wolf | |
1873 | 797ac58c | Kevin Wolf | cvtstr(offset, s1, sizeof(s1));
|
1874 | 797ac58c | Kevin Wolf | |
1875 | 797ac58c | Kevin Wolf | printf("%d/%d sectors allocated at offset %s\n",
|
1876 | 797ac58c | Kevin Wolf | sum_alloc, nb_sectors, s1); |
1877 | 797ac58c | Kevin Wolf | return 0; |
1878 | 797ac58c | Kevin Wolf | } |
1879 | 797ac58c | Kevin Wolf | |
1880 | 797ac58c | Kevin Wolf | static const cmdinfo_t alloc_cmd = { |
1881 | 797ac58c | Kevin Wolf | .name = "alloc",
|
1882 | 797ac58c | Kevin Wolf | .altname = "a",
|
1883 | 797ac58c | Kevin Wolf | .argmin = 1,
|
1884 | 797ac58c | Kevin Wolf | .argmax = 2,
|
1885 | 797ac58c | Kevin Wolf | .cfunc = alloc_f, |
1886 | 797ac58c | Kevin Wolf | .args = "off [sectors]",
|
1887 | 797ac58c | Kevin Wolf | .oneline = "checks if a sector is present in the file",
|
1888 | 797ac58c | Kevin Wolf | }; |
1889 | 797ac58c | Kevin Wolf | |
1890 | 797ac58c | Kevin Wolf | |
1891 | 797ac58c | Kevin Wolf | static int map_is_allocated(BlockDriverState *bs, int64_t sector_num, |
1892 | 797ac58c | Kevin Wolf | int64_t nb_sectors, int64_t *pnum) |
1893 | 797ac58c | Kevin Wolf | { |
1894 | 797ac58c | Kevin Wolf | int num, num_checked;
|
1895 | 797ac58c | Kevin Wolf | int ret, firstret;
|
1896 | 797ac58c | Kevin Wolf | |
1897 | 797ac58c | Kevin Wolf | num_checked = MIN(nb_sectors, INT_MAX); |
1898 | 797ac58c | Kevin Wolf | ret = bdrv_is_allocated(bs, sector_num, num_checked, &num); |
1899 | 797ac58c | Kevin Wolf | if (ret < 0) { |
1900 | 797ac58c | Kevin Wolf | return ret;
|
1901 | 797ac58c | Kevin Wolf | } |
1902 | 797ac58c | Kevin Wolf | |
1903 | 797ac58c | Kevin Wolf | firstret = ret; |
1904 | 797ac58c | Kevin Wolf | *pnum = num; |
1905 | 797ac58c | Kevin Wolf | |
1906 | 797ac58c | Kevin Wolf | while (nb_sectors > 0 && ret == firstret) { |
1907 | 797ac58c | Kevin Wolf | sector_num += num; |
1908 | 797ac58c | Kevin Wolf | nb_sectors -= num; |
1909 | 797ac58c | Kevin Wolf | |
1910 | 797ac58c | Kevin Wolf | num_checked = MIN(nb_sectors, INT_MAX); |
1911 | 797ac58c | Kevin Wolf | ret = bdrv_is_allocated(bs, sector_num, num_checked, &num); |
1912 | 797ac58c | Kevin Wolf | if (ret == firstret) {
|
1913 | 797ac58c | Kevin Wolf | *pnum += num; |
1914 | 797ac58c | Kevin Wolf | } else {
|
1915 | 797ac58c | Kevin Wolf | break;
|
1916 | 797ac58c | Kevin Wolf | } |
1917 | 797ac58c | Kevin Wolf | } |
1918 | 797ac58c | Kevin Wolf | |
1919 | 797ac58c | Kevin Wolf | return firstret;
|
1920 | 797ac58c | Kevin Wolf | } |
1921 | 797ac58c | Kevin Wolf | |
1922 | 797ac58c | Kevin Wolf | static int map_f(BlockDriverState *bs, int argc, char **argv) |
1923 | 797ac58c | Kevin Wolf | { |
1924 | 797ac58c | Kevin Wolf | int64_t offset; |
1925 | 797ac58c | Kevin Wolf | int64_t nb_sectors; |
1926 | 797ac58c | Kevin Wolf | char s1[64]; |
1927 | 797ac58c | Kevin Wolf | int64_t num; |
1928 | 797ac58c | Kevin Wolf | int ret;
|
1929 | 797ac58c | Kevin Wolf | const char *retstr; |
1930 | 797ac58c | Kevin Wolf | |
1931 | 797ac58c | Kevin Wolf | offset = 0;
|
1932 | 797ac58c | Kevin Wolf | nb_sectors = bs->total_sectors; |
1933 | 797ac58c | Kevin Wolf | |
1934 | 797ac58c | Kevin Wolf | do {
|
1935 | 797ac58c | Kevin Wolf | ret = map_is_allocated(bs, offset, nb_sectors, &num); |
1936 | 797ac58c | Kevin Wolf | if (ret < 0) { |
1937 | 797ac58c | Kevin Wolf | error_report("Failed to get allocation status: %s", strerror(-ret));
|
1938 | 797ac58c | Kevin Wolf | return 0; |
1939 | 797ac58c | Kevin Wolf | } |
1940 | 797ac58c | Kevin Wolf | |
1941 | 797ac58c | Kevin Wolf | retstr = ret ? " allocated" : "not allocated"; |
1942 | 797ac58c | Kevin Wolf | cvtstr(offset << 9ULL, s1, sizeof(s1)); |
1943 | 797ac58c | Kevin Wolf | printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s " |
1944 | 797ac58c | Kevin Wolf | "at offset %s (%d)\n",
|
1945 | 797ac58c | Kevin Wolf | offset << 9ULL, num, nb_sectors, retstr, s1, ret);
|
1946 | 797ac58c | Kevin Wolf | |
1947 | 797ac58c | Kevin Wolf | offset += num; |
1948 | 797ac58c | Kevin Wolf | nb_sectors -= num; |
1949 | 797ac58c | Kevin Wolf | } while (offset < bs->total_sectors);
|
1950 | 797ac58c | Kevin Wolf | |
1951 | 797ac58c | Kevin Wolf | return 0; |
1952 | 797ac58c | Kevin Wolf | } |
1953 | 797ac58c | Kevin Wolf | |
1954 | 797ac58c | Kevin Wolf | static const cmdinfo_t map_cmd = { |
1955 | 797ac58c | Kevin Wolf | .name = "map",
|
1956 | 797ac58c | Kevin Wolf | .argmin = 0,
|
1957 | 797ac58c | Kevin Wolf | .argmax = 0,
|
1958 | 797ac58c | Kevin Wolf | .cfunc = map_f, |
1959 | 797ac58c | Kevin Wolf | .args = "",
|
1960 | 797ac58c | Kevin Wolf | .oneline = "prints the allocated areas of a file",
|
1961 | 797ac58c | Kevin Wolf | }; |
1962 | 797ac58c | Kevin Wolf | |
1963 | 797ac58c | Kevin Wolf | static int break_f(BlockDriverState *bs, int argc, char **argv) |
1964 | 797ac58c | Kevin Wolf | { |
1965 | 797ac58c | Kevin Wolf | int ret;
|
1966 | 797ac58c | Kevin Wolf | |
1967 | 797ac58c | Kevin Wolf | ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]); |
1968 | 797ac58c | Kevin Wolf | if (ret < 0) { |
1969 | 797ac58c | Kevin Wolf | printf("Could not set breakpoint: %s\n", strerror(-ret));
|
1970 | 797ac58c | Kevin Wolf | } |
1971 | 797ac58c | Kevin Wolf | |
1972 | 797ac58c | Kevin Wolf | return 0; |
1973 | 797ac58c | Kevin Wolf | } |
1974 | 797ac58c | Kevin Wolf | |
1975 | 4cc70e93 | Fam Zheng | static int remove_break_f(BlockDriverState *bs, int argc, char **argv) |
1976 | 4cc70e93 | Fam Zheng | { |
1977 | 4cc70e93 | Fam Zheng | int ret;
|
1978 | 4cc70e93 | Fam Zheng | |
1979 | 4cc70e93 | Fam Zheng | ret = bdrv_debug_remove_breakpoint(bs, argv[1]);
|
1980 | 4cc70e93 | Fam Zheng | if (ret < 0) { |
1981 | 4cc70e93 | Fam Zheng | printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret)); |
1982 | 4cc70e93 | Fam Zheng | } |
1983 | 4cc70e93 | Fam Zheng | |
1984 | 4cc70e93 | Fam Zheng | return 0; |
1985 | 4cc70e93 | Fam Zheng | } |
1986 | 4cc70e93 | Fam Zheng | |
1987 | 797ac58c | Kevin Wolf | static const cmdinfo_t break_cmd = { |
1988 | 797ac58c | Kevin Wolf | .name = "break",
|
1989 | 797ac58c | Kevin Wolf | .argmin = 2,
|
1990 | 797ac58c | Kevin Wolf | .argmax = 2,
|
1991 | 797ac58c | Kevin Wolf | .cfunc = break_f, |
1992 | 797ac58c | Kevin Wolf | .args = "event tag",
|
1993 | 797ac58c | Kevin Wolf | .oneline = "sets a breakpoint on event and tags the stopped "
|
1994 | 797ac58c | Kevin Wolf | "request as tag",
|
1995 | 797ac58c | Kevin Wolf | }; |
1996 | 797ac58c | Kevin Wolf | |
1997 | 4cc70e93 | Fam Zheng | static const cmdinfo_t remove_break_cmd = { |
1998 | 4cc70e93 | Fam Zheng | .name = "remove_break",
|
1999 | 4cc70e93 | Fam Zheng | .argmin = 1,
|
2000 | 4cc70e93 | Fam Zheng | .argmax = 1,
|
2001 | 4cc70e93 | Fam Zheng | .cfunc = remove_break_f, |
2002 | 4cc70e93 | Fam Zheng | .args = "tag",
|
2003 | 4cc70e93 | Fam Zheng | .oneline = "remove a breakpoint by tag",
|
2004 | 4cc70e93 | Fam Zheng | }; |
2005 | 4cc70e93 | Fam Zheng | |
2006 | 797ac58c | Kevin Wolf | static int resume_f(BlockDriverState *bs, int argc, char **argv) |
2007 | 797ac58c | Kevin Wolf | { |
2008 | 797ac58c | Kevin Wolf | int ret;
|
2009 | 797ac58c | Kevin Wolf | |
2010 | 797ac58c | Kevin Wolf | ret = bdrv_debug_resume(bs, argv[1]);
|
2011 | 797ac58c | Kevin Wolf | if (ret < 0) { |
2012 | 797ac58c | Kevin Wolf | printf("Could not resume request: %s\n", strerror(-ret));
|
2013 | 797ac58c | Kevin Wolf | } |
2014 | 797ac58c | Kevin Wolf | |
2015 | 797ac58c | Kevin Wolf | return 0; |
2016 | 797ac58c | Kevin Wolf | } |
2017 | 797ac58c | Kevin Wolf | |
2018 | 797ac58c | Kevin Wolf | static const cmdinfo_t resume_cmd = { |
2019 | 797ac58c | Kevin Wolf | .name = "resume",
|
2020 | 797ac58c | Kevin Wolf | .argmin = 1,
|
2021 | 797ac58c | Kevin Wolf | .argmax = 1,
|
2022 | 797ac58c | Kevin Wolf | .cfunc = resume_f, |
2023 | 797ac58c | Kevin Wolf | .args = "tag",
|
2024 | 797ac58c | Kevin Wolf | .oneline = "resumes the request tagged as tag",
|
2025 | 797ac58c | Kevin Wolf | }; |
2026 | 797ac58c | Kevin Wolf | |
2027 | 797ac58c | Kevin Wolf | static int wait_break_f(BlockDriverState *bs, int argc, char **argv) |
2028 | 797ac58c | Kevin Wolf | { |
2029 | 797ac58c | Kevin Wolf | while (!bdrv_debug_is_suspended(bs, argv[1])) { |
2030 | 797ac58c | Kevin Wolf | qemu_aio_wait(); |
2031 | 797ac58c | Kevin Wolf | } |
2032 | 797ac58c | Kevin Wolf | |
2033 | 797ac58c | Kevin Wolf | return 0; |
2034 | 797ac58c | Kevin Wolf | } |
2035 | 797ac58c | Kevin Wolf | |
2036 | 797ac58c | Kevin Wolf | static const cmdinfo_t wait_break_cmd = { |
2037 | 797ac58c | Kevin Wolf | .name = "wait_break",
|
2038 | 797ac58c | Kevin Wolf | .argmin = 1,
|
2039 | 797ac58c | Kevin Wolf | .argmax = 1,
|
2040 | 797ac58c | Kevin Wolf | .cfunc = wait_break_f, |
2041 | 797ac58c | Kevin Wolf | .args = "tag",
|
2042 | 797ac58c | Kevin Wolf | .oneline = "waits for the suspension of a request",
|
2043 | 797ac58c | Kevin Wolf | }; |
2044 | 797ac58c | Kevin Wolf | |
2045 | 797ac58c | Kevin Wolf | static int abort_f(BlockDriverState *bs, int argc, char **argv) |
2046 | 797ac58c | Kevin Wolf | { |
2047 | 797ac58c | Kevin Wolf | abort(); |
2048 | 797ac58c | Kevin Wolf | } |
2049 | 797ac58c | Kevin Wolf | |
2050 | 797ac58c | Kevin Wolf | static const cmdinfo_t abort_cmd = { |
2051 | 797ac58c | Kevin Wolf | .name = "abort",
|
2052 | 797ac58c | Kevin Wolf | .cfunc = abort_f, |
2053 | 797ac58c | Kevin Wolf | .flags = CMD_NOFILE_OK, |
2054 | 797ac58c | Kevin Wolf | .oneline = "simulate a program crash using abort(3)",
|
2055 | 797ac58c | Kevin Wolf | }; |
2056 | 797ac58c | Kevin Wolf | |
2057 | cd33d02a | Kevin Wolf | static void sleep_cb(void *opaque) |
2058 | cd33d02a | Kevin Wolf | { |
2059 | cd33d02a | Kevin Wolf | bool *expired = opaque;
|
2060 | cd33d02a | Kevin Wolf | *expired = true;
|
2061 | cd33d02a | Kevin Wolf | } |
2062 | cd33d02a | Kevin Wolf | |
2063 | cd33d02a | Kevin Wolf | static int sleep_f(BlockDriverState *bs, int argc, char **argv) |
2064 | cd33d02a | Kevin Wolf | { |
2065 | cd33d02a | Kevin Wolf | char *endptr;
|
2066 | cd33d02a | Kevin Wolf | long ms;
|
2067 | cd33d02a | Kevin Wolf | struct QEMUTimer *timer;
|
2068 | cd33d02a | Kevin Wolf | bool expired = false; |
2069 | cd33d02a | Kevin Wolf | |
2070 | cd33d02a | Kevin Wolf | ms = strtol(argv[1], &endptr, 0); |
2071 | cd33d02a | Kevin Wolf | if (ms < 0 || *endptr != '\0') { |
2072 | cd33d02a | Kevin Wolf | printf("%s is not a valid number\n", argv[1]); |
2073 | cd33d02a | Kevin Wolf | return 0; |
2074 | cd33d02a | Kevin Wolf | } |
2075 | cd33d02a | Kevin Wolf | |
2076 | cd33d02a | Kevin Wolf | timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired); |
2077 | cd33d02a | Kevin Wolf | timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms); |
2078 | cd33d02a | Kevin Wolf | |
2079 | cd33d02a | Kevin Wolf | while (!expired) {
|
2080 | cd33d02a | Kevin Wolf | main_loop_wait(false);
|
2081 | cd33d02a | Kevin Wolf | } |
2082 | cd33d02a | Kevin Wolf | |
2083 | cd33d02a | Kevin Wolf | timer_free(timer); |
2084 | cd33d02a | Kevin Wolf | |
2085 | cd33d02a | Kevin Wolf | return 0; |
2086 | cd33d02a | Kevin Wolf | } |
2087 | cd33d02a | Kevin Wolf | |
2088 | cd33d02a | Kevin Wolf | static const cmdinfo_t sleep_cmd = { |
2089 | cd33d02a | Kevin Wolf | .name = "sleep",
|
2090 | cd33d02a | Kevin Wolf | .argmin = 1,
|
2091 | cd33d02a | Kevin Wolf | .argmax = 1,
|
2092 | cd33d02a | Kevin Wolf | .cfunc = sleep_f, |
2093 | cd33d02a | Kevin Wolf | .flags = CMD_NOFILE_OK, |
2094 | cd33d02a | Kevin Wolf | .oneline = "waits for the given value in milliseconds",
|
2095 | cd33d02a | Kevin Wolf | }; |
2096 | cd33d02a | Kevin Wolf | |
2097 | f18a834a | Kevin Wolf | static void help_oneline(const char *cmd, const cmdinfo_t *ct) |
2098 | f18a834a | Kevin Wolf | { |
2099 | f18a834a | Kevin Wolf | if (cmd) {
|
2100 | f18a834a | Kevin Wolf | printf("%s ", cmd);
|
2101 | f18a834a | Kevin Wolf | } else {
|
2102 | f18a834a | Kevin Wolf | printf("%s ", ct->name);
|
2103 | f18a834a | Kevin Wolf | if (ct->altname) {
|
2104 | f18a834a | Kevin Wolf | printf("(or %s) ", ct->altname);
|
2105 | f18a834a | Kevin Wolf | } |
2106 | f18a834a | Kevin Wolf | } |
2107 | f18a834a | Kevin Wolf | |
2108 | f18a834a | Kevin Wolf | if (ct->args) {
|
2109 | f18a834a | Kevin Wolf | printf("%s ", ct->args);
|
2110 | f18a834a | Kevin Wolf | } |
2111 | f18a834a | Kevin Wolf | printf("-- %s\n", ct->oneline);
|
2112 | f18a834a | Kevin Wolf | } |
2113 | f18a834a | Kevin Wolf | |
2114 | f18a834a | Kevin Wolf | static void help_onecmd(const char *cmd, const cmdinfo_t *ct) |
2115 | f18a834a | Kevin Wolf | { |
2116 | f18a834a | Kevin Wolf | help_oneline(cmd, ct); |
2117 | f18a834a | Kevin Wolf | if (ct->help) {
|
2118 | f18a834a | Kevin Wolf | ct->help(); |
2119 | f18a834a | Kevin Wolf | } |
2120 | f18a834a | Kevin Wolf | } |
2121 | f18a834a | Kevin Wolf | |
2122 | f18a834a | Kevin Wolf | static void help_all(void) |
2123 | f18a834a | Kevin Wolf | { |
2124 | f18a834a | Kevin Wolf | const cmdinfo_t *ct;
|
2125 | f18a834a | Kevin Wolf | |
2126 | f18a834a | Kevin Wolf | for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
|
2127 | f18a834a | Kevin Wolf | help_oneline(ct->name, ct); |
2128 | f18a834a | Kevin Wolf | } |
2129 | f18a834a | Kevin Wolf | printf("\nUse 'help commandname' for extended help.\n");
|
2130 | f18a834a | Kevin Wolf | } |
2131 | f18a834a | Kevin Wolf | |
2132 | f18a834a | Kevin Wolf | static int help_f(BlockDriverState *bs, int argc, char **argv) |
2133 | f18a834a | Kevin Wolf | { |
2134 | f18a834a | Kevin Wolf | const cmdinfo_t *ct;
|
2135 | f18a834a | Kevin Wolf | |
2136 | f18a834a | Kevin Wolf | if (argc == 1) { |
2137 | f18a834a | Kevin Wolf | help_all(); |
2138 | f18a834a | Kevin Wolf | return 0; |
2139 | f18a834a | Kevin Wolf | } |
2140 | f18a834a | Kevin Wolf | |
2141 | f18a834a | Kevin Wolf | ct = find_command(argv[1]);
|
2142 | f18a834a | Kevin Wolf | if (ct == NULL) { |
2143 | f18a834a | Kevin Wolf | printf("command %s not found\n", argv[1]); |
2144 | f18a834a | Kevin Wolf | return 0; |
2145 | f18a834a | Kevin Wolf | } |
2146 | f18a834a | Kevin Wolf | |
2147 | f18a834a | Kevin Wolf | help_onecmd(argv[1], ct);
|
2148 | f18a834a | Kevin Wolf | return 0; |
2149 | f18a834a | Kevin Wolf | } |
2150 | f18a834a | Kevin Wolf | |
2151 | f18a834a | Kevin Wolf | static const cmdinfo_t help_cmd = { |
2152 | f18a834a | Kevin Wolf | .name = "help",
|
2153 | f18a834a | Kevin Wolf | .altname = "?",
|
2154 | f18a834a | Kevin Wolf | .cfunc = help_f, |
2155 | f18a834a | Kevin Wolf | .argmin = 0,
|
2156 | f18a834a | Kevin Wolf | .argmax = 1,
|
2157 | f18a834a | Kevin Wolf | .flags = CMD_FLAG_GLOBAL, |
2158 | f18a834a | Kevin Wolf | .args = "[command]",
|
2159 | f18a834a | Kevin Wolf | .oneline = "help for one or all commands",
|
2160 | f18a834a | Kevin Wolf | }; |
2161 | f18a834a | Kevin Wolf | |
2162 | 3d21994f | Kevin Wolf | bool qemuio_command(BlockDriverState *bs, const char *cmd) |
2163 | dd583296 | Kevin Wolf | { |
2164 | dd583296 | Kevin Wolf | char *input;
|
2165 | dd583296 | Kevin Wolf | const cmdinfo_t *ct;
|
2166 | dd583296 | Kevin Wolf | char **v;
|
2167 | dd583296 | Kevin Wolf | int c;
|
2168 | dd583296 | Kevin Wolf | bool done = false; |
2169 | dd583296 | Kevin Wolf | |
2170 | dd583296 | Kevin Wolf | input = g_strdup(cmd); |
2171 | dd583296 | Kevin Wolf | v = breakline(input, &c); |
2172 | dd583296 | Kevin Wolf | if (c) {
|
2173 | dd583296 | Kevin Wolf | ct = find_command(v[0]);
|
2174 | dd583296 | Kevin Wolf | if (ct) {
|
2175 | 3d21994f | Kevin Wolf | done = command(bs, ct, c, v); |
2176 | dd583296 | Kevin Wolf | } else {
|
2177 | dd583296 | Kevin Wolf | fprintf(stderr, "command \"%s\" not found\n", v[0]); |
2178 | dd583296 | Kevin Wolf | } |
2179 | dd583296 | Kevin Wolf | } |
2180 | dd583296 | Kevin Wolf | g_free(input); |
2181 | dd583296 | Kevin Wolf | g_free(v); |
2182 | dd583296 | Kevin Wolf | |
2183 | dd583296 | Kevin Wolf | return done;
|
2184 | dd583296 | Kevin Wolf | } |
2185 | dd583296 | Kevin Wolf | |
2186 | 797ac58c | Kevin Wolf | static void __attribute((constructor)) init_qemuio_commands(void) |
2187 | 797ac58c | Kevin Wolf | { |
2188 | 797ac58c | Kevin Wolf | /* initialize commands */
|
2189 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&help_cmd); |
2190 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&read_cmd); |
2191 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&readv_cmd); |
2192 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&write_cmd); |
2193 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&writev_cmd); |
2194 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&multiwrite_cmd); |
2195 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&aio_read_cmd); |
2196 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&aio_write_cmd); |
2197 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&aio_flush_cmd); |
2198 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&flush_cmd); |
2199 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&truncate_cmd); |
2200 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&length_cmd); |
2201 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&info_cmd); |
2202 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&discard_cmd); |
2203 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&alloc_cmd); |
2204 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&map_cmd); |
2205 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&break_cmd); |
2206 | 4cc70e93 | Fam Zheng | qemuio_add_command(&remove_break_cmd); |
2207 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&resume_cmd); |
2208 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&wait_break_cmd); |
2209 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&abort_cmd); |
2210 | cd33d02a | Kevin Wolf | qemuio_add_command(&sleep_cmd); |
2211 | 797ac58c | Kevin Wolf | } |