2 * Copyright (C) 2002 - 2003 Ardis Technolgies <roman@ardistech.com>
3 * Copyright (C) 2007 - 2009 Vladislav Bolkhovitin
4 * Copyright (C) 2007 - 2009 ID7 Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, version 2
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
27 struct __qelem targets_list = LIST_HEAD_INIT(targets_list);
29 void target_list_build(struct connection *conn, char *addr, char *name)
31 struct target *target;
33 list_for_each_entry(target, &targets_list, tlist) {
34 if (name && strcmp(target->name, name))
36 if (config_initiator_access(target->tid, conn->fd) ||
37 isns_scn_access(target->tid, conn->fd, conn->initiator))
40 text_key_add(conn, "TargetName", target->name);
41 text_key_add(conn, "TargetAddress", addr);
45 u32 target_find_id_by_name(const char *name)
47 struct target *target;
49 list_for_each_entry(target, &targets_list, tlist) {
50 if (!strcasecmp(target->name, name))
57 struct target *target_find_by_name(const char *name)
59 struct target *target;
61 list_for_each_entry(target, &targets_list, tlist) {
62 if (!strcasecmp(target->name, name))
69 struct target *target_find_by_id(u32 tid)
71 struct target *target;
73 list_for_each_entry(target, &targets_list, tlist) {
74 if (target->tid == tid)
81 static void all_accounts_del(u32 tid, int dir)
83 char name[ISCSI_NAME_LEN], pass[ISCSI_NAME_LEN];
85 memset(name, 0, sizeof(name));
87 for (; config_account_query(tid, dir, name, pass) != -ENOENT;
88 memset(name, 0, sizeof(name))) {
89 config_account_del(tid, dir, name);
94 int target_del(u32 tid)
96 struct target *target = target_find_by_id(tid);
97 int err = kernel_target_destroy(tid);
99 if (err < 0 && err != -ENOENT)
101 else if (!err && !target)
102 /* A leftover kernel object was cleaned up - don't complain. */
108 remque(&target->tlist);
110 if (!list_empty(&target->sessions_list)) {
111 log_error("%s: target %u still has sessions\n", __FUNCTION__,
116 all_accounts_del(tid, AUTH_DIR_INCOMING);
117 all_accounts_del(tid, AUTH_DIR_OUTGOING);
119 isns_target_deregister(target->name);
125 int target_add(u32 *tid, char *name)
127 struct target *target;
129 struct iscsi_param tgt_params[target_key_last];
130 struct iscsi_param sess_params[session_key_last];
135 if (!(target = malloc(sizeof(*target))))
138 memset(target, 0, sizeof(*target));
139 memcpy(target->name, name, sizeof(target->name) - 1);
141 if ((err = kernel_target_create(tid, name)) < 0)
144 param_set_defaults(tgt_params, target_keys);
145 err = kernel_param_set(*tid, 0, key_target, 0, tgt_params);
149 param_set_defaults(sess_params, session_keys);
150 err = kernel_param_set(*tid, 0, key_session, 0, sess_params);
154 INIT_LIST_HEAD(&target->tlist);
155 INIT_LIST_HEAD(&target->sessions_list);
156 INIT_LIST_HEAD(&target->isns_head);
158 insque(&target->tlist, &targets_list);
160 isns_target_register(name);
166 kernel_target_destroy(*tid);