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>
This commit is contained in:
parent
12888904fe
commit
c3993cdca3
25
block.c
25
block.c
@ -437,6 +437,31 @@ static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
|
|||||||
return 0;
|
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
|
* Common part for opening disk images and files
|
||||||
*/
|
*/
|
||||||
|
1
block.h
1
block.h
@ -69,6 +69,7 @@ int bdrv_create_file(const char* filename, QEMUOptionParameter *options);
|
|||||||
BlockDriverState *bdrv_new(const char *device_name);
|
BlockDriverState *bdrv_new(const char *device_name);
|
||||||
void bdrv_make_anon(BlockDriverState *bs);
|
void bdrv_make_anon(BlockDriverState *bs);
|
||||||
void bdrv_delete(BlockDriverState *bs);
|
void bdrv_delete(BlockDriverState *bs);
|
||||||
|
int bdrv_parse_cache_flags(const char *mode, int *flags);
|
||||||
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
|
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
|
||||||
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
|
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
|
||||||
BlockDriver *drv);
|
BlockDriver *drv);
|
||||||
|
11
blockdev.c
11
blockdev.c
@ -321,16 +321,7 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
|
if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
|
||||||
if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
|
if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
|
||||||
bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
|
|
||||||
} else if (!strcmp(buf, "writeback")) {
|
|
||||||
bdrv_flags |= BDRV_O_CACHE_WB;
|
|
||||||
} else if (!strcmp(buf, "unsafe")) {
|
|
||||||
bdrv_flags |= BDRV_O_CACHE_WB;
|
|
||||||
bdrv_flags |= BDRV_O_NO_FLUSH;
|
|
||||||
} else if (!strcmp(buf, "writethrough")) {
|
|
||||||
/* this is the default */
|
|
||||||
} else {
|
|
||||||
error_report("invalid cache option");
|
error_report("invalid cache option");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
27
qemu-img.c
27
qemu-img.c
@ -183,27 +183,6 @@ static int read_password(char *buf, int buf_size)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int set_cache_flag(const char *mode, int *flags)
|
|
||||||
{
|
|
||||||
*flags &= ~BDRV_O_CACHE_MASK;
|
|
||||||
|
|
||||||
if (!strcmp(mode, "none") || !strcmp(mode, "off")) {
|
|
||||||
*flags |= BDRV_O_CACHE_WB;
|
|
||||||
*flags |= BDRV_O_NOCACHE;
|
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int print_block_option_help(const char *filename, const char *fmt)
|
static int print_block_option_help(const char *filename, const char *fmt)
|
||||||
{
|
{
|
||||||
BlockDriver *drv, *proto_drv;
|
BlockDriver *drv, *proto_drv;
|
||||||
@ -495,7 +474,7 @@ static int img_commit(int argc, char **argv)
|
|||||||
filename = argv[optind++];
|
filename = argv[optind++];
|
||||||
|
|
||||||
flags = BDRV_O_RDWR;
|
flags = BDRV_O_RDWR;
|
||||||
ret = set_cache_flag(cache, &flags);
|
ret = bdrv_parse_cache_flags(cache, &flags);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report("Invalid cache option: %s", cache);
|
error_report("Invalid cache option: %s", cache);
|
||||||
return -1;
|
return -1;
|
||||||
@ -819,7 +798,7 @@ static int img_convert(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
flags = BDRV_O_RDWR;
|
flags = BDRV_O_RDWR;
|
||||||
ret = set_cache_flag(cache, &flags);
|
ret = bdrv_parse_cache_flags(cache, &flags);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report("Invalid cache option: %s", cache);
|
error_report("Invalid cache option: %s", cache);
|
||||||
return -1;
|
return -1;
|
||||||
@ -1291,7 +1270,7 @@ static int img_rebase(int argc, char **argv)
|
|||||||
qemu_progress_print(0, 100);
|
qemu_progress_print(0, 100);
|
||||||
|
|
||||||
flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
|
flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
|
||||||
ret = set_cache_flag(cache, &flags);
|
ret = bdrv_parse_cache_flags(cache, &flags);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_report("Invalid cache option: %s", cache);
|
error_report("Invalid cache option: %s", cache);
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
Reference in New Issue
Block a user