xref: /qemu/qemu-io-cmds.c (revision dd583296)
1797ac58cSKevin Wolf /*
2797ac58cSKevin Wolf  * Command line utility to exercise the QEMU I/O path.
3797ac58cSKevin Wolf  *
4797ac58cSKevin Wolf  * Copyright (C) 2009 Red Hat, Inc.
5797ac58cSKevin Wolf  * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6797ac58cSKevin Wolf  *
7797ac58cSKevin Wolf  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8797ac58cSKevin Wolf  * See the COPYING file in the top-level directory.
9797ac58cSKevin Wolf  */
10797ac58cSKevin Wolf 
11797ac58cSKevin Wolf #include "qemu-common.h"
12797ac58cSKevin Wolf #include "block/block_int.h"
13797ac58cSKevin Wolf #include "cmd.h"
14797ac58cSKevin Wolf 
15797ac58cSKevin Wolf #define CMD_NOFILE_OK   0x01
16797ac58cSKevin Wolf 
17797ac58cSKevin Wolf int qemuio_misalign;
18797ac58cSKevin Wolf 
19797ac58cSKevin Wolf static int64_t cvtnum(const char *s)
20797ac58cSKevin Wolf {
21797ac58cSKevin Wolf     char *end;
22797ac58cSKevin Wolf     return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
23797ac58cSKevin Wolf }
24797ac58cSKevin Wolf 
25797ac58cSKevin Wolf /*
26797ac58cSKevin Wolf  * Parse the pattern argument to various sub-commands.
27797ac58cSKevin Wolf  *
28797ac58cSKevin Wolf  * Because the pattern is used as an argument to memset it must evaluate
29797ac58cSKevin Wolf  * to an unsigned integer that fits into a single byte.
30797ac58cSKevin Wolf  */
31797ac58cSKevin Wolf static int parse_pattern(const char *arg)
32797ac58cSKevin Wolf {
33797ac58cSKevin Wolf     char *endptr = NULL;
34797ac58cSKevin Wolf     long pattern;
35797ac58cSKevin Wolf 
36797ac58cSKevin Wolf     pattern = strtol(arg, &endptr, 0);
37797ac58cSKevin Wolf     if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
38797ac58cSKevin Wolf         printf("%s is not a valid pattern byte\n", arg);
39797ac58cSKevin Wolf         return -1;
40797ac58cSKevin Wolf     }
41797ac58cSKevin Wolf 
42797ac58cSKevin Wolf     return pattern;
43797ac58cSKevin Wolf }
44797ac58cSKevin Wolf 
45797ac58cSKevin Wolf /*
46797ac58cSKevin Wolf  * Memory allocation helpers.
47797ac58cSKevin Wolf  *
48797ac58cSKevin Wolf  * Make sure memory is aligned by default, or purposefully misaligned if
49797ac58cSKevin Wolf  * that is specified on the command line.
50797ac58cSKevin Wolf  */
51797ac58cSKevin Wolf 
52797ac58cSKevin Wolf #define MISALIGN_OFFSET     16
53797ac58cSKevin Wolf static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
54797ac58cSKevin Wolf {
55797ac58cSKevin Wolf     void *buf;
56797ac58cSKevin Wolf 
57797ac58cSKevin Wolf     if (qemuio_misalign) {
58797ac58cSKevin Wolf         len += MISALIGN_OFFSET;
59797ac58cSKevin Wolf     }
60797ac58cSKevin Wolf     buf = qemu_blockalign(bs, len);
61797ac58cSKevin Wolf     memset(buf, pattern, len);
62797ac58cSKevin Wolf     if (qemuio_misalign) {
63797ac58cSKevin Wolf         buf += MISALIGN_OFFSET;
64797ac58cSKevin Wolf     }
65797ac58cSKevin Wolf     return buf;
66797ac58cSKevin Wolf }
67797ac58cSKevin Wolf 
68797ac58cSKevin Wolf static void qemu_io_free(void *p)
69797ac58cSKevin Wolf {
70797ac58cSKevin Wolf     if (qemuio_misalign) {
71797ac58cSKevin Wolf         p -= MISALIGN_OFFSET;
72797ac58cSKevin Wolf     }
73797ac58cSKevin Wolf     qemu_vfree(p);
74797ac58cSKevin Wolf }
75797ac58cSKevin Wolf 
76797ac58cSKevin Wolf static void dump_buffer(const void *buffer, int64_t offset, int len)
77797ac58cSKevin Wolf {
78797ac58cSKevin Wolf     int i, j;
79797ac58cSKevin Wolf     const uint8_t *p;
80797ac58cSKevin Wolf 
81797ac58cSKevin Wolf     for (i = 0, p = buffer; i < len; i += 16) {
82797ac58cSKevin Wolf         const uint8_t *s = p;
83797ac58cSKevin Wolf 
84797ac58cSKevin Wolf         printf("%08" PRIx64 ":  ", offset + i);
85797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, p++) {
86797ac58cSKevin Wolf             printf("%02x ", *p);
87797ac58cSKevin Wolf         }
88797ac58cSKevin Wolf         printf(" ");
89797ac58cSKevin Wolf         for (j = 0; j < 16 && i + j < len; j++, s++) {
90797ac58cSKevin Wolf             if (isalnum(*s)) {
91797ac58cSKevin Wolf                 printf("%c", *s);
92797ac58cSKevin Wolf             } else {
93797ac58cSKevin Wolf                 printf(".");
94797ac58cSKevin Wolf             }
95797ac58cSKevin Wolf         }
96797ac58cSKevin Wolf         printf("\n");
97797ac58cSKevin Wolf     }
98797ac58cSKevin Wolf }
99797ac58cSKevin Wolf 
100797ac58cSKevin Wolf static void print_report(const char *op, struct timeval *t, int64_t offset,
101797ac58cSKevin Wolf                          int count, int total, int cnt, int Cflag)
102797ac58cSKevin Wolf {
103797ac58cSKevin Wolf     char s1[64], s2[64], ts[64];
104797ac58cSKevin Wolf 
105797ac58cSKevin Wolf     timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
106797ac58cSKevin Wolf     if (!Cflag) {
107797ac58cSKevin Wolf         cvtstr((double)total, s1, sizeof(s1));
108797ac58cSKevin Wolf         cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
109797ac58cSKevin Wolf         printf("%s %d/%d bytes at offset %" PRId64 "\n",
110797ac58cSKevin Wolf                op, total, count, offset);
111797ac58cSKevin Wolf         printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
112797ac58cSKevin Wolf                s1, cnt, ts, s2, tdiv((double)cnt, *t));
113797ac58cSKevin Wolf     } else {/* bytes,ops,time,bytes/sec,ops/sec */
114797ac58cSKevin Wolf         printf("%d,%d,%s,%.3f,%.3f\n",
115797ac58cSKevin Wolf             total, cnt, ts,
116797ac58cSKevin Wolf             tdiv((double)total, *t),
117797ac58cSKevin Wolf             tdiv((double)cnt, *t));
118797ac58cSKevin Wolf     }
119797ac58cSKevin Wolf }
120797ac58cSKevin Wolf 
121797ac58cSKevin Wolf /*
122797ac58cSKevin Wolf  * Parse multiple length statements for vectored I/O, and construct an I/O
123797ac58cSKevin Wolf  * vector matching it.
124797ac58cSKevin Wolf  */
125797ac58cSKevin Wolf static void *
126797ac58cSKevin Wolf create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
127797ac58cSKevin Wolf              int pattern)
128797ac58cSKevin Wolf {
129797ac58cSKevin Wolf     size_t *sizes = g_new0(size_t, nr_iov);
130797ac58cSKevin Wolf     size_t count = 0;
131797ac58cSKevin Wolf     void *buf = NULL;
132797ac58cSKevin Wolf     void *p;
133797ac58cSKevin Wolf     int i;
134797ac58cSKevin Wolf 
135797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
136797ac58cSKevin Wolf         char *arg = argv[i];
137797ac58cSKevin Wolf         int64_t len;
138797ac58cSKevin Wolf 
139797ac58cSKevin Wolf         len = cvtnum(arg);
140797ac58cSKevin Wolf         if (len < 0) {
141797ac58cSKevin Wolf             printf("non-numeric length argument -- %s\n", arg);
142797ac58cSKevin Wolf             goto fail;
143797ac58cSKevin Wolf         }
144797ac58cSKevin Wolf 
145797ac58cSKevin Wolf         /* should be SIZE_T_MAX, but that doesn't exist */
146797ac58cSKevin Wolf         if (len > INT_MAX) {
147797ac58cSKevin Wolf             printf("too large length argument -- %s\n", arg);
148797ac58cSKevin Wolf             goto fail;
149797ac58cSKevin Wolf         }
150797ac58cSKevin Wolf 
151797ac58cSKevin Wolf         if (len & 0x1ff) {
152797ac58cSKevin Wolf             printf("length argument %" PRId64
153797ac58cSKevin Wolf                    " is not sector aligned\n", len);
154797ac58cSKevin Wolf             goto fail;
155797ac58cSKevin Wolf         }
156797ac58cSKevin Wolf 
157797ac58cSKevin Wolf         sizes[i] = len;
158797ac58cSKevin Wolf         count += len;
159797ac58cSKevin Wolf     }
160797ac58cSKevin Wolf 
161797ac58cSKevin Wolf     qemu_iovec_init(qiov, nr_iov);
162797ac58cSKevin Wolf 
163797ac58cSKevin Wolf     buf = p = qemu_io_alloc(bs, count, pattern);
164797ac58cSKevin Wolf 
165797ac58cSKevin Wolf     for (i = 0; i < nr_iov; i++) {
166797ac58cSKevin Wolf         qemu_iovec_add(qiov, p, sizes[i]);
167797ac58cSKevin Wolf         p += sizes[i];
168797ac58cSKevin Wolf     }
169797ac58cSKevin Wolf 
170797ac58cSKevin Wolf fail:
171797ac58cSKevin Wolf     g_free(sizes);
172797ac58cSKevin Wolf     return buf;
173797ac58cSKevin Wolf }
174797ac58cSKevin Wolf 
175797ac58cSKevin Wolf static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
176797ac58cSKevin Wolf                    int *total)
177797ac58cSKevin Wolf {
178797ac58cSKevin Wolf     int ret;
179797ac58cSKevin Wolf 
180797ac58cSKevin Wolf     ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
181797ac58cSKevin Wolf     if (ret < 0) {
182797ac58cSKevin Wolf         return ret;
183797ac58cSKevin Wolf     }
184797ac58cSKevin Wolf     *total = count;
185797ac58cSKevin Wolf     return 1;
186797ac58cSKevin Wolf }
187797ac58cSKevin Wolf 
188797ac58cSKevin Wolf static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
189797ac58cSKevin Wolf                     int *total)
190797ac58cSKevin Wolf {
191797ac58cSKevin Wolf     int ret;
192797ac58cSKevin Wolf 
193797ac58cSKevin Wolf     ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
194797ac58cSKevin Wolf     if (ret < 0) {
195797ac58cSKevin Wolf         return ret;
196797ac58cSKevin Wolf     }
197797ac58cSKevin Wolf     *total = count;
198797ac58cSKevin Wolf     return 1;
199797ac58cSKevin Wolf }
200797ac58cSKevin Wolf 
201797ac58cSKevin Wolf static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
202797ac58cSKevin Wolf                     int *total)
203797ac58cSKevin Wolf {
204797ac58cSKevin Wolf     *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
205797ac58cSKevin Wolf     if (*total < 0) {
206797ac58cSKevin Wolf         return *total;
207797ac58cSKevin Wolf     }
208797ac58cSKevin Wolf     return 1;
209797ac58cSKevin Wolf }
210797ac58cSKevin Wolf 
211797ac58cSKevin Wolf static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
212797ac58cSKevin Wolf                      int *total)
213797ac58cSKevin Wolf {
214797ac58cSKevin Wolf     *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
215797ac58cSKevin Wolf     if (*total < 0) {
216797ac58cSKevin Wolf         return *total;
217797ac58cSKevin Wolf     }
218797ac58cSKevin Wolf     return 1;
219797ac58cSKevin Wolf }
220797ac58cSKevin Wolf 
221797ac58cSKevin Wolf typedef struct {
222797ac58cSKevin Wolf     BlockDriverState *bs;
223797ac58cSKevin Wolf     int64_t offset;
224797ac58cSKevin Wolf     int count;
225797ac58cSKevin Wolf     int *total;
226797ac58cSKevin Wolf     int ret;
227797ac58cSKevin Wolf     bool done;
228797ac58cSKevin Wolf } CoWriteZeroes;
229797ac58cSKevin Wolf 
230797ac58cSKevin Wolf static void coroutine_fn co_write_zeroes_entry(void *opaque)
231797ac58cSKevin Wolf {
232797ac58cSKevin Wolf     CoWriteZeroes *data = opaque;
233797ac58cSKevin Wolf 
234797ac58cSKevin Wolf     data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
235797ac58cSKevin Wolf                                      data->count / BDRV_SECTOR_SIZE);
236797ac58cSKevin Wolf     data->done = true;
237797ac58cSKevin Wolf     if (data->ret < 0) {
238797ac58cSKevin Wolf         *data->total = data->ret;
239797ac58cSKevin Wolf         return;
240797ac58cSKevin Wolf     }
241797ac58cSKevin Wolf 
242797ac58cSKevin Wolf     *data->total = data->count;
243797ac58cSKevin Wolf }
244797ac58cSKevin Wolf 
245797ac58cSKevin Wolf static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
246797ac58cSKevin Wolf                               int *total)
247797ac58cSKevin Wolf {
248797ac58cSKevin Wolf     Coroutine *co;
249797ac58cSKevin Wolf     CoWriteZeroes data = {
250797ac58cSKevin Wolf         .bs     = bs,
251797ac58cSKevin Wolf         .offset = offset,
252797ac58cSKevin Wolf         .count  = count,
253797ac58cSKevin Wolf         .total  = total,
254797ac58cSKevin Wolf         .done   = false,
255797ac58cSKevin Wolf     };
256797ac58cSKevin Wolf 
257797ac58cSKevin Wolf     co = qemu_coroutine_create(co_write_zeroes_entry);
258797ac58cSKevin Wolf     qemu_coroutine_enter(co, &data);
259797ac58cSKevin Wolf     while (!data.done) {
260797ac58cSKevin Wolf         qemu_aio_wait();
261797ac58cSKevin Wolf     }
262797ac58cSKevin Wolf     if (data.ret < 0) {
263797ac58cSKevin Wolf         return data.ret;
264797ac58cSKevin Wolf     } else {
265797ac58cSKevin Wolf         return 1;
266797ac58cSKevin Wolf     }
267797ac58cSKevin Wolf }
268797ac58cSKevin Wolf 
269797ac58cSKevin Wolf static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
270797ac58cSKevin Wolf                                int count, int *total)
271797ac58cSKevin Wolf {
272797ac58cSKevin Wolf     int ret;
273797ac58cSKevin Wolf 
274797ac58cSKevin Wolf     ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
275797ac58cSKevin Wolf     if (ret < 0) {
276797ac58cSKevin Wolf         return ret;
277797ac58cSKevin Wolf     }
278797ac58cSKevin Wolf     *total = count;
279797ac58cSKevin Wolf     return 1;
280797ac58cSKevin Wolf }
281797ac58cSKevin Wolf 
282797ac58cSKevin Wolf static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
283797ac58cSKevin Wolf                            int count, int *total)
284797ac58cSKevin Wolf {
285797ac58cSKevin Wolf     *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
286797ac58cSKevin Wolf     if (*total < 0) {
287797ac58cSKevin Wolf         return *total;
288797ac58cSKevin Wolf     }
289797ac58cSKevin Wolf     return 1;
290797ac58cSKevin Wolf }
291797ac58cSKevin Wolf 
292797ac58cSKevin Wolf static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
293797ac58cSKevin Wolf                            int count, int *total)
294797ac58cSKevin Wolf {
295797ac58cSKevin Wolf     *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
296797ac58cSKevin Wolf     if (*total < 0) {
297797ac58cSKevin Wolf         return *total;
298797ac58cSKevin Wolf     }
299797ac58cSKevin Wolf     return 1;
300797ac58cSKevin Wolf }
301797ac58cSKevin Wolf 
302797ac58cSKevin Wolf #define NOT_DONE 0x7fffffff
303797ac58cSKevin Wolf static void aio_rw_done(void *opaque, int ret)
304797ac58cSKevin Wolf {
305797ac58cSKevin Wolf     *(int *)opaque = ret;
306797ac58cSKevin Wolf }
307797ac58cSKevin Wolf 
308797ac58cSKevin Wolf static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
309797ac58cSKevin Wolf                         int64_t offset, int *total)
310797ac58cSKevin Wolf {
311797ac58cSKevin Wolf     int async_ret = NOT_DONE;
312797ac58cSKevin Wolf 
313797ac58cSKevin Wolf     bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
314797ac58cSKevin Wolf                    aio_rw_done, &async_ret);
315797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
316797ac58cSKevin Wolf         main_loop_wait(false);
317797ac58cSKevin Wolf     }
318797ac58cSKevin Wolf 
319797ac58cSKevin Wolf     *total = qiov->size;
320797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
321797ac58cSKevin Wolf }
322797ac58cSKevin Wolf 
323797ac58cSKevin Wolf static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
324797ac58cSKevin Wolf                          int64_t offset, int *total)
325797ac58cSKevin Wolf {
326797ac58cSKevin Wolf     int async_ret = NOT_DONE;
327797ac58cSKevin Wolf 
328797ac58cSKevin Wolf     bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
329797ac58cSKevin Wolf                     aio_rw_done, &async_ret);
330797ac58cSKevin Wolf     while (async_ret == NOT_DONE) {
331797ac58cSKevin Wolf         main_loop_wait(false);
332797ac58cSKevin Wolf     }
333797ac58cSKevin Wolf 
334797ac58cSKevin Wolf     *total = qiov->size;
335797ac58cSKevin Wolf     return async_ret < 0 ? async_ret : 1;
336797ac58cSKevin Wolf }
337797ac58cSKevin Wolf 
338797ac58cSKevin Wolf struct multiwrite_async_ret {
339797ac58cSKevin Wolf     int num_done;
340797ac58cSKevin Wolf     int error;
341797ac58cSKevin Wolf };
342797ac58cSKevin Wolf 
343797ac58cSKevin Wolf static void multiwrite_cb(void *opaque, int ret)
344797ac58cSKevin Wolf {
345797ac58cSKevin Wolf     struct multiwrite_async_ret *async_ret = opaque;
346797ac58cSKevin Wolf 
347797ac58cSKevin Wolf     async_ret->num_done++;
348797ac58cSKevin Wolf     if (ret < 0) {
349797ac58cSKevin Wolf         async_ret->error = ret;
350797ac58cSKevin Wolf     }
351797ac58cSKevin Wolf }
352797ac58cSKevin Wolf 
353797ac58cSKevin Wolf static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
354797ac58cSKevin Wolf                              int num_reqs, int *total)
355797ac58cSKevin Wolf {
356797ac58cSKevin Wolf     int i, ret;
357797ac58cSKevin Wolf     struct multiwrite_async_ret async_ret = {
358797ac58cSKevin Wolf         .num_done = 0,
359797ac58cSKevin Wolf         .error = 0,
360797ac58cSKevin Wolf     };
361797ac58cSKevin Wolf 
362797ac58cSKevin Wolf     *total = 0;
363797ac58cSKevin Wolf     for (i = 0; i < num_reqs; i++) {
364797ac58cSKevin Wolf         reqs[i].cb = multiwrite_cb;
365797ac58cSKevin Wolf         reqs[i].opaque = &async_ret;
366797ac58cSKevin Wolf         *total += reqs[i].qiov->size;
367797ac58cSKevin Wolf     }
368797ac58cSKevin Wolf 
369797ac58cSKevin Wolf     ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
370797ac58cSKevin Wolf     if (ret < 0) {
371797ac58cSKevin Wolf         return ret;
372797ac58cSKevin Wolf     }
373797ac58cSKevin Wolf 
374797ac58cSKevin Wolf     while (async_ret.num_done < num_reqs) {
375797ac58cSKevin Wolf         main_loop_wait(false);
376797ac58cSKevin Wolf     }
377797ac58cSKevin Wolf 
378797ac58cSKevin Wolf     return async_ret.error < 0 ? async_ret.error : 1;
379797ac58cSKevin Wolf }
380797ac58cSKevin Wolf 
381797ac58cSKevin Wolf static void read_help(void)
382797ac58cSKevin Wolf {
383797ac58cSKevin Wolf     printf(
384797ac58cSKevin Wolf "\n"
385797ac58cSKevin Wolf " reads a range of bytes from the given offset\n"
386797ac58cSKevin Wolf "\n"
387797ac58cSKevin Wolf " Example:\n"
388797ac58cSKevin Wolf " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
389797ac58cSKevin Wolf "\n"
390797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
391797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
392797ac58cSKevin Wolf " -b, -- read from the VM state rather than the virtual disk\n"
393797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
394797ac58cSKevin Wolf " -l, -- length for pattern verification (only with -P)\n"
395797ac58cSKevin Wolf " -p, -- use bdrv_pread to read the file\n"
396797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
397797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
398797ac58cSKevin Wolf " -s, -- start offset for pattern verification (only with -P)\n"
399797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
400797ac58cSKevin Wolf "\n");
401797ac58cSKevin Wolf }
402797ac58cSKevin Wolf 
403797ac58cSKevin Wolf static int read_f(BlockDriverState *bs, int argc, char **argv);
404797ac58cSKevin Wolf 
405797ac58cSKevin Wolf static const cmdinfo_t read_cmd = {
406797ac58cSKevin Wolf     .name       = "read",
407797ac58cSKevin Wolf     .altname    = "r",
408797ac58cSKevin Wolf     .cfunc      = read_f,
409797ac58cSKevin Wolf     .argmin     = 2,
410797ac58cSKevin Wolf     .argmax     = -1,
411797ac58cSKevin Wolf     .args       = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
412797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
413797ac58cSKevin Wolf     .help       = read_help,
414797ac58cSKevin Wolf };
415797ac58cSKevin Wolf 
416797ac58cSKevin Wolf static int read_f(BlockDriverState *bs, int argc, char **argv)
417797ac58cSKevin Wolf {
418797ac58cSKevin Wolf     struct timeval t1, t2;
419797ac58cSKevin Wolf     int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
420797ac58cSKevin Wolf     int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
421797ac58cSKevin Wolf     int c, cnt;
422797ac58cSKevin Wolf     char *buf;
423797ac58cSKevin Wolf     int64_t offset;
424797ac58cSKevin Wolf     int count;
425797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
426797ac58cSKevin Wolf     int total = 0;
427797ac58cSKevin Wolf     int pattern = 0, pattern_offset = 0, pattern_count = 0;
428797ac58cSKevin Wolf 
429797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
430797ac58cSKevin Wolf         switch (c) {
431797ac58cSKevin Wolf         case 'b':
432797ac58cSKevin Wolf             bflag = 1;
433797ac58cSKevin Wolf             break;
434797ac58cSKevin Wolf         case 'C':
435797ac58cSKevin Wolf             Cflag = 1;
436797ac58cSKevin Wolf             break;
437797ac58cSKevin Wolf         case 'l':
438797ac58cSKevin Wolf             lflag = 1;
439797ac58cSKevin Wolf             pattern_count = cvtnum(optarg);
440797ac58cSKevin Wolf             if (pattern_count < 0) {
441797ac58cSKevin Wolf                 printf("non-numeric length argument -- %s\n", optarg);
442797ac58cSKevin Wolf                 return 0;
443797ac58cSKevin Wolf             }
444797ac58cSKevin Wolf             break;
445797ac58cSKevin Wolf         case 'p':
446797ac58cSKevin Wolf             pflag = 1;
447797ac58cSKevin Wolf             break;
448797ac58cSKevin Wolf         case 'P':
449797ac58cSKevin Wolf             Pflag = 1;
450797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
451797ac58cSKevin Wolf             if (pattern < 0) {
452797ac58cSKevin Wolf                 return 0;
453797ac58cSKevin Wolf             }
454797ac58cSKevin Wolf             break;
455797ac58cSKevin Wolf         case 'q':
456797ac58cSKevin Wolf             qflag = 1;
457797ac58cSKevin Wolf             break;
458797ac58cSKevin Wolf         case 's':
459797ac58cSKevin Wolf             sflag = 1;
460797ac58cSKevin Wolf             pattern_offset = cvtnum(optarg);
461797ac58cSKevin Wolf             if (pattern_offset < 0) {
462797ac58cSKevin Wolf                 printf("non-numeric length argument -- %s\n", optarg);
463797ac58cSKevin Wolf                 return 0;
464797ac58cSKevin Wolf             }
465797ac58cSKevin Wolf             break;
466797ac58cSKevin Wolf         case 'v':
467797ac58cSKevin Wolf             vflag = 1;
468797ac58cSKevin Wolf             break;
469797ac58cSKevin Wolf         default:
470797ac58cSKevin Wolf             return command_usage(&read_cmd);
471797ac58cSKevin Wolf         }
472797ac58cSKevin Wolf     }
473797ac58cSKevin Wolf 
474797ac58cSKevin Wolf     if (optind != argc - 2) {
475797ac58cSKevin Wolf         return command_usage(&read_cmd);
476797ac58cSKevin Wolf     }
477797ac58cSKevin Wolf 
478797ac58cSKevin Wolf     if (bflag && pflag) {
479797ac58cSKevin Wolf         printf("-b and -p cannot be specified at the same time\n");
480797ac58cSKevin Wolf         return 0;
481797ac58cSKevin Wolf     }
482797ac58cSKevin Wolf 
483797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
484797ac58cSKevin Wolf     if (offset < 0) {
485797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
486797ac58cSKevin Wolf         return 0;
487797ac58cSKevin Wolf     }
488797ac58cSKevin Wolf 
489797ac58cSKevin Wolf     optind++;
490797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
491797ac58cSKevin Wolf     if (count < 0) {
492797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
493797ac58cSKevin Wolf         return 0;
494797ac58cSKevin Wolf     }
495797ac58cSKevin Wolf 
496797ac58cSKevin Wolf     if (!Pflag && (lflag || sflag)) {
497797ac58cSKevin Wolf         return command_usage(&read_cmd);
498797ac58cSKevin Wolf     }
499797ac58cSKevin Wolf 
500797ac58cSKevin Wolf     if (!lflag) {
501797ac58cSKevin Wolf         pattern_count = count - pattern_offset;
502797ac58cSKevin Wolf     }
503797ac58cSKevin Wolf 
504797ac58cSKevin Wolf     if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
505797ac58cSKevin Wolf         printf("pattern verification range exceeds end of read data\n");
506797ac58cSKevin Wolf         return 0;
507797ac58cSKevin Wolf     }
508797ac58cSKevin Wolf 
509797ac58cSKevin Wolf     if (!pflag) {
510797ac58cSKevin Wolf         if (offset & 0x1ff) {
511797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
512797ac58cSKevin Wolf                    offset);
513797ac58cSKevin Wolf             return 0;
514797ac58cSKevin Wolf         }
515797ac58cSKevin Wolf         if (count & 0x1ff) {
516797ac58cSKevin Wolf             printf("count %d is not sector aligned\n",
517797ac58cSKevin Wolf                    count);
518797ac58cSKevin Wolf             return 0;
519797ac58cSKevin Wolf         }
520797ac58cSKevin Wolf     }
521797ac58cSKevin Wolf 
522797ac58cSKevin Wolf     buf = qemu_io_alloc(bs, count, 0xab);
523797ac58cSKevin Wolf 
524797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
525797ac58cSKevin Wolf     if (pflag) {
526797ac58cSKevin Wolf         cnt = do_pread(bs, buf, offset, count, &total);
527797ac58cSKevin Wolf     } else if (bflag) {
528797ac58cSKevin Wolf         cnt = do_load_vmstate(bs, buf, offset, count, &total);
529797ac58cSKevin Wolf     } else {
530797ac58cSKevin Wolf         cnt = do_read(bs, buf, offset, count, &total);
531797ac58cSKevin Wolf     }
532797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
533797ac58cSKevin Wolf 
534797ac58cSKevin Wolf     if (cnt < 0) {
535797ac58cSKevin Wolf         printf("read failed: %s\n", strerror(-cnt));
536797ac58cSKevin Wolf         goto out;
537797ac58cSKevin Wolf     }
538797ac58cSKevin Wolf 
539797ac58cSKevin Wolf     if (Pflag) {
540797ac58cSKevin Wolf         void *cmp_buf = g_malloc(pattern_count);
541797ac58cSKevin Wolf         memset(cmp_buf, pattern, pattern_count);
542797ac58cSKevin Wolf         if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
543797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
544797ac58cSKevin Wolf                    PRId64 ", %d bytes\n",
545797ac58cSKevin Wolf                    offset + pattern_offset, pattern_count);
546797ac58cSKevin Wolf         }
547797ac58cSKevin Wolf         g_free(cmp_buf);
548797ac58cSKevin Wolf     }
549797ac58cSKevin Wolf 
550797ac58cSKevin Wolf     if (qflag) {
551797ac58cSKevin Wolf         goto out;
552797ac58cSKevin Wolf     }
553797ac58cSKevin Wolf 
554797ac58cSKevin Wolf     if (vflag) {
555797ac58cSKevin Wolf         dump_buffer(buf, offset, count);
556797ac58cSKevin Wolf     }
557797ac58cSKevin Wolf 
558797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
559797ac58cSKevin Wolf     t2 = tsub(t2, t1);
560797ac58cSKevin Wolf     print_report("read", &t2, offset, count, total, cnt, Cflag);
561797ac58cSKevin Wolf 
562797ac58cSKevin Wolf out:
563797ac58cSKevin Wolf     qemu_io_free(buf);
564797ac58cSKevin Wolf 
565797ac58cSKevin Wolf     return 0;
566797ac58cSKevin Wolf }
567797ac58cSKevin Wolf 
568797ac58cSKevin Wolf static void readv_help(void)
569797ac58cSKevin Wolf {
570797ac58cSKevin Wolf     printf(
571797ac58cSKevin Wolf "\n"
572797ac58cSKevin Wolf " reads a range of bytes from the given offset into multiple buffers\n"
573797ac58cSKevin Wolf "\n"
574797ac58cSKevin Wolf " Example:\n"
575797ac58cSKevin Wolf " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
576797ac58cSKevin Wolf "\n"
577797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
578797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
579797ac58cSKevin Wolf " Uses multiple iovec buffers if more than one byte range is specified.\n"
580797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
581797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
582797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
583797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
584797ac58cSKevin Wolf "\n");
585797ac58cSKevin Wolf }
586797ac58cSKevin Wolf 
587797ac58cSKevin Wolf static int readv_f(BlockDriverState *bs, int argc, char **argv);
588797ac58cSKevin Wolf 
589797ac58cSKevin Wolf static const cmdinfo_t readv_cmd = {
590797ac58cSKevin Wolf     .name       = "readv",
591797ac58cSKevin Wolf     .cfunc      = readv_f,
592797ac58cSKevin Wolf     .argmin     = 2,
593797ac58cSKevin Wolf     .argmax     = -1,
594797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern ] off len [len..]",
595797ac58cSKevin Wolf     .oneline    = "reads a number of bytes at a specified offset",
596797ac58cSKevin Wolf     .help       = readv_help,
597797ac58cSKevin Wolf };
598797ac58cSKevin Wolf 
599797ac58cSKevin Wolf static int readv_f(BlockDriverState *bs, int argc, char **argv)
600797ac58cSKevin Wolf {
601797ac58cSKevin Wolf     struct timeval t1, t2;
602797ac58cSKevin Wolf     int Cflag = 0, qflag = 0, vflag = 0;
603797ac58cSKevin Wolf     int c, cnt;
604797ac58cSKevin Wolf     char *buf;
605797ac58cSKevin Wolf     int64_t offset;
606797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
607797ac58cSKevin Wolf     int total = 0;
608797ac58cSKevin Wolf     int nr_iov;
609797ac58cSKevin Wolf     QEMUIOVector qiov;
610797ac58cSKevin Wolf     int pattern = 0;
611797ac58cSKevin Wolf     int Pflag = 0;
612797ac58cSKevin Wolf 
613797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
614797ac58cSKevin Wolf         switch (c) {
615797ac58cSKevin Wolf         case 'C':
616797ac58cSKevin Wolf             Cflag = 1;
617797ac58cSKevin Wolf             break;
618797ac58cSKevin Wolf         case 'P':
619797ac58cSKevin Wolf             Pflag = 1;
620797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
621797ac58cSKevin Wolf             if (pattern < 0) {
622797ac58cSKevin Wolf                 return 0;
623797ac58cSKevin Wolf             }
624797ac58cSKevin Wolf             break;
625797ac58cSKevin Wolf         case 'q':
626797ac58cSKevin Wolf             qflag = 1;
627797ac58cSKevin Wolf             break;
628797ac58cSKevin Wolf         case 'v':
629797ac58cSKevin Wolf             vflag = 1;
630797ac58cSKevin Wolf             break;
631797ac58cSKevin Wolf         default:
632797ac58cSKevin Wolf             return command_usage(&readv_cmd);
633797ac58cSKevin Wolf         }
634797ac58cSKevin Wolf     }
635797ac58cSKevin Wolf 
636797ac58cSKevin Wolf     if (optind > argc - 2) {
637797ac58cSKevin Wolf         return command_usage(&readv_cmd);
638797ac58cSKevin Wolf     }
639797ac58cSKevin Wolf 
640797ac58cSKevin Wolf 
641797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
642797ac58cSKevin Wolf     if (offset < 0) {
643797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
644797ac58cSKevin Wolf         return 0;
645797ac58cSKevin Wolf     }
646797ac58cSKevin Wolf     optind++;
647797ac58cSKevin Wolf 
648797ac58cSKevin Wolf     if (offset & 0x1ff) {
649797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
650797ac58cSKevin Wolf                offset);
651797ac58cSKevin Wolf         return 0;
652797ac58cSKevin Wolf     }
653797ac58cSKevin Wolf 
654797ac58cSKevin Wolf     nr_iov = argc - optind;
655797ac58cSKevin Wolf     buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
656797ac58cSKevin Wolf     if (buf == NULL) {
657797ac58cSKevin Wolf         return 0;
658797ac58cSKevin Wolf     }
659797ac58cSKevin Wolf 
660797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
661797ac58cSKevin Wolf     cnt = do_aio_readv(bs, &qiov, offset, &total);
662797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
663797ac58cSKevin Wolf 
664797ac58cSKevin Wolf     if (cnt < 0) {
665797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-cnt));
666797ac58cSKevin Wolf         goto out;
667797ac58cSKevin Wolf     }
668797ac58cSKevin Wolf 
669797ac58cSKevin Wolf     if (Pflag) {
670797ac58cSKevin Wolf         void *cmp_buf = g_malloc(qiov.size);
671797ac58cSKevin Wolf         memset(cmp_buf, pattern, qiov.size);
672797ac58cSKevin Wolf         if (memcmp(buf, cmp_buf, qiov.size)) {
673797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
674797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", offset, qiov.size);
675797ac58cSKevin Wolf         }
676797ac58cSKevin Wolf         g_free(cmp_buf);
677797ac58cSKevin Wolf     }
678797ac58cSKevin Wolf 
679797ac58cSKevin Wolf     if (qflag) {
680797ac58cSKevin Wolf         goto out;
681797ac58cSKevin Wolf     }
682797ac58cSKevin Wolf 
683797ac58cSKevin Wolf     if (vflag) {
684797ac58cSKevin Wolf         dump_buffer(buf, offset, qiov.size);
685797ac58cSKevin Wolf     }
686797ac58cSKevin Wolf 
687797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
688797ac58cSKevin Wolf     t2 = tsub(t2, t1);
689797ac58cSKevin Wolf     print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
690797ac58cSKevin Wolf 
691797ac58cSKevin Wolf out:
692797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
693797ac58cSKevin Wolf     qemu_io_free(buf);
694797ac58cSKevin Wolf     return 0;
695797ac58cSKevin Wolf }
696797ac58cSKevin Wolf 
697797ac58cSKevin Wolf static void write_help(void)
698797ac58cSKevin Wolf {
699797ac58cSKevin Wolf     printf(
700797ac58cSKevin Wolf "\n"
701797ac58cSKevin Wolf " writes a range of bytes from the given offset\n"
702797ac58cSKevin Wolf "\n"
703797ac58cSKevin Wolf " Example:\n"
704797ac58cSKevin Wolf " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
705797ac58cSKevin Wolf "\n"
706797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
707797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
708797ac58cSKevin Wolf " -b, -- write to the VM state rather than the virtual disk\n"
709797ac58cSKevin Wolf " -c, -- write compressed data with bdrv_write_compressed\n"
710797ac58cSKevin Wolf " -p, -- use bdrv_pwrite to write the file\n"
711797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
712797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
713797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
714797ac58cSKevin Wolf " -z, -- write zeroes using bdrv_co_write_zeroes\n"
715797ac58cSKevin Wolf "\n");
716797ac58cSKevin Wolf }
717797ac58cSKevin Wolf 
718797ac58cSKevin Wolf static int write_f(BlockDriverState *bs, int argc, char **argv);
719797ac58cSKevin Wolf 
720797ac58cSKevin Wolf static const cmdinfo_t write_cmd = {
721797ac58cSKevin Wolf     .name       = "write",
722797ac58cSKevin Wolf     .altname    = "w",
723797ac58cSKevin Wolf     .cfunc      = write_f,
724797ac58cSKevin Wolf     .argmin     = 2,
725797ac58cSKevin Wolf     .argmax     = -1,
726797ac58cSKevin Wolf     .args       = "[-bcCpqz] [-P pattern ] off len",
727797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
728797ac58cSKevin Wolf     .help       = write_help,
729797ac58cSKevin Wolf };
730797ac58cSKevin Wolf 
731797ac58cSKevin Wolf static int write_f(BlockDriverState *bs, int argc, char **argv)
732797ac58cSKevin Wolf {
733797ac58cSKevin Wolf     struct timeval t1, t2;
734797ac58cSKevin Wolf     int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
735797ac58cSKevin Wolf     int cflag = 0;
736797ac58cSKevin Wolf     int c, cnt;
737797ac58cSKevin Wolf     char *buf = NULL;
738797ac58cSKevin Wolf     int64_t offset;
739797ac58cSKevin Wolf     int count;
740797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
741797ac58cSKevin Wolf     int total = 0;
742797ac58cSKevin Wolf     int pattern = 0xcd;
743797ac58cSKevin Wolf 
744797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
745797ac58cSKevin Wolf         switch (c) {
746797ac58cSKevin Wolf         case 'b':
747797ac58cSKevin Wolf             bflag = 1;
748797ac58cSKevin Wolf             break;
749797ac58cSKevin Wolf         case 'c':
750797ac58cSKevin Wolf             cflag = 1;
751797ac58cSKevin Wolf             break;
752797ac58cSKevin Wolf         case 'C':
753797ac58cSKevin Wolf             Cflag = 1;
754797ac58cSKevin Wolf             break;
755797ac58cSKevin Wolf         case 'p':
756797ac58cSKevin Wolf             pflag = 1;
757797ac58cSKevin Wolf             break;
758797ac58cSKevin Wolf         case 'P':
759797ac58cSKevin Wolf             Pflag = 1;
760797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
761797ac58cSKevin Wolf             if (pattern < 0) {
762797ac58cSKevin Wolf                 return 0;
763797ac58cSKevin Wolf             }
764797ac58cSKevin Wolf             break;
765797ac58cSKevin Wolf         case 'q':
766797ac58cSKevin Wolf             qflag = 1;
767797ac58cSKevin Wolf             break;
768797ac58cSKevin Wolf         case 'z':
769797ac58cSKevin Wolf             zflag = 1;
770797ac58cSKevin Wolf             break;
771797ac58cSKevin Wolf         default:
772797ac58cSKevin Wolf             return command_usage(&write_cmd);
773797ac58cSKevin Wolf         }
774797ac58cSKevin Wolf     }
775797ac58cSKevin Wolf 
776797ac58cSKevin Wolf     if (optind != argc - 2) {
777797ac58cSKevin Wolf         return command_usage(&write_cmd);
778797ac58cSKevin Wolf     }
779797ac58cSKevin Wolf 
780797ac58cSKevin Wolf     if (bflag + pflag + zflag > 1) {
781797ac58cSKevin Wolf         printf("-b, -p, or -z cannot be specified at the same time\n");
782797ac58cSKevin Wolf         return 0;
783797ac58cSKevin Wolf     }
784797ac58cSKevin Wolf 
785797ac58cSKevin Wolf     if (zflag && Pflag) {
786797ac58cSKevin Wolf         printf("-z and -P cannot be specified at the same time\n");
787797ac58cSKevin Wolf         return 0;
788797ac58cSKevin Wolf     }
789797ac58cSKevin Wolf 
790797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
791797ac58cSKevin Wolf     if (offset < 0) {
792797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
793797ac58cSKevin Wolf         return 0;
794797ac58cSKevin Wolf     }
795797ac58cSKevin Wolf 
796797ac58cSKevin Wolf     optind++;
797797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
798797ac58cSKevin Wolf     if (count < 0) {
799797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
800797ac58cSKevin Wolf         return 0;
801797ac58cSKevin Wolf     }
802797ac58cSKevin Wolf 
803797ac58cSKevin Wolf     if (!pflag) {
804797ac58cSKevin Wolf         if (offset & 0x1ff) {
805797ac58cSKevin Wolf             printf("offset %" PRId64 " is not sector aligned\n",
806797ac58cSKevin Wolf                    offset);
807797ac58cSKevin Wolf             return 0;
808797ac58cSKevin Wolf         }
809797ac58cSKevin Wolf 
810797ac58cSKevin Wolf         if (count & 0x1ff) {
811797ac58cSKevin Wolf             printf("count %d is not sector aligned\n",
812797ac58cSKevin Wolf                    count);
813797ac58cSKevin Wolf             return 0;
814797ac58cSKevin Wolf         }
815797ac58cSKevin Wolf     }
816797ac58cSKevin Wolf 
817797ac58cSKevin Wolf     if (!zflag) {
818797ac58cSKevin Wolf         buf = qemu_io_alloc(bs, count, pattern);
819797ac58cSKevin Wolf     }
820797ac58cSKevin Wolf 
821797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
822797ac58cSKevin Wolf     if (pflag) {
823797ac58cSKevin Wolf         cnt = do_pwrite(bs, buf, offset, count, &total);
824797ac58cSKevin Wolf     } else if (bflag) {
825797ac58cSKevin Wolf         cnt = do_save_vmstate(bs, buf, offset, count, &total);
826797ac58cSKevin Wolf     } else if (zflag) {
827797ac58cSKevin Wolf         cnt = do_co_write_zeroes(bs, offset, count, &total);
828797ac58cSKevin Wolf     } else if (cflag) {
829797ac58cSKevin Wolf         cnt = do_write_compressed(bs, buf, offset, count, &total);
830797ac58cSKevin Wolf     } else {
831797ac58cSKevin Wolf         cnt = do_write(bs, buf, offset, count, &total);
832797ac58cSKevin Wolf     }
833797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
834797ac58cSKevin Wolf 
835797ac58cSKevin Wolf     if (cnt < 0) {
836797ac58cSKevin Wolf         printf("write failed: %s\n", strerror(-cnt));
837797ac58cSKevin Wolf         goto out;
838797ac58cSKevin Wolf     }
839797ac58cSKevin Wolf 
840797ac58cSKevin Wolf     if (qflag) {
841797ac58cSKevin Wolf         goto out;
842797ac58cSKevin Wolf     }
843797ac58cSKevin Wolf 
844797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
845797ac58cSKevin Wolf     t2 = tsub(t2, t1);
846797ac58cSKevin Wolf     print_report("wrote", &t2, offset, count, total, cnt, Cflag);
847797ac58cSKevin Wolf 
848797ac58cSKevin Wolf out:
849797ac58cSKevin Wolf     if (!zflag) {
850797ac58cSKevin Wolf         qemu_io_free(buf);
851797ac58cSKevin Wolf     }
852797ac58cSKevin Wolf 
853797ac58cSKevin Wolf     return 0;
854797ac58cSKevin Wolf }
855797ac58cSKevin Wolf 
856797ac58cSKevin Wolf static void
857797ac58cSKevin Wolf writev_help(void)
858797ac58cSKevin Wolf {
859797ac58cSKevin Wolf     printf(
860797ac58cSKevin Wolf "\n"
861797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers\n"
862797ac58cSKevin Wolf "\n"
863797ac58cSKevin Wolf " Example:\n"
864797ac58cSKevin Wolf " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
865797ac58cSKevin Wolf "\n"
866797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
867797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
868797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
869797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
870797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
871797ac58cSKevin Wolf "\n");
872797ac58cSKevin Wolf }
873797ac58cSKevin Wolf 
874797ac58cSKevin Wolf static int writev_f(BlockDriverState *bs, int argc, char **argv);
875797ac58cSKevin Wolf 
876797ac58cSKevin Wolf static const cmdinfo_t writev_cmd = {
877797ac58cSKevin Wolf     .name       = "writev",
878797ac58cSKevin Wolf     .cfunc      = writev_f,
879797ac58cSKevin Wolf     .argmin     = 2,
880797ac58cSKevin Wolf     .argmax     = -1,
881797ac58cSKevin Wolf     .args       = "[-Cq] [-P pattern ] off len [len..]",
882797ac58cSKevin Wolf     .oneline    = "writes a number of bytes at a specified offset",
883797ac58cSKevin Wolf     .help       = writev_help,
884797ac58cSKevin Wolf };
885797ac58cSKevin Wolf 
886797ac58cSKevin Wolf static int writev_f(BlockDriverState *bs, int argc, char **argv)
887797ac58cSKevin Wolf {
888797ac58cSKevin Wolf     struct timeval t1, t2;
889797ac58cSKevin Wolf     int Cflag = 0, qflag = 0;
890797ac58cSKevin Wolf     int c, cnt;
891797ac58cSKevin Wolf     char *buf;
892797ac58cSKevin Wolf     int64_t offset;
893797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
894797ac58cSKevin Wolf     int total = 0;
895797ac58cSKevin Wolf     int nr_iov;
896797ac58cSKevin Wolf     int pattern = 0xcd;
897797ac58cSKevin Wolf     QEMUIOVector qiov;
898797ac58cSKevin Wolf 
899797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CqP:")) != EOF) {
900797ac58cSKevin Wolf         switch (c) {
901797ac58cSKevin Wolf         case 'C':
902797ac58cSKevin Wolf             Cflag = 1;
903797ac58cSKevin Wolf             break;
904797ac58cSKevin Wolf         case 'q':
905797ac58cSKevin Wolf             qflag = 1;
906797ac58cSKevin Wolf             break;
907797ac58cSKevin Wolf         case 'P':
908797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
909797ac58cSKevin Wolf             if (pattern < 0) {
910797ac58cSKevin Wolf                 return 0;
911797ac58cSKevin Wolf             }
912797ac58cSKevin Wolf             break;
913797ac58cSKevin Wolf         default:
914797ac58cSKevin Wolf             return command_usage(&writev_cmd);
915797ac58cSKevin Wolf         }
916797ac58cSKevin Wolf     }
917797ac58cSKevin Wolf 
918797ac58cSKevin Wolf     if (optind > argc - 2) {
919797ac58cSKevin Wolf         return command_usage(&writev_cmd);
920797ac58cSKevin Wolf     }
921797ac58cSKevin Wolf 
922797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
923797ac58cSKevin Wolf     if (offset < 0) {
924797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
925797ac58cSKevin Wolf         return 0;
926797ac58cSKevin Wolf     }
927797ac58cSKevin Wolf     optind++;
928797ac58cSKevin Wolf 
929797ac58cSKevin Wolf     if (offset & 0x1ff) {
930797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
931797ac58cSKevin Wolf                offset);
932797ac58cSKevin Wolf         return 0;
933797ac58cSKevin Wolf     }
934797ac58cSKevin Wolf 
935797ac58cSKevin Wolf     nr_iov = argc - optind;
936797ac58cSKevin Wolf     buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
937797ac58cSKevin Wolf     if (buf == NULL) {
938797ac58cSKevin Wolf         return 0;
939797ac58cSKevin Wolf     }
940797ac58cSKevin Wolf 
941797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
942797ac58cSKevin Wolf     cnt = do_aio_writev(bs, &qiov, offset, &total);
943797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
944797ac58cSKevin Wolf 
945797ac58cSKevin Wolf     if (cnt < 0) {
946797ac58cSKevin Wolf         printf("writev failed: %s\n", strerror(-cnt));
947797ac58cSKevin Wolf         goto out;
948797ac58cSKevin Wolf     }
949797ac58cSKevin Wolf 
950797ac58cSKevin Wolf     if (qflag) {
951797ac58cSKevin Wolf         goto out;
952797ac58cSKevin Wolf     }
953797ac58cSKevin Wolf 
954797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
955797ac58cSKevin Wolf     t2 = tsub(t2, t1);
956797ac58cSKevin Wolf     print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
957797ac58cSKevin Wolf out:
958797ac58cSKevin Wolf     qemu_iovec_destroy(&qiov);
959797ac58cSKevin Wolf     qemu_io_free(buf);
960797ac58cSKevin Wolf     return 0;
961797ac58cSKevin Wolf }
962797ac58cSKevin Wolf 
963797ac58cSKevin Wolf static void multiwrite_help(void)
964797ac58cSKevin Wolf {
965797ac58cSKevin Wolf     printf(
966797ac58cSKevin Wolf "\n"
967797ac58cSKevin Wolf " writes a range of bytes from the given offset source from multiple buffers,\n"
968797ac58cSKevin Wolf " in a batch of requests that may be merged by qemu\n"
969797ac58cSKevin Wolf "\n"
970797ac58cSKevin Wolf " Example:\n"
971797ac58cSKevin Wolf " 'multiwrite 512 1k 1k ; 4k 1k'\n"
972797ac58cSKevin Wolf "  writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
973797ac58cSKevin Wolf "\n"
974797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
975797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
976797ac58cSKevin Wolf " by one for each request contained in the multiwrite command.\n"
977797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
978797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
979797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
980797ac58cSKevin Wolf "\n");
981797ac58cSKevin Wolf }
982797ac58cSKevin Wolf 
983797ac58cSKevin Wolf static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
984797ac58cSKevin Wolf 
985797ac58cSKevin Wolf static const cmdinfo_t multiwrite_cmd = {
986797ac58cSKevin Wolf     .name       = "multiwrite",
987797ac58cSKevin Wolf     .cfunc      = multiwrite_f,
988797ac58cSKevin Wolf     .argmin     = 2,
989797ac58cSKevin Wolf     .argmax     = -1,
990797ac58cSKevin Wolf     .args       = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
991797ac58cSKevin Wolf     .oneline    = "issues multiple write requests at once",
992797ac58cSKevin Wolf     .help       = multiwrite_help,
993797ac58cSKevin Wolf };
994797ac58cSKevin Wolf 
995797ac58cSKevin Wolf static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
996797ac58cSKevin Wolf {
997797ac58cSKevin Wolf     struct timeval t1, t2;
998797ac58cSKevin Wolf     int Cflag = 0, qflag = 0;
999797ac58cSKevin Wolf     int c, cnt;
1000797ac58cSKevin Wolf     char **buf;
1001797ac58cSKevin Wolf     int64_t offset, first_offset = 0;
1002797ac58cSKevin Wolf     /* Some compilers get confused and warn if this is not initialized.  */
1003797ac58cSKevin Wolf     int total = 0;
1004797ac58cSKevin Wolf     int nr_iov;
1005797ac58cSKevin Wolf     int nr_reqs;
1006797ac58cSKevin Wolf     int pattern = 0xcd;
1007797ac58cSKevin Wolf     QEMUIOVector *qiovs;
1008797ac58cSKevin Wolf     int i;
1009797ac58cSKevin Wolf     BlockRequest *reqs;
1010797ac58cSKevin Wolf 
1011797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1012797ac58cSKevin Wolf         switch (c) {
1013797ac58cSKevin Wolf         case 'C':
1014797ac58cSKevin Wolf             Cflag = 1;
1015797ac58cSKevin Wolf             break;
1016797ac58cSKevin Wolf         case 'q':
1017797ac58cSKevin Wolf             qflag = 1;
1018797ac58cSKevin Wolf             break;
1019797ac58cSKevin Wolf         case 'P':
1020797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1021797ac58cSKevin Wolf             if (pattern < 0) {
1022797ac58cSKevin Wolf                 return 0;
1023797ac58cSKevin Wolf             }
1024797ac58cSKevin Wolf             break;
1025797ac58cSKevin Wolf         default:
1026797ac58cSKevin Wolf             return command_usage(&writev_cmd);
1027797ac58cSKevin Wolf         }
1028797ac58cSKevin Wolf     }
1029797ac58cSKevin Wolf 
1030797ac58cSKevin Wolf     if (optind > argc - 2) {
1031797ac58cSKevin Wolf         return command_usage(&writev_cmd);
1032797ac58cSKevin Wolf     }
1033797ac58cSKevin Wolf 
1034797ac58cSKevin Wolf     nr_reqs = 1;
1035797ac58cSKevin Wolf     for (i = optind; i < argc; i++) {
1036797ac58cSKevin Wolf         if (!strcmp(argv[i], ";")) {
1037797ac58cSKevin Wolf             nr_reqs++;
1038797ac58cSKevin Wolf         }
1039797ac58cSKevin Wolf     }
1040797ac58cSKevin Wolf 
1041797ac58cSKevin Wolf     reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1042797ac58cSKevin Wolf     buf = g_malloc0(nr_reqs * sizeof(*buf));
1043797ac58cSKevin Wolf     qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
1044797ac58cSKevin Wolf 
1045797ac58cSKevin Wolf     for (i = 0; i < nr_reqs && optind < argc; i++) {
1046797ac58cSKevin Wolf         int j;
1047797ac58cSKevin Wolf 
1048797ac58cSKevin Wolf         /* Read the offset of the request */
1049797ac58cSKevin Wolf         offset = cvtnum(argv[optind]);
1050797ac58cSKevin Wolf         if (offset < 0) {
1051797ac58cSKevin Wolf             printf("non-numeric offset argument -- %s\n", argv[optind]);
1052797ac58cSKevin Wolf             goto out;
1053797ac58cSKevin Wolf         }
1054797ac58cSKevin Wolf         optind++;
1055797ac58cSKevin Wolf 
1056797ac58cSKevin Wolf         if (offset & 0x1ff) {
1057797ac58cSKevin Wolf             printf("offset %lld is not sector aligned\n",
1058797ac58cSKevin Wolf                    (long long)offset);
1059797ac58cSKevin Wolf             goto out;
1060797ac58cSKevin Wolf         }
1061797ac58cSKevin Wolf 
1062797ac58cSKevin Wolf         if (i == 0) {
1063797ac58cSKevin Wolf             first_offset = offset;
1064797ac58cSKevin Wolf         }
1065797ac58cSKevin Wolf 
1066797ac58cSKevin Wolf         /* Read lengths for qiov entries */
1067797ac58cSKevin Wolf         for (j = optind; j < argc; j++) {
1068797ac58cSKevin Wolf             if (!strcmp(argv[j], ";")) {
1069797ac58cSKevin Wolf                 break;
1070797ac58cSKevin Wolf             }
1071797ac58cSKevin Wolf         }
1072797ac58cSKevin Wolf 
1073797ac58cSKevin Wolf         nr_iov = j - optind;
1074797ac58cSKevin Wolf 
1075797ac58cSKevin Wolf         /* Build request */
1076797ac58cSKevin Wolf         buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
1077797ac58cSKevin Wolf         if (buf[i] == NULL) {
1078797ac58cSKevin Wolf             goto out;
1079797ac58cSKevin Wolf         }
1080797ac58cSKevin Wolf 
1081797ac58cSKevin Wolf         reqs[i].qiov = &qiovs[i];
1082797ac58cSKevin Wolf         reqs[i].sector = offset >> 9;
1083797ac58cSKevin Wolf         reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1084797ac58cSKevin Wolf 
1085797ac58cSKevin Wolf         optind = j + 1;
1086797ac58cSKevin Wolf 
1087797ac58cSKevin Wolf         pattern++;
1088797ac58cSKevin Wolf     }
1089797ac58cSKevin Wolf 
1090797ac58cSKevin Wolf     /* If there were empty requests at the end, ignore them */
1091797ac58cSKevin Wolf     nr_reqs = i;
1092797ac58cSKevin Wolf 
1093797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1094797ac58cSKevin Wolf     cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
1095797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1096797ac58cSKevin Wolf 
1097797ac58cSKevin Wolf     if (cnt < 0) {
1098797ac58cSKevin Wolf         printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1099797ac58cSKevin Wolf         goto out;
1100797ac58cSKevin Wolf     }
1101797ac58cSKevin Wolf 
1102797ac58cSKevin Wolf     if (qflag) {
1103797ac58cSKevin Wolf         goto out;
1104797ac58cSKevin Wolf     }
1105797ac58cSKevin Wolf 
1106797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1107797ac58cSKevin Wolf     t2 = tsub(t2, t1);
1108797ac58cSKevin Wolf     print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1109797ac58cSKevin Wolf out:
1110797ac58cSKevin Wolf     for (i = 0; i < nr_reqs; i++) {
1111797ac58cSKevin Wolf         qemu_io_free(buf[i]);
1112797ac58cSKevin Wolf         if (reqs[i].qiov != NULL) {
1113797ac58cSKevin Wolf             qemu_iovec_destroy(&qiovs[i]);
1114797ac58cSKevin Wolf         }
1115797ac58cSKevin Wolf     }
1116797ac58cSKevin Wolf     g_free(buf);
1117797ac58cSKevin Wolf     g_free(reqs);
1118797ac58cSKevin Wolf     g_free(qiovs);
1119797ac58cSKevin Wolf     return 0;
1120797ac58cSKevin Wolf }
1121797ac58cSKevin Wolf 
1122797ac58cSKevin Wolf struct aio_ctx {
1123797ac58cSKevin Wolf     QEMUIOVector qiov;
1124797ac58cSKevin Wolf     int64_t offset;
1125797ac58cSKevin Wolf     char *buf;
1126797ac58cSKevin Wolf     int qflag;
1127797ac58cSKevin Wolf     int vflag;
1128797ac58cSKevin Wolf     int Cflag;
1129797ac58cSKevin Wolf     int Pflag;
1130797ac58cSKevin Wolf     int pattern;
1131797ac58cSKevin Wolf     struct timeval t1;
1132797ac58cSKevin Wolf };
1133797ac58cSKevin Wolf 
1134797ac58cSKevin Wolf static void aio_write_done(void *opaque, int ret)
1135797ac58cSKevin Wolf {
1136797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1137797ac58cSKevin Wolf     struct timeval t2;
1138797ac58cSKevin Wolf 
1139797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1140797ac58cSKevin Wolf 
1141797ac58cSKevin Wolf 
1142797ac58cSKevin Wolf     if (ret < 0) {
1143797ac58cSKevin Wolf         printf("aio_write failed: %s\n", strerror(-ret));
1144797ac58cSKevin Wolf         goto out;
1145797ac58cSKevin Wolf     }
1146797ac58cSKevin Wolf 
1147797ac58cSKevin Wolf     if (ctx->qflag) {
1148797ac58cSKevin Wolf         goto out;
1149797ac58cSKevin Wolf     }
1150797ac58cSKevin Wolf 
1151797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1152797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1153797ac58cSKevin Wolf     print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1154797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1155797ac58cSKevin Wolf out:
1156797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1157797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1158797ac58cSKevin Wolf     g_free(ctx);
1159797ac58cSKevin Wolf }
1160797ac58cSKevin Wolf 
1161797ac58cSKevin Wolf static void aio_read_done(void *opaque, int ret)
1162797ac58cSKevin Wolf {
1163797ac58cSKevin Wolf     struct aio_ctx *ctx = opaque;
1164797ac58cSKevin Wolf     struct timeval t2;
1165797ac58cSKevin Wolf 
1166797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1167797ac58cSKevin Wolf 
1168797ac58cSKevin Wolf     if (ret < 0) {
1169797ac58cSKevin Wolf         printf("readv failed: %s\n", strerror(-ret));
1170797ac58cSKevin Wolf         goto out;
1171797ac58cSKevin Wolf     }
1172797ac58cSKevin Wolf 
1173797ac58cSKevin Wolf     if (ctx->Pflag) {
1174797ac58cSKevin Wolf         void *cmp_buf = g_malloc(ctx->qiov.size);
1175797ac58cSKevin Wolf 
1176797ac58cSKevin Wolf         memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1177797ac58cSKevin Wolf         if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1178797ac58cSKevin Wolf             printf("Pattern verification failed at offset %"
1179797ac58cSKevin Wolf                    PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1180797ac58cSKevin Wolf         }
1181797ac58cSKevin Wolf         g_free(cmp_buf);
1182797ac58cSKevin Wolf     }
1183797ac58cSKevin Wolf 
1184797ac58cSKevin Wolf     if (ctx->qflag) {
1185797ac58cSKevin Wolf         goto out;
1186797ac58cSKevin Wolf     }
1187797ac58cSKevin Wolf 
1188797ac58cSKevin Wolf     if (ctx->vflag) {
1189797ac58cSKevin Wolf         dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1190797ac58cSKevin Wolf     }
1191797ac58cSKevin Wolf 
1192797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1193797ac58cSKevin Wolf     t2 = tsub(t2, ctx->t1);
1194797ac58cSKevin Wolf     print_report("read", &t2, ctx->offset, ctx->qiov.size,
1195797ac58cSKevin Wolf                  ctx->qiov.size, 1, ctx->Cflag);
1196797ac58cSKevin Wolf out:
1197797ac58cSKevin Wolf     qemu_io_free(ctx->buf);
1198797ac58cSKevin Wolf     qemu_iovec_destroy(&ctx->qiov);
1199797ac58cSKevin Wolf     g_free(ctx);
1200797ac58cSKevin Wolf }
1201797ac58cSKevin Wolf 
1202797ac58cSKevin Wolf static void aio_read_help(void)
1203797ac58cSKevin Wolf {
1204797ac58cSKevin Wolf     printf(
1205797ac58cSKevin Wolf "\n"
1206797ac58cSKevin Wolf " asynchronously reads a range of bytes from the given offset\n"
1207797ac58cSKevin Wolf "\n"
1208797ac58cSKevin Wolf " Example:\n"
1209797ac58cSKevin Wolf " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1210797ac58cSKevin Wolf "\n"
1211797ac58cSKevin Wolf " Reads a segment of the currently open file, optionally dumping it to the\n"
1212797ac58cSKevin Wolf " standard output stream (with -v option) for subsequent inspection.\n"
1213797ac58cSKevin Wolf " The read is performed asynchronously and the aio_flush command must be\n"
1214797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1215797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1216797ac58cSKevin Wolf " -P, -- use a pattern to verify read data\n"
1217797ac58cSKevin Wolf " -v, -- dump buffer to standard output\n"
1218797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1219797ac58cSKevin Wolf "\n");
1220797ac58cSKevin Wolf }
1221797ac58cSKevin Wolf 
1222797ac58cSKevin Wolf static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
1223797ac58cSKevin Wolf 
1224797ac58cSKevin Wolf static const cmdinfo_t aio_read_cmd = {
1225797ac58cSKevin Wolf     .name       = "aio_read",
1226797ac58cSKevin Wolf     .cfunc      = aio_read_f,
1227797ac58cSKevin Wolf     .argmin     = 2,
1228797ac58cSKevin Wolf     .argmax     = -1,
1229797ac58cSKevin Wolf     .args       = "[-Cqv] [-P pattern ] off len [len..]",
1230797ac58cSKevin Wolf     .oneline    = "asynchronously reads a number of bytes",
1231797ac58cSKevin Wolf     .help       = aio_read_help,
1232797ac58cSKevin Wolf };
1233797ac58cSKevin Wolf 
1234797ac58cSKevin Wolf static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
1235797ac58cSKevin Wolf {
1236797ac58cSKevin Wolf     int nr_iov, c;
1237797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1238797ac58cSKevin Wolf 
1239797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1240797ac58cSKevin Wolf         switch (c) {
1241797ac58cSKevin Wolf         case 'C':
1242797ac58cSKevin Wolf             ctx->Cflag = 1;
1243797ac58cSKevin Wolf             break;
1244797ac58cSKevin Wolf         case 'P':
1245797ac58cSKevin Wolf             ctx->Pflag = 1;
1246797ac58cSKevin Wolf             ctx->pattern = parse_pattern(optarg);
1247797ac58cSKevin Wolf             if (ctx->pattern < 0) {
1248797ac58cSKevin Wolf                 g_free(ctx);
1249797ac58cSKevin Wolf                 return 0;
1250797ac58cSKevin Wolf             }
1251797ac58cSKevin Wolf             break;
1252797ac58cSKevin Wolf         case 'q':
1253797ac58cSKevin Wolf             ctx->qflag = 1;
1254797ac58cSKevin Wolf             break;
1255797ac58cSKevin Wolf         case 'v':
1256797ac58cSKevin Wolf             ctx->vflag = 1;
1257797ac58cSKevin Wolf             break;
1258797ac58cSKevin Wolf         default:
1259797ac58cSKevin Wolf             g_free(ctx);
1260797ac58cSKevin Wolf             return command_usage(&aio_read_cmd);
1261797ac58cSKevin Wolf         }
1262797ac58cSKevin Wolf     }
1263797ac58cSKevin Wolf 
1264797ac58cSKevin Wolf     if (optind > argc - 2) {
1265797ac58cSKevin Wolf         g_free(ctx);
1266797ac58cSKevin Wolf         return command_usage(&aio_read_cmd);
1267797ac58cSKevin Wolf     }
1268797ac58cSKevin Wolf 
1269797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1270797ac58cSKevin Wolf     if (ctx->offset < 0) {
1271797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1272797ac58cSKevin Wolf         g_free(ctx);
1273797ac58cSKevin Wolf         return 0;
1274797ac58cSKevin Wolf     }
1275797ac58cSKevin Wolf     optind++;
1276797ac58cSKevin Wolf 
1277797ac58cSKevin Wolf     if (ctx->offset & 0x1ff) {
1278797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1279797ac58cSKevin Wolf                ctx->offset);
1280797ac58cSKevin Wolf         g_free(ctx);
1281797ac58cSKevin Wolf         return 0;
1282797ac58cSKevin Wolf     }
1283797ac58cSKevin Wolf 
1284797ac58cSKevin Wolf     nr_iov = argc - optind;
1285797ac58cSKevin Wolf     ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1286797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1287797ac58cSKevin Wolf         g_free(ctx);
1288797ac58cSKevin Wolf         return 0;
1289797ac58cSKevin Wolf     }
1290797ac58cSKevin Wolf 
1291797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
1292797ac58cSKevin Wolf     bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1293797ac58cSKevin Wolf                    ctx->qiov.size >> 9, aio_read_done, ctx);
1294797ac58cSKevin Wolf     return 0;
1295797ac58cSKevin Wolf }
1296797ac58cSKevin Wolf 
1297797ac58cSKevin Wolf static void aio_write_help(void)
1298797ac58cSKevin Wolf {
1299797ac58cSKevin Wolf     printf(
1300797ac58cSKevin Wolf "\n"
1301797ac58cSKevin Wolf " asynchronously writes a range of bytes from the given offset source\n"
1302797ac58cSKevin Wolf " from multiple buffers\n"
1303797ac58cSKevin Wolf "\n"
1304797ac58cSKevin Wolf " Example:\n"
1305797ac58cSKevin Wolf " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1306797ac58cSKevin Wolf "\n"
1307797ac58cSKevin Wolf " Writes into a segment of the currently open file, using a buffer\n"
1308797ac58cSKevin Wolf " filled with a set pattern (0xcdcdcdcd).\n"
1309797ac58cSKevin Wolf " The write is performed asynchronously and the aio_flush command must be\n"
1310797ac58cSKevin Wolf " used to ensure all outstanding aio requests have been completed.\n"
1311797ac58cSKevin Wolf " -P, -- use different pattern to fill file\n"
1312797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1313797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1314797ac58cSKevin Wolf "\n");
1315797ac58cSKevin Wolf }
1316797ac58cSKevin Wolf 
1317797ac58cSKevin Wolf static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
1318797ac58cSKevin Wolf 
1319797ac58cSKevin Wolf static const cmdinfo_t aio_write_cmd = {
1320797ac58cSKevin Wolf     .name       = "aio_write",
1321797ac58cSKevin Wolf     .cfunc      = aio_write_f,
1322797ac58cSKevin Wolf     .argmin     = 2,
1323797ac58cSKevin Wolf     .argmax     = -1,
1324797ac58cSKevin Wolf     .args       = "[-Cq] [-P pattern ] off len [len..]",
1325797ac58cSKevin Wolf     .oneline    = "asynchronously writes a number of bytes",
1326797ac58cSKevin Wolf     .help       = aio_write_help,
1327797ac58cSKevin Wolf };
1328797ac58cSKevin Wolf 
1329797ac58cSKevin Wolf static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
1330797ac58cSKevin Wolf {
1331797ac58cSKevin Wolf     int nr_iov, c;
1332797ac58cSKevin Wolf     int pattern = 0xcd;
1333797ac58cSKevin Wolf     struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1334797ac58cSKevin Wolf 
1335797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1336797ac58cSKevin Wolf         switch (c) {
1337797ac58cSKevin Wolf         case 'C':
1338797ac58cSKevin Wolf             ctx->Cflag = 1;
1339797ac58cSKevin Wolf             break;
1340797ac58cSKevin Wolf         case 'q':
1341797ac58cSKevin Wolf             ctx->qflag = 1;
1342797ac58cSKevin Wolf             break;
1343797ac58cSKevin Wolf         case 'P':
1344797ac58cSKevin Wolf             pattern = parse_pattern(optarg);
1345797ac58cSKevin Wolf             if (pattern < 0) {
1346797ac58cSKevin Wolf                 g_free(ctx);
1347797ac58cSKevin Wolf                 return 0;
1348797ac58cSKevin Wolf             }
1349797ac58cSKevin Wolf             break;
1350797ac58cSKevin Wolf         default:
1351797ac58cSKevin Wolf             g_free(ctx);
1352797ac58cSKevin Wolf             return command_usage(&aio_write_cmd);
1353797ac58cSKevin Wolf         }
1354797ac58cSKevin Wolf     }
1355797ac58cSKevin Wolf 
1356797ac58cSKevin Wolf     if (optind > argc - 2) {
1357797ac58cSKevin Wolf         g_free(ctx);
1358797ac58cSKevin Wolf         return command_usage(&aio_write_cmd);
1359797ac58cSKevin Wolf     }
1360797ac58cSKevin Wolf 
1361797ac58cSKevin Wolf     ctx->offset = cvtnum(argv[optind]);
1362797ac58cSKevin Wolf     if (ctx->offset < 0) {
1363797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1364797ac58cSKevin Wolf         g_free(ctx);
1365797ac58cSKevin Wolf         return 0;
1366797ac58cSKevin Wolf     }
1367797ac58cSKevin Wolf     optind++;
1368797ac58cSKevin Wolf 
1369797ac58cSKevin Wolf     if (ctx->offset & 0x1ff) {
1370797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1371797ac58cSKevin Wolf                ctx->offset);
1372797ac58cSKevin Wolf         g_free(ctx);
1373797ac58cSKevin Wolf         return 0;
1374797ac58cSKevin Wolf     }
1375797ac58cSKevin Wolf 
1376797ac58cSKevin Wolf     nr_iov = argc - optind;
1377797ac58cSKevin Wolf     ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
1378797ac58cSKevin Wolf     if (ctx->buf == NULL) {
1379797ac58cSKevin Wolf         g_free(ctx);
1380797ac58cSKevin Wolf         return 0;
1381797ac58cSKevin Wolf     }
1382797ac58cSKevin Wolf 
1383797ac58cSKevin Wolf     gettimeofday(&ctx->t1, NULL);
1384797ac58cSKevin Wolf     bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1385797ac58cSKevin Wolf                     ctx->qiov.size >> 9, aio_write_done, ctx);
1386797ac58cSKevin Wolf     return 0;
1387797ac58cSKevin Wolf }
1388797ac58cSKevin Wolf 
1389797ac58cSKevin Wolf static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
1390797ac58cSKevin Wolf {
1391797ac58cSKevin Wolf     bdrv_drain_all();
1392797ac58cSKevin Wolf     return 0;
1393797ac58cSKevin Wolf }
1394797ac58cSKevin Wolf 
1395797ac58cSKevin Wolf static const cmdinfo_t aio_flush_cmd = {
1396797ac58cSKevin Wolf     .name       = "aio_flush",
1397797ac58cSKevin Wolf     .cfunc      = aio_flush_f,
1398797ac58cSKevin Wolf     .oneline    = "completes all outstanding aio requests"
1399797ac58cSKevin Wolf };
1400797ac58cSKevin Wolf 
1401797ac58cSKevin Wolf static int flush_f(BlockDriverState *bs, int argc, char **argv)
1402797ac58cSKevin Wolf {
1403797ac58cSKevin Wolf     bdrv_flush(bs);
1404797ac58cSKevin Wolf     return 0;
1405797ac58cSKevin Wolf }
1406797ac58cSKevin Wolf 
1407797ac58cSKevin Wolf static const cmdinfo_t flush_cmd = {
1408797ac58cSKevin Wolf     .name       = "flush",
1409797ac58cSKevin Wolf     .altname    = "f",
1410797ac58cSKevin Wolf     .cfunc      = flush_f,
1411797ac58cSKevin Wolf     .oneline    = "flush all in-core file state to disk",
1412797ac58cSKevin Wolf };
1413797ac58cSKevin Wolf 
1414797ac58cSKevin Wolf static int truncate_f(BlockDriverState *bs, int argc, char **argv)
1415797ac58cSKevin Wolf {
1416797ac58cSKevin Wolf     int64_t offset;
1417797ac58cSKevin Wolf     int ret;
1418797ac58cSKevin Wolf 
1419797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1420797ac58cSKevin Wolf     if (offset < 0) {
1421797ac58cSKevin Wolf         printf("non-numeric truncate argument -- %s\n", argv[1]);
1422797ac58cSKevin Wolf         return 0;
1423797ac58cSKevin Wolf     }
1424797ac58cSKevin Wolf 
1425797ac58cSKevin Wolf     ret = bdrv_truncate(bs, offset);
1426797ac58cSKevin Wolf     if (ret < 0) {
1427797ac58cSKevin Wolf         printf("truncate: %s\n", strerror(-ret));
1428797ac58cSKevin Wolf         return 0;
1429797ac58cSKevin Wolf     }
1430797ac58cSKevin Wolf 
1431797ac58cSKevin Wolf     return 0;
1432797ac58cSKevin Wolf }
1433797ac58cSKevin Wolf 
1434797ac58cSKevin Wolf static const cmdinfo_t truncate_cmd = {
1435797ac58cSKevin Wolf     .name       = "truncate",
1436797ac58cSKevin Wolf     .altname    = "t",
1437797ac58cSKevin Wolf     .cfunc      = truncate_f,
1438797ac58cSKevin Wolf     .argmin     = 1,
1439797ac58cSKevin Wolf     .argmax     = 1,
1440797ac58cSKevin Wolf     .args       = "off",
1441797ac58cSKevin Wolf     .oneline    = "truncates the current file at the given offset",
1442797ac58cSKevin Wolf };
1443797ac58cSKevin Wolf 
1444797ac58cSKevin Wolf static int length_f(BlockDriverState *bs, int argc, char **argv)
1445797ac58cSKevin Wolf {
1446797ac58cSKevin Wolf     int64_t size;
1447797ac58cSKevin Wolf     char s1[64];
1448797ac58cSKevin Wolf 
1449797ac58cSKevin Wolf     size = bdrv_getlength(bs);
1450797ac58cSKevin Wolf     if (size < 0) {
1451797ac58cSKevin Wolf         printf("getlength: %s\n", strerror(-size));
1452797ac58cSKevin Wolf         return 0;
1453797ac58cSKevin Wolf     }
1454797ac58cSKevin Wolf 
1455797ac58cSKevin Wolf     cvtstr(size, s1, sizeof(s1));
1456797ac58cSKevin Wolf     printf("%s\n", s1);
1457797ac58cSKevin Wolf     return 0;
1458797ac58cSKevin Wolf }
1459797ac58cSKevin Wolf 
1460797ac58cSKevin Wolf 
1461797ac58cSKevin Wolf static const cmdinfo_t length_cmd = {
1462797ac58cSKevin Wolf     .name   = "length",
1463797ac58cSKevin Wolf     .altname    = "l",
1464797ac58cSKevin Wolf     .cfunc      = length_f,
1465797ac58cSKevin Wolf     .oneline    = "gets the length of the current file",
1466797ac58cSKevin Wolf };
1467797ac58cSKevin Wolf 
1468797ac58cSKevin Wolf 
1469797ac58cSKevin Wolf static int info_f(BlockDriverState *bs, int argc, char **argv)
1470797ac58cSKevin Wolf {
1471797ac58cSKevin Wolf     BlockDriverInfo bdi;
1472797ac58cSKevin Wolf     char s1[64], s2[64];
1473797ac58cSKevin Wolf     int ret;
1474797ac58cSKevin Wolf 
1475797ac58cSKevin Wolf     if (bs->drv && bs->drv->format_name) {
1476797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->format_name);
1477797ac58cSKevin Wolf     }
1478797ac58cSKevin Wolf     if (bs->drv && bs->drv->protocol_name) {
1479797ac58cSKevin Wolf         printf("format name: %s\n", bs->drv->protocol_name);
1480797ac58cSKevin Wolf     }
1481797ac58cSKevin Wolf 
1482797ac58cSKevin Wolf     ret = bdrv_get_info(bs, &bdi);
1483797ac58cSKevin Wolf     if (ret) {
1484797ac58cSKevin Wolf         return 0;
1485797ac58cSKevin Wolf     }
1486797ac58cSKevin Wolf 
1487797ac58cSKevin Wolf     cvtstr(bdi.cluster_size, s1, sizeof(s1));
1488797ac58cSKevin Wolf     cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1489797ac58cSKevin Wolf 
1490797ac58cSKevin Wolf     printf("cluster size: %s\n", s1);
1491797ac58cSKevin Wolf     printf("vm state offset: %s\n", s2);
1492797ac58cSKevin Wolf 
1493797ac58cSKevin Wolf     return 0;
1494797ac58cSKevin Wolf }
1495797ac58cSKevin Wolf 
1496797ac58cSKevin Wolf 
1497797ac58cSKevin Wolf 
1498797ac58cSKevin Wolf static const cmdinfo_t info_cmd = {
1499797ac58cSKevin Wolf     .name       = "info",
1500797ac58cSKevin Wolf     .altname    = "i",
1501797ac58cSKevin Wolf     .cfunc      = info_f,
1502797ac58cSKevin Wolf     .oneline    = "prints information about the current file",
1503797ac58cSKevin Wolf };
1504797ac58cSKevin Wolf 
1505797ac58cSKevin Wolf static void discard_help(void)
1506797ac58cSKevin Wolf {
1507797ac58cSKevin Wolf     printf(
1508797ac58cSKevin Wolf "\n"
1509797ac58cSKevin Wolf " discards a range of bytes from the given offset\n"
1510797ac58cSKevin Wolf "\n"
1511797ac58cSKevin Wolf " Example:\n"
1512797ac58cSKevin Wolf " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1513797ac58cSKevin Wolf "\n"
1514797ac58cSKevin Wolf " Discards a segment of the currently open file.\n"
1515797ac58cSKevin Wolf " -C, -- report statistics in a machine parsable format\n"
1516797ac58cSKevin Wolf " -q, -- quiet mode, do not show I/O statistics\n"
1517797ac58cSKevin Wolf "\n");
1518797ac58cSKevin Wolf }
1519797ac58cSKevin Wolf 
1520797ac58cSKevin Wolf static int discard_f(BlockDriverState *bs, int argc, char **argv);
1521797ac58cSKevin Wolf 
1522797ac58cSKevin Wolf static const cmdinfo_t discard_cmd = {
1523797ac58cSKevin Wolf     .name       = "discard",
1524797ac58cSKevin Wolf     .altname    = "d",
1525797ac58cSKevin Wolf     .cfunc      = discard_f,
1526797ac58cSKevin Wolf     .argmin     = 2,
1527797ac58cSKevin Wolf     .argmax     = -1,
1528797ac58cSKevin Wolf     .args       = "[-Cq] off len",
1529797ac58cSKevin Wolf     .oneline    = "discards a number of bytes at a specified offset",
1530797ac58cSKevin Wolf     .help       = discard_help,
1531797ac58cSKevin Wolf };
1532797ac58cSKevin Wolf 
1533797ac58cSKevin Wolf static int discard_f(BlockDriverState *bs, int argc, char **argv)
1534797ac58cSKevin Wolf {
1535797ac58cSKevin Wolf     struct timeval t1, t2;
1536797ac58cSKevin Wolf     int Cflag = 0, qflag = 0;
1537797ac58cSKevin Wolf     int c, ret;
1538797ac58cSKevin Wolf     int64_t offset;
1539797ac58cSKevin Wolf     int count;
1540797ac58cSKevin Wolf 
1541797ac58cSKevin Wolf     while ((c = getopt(argc, argv, "Cq")) != EOF) {
1542797ac58cSKevin Wolf         switch (c) {
1543797ac58cSKevin Wolf         case 'C':
1544797ac58cSKevin Wolf             Cflag = 1;
1545797ac58cSKevin Wolf             break;
1546797ac58cSKevin Wolf         case 'q':
1547797ac58cSKevin Wolf             qflag = 1;
1548797ac58cSKevin Wolf             break;
1549797ac58cSKevin Wolf         default:
1550797ac58cSKevin Wolf             return command_usage(&discard_cmd);
1551797ac58cSKevin Wolf         }
1552797ac58cSKevin Wolf     }
1553797ac58cSKevin Wolf 
1554797ac58cSKevin Wolf     if (optind != argc - 2) {
1555797ac58cSKevin Wolf         return command_usage(&discard_cmd);
1556797ac58cSKevin Wolf     }
1557797ac58cSKevin Wolf 
1558797ac58cSKevin Wolf     offset = cvtnum(argv[optind]);
1559797ac58cSKevin Wolf     if (offset < 0) {
1560797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1561797ac58cSKevin Wolf         return 0;
1562797ac58cSKevin Wolf     }
1563797ac58cSKevin Wolf 
1564797ac58cSKevin Wolf     optind++;
1565797ac58cSKevin Wolf     count = cvtnum(argv[optind]);
1566797ac58cSKevin Wolf     if (count < 0) {
1567797ac58cSKevin Wolf         printf("non-numeric length argument -- %s\n", argv[optind]);
1568797ac58cSKevin Wolf         return 0;
1569797ac58cSKevin Wolf     }
1570797ac58cSKevin Wolf 
1571797ac58cSKevin Wolf     gettimeofday(&t1, NULL);
1572797ac58cSKevin Wolf     ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1573797ac58cSKevin Wolf                        count >> BDRV_SECTOR_BITS);
1574797ac58cSKevin Wolf     gettimeofday(&t2, NULL);
1575797ac58cSKevin Wolf 
1576797ac58cSKevin Wolf     if (ret < 0) {
1577797ac58cSKevin Wolf         printf("discard failed: %s\n", strerror(-ret));
1578797ac58cSKevin Wolf         goto out;
1579797ac58cSKevin Wolf     }
1580797ac58cSKevin Wolf 
1581797ac58cSKevin Wolf     /* Finally, report back -- -C gives a parsable format */
1582797ac58cSKevin Wolf     if (!qflag) {
1583797ac58cSKevin Wolf         t2 = tsub(t2, t1);
1584797ac58cSKevin Wolf         print_report("discard", &t2, offset, count, count, 1, Cflag);
1585797ac58cSKevin Wolf     }
1586797ac58cSKevin Wolf 
1587797ac58cSKevin Wolf out:
1588797ac58cSKevin Wolf     return 0;
1589797ac58cSKevin Wolf }
1590797ac58cSKevin Wolf 
1591797ac58cSKevin Wolf static int alloc_f(BlockDriverState *bs, int argc, char **argv)
1592797ac58cSKevin Wolf {
1593797ac58cSKevin Wolf     int64_t offset, sector_num;
1594797ac58cSKevin Wolf     int nb_sectors, remaining;
1595797ac58cSKevin Wolf     char s1[64];
1596797ac58cSKevin Wolf     int num, sum_alloc;
1597797ac58cSKevin Wolf     int ret;
1598797ac58cSKevin Wolf 
1599797ac58cSKevin Wolf     offset = cvtnum(argv[1]);
1600797ac58cSKevin Wolf     if (offset < 0) {
1601797ac58cSKevin Wolf         printf("non-numeric offset argument -- %s\n", argv[1]);
1602797ac58cSKevin Wolf         return 0;
1603797ac58cSKevin Wolf     } else if (offset & 0x1ff) {
1604797ac58cSKevin Wolf         printf("offset %" PRId64 " is not sector aligned\n",
1605797ac58cSKevin Wolf                offset);
1606797ac58cSKevin Wolf         return 0;
1607797ac58cSKevin Wolf     }
1608797ac58cSKevin Wolf 
1609797ac58cSKevin Wolf     if (argc == 3) {
1610797ac58cSKevin Wolf         nb_sectors = cvtnum(argv[2]);
1611797ac58cSKevin Wolf         if (nb_sectors < 0) {
1612797ac58cSKevin Wolf             printf("non-numeric length argument -- %s\n", argv[2]);
1613797ac58cSKevin Wolf             return 0;
1614797ac58cSKevin Wolf         }
1615797ac58cSKevin Wolf     } else {
1616797ac58cSKevin Wolf         nb_sectors = 1;
1617797ac58cSKevin Wolf     }
1618797ac58cSKevin Wolf 
1619797ac58cSKevin Wolf     remaining = nb_sectors;
1620797ac58cSKevin Wolf     sum_alloc = 0;
1621797ac58cSKevin Wolf     sector_num = offset >> 9;
1622797ac58cSKevin Wolf     while (remaining) {
1623797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1624797ac58cSKevin Wolf         sector_num += num;
1625797ac58cSKevin Wolf         remaining -= num;
1626797ac58cSKevin Wolf         if (ret) {
1627797ac58cSKevin Wolf             sum_alloc += num;
1628797ac58cSKevin Wolf         }
1629797ac58cSKevin Wolf         if (num == 0) {
1630797ac58cSKevin Wolf             nb_sectors -= remaining;
1631797ac58cSKevin Wolf             remaining = 0;
1632797ac58cSKevin Wolf         }
1633797ac58cSKevin Wolf     }
1634797ac58cSKevin Wolf 
1635797ac58cSKevin Wolf     cvtstr(offset, s1, sizeof(s1));
1636797ac58cSKevin Wolf 
1637797ac58cSKevin Wolf     printf("%d/%d sectors allocated at offset %s\n",
1638797ac58cSKevin Wolf            sum_alloc, nb_sectors, s1);
1639797ac58cSKevin Wolf     return 0;
1640797ac58cSKevin Wolf }
1641797ac58cSKevin Wolf 
1642797ac58cSKevin Wolf static const cmdinfo_t alloc_cmd = {
1643797ac58cSKevin Wolf     .name       = "alloc",
1644797ac58cSKevin Wolf     .altname    = "a",
1645797ac58cSKevin Wolf     .argmin     = 1,
1646797ac58cSKevin Wolf     .argmax     = 2,
1647797ac58cSKevin Wolf     .cfunc      = alloc_f,
1648797ac58cSKevin Wolf     .args       = "off [sectors]",
1649797ac58cSKevin Wolf     .oneline    = "checks if a sector is present in the file",
1650797ac58cSKevin Wolf };
1651797ac58cSKevin Wolf 
1652797ac58cSKevin Wolf 
1653797ac58cSKevin Wolf static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1654797ac58cSKevin Wolf                             int64_t nb_sectors, int64_t *pnum)
1655797ac58cSKevin Wolf {
1656797ac58cSKevin Wolf     int num, num_checked;
1657797ac58cSKevin Wolf     int ret, firstret;
1658797ac58cSKevin Wolf 
1659797ac58cSKevin Wolf     num_checked = MIN(nb_sectors, INT_MAX);
1660797ac58cSKevin Wolf     ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1661797ac58cSKevin Wolf     if (ret < 0) {
1662797ac58cSKevin Wolf         return ret;
1663797ac58cSKevin Wolf     }
1664797ac58cSKevin Wolf 
1665797ac58cSKevin Wolf     firstret = ret;
1666797ac58cSKevin Wolf     *pnum = num;
1667797ac58cSKevin Wolf 
1668797ac58cSKevin Wolf     while (nb_sectors > 0 && ret == firstret) {
1669797ac58cSKevin Wolf         sector_num += num;
1670797ac58cSKevin Wolf         nb_sectors -= num;
1671797ac58cSKevin Wolf 
1672797ac58cSKevin Wolf         num_checked = MIN(nb_sectors, INT_MAX);
1673797ac58cSKevin Wolf         ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1674797ac58cSKevin Wolf         if (ret == firstret) {
1675797ac58cSKevin Wolf             *pnum += num;
1676797ac58cSKevin Wolf         } else {
1677797ac58cSKevin Wolf             break;
1678797ac58cSKevin Wolf         }
1679797ac58cSKevin Wolf     }
1680797ac58cSKevin Wolf 
1681797ac58cSKevin Wolf     return firstret;
1682797ac58cSKevin Wolf }
1683797ac58cSKevin Wolf 
1684797ac58cSKevin Wolf static int map_f(BlockDriverState *bs, int argc, char **argv)
1685797ac58cSKevin Wolf {
1686797ac58cSKevin Wolf     int64_t offset;
1687797ac58cSKevin Wolf     int64_t nb_sectors;
1688797ac58cSKevin Wolf     char s1[64];
1689797ac58cSKevin Wolf     int64_t num;
1690797ac58cSKevin Wolf     int ret;
1691797ac58cSKevin Wolf     const char *retstr;
1692797ac58cSKevin Wolf 
1693797ac58cSKevin Wolf     offset = 0;
1694797ac58cSKevin Wolf     nb_sectors = bs->total_sectors;
1695797ac58cSKevin Wolf 
1696797ac58cSKevin Wolf     do {
1697797ac58cSKevin Wolf         ret = map_is_allocated(bs, offset, nb_sectors, &num);
1698797ac58cSKevin Wolf         if (ret < 0) {
1699797ac58cSKevin Wolf             error_report("Failed to get allocation status: %s", strerror(-ret));
1700797ac58cSKevin Wolf             return 0;
1701797ac58cSKevin Wolf         }
1702797ac58cSKevin Wolf 
1703797ac58cSKevin Wolf         retstr = ret ? "    allocated" : "not allocated";
1704797ac58cSKevin Wolf         cvtstr(offset << 9ULL, s1, sizeof(s1));
1705797ac58cSKevin Wolf         printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1706797ac58cSKevin Wolf                "at offset %s (%d)\n",
1707797ac58cSKevin Wolf                offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1708797ac58cSKevin Wolf 
1709797ac58cSKevin Wolf         offset += num;
1710797ac58cSKevin Wolf         nb_sectors -= num;
1711797ac58cSKevin Wolf     } while (offset < bs->total_sectors);
1712797ac58cSKevin Wolf 
1713797ac58cSKevin Wolf     return 0;
1714797ac58cSKevin Wolf }
1715797ac58cSKevin Wolf 
1716797ac58cSKevin Wolf static const cmdinfo_t map_cmd = {
1717797ac58cSKevin Wolf        .name           = "map",
1718797ac58cSKevin Wolf        .argmin         = 0,
1719797ac58cSKevin Wolf        .argmax         = 0,
1720797ac58cSKevin Wolf        .cfunc          = map_f,
1721797ac58cSKevin Wolf        .args           = "",
1722797ac58cSKevin Wolf        .oneline        = "prints the allocated areas of a file",
1723797ac58cSKevin Wolf };
1724797ac58cSKevin Wolf 
1725797ac58cSKevin Wolf static int break_f(BlockDriverState *bs, int argc, char **argv)
1726797ac58cSKevin Wolf {
1727797ac58cSKevin Wolf     int ret;
1728797ac58cSKevin Wolf 
1729797ac58cSKevin Wolf     ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1730797ac58cSKevin Wolf     if (ret < 0) {
1731797ac58cSKevin Wolf         printf("Could not set breakpoint: %s\n", strerror(-ret));
1732797ac58cSKevin Wolf     }
1733797ac58cSKevin Wolf 
1734797ac58cSKevin Wolf     return 0;
1735797ac58cSKevin Wolf }
1736797ac58cSKevin Wolf 
1737797ac58cSKevin Wolf static const cmdinfo_t break_cmd = {
1738797ac58cSKevin Wolf        .name           = "break",
1739797ac58cSKevin Wolf        .argmin         = 2,
1740797ac58cSKevin Wolf        .argmax         = 2,
1741797ac58cSKevin Wolf        .cfunc          = break_f,
1742797ac58cSKevin Wolf        .args           = "event tag",
1743797ac58cSKevin Wolf        .oneline        = "sets a breakpoint on event and tags the stopped "
1744797ac58cSKevin Wolf                          "request as tag",
1745797ac58cSKevin Wolf };
1746797ac58cSKevin Wolf 
1747797ac58cSKevin Wolf static int resume_f(BlockDriverState *bs, int argc, char **argv)
1748797ac58cSKevin Wolf {
1749797ac58cSKevin Wolf     int ret;
1750797ac58cSKevin Wolf 
1751797ac58cSKevin Wolf     ret = bdrv_debug_resume(bs, argv[1]);
1752797ac58cSKevin Wolf     if (ret < 0) {
1753797ac58cSKevin Wolf         printf("Could not resume request: %s\n", strerror(-ret));
1754797ac58cSKevin Wolf     }
1755797ac58cSKevin Wolf 
1756797ac58cSKevin Wolf     return 0;
1757797ac58cSKevin Wolf }
1758797ac58cSKevin Wolf 
1759797ac58cSKevin Wolf static const cmdinfo_t resume_cmd = {
1760797ac58cSKevin Wolf        .name           = "resume",
1761797ac58cSKevin Wolf        .argmin         = 1,
1762797ac58cSKevin Wolf        .argmax         = 1,
1763797ac58cSKevin Wolf        .cfunc          = resume_f,
1764797ac58cSKevin Wolf        .args           = "tag",
1765797ac58cSKevin Wolf        .oneline        = "resumes the request tagged as tag",
1766797ac58cSKevin Wolf };
1767797ac58cSKevin Wolf 
1768797ac58cSKevin Wolf static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
1769797ac58cSKevin Wolf {
1770797ac58cSKevin Wolf     while (!bdrv_debug_is_suspended(bs, argv[1])) {
1771797ac58cSKevin Wolf         qemu_aio_wait();
1772797ac58cSKevin Wolf     }
1773797ac58cSKevin Wolf 
1774797ac58cSKevin Wolf     return 0;
1775797ac58cSKevin Wolf }
1776797ac58cSKevin Wolf 
1777797ac58cSKevin Wolf static const cmdinfo_t wait_break_cmd = {
1778797ac58cSKevin Wolf        .name           = "wait_break",
1779797ac58cSKevin Wolf        .argmin         = 1,
1780797ac58cSKevin Wolf        .argmax         = 1,
1781797ac58cSKevin Wolf        .cfunc          = wait_break_f,
1782797ac58cSKevin Wolf        .args           = "tag",
1783797ac58cSKevin Wolf        .oneline        = "waits for the suspension of a request",
1784797ac58cSKevin Wolf };
1785797ac58cSKevin Wolf 
1786797ac58cSKevin Wolf static int abort_f(BlockDriverState *bs, int argc, char **argv)
1787797ac58cSKevin Wolf {
1788797ac58cSKevin Wolf     abort();
1789797ac58cSKevin Wolf }
1790797ac58cSKevin Wolf 
1791797ac58cSKevin Wolf static const cmdinfo_t abort_cmd = {
1792797ac58cSKevin Wolf        .name           = "abort",
1793797ac58cSKevin Wolf        .cfunc          = abort_f,
1794797ac58cSKevin Wolf        .flags          = CMD_NOFILE_OK,
1795797ac58cSKevin Wolf        .oneline        = "simulate a program crash using abort(3)",
1796797ac58cSKevin Wolf };
1797797ac58cSKevin Wolf 
1798797ac58cSKevin Wolf static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
1799797ac58cSKevin Wolf {
1800797ac58cSKevin Wolf     if (ct->flags & CMD_FLAG_GLOBAL) {
1801797ac58cSKevin Wolf         return 1;
1802797ac58cSKevin Wolf     }
1803797ac58cSKevin Wolf     if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1804797ac58cSKevin Wolf         fprintf(stderr, "no file open, try 'help open'\n");
1805797ac58cSKevin Wolf         return 0;
1806797ac58cSKevin Wolf     }
1807797ac58cSKevin Wolf     return 1;
1808797ac58cSKevin Wolf }
1809797ac58cSKevin Wolf 
1810*dd583296SKevin Wolf bool qemuio_command(const char *cmd)
1811*dd583296SKevin Wolf {
1812*dd583296SKevin Wolf     char *input;
1813*dd583296SKevin Wolf     const cmdinfo_t *ct;
1814*dd583296SKevin Wolf     char **v;
1815*dd583296SKevin Wolf     int c;
1816*dd583296SKevin Wolf     bool done = false;
1817*dd583296SKevin Wolf 
1818*dd583296SKevin Wolf     input = g_strdup(cmd);
1819*dd583296SKevin Wolf     v = breakline(input, &c);
1820*dd583296SKevin Wolf     if (c) {
1821*dd583296SKevin Wolf         ct = find_command(v[0]);
1822*dd583296SKevin Wolf         if (ct) {
1823*dd583296SKevin Wolf             done = command(ct, c, v);
1824*dd583296SKevin Wolf         } else {
1825*dd583296SKevin Wolf             fprintf(stderr, "command \"%s\" not found\n", v[0]);
1826*dd583296SKevin Wolf         }
1827*dd583296SKevin Wolf     }
1828*dd583296SKevin Wolf     g_free(input);
1829*dd583296SKevin Wolf     g_free(v);
1830*dd583296SKevin Wolf 
1831*dd583296SKevin Wolf     return done;
1832*dd583296SKevin Wolf }
1833*dd583296SKevin Wolf 
1834797ac58cSKevin Wolf static void __attribute((constructor)) init_qemuio_commands(void)
1835797ac58cSKevin Wolf {
1836797ac58cSKevin Wolf     /* initialize commands */
1837797ac58cSKevin Wolf     help_init();
1838797ac58cSKevin Wolf     add_command(&read_cmd);
1839797ac58cSKevin Wolf     add_command(&readv_cmd);
1840797ac58cSKevin Wolf     add_command(&write_cmd);
1841797ac58cSKevin Wolf     add_command(&writev_cmd);
1842797ac58cSKevin Wolf     add_command(&multiwrite_cmd);
1843797ac58cSKevin Wolf     add_command(&aio_read_cmd);
1844797ac58cSKevin Wolf     add_command(&aio_write_cmd);
1845797ac58cSKevin Wolf     add_command(&aio_flush_cmd);
1846797ac58cSKevin Wolf     add_command(&flush_cmd);
1847797ac58cSKevin Wolf     add_command(&truncate_cmd);
1848797ac58cSKevin Wolf     add_command(&length_cmd);
1849797ac58cSKevin Wolf     add_command(&info_cmd);
1850797ac58cSKevin Wolf     add_command(&discard_cmd);
1851797ac58cSKevin Wolf     add_command(&alloc_cmd);
1852797ac58cSKevin Wolf     add_command(&map_cmd);
1853797ac58cSKevin Wolf     add_command(&break_cmd);
1854797ac58cSKevin Wolf     add_command(&resume_cmd);
1855797ac58cSKevin Wolf     add_command(&wait_break_cmd);
1856797ac58cSKevin Wolf     add_command(&abort_cmd);
1857797ac58cSKevin Wolf 
1858797ac58cSKevin Wolf     add_check_command(init_check_command);
1859797ac58cSKevin Wolf }
1860