Use enum tgtadm_errno explicitly in all related functions. This enum is typedef'ed to tgtadm_err for brevity. This is done to avoid mix-up of the regular linux errno codes, of int type, and the codes defined by enum tgtadm_errno. As C does not provide for strict type checking for enums and mixes them freely with integers, some extra effort should be made to separate between these two types of error codes. These changes also make the code more readable by explicitly defining some functions as acting on behalf of the management tasks by making them return tgtadm_err. Signed-off-by: Alexander Nezhinsky <alexandern@xxxxxxxxxxxx> --- usr/bs.c | 6 +- usr/bs_aio.c | 4 +- usr/bs_rdwr.c | 2 +- usr/bs_sg.c | 8 +- usr/bs_ssc.c | 2 +- usr/bs_thread.h | 4 +- usr/driver.h | 6 +- usr/iscsi/conn.c | 2 +- usr/iscsi/iscsid.h | 20 ++-- usr/iscsi/target.c | 30 +++--- usr/mgmt.c | 255 +++++++++++++++++++++++++++------------------------- usr/mmc.c | 6 +- usr/osd.c | 4 +- usr/sbc.c | 4 +- usr/scc.c | 4 +- usr/smc.c | 56 ++++++------ usr/spc.c | 31 ++++--- usr/spc.h | 8 +- usr/ssc.c | 9 +- usr/target.c | 164 +++++++++++++++++----------------- usr/tgtadm.c | 2 + usr/tgtadm_error.h | 4 + usr/tgtd.h | 45 +++++----- 23 files changed, 353 insertions(+), 323 deletions(-) diff --git a/usr/bs.c b/usr/bs.c index e56e450..e2faf30 100644 --- a/usr/bs.c +++ b/usr/bs.c @@ -323,8 +323,8 @@ int bs_init(void) return 1; } -int bs_thread_open(struct bs_thread_info *info, request_func_t *rfn, - int nr_threads) +tgtadm_err bs_thread_open(struct bs_thread_info *info, request_func_t *rfn, + int nr_threads) { int i, ret; @@ -356,7 +356,7 @@ int bs_thread_open(struct bs_thread_info *info, request_func_t *rfn, pthread_mutex_unlock(&info->startup_lock); info->nr_worker_threads = nr_threads; - return 0; + return TGTADM_SUCCESS; destroy_threads: info->stop = 1; diff --git a/usr/bs_aio.c b/usr/bs_aio.c index 8d07df1..cbd91ba 100644 --- a/usr/bs_aio.c +++ b/usr/bs_aio.c @@ -366,7 +366,7 @@ static void bs_aio_close(struct scsi_lu *lu) close(lu->fd); } -static int bs_aio_init(struct scsi_lu *lu) +static tgtadm_err bs_aio_init(struct scsi_lu *lu) { struct bs_aio_info *info = BS_AIO_I(lu); int i; @@ -379,7 +379,7 @@ static int bs_aio_init(struct scsi_lu *lu) for (i=0; i < ARRAY_SIZE(info->iocb_arr); i++) info->piocb_arr[i] = &info->iocb_arr[i]; - return 0; + return TGTADM_SUCCESS; } static void bs_aio_exit(struct scsi_lu *lu) diff --git a/usr/bs_rdwr.c b/usr/bs_rdwr.c index 03b869d..63575b6 100644 --- a/usr/bs_rdwr.c +++ b/usr/bs_rdwr.c @@ -150,7 +150,7 @@ static void bs_rdwr_close(struct scsi_lu *lu) int nr_iothreads = 16; -static int bs_rdwr_init(struct scsi_lu *lu) +static tgtadm_err bs_rdwr_init(struct scsi_lu *lu) { struct bs_thread_info *info = BS_THREAD_I(lu); diff --git a/usr/bs_sg.c b/usr/bs_sg.c index f0f037d..4f78def 100644 --- a/usr/bs_sg.c +++ b/usr/bs_sg.c @@ -400,7 +400,7 @@ static int init_sg_device(int fd) return 0; } -static int bs_sg_init(struct scsi_lu *lu) +static tgtadm_err bs_sg_init(struct scsi_lu *lu) { /* * Setup struct scsi_lu->cmd_perform() passthrough pointer @@ -413,7 +413,7 @@ static int bs_sg_init(struct scsi_lu *lu) * usr/target.c:__cmd_done_passthrough(). */ lu->cmd_done = &__cmd_done_passthrough; - return 0; + return TGTADM_SUCCESS; } static int bs_sg_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size) @@ -461,12 +461,12 @@ static void bs_sg_close(struct scsi_lu *lu) close(lu->fd); } -static int bs_sg_lu_init(struct scsi_lu *lu) +static tgtadm_err bs_sg_lu_init(struct scsi_lu *lu) { if (spc_lu_init(lu)) return TGTADM_NOMEM; - return 0; + return TGTADM_SUCCESS; } static struct backingstore_template sg_bst = { diff --git a/usr/bs_ssc.c b/usr/bs_ssc.c index 469057d..7270734 100644 --- a/usr/bs_ssc.c +++ b/usr/bs_ssc.c @@ -604,7 +604,7 @@ static void tape_rdwr_request(struct scsi_cmd *cmd) cmd, cmd->scb[0], ret, length, cmd->offset); } -static int bs_ssc_init(struct scsi_lu *lu) +static tgtadm_err bs_ssc_init(struct scsi_lu *lu) { struct bs_thread_info *info = BS_THREAD_I(lu); return bs_thread_open(info, tape_rdwr_request, 1); diff --git a/usr/bs_thread.h b/usr/bs_thread.h index 50dd1c3..beb4c3f 100644 --- a/usr/bs_thread.h +++ b/usr/bs_thread.h @@ -23,8 +23,8 @@ static inline struct bs_thread_info *BS_THREAD_I(struct scsi_lu *lu) return (struct bs_thread_info *) ((char *)lu + sizeof(*lu)); } -extern int bs_thread_open(struct bs_thread_info *info, request_func_t *rfn, - int nr_threads); +extern tgtadm_err bs_thread_open(struct bs_thread_info *info, request_func_t *rfn, + int nr_threads); extern void bs_thread_close(struct bs_thread_info *info); extern int bs_thread_cmd_submit(struct scsi_cmd *cmd); diff --git a/usr/driver.h b/usr/driver.h index b7e454c..53c19b6 100644 --- a/usr/driver.h +++ b/usr/driver.h @@ -1,6 +1,8 @@ #ifndef __DRIVER_H__ #define __DRIVER_H__ +#include "tgtadm_error.h" + enum tgt_driver_state { DRIVER_REGD = 0, /* just registered */ DRIVER_INIT, /* initialized ok */ @@ -23,8 +25,8 @@ struct tgt_driver { int (*lu_create)(struct scsi_lu *); - int (*update)(int, int, int ,uint64_t, uint64_t, uint32_t, char *); - int (*show)(int, int, uint64_t, uint32_t, uint64_t, struct concat_buf *); + tgtadm_err (*update)(int, int, int ,uint64_t, uint64_t, uint32_t, char *); + tgtadm_err (*show)(int, int, uint64_t, uint32_t, uint64_t, struct concat_buf *); uint64_t (*scsi_get_lun)(uint8_t *); diff --git a/usr/iscsi/conn.c b/usr/iscsi/conn.c index 53e719e..9015610 100644 --- a/usr/iscsi/conn.c +++ b/usr/iscsi/conn.c @@ -216,7 +216,7 @@ int conn_take_fd(struct iscsi_connection *conn) } /* called by tgtadm */ -int conn_close_admin(uint32_t tid, uint64_t sid, uint32_t cid) +tgtadm_err conn_close_admin(uint32_t tid, uint64_t sid, uint32_t cid) { struct iscsi_target* target = NULL; struct iscsi_session *session; diff --git a/usr/iscsi/iscsid.h b/usr/iscsi/iscsid.h index 4f37604..bed83c0 100644 --- a/usr/iscsi/iscsid.h +++ b/usr/iscsi/iscsid.h @@ -302,7 +302,7 @@ extern int conn_get(struct iscsi_connection *conn); extern struct iscsi_connection * conn_find(struct iscsi_session *session, uint32_t cid); extern int conn_take_fd(struct iscsi_connection *conn); extern void conn_add_to_session(struct iscsi_connection *conn, struct iscsi_session *session); -extern int conn_close_admin(uint32_t tid, uint64_t sid, uint32_t cid); +extern tgtadm_err conn_close_admin(uint32_t tid, uint64_t sid, uint32_t cid); /* iscsid.c */ extern char *text_key_find(struct iscsi_connection *conn, char *searchKey); @@ -328,22 +328,22 @@ extern void session_get(struct iscsi_session *session); extern void session_put(struct iscsi_session *session); /* target.c */ -struct iscsi_target * target_find_by_name(const char *name); -struct iscsi_target * target_find_by_id(int tid); +extern struct iscsi_target * target_find_by_name(const char *name); +extern struct iscsi_target * target_find_by_id(int tid); extern void target_list_build(struct iscsi_connection *, char *, char *); extern int ip_acl(int tid, struct iscsi_connection *conn); extern int iqn_acl(int tid, struct iscsi_connection *conn); extern int iscsi_target_create(struct target *); extern void iscsi_target_destroy(int tid, int force); -extern int iscsi_target_show(int mode, int tid, uint64_t sid, uint32_t cid, - uint64_t lun, struct concat_buf *b); -int iscsi_target_update(int mode, int op, int tid, uint64_t sid, uint64_t lun, - uint32_t cid, char *name); -int target_redirected(struct iscsi_target *target, - struct iscsi_connection *conn, char *buf, int *reason); +extern tgtadm_err iscsi_target_show(int mode, int tid, uint64_t sid, uint32_t cid, + uint64_t lun, struct concat_buf *b); +extern tgtadm_err iscsi_target_update(int mode, int op, int tid, uint64_t sid, uint64_t lun, + uint32_t cid, char *name); +extern int target_redirected(struct iscsi_target *target, + struct iscsi_connection *conn, char *buf, int *reason); /* param.c */ -int param_index_by_name(char *name, struct iscsi_key *keys); +extern int param_index_by_name(char *name, struct iscsi_key *keys); /* transport.c */ extern int iscsi_init(int, char *); diff --git a/usr/iscsi/target.c b/usr/iscsi/target.c index eb3afac..d266c62 100644 --- a/usr/iscsi/target.c +++ b/usr/iscsi/target.c @@ -496,16 +496,17 @@ static int iscsi_session_param_update(struct iscsi_target* target, int idx, char return 0; } -int iscsi_target_update(int mode, int op, int tid, uint64_t sid, uint64_t lun, - uint32_t cid, char *name) +tgtadm_err iscsi_target_update(int mode, int op, int tid, uint64_t sid, uint64_t lun, + uint32_t cid, char *name) { - int idx, err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; + int idx, err; char *str; struct iscsi_target* target; switch (mode) { case MODE_SYSTEM: - err = isns_update(name); + adm_err = isns_update(name); break; case MODE_TARGET: target = target_find_by_id(tid); @@ -519,12 +520,12 @@ int iscsi_target_update(int mode, int op, int tid, uint64_t sid, uint64_t lun, if (!strncmp(name, "RedirectAddress", 15)) { snprintf(target->redirect_info.addr, sizeof(target->redirect_info.addr), "%s", str); - err = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; break; } else if (!strncmp(name, "RedirectPort", 12)) { snprintf(target->redirect_info.port, sizeof(target->redirect_info.port), "%s", str); - err = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; break; } else if (!strncmp(name, "RedirectReason", 14)) { if (!strncmp(str, "Temporary", 9)) { @@ -534,33 +535,32 @@ int iscsi_target_update(int mode, int op, int tid, uint64_t sid, uint64_t lun, } else if (!strncmp(str, "Permanent", 9)) { target->redirect_info.reason = ISCSI_LOGIN_STATUS_TGT_MOVED_PERM; - err = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; } else break; } else if (!strncmp(name, "RedirectCallback", 16)) { target->redirect_info.callback = strdup(str); if (!target->redirect_info.callback) { - err = TGTADM_NOMEM; + adm_err = TGTADM_NOMEM; break; } - err = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; } idx = param_index_by_name(name, session_keys); if (idx >= 0) { err = iscsi_session_param_update(target, idx, str); - if (err < 0) - err = TGTADM_INVALID_REQUEST; + adm_err = !err ? TGTADM_SUCCESS : TGTADM_INVALID_REQUEST; } break; case MODE_CONNECTION: if (op == OP_DELETE) - err = conn_close_admin(tid, sid, cid); + adm_err = conn_close_admin(tid, sid, cid); break; default: break; } - return err; + return adm_err; } static int show_iscsi_param(struct param *param, struct concat_buf *b) @@ -702,8 +702,8 @@ static int show_redirect_callback(struct iscsi_target *target, struct concat_buf return TGTADM_SUCCESS; } -int iscsi_target_show(int mode, int tid, uint64_t sid, uint32_t cid, uint64_t lun, - struct concat_buf *b) +tgtadm_err iscsi_target_show(int mode, int tid, uint64_t sid, uint32_t cid, uint64_t lun, + struct concat_buf *b) { struct iscsi_target* target = NULL; diff --git a/usr/mgmt.c b/usr/mgmt.c index 819afe2..d7a1de8 100644 --- a/usr/mgmt.c +++ b/usr/mgmt.c @@ -68,20 +68,18 @@ char mgmt_path[256]; static struct mgmt_task *mtask_alloc(void); static void mtask_free(struct mgmt_task *mtask); -static void set_mtask_result(struct mgmt_task *mtask, int err) +static void set_mtask_result(struct mgmt_task *mtask, tgtadm_err adm_err) { mtask->rsp.len = sizeof(mtask->rsp); - if (!err) { + if (adm_err == TGTADM_SUCCESS) { mtask->rsp.err = 0; mtask->rsp.len += mtask->rsp_bsize; } - else if (err < 0) - mtask->rsp.err = -err; else - mtask->rsp.err = err; + mtask->rsp.err = (uint32_t)adm_err; } -static enum tgtadm_errno errno2tgtadm(int err) +static tgtadm_err errno2tgtadm(int err) { if (err >= 0) return TGTADM_SUCCESS; @@ -91,66 +89,88 @@ static enum tgtadm_errno errno2tgtadm(int err) return TGTADM_UNKNOWN_ERR; } -static int target_mgmt(int lld_no, struct mgmt_task *mtask) +static tgtadm_err target_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; struct concat_buf b; - int err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; switch (req->op) { case OP_NEW: - err = tgt_target_create(lld_no, req->tid, mtask->req_buf); + adm_err = tgt_target_create(lld_no, req->tid, mtask->req_buf); break; case OP_DELETE: - err = tgt_target_destroy(lld_no, req->tid, req->force); + adm_err = tgt_target_destroy(lld_no, req->tid, req->force); break; case OP_BIND: /* FIXME */ if (req->len == sizeof(*req)) - err = tgt_bind_host_to_target(req->tid, req->host_no); + adm_err = tgt_bind_host_to_target(req->tid, req->host_no); else { char *p; p = strstr(mtask->req_buf, "initiator-address="); - if (p) - err = acl_add(req->tid, p + strlen("initiator-address=")); + if (p) { + p += strlen("initiator-address="); + adm_err = acl_add(req->tid, p); + if (adm_err != TGTADM_SUCCESS) { + eprintf("Failed to bind by address: %s\n", p); + break; + } + } p = strstr(mtask->req_buf, "initiator-name="); - if (p) - err = iqn_acl_add(req->tid, p + strlen("initiator-name=")); + if (p) { + p += strlen("initiator-name="); + adm_err = iqn_acl_add(req->tid, p); + if (adm_err != TGTADM_SUCCESS) { + eprintf("Failed to bind by name: %s\n", p); + break; + } + } } break; case OP_UNBIND: if (req->len == sizeof(*req)) - err = tgt_unbind_host_to_target(req->tid, req->host_no); + adm_err = tgt_unbind_host_to_target(req->tid, req->host_no); else { char *p; p = strstr(mtask->req_buf, "initiator-address="); if (p) { - err = acl_del(req->tid, p + strlen("initiator-address=")); + p += strlen("initiator-address="); + adm_err = acl_del(req->tid, p); + if (adm_err != TGTADM_SUCCESS) { + eprintf("Failed to unbind by address: %s\n", p); + break; + } } p = strstr(mtask->req_buf, "initiator-name="); if (p) { - err = iqn_acl_del(req->tid, p + strlen("initiator-name=")); + p += strlen("initiator-name="); + adm_err = iqn_acl_del(req->tid, p + strlen("initiator-name=")); + if (adm_err != TGTADM_SUCCESS) { + eprintf("Failed to unbind by name: %s\n", p); + break; + } } } break; case OP_UPDATE: { char *p; - err = TGTADM_UNSUPPORTED_OPERATION; + adm_err = TGTADM_UNSUPPORTED_OPERATION; p = strchr(mtask->req_buf, '='); if (!p) break; *p++ = '\0'; - if (!strcmp(mtask->req_buf, "state")) - err = tgt_set_target_state(req->tid, p); - else if (tgt_drivers[lld_no]->update) - err = tgt_drivers[lld_no]->update(req->mode, req->op, req->tid, + if (!strcmp(mtask->req_buf, "state")) { + adm_err = tgt_set_target_state(req->tid, p); + } else if (tgt_drivers[lld_no]->update) + adm_err = tgt_drivers[lld_no]->update(req->mode, req->op, req->tid, req->sid, req->lun, req->cid, mtask->req_buf); break; @@ -159,97 +179,96 @@ static int target_mgmt(int lld_no, struct mgmt_task *mtask) { concat_buf_init(&b, &mtask->rsp_buf, &mtask->rsp_bsize); if (req->tid < 0) - err = tgt_target_show_all(&b); - else if (tgt_drivers[lld_no]->show) - err = tgt_drivers[lld_no]->show(req->mode, + tgt_target_show_all(&b); + else if (tgt_drivers[lld_no]->show) { + adm_err = tgt_drivers[lld_no]->show(req->mode, req->tid, req->sid, req->cid, req->lun, &b); - if (!err) { - err = concat_buf_finish(&b); - err = errno2tgtadm(err); + if (adm_err != TGTADM_SUCCESS) { + concat_buf_finish(&b); + break; + } } - else - concat_buf_finish(&b); + adm_err = errno2tgtadm(concat_buf_finish(&b)); break; } default: break; } - set_mtask_result(mtask, err); - return err; + set_mtask_result(mtask, adm_err); + return adm_err; } -static int portal_mgmt(int lld_no, struct mgmt_task *mtask) +static tgtadm_err portal_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; struct concat_buf b; - int err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; switch (req->op) { case OP_SHOW: if (tgt_drivers[lld_no]->show) { concat_buf_init(&b, &mtask->rsp_buf, &mtask->rsp_bsize); - err = tgt_drivers[lld_no]->show(req->mode, + adm_err = tgt_drivers[lld_no]->show(req->mode, req->tid, req->sid, req->cid, req->lun, &b); - if (!err) { - err = concat_buf_finish(&b); - err = errno2tgtadm(err); - } - else + if (adm_err != TGTADM_SUCCESS) { concat_buf_finish(&b); + break; + } + adm_err = errno2tgtadm(concat_buf_finish(&b)); } break; case OP_NEW: - err = tgt_portal_create(lld_no, mtask->req_buf); + adm_err = tgt_portal_create(lld_no, mtask->req_buf); break; case OP_DELETE: - err = tgt_portal_destroy(lld_no, mtask->req_buf); + adm_err = tgt_portal_destroy(lld_no, mtask->req_buf); break; default: break; } - set_mtask_result(mtask, err); - return err; + set_mtask_result(mtask, adm_err); + return adm_err; } -static int device_mgmt(int lld_no, struct mgmt_task *mtask) +static tgtadm_err device_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; char *params = mtask->req_buf; - int err = TGTADM_UNSUPPORTED_OPERATION; + tgtadm_err adm_err = TGTADM_UNSUPPORTED_OPERATION; switch (req->op) { case OP_NEW: eprintf("sz:%d params:%s\n",mtask->req_bsize,params); - err = tgt_device_create(req->tid, req->device_type, req->lun, - params, 1); + adm_err = tgt_device_create(req->tid, req->device_type, req->lun, + params, 1); break; case OP_DELETE: - err = tgt_device_destroy(req->tid, req->lun, 0); + adm_err = tgt_device_destroy(req->tid, req->lun, 0); break; case OP_UPDATE: - err = tgt_device_update(req->tid, req->lun, params); + adm_err = tgt_device_update(req->tid, req->lun, params); break; default: break; } - set_mtask_result(mtask, err); - return err; + set_mtask_result(mtask, adm_err); + return adm_err; } -static int account_mgmt(int lld_no, struct mgmt_task *mtask) +static tgtadm_err account_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; char *user, *password; struct concat_buf b; - int err = TGTADM_UNSUPPORTED_OPERATION; + tgtadm_err adm_err = TGTADM_UNSUPPORTED_OPERATION; switch (req->op) { case OP_NEW: @@ -269,53 +288,48 @@ static int account_mgmt(int lld_no, struct mgmt_task *mtask) *password++ = '\0'; password += strlen("password="); - err = account_add(user, password); + adm_err = account_add(user, password); } else { if (req->op == OP_DELETE) { - err = account_del(user); + adm_err = account_del(user); } else - err = account_ctl(req->tid, req->ac_dir, - user, req->op == OP_BIND); + adm_err = account_ctl(req->tid, req->ac_dir, + user, req->op == OP_BIND); } break; case OP_SHOW: concat_buf_init(&b, &mtask->rsp_buf, &mtask->rsp_bsize); - err = account_show(&b); - if (!err) { - err = concat_buf_finish(&b); - err = errno2tgtadm(err); - } - else - concat_buf_finish(&b); + account_show(&b); + adm_err = errno2tgtadm(concat_buf_finish(&b)); break; default: break; } out: - set_mtask_result(mtask, err); - return err; + set_mtask_result(mtask, adm_err); + return adm_err; } -static int sys_mgmt(int lld_no, struct mgmt_task *mtask) +static tgtadm_err sys_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; struct concat_buf b; - int err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; switch (req->op) { case OP_UPDATE: if (!strncmp(mtask->req_buf, "debug=", 6)) { if (!strncmp(mtask->req_buf+6, "on", 2)) { is_debug = 1; - err = 0; + adm_err = TGTADM_SUCCESS; } else if (!strncmp(mtask->req_buf+6, "off", 3)) { is_debug = 0; - err = 0; + adm_err = TGTADM_SUCCESS; } - if (!err) + if (adm_err == TGTADM_SUCCESS) eprintf("set debug to: %d\n", is_debug); } else if (tgt_drivers[lld_no]->update) - err = tgt_drivers[lld_no]->update(req->mode, req->op, + adm_err = tgt_drivers[lld_no]->update(req->mode, req->op, req->tid, req->sid, req->lun, req->cid, mtask->req_buf); @@ -323,74 +337,72 @@ static int sys_mgmt(int lld_no, struct mgmt_task *mtask) break; case OP_SHOW: concat_buf_init(&b, &mtask->rsp_buf, &mtask->rsp_bsize); - err = system_show(req->mode, &b); - if (!err && tgt_drivers[lld_no]->show) { - err = tgt_drivers[lld_no]->show(req->mode, + system_show(req->mode, &b); + if (tgt_drivers[lld_no]->show) { + adm_err = tgt_drivers[lld_no]->show(req->mode, req->tid, req->sid, req->cid, req->lun, &b); + if (adm_err != TGTADM_SUCCESS) { + concat_buf_finish(&b); + break; + } } - if (!err) { - err = concat_buf_finish(&b); - err = errno2tgtadm(err); - } - else - concat_buf_finish(&b); + adm_err = errno2tgtadm(concat_buf_finish(&b)); break; case OP_DELETE: if (is_system_inactive()) - err = 0; + adm_err = TGTADM_SUCCESS; break; default: break; } - set_mtask_result(mtask, err); - return err; + set_mtask_result(mtask, adm_err); + return adm_err; } -static int connection_mgmt(int lld_no, struct mgmt_task *mtask) +static tgtadm_err connection_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; struct concat_buf b; - int err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; switch (req->op) { case OP_SHOW: if (tgt_drivers[lld_no]->show) { concat_buf_init(&b, &mtask->rsp_buf, &mtask->rsp_bsize); - err = tgt_drivers[lld_no]->show(req->mode, - req->tid, req->sid, - req->cid, req->lun, - &b); - if (!err) { - err = concat_buf_finish(&b); - err = errno2tgtadm(err); - } - else + adm_err = tgt_drivers[lld_no]->show(req->mode, + req->tid, req->sid, + req->cid, req->lun, + &b); + if (adm_err != TGTADM_SUCCESS) { concat_buf_finish(&b); + break; + } + adm_err = errno2tgtadm(concat_buf_finish(&b)); break; } break; default: if (tgt_drivers[lld_no]->update) - err = tgt_drivers[lld_no]->update(req->mode, req->op, - req->tid, - req->sid, req->lun, - req->cid, mtask->req_buf); + adm_err = tgt_drivers[lld_no]->update(req->mode, req->op, + req->tid, + req->sid, req->lun, + req->cid, mtask->req_buf); break; } - set_mtask_result(mtask, err); - return err; + set_mtask_result(mtask, adm_err); + return adm_err; } -static int mtask_execute(struct mgmt_task *mtask) +static tgtadm_err mtask_execute(struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; struct concat_buf b; int lld_no; - int err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; if (!strlen(req->lld)) lld_no = 0; @@ -413,42 +425,41 @@ static int mtask_execute(struct mgmt_task *mtask) switch (req->mode) { case MODE_SYSTEM: - err = sys_mgmt(lld_no, mtask); + adm_err = sys_mgmt(lld_no, mtask); break; case MODE_TARGET: - err = target_mgmt(lld_no, mtask); + adm_err = target_mgmt(lld_no, mtask); break; case MODE_PORTAL: - err = portal_mgmt(lld_no, mtask); + adm_err = portal_mgmt(lld_no, mtask); break; case MODE_DEVICE: - err = device_mgmt(lld_no, mtask); + adm_err = device_mgmt(lld_no, mtask); break; case MODE_ACCOUNT: - err = account_mgmt(lld_no, mtask); + adm_err = account_mgmt(lld_no, mtask); break; case MODE_CONNECTION: - err = connection_mgmt(lld_no, mtask); + adm_err = connection_mgmt(lld_no, mtask); break; default: if (req->op == OP_SHOW && tgt_drivers[lld_no]->show) { concat_buf_init(&b, &mtask->req_buf, &mtask->req_bsize); - err = tgt_drivers[lld_no]->show(req->mode, - req->tid, req->sid, - req->cid, req->lun, - &b); - if (!err) { - err = concat_buf_finish(&b); - err = errno2tgtadm(err); - } - else + adm_err = tgt_drivers[lld_no]->show(req->mode, + req->tid, req->sid, + req->cid, req->lun, + &b); + if (adm_err != TGTADM_SUCCESS) { concat_buf_finish(&b); + break; + } + adm_err = errno2tgtadm(concat_buf_finish(&b)); } - set_mtask_result(mtask, err); break; } - return err; + set_mtask_result(mtask, adm_err); + return adm_err; } static int ipc_accept(int accept_fd) diff --git a/usr/mmc.c b/usr/mmc.c index 3279c3d..e30b140 100644 --- a/usr/mmc.c +++ b/usr/mmc.c @@ -2193,14 +2193,14 @@ static int mmc_mode_sense(int host_no, struct scsi_cmd *cmd) return spc_mode_sense(host_no, cmd); } -static int mmc_lu_init(struct scsi_lu *lu) +static tgtadm_err mmc_lu_init(struct scsi_lu *lu) { struct backingstore_template *bst; struct mmc_info *mmc; mmc = zalloc(sizeof(struct mmc_info)); if (!mmc) - return -ENOMEM; + return TGTADM_NOMEM; lu->xxc_p = mmc; @@ -2265,7 +2265,7 @@ static int mmc_lu_init(struct scsi_lu *lu) "0x10:0:0x96:0:0:0:0:0:0:0:0:0:0:0:0:0:0:" "0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:" "0:0"); - return 0; + return TGTADM_SUCCESS; } static int mmc_lu_online(struct scsi_lu *lu) diff --git a/usr/osd.c b/usr/osd.c index 2ab29ac..984dcad 100644 --- a/usr/osd.c +++ b/usr/osd.c @@ -38,7 +38,7 @@ static int osd_varlen_cdb(int host_no, struct scsi_cmd *cmd) * XXX: missing support for b0 and b1, in page 0 and in inquiry code. * Figure out how to make spc_inquiry handle extra mode pages. */ -static int osd_lu_init(struct scsi_lu *lu) +static tgtadm_err osd_lu_init(struct scsi_lu *lu) { if (spc_lu_init(lu)) return TGTADM_NOMEM; @@ -49,7 +49,7 @@ static int osd_lu_init(struct scsi_lu *lu) lu->attrs.version_desc[1] = 0x0960; /* iSCSI */ lu->attrs.version_desc[2] = 0x0300; /* SPC-3 */ - return 0; + return TGTADM_SUCCESS; } static struct device_type_template osd_template = { diff --git a/usr/sbc.c b/usr/sbc.c index 9666801..092e1cc 100644 --- a/usr/sbc.c +++ b/usr/sbc.c @@ -292,7 +292,7 @@ sense: return SAM_STAT_CHECK_CONDITION; } -static int sbc_lu_init(struct scsi_lu *lu) +static tgtadm_err sbc_lu_init(struct scsi_lu *lu) { uint64_t size; uint8_t *data; @@ -335,7 +335,7 @@ static int sbc_lu_init(struct scsi_lu *lu) /* Informational Exceptions Control page */ add_mode_page(lu, "0x1c:0:10:8:0:0:0:0:0:0:0:0:0"); - return 0; + return TGTADM_SUCCESS; } static struct device_type_template sbc_template = { diff --git a/usr/scc.c b/usr/scc.c index 27bc07b..ec67ca8 100644 --- a/usr/scc.c +++ b/usr/scc.c @@ -36,7 +36,7 @@ #include "tgtadm_error.h" #include "spc.h" -static int scc_lu_init(struct scsi_lu *lu) +static tgtadm_err scc_lu_init(struct scsi_lu *lu) { if (spc_lu_init(lu)) return TGTADM_NOMEM; @@ -49,7 +49,7 @@ static int scc_lu_init(struct scsi_lu *lu) lu->dev_type_template.lu_online(lu); - return 0; + return TGTADM_SUCCESS; } static struct device_type_template scc_template = { diff --git a/usr/smc.c b/usr/smc.c index c0f25d6..23ee1e0 100644 --- a/usr/smc.c +++ b/usr/smc.c @@ -476,7 +476,7 @@ sense: return SAM_STAT_CHECK_CONDITION; } -static int smc_lu_init(struct scsi_lu *lu) +static tgtadm_err smc_lu_init(struct scsi_lu *lu) { struct smc_info *smc; @@ -484,7 +484,7 @@ static int smc_lu_init(struct scsi_lu *lu) if (smc) dtype_priv(lu) = smc; else - return -ENOMEM; + return TGTADM_NOMEM; if (spc_lu_init(lu)) return TGTADM_NOMEM; @@ -515,7 +515,7 @@ static int smc_lu_init(struct scsi_lu *lu) lu->dev_type_template.lu_online(lu); /* Library will now report as Online */ lu->attrs.removable = 1; /* Default to removable media */ - return 0; + return TGTADM_SUCCESS; } static void smc_lu_exit(struct scsi_lu *lu) @@ -592,10 +592,10 @@ static void slot_dump(struct list_head *head) } } -static int add_slt(struct scsi_lu *lu, struct tmp_param *tmp) +static tgtadm_err add_slt(struct scsi_lu *lu, struct tmp_param *tmp) { struct smc_info *smc = dtype_priv(lu); - int ret = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; struct mode_pg *pg; struct slot *s; uint16_t *element; @@ -641,22 +641,22 @@ static int add_slt(struct scsi_lu *lu, struct tmp_param *tmp) if (s) // Opps... Found a slot at this address.. goto dont_do_slots; - ret = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; for(i = tmp->start_addr; i < (tmp->start_addr + tmp->quantity); i++) if (slot_insert(&smc->slots, tmp->element_type, i)) - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; } dont_do_slots: - return ret; + return adm_err; } -static int config_slot(struct scsi_lu *lu, struct tmp_param *tmp) +static tgtadm_err config_slot(struct scsi_lu *lu, struct tmp_param *tmp) { struct smc_info *smc = dtype_priv(lu); struct mode_pg *m = NULL; struct slot *s = NULL; - int ret = TGTADM_INVALID_REQUEST; + int adm_err = TGTADM_INVALID_REQUEST; switch(tmp->element_type) { case ELEMENT_MEDIUM_TRANSPORT: @@ -664,7 +664,7 @@ static int config_slot(struct scsi_lu *lu, struct tmp_param *tmp) m = lu->mode_pgs[0x1e]; if (m) { m->mode_data[0] = (tmp->sides > 1) ? 1 : 0; - ret = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; } break; case ELEMENT_STORAGE: @@ -674,7 +674,7 @@ static int config_slot(struct scsi_lu *lu, struct tmp_param *tmp) break; // Slot not found.. strncpy(s->barcode, tmp->barcode, sizeof(s->barcode)); set_slot_full(s, 0, NULL); - ret = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; break; case ELEMENT_DATA_TRANSFER: if (!tmp->tid) @@ -685,23 +685,23 @@ static int config_slot(struct scsi_lu *lu, struct tmp_param *tmp) s->asc = NO_ADDITIONAL_SENSE; s->drive_tid = tmp->tid; s->drive_lun = tmp->lun; - ret = TGTADM_SUCCESS; + adm_err = TGTADM_SUCCESS; break; } - return ret; + return adm_err; } #define ADD 1 #define CONFIGURE 2 -static int __smc_lu_config(struct scsi_lu *lu, char *params) +static tgtadm_err __smc_lu_config(struct scsi_lu *lu, char *params) { struct smc_info *smc = dtype_priv(lu); - int err = TGTADM_SUCCESS; + tgtadm_err adm_err = TGTADM_SUCCESS; char *p; char buf[256]; - while ((p = strsep(¶ms, ",")) != NULL) { + while (adm_err == TGTADM_SUCCESS && (p = strsep(¶ms, ",")) != NULL) { substring_t args[MAX_OPT_ARGS]; int token; if (!*p) @@ -751,33 +751,35 @@ static int __smc_lu_config(struct scsi_lu *lu, char *params) match_strncpy(buf, &args[0], sizeof(buf)); smc->media_home = strdup(buf); if (!smc->media_home) - return TGTADM_NOMEM; + adm_err = TGTADM_NOMEM; break; default: - err = TGTADM_UNKNOWN_PARAM; + adm_err = TGTADM_UNKNOWN_PARAM; + break; } } - return err; + return adm_err; } -static int smc_lu_config(struct scsi_lu *lu, char *params) +static tgtadm_err smc_lu_config(struct scsi_lu *lu, char *params) { - int ret = TGTADM_SUCCESS; + tgtadm_err adm_err = TGTADM_SUCCESS; memset(&sv_param, 0, sizeof(struct tmp_param)); - if ((ret = lu_config(lu, params, __smc_lu_config))) - return TGTADM_UNKNOWN_PARAM; + adm_err = lu_config(lu, params, __smc_lu_config); + if (adm_err != TGTADM_SUCCESS) + return adm_err; switch(sv_param.operation) { case ADD: - ret = add_slt(lu, &sv_param); + adm_err = add_slt(lu, &sv_param); break; case CONFIGURE: - ret = config_slot(lu, &sv_param); + adm_err = config_slot(lu, &sv_param); break; } - return ret; + return adm_err; } struct device_type_template smc_template = { diff --git a/usr/spc.c b/usr/spc.c index eba4857..837f611 100644 --- a/usr/spc.c +++ b/usr/spc.c @@ -1509,9 +1509,10 @@ static struct mode_pg *alloc_mode_pg(uint8_t pcode, uint8_t subpcode, return pg; } -int add_mode_page(struct scsi_lu *lu, char *p) +tgtadm_err add_mode_page(struct scsi_lu *lu, char *p) { - int i, tmp, ret = TGTADM_SUCCESS; + tgtadm_err adm_err = TGTADM_SUCCESS; + int i, tmp; uint8_t pcode, subpcode, *data; uint16_t size; struct mode_pg *pg; @@ -1535,7 +1536,7 @@ int add_mode_page(struct scsi_lu *lu, char *p) pg = alloc_mode_pg(pcode, subpcode, size); if (!pg) { - ret = TGTADM_NOMEM; + adm_err = TGTADM_NOMEM; goto exit; } @@ -1560,12 +1561,12 @@ int add_mode_page(struct scsi_lu *lu, char *p) } if (i != size + 3) { - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; eprintf("Mode Page %d (0x%02x): param_count %d != " "MODE PAGE size : %d\n", pcode, subpcode, i, size + 3); } exit: - return ret; + return adm_err; } void dump_cdb(struct scsi_cmd *cmd) @@ -1653,9 +1654,9 @@ int spc_lu_offline(struct scsi_lu *lu) return 0; } -int lu_config(struct scsi_lu *lu, char *params, match_fn_t *fn) +tgtadm_err lu_config(struct scsi_lu *lu, char *params, match_fn_t *fn) { - int err = TGTADM_SUCCESS; + tgtadm_err adm_err = TGTADM_SUCCESS; char *p; char buf[1024]; struct lu_phy_attr *attrs; @@ -1734,20 +1735,20 @@ int lu_config(struct scsi_lu *lu, char *params, match_fn_t *fn) break; case Opt_mode_page: match_strncpy(buf, &args[0], sizeof(buf)); - err = add_mode_page(lu, buf); + adm_err = add_mode_page(lu, buf); break; case Opt_path: match_strncpy(buf, &args[0], sizeof(buf)); - err = tgt_device_path_update(lu->tgt, lu, buf); + adm_err = tgt_device_path_update(lu->tgt, lu, buf); break; default: - err |= fn ? fn(lu, p) : TGTADM_INVALID_REQUEST; + adm_err = fn ? fn(lu, p) : TGTADM_INVALID_REQUEST; } } - return err; + return adm_err; } -int spc_lu_config(struct scsi_lu *lu, char *params) +tgtadm_err spc_lu_config(struct scsi_lu *lu, char *params) { return lu_config(lu, params, NULL); } @@ -1773,18 +1774,24 @@ int spc_lu_init(struct scsi_lu *lu) /* VPD page 0x80 */ pg = PCODE_OFFSET(0x80); lu_vpd[pg] = alloc_vpd(SCSI_SN_LEN); + if (!lu_vpd[pg]) + return -ENOMEM; lu_vpd[pg]->vpd_update = update_vpd_80; lu_vpd[pg]->vpd_update(lu, lu->attrs.scsi_sn); /* VPD page 0x83 */ pg = PCODE_OFFSET(0x83); lu_vpd[pg] = alloc_vpd(SCSI_ID_LEN + 4); + if (!lu_vpd[pg]) + return -ENOMEM; lu_vpd[pg]->vpd_update = update_vpd_83; lu_vpd[pg]->vpd_update(lu, lu->attrs.scsi_id); /* VPD page 0xb0 */ pg = PCODE_OFFSET(0xb0); lu_vpd[pg] = alloc_vpd(BLOCK_LIMITS_VPD_LEN); + if (!lu_vpd[pg]) + return -ENOMEM; lu->attrs.removable = 0; lu->attrs.readonly = 0; diff --git a/usr/spc.h b/usr/spc.h index 430a882..42d7a24 100644 --- a/usr/spc.h +++ b/usr/spc.h @@ -14,13 +14,13 @@ extern int spc_prevent_allow_media_removal(int host_no, struct scsi_cmd *cmd); extern int spc_illegal_op(int host_no, struct scsi_cmd *cmd); extern int spc_lu_init(struct scsi_lu *lu); -typedef int (match_fn_t)(struct scsi_lu *lu, char *params); -extern int lu_config(struct scsi_lu *lu, char *params, match_fn_t *); -extern int spc_lu_config(struct scsi_lu *lu, char *params); +typedef tgtadm_err (match_fn_t)(struct scsi_lu *lu, char *params); +extern tgtadm_err lu_config(struct scsi_lu *lu, char *params, match_fn_t *); +extern tgtadm_err spc_lu_config(struct scsi_lu *lu, char *params); extern void spc_lu_exit(struct scsi_lu *lu); extern void dump_cdb(struct scsi_cmd *cmd); extern int spc_mode_sense(int host_no, struct scsi_cmd *cmd); -extern int add_mode_page(struct scsi_lu *lu, char *params); +extern tgtadm_err add_mode_page(struct scsi_lu *lu, char *params); extern int set_mode_page_changeable_mask(struct scsi_lu *lu, uint8_t pcode, uint8_t *mask); extern int spc_mode_select(int host_no, struct scsi_cmd *cmd, diff --git a/usr/ssc.c b/usr/ssc.c index 94beda1..75fe589 100644 --- a/usr/ssc.c +++ b/usr/ssc.c @@ -105,16 +105,15 @@ static int ssc_read_block_limit(int host_no, struct scsi_cmd *cmd) return SAM_STAT_GOOD; } -static int ssc_lu_init(struct scsi_lu *lu) +static tgtadm_err ssc_lu_init(struct scsi_lu *lu) { uint8_t *data; struct ssc_info *ssc; ssc = zalloc(sizeof(struct ssc_info)); - if (ssc) - dtype_priv(lu) = ssc; - else + if (!ssc) return TGTADM_NOMEM; + dtype_priv(lu) = ssc; if (spc_lu_init(lu)) return TGTADM_NOMEM; @@ -154,7 +153,7 @@ static int ssc_lu_init(struct scsi_lu *lu) /* Medium Configuration - Mandatory - SSC3 8.3.7 */ add_mode_page(lu, "0x1d:0:0x1e:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0" ":0:0:0:0:0:0:0:0:0:0:0:0:0"); - return 0; + return TGTADM_SUCCESS; } static struct device_type_template ssc_template = { diff --git a/usr/target.c b/usr/target.c index 5d7aab1..04febc3 100644 --- a/usr/target.c +++ b/usr/target.c @@ -369,10 +369,11 @@ static void tgt_cmd_queue_init(struct tgt_cmd_queue *q) INIT_LIST_HEAD(&q->queue); } -int tgt_device_path_update(struct target *target, struct scsi_lu *lu, char *path) +tgtadm_err tgt_device_path_update(struct target *target, struct scsi_lu *lu, char *path) { - int err, dev_fd; + int dev_fd; uint64_t size; + int err; if (lu->path) { if (lu->attrs.online) @@ -403,7 +404,7 @@ int tgt_device_path_update(struct target *target, struct scsi_lu *lu, char *path lu->path = path; lu->dev_type_template.lu_online(lu); - return 0; + return TGTADM_SUCCESS; } static struct scsi_lu * @@ -438,12 +439,13 @@ static match_table_t device_tokens = { static void __cmd_done(struct target *, struct scsi_cmd *); -int tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, +tgtadm_err tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, int backing) { char *p, *path = NULL, *bstype = NULL; char *bsoflags = NULL, *blocksize = NULL; - int ret = 0, lu_bsoflags = 0; + int lu_bsoflags = 0; + tgtadm_err adm_err = TGTADM_SUCCESS; struct target *target; struct scsi_lu *lu, *pos; struct device_type_template *t; @@ -480,14 +482,14 @@ int tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, target = target_lookup(tid); if (!target) { - ret = TGTADM_NO_TARGET; + adm_err = TGTADM_NO_TARGET; goto out; } lu = device_lookup(target, lun); if (lu) { eprintf("device %" PRIu64 " already exists\n", lun); - ret = TGTADM_LUN_EXIST; + adm_err = TGTADM_LUN_EXIST; goto out; } @@ -497,7 +499,7 @@ int tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, bst = get_backingstore_template(bstype); if (!bst) { eprintf("failed to find bstype, %s\n", bstype); - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; goto out; } } @@ -507,14 +509,14 @@ int tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, if ((!strncmp(bst->bs_name, "bsg", 3) || !strncmp(bst->bs_name, "sg", 2)) && dev_type != TYPE_PT) { - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; goto out; } if (bsoflags) { lu_bsoflags = str_to_open_flags(bsoflags); if (lu_bsoflags == -1) { - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; goto out; } } @@ -524,20 +526,20 @@ int tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, open_flags_to_str(strflags, (bst->bs_oflags_supported & lu_bsoflags) ^ lu_bsoflags), bst->bs_name); - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; goto out; } t = device_type_lookup(dev_type); if (!t) { eprintf("Unknown device type %d\n", dev_type); - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; goto out; } lu = zalloc(sizeof(*lu) + bst->bs_datasize); if (!lu) { - ret = TGTADM_NOMEM; + adm_err = TGTADM_NOMEM; goto out; } @@ -575,25 +577,25 @@ int tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, } if (lu->dev_type_template.lu_init) { - ret = lu->dev_type_template.lu_init(lu); - if (ret) + adm_err = lu->dev_type_template.lu_init(lu); + if (adm_err) goto fail_lu_init; } if (lu->bst->bs_init) { - ret = lu->bst->bs_init(lu); - if (ret) + adm_err = lu->bst->bs_init(lu); + if (adm_err) goto fail_lu_init; } if (backing && !path && !lu->attrs.removable) { - ret = TGTADM_INVALID_REQUEST; + adm_err = TGTADM_INVALID_REQUEST; goto fail_bs_init; } if (backing && path) { - ret = tgt_device_path_update(target, lu, path); - if (ret) + adm_err = tgt_device_path_update(target, lu, path); + if (adm_err) goto fail_bs_init; } @@ -621,8 +623,10 @@ int tgt_device_create(int tid, int dev_type, uint64_t lun, char *params, list_for_each_entry(itn_lu, &itn->it_nexus_lu_info_list, lu_info_siblings) { - ret = ua_sense_add(itn_lu, - ASC_REPORTED_LUNS_DATA_HAS_CHANGED); + if (ua_sense_add(itn_lu, ASC_REPORTED_LUNS_DATA_HAS_CHANGED)) { + adm_err = TGTADM_NOMEM; + goto fail_bs_init; + } } } @@ -639,7 +643,8 @@ out: free(path); if (bsoflags) free(bsoflags); - return ret; + return adm_err; + fail_bs_init: if (lu->bst->bs_exit) lu->bst->bs_exit(lu); @@ -648,7 +653,7 @@ fail_lu_init: goto out; } -int tgt_device_destroy(int tid, uint64_t lun, int force) +tgtadm_err tgt_device_destroy(int tid, uint64_t lun, int force) { struct target *target; struct scsi_lu *lu; @@ -711,7 +716,7 @@ int tgt_device_destroy(int tid, uint64_t lun, int force) } } - return 0; + return TGTADM_SUCCESS; } struct lu_phy_attr *lu_attr_lookup(int tid, uint64_t lun) @@ -822,9 +827,9 @@ int device_reserved(struct scsi_cmd *cmd) return -EBUSY; } -int tgt_device_update(int tid, uint64_t dev_id, char *params) +tgtadm_err tgt_device_update(int tid, uint64_t dev_id, char *params) { - int err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; struct target *target; struct scsi_lu *lu; @@ -839,9 +844,9 @@ int tgt_device_update(int tid, uint64_t dev_id, char *params) } if (lu->dev_type_template.lu_config) - err = lu->dev_type_template.lu_config(lu, params); + adm_err = lu->dev_type_template.lu_config(lu, params); - return err; + return adm_err; } static int cmd_enabled(struct tgt_cmd_queue *q, struct scsi_cmd *cmd) @@ -1295,7 +1300,7 @@ found: return 0; } -int account_add(char *user, char *password) +tgtadm_err account_add(char *user, char *password) { int aid; struct account_entry *ac; @@ -1331,7 +1336,7 @@ free_account: return TGTADM_NOMEM; } -static int __inaccount_bind(struct target *target, int aid) +static tgtadm_err __inaccount_bind(struct target *target, int aid) { int i; @@ -1367,14 +1372,15 @@ static int __inaccount_bind(struct target *target, int aid) target->account.max_inaccount = new_max; } - return 0; + return TGTADM_SUCCESS; } -int account_ctl(int tid, int type, char *user, int bind) +tgtadm_err account_ctl(int tid, int type, char *user, int bind) { + tgtadm_err adm_err = 0; struct target *target; struct account_entry *ac; - int i, err = 0; + int i; if (tid == GLOBAL_TID) target = &global_target; @@ -1389,10 +1395,10 @@ int account_ctl(int tid, int type, char *user, int bind) if (bind) { if (type == ACCOUNT_TYPE_INCOMING) - err = __inaccount_bind(target, ac->aid); + adm_err = __inaccount_bind(target, ac->aid); else { if (target->account.out_aid) - err = TGTADM_OUTACCOUNT_EXIST; + adm_err = TGTADM_OUTACCOUNT_EXIST; else target->account.out_aid = ac->aid; } @@ -1406,17 +1412,17 @@ int account_ctl(int tid, int type, char *user, int bind) } if (i == target->account.max_inaccount) - err = TGTADM_NO_USER; + adm_err = TGTADM_NO_USER; } else if (target->account.out_aid == ac->aid) target->account.out_aid = 0; else - err = TGTADM_NO_USER; + adm_err = TGTADM_NO_USER; - return err; + return adm_err; } -int account_del(char *user) +tgtadm_err account_del(char *user) { struct account_entry *ac; struct target *target; @@ -1437,7 +1443,7 @@ int account_del(char *user) free(ac->user); free(ac->password); free(ac); - return 0; + return TGTADM_SUCCESS; } int account_available(int tid, int dir) @@ -1457,7 +1463,7 @@ int account_available(int tid, int dir) return target->account.out_aid; } -int acl_add(int tid, char *address) +tgtadm_err acl_add(int tid, char *address) { char *str; struct target *target; @@ -1484,14 +1490,14 @@ int acl_add(int tid, char *address) acl->address = str; list_add_tail(&acl->aclent_list, &target->acl_list); - return 0; + return TGTADM_SUCCESS; } -int acl_del(int tid, char *address) +tgtadm_err acl_del(int tid, char *address) { struct target *target; struct acl_entry *acl, *tmp; - int err = TGTADM_ACL_NOEXIST; + tgtadm_err adm_err = TGTADM_ACL_NOEXIST; target = target_lookup(tid); if (!target) @@ -1502,11 +1508,11 @@ int acl_del(int tid, char *address) list_del(&acl->aclent_list); free(acl->address); free(acl); - err = 0; + adm_err = TGTADM_SUCCESS; break; } } - return err; + return adm_err; } char *acl_get(int tid, int idx) @@ -1609,7 +1615,7 @@ struct bound_host { struct list_head bhost_siblings; }; -int tgt_bind_host_to_target(int tid, int host_no) +tgtadm_err tgt_bind_host_to_target(int tid, int host_no) { struct target *target; struct bound_host *bhost; @@ -1617,19 +1623,19 @@ int tgt_bind_host_to_target(int tid, int host_no) target = target_lookup(tid); if (!target) { eprintf("can't find a target %d\n", tid); - return -ENOENT; + return TGTADM_NO_TARGET; } list_for_each_entry(bhost, &bound_host_list, bhost_siblings) { if (bhost->host_no == host_no) { eprintf("already bound %d\n", host_no); - return -EINVAL; + return TGTADM_BINDING_EXIST; } } bhost = malloc(sizeof(*bhost)); if (!bhost) - return -ENOMEM; + return TGTADM_NOMEM; bhost->host_no = host_no; bhost->target = target; @@ -1638,10 +1644,10 @@ int tgt_bind_host_to_target(int tid, int host_no) dprintf("bound the scsi host %d to the target %d\n", host_no, tid); - return 0; + return TGTADM_SUCCESS; } -int tgt_unbind_host_to_target(int tid, int host_no) +tgtadm_err tgt_unbind_host_to_target(int tid, int host_no) { struct bound_host *bhost; @@ -1649,14 +1655,14 @@ int tgt_unbind_host_to_target(int tid, int host_no) if (bhost->host_no == host_no) { if (!list_empty(&bhost->target->it_nexus_list)) { eprintf("the target has IT_nexus\n"); - return -EBUSY; + return TGTADM_TARGET_ACTIVE; } list_del(&bhost->bhost_siblings); free(bhost); - return 0; + return TGTADM_SUCCESS; } } - return -ENOENT; + return TGTADM_NO_BINDING; } enum scsi_target_state tgt_get_target_state(int tid) @@ -1691,10 +1697,11 @@ static char *target_state_name(enum scsi_target_state state) return name; } -int tgt_set_target_state(int tid, char *str) +tgtadm_err tgt_set_target_state(int tid, char *str) { - int i, err = TGTADM_INVALID_REQUEST; + tgtadm_err adm_err = TGTADM_INVALID_REQUEST; struct target *target; + int i; target = target_lookup(tid); if (!target) @@ -1703,12 +1710,12 @@ int tgt_set_target_state(int tid, char *str) for (i = 0; i < ARRAY_SIZE(target_state); i++) { if (!strcmp(target_state[i].name, str)) { target->target_state = target_state[i].value; - err = 0; + adm_err = TGTADM_SUCCESS; break; } } - return err; + return adm_err; } static char *print_disksize(uint64_t size) @@ -1758,7 +1765,7 @@ static char *print_type(int type) return name; } -int tgt_target_show_all(struct concat_buf *b) +void tgt_target_show_all(struct concat_buf *b) { char strflags[128]; struct target *target; @@ -1840,7 +1847,6 @@ int tgt_target_show_all(struct concat_buf *b) concat_printf(b, _TAB2 "%s\n", iqn_acl->name); } - return TGTADM_SUCCESS; } char *tgt_targetname(int tid) @@ -1856,7 +1862,7 @@ char *tgt_targetname(int tid) #define DEFAULT_NR_ACCOUNT 16 -int tgt_target_create(int lld, int tid, char *args) +tgtadm_err tgt_target_create(int lld, int tid, char *args) { struct target *target, *pos; char *p, *q, *targetname = NULL; @@ -1939,16 +1945,16 @@ int tgt_target_create(int lld, int tid, char *args) dprintf("Succeed to create a new target %d\n", tid); - return 0; + return TGTADM_SUCCESS; } -int tgt_target_destroy(int lld_no, int tid, int force) +tgtadm_err tgt_target_destroy(int lld_no, int tid, int force) { - int ret; struct target *target; struct acl_entry *acl, *tmp; struct iqn_acl_entry *iqn_acl, *tmp1; struct scsi_lu *lu; + tgtadm_err adm_err; target = target_lookup(tid); if (!target) @@ -1963,9 +1969,9 @@ int tgt_target_destroy(int lld_no, int tid, int force) /* we remove lun0 last */ lu = list_entry(target->device_list.prev, struct scsi_lu, device_siblings); - ret = tgt_device_destroy(tid, lu->lun, 1); - if (ret) - return ret; + adm_err = tgt_device_destroy(tid, lu->lun, 1); + if (adm_err != TGTADM_SUCCESS) + return adm_err; } if (tgt_drivers[lld_no]->target_destroy) @@ -1989,10 +1995,10 @@ int tgt_target_destroy(int lld_no, int tid, int force) free(target->name); free(target); - return 0; + return TGTADM_SUCCESS; } -int tgt_portal_create(int lld, char *args) +tgtadm_err tgt_portal_create(int lld, char *args) { char *portals = NULL; @@ -2014,10 +2020,10 @@ int tgt_portal_create(int lld, char *args) dprintf("succeed to create new portals %s\n", portals); - return 0; + return TGTADM_SUCCESS; } -int tgt_portal_destroy(int lld, char *args) +tgtadm_err tgt_portal_destroy(int lld, char *args) { char *portals = NULL; @@ -2039,10 +2045,10 @@ int tgt_portal_destroy(int lld, char *args) dprintf("succeed to destroy portals %s\n", portals); - return 0; + return TGTADM_SUCCESS; } -int account_show(struct concat_buf *b) +void account_show(struct concat_buf *b) { struct account_entry *ac; @@ -2051,8 +2057,6 @@ int account_show(struct concat_buf *b) list_for_each_entry(ac, &account_list, account_siblings) concat_printf(b, _TAB1 "%s\n", ac->user); - - return TGTADM_SUCCESS; } static struct { @@ -2093,7 +2097,7 @@ int system_set_state(char *str) return err; } -int system_show(int mode, struct concat_buf *b) +void system_show(int mode, struct concat_buf *b) { struct backingstore_template *bst; struct device_type_template *devt; @@ -2102,7 +2106,7 @@ int system_show(int mode, struct concat_buf *b) /* FIXME: too hacky */ if (mode != MODE_SYSTEM) - return 0; + return; concat_printf(b, "System:\n"); concat_printf(b, _TAB1 "State: %s\n", system_state_name(sys_state)); @@ -2141,8 +2145,6 @@ int system_show(int mode, struct concat_buf *b) __account_lookup_id(aid)->user); } } - - return TGTADM_SUCCESS; } int is_system_available(void) diff --git a/usr/tgtadm.c b/usr/tgtadm.c index c786c15..8771b5d 100644 --- a/usr/tgtadm.c +++ b/usr/tgtadm.c @@ -63,7 +63,9 @@ static const char * tgtadm_strerror(int err) { TGTADM_NO_LUN, "can't find the logical unit" }, { TGTADM_NO_SESSION, "can't find the session" }, { TGTADM_NO_CONNECTION, "can't find the connection" }, + { TGTADM_NO_BINDING, "can't find the binding" }, { TGTADM_TARGET_EXIST, "this target already exists" }, + { TGTADM_BINDING_EXIST, "this binding already exists" }, { TGTADM_LUN_EXIST, "this logical unit number already exists" }, { TGTADM_ACL_EXIST, "this access control rule already exists" }, { TGTADM_ACL_NOEXIST, "this access control rule does not exist" }, diff --git a/usr/tgtadm_error.h b/usr/tgtadm_error.h index 4cd8f81..a1f38b0 100644 --- a/usr/tgtadm_error.h +++ b/usr/tgtadm_error.h @@ -11,8 +11,10 @@ enum tgtadm_errno { TGTADM_NO_LUN, TGTADM_NO_SESSION, TGTADM_NO_CONNECTION, + TGTADM_NO_BINDING, TGTADM_TARGET_EXIST, TGTADM_LUN_EXIST, + TGTADM_BINDING_EXIST, TGTADM_ACL_EXIST, TGTADM_ACL_NOEXIST, @@ -28,4 +30,6 @@ enum tgtadm_errno { TGTADM_UNKNOWN_PARAM, }; +typedef enum tgtadm_errno tgtadm_err; + #endif diff --git a/usr/tgtd.h b/usr/tgtd.h index c60957e..05af181 100644 --- a/usr/tgtd.h +++ b/usr/tgtd.h @@ -3,6 +3,7 @@ #include "log.h" #include "scsi_cmnd.h" +#include "tgtadm_error.h" struct concat_buf; @@ -110,9 +111,9 @@ struct device_type_operations { struct device_type_template { unsigned char type; - int (*lu_init)(struct scsi_lu *lu); + tgtadm_err (*lu_init)(struct scsi_lu *lu); void (*lu_exit)(struct scsi_lu *lu); - int (*lu_config)(struct scsi_lu *lu, char *args); + tgtadm_err (*lu_config)(struct scsi_lu *lu, char *args); int (*lu_online)(struct scsi_lu *lu); int (*lu_offline)(struct scsi_lu *lu); int (*cmd_passthrough)(int, struct scsi_cmd *); @@ -127,7 +128,7 @@ struct backingstore_template { int bs_datasize; int (*bs_open)(struct scsi_lu *dev, char *path, int *fd, uint64_t *size); void (*bs_close)(struct scsi_lu *dev); - int (*bs_init)(struct scsi_lu *dev); + tgtadm_err (*bs_init)(struct scsi_lu *dev); void (*bs_exit)(struct scsi_lu *dev); int (*bs_cmd_submit)(struct scsi_cmd *cmd); int bs_oflags_supported; @@ -221,28 +222,28 @@ extern struct list_head bst_list; extern int ipc_init(void); extern void ipc_exit(void); -extern int tgt_device_create(int tid, int dev_type, uint64_t lun, char *args, int backing); -extern int tgt_device_destroy(int tid, uint64_t lun, int force); -extern int tgt_device_update(int tid, uint64_t dev_id, char *name); +extern tgtadm_err tgt_device_create(int tid, int dev_type, uint64_t lun, char *args, int backing); +extern tgtadm_err tgt_device_destroy(int tid, uint64_t lun, int force); +extern tgtadm_err tgt_device_update(int tid, uint64_t dev_id, char *name); extern int device_reserve(struct scsi_cmd *cmd); extern int device_release(int tid, uint64_t itn_id, uint64_t lun, int force); extern int device_reserved(struct scsi_cmd *cmd); -extern int tgt_device_path_update(struct target *target, struct scsi_lu *lu, char *path); +extern tgtadm_err tgt_device_path_update(struct target *target, struct scsi_lu *lu, char *path); -extern int tgt_target_create(int lld, int tid, char *args); -extern int tgt_target_destroy(int lld, int tid, int force); +extern tgtadm_err tgt_target_create(int lld, int tid, char *args); +extern tgtadm_err tgt_target_destroy(int lld, int tid, int force); extern char *tgt_targetname(int tid); -extern int tgt_target_show_all(struct concat_buf *b); +extern void tgt_target_show_all(struct concat_buf *b); int system_set_state(char *str); -int system_show(int mode, struct concat_buf *b); +void system_show(int mode, struct concat_buf *b); int is_system_available(void); int is_system_inactive(void); -extern int tgt_portal_create(int lld, char *args); -extern int tgt_portal_destroy(int lld, char *args); +extern tgtadm_err tgt_portal_create(int lld, char *args); +extern tgtadm_err tgt_portal_destroy(int lld, char *args); -extern int tgt_bind_host_to_target(int tid, int host_no); -extern int tgt_unbind_host_to_target(int tid, int host_no); +extern tgtadm_err tgt_bind_host_to_target(int tid, int host_no); +extern tgtadm_err tgt_unbind_host_to_target(int tid, int host_no); struct event_data; typedef void (*sched_event_handler_t)(struct event_data *tev); @@ -289,10 +290,10 @@ extern int get_scsi_cdb_size(struct scsi_cmd *cmd); extern int get_scsi_command_size(unsigned char op); extern enum scsi_target_state tgt_get_target_state(int tid); -extern int tgt_set_target_state(int tid, char *str); +extern tgtadm_err tgt_set_target_state(int tid, char *str); -extern int acl_add(int tid, char *address); -extern int acl_del(int tid, char *address); +extern tgtadm_err acl_add(int tid, char *address); +extern tgtadm_err acl_del(int tid, char *address); extern char *acl_get(int tid, int idx); extern int iqn_acl_add(int tid, char *name); @@ -300,10 +301,10 @@ extern int iqn_acl_del(int tid, char *name); extern char *iqn_acl_get(int tid, int idx); extern int account_lookup(int tid, int type, char *user, int ulen, char *password, int plen); -extern int account_add(char *user, char *password); -extern int account_del(char *user); -extern int account_ctl(int tid, int type, char *user, int bind); -extern int account_show(struct concat_buf *b); +extern tgtadm_err account_add(char *user, char *password); +extern tgtadm_err account_del(char *user); +extern tgtadm_err account_ctl(int tid, int type, char *user, int bind); +extern void account_show(struct concat_buf *b); extern int account_available(int tid, int dir); extern int it_nexus_create(int tid, uint64_t itn_id, int host_no, char *info); -- 1.7.3.2 -- To unsubscribe from this list: send the line "unsubscribe stgt" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html