block: Open the underlying image file in generic code
Format drivers shouldn't need to bother with things like file names, but rather
just get an open BlockDriverState for the underlying protocol. This patch
introduces this behaviour for bdrv_open implementation. For protocols which
need to access the filename to open their file/device/connection/... a new
callback bdrv_file_open is introduced which doesn't get an underlying file
opened.
For now, also some of the more obscure formats use bdrv_file_open because they
open() the file themselves instead of using the block.c functions. They need to
be fixed in later patches.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
diff --git a/block.c b/block.c
index f9b5e53..eb1d562 100644
--- a/block.c
+++ b/block.c
@@ -288,6 +288,8 @@
int len;
const char *p;
+ /* TODO Drivers without bdrv_file_open must be specified explicitly */
+
#ifdef _WIN32
if (is_windows_drive(filename) ||
is_windows_drive_prefix(filename))
@@ -360,6 +362,7 @@
assert(drv != NULL);
+ bs->file = NULL;
bs->is_temporary = 0;
bs->encrypted = 0;
bs->valid_key = 0;
@@ -398,7 +401,16 @@
open_flags |= BDRV_O_RDWR;
}
- ret = drv->bdrv_open(bs, filename, open_flags);
+ /* Open the image, either directly or using a protocol */
+ if (drv->bdrv_file_open) {
+ ret = drv->bdrv_file_open(bs, filename, open_flags);
+ } else {
+ ret = bdrv_file_open(&bs->file, filename, open_flags);
+ if (ret >= 0) {
+ ret = drv->bdrv_open(bs, open_flags);
+ }
+ }
+
if (ret < 0) {
goto free_and_fail;
}
@@ -415,6 +427,10 @@
return 0;
free_and_fail:
+ if (bs->file) {
+ bdrv_delete(bs->file);
+ bs->file = NULL;
+ }
qemu_free(bs->opaque);
bs->opaque = NULL;
bs->drv = NULL;
@@ -585,6 +601,10 @@
bs->opaque = NULL;
bs->drv = NULL;
+ if (bs->file != NULL) {
+ bdrv_close(bs->file);
+ }
+
/* call the change callback */
bs->media_changed = 1;
if (bs->change_cb)
@@ -600,6 +620,10 @@
}
bdrv_close(bs);
+ if (bs->file != NULL) {
+ bdrv_delete(bs->file);
+ }
+
qemu_free(bs);
}