blob: 39d70638161e59f27f872e54fb2f3394eb42f5a3 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
Stefan Weilc32d7662009-08-31 22:16:16 +020010#include <sys/time.h>
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020015#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000016
17#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/block_int.h"
aliguorie3aff4f2009-04-05 19:14:04 +000020#include "cmd.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000021#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000022
23#define VERSION "0.0.1"
24
Devin Nakamura43642b32011-07-11 11:22:16 -040025#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000026
27char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000028
Kevin Wolf734c3b82013-06-05 14:19:30 +020029BlockDriverState *qemuio_bs;
aliguorie3aff4f2009-04-05 19:14:04 +000030static int misalign;
31
Kevin Wolfb6e356a2013-06-05 14:19:28 +020032static int64_t cvtnum(const char *s)
33{
34 char *end;
35 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
36}
37
aliguorie3aff4f2009-04-05 19:14:04 +000038/*
Christoph Hellwigcf070d72009-07-20 01:19:25 +020039 * Parse the pattern argument to various sub-commands.
40 *
41 * Because the pattern is used as an argument to memset it must evaluate
42 * to an unsigned integer that fits into a single byte.
43 */
44static int parse_pattern(const char *arg)
45{
Devin Nakamura43642b32011-07-11 11:22:16 -040046 char *endptr = NULL;
47 long pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020048
Devin Nakamura43642b32011-07-11 11:22:16 -040049 pattern = strtol(arg, &endptr, 0);
50 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
51 printf("%s is not a valid pattern byte\n", arg);
52 return -1;
53 }
Christoph Hellwigcf070d72009-07-20 01:19:25 +020054
Devin Nakamura43642b32011-07-11 11:22:16 -040055 return pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020056}
57
58/*
aliguorie3aff4f2009-04-05 19:14:04 +000059 * Memory allocation helpers.
60 *
61 * Make sure memory is aligned by default, or purposefully misaligned if
62 * that is specified on the command line.
63 */
64
Devin Nakamura43642b32011-07-11 11:22:16 -040065#define MISALIGN_OFFSET 16
Kevin Wolf734c3b82013-06-05 14:19:30 +020066static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
aliguorie3aff4f2009-04-05 19:14:04 +000067{
Devin Nakamura43642b32011-07-11 11:22:16 -040068 void *buf;
aliguorie3aff4f2009-04-05 19:14:04 +000069
Devin Nakamura43642b32011-07-11 11:22:16 -040070 if (misalign) {
71 len += MISALIGN_OFFSET;
72 }
73 buf = qemu_blockalign(bs, len);
74 memset(buf, pattern, len);
75 if (misalign) {
76 buf += MISALIGN_OFFSET;
77 }
78 return buf;
aliguorie3aff4f2009-04-05 19:14:04 +000079}
80
81static void qemu_io_free(void *p)
82{
Devin Nakamura43642b32011-07-11 11:22:16 -040083 if (misalign) {
84 p -= MISALIGN_OFFSET;
85 }
86 qemu_vfree(p);
aliguorie3aff4f2009-04-05 19:14:04 +000087}
88
Devin Nakamura43642b32011-07-11 11:22:16 -040089static void dump_buffer(const void *buffer, int64_t offset, int len)
aliguorie3aff4f2009-04-05 19:14:04 +000090{
Devin Nakamura43642b32011-07-11 11:22:16 -040091 int i, j;
92 const uint8_t *p;
aliguorie3aff4f2009-04-05 19:14:04 +000093
Devin Nakamura43642b32011-07-11 11:22:16 -040094 for (i = 0, p = buffer; i < len; i += 16) {
95 const uint8_t *s = p;
aliguorie3aff4f2009-04-05 19:14:04 +000096
Devin Nakamura43642b32011-07-11 11:22:16 -040097 printf("%08" PRIx64 ": ", offset + i);
98 for (j = 0; j < 16 && i + j < len; j++, p++) {
99 printf("%02x ", *p);
100 }
101 printf(" ");
102 for (j = 0; j < 16 && i + j < len; j++, s++) {
103 if (isalnum(*s)) {
104 printf("%c", *s);
105 } else {
106 printf(".");
107 }
108 }
109 printf("\n");
110 }
aliguorie3aff4f2009-04-05 19:14:04 +0000111}
112
Devin Nakamura43642b32011-07-11 11:22:16 -0400113static void print_report(const char *op, struct timeval *t, int64_t offset,
114 int count, int total, int cnt, int Cflag)
aliguorie3aff4f2009-04-05 19:14:04 +0000115{
Devin Nakamura43642b32011-07-11 11:22:16 -0400116 char s1[64], s2[64], ts[64];
aliguorie3aff4f2009-04-05 19:14:04 +0000117
Devin Nakamura43642b32011-07-11 11:22:16 -0400118 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
119 if (!Cflag) {
120 cvtstr((double)total, s1, sizeof(s1));
121 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
122 printf("%s %d/%d bytes at offset %" PRId64 "\n",
123 op, total, count, offset);
124 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
125 s1, cnt, ts, s2, tdiv((double)cnt, *t));
126 } else {/* bytes,ops,time,bytes/sec,ops/sec */
127 printf("%d,%d,%s,%.3f,%.3f\n",
128 total, cnt, ts,
129 tdiv((double)total, *t),
130 tdiv((double)cnt, *t));
131 }
aliguorie3aff4f2009-04-05 19:14:04 +0000132}
133
Christoph Hellwigcf572982009-07-10 13:33:42 +0200134/*
135 * Parse multiple length statements for vectored I/O, and construct an I/O
136 * vector matching it.
137 */
138static void *
Kevin Wolf734c3b82013-06-05 14:19:30 +0200139create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
140 int pattern)
Christoph Hellwigcf572982009-07-10 13:33:42 +0200141{
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000142 size_t *sizes = g_new0(size_t, nr_iov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400143 size_t count = 0;
144 void *buf = NULL;
145 void *p;
146 int i;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200147
Devin Nakamura43642b32011-07-11 11:22:16 -0400148 for (i = 0; i < nr_iov; i++) {
149 char *arg = argv[i];
150 int64_t len;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200151
Devin Nakamura43642b32011-07-11 11:22:16 -0400152 len = cvtnum(arg);
153 if (len < 0) {
154 printf("non-numeric length argument -- %s\n", arg);
155 goto fail;
156 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200157
Devin Nakamura43642b32011-07-11 11:22:16 -0400158 /* should be SIZE_T_MAX, but that doesn't exist */
159 if (len > INT_MAX) {
160 printf("too large length argument -- %s\n", arg);
161 goto fail;
162 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200163
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 if (len & 0x1ff) {
165 printf("length argument %" PRId64
166 " is not sector aligned\n", len);
167 goto fail;
168 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200169
Devin Nakamura43642b32011-07-11 11:22:16 -0400170 sizes[i] = len;
171 count += len;
172 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200173
Devin Nakamura43642b32011-07-11 11:22:16 -0400174 qemu_iovec_init(qiov, nr_iov);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200175
Kevin Wolf734c3b82013-06-05 14:19:30 +0200176 buf = p = qemu_io_alloc(bs, count, pattern);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200177
Devin Nakamura43642b32011-07-11 11:22:16 -0400178 for (i = 0; i < nr_iov; i++) {
179 qemu_iovec_add(qiov, p, sizes[i]);
180 p += sizes[i];
181 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200182
Kevin Wolf40a0d7c2009-11-18 10:42:59 +0100183fail:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000184 g_free(sizes);
Devin Nakamura43642b32011-07-11 11:22:16 -0400185 return buf;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200186}
187
Kevin Wolf734c3b82013-06-05 14:19:30 +0200188static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
189 int *total)
aliguorie3aff4f2009-04-05 19:14:04 +0000190{
Devin Nakamura43642b32011-07-11 11:22:16 -0400191 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000192
Devin Nakamura43642b32011-07-11 11:22:16 -0400193 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
194 if (ret < 0) {
195 return ret;
196 }
197 *total = count;
198 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000199}
200
Kevin Wolf734c3b82013-06-05 14:19:30 +0200201static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
202 int *total)
aliguorie3aff4f2009-04-05 19:14:04 +0000203{
Devin Nakamura43642b32011-07-11 11:22:16 -0400204 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000205
Devin Nakamura43642b32011-07-11 11:22:16 -0400206 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
207 if (ret < 0) {
208 return ret;
209 }
210 *total = count;
211 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000212}
213
Kevin Wolf734c3b82013-06-05 14:19:30 +0200214static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
215 int *total)
aliguorie3aff4f2009-04-05 19:14:04 +0000216{
Devin Nakamura43642b32011-07-11 11:22:16 -0400217 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
218 if (*total < 0) {
219 return *total;
220 }
221 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000222}
223
Kevin Wolf734c3b82013-06-05 14:19:30 +0200224static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
225 int *total)
aliguorie3aff4f2009-04-05 19:14:04 +0000226{
Devin Nakamura43642b32011-07-11 11:22:16 -0400227 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
228 if (*total < 0) {
229 return *total;
230 }
231 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000232}
233
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000234typedef struct {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200235 BlockDriverState *bs;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000236 int64_t offset;
237 int count;
238 int *total;
239 int ret;
240 bool done;
241} CoWriteZeroes;
242
243static void coroutine_fn co_write_zeroes_entry(void *opaque)
244{
245 CoWriteZeroes *data = opaque;
246
Kevin Wolf734c3b82013-06-05 14:19:30 +0200247 data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000248 data->count / BDRV_SECTOR_SIZE);
249 data->done = true;
250 if (data->ret < 0) {
251 *data->total = data->ret;
252 return;
253 }
254
255 *data->total = data->count;
256}
257
Kevin Wolf734c3b82013-06-05 14:19:30 +0200258static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
259 int *total)
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000260{
261 Coroutine *co;
262 CoWriteZeroes data = {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200263 .bs = bs,
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000264 .offset = offset,
265 .count = count,
266 .total = total,
267 .done = false,
268 };
269
270 co = qemu_coroutine_create(co_write_zeroes_entry);
271 qemu_coroutine_enter(co, &data);
272 while (!data.done) {
273 qemu_aio_wait();
274 }
275 if (data.ret < 0) {
276 return data.ret;
277 } else {
278 return 1;
279 }
280}
281
Kevin Wolf734c3b82013-06-05 14:19:30 +0200282static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
283 int count, int *total)
Kevin Wolf791bfa32012-12-04 16:35:12 +0100284{
285 int ret;
286
287 ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
288 if (ret < 0) {
289 return ret;
290 }
291 *total = count;
292 return 1;
293}
294
Kevin Wolf734c3b82013-06-05 14:19:30 +0200295static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
296 int count, int *total)
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200297{
Devin Nakamura43642b32011-07-11 11:22:16 -0400298 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
299 if (*total < 0) {
300 return *total;
301 }
302 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200303}
304
Kevin Wolf734c3b82013-06-05 14:19:30 +0200305static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
306 int count, int *total)
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200307{
Devin Nakamura43642b32011-07-11 11:22:16 -0400308 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
309 if (*total < 0) {
310 return *total;
311 }
312 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200313}
314
aliguorie3aff4f2009-04-05 19:14:04 +0000315#define NOT_DONE 0x7fffffff
316static void aio_rw_done(void *opaque, int ret)
317{
Devin Nakamura43642b32011-07-11 11:22:16 -0400318 *(int *)opaque = ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000319}
320
Kevin Wolf734c3b82013-06-05 14:19:30 +0200321static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
322 int64_t offset, int *total)
aliguorie3aff4f2009-04-05 19:14:04 +0000323{
Devin Nakamura43642b32011-07-11 11:22:16 -0400324 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000325
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100326 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
327 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400328 while (async_ret == NOT_DONE) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200329 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400330 }
aliguorie3aff4f2009-04-05 19:14:04 +0000331
Devin Nakamura43642b32011-07-11 11:22:16 -0400332 *total = qiov->size;
333 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000334}
335
Kevin Wolf734c3b82013-06-05 14:19:30 +0200336static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
337 int64_t offset, int *total)
aliguorie3aff4f2009-04-05 19:14:04 +0000338{
Devin Nakamura43642b32011-07-11 11:22:16 -0400339 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000340
Paolo Bonziniad54ae82011-11-30 09:12:30 +0100341 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
342 aio_rw_done, &async_ret);
Devin Nakamura43642b32011-07-11 11:22:16 -0400343 while (async_ret == NOT_DONE) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200344 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400345 }
aliguorie3aff4f2009-04-05 19:14:04 +0000346
Devin Nakamura43642b32011-07-11 11:22:16 -0400347 *total = qiov->size;
348 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000349}
350
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200351struct multiwrite_async_ret {
Devin Nakamura43642b32011-07-11 11:22:16 -0400352 int num_done;
353 int error;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200354};
355
356static void multiwrite_cb(void *opaque, int ret)
357{
Devin Nakamura43642b32011-07-11 11:22:16 -0400358 struct multiwrite_async_ret *async_ret = opaque;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200359
Devin Nakamura43642b32011-07-11 11:22:16 -0400360 async_ret->num_done++;
361 if (ret < 0) {
362 async_ret->error = ret;
363 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200364}
365
Kevin Wolf734c3b82013-06-05 14:19:30 +0200366static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
367 int num_reqs, int *total)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200368{
Devin Nakamura43642b32011-07-11 11:22:16 -0400369 int i, ret;
370 struct multiwrite_async_ret async_ret = {
371 .num_done = 0,
372 .error = 0,
373 };
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200374
Devin Nakamura43642b32011-07-11 11:22:16 -0400375 *total = 0;
376 for (i = 0; i < num_reqs; i++) {
377 reqs[i].cb = multiwrite_cb;
378 reqs[i].opaque = &async_ret;
379 *total += reqs[i].qiov->size;
380 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200381
Devin Nakamura43642b32011-07-11 11:22:16 -0400382 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
383 if (ret < 0) {
384 return ret;
385 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200386
Devin Nakamura43642b32011-07-11 11:22:16 -0400387 while (async_ret.num_done < num_reqs) {
Paolo Bonzinia5a52382012-04-12 14:00:51 +0200388 main_loop_wait(false);
Devin Nakamura43642b32011-07-11 11:22:16 -0400389 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200390
Devin Nakamura43642b32011-07-11 11:22:16 -0400391 return async_ret.error < 0 ? async_ret.error : 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200392}
aliguorie3aff4f2009-04-05 19:14:04 +0000393
Devin Nakamura43642b32011-07-11 11:22:16 -0400394static void read_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000395{
Devin Nakamura43642b32011-07-11 11:22:16 -0400396 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000397"\n"
398" reads a range of bytes from the given offset\n"
399"\n"
400" Example:\n"
401" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
402"\n"
403" Reads a segment of the currently open file, optionally dumping it to the\n"
404" standard output stream (with -v option) for subsequent inspection.\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200405" -b, -- read from the VM state rather than the virtual disk\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200406" -C, -- report statistics in a machine parsable format\n"
407" -l, -- length for pattern verification (only with -P)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000408" -p, -- use bdrv_pread to read the file\n"
aliguoric48101a2009-04-18 15:36:23 +0000409" -P, -- use a pattern to verify read data\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100410" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200411" -s, -- start offset for pattern verification (only with -P)\n"
412" -v, -- dump buffer to standard output\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000413"\n");
414}
415
Kevin Wolf734c3b82013-06-05 14:19:30 +0200416static int read_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000417
418static const cmdinfo_t read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400419 .name = "read",
420 .altname = "r",
421 .cfunc = read_f,
422 .argmin = 2,
423 .argmax = -1,
424 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
425 .oneline = "reads a number of bytes at a specified offset",
426 .help = read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000427};
428
Kevin Wolf734c3b82013-06-05 14:19:30 +0200429static int read_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000430{
Devin Nakamura43642b32011-07-11 11:22:16 -0400431 struct timeval t1, t2;
432 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
433 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
434 int c, cnt;
435 char *buf;
436 int64_t offset;
437 int count;
438 /* Some compilers get confused and warn if this is not initialized. */
439 int total = 0;
440 int pattern = 0, pattern_offset = 0, pattern_count = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000441
Devin Nakamura43642b32011-07-11 11:22:16 -0400442 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
443 switch (c) {
444 case 'b':
445 bflag = 1;
446 break;
447 case 'C':
448 Cflag = 1;
449 break;
450 case 'l':
451 lflag = 1;
452 pattern_count = cvtnum(optarg);
453 if (pattern_count < 0) {
454 printf("non-numeric length argument -- %s\n", optarg);
455 return 0;
456 }
457 break;
458 case 'p':
459 pflag = 1;
460 break;
461 case 'P':
462 Pflag = 1;
463 pattern = parse_pattern(optarg);
464 if (pattern < 0) {
465 return 0;
466 }
467 break;
468 case 'q':
469 qflag = 1;
470 break;
471 case 's':
472 sflag = 1;
473 pattern_offset = cvtnum(optarg);
474 if (pattern_offset < 0) {
475 printf("non-numeric length argument -- %s\n", optarg);
476 return 0;
477 }
478 break;
479 case 'v':
480 vflag = 1;
481 break;
482 default:
483 return command_usage(&read_cmd);
484 }
485 }
aliguorie3aff4f2009-04-05 19:14:04 +0000486
Devin Nakamura43642b32011-07-11 11:22:16 -0400487 if (optind != argc - 2) {
488 return command_usage(&read_cmd);
489 }
aliguorie3aff4f2009-04-05 19:14:04 +0000490
Devin Nakamura43642b32011-07-11 11:22:16 -0400491 if (bflag && pflag) {
492 printf("-b and -p cannot be specified at the same time\n");
493 return 0;
494 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200495
Devin Nakamura43642b32011-07-11 11:22:16 -0400496 offset = cvtnum(argv[optind]);
497 if (offset < 0) {
498 printf("non-numeric length argument -- %s\n", argv[optind]);
499 return 0;
500 }
aliguorie3aff4f2009-04-05 19:14:04 +0000501
Devin Nakamura43642b32011-07-11 11:22:16 -0400502 optind++;
503 count = cvtnum(argv[optind]);
504 if (count < 0) {
505 printf("non-numeric length argument -- %s\n", argv[optind]);
506 return 0;
507 }
aliguorie3aff4f2009-04-05 19:14:04 +0000508
Kevin Wolfd9654a52009-04-23 15:11:13 +0200509 if (!Pflag && (lflag || sflag)) {
510 return command_usage(&read_cmd);
511 }
512
513 if (!lflag) {
514 pattern_count = count - pattern_offset;
515 }
516
517 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
Dong Xu Wang07f35072011-11-22 18:06:26 +0800518 printf("pattern verification range exceeds end of read data\n");
Kevin Wolfd9654a52009-04-23 15:11:13 +0200519 return 0;
520 }
521
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400522 if (!pflag) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400523 if (offset & 0x1ff) {
524 printf("offset %" PRId64 " is not sector aligned\n",
525 offset);
526 return 0;
527 }
528 if (count & 0x1ff) {
529 printf("count %d is not sector aligned\n",
530 count);
531 return 0;
532 }
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400533 }
aliguorie3aff4f2009-04-05 19:14:04 +0000534
Kevin Wolf734c3b82013-06-05 14:19:30 +0200535 buf = qemu_io_alloc(bs, count, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000536
Devin Nakamura43642b32011-07-11 11:22:16 -0400537 gettimeofday(&t1, NULL);
538 if (pflag) {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200539 cnt = do_pread(bs, buf, offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400540 } else if (bflag) {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200541 cnt = do_load_vmstate(bs, buf, offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400542 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200543 cnt = do_read(bs, buf, offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 }
545 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000546
Devin Nakamura43642b32011-07-11 11:22:16 -0400547 if (cnt < 0) {
548 printf("read failed: %s\n", strerror(-cnt));
549 goto out;
550 }
aliguorie3aff4f2009-04-05 19:14:04 +0000551
Devin Nakamura43642b32011-07-11 11:22:16 -0400552 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000553 void *cmp_buf = g_malloc(pattern_count);
Devin Nakamura43642b32011-07-11 11:22:16 -0400554 memset(cmp_buf, pattern, pattern_count);
555 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
556 printf("Pattern verification failed at offset %"
557 PRId64 ", %d bytes\n",
558 offset + pattern_offset, pattern_count);
559 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000560 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400561 }
aliguorie3aff4f2009-04-05 19:14:04 +0000562
Devin Nakamura43642b32011-07-11 11:22:16 -0400563 if (qflag) {
564 goto out;
565 }
aliguoric48101a2009-04-18 15:36:23 +0000566
Devin Nakamura43642b32011-07-11 11:22:16 -0400567 if (vflag) {
568 dump_buffer(buf, offset, count);
569 }
aliguorie3aff4f2009-04-05 19:14:04 +0000570
Devin Nakamura43642b32011-07-11 11:22:16 -0400571 /* Finally, report back -- -C gives a parsable format */
572 t2 = tsub(t2, t1);
573 print_report("read", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000574
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200575out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400576 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000577
Devin Nakamura43642b32011-07-11 11:22:16 -0400578 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000579}
580
Devin Nakamura43642b32011-07-11 11:22:16 -0400581static void readv_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000582{
Devin Nakamura43642b32011-07-11 11:22:16 -0400583 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000584"\n"
585" reads a range of bytes from the given offset into multiple buffers\n"
586"\n"
587" Example:\n"
588" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
589"\n"
590" Reads a segment of the currently open file, optionally dumping it to the\n"
591" standard output stream (with -v option) for subsequent inspection.\n"
592" Uses multiple iovec buffers if more than one byte range is specified.\n"
593" -C, -- report statistics in a machine parsable format\n"
aliguoric48101a2009-04-18 15:36:23 +0000594" -P, -- use a pattern to verify read data\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000595" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100596" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000597"\n");
598}
599
Kevin Wolf734c3b82013-06-05 14:19:30 +0200600static int readv_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000601
602static const cmdinfo_t readv_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400603 .name = "readv",
604 .cfunc = readv_f,
605 .argmin = 2,
606 .argmax = -1,
607 .args = "[-Cqv] [-P pattern ] off len [len..]",
608 .oneline = "reads a number of bytes at a specified offset",
609 .help = readv_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000610};
611
Kevin Wolf734c3b82013-06-05 14:19:30 +0200612static int readv_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000613{
Devin Nakamura43642b32011-07-11 11:22:16 -0400614 struct timeval t1, t2;
615 int Cflag = 0, qflag = 0, vflag = 0;
616 int c, cnt;
617 char *buf;
618 int64_t offset;
619 /* Some compilers get confused and warn if this is not initialized. */
620 int total = 0;
621 int nr_iov;
622 QEMUIOVector qiov;
623 int pattern = 0;
624 int Pflag = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000625
Devin Nakamura43642b32011-07-11 11:22:16 -0400626 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
627 switch (c) {
628 case 'C':
629 Cflag = 1;
630 break;
631 case 'P':
632 Pflag = 1;
633 pattern = parse_pattern(optarg);
634 if (pattern < 0) {
635 return 0;
636 }
637 break;
638 case 'q':
639 qflag = 1;
640 break;
641 case 'v':
642 vflag = 1;
643 break;
644 default:
645 return command_usage(&readv_cmd);
646 }
647 }
aliguorie3aff4f2009-04-05 19:14:04 +0000648
Devin Nakamura43642b32011-07-11 11:22:16 -0400649 if (optind > argc - 2) {
650 return command_usage(&readv_cmd);
651 }
aliguorie3aff4f2009-04-05 19:14:04 +0000652
653
Devin Nakamura43642b32011-07-11 11:22:16 -0400654 offset = cvtnum(argv[optind]);
655 if (offset < 0) {
656 printf("non-numeric length argument -- %s\n", argv[optind]);
657 return 0;
658 }
659 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000660
Devin Nakamura43642b32011-07-11 11:22:16 -0400661 if (offset & 0x1ff) {
662 printf("offset %" PRId64 " is not sector aligned\n",
663 offset);
664 return 0;
665 }
aliguorie3aff4f2009-04-05 19:14:04 +0000666
Devin Nakamura43642b32011-07-11 11:22:16 -0400667 nr_iov = argc - optind;
Kevin Wolf734c3b82013-06-05 14:19:30 +0200668 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +0100669 if (buf == NULL) {
670 return 0;
671 }
aliguorie3aff4f2009-04-05 19:14:04 +0000672
Devin Nakamura43642b32011-07-11 11:22:16 -0400673 gettimeofday(&t1, NULL);
Kevin Wolf734c3b82013-06-05 14:19:30 +0200674 cnt = do_aio_readv(bs, &qiov, offset, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400675 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000676
Devin Nakamura43642b32011-07-11 11:22:16 -0400677 if (cnt < 0) {
678 printf("readv failed: %s\n", strerror(-cnt));
679 goto out;
680 }
aliguorie3aff4f2009-04-05 19:14:04 +0000681
Devin Nakamura43642b32011-07-11 11:22:16 -0400682 if (Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000683 void *cmp_buf = g_malloc(qiov.size);
Devin Nakamura43642b32011-07-11 11:22:16 -0400684 memset(cmp_buf, pattern, qiov.size);
685 if (memcmp(buf, cmp_buf, qiov.size)) {
686 printf("Pattern verification failed at offset %"
687 PRId64 ", %zd bytes\n", offset, qiov.size);
688 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +0000689 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -0400690 }
aliguoric48101a2009-04-18 15:36:23 +0000691
Devin Nakamura43642b32011-07-11 11:22:16 -0400692 if (qflag) {
693 goto out;
694 }
aliguorie3aff4f2009-04-05 19:14:04 +0000695
Devin Nakamura43642b32011-07-11 11:22:16 -0400696 if (vflag) {
697 dump_buffer(buf, offset, qiov.size);
698 }
aliguorie3aff4f2009-04-05 19:14:04 +0000699
Devin Nakamura43642b32011-07-11 11:22:16 -0400700 /* Finally, report back -- -C gives a parsable format */
701 t2 = tsub(t2, t1);
702 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000703
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200704out:
Kevin Wolf9e559532012-07-02 15:13:53 +0200705 qemu_iovec_destroy(&qiov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400706 qemu_io_free(buf);
707 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000708}
709
Devin Nakamura43642b32011-07-11 11:22:16 -0400710static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000711{
Devin Nakamura43642b32011-07-11 11:22:16 -0400712 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000713"\n"
714" writes a range of bytes from the given offset\n"
715"\n"
716" Example:\n"
717" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
718"\n"
719" Writes into a segment of the currently open file, using a buffer\n"
720" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200721" -b, -- write to the VM state rather than the virtual disk\n"
Kevin Wolf791bfa32012-12-04 16:35:12 +0100722" -c, -- write compressed data with bdrv_write_compressed\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000723" -p, -- use bdrv_pwrite to write the file\n"
724" -P, -- use different pattern to fill file\n"
725" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100726" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000727" -z, -- write zeroes using bdrv_co_write_zeroes\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000728"\n");
729}
730
Kevin Wolf734c3b82013-06-05 14:19:30 +0200731static int write_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000732
733static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400734 .name = "write",
735 .altname = "w",
736 .cfunc = write_f,
737 .argmin = 2,
738 .argmax = -1,
Kevin Wolf791bfa32012-12-04 16:35:12 +0100739 .args = "[-bcCpqz] [-P pattern ] off len",
Devin Nakamura43642b32011-07-11 11:22:16 -0400740 .oneline = "writes a number of bytes at a specified offset",
741 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000742};
743
Kevin Wolf734c3b82013-06-05 14:19:30 +0200744static int write_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000745{
Devin Nakamura43642b32011-07-11 11:22:16 -0400746 struct timeval t1, t2;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000747 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
Kevin Wolf791bfa32012-12-04 16:35:12 +0100748 int cflag = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400749 int c, cnt;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000750 char *buf = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400751 int64_t offset;
752 int count;
753 /* Some compilers get confused and warn if this is not initialized. */
754 int total = 0;
755 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000756
Kevin Wolf791bfa32012-12-04 16:35:12 +0100757 while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400758 switch (c) {
759 case 'b':
760 bflag = 1;
761 break;
Kevin Wolf791bfa32012-12-04 16:35:12 +0100762 case 'c':
763 cflag = 1;
764 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400765 case 'C':
766 Cflag = 1;
767 break;
768 case 'p':
769 pflag = 1;
770 break;
771 case 'P':
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000772 Pflag = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400773 pattern = parse_pattern(optarg);
774 if (pattern < 0) {
775 return 0;
776 }
777 break;
778 case 'q':
779 qflag = 1;
780 break;
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000781 case 'z':
782 zflag = 1;
783 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400784 default:
785 return command_usage(&write_cmd);
786 }
787 }
aliguorie3aff4f2009-04-05 19:14:04 +0000788
Devin Nakamura43642b32011-07-11 11:22:16 -0400789 if (optind != argc - 2) {
790 return command_usage(&write_cmd);
791 }
aliguorie3aff4f2009-04-05 19:14:04 +0000792
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000793 if (bflag + pflag + zflag > 1) {
794 printf("-b, -p, or -z cannot be specified at the same time\n");
795 return 0;
796 }
797
798 if (zflag && Pflag) {
799 printf("-z and -P cannot be specified at the same time\n");
Devin Nakamura43642b32011-07-11 11:22:16 -0400800 return 0;
801 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200802
Devin Nakamura43642b32011-07-11 11:22:16 -0400803 offset = cvtnum(argv[optind]);
804 if (offset < 0) {
805 printf("non-numeric length argument -- %s\n", argv[optind]);
806 return 0;
807 }
aliguorie3aff4f2009-04-05 19:14:04 +0000808
Devin Nakamura43642b32011-07-11 11:22:16 -0400809 optind++;
810 count = cvtnum(argv[optind]);
811 if (count < 0) {
812 printf("non-numeric length argument -- %s\n", argv[optind]);
813 return 0;
814 }
aliguorie3aff4f2009-04-05 19:14:04 +0000815
Devin Nakamura43642b32011-07-11 11:22:16 -0400816 if (!pflag) {
817 if (offset & 0x1ff) {
818 printf("offset %" PRId64 " is not sector aligned\n",
819 offset);
820 return 0;
821 }
aliguorie3aff4f2009-04-05 19:14:04 +0000822
Devin Nakamura43642b32011-07-11 11:22:16 -0400823 if (count & 0x1ff) {
824 printf("count %d is not sector aligned\n",
825 count);
826 return 0;
827 }
828 }
aliguorie3aff4f2009-04-05 19:14:04 +0000829
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000830 if (!zflag) {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200831 buf = qemu_io_alloc(bs, count, pattern);
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000832 }
aliguorie3aff4f2009-04-05 19:14:04 +0000833
Devin Nakamura43642b32011-07-11 11:22:16 -0400834 gettimeofday(&t1, NULL);
835 if (pflag) {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200836 cnt = do_pwrite(bs, buf, offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400837 } else if (bflag) {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200838 cnt = do_save_vmstate(bs, buf, offset, count, &total);
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000839 } else if (zflag) {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200840 cnt = do_co_write_zeroes(bs, offset, count, &total);
Kevin Wolf791bfa32012-12-04 16:35:12 +0100841 } else if (cflag) {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200842 cnt = do_write_compressed(bs, buf, offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400843 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +0200844 cnt = do_write(bs, buf, offset, count, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400845 }
846 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000847
Devin Nakamura43642b32011-07-11 11:22:16 -0400848 if (cnt < 0) {
849 printf("write failed: %s\n", strerror(-cnt));
850 goto out;
851 }
aliguorie3aff4f2009-04-05 19:14:04 +0000852
Devin Nakamura43642b32011-07-11 11:22:16 -0400853 if (qflag) {
854 goto out;
855 }
aliguorie3aff4f2009-04-05 19:14:04 +0000856
Devin Nakamura43642b32011-07-11 11:22:16 -0400857 /* Finally, report back -- -C gives a parsable format */
858 t2 = tsub(t2, t1);
859 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000860
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200861out:
Stefan Hajnoczi71b58b82012-02-07 13:27:29 +0000862 if (!zflag) {
863 qemu_io_free(buf);
864 }
aliguorie3aff4f2009-04-05 19:14:04 +0000865
Devin Nakamura43642b32011-07-11 11:22:16 -0400866 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000867}
868
aliguorie3aff4f2009-04-05 19:14:04 +0000869static void
870writev_help(void)
871{
Devin Nakamura43642b32011-07-11 11:22:16 -0400872 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000873"\n"
874" writes a range of bytes from the given offset source from multiple buffers\n"
875"\n"
876" Example:\n"
877" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
878"\n"
879" Writes into a segment of the currently open file, using a buffer\n"
880" filled with a set pattern (0xcdcdcdcd).\n"
881" -P, -- use different pattern to fill file\n"
882" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100883" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000884"\n");
885}
886
Kevin Wolf734c3b82013-06-05 14:19:30 +0200887static int writev_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000888
889static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400890 .name = "writev",
891 .cfunc = writev_f,
892 .argmin = 2,
893 .argmax = -1,
894 .args = "[-Cq] [-P pattern ] off len [len..]",
895 .oneline = "writes a number of bytes at a specified offset",
896 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000897};
898
Kevin Wolf734c3b82013-06-05 14:19:30 +0200899static int writev_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000900{
Devin Nakamura43642b32011-07-11 11:22:16 -0400901 struct timeval t1, t2;
902 int Cflag = 0, qflag = 0;
903 int c, cnt;
904 char *buf;
905 int64_t offset;
906 /* Some compilers get confused and warn if this is not initialized. */
907 int total = 0;
908 int nr_iov;
909 int pattern = 0xcd;
910 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000911
Devin Nakamura43642b32011-07-11 11:22:16 -0400912 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
913 switch (c) {
914 case 'C':
915 Cflag = 1;
916 break;
917 case 'q':
918 qflag = 1;
919 break;
920 case 'P':
921 pattern = parse_pattern(optarg);
922 if (pattern < 0) {
923 return 0;
924 }
925 break;
926 default:
927 return command_usage(&writev_cmd);
928 }
929 }
aliguorie3aff4f2009-04-05 19:14:04 +0000930
Devin Nakamura43642b32011-07-11 11:22:16 -0400931 if (optind > argc - 2) {
932 return command_usage(&writev_cmd);
933 }
aliguorie3aff4f2009-04-05 19:14:04 +0000934
Devin Nakamura43642b32011-07-11 11:22:16 -0400935 offset = cvtnum(argv[optind]);
936 if (offset < 0) {
937 printf("non-numeric length argument -- %s\n", argv[optind]);
938 return 0;
939 }
940 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000941
Devin Nakamura43642b32011-07-11 11:22:16 -0400942 if (offset & 0x1ff) {
943 printf("offset %" PRId64 " is not sector aligned\n",
944 offset);
945 return 0;
946 }
aliguorie3aff4f2009-04-05 19:14:04 +0000947
Devin Nakamura43642b32011-07-11 11:22:16 -0400948 nr_iov = argc - optind;
Kevin Wolf734c3b82013-06-05 14:19:30 +0200949 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +0100950 if (buf == NULL) {
951 return 0;
952 }
aliguorie3aff4f2009-04-05 19:14:04 +0000953
Devin Nakamura43642b32011-07-11 11:22:16 -0400954 gettimeofday(&t1, NULL);
Kevin Wolf734c3b82013-06-05 14:19:30 +0200955 cnt = do_aio_writev(bs, &qiov, offset, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -0400956 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000957
Devin Nakamura43642b32011-07-11 11:22:16 -0400958 if (cnt < 0) {
959 printf("writev failed: %s\n", strerror(-cnt));
960 goto out;
961 }
aliguorie3aff4f2009-04-05 19:14:04 +0000962
Devin Nakamura43642b32011-07-11 11:22:16 -0400963 if (qflag) {
964 goto out;
965 }
aliguorie3aff4f2009-04-05 19:14:04 +0000966
Devin Nakamura43642b32011-07-11 11:22:16 -0400967 /* Finally, report back -- -C gives a parsable format */
968 t2 = tsub(t2, t1);
969 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200970out:
Kevin Wolf9e559532012-07-02 15:13:53 +0200971 qemu_iovec_destroy(&qiov);
Devin Nakamura43642b32011-07-11 11:22:16 -0400972 qemu_io_free(buf);
973 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000974}
975
Devin Nakamura43642b32011-07-11 11:22:16 -0400976static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200977{
Devin Nakamura43642b32011-07-11 11:22:16 -0400978 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200979"\n"
980" writes a range of bytes from the given offset source from multiple buffers,\n"
981" in a batch of requests that may be merged by qemu\n"
982"\n"
983" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200984" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200985" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
986"\n"
987" Writes into a segment of the currently open file, using a buffer\n"
988" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
989" by one for each request contained in the multiwrite command.\n"
990" -P, -- use different pattern to fill file\n"
991" -C, -- report statistics in a machine parsable format\n"
992" -q, -- quiet mode, do not show I/O statistics\n"
993"\n");
994}
995
Kevin Wolf734c3b82013-06-05 14:19:30 +0200996static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200997
998static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400999 .name = "multiwrite",
1000 .cfunc = multiwrite_f,
1001 .argmin = 2,
1002 .argmax = -1,
1003 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1004 .oneline = "issues multiple write requests at once",
1005 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001006};
1007
Kevin Wolf734c3b82013-06-05 14:19:30 +02001008static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001009{
Devin Nakamura43642b32011-07-11 11:22:16 -04001010 struct timeval t1, t2;
1011 int Cflag = 0, qflag = 0;
1012 int c, cnt;
1013 char **buf;
1014 int64_t offset, first_offset = 0;
1015 /* Some compilers get confused and warn if this is not initialized. */
1016 int total = 0;
1017 int nr_iov;
1018 int nr_reqs;
1019 int pattern = 0xcd;
1020 QEMUIOVector *qiovs;
1021 int i;
1022 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001023
Devin Nakamura43642b32011-07-11 11:22:16 -04001024 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1025 switch (c) {
1026 case 'C':
1027 Cflag = 1;
1028 break;
1029 case 'q':
1030 qflag = 1;
1031 break;
1032 case 'P':
1033 pattern = parse_pattern(optarg);
1034 if (pattern < 0) {
1035 return 0;
1036 }
1037 break;
1038 default:
1039 return command_usage(&writev_cmd);
1040 }
1041 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001042
Devin Nakamura43642b32011-07-11 11:22:16 -04001043 if (optind > argc - 2) {
1044 return command_usage(&writev_cmd);
1045 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001046
Devin Nakamura43642b32011-07-11 11:22:16 -04001047 nr_reqs = 1;
1048 for (i = optind; i < argc; i++) {
1049 if (!strcmp(argv[i], ";")) {
1050 nr_reqs++;
1051 }
1052 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001053
Kevin Wolff2360622011-10-31 11:36:32 +01001054 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1055 buf = g_malloc0(nr_reqs * sizeof(*buf));
Anthony Liguori7267c092011-08-20 22:09:37 -05001056 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001057
Kevin Wolf67403db2011-10-31 11:49:21 +01001058 for (i = 0; i < nr_reqs && optind < argc; i++) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001059 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001060
Devin Nakamura43642b32011-07-11 11:22:16 -04001061 /* Read the offset of the request */
1062 offset = cvtnum(argv[optind]);
1063 if (offset < 0) {
1064 printf("non-numeric offset argument -- %s\n", argv[optind]);
Kevin Wolf67403db2011-10-31 11:49:21 +01001065 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001066 }
1067 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001068
Devin Nakamura43642b32011-07-11 11:22:16 -04001069 if (offset & 0x1ff) {
1070 printf("offset %lld is not sector aligned\n",
1071 (long long)offset);
Kevin Wolf67403db2011-10-31 11:49:21 +01001072 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -04001073 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001074
1075 if (i == 0) {
1076 first_offset = offset;
1077 }
1078
Devin Nakamura43642b32011-07-11 11:22:16 -04001079 /* Read lengths for qiov entries */
1080 for (j = optind; j < argc; j++) {
1081 if (!strcmp(argv[j], ";")) {
1082 break;
1083 }
1084 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001085
Devin Nakamura43642b32011-07-11 11:22:16 -04001086 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001087
Devin Nakamura43642b32011-07-11 11:22:16 -04001088 /* Build request */
Kevin Wolf734c3b82013-06-05 14:19:30 +02001089 buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001090 if (buf[i] == NULL) {
1091 goto out;
1092 }
1093
Devin Nakamura43642b32011-07-11 11:22:16 -04001094 reqs[i].qiov = &qiovs[i];
Devin Nakamura43642b32011-07-11 11:22:16 -04001095 reqs[i].sector = offset >> 9;
1096 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001097
Devin Nakamura43642b32011-07-11 11:22:16 -04001098 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001099
Devin Nakamura43642b32011-07-11 11:22:16 -04001100 pattern++;
1101 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001102
Kevin Wolf67403db2011-10-31 11:49:21 +01001103 /* If there were empty requests at the end, ignore them */
1104 nr_reqs = i;
1105
Devin Nakamura43642b32011-07-11 11:22:16 -04001106 gettimeofday(&t1, NULL);
Kevin Wolf734c3b82013-06-05 14:19:30 +02001107 cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
Devin Nakamura43642b32011-07-11 11:22:16 -04001108 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001109
Devin Nakamura43642b32011-07-11 11:22:16 -04001110 if (cnt < 0) {
1111 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1112 goto out;
1113 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001114
Devin Nakamura43642b32011-07-11 11:22:16 -04001115 if (qflag) {
1116 goto out;
1117 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001118
Devin Nakamura43642b32011-07-11 11:22:16 -04001119 /* Finally, report back -- -C gives a parsable format */
1120 t2 = tsub(t2, t1);
1121 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001122out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001123 for (i = 0; i < nr_reqs; i++) {
1124 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001125 if (reqs[i].qiov != NULL) {
1126 qemu_iovec_destroy(&qiovs[i]);
1127 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001128 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001129 g_free(buf);
1130 g_free(reqs);
1131 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001132 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001133}
1134
Christoph Hellwig95533d52009-06-20 21:10:44 +02001135struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001136 QEMUIOVector qiov;
1137 int64_t offset;
1138 char *buf;
1139 int qflag;
1140 int vflag;
1141 int Cflag;
1142 int Pflag;
1143 int pattern;
1144 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001145};
1146
Devin Nakamura43642b32011-07-11 11:22:16 -04001147static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001148{
Devin Nakamura43642b32011-07-11 11:22:16 -04001149 struct aio_ctx *ctx = opaque;
1150 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001151
Devin Nakamura43642b32011-07-11 11:22:16 -04001152 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001153
Christoph Hellwig95533d52009-06-20 21:10:44 +02001154
Devin Nakamura43642b32011-07-11 11:22:16 -04001155 if (ret < 0) {
1156 printf("aio_write failed: %s\n", strerror(-ret));
1157 goto out;
1158 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001159
Devin Nakamura43642b32011-07-11 11:22:16 -04001160 if (ctx->qflag) {
1161 goto out;
1162 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001163
Devin Nakamura43642b32011-07-11 11:22:16 -04001164 /* Finally, report back -- -C gives a parsable format */
1165 t2 = tsub(t2, ctx->t1);
1166 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1167 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001168out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001169 qemu_io_free(ctx->buf);
Kevin Wolf9e559532012-07-02 15:13:53 +02001170 qemu_iovec_destroy(&ctx->qiov);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001171 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001172}
1173
Devin Nakamura43642b32011-07-11 11:22:16 -04001174static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001175{
Devin Nakamura43642b32011-07-11 11:22:16 -04001176 struct aio_ctx *ctx = opaque;
1177 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001178
Devin Nakamura43642b32011-07-11 11:22:16 -04001179 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001180
Devin Nakamura43642b32011-07-11 11:22:16 -04001181 if (ret < 0) {
1182 printf("readv failed: %s\n", strerror(-ret));
1183 goto out;
1184 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001185
Devin Nakamura43642b32011-07-11 11:22:16 -04001186 if (ctx->Pflag) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001187 void *cmp_buf = g_malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001188
Devin Nakamura43642b32011-07-11 11:22:16 -04001189 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1190 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1191 printf("Pattern verification failed at offset %"
1192 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1193 }
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001194 g_free(cmp_buf);
Devin Nakamura43642b32011-07-11 11:22:16 -04001195 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001196
Devin Nakamura43642b32011-07-11 11:22:16 -04001197 if (ctx->qflag) {
1198 goto out;
1199 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001200
Devin Nakamura43642b32011-07-11 11:22:16 -04001201 if (ctx->vflag) {
1202 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1203 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001204
Devin Nakamura43642b32011-07-11 11:22:16 -04001205 /* Finally, report back -- -C gives a parsable format */
1206 t2 = tsub(t2, ctx->t1);
1207 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1208 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001209out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001210 qemu_io_free(ctx->buf);
Kevin Wolf9e559532012-07-02 15:13:53 +02001211 qemu_iovec_destroy(&ctx->qiov);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001212 g_free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001213}
1214
Devin Nakamura43642b32011-07-11 11:22:16 -04001215static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001216{
Devin Nakamura43642b32011-07-11 11:22:16 -04001217 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001218"\n"
1219" asynchronously reads a range of bytes from the given offset\n"
1220"\n"
1221" Example:\n"
1222" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1223"\n"
1224" Reads a segment of the currently open file, optionally dumping it to the\n"
1225" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001226" The read is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001227" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001228" -C, -- report statistics in a machine parsable format\n"
1229" -P, -- use a pattern to verify read data\n"
1230" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001231" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001232"\n");
1233}
1234
Kevin Wolf734c3b82013-06-05 14:19:30 +02001235static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001236
1237static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001238 .name = "aio_read",
1239 .cfunc = aio_read_f,
1240 .argmin = 2,
1241 .argmax = -1,
1242 .args = "[-Cqv] [-P pattern ] off len [len..]",
1243 .oneline = "asynchronously reads a number of bytes",
1244 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001245};
1246
Kevin Wolf734c3b82013-06-05 14:19:30 +02001247static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001248{
Devin Nakamura43642b32011-07-11 11:22:16 -04001249 int nr_iov, c;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001250 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001251
Devin Nakamura43642b32011-07-11 11:22:16 -04001252 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1253 switch (c) {
1254 case 'C':
1255 ctx->Cflag = 1;
1256 break;
1257 case 'P':
1258 ctx->Pflag = 1;
1259 ctx->pattern = parse_pattern(optarg);
1260 if (ctx->pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001261 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001262 return 0;
1263 }
1264 break;
1265 case 'q':
1266 ctx->qflag = 1;
1267 break;
1268 case 'v':
1269 ctx->vflag = 1;
1270 break;
1271 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001272 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001273 return command_usage(&aio_read_cmd);
1274 }
1275 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001276
Devin Nakamura43642b32011-07-11 11:22:16 -04001277 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001278 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001279 return command_usage(&aio_read_cmd);
1280 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001281
Devin Nakamura43642b32011-07-11 11:22:16 -04001282 ctx->offset = cvtnum(argv[optind]);
1283 if (ctx->offset < 0) {
1284 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001285 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001286 return 0;
1287 }
1288 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001289
Devin Nakamura43642b32011-07-11 11:22:16 -04001290 if (ctx->offset & 0x1ff) {
1291 printf("offset %" PRId64 " is not sector aligned\n",
1292 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001293 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001294 return 0;
1295 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001296
Devin Nakamura43642b32011-07-11 11:22:16 -04001297 nr_iov = argc - optind;
Kevin Wolf734c3b82013-06-05 14:19:30 +02001298 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001299 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001300 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001301 return 0;
1302 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001303
Devin Nakamura43642b32011-07-11 11:22:16 -04001304 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001305 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1306 ctx->qiov.size >> 9, aio_read_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001307 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001308}
1309
Devin Nakamura43642b32011-07-11 11:22:16 -04001310static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001311{
Devin Nakamura43642b32011-07-11 11:22:16 -04001312 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001313"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001314" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001315" from multiple buffers\n"
1316"\n"
1317" Example:\n"
1318" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1319"\n"
1320" Writes into a segment of the currently open file, using a buffer\n"
1321" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001322" The write is performed asynchronously and the aio_flush command must be\n"
Laszlo Ersek96bab412012-01-24 21:13:28 +01001323" used to ensure all outstanding aio requests have been completed.\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001324" -P, -- use different pattern to fill file\n"
1325" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001326" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001327"\n");
1328}
1329
Kevin Wolf734c3b82013-06-05 14:19:30 +02001330static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001331
1332static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001333 .name = "aio_write",
1334 .cfunc = aio_write_f,
1335 .argmin = 2,
1336 .argmax = -1,
1337 .args = "[-Cq] [-P pattern ] off len [len..]",
1338 .oneline = "asynchronously writes a number of bytes",
1339 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001340};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001341
Kevin Wolf734c3b82013-06-05 14:19:30 +02001342static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001343{
Devin Nakamura43642b32011-07-11 11:22:16 -04001344 int nr_iov, c;
1345 int pattern = 0xcd;
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001346 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001347
Devin Nakamura43642b32011-07-11 11:22:16 -04001348 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1349 switch (c) {
1350 case 'C':
1351 ctx->Cflag = 1;
1352 break;
1353 case 'q':
1354 ctx->qflag = 1;
1355 break;
1356 case 'P':
1357 pattern = parse_pattern(optarg);
1358 if (pattern < 0) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001359 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001360 return 0;
1361 }
1362 break;
1363 default:
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001364 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001365 return command_usage(&aio_write_cmd);
1366 }
1367 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001368
Devin Nakamura43642b32011-07-11 11:22:16 -04001369 if (optind > argc - 2) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001370 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001371 return command_usage(&aio_write_cmd);
1372 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001373
Devin Nakamura43642b32011-07-11 11:22:16 -04001374 ctx->offset = cvtnum(argv[optind]);
1375 if (ctx->offset < 0) {
1376 printf("non-numeric length argument -- %s\n", argv[optind]);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001377 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001378 return 0;
1379 }
1380 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001381
Devin Nakamura43642b32011-07-11 11:22:16 -04001382 if (ctx->offset & 0x1ff) {
1383 printf("offset %" PRId64 " is not sector aligned\n",
1384 ctx->offset);
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001385 g_free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001386 return 0;
1387 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001388
Devin Nakamura43642b32011-07-11 11:22:16 -04001389 nr_iov = argc - optind;
Kevin Wolf734c3b82013-06-05 14:19:30 +02001390 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001391 if (ctx->buf == NULL) {
Stefan Hajnoczi031380d2012-01-16 09:28:06 +00001392 g_free(ctx);
Kevin Wolff2360622011-10-31 11:36:32 +01001393 return 0;
1394 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001395
Devin Nakamura43642b32011-07-11 11:22:16 -04001396 gettimeofday(&ctx->t1, NULL);
Paolo Bonziniad54ae82011-11-30 09:12:30 +01001397 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1398 ctx->qiov.size >> 9, aio_write_done, ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001399 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001400}
1401
Kevin Wolf734c3b82013-06-05 14:19:30 +02001402static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001403{
Kevin Wolfe7c8b092012-11-13 16:41:22 +01001404 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04001405 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001406}
1407
1408static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001409 .name = "aio_flush",
1410 .cfunc = aio_flush_f,
1411 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001412};
1413
Kevin Wolf734c3b82013-06-05 14:19:30 +02001414static int flush_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001415{
Devin Nakamura43642b32011-07-11 11:22:16 -04001416 bdrv_flush(bs);
1417 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001418}
1419
1420static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001421 .name = "flush",
1422 .altname = "f",
1423 .cfunc = flush_f,
1424 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001425};
1426
Kevin Wolf734c3b82013-06-05 14:19:30 +02001427static int truncate_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001428{
Devin Nakamura43642b32011-07-11 11:22:16 -04001429 int64_t offset;
1430 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001431
Devin Nakamura43642b32011-07-11 11:22:16 -04001432 offset = cvtnum(argv[1]);
1433 if (offset < 0) {
1434 printf("non-numeric truncate argument -- %s\n", argv[1]);
1435 return 0;
1436 }
aliguorie3aff4f2009-04-05 19:14:04 +00001437
Devin Nakamura43642b32011-07-11 11:22:16 -04001438 ret = bdrv_truncate(bs, offset);
1439 if (ret < 0) {
1440 printf("truncate: %s\n", strerror(-ret));
1441 return 0;
1442 }
aliguorie3aff4f2009-04-05 19:14:04 +00001443
Devin Nakamura43642b32011-07-11 11:22:16 -04001444 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001445}
1446
1447static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001448 .name = "truncate",
1449 .altname = "t",
1450 .cfunc = truncate_f,
1451 .argmin = 1,
1452 .argmax = 1,
1453 .args = "off",
1454 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001455};
1456
Kevin Wolf734c3b82013-06-05 14:19:30 +02001457static int length_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001458{
Devin Nakamura43642b32011-07-11 11:22:16 -04001459 int64_t size;
1460 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001461
Devin Nakamura43642b32011-07-11 11:22:16 -04001462 size = bdrv_getlength(bs);
1463 if (size < 0) {
1464 printf("getlength: %s\n", strerror(-size));
1465 return 0;
1466 }
aliguorie3aff4f2009-04-05 19:14:04 +00001467
Devin Nakamura43642b32011-07-11 11:22:16 -04001468 cvtstr(size, s1, sizeof(s1));
1469 printf("%s\n", s1);
1470 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001471}
1472
1473
1474static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001475 .name = "length",
1476 .altname = "l",
1477 .cfunc = length_f,
1478 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001479};
1480
1481
Kevin Wolf734c3b82013-06-05 14:19:30 +02001482static int info_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001483{
Devin Nakamura43642b32011-07-11 11:22:16 -04001484 BlockDriverInfo bdi;
1485 char s1[64], s2[64];
1486 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001487
Devin Nakamura43642b32011-07-11 11:22:16 -04001488 if (bs->drv && bs->drv->format_name) {
1489 printf("format name: %s\n", bs->drv->format_name);
1490 }
1491 if (bs->drv && bs->drv->protocol_name) {
1492 printf("format name: %s\n", bs->drv->protocol_name);
1493 }
aliguorie3aff4f2009-04-05 19:14:04 +00001494
Devin Nakamura43642b32011-07-11 11:22:16 -04001495 ret = bdrv_get_info(bs, &bdi);
1496 if (ret) {
1497 return 0;
1498 }
aliguorie3aff4f2009-04-05 19:14:04 +00001499
Devin Nakamura43642b32011-07-11 11:22:16 -04001500 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1501 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001502
Devin Nakamura43642b32011-07-11 11:22:16 -04001503 printf("cluster size: %s\n", s1);
1504 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001505
Devin Nakamura43642b32011-07-11 11:22:16 -04001506 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001507}
1508
1509
1510
1511static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001512 .name = "info",
1513 .altname = "i",
1514 .cfunc = info_f,
1515 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001516};
1517
Devin Nakamura43642b32011-07-11 11:22:16 -04001518static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001519{
Devin Nakamura43642b32011-07-11 11:22:16 -04001520 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001521"\n"
1522" discards a range of bytes from the given offset\n"
1523"\n"
1524" Example:\n"
1525" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1526"\n"
1527" Discards a segment of the currently open file.\n"
1528" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001529" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001530"\n");
1531}
1532
Kevin Wolf734c3b82013-06-05 14:19:30 +02001533static int discard_f(BlockDriverState *bs, int argc, char **argv);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001534
1535static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001536 .name = "discard",
1537 .altname = "d",
1538 .cfunc = discard_f,
1539 .argmin = 2,
1540 .argmax = -1,
1541 .args = "[-Cq] off len",
1542 .oneline = "discards a number of bytes at a specified offset",
1543 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001544};
1545
Kevin Wolf734c3b82013-06-05 14:19:30 +02001546static int discard_f(BlockDriverState *bs, int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001547{
Devin Nakamura43642b32011-07-11 11:22:16 -04001548 struct timeval t1, t2;
1549 int Cflag = 0, qflag = 0;
1550 int c, ret;
1551 int64_t offset;
1552 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001553
Devin Nakamura43642b32011-07-11 11:22:16 -04001554 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1555 switch (c) {
1556 case 'C':
1557 Cflag = 1;
1558 break;
1559 case 'q':
1560 qflag = 1;
1561 break;
1562 default:
1563 return command_usage(&discard_cmd);
1564 }
1565 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001566
Devin Nakamura43642b32011-07-11 11:22:16 -04001567 if (optind != argc - 2) {
1568 return command_usage(&discard_cmd);
1569 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001570
Devin Nakamura43642b32011-07-11 11:22:16 -04001571 offset = cvtnum(argv[optind]);
1572 if (offset < 0) {
1573 printf("non-numeric length argument -- %s\n", argv[optind]);
1574 return 0;
1575 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001576
Devin Nakamura43642b32011-07-11 11:22:16 -04001577 optind++;
1578 count = cvtnum(argv[optind]);
1579 if (count < 0) {
1580 printf("non-numeric length argument -- %s\n", argv[optind]);
1581 return 0;
1582 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001583
Devin Nakamura43642b32011-07-11 11:22:16 -04001584 gettimeofday(&t1, NULL);
1585 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1586 count >> BDRV_SECTOR_BITS);
1587 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001588
Devin Nakamura43642b32011-07-11 11:22:16 -04001589 if (ret < 0) {
1590 printf("discard failed: %s\n", strerror(-ret));
1591 goto out;
1592 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001593
Devin Nakamura43642b32011-07-11 11:22:16 -04001594 /* Finally, report back -- -C gives a parsable format */
1595 if (!qflag) {
1596 t2 = tsub(t2, t1);
1597 print_report("discard", &t2, offset, count, count, 1, Cflag);
1598 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001599
1600out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001601 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001602}
1603
Kevin Wolf734c3b82013-06-05 14:19:30 +02001604static int alloc_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001605{
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001606 int64_t offset, sector_num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001607 int nb_sectors, remaining;
1608 char s1[64];
1609 int num, sum_alloc;
1610 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001611
Devin Nakamura43642b32011-07-11 11:22:16 -04001612 offset = cvtnum(argv[1]);
Kevin Wolfcf49a6a2013-06-05 14:19:29 +02001613 if (offset < 0) {
1614 printf("non-numeric offset argument -- %s\n", argv[1]);
1615 return 0;
1616 } else if (offset & 0x1ff) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001617 printf("offset %" PRId64 " is not sector aligned\n",
1618 offset);
1619 return 0;
1620 }
aliguorie3aff4f2009-04-05 19:14:04 +00001621
Devin Nakamura43642b32011-07-11 11:22:16 -04001622 if (argc == 3) {
1623 nb_sectors = cvtnum(argv[2]);
Kevin Wolfcf49a6a2013-06-05 14:19:29 +02001624 if (nb_sectors < 0) {
1625 printf("non-numeric length argument -- %s\n", argv[2]);
1626 return 0;
1627 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001628 } else {
1629 nb_sectors = 1;
1630 }
aliguorie3aff4f2009-04-05 19:14:04 +00001631
Devin Nakamura43642b32011-07-11 11:22:16 -04001632 remaining = nb_sectors;
1633 sum_alloc = 0;
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001634 sector_num = offset >> 9;
Devin Nakamura43642b32011-07-11 11:22:16 -04001635 while (remaining) {
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001636 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1637 sector_num += num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001638 remaining -= num;
1639 if (ret) {
1640 sum_alloc += num;
1641 }
Paolo Bonzinicc785c32012-05-08 16:51:52 +02001642 if (num == 0) {
1643 nb_sectors -= remaining;
1644 remaining = 0;
1645 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001646 }
aliguorie3aff4f2009-04-05 19:14:04 +00001647
Devin Nakamura43642b32011-07-11 11:22:16 -04001648 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001649
Devin Nakamura43642b32011-07-11 11:22:16 -04001650 printf("%d/%d sectors allocated at offset %s\n",
1651 sum_alloc, nb_sectors, s1);
1652 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001653}
1654
1655static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001656 .name = "alloc",
1657 .altname = "a",
1658 .argmin = 1,
1659 .argmax = 2,
1660 .cfunc = alloc_f,
1661 .args = "off [sectors]",
1662 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001663};
1664
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001665
Kevin Wolf734c3b82013-06-05 14:19:30 +02001666static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1667 int64_t nb_sectors, int64_t *pnum)
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001668{
1669 int num, num_checked;
1670 int ret, firstret;
1671
1672 num_checked = MIN(nb_sectors, INT_MAX);
1673 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1674 if (ret < 0) {
1675 return ret;
1676 }
1677
1678 firstret = ret;
1679 *pnum = num;
1680
1681 while (nb_sectors > 0 && ret == firstret) {
1682 sector_num += num;
1683 nb_sectors -= num;
1684
1685 num_checked = MIN(nb_sectors, INT_MAX);
1686 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1687 if (ret == firstret) {
1688 *pnum += num;
1689 } else {
1690 break;
1691 }
1692 }
1693
1694 return firstret;
1695}
1696
Kevin Wolf734c3b82013-06-05 14:19:30 +02001697static int map_f(BlockDriverState *bs, int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001698{
Devin Nakamura43642b32011-07-11 11:22:16 -04001699 int64_t offset;
1700 int64_t nb_sectors;
1701 char s1[64];
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001702 int64_t num;
Devin Nakamura43642b32011-07-11 11:22:16 -04001703 int ret;
1704 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001705
Devin Nakamura43642b32011-07-11 11:22:16 -04001706 offset = 0;
1707 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001708
Devin Nakamura43642b32011-07-11 11:22:16 -04001709 do {
Kevin Wolf734c3b82013-06-05 14:19:30 +02001710 ret = map_is_allocated(bs, offset, nb_sectors, &num);
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001711 if (ret < 0) {
1712 error_report("Failed to get allocation status: %s", strerror(-ret));
1713 return 0;
1714 }
1715
Devin Nakamura43642b32011-07-11 11:22:16 -04001716 retstr = ret ? " allocated" : "not allocated";
1717 cvtstr(offset << 9ULL, s1, sizeof(s1));
Kevin Wolfa00e81e2013-05-13 15:31:34 +02001718 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1719 "at offset %s (%d)\n",
1720 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001721
Devin Nakamura43642b32011-07-11 11:22:16 -04001722 offset += num;
1723 nb_sectors -= num;
1724 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001725
Devin Nakamura43642b32011-07-11 11:22:16 -04001726 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001727}
1728
1729static const cmdinfo_t map_cmd = {
1730 .name = "map",
1731 .argmin = 0,
1732 .argmax = 0,
1733 .cfunc = map_f,
1734 .args = "",
1735 .oneline = "prints the allocated areas of a file",
1736};
1737
Kevin Wolf734c3b82013-06-05 14:19:30 +02001738static int break_f(BlockDriverState *bs, int argc, char **argv)
Kevin Wolf41c695c2012-12-06 14:32:58 +01001739{
1740 int ret;
1741
1742 ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1743 if (ret < 0) {
1744 printf("Could not set breakpoint: %s\n", strerror(-ret));
1745 }
1746
1747 return 0;
1748}
1749
1750static const cmdinfo_t break_cmd = {
1751 .name = "break",
1752 .argmin = 2,
1753 .argmax = 2,
1754 .cfunc = break_f,
1755 .args = "event tag",
1756 .oneline = "sets a breakpoint on event and tags the stopped "
1757 "request as tag",
1758};
1759
Kevin Wolf734c3b82013-06-05 14:19:30 +02001760static int resume_f(BlockDriverState *bs, int argc, char **argv)
Kevin Wolf41c695c2012-12-06 14:32:58 +01001761{
1762 int ret;
1763
1764 ret = bdrv_debug_resume(bs, argv[1]);
1765 if (ret < 0) {
1766 printf("Could not resume request: %s\n", strerror(-ret));
1767 }
1768
1769 return 0;
1770}
1771
1772static const cmdinfo_t resume_cmd = {
1773 .name = "resume",
1774 .argmin = 1,
1775 .argmax = 1,
1776 .cfunc = resume_f,
1777 .args = "tag",
1778 .oneline = "resumes the request tagged as tag",
1779};
1780
Kevin Wolf734c3b82013-06-05 14:19:30 +02001781static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
Kevin Wolf41c695c2012-12-06 14:32:58 +01001782{
1783 while (!bdrv_debug_is_suspended(bs, argv[1])) {
1784 qemu_aio_wait();
1785 }
1786
1787 return 0;
1788}
1789
1790static const cmdinfo_t wait_break_cmd = {
1791 .name = "wait_break",
1792 .argmin = 1,
1793 .argmax = 1,
1794 .cfunc = wait_break_f,
1795 .args = "tag",
1796 .oneline = "waits for the suspension of a request",
1797};
1798
Kevin Wolf734c3b82013-06-05 14:19:30 +02001799static int abort_f(BlockDriverState *bs, int argc, char **argv)
Stefan Hajnoczie01c30d2012-07-27 09:05:23 +01001800{
1801 abort();
1802}
1803
1804static const cmdinfo_t abort_cmd = {
1805 .name = "abort",
1806 .cfunc = abort_f,
1807 .flags = CMD_NOFILE_OK,
1808 .oneline = "simulate a program crash using abort(3)",
1809};
Kevin Wolf191c2892010-09-16 13:18:08 +02001810
Kevin Wolf734c3b82013-06-05 14:19:30 +02001811static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001812{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001813 bdrv_delete(bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +02001814 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -04001815 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001816}
1817
1818static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001819 .name = "close",
1820 .altname = "c",
1821 .cfunc = close_f,
1822 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001823};
1824
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001825static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001826{
Kevin Wolf734c3b82013-06-05 14:19:30 +02001827 if (qemuio_bs) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001828 fprintf(stderr, "file open already, try 'help close'\n");
1829 return 1;
1830 }
aliguorie3aff4f2009-04-05 19:14:04 +00001831
Devin Nakamura43642b32011-07-11 11:22:16 -04001832 if (growable) {
Kevin Wolf734c3b82013-06-05 14:19:30 +02001833 if (bdrv_file_open(&qemuio_bs, name, NULL, flags)) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001834 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1835 return 1;
1836 }
1837 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +02001838 qemuio_bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001839
Kevin Wolf734c3b82013-06-05 14:19:30 +02001840 if (bdrv_open(qemuio_bs, name, NULL, flags, NULL) < 0) {
Devin Nakamura43642b32011-07-11 11:22:16 -04001841 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Kevin Wolf734c3b82013-06-05 14:19:30 +02001842 bdrv_delete(qemuio_bs);
1843 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -04001844 return 1;
1845 }
1846 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001847
Devin Nakamura43642b32011-07-11 11:22:16 -04001848 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001849}
1850
Devin Nakamura43642b32011-07-11 11:22:16 -04001851static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001852{
Devin Nakamura43642b32011-07-11 11:22:16 -04001853 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001854"\n"
1855" opens a new file in the requested mode\n"
1856"\n"
1857" Example:\n"
1858" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1859"\n"
1860" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001861" -r, -- open file read-only\n"
1862" -s, -- use snapshot file\n"
1863" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001864" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001865"\n");
1866}
1867
Kevin Wolf734c3b82013-06-05 14:19:30 +02001868static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001869
1870static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001871 .name = "open",
1872 .altname = "o",
1873 .cfunc = open_f,
1874 .argmin = 1,
1875 .argmax = -1,
1876 .flags = CMD_NOFILE_OK,
1877 .args = "[-Crsn] [path]",
1878 .oneline = "open the file specified by path",
1879 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001880};
aliguorie3aff4f2009-04-05 19:14:04 +00001881
Kevin Wolf734c3b82013-06-05 14:19:30 +02001882static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001883{
Devin Nakamura43642b32011-07-11 11:22:16 -04001884 int flags = 0;
1885 int readonly = 0;
1886 int growable = 0;
1887 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001888
Devin Nakamura43642b32011-07-11 11:22:16 -04001889 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1890 switch (c) {
1891 case 's':
1892 flags |= BDRV_O_SNAPSHOT;
1893 break;
1894 case 'n':
1895 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1896 break;
1897 case 'r':
1898 readonly = 1;
1899 break;
1900 case 'g':
1901 growable = 1;
1902 break;
1903 default:
1904 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001905 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001906 }
aliguorie3aff4f2009-04-05 19:14:04 +00001907
Devin Nakamura43642b32011-07-11 11:22:16 -04001908 if (!readonly) {
1909 flags |= BDRV_O_RDWR;
1910 }
aliguorie3aff4f2009-04-05 19:14:04 +00001911
Devin Nakamura43642b32011-07-11 11:22:16 -04001912 if (optind != argc - 1) {
1913 return command_usage(&open_cmd);
1914 }
1915
1916 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001917}
1918
Kevin Wolf734c3b82013-06-05 14:19:30 +02001919static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001920{
Devin Nakamura43642b32011-07-11 11:22:16 -04001921 if (ct->flags & CMD_FLAG_GLOBAL) {
1922 return 1;
1923 }
1924 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1925 fprintf(stderr, "no file open, try 'help open'\n");
1926 return 0;
1927 }
1928 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001929}
1930
1931static void usage(const char *name)
1932{
Devin Nakamura43642b32011-07-11 11:22:16 -04001933 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001934"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001935"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001936"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001937" -c, --cmd command to execute\n"
1938" -r, --read-only export read-only\n"
1939" -s, --snapshot use snapshot file\n"
1940" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001941" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001942" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001943" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +02001944" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001945" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001946" -h, --help display this help and exit\n"
1947" -V, --version output version information and exit\n"
1948"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001949 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001950}
1951
1952
1953int main(int argc, char **argv)
1954{
Devin Nakamura43642b32011-07-11 11:22:16 -04001955 int readonly = 0;
1956 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001957 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -04001958 const struct option lopt[] = {
1959 { "help", 0, NULL, 'h' },
1960 { "version", 0, NULL, 'V' },
1961 { "offset", 1, NULL, 'o' },
1962 { "cmd", 1, NULL, 'c' },
1963 { "read-only", 0, NULL, 'r' },
1964 { "snapshot", 0, NULL, 's' },
1965 { "nocache", 0, NULL, 'n' },
1966 { "misalign", 0, NULL, 'm' },
1967 { "growable", 0, NULL, 'g' },
1968 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001969 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +02001970 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00001971 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -04001972 { NULL, 0, NULL, 0 }
1973 };
1974 int c;
1975 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001976 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +00001977
Devin Nakamura43642b32011-07-11 11:22:16 -04001978 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001979
Devin Nakamura43642b32011-07-11 11:22:16 -04001980 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1981 switch (c) {
1982 case 's':
1983 flags |= BDRV_O_SNAPSHOT;
1984 break;
1985 case 'n':
1986 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1987 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +01001988 case 'd':
1989 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
1990 error_report("Invalid discard option: %s", optarg);
1991 exit(1);
1992 }
1993 break;
Devin Nakamura43642b32011-07-11 11:22:16 -04001994 case 'c':
1995 add_user_command(optarg);
1996 break;
1997 case 'r':
1998 readonly = 1;
1999 break;
2000 case 'm':
2001 misalign = 1;
2002 break;
2003 case 'g':
2004 growable = 1;
2005 break;
2006 case 'k':
2007 flags |= BDRV_O_NATIVE_AIO;
2008 break;
Kevin Wolf592fa072012-04-18 12:07:39 +02002009 case 't':
2010 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
2011 error_report("Invalid cache option: %s", optarg);
2012 exit(1);
2013 }
2014 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +00002015 case 'T':
2016 if (!trace_backend_init(optarg, NULL)) {
2017 exit(1); /* error message will have been printed */
2018 }
2019 break;
Devin Nakamura43642b32011-07-11 11:22:16 -04002020 case 'V':
2021 printf("%s version %s\n", progname, VERSION);
2022 exit(0);
2023 case 'h':
2024 usage(progname);
2025 exit(0);
2026 default:
2027 usage(progname);
2028 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02002029 }
Devin Nakamura43642b32011-07-11 11:22:16 -04002030 }
aliguorie3aff4f2009-04-05 19:14:04 +00002031
Devin Nakamura43642b32011-07-11 11:22:16 -04002032 if ((argc - optind) > 1) {
2033 usage(progname);
2034 exit(1);
2035 }
aliguorie3aff4f2009-04-05 19:14:04 +00002036
Zhi Yong Wua57d1142012-02-19 22:24:59 +08002037 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +01002038 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +08002039
Devin Nakamura43642b32011-07-11 11:22:16 -04002040 /* initialize commands */
2041 quit_init();
2042 help_init();
2043 add_command(&open_cmd);
2044 add_command(&close_cmd);
2045 add_command(&read_cmd);
2046 add_command(&readv_cmd);
2047 add_command(&write_cmd);
2048 add_command(&writev_cmd);
2049 add_command(&multiwrite_cmd);
2050 add_command(&aio_read_cmd);
2051 add_command(&aio_write_cmd);
2052 add_command(&aio_flush_cmd);
2053 add_command(&flush_cmd);
2054 add_command(&truncate_cmd);
2055 add_command(&length_cmd);
2056 add_command(&info_cmd);
2057 add_command(&discard_cmd);
2058 add_command(&alloc_cmd);
2059 add_command(&map_cmd);
Kevin Wolf41c695c2012-12-06 14:32:58 +01002060 add_command(&break_cmd);
2061 add_command(&resume_cmd);
2062 add_command(&wait_break_cmd);
Stefan Hajnoczie01c30d2012-07-27 09:05:23 +01002063 add_command(&abort_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -04002064
Devin Nakamura43642b32011-07-11 11:22:16 -04002065 add_check_command(init_check_command);
2066
2067 /* open the device */
2068 if (!readonly) {
2069 flags |= BDRV_O_RDWR;
2070 }
2071
2072 if ((argc - optind) == 1) {
2073 openfile(argv[optind], flags, growable);
2074 }
2075 command_loop();
2076
2077 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002078 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -04002079 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002080 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -04002081
Kevin Wolf734c3b82013-06-05 14:19:30 +02002082 if (qemuio_bs) {
2083 bdrv_delete(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04002084 }
2085 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00002086}