casadm: cache start refactor

Cache start and cache attach will share the same code in casadm; the following
refactor is supposed to facilitate reusing the existing code in next commits

Signed-off-by: Michal Mielewczyk <michal.mielewczyk@huawei.com>
Signed-off-by: Rafal Stefanowski <rafal.stefanowski@huawei.com>
This commit is contained in:
Michal Mielewczyk 2023-08-22 14:40:36 +02:00 committed by Rafal Stefanowski
parent 12e7c8339a
commit b480bbaf47

View File

@ -941,16 +941,13 @@ int check_cache_already_added(const char *cache_device) {
return SUCCESS; return SUCCESS;
} }
int start_cache(uint16_t cache_id, unsigned int cache_init, static int _verify_and_parse_volume_path(char *tgt_buf,
const char *cache_device, ocf_cache_mode_t cache_mode, size_t tgt_buf_size, const char *cache_device,
ocf_cache_line_size_t line_size, int force) size_t paths_size)
{ {
int fd = 0; int fd = 0;
struct kcas_start_cache cmd;
int status;
double min_free_ram_gb;
/* check if cache device given exists */ /* check if cache device exists */
fd = open(cache_device, 0); fd = open(cache_device, 0);
if (fd < 0) { if (fd < 0) {
cas_printf(LOG_ERR, "Device %s not found.\n", cache_device); cas_printf(LOG_ERR, "Device %s not found.\n", cache_device);
@ -958,25 +955,49 @@ int start_cache(uint16_t cache_id, unsigned int cache_init,
} }
close(fd); close(fd);
if (set_device_path(tgt_buf, tgt_buf_size, cache_device, paths_size) != SUCCESS) {
return FAILURE;
}
return SUCCESS;
}
static int _start_cache(uint16_t cache_id, unsigned int cache_init,
const char *cache_device, ocf_cache_mode_t cache_mode,
ocf_cache_line_size_t line_size, int force, bool start)
{
int fd = 0;
struct kcas_start_cache cmd = {};
int status;
double min_free_ram_gb;
fd = open_ctrl_device(); fd = open_ctrl_device();
if (fd == -1) if (fd == -1)
return FAILURE; return FAILURE;
memset(&cmd, 0, sizeof(cmd)); status = _verify_and_parse_volume_path(
cmd.cache_path_name,
cmd.cache_id = cache_id; sizeof(cmd.cache_path_name),
cmd.init_cache = cache_init; cache_device,
if (set_device_path(cmd.cache_path_name, sizeof(cmd.cache_path_name), MAX_STR_LEN);
cache_device, MAX_STR_LEN) != SUCCESS) { if (status != SUCCESS) {
close(fd); close(fd);
return FAILURE; return FAILURE;
} }
cmd.cache_id = cache_id;
cmd.caching_mode = cache_mode; cmd.caching_mode = cache_mode;
cmd.line_size = line_size; cmd.line_size = line_size;
cmd.force = (uint8_t)force; cmd.force = (uint8_t)force;
cmd.init_cache = cache_init;
status = run_ioctl_interruptible_retry(fd, KCAS_IOCTL_START_CACHE, &cmd, status = run_ioctl_interruptible_retry(
"Starting cache", cache_id, OCF_CORE_ID_INVALID); fd,
KCAS_IOCTL_START_CACHE,
&cmd,
"Starting cache",
cache_id,
OCF_CORE_ID_INVALID);
cache_id = cmd.cache_id; cache_id = cmd.cache_id;
if (status < 0) { if (status < 0) {
close(fd); close(fd);
@ -1014,6 +1035,14 @@ int start_cache(uint16_t cache_id, unsigned int cache_init,
return SUCCESS; return SUCCESS;
} }
int start_cache(uint16_t cache_id, unsigned int cache_init,
const char *cache_device, ocf_cache_mode_t cache_mode,
ocf_cache_line_size_t line_size, int force)
{
return _start_cache(cache_id, cache_init, cache_device, cache_mode,
line_size, force, true);
}
int stop_cache(uint16_t cache_id, int flush) int stop_cache(uint16_t cache_id, int flush)
{ {
int fd = 0; int fd = 0;