Improve error handling in ocf_volume_init()

Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
Robert Baldyga 2022-05-13 20:02:33 +02:00 committed by Jan Musial
parent 9646df431f
commit 70a410b2fe

View File

@ -1,5 +1,5 @@
/* /*
* Copyright(c) 2012-2021 Intel Corporation * Copyright(c) 2012-2022 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
*/ */
@ -94,38 +94,39 @@ int ocf_volume_init(ocf_volume_t volume, ocf_volume_type_t type,
return -OCF_ERR_INVAL; return -OCF_ERR_INVAL;
priv_size = type->properties->volume_priv_size; priv_size = type->properties->volume_priv_size;
volume->opened = false;
volume->type = type;
volume->priv = env_zalloc(priv_size, ENV_MEM_NORMAL); volume->priv = env_zalloc(priv_size, ENV_MEM_NORMAL);
if (!volume->priv) if (!volume->priv)
return -OCF_ERR_NO_MEM; return -OCF_ERR_NO_MEM;
volume->opened = false;
volume->type = type;
volume->uuid.size = 0;
volume->uuid.data = NULL;
volume->uuid_copy = false;
ocf_refcnt_init(&volume->refcnt); ocf_refcnt_init(&volume->refcnt);
ocf_refcnt_freeze(&volume->refcnt); ocf_refcnt_freeze(&volume->refcnt);
if (!uuid) { if (!uuid)
volume->uuid.size = 0;
volume->uuid.data = NULL;
volume->uuid_copy = false;
return 0; return 0;
}
volume->uuid_copy = uuid_copy; volume->uuid_copy = uuid_copy;
if (uuid_copy) { if (uuid_copy) {
data = env_vmalloc(uuid->size); data = env_vmalloc(uuid->size);
if (!data) if (!data) {
goto err; ret = -OCF_ERR_NO_MEM;
ret = env_memcpy(data, uuid->size, uuid->data, uuid->size);
if (ret) {
env_vfree(data);
goto err; goto err;
} }
volume->uuid.data = data; volume->uuid.data = data;
ret = env_memcpy(data, uuid->size, uuid->data, uuid->size);
if (ret) {
ret = -OCF_ERR_INVAL;
goto err;
}
} else { } else {
volume->uuid.data = uuid->data; volume->uuid.data = uuid->data;
} }
@ -137,7 +138,11 @@ int ocf_volume_init(ocf_volume_t volume, ocf_volume_type_t type,
err: err:
ocf_refcnt_unfreeze(&volume->refcnt); ocf_refcnt_unfreeze(&volume->refcnt);
env_free(volume->priv); env_free(volume->priv);
return -OCF_ERR_NO_MEM; if (volume->uuid_copy && volume->uuid.data)
env_vfree(volume->uuid.data);
volume->uuid.data = NULL;
volume->uuid.size = 0;
return ret;
} }
void ocf_volume_deinit(ocf_volume_t volume) void ocf_volume_deinit(ocf_volume_t volume)