From 282374ef47a4d8176ad811f62d4453d444a7a257 Mon Sep 17 00:00:00 2001 From: Jason Gunthorpe Date: Wed, 21 Oct 2009 08:11:25 -0700 Subject: [PATCH] libibcm: return errors from the library consistently Remove the return of -errno and always return codes via errno. As documented in libibcm, these libraries are already documented to return -1 to indicate the code is in errno. Also fix errant return of 0 if the read/write syscalls return 0. Signed-off-by: Jason Gunthorpe Signed-off-by: Sean Hefty --- src/cm.c | 89 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/src/cm.c b/src/cm.c index 571e28a..440ea45 100644 --- a/src/cm.c +++ b/src/cm.c @@ -71,6 +71,13 @@ enum { IB_UCM_MAX_DEVICES = 32 }; +static inline int ERR(int err) +{ + errno = err; + return -1; +} + + #define CM_CREATE_MSG_CMD_RESP(msg, cmd, resp, type, size) \ do { \ struct cm_abi_cmd_hdr *hdr; \ @@ -78,7 +85,7 @@ do { \ size = sizeof(*hdr) + sizeof(*cmd); \ msg = alloca(size); \ if (!msg) \ - return -ENOMEM; \ + return ERR(ENOMEM); \ hdr = msg; \ cmd = msg + sizeof(*hdr); \ hdr->cmd = type; \ @@ -87,7 +94,7 @@ do { \ memset(cmd, 0, sizeof(*cmd)); \ resp = alloca(sizeof(*resp)); \ if (!resp) \ - return -ENOMEM; \ + return ERR(ENOMEM); \ cmd->response = (uintptr_t)resp;\ } while (0) @@ -98,7 +105,7 @@ do { \ size = sizeof(*hdr) + sizeof(*cmd); \ msg = alloca(size); \ if (!msg) \ - return -ENOMEM; \ + return ERR(ENOMEM); \ hdr = msg; \ cmd = msg + sizeof(*hdr); \ hdr->cmd = type; \ @@ -260,7 +267,7 @@ int ib_cm_create_id(struct ib_cm_device *device, cm_id_priv = ib_cm_alloc_id(device, context); if (!cm_id_priv) - return -ENOMEM; + return ERR(ENOMEM); CM_CREATE_MSG_CMD_RESP(msg, cmd, resp, IB_USER_CM_CMD_CREATE_ID, size); cmd->uid = (uintptr_t) cm_id_priv; @@ -293,7 +300,7 @@ int ib_cm_destroy_id(struct ib_cm_id *cm_id) result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; VALGRIND_MAKE_MEM_DEFINED(resp, sizeof *resp); @@ -317,14 +324,14 @@ int ib_cm_attr_id(struct ib_cm_id *cm_id, struct ib_cm_attr_param *param) int size; if (!param) - return -EINVAL; + return ERR(EINVAL); CM_CREATE_MSG_CMD_RESP(msg, cmd, resp, IB_USER_CM_CMD_ATTR_ID, size); cmd->id = cm_id->handle; result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; VALGRIND_MAKE_MEM_DEFINED(resp, sizeof *resp); @@ -346,7 +353,7 @@ int ib_cm_init_qp_attr(struct ib_cm_id *cm_id, int size; if (!qp_attr || !qp_attr_mask) - return -EINVAL; + return ERR(EINVAL); CM_CREATE_MSG_CMD_RESP(msg, cmd, resp, IB_USER_CM_CMD_INIT_QP_ATTR, size); cmd->id = cm_id->handle; @@ -354,7 +361,7 @@ int ib_cm_init_qp_attr(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : result; VALGRIND_MAKE_MEM_DEFINED(resp, sizeof *resp); @@ -380,7 +387,7 @@ int ib_cm_listen(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -395,7 +402,7 @@ int ib_cm_send_req(struct ib_cm_id *cm_id, struct ib_cm_req_param *param) int size; if (!param) - return -EINVAL; + return ERR(EINVAL); CM_CREATE_MSG_CMD(msg, cmd, IB_USER_CM_CMD_SEND_REQ, size); cmd->id = cm_id->handle; @@ -417,7 +424,7 @@ int ib_cm_send_req(struct ib_cm_id *cm_id, struct ib_cm_req_param *param) if (param->primary_path) { p_path = alloca(sizeof(*p_path)); if (!p_path) - return -ENOMEM; + return ERR(ENOMEM); ibv_copy_path_rec_to_kern(p_path, param->primary_path); cmd->primary_path = (uintptr_t) p_path; @@ -426,7 +433,7 @@ int ib_cm_send_req(struct ib_cm_id *cm_id, struct ib_cm_req_param *param) if (param->alternate_path) { a_path = alloca(sizeof(*a_path)); if (!a_path) - return -ENOMEM; + return ERR(ENOMEM); ibv_copy_path_rec_to_kern(a_path, param->alternate_path); cmd->alternate_path = (uintptr_t) a_path; @@ -439,7 +446,7 @@ int ib_cm_send_req(struct ib_cm_id *cm_id, struct ib_cm_req_param *param) result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -452,7 +459,7 @@ int ib_cm_send_rep(struct ib_cm_id *cm_id, struct ib_cm_rep_param *param) int size; if (!param) - return -EINVAL; + return ERR(EINVAL); CM_CREATE_MSG_CMD(msg, cmd, IB_USER_CM_CMD_SEND_REP, size); cmd->uid = (uintptr_t) container_of(cm_id, struct cm_id_private, id); @@ -474,7 +481,7 @@ int ib_cm_send_rep(struct ib_cm_id *cm_id, struct ib_cm_rep_param *param) result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -499,7 +506,7 @@ static inline int cm_send_private_data(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -540,7 +547,7 @@ static int cm_establish(struct ib_cm_id *cm_id) result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -556,7 +563,7 @@ int ib_cm_notify(struct ib_cm_id *cm_id, enum ibv_event_type event) if (event == IBV_EVENT_COMM_EST) return cm_establish(cm_id); else - return -EINVAL; + return ERR(EINVAL); } CM_CREATE_MSG_CMD(msg, cmd, IB_USER_CM_CMD_NOTIFY, size); @@ -565,7 +572,7 @@ int ib_cm_notify(struct ib_cm_id *cm_id, enum ibv_event_type event) result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -599,7 +606,7 @@ static inline int cm_send_status(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -649,7 +656,7 @@ int ib_cm_send_mra(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : result; return 0; } @@ -671,7 +678,7 @@ int ib_cm_send_lap(struct ib_cm_id *cm_id, if (alternate_path) { abi_path = alloca(sizeof(*abi_path)); if (!abi_path) - return -ENOMEM; + return ERR(ENOMEM); ibv_copy_path_rec_to_kern(abi_path, alternate_path); cmd->path = (uintptr_t) abi_path; @@ -684,7 +691,7 @@ int ib_cm_send_lap(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -699,7 +706,7 @@ int ib_cm_send_sidr_req(struct ib_cm_id *cm_id, int size; if (!param) - return -EINVAL; + return ERR(EINVAL); CM_CREATE_MSG_CMD(msg, cmd, IB_USER_CM_CMD_SEND_SIDR_REQ, size); cmd->id = cm_id->handle; @@ -711,7 +718,7 @@ int ib_cm_send_sidr_req(struct ib_cm_id *cm_id, if (param->path) { abi_path = alloca(sizeof(*abi_path)); if (!abi_path) - return -ENOMEM; + return ERR(ENOMEM); ibv_copy_path_rec_to_kern(abi_path, param->path); cmd->path = (uintptr_t) abi_path; @@ -724,7 +731,7 @@ int ib_cm_send_sidr_req(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : result; return 0; } @@ -738,7 +745,7 @@ int ib_cm_send_sidr_rep(struct ib_cm_id *cm_id, int size; if (!param) - return -EINVAL; + return ERR(EINVAL); CM_CREATE_MSG_CMD(msg, cmd, IB_USER_CM_CMD_SEND_SIDR_REP, size); cmd->id = cm_id->handle; @@ -758,7 +765,7 @@ int ib_cm_send_sidr_rep(struct ib_cm_id *cm_id, result = write(cm_id->device->fd, msg, size); if (result != size) - return (result > 0) ? -ENODATA : result; + return (result >= 0) ? ERR(ENODATA) : -1; return 0; } @@ -827,12 +834,12 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) int size; if (!event) - return -EINVAL; + return ERR(EINVAL); size = sizeof(*hdr) + sizeof(*cmd); msg = alloca(size); if (!msg) - return -ENOMEM; + return ERR(ENOMEM); hdr = msg; cmd = msg + sizeof(*hdr); @@ -845,7 +852,7 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) resp = alloca(sizeof(*resp)); if (!resp) - return -ENOMEM; + return ERR(ENOMEM); cmd->response = (uintptr_t) resp; cmd->data_len = (uint8_t)(~0U); @@ -853,13 +860,13 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) data = malloc(cmd->data_len); if (!data) { - result = -ENOMEM; + result = ERR(ENOMEM); goto done; } info = malloc(cmd->info_len); if (!info) { - result = -ENOMEM; + result = ERR(ENOMEM); goto done; } @@ -868,7 +875,7 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) result = write(device->fd, msg, size); if (result != size) { - result = (result > 0) ? -ENODATA : result; + result = (result >= 0) ? ERR(ENODATA) : -1; goto done; } @@ -879,7 +886,7 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) */ evt = malloc(sizeof(*evt)); if (!evt) { - result = -ENOMEM; + result = ERR(ENOMEM); goto done; } memset(evt, 0, sizeof(*evt)); @@ -889,7 +896,7 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) if (resp->present & CM_ABI_PRES_PRIMARY) { path_a = malloc(sizeof(*path_a)); if (!path_a) { - result = -ENOMEM; + result = ERR(ENOMEM); goto done; } } @@ -897,7 +904,7 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) if (resp->present & CM_ABI_PRES_ALTERNATE) { path_b = malloc(sizeof(*path_b)); if (!path_b) { - result = -ENOMEM; + result = ERR(ENOMEM); goto done; } } @@ -908,7 +915,7 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) cm_id_priv = ib_cm_alloc_id(evt->cm_id->device, evt->cm_id->context); if (!cm_id_priv) { - result = -ENOMEM; + result = ERR(ENOMEM); goto done; } cm_id_priv->id.handle = resp->id; @@ -946,7 +953,7 @@ int ib_cm_get_event(struct ib_cm_device *device, struct ib_cm_event **event) cm_id_priv = ib_cm_alloc_id(evt->cm_id->device, evt->cm_id->context); if (!cm_id_priv) { - result = -ENOMEM; + result = ERR(ENOMEM); goto done; } cm_id_priv->id.handle = resp->id; @@ -993,7 +1000,7 @@ int ib_cm_ack_event(struct ib_cm_event *event) struct cm_id_private *cm_id_priv; if (!event) - return -EINVAL; + return ERR(EINVAL); if (event->private_data) free(event->private_data); -- 2.41.0