diff options
author | Naphtali Sprei <nsprei@redhat.com> | 2009-10-26 16:25:16 +0200 |
---|---|---|
committer | Anthony Liguori <aliguori@us.ibm.com> | 2009-11-09 08:43:01 -0600 |
commit | 8e67cb89a87fd88022a6047063802b224562aae3 (patch) | |
tree | feff1eaf42f55486ef2a9fb6573191dbc5001c83 /block.c | |
parent | 5b4a6b74d0450575a99d9b9f4119f033d3ba64c9 (diff) | |
download | qemu-8e67cb89a87fd88022a6047063802b224562aae3.tar.gz qemu-8e67cb89a87fd88022a6047063802b224562aae3.tar.bz2 qemu-8e67cb89a87fd88022a6047063802b224562aae3.zip |
Added readonly flag to -drive command
This is a slightly revised patch for adding readonly flag to the -drive command.
Even though this patch is "stand-alone", it assumes a previous related patch (in Anthony staging tree), that passes
the readonly attribute of the drive to the guest OS, applied first.
This enables sharing same image between guests, with readonly access.
Implementaion mark the drive as read_only and changes the flags when actually opening the file.
The readonly attribute of a qcow also passed to it's base file.
For ide that cannot pass the readonly attribute to the guest OS, disallow the readonly flag.
Also, return error code from bdrv_truncate for readonly drive.
Signed-off-by: Naphtali Sprei <nsprei@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'block.c')
-rw-r--r-- | block.c | 19 |
1 files changed, 15 insertions, 4 deletions
@@ -331,11 +331,10 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags) int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, BlockDriver *drv) { - int ret, open_flags; + int ret, open_flags, try_rw; char tmp_filename[PATH_MAX]; char backing_filename[PATH_MAX]; - bs->read_only = 0; bs->is_temporary = 0; bs->encrypted = 0; bs->valid_key = 0; @@ -422,9 +421,10 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, /* Note: for compatibility, we open disk image files as RDWR, and RDONLY as fallback */ + try_rw = !bs->read_only || bs->is_temporary; if (!(flags & BDRV_O_FILE)) - open_flags = BDRV_O_RDWR | - (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); + open_flags = (try_rw ? BDRV_O_RDWR : 0) | + (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); else open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); ret = drv->bdrv_open(bs, filename, open_flags); @@ -453,6 +453,8 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, /* if there is a backing file, use it */ BlockDriver *back_drv = NULL; bs->backing_hd = bdrv_new(""); + /* pass on read_only property to the backing_hd */ + bs->backing_hd->read_only = bs->read_only; path_combine(backing_filename, sizeof(backing_filename), filename, bs->backing_file); if (bs->backing_format[0] != '\0') @@ -731,6 +733,8 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset) return -ENOMEDIUM; if (!drv->bdrv_truncate) return -ENOTSUP; + if (bs->read_only) + return -EACCES; return drv->bdrv_truncate(bs, offset); } @@ -925,6 +929,13 @@ int bdrv_is_read_only(BlockDriverState *bs) return bs->read_only; } +int bdrv_set_read_only(BlockDriverState *bs, int read_only) +{ + int ret = bs->read_only; + bs->read_only = read_only; + return ret; +} + int bdrv_is_sg(BlockDriverState *bs) { return bs->sg; |