Statistics
| Branch: | Revision:

root / qemu-io-cmds.c @ 6a1751b7

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