From 1b58b43802a0158d74f4ea7e52f852363e63fe2f Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Thu, 5 Feb 2015 13:58:20 -0500 Subject: qemu-io: Use blk_new_open() in openfile() Signed-off-by: Max Reitz Reviewed-by: Eric Blake Message-id: 1423162705-32065-12-git-send-email-mreitz@redhat.com Signed-off-by: Stefan Hajnoczi --- qemu-io.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'qemu-io.c') diff --git a/qemu-io.c b/qemu-io.c index 91a445a106..fa072df805 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -51,34 +51,29 @@ static const cmdinfo_t close_cmd = { .oneline = "close the current open file", }; -static int openfile(char *name, BlockDriver *drv, int flags, int growable, - QDict *opts) +static int openfile(char *name, int flags, int growable, QDict *opts) { Error *local_err = NULL; - if (qemuio_bs) { + if (qemuio_blk) { fprintf(stderr, "file open already, try 'help close'\n"); QDECREF(opts); return 1; } - qemuio_blk = blk_new_with_bs("hda", &error_abort); - qemuio_bs = blk_bs(qemuio_blk); - if (growable) { flags |= BDRV_O_PROTOCOL; } - if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, drv, &local_err) < 0) { + qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err); + if (!qemuio_blk) { fprintf(stderr, "%s: can't open%s%s: %s\n", progname, name ? " device " : "", name ?: "", error_get_pretty(local_err)); error_free(local_err); - blk_unref(qemuio_blk); - qemuio_bs = NULL; - qemuio_blk = NULL; return 1; } + qemuio_bs = blk_bs(qemuio_blk); return 0; } @@ -170,9 +165,9 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) qemu_opts_reset(&empty_opts); if (optind == argc - 1) { - return openfile(argv[optind], NULL, flags, growable, opts); + return openfile(argv[optind], flags, growable, opts); } else if (optind == argc) { - return openfile(NULL, NULL, flags, growable, opts); + return openfile(NULL, flags, growable, opts); } else { QDECREF(opts); return qemuio_command_usage(&open_cmd); @@ -387,8 +382,8 @@ int main(int argc, char **argv) int c; int opt_index = 0; int flags = BDRV_O_UNMAP; - BlockDriver *drv = NULL; Error *local_error = NULL; + QDict *opts = NULL; #ifdef CONFIG_POSIX signal(SIGPIPE, SIG_IGN); @@ -414,11 +409,10 @@ int main(int argc, char **argv) } break; case 'f': - drv = bdrv_find_format(optarg); - if (!drv) { - error_report("Invalid format '%s'", optarg); - exit(EXIT_FAILURE); + if (!opts) { + opts = qdict_new(); } + qdict_put(opts, "driver", qstring_from_str(optarg)); break; case 'c': add_user_command(optarg); @@ -489,7 +483,7 @@ int main(int argc, char **argv) } if ((argc - optind) == 1) { - openfile(argv[optind], drv, flags, growable, NULL); + openfile(argv[optind], flags, growable, opts); } command_loop(); -- cgit v1.2.3 From 10d9d75ce4cfb568b4845d8c4d0e65968f740edf Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Thu, 5 Feb 2015 13:58:21 -0500 Subject: qemu-io: Remove "growable" option Remove "growable" option from the "open" command and from the qemu-io command line. qemu-io is about to be converted to BlockBackend which will make sure that no request exceeds the image size, so the only way to keep "growable" would be to use BlockBackend if it is not given and to directly access the BDS if it is. qemu-io is a debugging tool, therefore removing a rarely used option will have only a very small impact, if any. There was only one qemu-iotest which used the option; since it is not critical, this patch just removes it. Signed-off-by: Max Reitz Reviewed-by: Eric Blake Message-id: 1423162705-32065-13-git-send-email-mreitz@redhat.com Signed-off-by: Stefan Hajnoczi --- qemu-io.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) (limited to 'qemu-io.c') diff --git a/qemu-io.c b/qemu-io.c index fa072df805..a85522a1b3 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -51,7 +51,7 @@ static const cmdinfo_t close_cmd = { .oneline = "close the current open file", }; -static int openfile(char *name, int flags, int growable, QDict *opts) +static int openfile(char *name, int flags, QDict *opts) { Error *local_err = NULL; @@ -61,10 +61,6 @@ static int openfile(char *name, int flags, int growable, QDict *opts) return 1; } - if (growable) { - flags |= BDRV_O_PROTOCOL; - } - qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err); if (!qemuio_blk) { fprintf(stderr, "%s: can't open%s%s: %s\n", progname, @@ -91,7 +87,6 @@ static void open_help(void) " -r, -- open file read-only\n" " -s, -- use snapshot file\n" " -n, -- disable host cache\n" -" -g, -- allow file to grow (only applies to protocols)\n" " -o, -- options to be given to the block driver" "\n"); } @@ -124,7 +119,6 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) { int flags = 0; int readonly = 0; - int growable = 0; int c; QemuOpts *qopts; QDict *opts; @@ -140,9 +134,6 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) case 'r': readonly = 1; break; - case 'g': - growable = 1; - break; case 'o': if (!qemu_opts_parse(&empty_opts, optarg, 0)) { printf("could not parse option list -- %s\n", optarg); @@ -165,9 +156,9 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) qemu_opts_reset(&empty_opts); if (optind == argc - 1) { - return openfile(argv[optind], flags, growable, opts); + return openfile(argv[optind], flags, opts); } else if (optind == argc) { - return openfile(NULL, flags, growable, opts); + return openfile(NULL, flags, opts); } else { QDECREF(opts); return qemuio_command_usage(&open_cmd); @@ -201,7 +192,6 @@ static void usage(const char *name) " -r, --read-only export read-only\n" " -s, --snapshot use snapshot file\n" " -n, --nocache disable host cache\n" -" -g, --growable allow file to grow (only applies to protocols)\n" " -m, --misalign misalign allocations for O_DIRECT\n" " -k, --native-aio use kernel AIO implementation (on Linux only)\n" " -t, --cache=MODE use the given cache mode for the image\n" @@ -360,7 +350,6 @@ static void reenable_tty_echo(void) int main(int argc, char **argv) { int readonly = 0; - int growable = 0; const char *sopt = "hVc:d:f:rsnmgkt:T:"; const struct option lopt[] = { { "help", 0, NULL, 'h' }, @@ -372,7 +361,6 @@ int main(int argc, char **argv) { "snapshot", 0, NULL, 's' }, { "nocache", 0, NULL, 'n' }, { "misalign", 0, NULL, 'm' }, - { "growable", 0, NULL, 'g' }, { "native-aio", 0, NULL, 'k' }, { "discard", 1, NULL, 'd' }, { "cache", 1, NULL, 't' }, @@ -423,9 +411,6 @@ int main(int argc, char **argv) case 'm': qemuio_misalign = true; break; - case 'g': - growable = 1; - break; case 'k': flags |= BDRV_O_NATIVE_AIO; break; @@ -483,7 +468,7 @@ int main(int argc, char **argv) } if ((argc - optind) == 1) { - openfile(argv[optind], flags, growable, opts); + openfile(argv[optind], flags, opts); } command_loop(); -- cgit v1.2.3 From 4c7b7e9b94b4e81aa85de7c13e209017fc7f61dc Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Thu, 5 Feb 2015 13:58:22 -0500 Subject: qemu-io: Use BlockBackend qemu-io should behave like a guest, therefore it should use BlockBackend to access the block layer. There are a couple of places where that is infeasible: First, the bdrv_debug_* functions could theoretically be mirrored in the BlockBackend, but since these are functions internal to the block layer, they should not be visible externally (qemu-io as a test tool is exempt from this). Second, bdrv_get_info() and bdrv_get_specific_info() work on a single BDS alone, therefore they should stay BDS-specific. Third, bdrv_is_allocated() mainly works on a single BDS as well. Some data may be passed through from the BDS's file (if sectors which are apparently allocated in the file are not really allocated there but just zero). [Fixed conflicts around block_acct_start() usage from Fam Zheng's "qemu-io: Account IO by aio_read and aio_write" commit. Use BlockBackend and blk_get_stats() instead of BlockDriverState. --Stefan] Signed-off-by: Max Reitz Reviewed-by: Eric Blake Message-id: 1423162705-32065-14-git-send-email-mreitz@redhat.com Signed-off-by: Stefan Hajnoczi --- qemu-io.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'qemu-io.c') diff --git a/qemu-io.c b/qemu-io.c index a85522a1b3..4a3e71991a 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -28,7 +28,6 @@ static char *progname; static BlockBackend *qemuio_blk; -static BlockDriverState *qemuio_bs; /* qemu-io commands passed using -c */ static int ncmdline; @@ -36,10 +35,9 @@ static char **cmdline; static ReadLineState *readline_state; -static int close_f(BlockDriverState *bs, int argc, char **argv) +static int close_f(BlockBackend *blk, int argc, char **argv) { blk_unref(qemuio_blk); - qemuio_bs = NULL; qemuio_blk = NULL; return 0; } @@ -69,7 +67,6 @@ static int openfile(char *name, int flags, QDict *opts) error_free(local_err); return 1; } - qemuio_bs = blk_bs(qemuio_blk); return 0; } @@ -91,7 +88,7 @@ static void open_help(void) "\n"); } -static int open_f(BlockDriverState *bs, int argc, char **argv); +static int open_f(BlockBackend *blk, int argc, char **argv); static const cmdinfo_t open_cmd = { .name = "open", @@ -115,7 +112,7 @@ static QemuOptsList empty_opts = { }, }; -static int open_f(BlockDriverState *bs, int argc, char **argv) +static int open_f(BlockBackend *blk, int argc, char **argv) { int flags = 0; int readonly = 0; @@ -165,7 +162,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) } } -static int quit_f(BlockDriverState *bs, int argc, char **argv) +static int quit_f(BlockBackend *blk, int argc, char **argv) { return 1; } @@ -302,7 +299,7 @@ static void command_loop(void) char *input; for (i = 0; !done && i < ncmdline; i++) { - done = qemuio_command(qemuio_bs, cmdline[i]); + done = qemuio_command(qemuio_blk, cmdline[i]); } if (cmdline) { g_free(cmdline); @@ -327,7 +324,7 @@ static void command_loop(void) if (input == NULL) { break; } - done = qemuio_command(qemuio_bs, input); + done = qemuio_command(qemuio_blk, input); g_free(input); prompted = 0; -- cgit v1.2.3