提交 7acea640 编写于 作者: G gaohuatao 提交者: lifeng68

devmapper: add remove device strerror

Signed-off-by: Ngaohuatao <gaohuatao@huawei.com>
上级 642cd5e5
......@@ -51,8 +51,6 @@ struct device_set {
pthread_rwlock_t devmapper_driver_rwlock; //protect all fields of DeviceSet
// options
int64_t data_loop_back_size;
int64_t meta_data_loop_back_size;
uint64_t base_fs_size;
char *filesystem;
char *mount_options;
......
......@@ -282,10 +282,41 @@ out:
return dev_name;
}
static int remove_device(const char *name)
{
int i = 0;
int max_retry = 200;
int ret = 0;
if (name == NULL) {
ERROR("Invalid input");
return -1;
}
for (; i < max_retry; i++) {
ret = dev_remove_device(name);
if (ret == 0) {
DEBUG("devmapper: remove device:%s success", name);
goto out;
}
if (ret != ERR_BUSY) {
ERROR("devmapper: remove device err,cause is not busy,stop retry");
goto out;
}
INFO("devmapper: device is busy so we cannot remove, after 0.1s to retry");
sleep(0.1);
}
out:
return ret;
}
static int deactivate_device_mode(struct device_set *devset, image_devmapper_device_info *dev_info,
bool deferred_remove)
{
int ret = 0;
int nret = 0;
char *dm_name = NULL;
struct dm_info dinfo;
......@@ -296,8 +327,9 @@ static int deactivate_device_mode(struct device_set *devset, image_devmapper_dev
dm_name = get_dm_name(devset, dev_info->hash);
if (dm_name == NULL) {
ret = -1;
ERROR("devmapper: get dm device name failed");
return -1;
goto free_out;
}
if (dev_get_info(&dinfo, dm_name) != 0) {
......@@ -307,12 +339,32 @@ static int deactivate_device_mode(struct device_set *devset, image_devmapper_dev
}
if (dinfo.exists == 0) {
ret = 0;
DEBUG("devmapper: device has exited, no need to remove again");
goto free_out;
}
if (deferred_remove) {
ret = dev_remove_device(dm_name);
nret = dev_remove_device_deferred(dm_name);
if (nret != 0) {
if (nret == ERR_ENXIO) {
WARN("devmapper: device %s has gone", dm_name);
goto free_out;
}
ret = -1;
ERROR("devmapper: remove device:%s failed, err:%s", dm_name, dev_strerror(nret));
goto free_out;
}
} else {
nret = remove_device(dm_name);
if (nret != 0) {
if (nret == ERR_ENXIO) {
WARN("devmapper: device %s has gone", dm_name);
goto free_out;
}
ret = -1;
ERROR("devmapper: remove device:%s without deferred failed, err:%s", dm_name, dev_strerror(nret));
goto free_out;
}
}
free_out:
......@@ -579,7 +631,7 @@ static uint64_t get_base_device_size(struct device_set *devset)
uint64_t res = 0;
devmapper_device_info_t *device_info = NULL;
device_info = lookup_device(devset, "");
device_info = lookup_device(devset, "base");
if (device_info == NULL) {
return 0;
}
......@@ -907,7 +959,7 @@ static void cleanup_deleted_devices(struct device_set *devset)
for (; i < ids_len; i++) {
if (delete_device(idsarray[i], false, devset) != 0) {
WARN("devmapper:Deletion of device %s failed", idsarray[i]);
WARN("devmapper:Deletion of device: \"%s\" failed", idsarray[i]);
}
}
......@@ -2188,9 +2240,13 @@ out:
static int do_delete_device(struct device_set *devset, const char *hash, bool sync_delete)
{
int ret = 0;
bool deferred_remove;
bool deferred_remove = false;
devmapper_device_info_t *device_info = NULL;
if (devset == NULL || hash == NULL) {
return -1;
}
device_info = lookup_device(devset, hash);
if (device_info == NULL) {
ERROR("devmapper: lookup device failed");
......@@ -2243,6 +2299,8 @@ static int setup_base_image(struct device_set *devset)
goto out;
}
DEBUG("devmapper: removing uninitialized base image");
if (do_delete_device(devset, "base", true) != 0) {
ret = -1;
ERROR("devmapper: remove uninitialized base image failed");
......@@ -2326,8 +2384,9 @@ static int do_check_all_devices(struct device_set *devset)
}
// remove broken device
if (length == 0) {
if (dev_remove_device(devices_list[i]) != 0) {
WARN("devmapper: remove broken device %s failed", devices_list[i]);
nret = dev_remove_device(devices_list[i]);
if (nret != 0) {
WARN("devmapper: remove broken device %s failed, err:%s", devices_list[i], dev_strerror(nret));
}
DEBUG("devmapper: remove broken device: %s", devices_list[i]);
}
......@@ -2338,8 +2397,9 @@ static int do_check_all_devices(struct device_set *devset)
continue;
}
if (stat(device_path, &st)) {
if (dev_remove_device(devices_list[i]) != 0) {
WARN("devmapper: remove incompelete device %s", devices_list[i]);
nret = dev_remove_device(devices_list[i]);
if (nret != 0) {
WARN("devmapper: remove incompelete device %s, err:%s", devices_list[i], dev_strerror(nret));
}
DEBUG("devmapper: remove incompelete device: %s", devices_list[i]);
}
......@@ -2680,6 +2740,11 @@ int add_device(const char *hash, const char *base_hash, struct device_set *devse
devmapper_device_info_t *device_info = NULL;
uint64_t size = 0;
if (devset == NULL || hash == NULL || base_hash == NULL) {
ERROR("devmapper: invalid input params to add device");
return -1;
}
if (pthread_rwlock_wrlock(&(devset->devmapper_driver_rwlock)) != 0) {
ERROR("lock devmapper conf failed");
return -1;
......@@ -2776,7 +2841,7 @@ int mount_device(const char *hash, const char *path, const struct driver_mount_o
char *options = NULL;
if (hash == NULL || path == NULL || mount_opts == NULL) {
ERROR("devmapper: failed to mount device");
ERROR("devmapper: invalid input params to mount device");
return -1;
}
......@@ -2785,9 +2850,9 @@ int mount_device(const char *hash, const char *path, const struct driver_mount_o
return -1;
}
device_info = lookup_device(devset, hash);
device_info = lookup_device(devset, strcmp(hash, "") == 0 ? "base" : hash);
if (device_info == NULL) {
ERROR("devmapper: lookup device %s failed", hash);
ERROR("devmapper: lookup device:\"%s\" failed", strcmp(hash, "") == 0 ? "base" : hash);
ret = -1;
goto free_out;
}
......@@ -2806,7 +2871,7 @@ int mount_device(const char *hash, const char *path, const struct driver_mount_o
if (activate_device_if_needed(devset, device_info->info, false) != 0) {
ret = -1;
ERROR("devmapper: Error activating devmapper device for %s", hash);
ERROR("devmapper: Error activating devmapper device for \"%s\"", strcmp(hash, "") == 0 ? "base" : hash);
goto free_out;
}
......@@ -2835,7 +2900,7 @@ int unmount_device(const char *hash, const char *mount_path, struct device_set *
devmapper_device_info_t *device_info = NULL;
if (hash == NULL || mount_path == NULL) {
ERROR("devmapper: failed to unmount device");
ERROR("devmapper: invalid input params to unmount device");
return -1;
}
......@@ -2844,9 +2909,9 @@ int unmount_device(const char *hash, const char *mount_path, struct device_set *
return -1;
}
device_info = lookup_device(devset, hash);
device_info = lookup_device(devset, strcmp(hash, "") == 0 ? "base" : hash);
if (device_info == NULL) {
ERROR("devmapper: lookup device %s failed", hash);
ERROR("devmapper: lookup device: \"%s\" failed", strcmp(hash, "") == 0 ? "base" : hash);
ret = -1;
goto free_out;
}
......@@ -2884,8 +2949,8 @@ bool has_device(const char *hash, struct device_set *devset)
bool res = false;
devmapper_device_info_t *device_info = NULL;
if (hash == NULL) {
ERROR("devmapper: failed to judge device metadata exists");
if (hash == NULL || devset == NULL) {
ERROR("devmapper: invalid input params to judge device metadata exists");
return false;
}
......@@ -2894,9 +2959,9 @@ bool has_device(const char *hash, struct device_set *devset)
return -1;
}
device_info = lookup_device(devset, hash);
device_info = lookup_device(devset, strcmp(hash, "") == 0 ? "base" : hash);
if (device_info == NULL) {
ERROR("devmapper: lookup device %s failed", hash);
ERROR("devmapper: lookup device: \"%s\" failed", strcmp(hash, "") == 0 ? "base" : hash);
goto free_out;
}
......@@ -2917,7 +2982,8 @@ int delete_device(const char *hash, bool sync_delete, struct device_set *devset)
int ret = 0;
devmapper_device_info_t *device_info = NULL;
if (hash == NULL) {
if (devset == NULL || hash == NULL) {
ERROR("Invalid input params");
return -1;
}
......@@ -2926,16 +2992,16 @@ int delete_device(const char *hash, bool sync_delete, struct device_set *devset)
return -1;
}
device_info = lookup_device(devset, hash);
device_info = lookup_device(devset, strcmp(hash, "") == 0 ? "base" : hash);
if (device_info == NULL) {
ret = -1;
ERROR("devmapper: lookup device %s failed", hash);
ERROR("devmapper: lookup device \"%s\" failed", strcmp(hash, "") == 0 ? "base" : hash);
goto free_out;
}
if (do_delete_device(devset, hash, sync_delete) != 0) {
if (do_delete_device(devset, strcmp(hash, "") == 0 ? "base" : hash, sync_delete) != 0) {
ret = -1;
ERROR("devmapper: do delete device:%s failed", hash);
ERROR("devmapper: do delete device: \"%s\" failed", strcmp(hash, "") == 0 ? "base" : hash);
goto free_out;
}
......@@ -2957,6 +3023,7 @@ int export_device_metadata(struct device_metadata *dev_metadata, const char *has
devmapper_device_info_t *device_info = NULL;
if (hash == NULL || dev_metadata == NULL) {
ERROR("Invalid input params");
return -1;
}
......@@ -2965,17 +3032,17 @@ int export_device_metadata(struct device_metadata *dev_metadata, const char *has
return -1;
}
dm_name = get_dm_name(devset, hash);
dm_name = get_dm_name(devset, strcmp(hash, "") == 0 ? "base" : hash);
if (dm_name == NULL) {
ret = -1;
ERROR("devmapper: failed to get dm %s name", hash);
ERROR("devmapper: failed to get device: \"%s\" dm name", strcmp(hash, "") == 0 ? "base" : hash);
goto free_out;
}
device_info = lookup_device(devset, hash);
device_info = lookup_device(devset, strcmp(hash, "") == 0 ? "base" : hash);
if (device_info == NULL) {
ret = -1;
ERROR("devmapper: lookup device %s failed", hash);
ERROR("devmapper: lookup device: \"%s\" failed", strcmp(hash, "") == 0 ? "base" : hash);
goto free_out;
}
......
......@@ -60,15 +60,20 @@ int device_set_init(struct graphdriver *driver, const char *drvier_home, const c
int add_device(const char *hash, const char *base_hash, struct device_set *devset,
const json_map_string_string *storage_opts);
int mount_device(const char *hash, const char *path, const struct driver_mount_opts *mount_opts,
struct device_set *devset);
int unmount_device(const char *hash, const char *mount_path, struct device_set *devset);
bool has_device(const char *hash, struct device_set *devset);
int delete_device(const char *hash, bool sync_delete, struct device_set *devset);
int export_device_metadata(struct device_metadata *dev_metadata, const char *hash, struct device_set *devset);
struct status *device_set_status(struct device_set *devset);
void free_devmapper_status(struct status *st);
int device_set_shutdown(struct device_set *devset, const char *home);
......
......@@ -27,6 +27,43 @@ static bool dm_saw_enxio = false; // no such device or address
// static bool dm_saw_eno_data = false; // no data available
static int64_t dm_udev_wait_timeout = 0;
char *dev_strerror(int errnum)
{
char *errmsg = NULL;
switch (errnum) {
case ERR_TASK_RUN:
errmsg = "Task run error";
break;
case ERR_TASK_SET_COOKIE:
errmsg = "Task set cookie error";
break;
case ERR_TASK_SET_ADD_NODE:
errmsg = "Task add dm node failed";
break;
case ERR_BUSY:
errmsg = "Device busy";
break;
case ERR_DEVICE_ID_EXISTS:
errmsg = "Device exists already";
break;
case ERR_ENXIO:
errmsg = "No such device of address";
break;
case ERR_TASK_ADD_TARGET:
errmsg = "Task add target device error";
break;
case ERR_TASK_DEFERRED_REMOVE:
errmsg = "dm_task_deferred_remove failed";
break;
default:
errmsg = "Unknown error";
break;
}
return errmsg;
}
struct dm_task *task_create(int type)
{
struct dm_task *dmt = NULL;
......@@ -489,18 +526,18 @@ free_out:
free(uwait);
}
int dev_remove_device(const char *pool_fname)
int dev_remove_device(const char *name)
{
int ret = 0;
struct dm_task *dmt = NULL;
uint32_t cookie = 0;
if (pool_fname == NULL) {
if (name == NULL) {
ret = -1;
goto out;
}
dmt = task_create_named(DM_DEVICE_REMOVE, pool_fname);
dmt = task_create_named(DM_DEVICE_REMOVE, name);
if (dmt == NULL) {
ERROR("devicemapper: create task with name:DM_DEVICE_REMOVE failed");
ret = -1;
......@@ -539,19 +576,19 @@ out:
return ret;
}
int dev_remove_device_deferred(const char *pool_fname)
int dev_remove_device_deferred(const char *name)
{
int ret = 0;
struct dm_task *dmt = NULL;
uint32_t cookie = 0;
uint16_t flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK;
if (pool_fname == NULL) {
if (name == NULL) {
ret = -1;
goto out;
}
dmt = task_create_named(DM_DEVICE_REMOVE, pool_fname);
dmt = task_create_named(DM_DEVICE_REMOVE, name);
if (dmt == NULL) {
ret = -1;
goto out;
......
......@@ -73,6 +73,7 @@ typedef struct {
int state; // 0: ok 1:err_udev_wait 2: err_udev_wait_timeout
} udev_wait_pth_t;
char *dev_strerror(int errnum);
struct dm_task* task_create(int type);
......@@ -104,6 +105,8 @@ int dev_get_info(struct dm_info *info, const char *name);
int dev_remove_device(const char *name);
int dev_remove_device_deferred(const char *name);
int dev_get_device_list(char ***list, size_t *length);
bool udev_sync_supported();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册