A bunch of minor fixes and important cleanups. Particularly:
[mirror/scst/.git] / iscsi-scst / kernel / session.c
1 /*
2  *  Copyright (C) 2002 - 2003 Ardis Technolgies <roman@ardistech.com>
3  *  Copyright (C) 2007 - 2008 Vladislav Bolkhovitin
4  *  Copyright (C) 2007 - 2008 CMS Distribution Limited
5  *
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
9  *  of the License.
10  *
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.
15  */
16
17 #include "iscsi.h"
18
19 /* target_mutex supposed to be locked */
20 struct iscsi_session *session_lookup(struct iscsi_target *target, u64 sid)
21 {
22         struct iscsi_session *session;
23
24         list_for_each_entry(session, &target->session_list,
25                         session_list_entry) {
26                 if (session->sid == sid)
27                         return session;
28         }
29         return NULL;
30 }
31
32 /* target_mgmt_mutex supposed to be locked */
33 static int iscsi_session_alloc(struct iscsi_target *target,
34         struct iscsi_kern_session_info *info, struct iscsi_session **result)
35 {
36         int err;
37         unsigned int i;
38         struct iscsi_session *session;
39         char *name = NULL;
40
41         session = kzalloc(sizeof(*session), GFP_KERNEL);
42         if (!session)
43                 return -ENOMEM;
44
45         session->target = target;
46         session->sid = info->sid;
47         session->sess_param = target->trgt_sess_param;
48         session->max_queued_cmnds = target->trgt_param.queued_cmnds;
49         atomic_set(&session->active_cmds, 0);
50
51         session->exp_cmd_sn = info->exp_cmd_sn;
52
53         session->initiator_name = kstrdup(info->initiator_name, GFP_KERNEL);
54         if (!session->initiator_name) {
55                 err = -ENOMEM;
56                 goto err;
57         }
58
59         name = kmalloc(strlen(info->user_name) + strlen(info->initiator_name) +
60                         1, GFP_KERNEL);
61         if (name == NULL) {
62                 err = -ENOMEM;
63                 goto err;
64         }
65
66         if (info->user_name[0] != '\0')
67                 sprintf(name, "%s@%s", info->user_name, info->initiator_name);
68         else
69                 sprintf(name, "%s", info->initiator_name);
70
71         INIT_LIST_HEAD(&session->conn_list);
72         INIT_LIST_HEAD(&session->pending_list);
73
74         spin_lock_init(&session->sn_lock);
75
76         spin_lock_init(&session->cmnd_hash_lock);
77         for (i = 0; i < ARRAY_SIZE(session->cmnd_hash); i++)
78                 INIT_LIST_HEAD(&session->cmnd_hash[i]);
79
80         session->next_ttt = 1;
81
82         session->scst_sess = scst_register_session(target->scst_tgt, 0,
83                 name, NULL, NULL);
84         if (session->scst_sess == NULL) {
85                 PRINT_ERROR("%s", "scst_register_session() failed");
86                 err = -ENOMEM;
87                 goto err;
88         }
89
90         kfree(name);
91
92         scst_sess_set_tgt_priv(session->scst_sess, session);
93
94         TRACE_MGMT_DBG("Session %p created: target %p, tid %u, sid %#Lx",
95                 session, target, target->tid, info->sid);
96
97         *result = session;
98         return 0;
99
100 err:
101         if (session) {
102                 kfree(session->initiator_name);
103                 kfree(session);
104                 kfree(name);
105         }
106         return err;
107 }
108
109 /* target_mutex supposed to be locked */
110 void sess_enable_reinstated_sess(struct iscsi_session *sess)
111 {
112         struct iscsi_conn *c;
113
114         TRACE_ENTRY();
115
116         TRACE_MGMT_DBG("Enabling reinstate successor sess %p", sess);
117
118         sBUG_ON(!sess->sess_reinstating);
119
120         list_for_each_entry(c, &sess->conn_list, conn_list_entry) {
121                 __iscsi_socket_bind(c);
122         }
123         sess->sess_reinstating = 0;
124
125         TRACE_EXIT();
126         return;
127 }
128
129 /* target_mgmt_mutex supposed to be locked */
130 int session_add(struct iscsi_target *target,
131         struct iscsi_kern_session_info *info)
132 {
133         struct iscsi_session *new_sess, *sess, *old_sess;
134         int err = 0;
135         union iscsi_sid sid;
136         bool reinstatement = false;
137
138         TRACE_MGMT_DBG("Adding session SID %llx", info->sid);
139
140         err = iscsi_session_alloc(target, info, &new_sess);
141         if (err != 0)
142                 goto out;
143
144         mutex_lock(&target->target_mutex);
145
146         sess = session_lookup(target, info->sid);
147         if (sess != NULL) {
148                 PRINT_ERROR("Attempt to add session with existing SID %llx",
149                         info->sid);
150                 err = -EEXIST;
151                 goto out_err_unlock;
152         }
153
154         sid = *(union iscsi_sid *)&info->sid;
155         sid.id.tsih = 0;
156         old_sess = NULL;
157
158         /*
159          * We need to find the latest session to correctly handle
160          * multi-reinstatements
161          */
162         list_for_each_entry_reverse(sess, &target->session_list,
163                         session_list_entry) {
164                 union iscsi_sid i = *(union iscsi_sid *)&sess->sid;
165                 i.id.tsih = 0;
166                 if ((sid.id64 == i.id64) &&
167                     (strcmp(info->initiator_name, sess->initiator_name) == 0)) {
168                         if (!sess->sess_shutting_down) {
169                                 /* session reinstatement */
170                                 old_sess = sess;
171                         }
172                         break;
173                 }
174         }
175         sess = NULL;
176
177         list_add_tail(&new_sess->session_list_entry, &target->session_list);
178
179         if (old_sess != NULL) {
180                 reinstatement = true;
181
182                 TRACE_MGMT_DBG("Reinstating sess %p with SID %llx (old %p, "
183                         "SID %llx)", new_sess, new_sess->sid, old_sess,
184                         old_sess->sid);
185
186                 new_sess->sess_reinstating = 1;
187                 old_sess->sess_reinst_successor = new_sess;
188
189                 target_del_session(old_sess->target, old_sess, 0);
190         }
191
192         mutex_unlock(&target->target_mutex);
193
194         if (reinstatement) {
195                 /*
196                  * Mutex target_mgmt_mutex won't allow to add connections to
197                  * the new session after target_mutex was dropped, so it's safe
198                  * to replace the initial UA without it. We can't do it under
199                  * target_mutex, because otherwise we can establish a
200                  * circular locking dependency between target_mutex and
201                  * scst_mutex in SCST core (iscsi_report_aen() called by
202                  * SCST core under scst_mutex).
203                  */
204                 scst_set_initial_UA(new_sess->scst_sess,
205                         SCST_LOAD_SENSE(scst_sense_nexus_loss_UA));
206         }
207
208 out:
209         return err;
210
211 out_err_unlock:
212         mutex_unlock(&target->target_mutex);
213
214         scst_unregister_session(new_sess->scst_sess, 1, NULL);
215         new_sess->scst_sess = NULL;
216
217         mutex_lock(&target->target_mutex);
218         session_free(new_sess, false);
219         mutex_unlock(&target->target_mutex);
220         goto out;
221 }
222
223 /* target_mutex supposed to be locked */
224 int session_free(struct iscsi_session *session, bool del)
225 {
226         unsigned int i;
227
228         TRACE_MGMT_DBG("Freeing session %p (SID %llx)",
229                 session, session->sid);
230
231         sBUG_ON(!list_empty(&session->conn_list));
232         if (unlikely(atomic_read(&session->active_cmds) != 0)) {
233                 PRINT_CRIT_ERROR("active_cmds not 0 (%d)!!",
234                         atomic_read(&session->active_cmds));
235                 sBUG();
236         }
237
238         for (i = 0; i < ARRAY_SIZE(session->cmnd_hash); i++)
239                 sBUG_ON(!list_empty(&session->cmnd_hash[i]));
240
241         if (session->sess_reinst_successor != NULL)
242                 sess_enable_reinstated_sess(session->sess_reinst_successor);
243
244         if (session->sess_reinstating) {
245                 struct iscsi_session *s;
246                 TRACE_MGMT_DBG("Freeing being reinstated sess %p", session);
247                 list_for_each_entry(s, &session->target->session_list,
248                                                 session_list_entry) {
249                         if (s->sess_reinst_successor == session) {
250                                 s->sess_reinst_successor = NULL;
251                                 break;
252                         }
253                 }
254         }
255
256         if (session->scst_sess != NULL) {
257                 /*
258                  * We must NOT call scst_unregister_session() in the waiting
259                  * mode, since we are under target_mutex. Otherwise we can
260                  * establish a circular locking dependency between target_mutex
261                  * and scst_mutex in SCST core (iscsi_report_aen() called by
262                  * SCST core under scst_mutex).
263                  */
264                 scst_unregister_session(session->scst_sess, 0, NULL);
265                 session->scst_sess = NULL;
266         }
267
268         if (del)
269                 list_del(&session->session_list_entry);
270
271         kfree(session->initiator_name);
272         kfree(session);
273
274         return 0;
275 }
276
277 /* target_mutex supposed to be locked */
278 int session_del(struct iscsi_target *target, u64 sid)
279 {
280         struct iscsi_session *session;
281
282         session = session_lookup(target, sid);
283         if (!session)
284                 return -ENOENT;
285
286         if (!list_empty(&session->conn_list)) {
287                 PRINT_ERROR("%llu still have connections",
288                             (long long unsigned int)session->sid);
289                 return -EBUSY;
290         }
291
292         return session_free(session, true);
293 }
294
295 /* target_mutex supposed to be locked */
296 static void iscsi_session_info_show(struct seq_file *seq,
297                                     struct iscsi_target *target)
298 {
299         struct iscsi_session *session;
300
301         list_for_each_entry(session, &target->session_list,
302                             session_list_entry) {
303                 seq_printf(seq, "\tsid:%llx initiator:%s (reinstating %s)\n",
304                         (long long unsigned int)session->sid,
305                         session->initiator_name,
306                         session->sess_reinstating ? "yes" : "no");
307                 conn_info_show(seq, session);
308         }
309         return;
310 }
311
312 static int iscsi_session_seq_open(struct inode *inode, struct file *file)
313 {
314         int res;
315         res = seq_open(file, &iscsi_seq_op);
316         if (!res)
317                 ((struct seq_file *)file->private_data)->private =
318                         iscsi_session_info_show;
319         return res;
320 }
321
322 const struct file_operations session_seq_fops = {
323         .owner          = THIS_MODULE,
324         .open           = iscsi_session_seq_open,
325         .read           = seq_read,
326         .llseek         = seq_lseek,
327         .release        = seq_release,
328 };