The patch below fixes the following recently introduced checkpatch complaints:
[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_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         list_add_tail(&session->session_list_entry, &target->session_list);
95
96         TRACE_MGMT_DBG("Session %p created: target %p, tid %u, sid %#Lx",
97                 session, target, target->tid, info->sid);
98
99         *result = session;
100         return 0;
101
102 err:
103         if (session) {
104                 kfree(session->initiator_name);
105                 kfree(session);
106                 kfree(name);
107         }
108         return err;
109 }
110
111 /* target_mutex supposed to be locked */
112 void sess_enable_reinstated_sess(struct iscsi_session *sess)
113 {
114         struct iscsi_conn *c;
115
116         TRACE_ENTRY();
117
118         TRACE_MGMT_DBG("Enabling reinstate successor sess %p", sess);
119
120         sBUG_ON(!sess->sess_reinstating);
121
122         list_for_each_entry(c, &sess->conn_list, conn_list_entry) {
123                 __iscsi_socket_bind(c);
124         }
125         sess->sess_reinstating = 0;
126
127         TRACE_EXIT();
128         return;
129 }
130
131 /* target_mutex supposed to be locked */
132 static void session_reinstate(struct iscsi_session *old_sess,
133         struct iscsi_session *new_sess)
134 {
135         TRACE_ENTRY();
136
137         TRACE_MGMT_DBG("Reinstating sess %p with SID %llx (old %p, SID %llx)",
138                 new_sess, new_sess->sid, old_sess, old_sess->sid);
139
140         new_sess->sess_reinstating = 1;
141         old_sess->sess_reinst_successor = new_sess;
142
143         scst_set_initial_UA(new_sess->scst_sess,
144                 SCST_LOAD_SENSE(scst_sense_nexus_loss_UA));
145
146         target_del_session(old_sess->target, old_sess, 0);
147
148         TRACE_EXIT();
149         return;
150 }
151
152 /* target_mutex supposed to be locked */
153 int session_add(struct iscsi_target *target,
154         struct iscsi_kern_session_info *info)
155 {
156         struct iscsi_session *session, *old_sess;
157         int err = 0;
158         union iscsi_sid sid;
159
160         TRACE_MGMT_DBG("Adding session SID %llx", info->sid);
161
162         session = session_lookup(target, info->sid);
163         if (session) {
164                 PRINT_ERROR("Attempt to add session with existing SID %llx",
165                         info->sid);
166                 err = -EEXIST;
167                 goto out;
168         }
169
170         sid = *(union iscsi_sid *)&info->sid;
171         sid.id.tsih = 0;
172         old_sess = NULL;
173
174         /*
175          * We need to find the latest session to correctly handle
176          * multi-reinstatements
177          */
178         list_for_each_entry_reverse(session, &target->session_list,
179                         session_list_entry) {
180                 union iscsi_sid i = *(union iscsi_sid *)&session->sid;
181                 i.id.tsih = 0;
182                 if ((sid.id64 == i.id64) &&
183                     (strcmp(info->initiator_name, session->initiator_name) == 0)) {
184                         if (!session->sess_shutting_down) {
185                                 /* session reinstatement */
186                                 old_sess = session;
187                         }
188                         break;
189                 }
190         }
191
192         err = iscsi_session_alloc(target, info, &session);
193         if ((err == 0) && (old_sess != NULL))
194                 session_reinstate(old_sess, session);
195
196 out:
197         return err;
198 }
199
200 /* target_mutex supposed to be locked */
201 int session_free(struct iscsi_session *session)
202 {
203         unsigned int i;
204
205         TRACE_MGMT_DBG("Freeing session %p (SID %llx)",
206                 session, session->sid);
207
208         sBUG_ON(!list_empty(&session->conn_list));
209         if (unlikely(atomic_read(&session->active_cmds) != 0)) {
210                 PRINT_CRIT_ERROR("active_cmds not 0 (%d)!!",
211                         atomic_read(&session->active_cmds));
212                 sBUG();
213         }
214
215         for (i = 0; i < ARRAY_SIZE(session->cmnd_hash); i++)
216                 sBUG_ON(!list_empty(&session->cmnd_hash[i]));
217
218         if (session->scst_sess != NULL)
219                 scst_unregister_session(session->scst_sess, 1, NULL);
220
221         if (session->sess_reinst_successor != NULL)
222                 sess_enable_reinstated_sess(session->sess_reinst_successor);
223
224         if (session->sess_reinstating) {
225                 struct iscsi_session *s;
226                 TRACE_MGMT_DBG("Freeing being reinstated sess %p", session);
227                 list_for_each_entry(s, &session->target->session_list,
228                                                 session_list_entry) {
229                         if (s->sess_reinst_successor == session) {
230                                 s->sess_reinst_successor = NULL;
231                                 break;
232                         }
233                 }
234         }
235
236         list_del(&session->session_list_entry);
237
238         kfree(session->initiator_name);
239         kfree(session);
240
241         return 0;
242 }
243
244 /* target_mutex supposed to be locked */
245 int session_del(struct iscsi_target *target, u64 sid)
246 {
247         struct iscsi_session *session;
248
249         session = session_lookup(target, sid);
250         if (!session)
251                 return -ENOENT;
252
253         if (!list_empty(&session->conn_list)) {
254                 PRINT_ERROR("%llu still have connections",
255                             (long long unsigned int)session->sid);
256                 return -EBUSY;
257         }
258
259         return session_free(session);
260 }
261
262 /* target_mutex supposed to be locked */
263 static void iscsi_session_info_show(struct seq_file *seq,
264                                     struct iscsi_target *target)
265 {
266         struct iscsi_session *session;
267
268         list_for_each_entry(session, &target->session_list,
269                             session_list_entry) {
270                 seq_printf(seq, "\tsid:%llx initiator:%s reinstating %d\n",
271                         (long long unsigned int)session->sid,
272                         session->initiator_name,
273                         session->sess_reinstating);
274                 conn_info_show(seq, session);
275         }
276         return;
277 }
278
279 static int iscsi_session_seq_open(struct inode *inode, struct file *file)
280 {
281         int res;
282         res = seq_open(file, &iscsi_seq_op);
283         if (!res)
284                 ((struct seq_file *)file->private_data)->private =
285                         iscsi_session_info_show;
286         return res;
287 }
288
289 struct file_operations session_seq_fops = {
290         .owner          = THIS_MODULE,
291         .open           = iscsi_session_seq_open,
292         .read           = seq_read,
293         .llseek         = seq_lseek,
294         .release        = seq_release,
295 };