1c503655ee3c35bf9c8b5c4f5f40995329d07c54
[mirror/scst/.git] / iscsi-scst / kernel / session.c
1 /*
2  *  Copyright (C) 2002 - 2003 Ardis Technolgies <roman@ardistech.com>
3  *  Copyright (C) 2007 - 2009 Vladislav Bolkhovitin
4  *  Copyright (C) 2007 - 2009 ID7 Ltd.
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_reinst_finished(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                 conn_reinst_finished(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 = NULL, *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 static void __session_free(struct iscsi_session *session)
224 {
225         kfree(session->initiator_name);
226         kfree(session);
227 }
228
229 static void iscsi_unreg_sess_done(struct scst_session *scst_sess)
230 {
231         struct iscsi_session *session;
232
233         TRACE_ENTRY();
234
235         session = (struct iscsi_session *)scst_sess_get_tgt_priv(scst_sess);
236
237         session->scst_sess = NULL;
238         __session_free(session);
239
240         TRACE_EXIT();
241         return;
242 }
243
244 /* target_mutex supposed to be locked */
245 int session_free(struct iscsi_session *session, bool del)
246 {
247         unsigned int i;
248
249         TRACE_MGMT_DBG("Freeing session %p (SID %llx)",
250                 session, session->sid);
251
252         sBUG_ON(!list_empty(&session->conn_list));
253         if (unlikely(atomic_read(&session->active_cmds) != 0)) {
254                 PRINT_CRIT_ERROR("active_cmds not 0 (%d)!!",
255                         atomic_read(&session->active_cmds));
256                 sBUG();
257         }
258
259         for (i = 0; i < ARRAY_SIZE(session->cmnd_hash); i++)
260                 sBUG_ON(!list_empty(&session->cmnd_hash[i]));
261
262         if (session->sess_reinst_successor != NULL)
263                 sess_reinst_finished(session->sess_reinst_successor);
264
265         if (session->sess_reinstating) {
266                 struct iscsi_session *s;
267                 TRACE_MGMT_DBG("Freeing being reinstated sess %p", session);
268                 list_for_each_entry(s, &session->target->session_list,
269                                                 session_list_entry) {
270                         if (s->sess_reinst_successor == session) {
271                                 s->sess_reinst_successor = NULL;
272                                 break;
273                         }
274                 }
275         }
276
277         if (del)
278                 list_del(&session->session_list_entry);
279
280         if (session->scst_sess != NULL) {
281                 /*
282                  * We must NOT call scst_unregister_session() in the waiting
283                  * mode, since we are under target_mutex. Otherwise we can
284                  * establish a circular locking dependency between target_mutex
285                  * and scst_mutex in SCST core (iscsi_report_aen() called by
286                  * SCST core under scst_mutex).
287                  */
288                 scst_unregister_session(session->scst_sess, 0,
289                         iscsi_unreg_sess_done);
290         } else
291                 __session_free(session);
292
293         return 0;
294 }
295
296 /* target_mutex supposed to be locked */
297 int session_del(struct iscsi_target *target, u64 sid)
298 {
299         struct iscsi_session *session;
300
301         session = session_lookup(target, sid);
302         if (!session)
303                 return -ENOENT;
304
305         if (!list_empty(&session->conn_list)) {
306                 PRINT_ERROR("%llu still have connections",
307                             (long long unsigned int)session->sid);
308                 return -EBUSY;
309         }
310
311         return session_free(session, true);
312 }
313
314 #ifdef CONFIG_SCST_PROC
315
316 /* target_mutex supposed to be locked */
317 static void iscsi_session_info_show(struct seq_file *seq,
318                                     struct iscsi_target *target)
319 {
320         struct iscsi_session *session;
321
322         list_for_each_entry(session, &target->session_list,
323                             session_list_entry) {
324                 seq_printf(seq, "\tsid:%llx initiator:%s (reinstating %s)\n",
325                         (long long unsigned int)session->sid,
326                         session->initiator_name,
327                         session->sess_reinstating ? "yes" : "no");
328                 conn_info_show(seq, session);
329         }
330         return;
331 }
332
333 static int iscsi_session_seq_open(struct inode *inode, struct file *file)
334 {
335         int res;
336         res = seq_open(file, &iscsi_seq_op);
337         if (!res)
338                 ((struct seq_file *)file->private_data)->private =
339                         iscsi_session_info_show;
340         return res;
341 }
342
343 const struct file_operations session_seq_fops = {
344         .owner          = THIS_MODULE,
345         .open           = iscsi_session_seq_open,
346         .read           = seq_read,
347         .llseek         = seq_lseek,
348         .release        = seq_release,
349 };
350
351 #else /* CONFIG_SCST_PROC */
352
353 static ssize_t iscsi_sess_sid_show(struct kobject *kobj,
354         struct kobj_attribute *attr, char *buf)
355 {
356         int pos;
357         struct scst_session *scst_sess;
358         struct iscsi_session *sess;
359
360         TRACE_ENTRY();
361
362         scst_sess = container_of(kobj, struct scst_session, sess_kobj);
363         sess = (struct iscsi_session *)scst_sess_get_tgt_priv(scst_sess);
364
365         pos = sprintf(buf, "%llx\n", sess->sid);
366
367         TRACE_EXIT_RES(pos);
368         return pos;
369 }
370
371 static struct kobj_attribute iscsi_sess_sid_attr =
372         __ATTR(sid, S_IRUGO, iscsi_sess_sid_show, NULL);
373
374 static ssize_t iscsi_sess_reinstating_show(struct kobject *kobj,
375         struct kobj_attribute *attr, char *buf)
376 {
377         int pos;
378         struct scst_session *scst_sess;
379         struct iscsi_session *sess;
380
381         TRACE_ENTRY();
382
383         scst_sess = container_of(kobj, struct scst_session, sess_kobj);
384         sess = (struct iscsi_session *)scst_sess_get_tgt_priv(scst_sess);
385
386         pos = sprintf(buf, "%d\n", sess->sess_reinstating ? 1 : 0);
387
388         TRACE_EXIT_RES(pos);
389         return pos;
390 }
391
392 static struct kobj_attribute iscsi_sess_reinstating_attr =
393         __ATTR(reinstating, S_IRUGO, iscsi_sess_reinstating_show, NULL);
394
395 const struct attribute *iscsi_sess_attrs[] = {
396         &iscsi_sess_sid_attr.attr,
397         &iscsi_sess_reinstating_attr.attr,
398         NULL,
399 };
400
401 #endif /* CONFIG_SCST_PROC */