]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-io.c
block: Add reference parameter to bdrv_open()
[mirror_qemu.git] / qemu-io.c
CommitLineData
e3aff4f6
AL
1/*
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 */
c32d766a 10#include <sys/time.h>
e3aff4f6
AL
11#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
c32d766a 15#include <libgen.h>
e3aff4f6 16
3d21994f 17#include "qemu-io.h"
1de7afc9 18#include "qemu/main-loop.h"
b543c5cd
HR
19#include "qemu/option.h"
20#include "qemu/config-file.h"
0cf17e18 21#include "qemu/readline.h"
737e150e 22#include "block/block_int.h"
d7bb72c8 23#include "trace/control.h"
e3aff4f6 24
43642b38 25#define CMD_NOFILE_OK 0x01
e3aff4f6
AL
26
27char *progname;
e3aff4f6 28
734c3b85 29BlockDriverState *qemuio_bs;
797ac58c 30extern int qemuio_misalign;
191c2890 31
d1174f13
KW
32/* qemu-io commands passed using -c */
33static int ncmdline;
34static char **cmdline;
35
0cf17e18
SH
36static ReadLineState *readline_state;
37
734c3b85 38static int close_f(BlockDriverState *bs, int argc, char **argv)
e3aff4f6 39{
4f6fd349 40 bdrv_unref(bs);
734c3b85 41 qemuio_bs = NULL;
43642b38 42 return 0;
e3aff4f6
AL
43}
44
45static const cmdinfo_t close_cmd = {
43642b38
DN
46 .name = "close",
47 .altname = "c",
48 .cfunc = close_f,
49 .oneline = "close the current open file",
e3aff4f6
AL
50};
51
b543c5cd 52static int openfile(char *name, int flags, int growable, QDict *opts)
e3aff4f6 53{
34b5d2c6
HR
54 Error *local_err = NULL;
55
734c3b85 56 if (qemuio_bs) {
43642b38
DN
57 fprintf(stderr, "file open already, try 'help close'\n");
58 return 1;
59 }
60
61 if (growable) {
72daa72e 62 if (bdrv_file_open(&qemuio_bs, name, NULL, opts, flags, &local_err)) {
34b5d2c6
HR
63 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
64 error_get_pretty(local_err));
65 error_free(local_err);
43642b38
DN
66 return 1;
67 }
68 } else {
734c3b85 69 qemuio_bs = bdrv_new("hda");
43642b38 70
ddf5636d
HR
71 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
72 < 0)
73 {
34b5d2c6
HR
74 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
75 error_get_pretty(local_err));
76 error_free(local_err);
4f6fd349 77 bdrv_unref(qemuio_bs);
734c3b85 78 qemuio_bs = NULL;
43642b38
DN
79 return 1;
80 }
81 }
82
83 return 0;
e3aff4f6
AL
84}
85
43642b38 86static void open_help(void)
e3aff4f6 87{
43642b38 88 printf(
e3aff4f6
AL
89"\n"
90" opens a new file in the requested mode\n"
91"\n"
92" Example:\n"
93" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
94"\n"
95" Opens a file for subsequent use by all of the other qemu-io commands.\n"
e3aff4f6
AL
96" -r, -- open file read-only\n"
97" -s, -- use snapshot file\n"
98" -n, -- disable host cache\n"
b543c5cd
HR
99" -g, -- allow file to grow (only applies to protocols)\n"
100" -o, -- options to be given to the block driver"
e3aff4f6
AL
101"\n");
102}
103
734c3b85 104static int open_f(BlockDriverState *bs, int argc, char **argv);
22a2bdcb
BS
105
106static const cmdinfo_t open_cmd = {
43642b38
DN
107 .name = "open",
108 .altname = "o",
109 .cfunc = open_f,
110 .argmin = 1,
111 .argmax = -1,
112 .flags = CMD_NOFILE_OK,
b543c5cd 113 .args = "[-Crsn] [-o options] [path]",
43642b38
DN
114 .oneline = "open the file specified by path",
115 .help = open_help,
22a2bdcb 116};
e3aff4f6 117
b543c5cd
HR
118static QemuOptsList empty_opts = {
119 .name = "drive",
120 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
121 .desc = {
122 /* no elements => accept any params */
123 { /* end of list */ }
124 },
125};
126
734c3b85 127static int open_f(BlockDriverState *bs, int argc, char **argv)
e3aff4f6 128{
43642b38
DN
129 int flags = 0;
130 int readonly = 0;
131 int growable = 0;
132 int c;
b543c5cd
HR
133 QemuOpts *qopts;
134 QDict *opts = NULL;
43642b38 135
b543c5cd 136 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
43642b38
DN
137 switch (c) {
138 case 's':
139 flags |= BDRV_O_SNAPSHOT;
140 break;
141 case 'n':
142 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
143 break;
144 case 'r':
145 readonly = 1;
146 break;
147 case 'g':
148 growable = 1;
149 break;
b543c5cd
HR
150 case 'o':
151 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
152 if (qopts == NULL) {
153 printf("could not parse option list -- %s\n", optarg);
154 return 0;
155 }
156 opts = qemu_opts_to_qdict(qopts, opts);
157 qemu_opts_del(qopts);
158 break;
43642b38 159 default:
c2cdf5c5 160 return qemuio_command_usage(&open_cmd);
f5edb014 161 }
43642b38
DN
162 }
163
164 if (!readonly) {
165 flags |= BDRV_O_RDWR;
166 }
e3aff4f6 167
fd0fee34
HR
168 if (optind == argc - 1) {
169 return openfile(argv[optind], flags, growable, opts);
170 } else if (optind == argc) {
171 return openfile(NULL, flags, growable, opts);
172 } else {
c2cdf5c5 173 return qemuio_command_usage(&open_cmd);
43642b38 174 }
e3aff4f6
AL
175}
176
e681be7e
KW
177static int quit_f(BlockDriverState *bs, int argc, char **argv)
178{
179 return 1;
180}
181
182static const cmdinfo_t quit_cmd = {
183 .name = "quit",
184 .altname = "q",
185 .cfunc = quit_f,
186 .argmin = -1,
187 .argmax = -1,
188 .flags = CMD_FLAG_GLOBAL,
189 .oneline = "exit the program",
190};
191
e3aff4f6
AL
192static void usage(const char *name)
193{
43642b38 194 printf(
9a2d77ad 195"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
84844a20 196"QEMU Disk exerciser\n"
e3aff4f6 197"\n"
e3aff4f6
AL
198" -c, --cmd command to execute\n"
199" -r, --read-only export read-only\n"
200" -s, --snapshot use snapshot file\n"
201" -n, --nocache disable host cache\n"
1db6947d 202" -g, --growable allow file to grow (only applies to protocols)\n"
e3aff4f6 203" -m, --misalign misalign allocations for O_DIRECT\n"
5c6c3a6c 204" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
592fa070 205" -t, --cache=MODE use the given cache mode for the image\n"
d7bb72c8 206" -T, --trace FILE enable trace events listed in the given file\n"
e3aff4f6
AL
207" -h, --help display this help and exit\n"
208" -V, --version output version information and exit\n"
209"\n",
43642b38 210 name);
e3aff4f6
AL
211}
212
d1174f13
KW
213static char *get_prompt(void)
214{
215 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
216
217 if (!prompt[0]) {
218 snprintf(prompt, sizeof(prompt), "%s> ", progname);
219 }
220
221 return prompt;
222}
223
d5d1507b
SW
224static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
225 const char *fmt, ...)
d1174f13 226{
0cf17e18
SH
227 va_list ap;
228 va_start(ap, fmt);
229 vprintf(fmt, ap);
230 va_end(ap);
d1174f13 231}
0cf17e18
SH
232
233static void readline_flush_func(void *opaque)
d1174f13 234{
0cf17e18 235 fflush(stdout);
d1174f13
KW
236}
237
0cf17e18 238static void readline_func(void *opaque, const char *str, void *readline_opaque)
d1174f13 239{
0cf17e18
SH
240 char **line = readline_opaque;
241 *line = g_strdup(str);
242}
243
4694020d
SH
244static void completion_match(const char *cmd, void *opaque)
245{
246 readline_add_completion(readline_state, cmd);
247}
248
0cf17e18
SH
249static void readline_completion_func(void *opaque, const char *str)
250{
4694020d
SH
251 readline_set_completion_index(readline_state, strlen(str));
252 qemuio_complete_command(str, completion_match, NULL);
0cf17e18
SH
253}
254
255static char *fetchline_readline(void)
256{
257 char *line = NULL;
258
259 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
260 while (!line) {
261 int ch = getchar();
262 if (ch == EOF) {
263 break;
d1174f13 264 }
0cf17e18 265 readline_handle_byte(readline_state, ch);
d1174f13
KW
266 }
267 return line;
268}
0cf17e18
SH
269
270#define MAXREADLINESZ 1024
271static char *fetchline_fgets(void)
d1174f13
KW
272{
273 char *p, *line = g_malloc(MAXREADLINESZ);
274
275 if (!fgets(line, MAXREADLINESZ, stdin)) {
276 g_free(line);
277 return NULL;
278 }
279
280 p = line + strlen(line);
281 if (p != line && p[-1] == '\n') {
282 p[-1] = '\0';
283 }
284
285 return line;
286}
0cf17e18
SH
287
288static char *fetchline(void)
289{
290 if (readline_state) {
291 return fetchline_readline();
292 } else {
293 return fetchline_fgets();
294 }
295}
d1174f13
KW
296
297static void prep_fetchline(void *opaque)
298{
299 int *fetchable = opaque;
300
301 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
302 *fetchable= 1;
303}
304
305static void command_loop(void)
306{
307 int i, done = 0, fetchable = 0, prompted = 0;
308 char *input;
309
310 for (i = 0; !done && i < ncmdline; i++) {
3d21994f 311 done = qemuio_command(qemuio_bs, cmdline[i]);
d1174f13
KW
312 }
313 if (cmdline) {
314 g_free(cmdline);
315 return;
316 }
317
318 while (!done) {
319 if (!prompted) {
320 printf("%s", get_prompt());
321 fflush(stdout);
322 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
323 prompted = 1;
324 }
325
326 main_loop_wait(false);
327
328 if (!fetchable) {
329 continue;
330 }
331
332 input = fetchline();
333 if (input == NULL) {
334 break;
335 }
3d21994f 336 done = qemuio_command(qemuio_bs, input);
d1174f13
KW
337 g_free(input);
338
339 prompted = 0;
340 fetchable = 0;
341 }
342 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
343}
344
345static void add_user_command(char *optarg)
346{
347 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
348 cmdline[ncmdline-1] = optarg;
349}
350
0cf17e18
SH
351static void reenable_tty_echo(void)
352{
353 qemu_set_tty_echo(STDIN_FILENO, true);
354}
355
e3aff4f6
AL
356int main(int argc, char **argv)
357{
43642b38
DN
358 int readonly = 0;
359 int growable = 0;
9e8f1835 360 const char *sopt = "hVc:d:rsnmgkt:T:";
43642b38
DN
361 const struct option lopt[] = {
362 { "help", 0, NULL, 'h' },
363 { "version", 0, NULL, 'V' },
364 { "offset", 1, NULL, 'o' },
365 { "cmd", 1, NULL, 'c' },
366 { "read-only", 0, NULL, 'r' },
367 { "snapshot", 0, NULL, 's' },
368 { "nocache", 0, NULL, 'n' },
369 { "misalign", 0, NULL, 'm' },
370 { "growable", 0, NULL, 'g' },
371 { "native-aio", 0, NULL, 'k' },
9e8f1835 372 { "discard", 1, NULL, 'd' },
592fa070 373 { "cache", 1, NULL, 't' },
d7bb72c8 374 { "trace", 1, NULL, 'T' },
43642b38
DN
375 { NULL, 0, NULL, 0 }
376 };
377 int c;
378 int opt_index = 0;
9e8f1835 379 int flags = BDRV_O_UNMAP;
43642b38 380
526eda14
MK
381#ifdef CONFIG_POSIX
382 signal(SIGPIPE, SIG_IGN);
383#endif
384
43642b38
DN
385 progname = basename(argv[0]);
386
387 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
388 switch (c) {
389 case 's':
390 flags |= BDRV_O_SNAPSHOT;
391 break;
392 case 'n':
393 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
394 break;
9e8f1835
PB
395 case 'd':
396 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
397 error_report("Invalid discard option: %s", optarg);
398 exit(1);
399 }
400 break;
43642b38
DN
401 case 'c':
402 add_user_command(optarg);
403 break;
404 case 'r':
405 readonly = 1;
406 break;
407 case 'm':
797ac58c 408 qemuio_misalign = 1;
43642b38
DN
409 break;
410 case 'g':
411 growable = 1;
412 break;
413 case 'k':
414 flags |= BDRV_O_NATIVE_AIO;
415 break;
592fa070
KW
416 case 't':
417 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
418 error_report("Invalid cache option: %s", optarg);
419 exit(1);
420 }
421 break;
d7bb72c8
SH
422 case 'T':
423 if (!trace_backend_init(optarg, NULL)) {
424 exit(1); /* error message will have been printed */
425 }
426 break;
43642b38 427 case 'V':
02da386a 428 printf("%s version %s\n", progname, QEMU_VERSION);
43642b38
DN
429 exit(0);
430 case 'h':
431 usage(progname);
432 exit(0);
433 default:
434 usage(progname);
435 exit(1);
f5edb014 436 }
43642b38
DN
437 }
438
439 if ((argc - optind) > 1) {
440 usage(progname);
441 exit(1);
442 }
e3aff4f6 443
a57d1143 444 qemu_init_main_loop();
2592c59a 445 bdrv_init();
a57d1143 446
43642b38 447 /* initialize commands */
c2cdf5c5
KW
448 qemuio_add_command(&quit_cmd);
449 qemuio_add_command(&open_cmd);
450 qemuio_add_command(&close_cmd);
43642b38 451
0cf17e18
SH
452 if (isatty(STDIN_FILENO)) {
453 readline_state = readline_init(readline_printf_func,
454 readline_flush_func,
455 NULL,
456 readline_completion_func);
457 qemu_set_tty_echo(STDIN_FILENO, false);
458 atexit(reenable_tty_echo);
459 }
460
43642b38
DN
461 /* open the device */
462 if (!readonly) {
463 flags |= BDRV_O_RDWR;
464 }
465
466 if ((argc - optind) == 1) {
b543c5cd 467 openfile(argv[optind], flags, growable, NULL);
43642b38
DN
468 }
469 command_loop();
e3aff4f6 470
43642b38 471 /*
922453bc 472 * Make sure all outstanding requests complete before the program exits.
43642b38 473 */
922453bc 474 bdrv_drain_all();
95533d5f 475
734c3b85 476 if (qemuio_bs) {
4f6fd349 477 bdrv_unref(qemuio_bs);
43642b38 478 }
0cf17e18 479 g_free(readline_state);
43642b38 480 return 0;
e3aff4f6 481}