static tgtadm_err target_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; tgtadm_err adm_err = TGTADM_INVALID_REQUEST; switch (req->op) { case OP_NEW: adm_err = tgt_target_create(lld_no, req->tid, mtask->req_buf); break; case OP_DELETE: adm_err = tgt_target_destroy(lld_no, req->tid, req->force); break; case OP_BIND: /* FIXME */ if (req->len == sizeof(*req)) adm_err = tgt_bind_host_to_target(req->tid, req->host_no); else { char *p; p = strstr(mtask->req_buf, "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) { 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)) adm_err = tgt_unbind_host_to_target(req->tid, req->host_no); else { char *p; p = strstr(mtask->req_buf, "initiator-address="); if (p) { 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) { p += strlen("initiator-name="); adm_err = iqn_acl_del(req->tid, p); if (adm_err != TGTADM_SUCCESS) { eprintf("Failed to unbind by name: %s\n", p); break; } } } break; case OP_UPDATE: { char *p; adm_err = TGTADM_UNSUPPORTED_OPERATION; p = strchr(mtask->req_buf, '='); if (!p) break; *p++ = '\0'; 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; } case OP_SHOW: { concat_buf_init(&mtask->rsp_concat); if (req->tid < 0) adm_err = tgt_target_show_all(&mtask->rsp_concat); else if (tgt_drivers[lld_no]->show) adm_err = tgt_drivers[lld_no]->show(req->mode, req->tid, req->sid, req->cid, req->lun, &mtask->rsp_concat); concat_buf_finish(&mtask->rsp_concat); break; } case OP_STATS: { concat_buf_init(&mtask->rsp_concat); adm_err = tgt_stat_target_by_id(req->tid, &mtask->rsp_concat); concat_buf_finish(&mtask->rsp_concat); break; } default: break; } return adm_err; }
static int target_mgmt(int lld_no, struct mgmt_task *mtask) { struct tgtadm_req *req = &mtask->req; struct tgtadm_rsp *rsp = &mtask->rsp; int err = TGTADM_INVALID_REQUEST; switch (req->op) { case OP_NEW: err = tgt_target_create(lld_no, req->tid, mtask->buf); break; case OP_DELETE: 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); else { char *p; p = strstr(mtask->buf, "initiator-address="); if (p) err = acl_add(req->tid, p + strlen("initiator-address=")); p = strstr(mtask->buf, "initiator-name="); if (p) err = iqn_acl_add(req->tid, p + strlen("initiator-name=")); } break; case OP_UNBIND: if (req->len == sizeof(*req)) err = tgt_unbind_host_to_target(req->tid, req->host_no); else { char *p; p = strstr(mtask->buf, "initiator-address="); if (p) { err = acl_del(req->tid, p + strlen("initiator-address=")); } p = strstr(mtask->buf, "initiator-name="); if (p) { err = iqn_acl_del(req->tid, p + strlen("initiator-name=")); } } break; case OP_UPDATE: { char *p; err = TGTADM_UNSUPPORTED_OPERATION; p = strchr(mtask->buf, '='); if (!p) break; *p++ = '\0'; if (!strcmp(mtask->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, req->sid, req->lun, req->cid, mtask->buf); break; } case OP_SHOW: if (req->tid < 0) { retry: err = tgt_target_show_all(mtask->buf, mtask->bsize); if (err == mtask->bsize) { char *p; mtask->bsize <<= 1; p = realloc(mtask->buf, mtask->bsize); if (p) { mtask->buf = p; goto retry; } else { eprintf("out of memory\n"); err = TGTADM_NOMEM; } } } else if (tgt_drivers[lld_no]->show) err = tgt_drivers[lld_no]->show(req->mode, req->tid, req->sid, req->cid, req->lun, mtask->buf, mtask->bsize); break; default: break; } if (req->op == OP_SHOW) set_show_results(rsp, &err); else { rsp->err = err; rsp->len = sizeof(*rsp); } return err; }