summaryrefslogtreecommitdiff
path: root/block.c
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@linux.vnet.ibm.com>2011-08-04 12:26:51 +0100
committerKevin Wolf <kwolf@redhat.com>2011-08-23 14:15:17 +0200
commit409e851823b4d13be16c88a54d6280a495ad4f7b (patch)
treefd31efb3149956a7fd09de821851b52ce2e20580 /block.c
parentb23888cecaec1ed2ec1408e8db52cc208002539f (diff)
downloadqemu-409e851823b4d13be16c88a54d6280a495ad4f7b.tar.gz
qemu-409e851823b4d13be16c88a54d6280a495ad4f7b.tar.bz2
qemu-409e851823b4d13be16c88a54d6280a495ad4f7b.zip
block: parse cache mode flags in a single place
This patch introduces bdrv_parse_cache_flags() which sets open flags given a cache mode. Previously this was duplicated in blockdev.c and qemu-img.c. Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'block.c')
-rw-r--r--block.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/block.c b/block.c
index a8a013a6d2..dbef3ae672 100644
--- a/block.c
+++ b/block.c
@@ -437,6 +437,31 @@ static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
return 0;
}
+/**
+ * Set open flags for a given cache mode
+ *
+ * Return 0 on success, -1 if the cache mode was invalid.
+ */
+int bdrv_parse_cache_flags(const char *mode, int *flags)
+{
+ *flags &= ~BDRV_O_CACHE_MASK;
+
+ if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
+ *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
+ } else if (!strcmp(mode, "writeback")) {
+ *flags |= BDRV_O_CACHE_WB;
+ } else if (!strcmp(mode, "unsafe")) {
+ *flags |= BDRV_O_CACHE_WB;
+ *flags |= BDRV_O_NO_FLUSH;
+ } else if (!strcmp(mode, "writethrough")) {
+ /* this is the default */
+ } else {
+ return -1;
+ }
+
+ return 0;
+}
+
/*
* Common part for opening disk images and files
*/