GNU Linux-libre 4.19.263-gnu1
[releases.git] / drivers / target / iscsi / iscsi_target_configfs.c
1 /*******************************************************************************
2  * This file contains the configfs implementation for iSCSI Target mode
3  * from the LIO-Target Project.
4  *
5  * (c) Copyright 2007-2013 Datera, Inc.
6  *
7  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  ****************************************************************************/
19
20 #include <linux/configfs.h>
21 #include <linux/ctype.h>
22 #include <linux/export.h>
23 #include <linux/inet.h>
24 #include <linux/module.h>
25 #include <net/ipv6.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
28 #include <target/iscsi/iscsi_transport.h>
29 #include <target/iscsi/iscsi_target_core.h>
30 #include "iscsi_target_parameters.h"
31 #include "iscsi_target_device.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_nodeattrib.h"
34 #include "iscsi_target_tpg.h"
35 #include "iscsi_target_util.h"
36 #include "iscsi_target.h"
37 #include <target/iscsi/iscsi_target_stat.h>
38
39
40 /* Start items for lio_target_portal_cit */
41
42 static inline struct iscsi_tpg_np *to_iscsi_tpg_np(struct config_item *item)
43 {
44         return container_of(to_tpg_np(item), struct iscsi_tpg_np, se_tpg_np);
45 }
46
47 static ssize_t lio_target_np_driver_show(struct config_item *item, char *page,
48                                          enum iscsit_transport_type type)
49 {
50         struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
51         struct iscsi_tpg_np *tpg_np_new;
52         ssize_t rb;
53
54         tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
55         if (tpg_np_new)
56                 rb = sprintf(page, "1\n");
57         else
58                 rb = sprintf(page, "0\n");
59
60         return rb;
61 }
62
63 static ssize_t lio_target_np_driver_store(struct config_item *item,
64                 const char *page, size_t count, enum iscsit_transport_type type,
65                 const char *mod_name)
66 {
67         struct iscsi_tpg_np *tpg_np = to_iscsi_tpg_np(item);
68         struct iscsi_np *np;
69         struct iscsi_portal_group *tpg;
70         struct iscsi_tpg_np *tpg_np_new = NULL;
71         u32 op;
72         int rc;
73
74         rc = kstrtou32(page, 0, &op);
75         if (rc)
76                 return rc;
77         if ((op != 1) && (op != 0)) {
78                 pr_err("Illegal value for tpg_enable: %u\n", op);
79                 return -EINVAL;
80         }
81         np = tpg_np->tpg_np;
82         if (!np) {
83                 pr_err("Unable to locate struct iscsi_np from"
84                                 " struct iscsi_tpg_np\n");
85                 return -EINVAL;
86         }
87
88         tpg = tpg_np->tpg;
89         if (iscsit_get_tpg(tpg) < 0)
90                 return -EINVAL;
91
92         if (op) {
93                 if (strlen(mod_name)) {
94                         rc = request_module(mod_name);
95                         if (rc != 0) {
96                                 pr_warn("Unable to request_module for %s\n",
97                                         mod_name);
98                                 rc = 0;
99                         }
100                 }
101
102                 tpg_np_new = iscsit_tpg_add_network_portal(tpg,
103                                         &np->np_sockaddr, tpg_np, type);
104                 if (IS_ERR(tpg_np_new)) {
105                         rc = PTR_ERR(tpg_np_new);
106                         goto out;
107                 }
108         } else {
109                 tpg_np_new = iscsit_tpg_locate_child_np(tpg_np, type);
110                 if (tpg_np_new) {
111                         rc = iscsit_tpg_del_network_portal(tpg, tpg_np_new);
112                         if (rc < 0)
113                                 goto out;
114                 }
115         }
116
117         iscsit_put_tpg(tpg);
118         return count;
119 out:
120         iscsit_put_tpg(tpg);
121         return rc;
122 }
123
124 static ssize_t lio_target_np_iser_show(struct config_item *item, char *page)
125 {
126         return lio_target_np_driver_show(item, page, ISCSI_INFINIBAND);
127 }
128
129 static ssize_t lio_target_np_iser_store(struct config_item *item,
130                                         const char *page, size_t count)
131 {
132         return lio_target_np_driver_store(item, page, count,
133                                           ISCSI_INFINIBAND, "ib_isert");
134 }
135 CONFIGFS_ATTR(lio_target_np_, iser);
136
137 static ssize_t lio_target_np_cxgbit_show(struct config_item *item, char *page)
138 {
139         return lio_target_np_driver_show(item, page, ISCSI_CXGBIT);
140 }
141
142 static ssize_t lio_target_np_cxgbit_store(struct config_item *item,
143                                           const char *page, size_t count)
144 {
145         return lio_target_np_driver_store(item, page, count,
146                                           ISCSI_CXGBIT, "cxgbit");
147 }
148 CONFIGFS_ATTR(lio_target_np_, cxgbit);
149
150 static struct configfs_attribute *lio_target_portal_attrs[] = {
151         &lio_target_np_attr_iser,
152         &lio_target_np_attr_cxgbit,
153         NULL,
154 };
155
156 /* Stop items for lio_target_portal_cit */
157
158 /* Start items for lio_target_np_cit */
159
160 #define MAX_PORTAL_LEN          256
161
162 static struct se_tpg_np *lio_target_call_addnptotpg(
163         struct se_portal_group *se_tpg,
164         struct config_group *group,
165         const char *name)
166 {
167         struct iscsi_portal_group *tpg;
168         struct iscsi_tpg_np *tpg_np;
169         char *str, *str2, *ip_str, *port_str;
170         struct sockaddr_storage sockaddr = { };
171         int ret;
172         char buf[MAX_PORTAL_LEN + 1];
173
174         if (strlen(name) > MAX_PORTAL_LEN) {
175                 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
176                         (int)strlen(name), MAX_PORTAL_LEN);
177                 return ERR_PTR(-EOVERFLOW);
178         }
179         memset(buf, 0, MAX_PORTAL_LEN + 1);
180         snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
181
182         str = strstr(buf, "[");
183         if (str) {
184                 str2 = strstr(str, "]");
185                 if (!str2) {
186                         pr_err("Unable to locate trailing \"]\""
187                                 " in IPv6 iSCSI network portal address\n");
188                         return ERR_PTR(-EINVAL);
189                 }
190
191                 ip_str = str + 1; /* Skip over leading "[" */
192                 *str2 = '\0'; /* Terminate the unbracketed IPv6 address */
193                 str2++; /* Skip over the \0 */
194
195                 port_str = strstr(str2, ":");
196                 if (!port_str) {
197                         pr_err("Unable to locate \":port\""
198                                 " in IPv6 iSCSI network portal address\n");
199                         return ERR_PTR(-EINVAL);
200                 }
201                 *port_str = '\0'; /* Terminate string for IP */
202                 port_str++; /* Skip over ":" */
203         } else {
204                 ip_str = &buf[0];
205                 port_str = strstr(ip_str, ":");
206                 if (!port_str) {
207                         pr_err("Unable to locate \":port\""
208                                 " in IPv4 iSCSI network portal address\n");
209                         return ERR_PTR(-EINVAL);
210                 }
211                 *port_str = '\0'; /* Terminate string for IP */
212                 port_str++; /* Skip over ":" */
213         }
214
215         ret = inet_pton_with_scope(&init_net, AF_UNSPEC, ip_str,
216                         port_str, &sockaddr);
217         if (ret) {
218                 pr_err("malformed ip/port passed: %s\n", name);
219                 return ERR_PTR(ret);
220         }
221
222         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
223         ret = iscsit_get_tpg(tpg);
224         if (ret < 0)
225                 return ERR_PTR(-EINVAL);
226
227         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
228                 " PORTAL: %s\n",
229                 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
230                 tpg->tpgt, name);
231         /*
232          * Assume ISCSI_TCP by default.  Other network portals for other
233          * iSCSI fabrics:
234          *
235          * Traditional iSCSI over SCTP (initial support)
236          * iSER/TCP (TODO, hardware available)
237          * iSER/SCTP (TODO, software emulation with osc-iwarp)
238          * iSER/IB (TODO, hardware available)
239          *
240          * can be enabled with attributes under
241          * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
242          *
243          */
244         tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, NULL,
245                                 ISCSI_TCP);
246         if (IS_ERR(tpg_np)) {
247                 iscsit_put_tpg(tpg);
248                 return ERR_CAST(tpg_np);
249         }
250         pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
251
252         iscsit_put_tpg(tpg);
253         return &tpg_np->se_tpg_np;
254 }
255
256 static void lio_target_call_delnpfromtpg(
257         struct se_tpg_np *se_tpg_np)
258 {
259         struct iscsi_portal_group *tpg;
260         struct iscsi_tpg_np *tpg_np;
261         struct se_portal_group *se_tpg;
262         int ret;
263
264         tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
265         tpg = tpg_np->tpg;
266         ret = iscsit_get_tpg(tpg);
267         if (ret < 0)
268                 return;
269
270         se_tpg = &tpg->tpg_se_tpg;
271         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
272                 " PORTAL: %pISpc\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
273                 tpg->tpgt, &tpg_np->tpg_np->np_sockaddr);
274
275         ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
276         if (ret < 0)
277                 goto out;
278
279         pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
280 out:
281         iscsit_put_tpg(tpg);
282 }
283
284 /* End items for lio_target_np_cit */
285
286 /* Start items for lio_target_nacl_attrib_cit */
287
288 #define ISCSI_NACL_ATTR(name)                                           \
289 static ssize_t iscsi_nacl_attrib_##name##_show(struct config_item *item,\
290                 char *page)                                             \
291 {                                                                       \
292         struct se_node_acl *se_nacl = attrib_to_nacl(item);             \
293         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
294                                         se_node_acl);                   \
295                                                                         \
296         return sprintf(page, "%u\n", nacl->node_attrib.name);           \
297 }                                                                       \
298                                                                         \
299 static ssize_t iscsi_nacl_attrib_##name##_store(struct config_item *item,\
300                 const char *page, size_t count)                         \
301 {                                                                       \
302         struct se_node_acl *se_nacl = attrib_to_nacl(item);             \
303         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
304                                         se_node_acl);                   \
305         u32 val;                                                        \
306         int ret;                                                        \
307                                                                         \
308         ret = kstrtou32(page, 0, &val);                                 \
309         if (ret)                                                        \
310                 return ret;                                             \
311         ret = iscsit_na_##name(nacl, val);                              \
312         if (ret < 0)                                                    \
313                 return ret;                                             \
314                                                                         \
315         return count;                                                   \
316 }                                                                       \
317                                                                         \
318 CONFIGFS_ATTR(iscsi_nacl_attrib_, name)
319
320 ISCSI_NACL_ATTR(dataout_timeout);
321 ISCSI_NACL_ATTR(dataout_timeout_retries);
322 ISCSI_NACL_ATTR(default_erl);
323 ISCSI_NACL_ATTR(nopin_timeout);
324 ISCSI_NACL_ATTR(nopin_response_timeout);
325 ISCSI_NACL_ATTR(random_datain_pdu_offsets);
326 ISCSI_NACL_ATTR(random_datain_seq_offsets);
327 ISCSI_NACL_ATTR(random_r2t_offsets);
328
329 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
330         &iscsi_nacl_attrib_attr_dataout_timeout,
331         &iscsi_nacl_attrib_attr_dataout_timeout_retries,
332         &iscsi_nacl_attrib_attr_default_erl,
333         &iscsi_nacl_attrib_attr_nopin_timeout,
334         &iscsi_nacl_attrib_attr_nopin_response_timeout,
335         &iscsi_nacl_attrib_attr_random_datain_pdu_offsets,
336         &iscsi_nacl_attrib_attr_random_datain_seq_offsets,
337         &iscsi_nacl_attrib_attr_random_r2t_offsets,
338         NULL,
339 };
340
341 /* End items for lio_target_nacl_attrib_cit */
342
343 /* Start items for lio_target_nacl_auth_cit */
344
345 #define __DEF_NACL_AUTH_STR(prefix, name, flags)                        \
346 static ssize_t __iscsi_##prefix##_##name##_show(                        \
347         struct iscsi_node_acl *nacl,                                    \
348         char *page)                                                     \
349 {                                                                       \
350         struct iscsi_node_auth *auth = &nacl->node_auth;                \
351                                                                         \
352         if (!capable(CAP_SYS_ADMIN))                                    \
353                 return -EPERM;                                          \
354         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);           \
355 }                                                                       \
356                                                                         \
357 static ssize_t __iscsi_##prefix##_##name##_store(                       \
358         struct iscsi_node_acl *nacl,                                    \
359         const char *page,                                               \
360         size_t count)                                                   \
361 {                                                                       \
362         struct iscsi_node_auth *auth = &nacl->node_auth;                \
363                                                                         \
364         if (!capable(CAP_SYS_ADMIN))                                    \
365                 return -EPERM;                                          \
366         if (count >= sizeof(auth->name))                                \
367                 return -EINVAL;                                         \
368         snprintf(auth->name, sizeof(auth->name), "%s", page);           \
369         if (!strncmp("NULL", auth->name, 4))                            \
370                 auth->naf_flags &= ~flags;                              \
371         else                                                            \
372                 auth->naf_flags |= flags;                               \
373                                                                         \
374         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                    \
375             (auth->naf_flags & NAF_PASSWORD_IN_SET))                    \
376                 auth->authenticate_target = 1;                          \
377         else                                                            \
378                 auth->authenticate_target = 0;                          \
379                                                                         \
380         return count;                                                   \
381 }
382
383 #define DEF_NACL_AUTH_STR(name, flags)                                  \
384         __DEF_NACL_AUTH_STR(nacl_auth, name, flags)                     \
385 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,  \
386                 char *page)                                             \
387 {                                                                       \
388         struct se_node_acl *nacl = auth_to_nacl(item);                  \
389         return __iscsi_nacl_auth_##name##_show(container_of(nacl,       \
390                         struct iscsi_node_acl, se_node_acl), page);     \
391 }                                                                       \
392 static ssize_t iscsi_nacl_auth_##name##_store(struct config_item *item, \
393                 const char *page, size_t count)                         \
394 {                                                                       \
395         struct se_node_acl *nacl = auth_to_nacl(item);                  \
396         return __iscsi_nacl_auth_##name##_store(container_of(nacl,      \
397                         struct iscsi_node_acl, se_node_acl), page, count); \
398 }                                                                       \
399                                                                         \
400 CONFIGFS_ATTR(iscsi_nacl_auth_, name)
401
402 /*
403  * One-way authentication userid
404  */
405 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
406 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
407 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
408 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
409
410 #define __DEF_NACL_AUTH_INT(prefix, name)                               \
411 static ssize_t __iscsi_##prefix##_##name##_show(                                \
412         struct iscsi_node_acl *nacl,                                    \
413         char *page)                                                     \
414 {                                                                       \
415         struct iscsi_node_auth *auth = &nacl->node_auth;                \
416                                                                         \
417         if (!capable(CAP_SYS_ADMIN))                                    \
418                 return -EPERM;                                          \
419                                                                         \
420         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);           \
421 }
422
423 #define DEF_NACL_AUTH_INT(name)                                         \
424         __DEF_NACL_AUTH_INT(nacl_auth, name)                            \
425 static ssize_t iscsi_nacl_auth_##name##_show(struct config_item *item,  \
426                 char *page)                                             \
427 {                                                                       \
428         struct se_node_acl *nacl = auth_to_nacl(item);                  \
429         return __iscsi_nacl_auth_##name##_show(container_of(nacl,       \
430                         struct iscsi_node_acl, se_node_acl), page);     \
431 }                                                                       \
432                                                                         \
433 CONFIGFS_ATTR_RO(iscsi_nacl_auth_, name)
434
435 DEF_NACL_AUTH_INT(authenticate_target);
436
437 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
438         &iscsi_nacl_auth_attr_userid,
439         &iscsi_nacl_auth_attr_password,
440         &iscsi_nacl_auth_attr_authenticate_target,
441         &iscsi_nacl_auth_attr_userid_mutual,
442         &iscsi_nacl_auth_attr_password_mutual,
443         NULL,
444 };
445
446 /* End items for lio_target_nacl_auth_cit */
447
448 /* Start items for lio_target_nacl_param_cit */
449
450 #define ISCSI_NACL_PARAM(name)                                          \
451 static ssize_t iscsi_nacl_param_##name##_show(struct config_item *item, \
452                 char *page)                                             \
453 {                                                                       \
454         struct se_node_acl *se_nacl = param_to_nacl(item);              \
455         struct iscsi_session *sess;                                     \
456         struct se_session *se_sess;                                     \
457         ssize_t rb;                                                     \
458                                                                         \
459         spin_lock_bh(&se_nacl->nacl_sess_lock);                         \
460         se_sess = se_nacl->nacl_sess;                                   \
461         if (!se_sess) {                                                 \
462                 rb = snprintf(page, PAGE_SIZE,                          \
463                         "No Active iSCSI Session\n");                   \
464         } else {                                                        \
465                 sess = se_sess->fabric_sess_ptr;                        \
466                 rb = snprintf(page, PAGE_SIZE, "%u\n",                  \
467                         (u32)sess->sess_ops->name);                     \
468         }                                                               \
469         spin_unlock_bh(&se_nacl->nacl_sess_lock);                       \
470                                                                         \
471         return rb;                                                      \
472 }                                                                       \
473                                                                         \
474 CONFIGFS_ATTR_RO(iscsi_nacl_param_, name)
475
476 ISCSI_NACL_PARAM(MaxConnections);
477 ISCSI_NACL_PARAM(InitialR2T);
478 ISCSI_NACL_PARAM(ImmediateData);
479 ISCSI_NACL_PARAM(MaxBurstLength);
480 ISCSI_NACL_PARAM(FirstBurstLength);
481 ISCSI_NACL_PARAM(DefaultTime2Wait);
482 ISCSI_NACL_PARAM(DefaultTime2Retain);
483 ISCSI_NACL_PARAM(MaxOutstandingR2T);
484 ISCSI_NACL_PARAM(DataPDUInOrder);
485 ISCSI_NACL_PARAM(DataSequenceInOrder);
486 ISCSI_NACL_PARAM(ErrorRecoveryLevel);
487
488 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
489         &iscsi_nacl_param_attr_MaxConnections,
490         &iscsi_nacl_param_attr_InitialR2T,
491         &iscsi_nacl_param_attr_ImmediateData,
492         &iscsi_nacl_param_attr_MaxBurstLength,
493         &iscsi_nacl_param_attr_FirstBurstLength,
494         &iscsi_nacl_param_attr_DefaultTime2Wait,
495         &iscsi_nacl_param_attr_DefaultTime2Retain,
496         &iscsi_nacl_param_attr_MaxOutstandingR2T,
497         &iscsi_nacl_param_attr_DataPDUInOrder,
498         &iscsi_nacl_param_attr_DataSequenceInOrder,
499         &iscsi_nacl_param_attr_ErrorRecoveryLevel,
500         NULL,
501 };
502
503 /* End items for lio_target_nacl_param_cit */
504
505 /* Start items for lio_target_acl_cit */
506
507 static ssize_t lio_target_nacl_info_show(struct config_item *item, char *page)
508 {
509         struct se_node_acl *se_nacl = acl_to_nacl(item);
510         struct iscsi_session *sess;
511         struct iscsi_conn *conn;
512         struct se_session *se_sess;
513         ssize_t rb = 0;
514         u32 max_cmd_sn;
515
516         spin_lock_bh(&se_nacl->nacl_sess_lock);
517         se_sess = se_nacl->nacl_sess;
518         if (!se_sess) {
519                 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
520                         " Endpoint: %s\n", se_nacl->initiatorname);
521         } else {
522                 sess = se_sess->fabric_sess_ptr;
523
524                 rb += sprintf(page+rb, "InitiatorName: %s\n",
525                         sess->sess_ops->InitiatorName);
526                 rb += sprintf(page+rb, "InitiatorAlias: %s\n",
527                         sess->sess_ops->InitiatorAlias);
528
529                 rb += sprintf(page+rb,
530                               "LIO Session ID: %u   ISID: 0x%6ph  TSIH: %hu  ",
531                               sess->sid, sess->isid, sess->tsih);
532                 rb += sprintf(page+rb, "SessionType: %s\n",
533                                 (sess->sess_ops->SessionType) ?
534                                 "Discovery" : "Normal");
535                 rb += sprintf(page+rb, "Session State: ");
536                 switch (sess->session_state) {
537                 case TARG_SESS_STATE_FREE:
538                         rb += sprintf(page+rb, "TARG_SESS_FREE\n");
539                         break;
540                 case TARG_SESS_STATE_ACTIVE:
541                         rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
542                         break;
543                 case TARG_SESS_STATE_LOGGED_IN:
544                         rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
545                         break;
546                 case TARG_SESS_STATE_FAILED:
547                         rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
548                         break;
549                 case TARG_SESS_STATE_IN_CONTINUE:
550                         rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
551                         break;
552                 default:
553                         rb += sprintf(page+rb, "ERROR: Unknown Session"
554                                         " State!\n");
555                         break;
556                 }
557
558                 rb += sprintf(page+rb, "---------------------[iSCSI Session"
559                                 " Values]-----------------------\n");
560                 rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
561                                 "  :  MaxCmdSN  :     ITT    :     TTT\n");
562                 max_cmd_sn = (u32) atomic_read(&sess->max_cmd_sn);
563                 rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
564                                 "   0x%08x   0x%08x\n",
565                         sess->cmdsn_window,
566                         (max_cmd_sn - sess->exp_cmd_sn) + 1,
567                         sess->exp_cmd_sn, max_cmd_sn,
568                         sess->init_task_tag, sess->targ_xfer_tag);
569                 rb += sprintf(page+rb, "----------------------[iSCSI"
570                                 " Connections]-------------------------\n");
571
572                 spin_lock(&sess->conn_lock);
573                 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
574                         rb += sprintf(page+rb, "CID: %hu  Connection"
575                                         " State: ", conn->cid);
576                         switch (conn->conn_state) {
577                         case TARG_CONN_STATE_FREE:
578                                 rb += sprintf(page+rb,
579                                         "TARG_CONN_STATE_FREE\n");
580                                 break;
581                         case TARG_CONN_STATE_XPT_UP:
582                                 rb += sprintf(page+rb,
583                                         "TARG_CONN_STATE_XPT_UP\n");
584                                 break;
585                         case TARG_CONN_STATE_IN_LOGIN:
586                                 rb += sprintf(page+rb,
587                                         "TARG_CONN_STATE_IN_LOGIN\n");
588                                 break;
589                         case TARG_CONN_STATE_LOGGED_IN:
590                                 rb += sprintf(page+rb,
591                                         "TARG_CONN_STATE_LOGGED_IN\n");
592                                 break;
593                         case TARG_CONN_STATE_IN_LOGOUT:
594                                 rb += sprintf(page+rb,
595                                         "TARG_CONN_STATE_IN_LOGOUT\n");
596                                 break;
597                         case TARG_CONN_STATE_LOGOUT_REQUESTED:
598                                 rb += sprintf(page+rb,
599                                         "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
600                                 break;
601                         case TARG_CONN_STATE_CLEANUP_WAIT:
602                                 rb += sprintf(page+rb,
603                                         "TARG_CONN_STATE_CLEANUP_WAIT\n");
604                                 break;
605                         default:
606                                 rb += sprintf(page+rb,
607                                         "ERROR: Unknown Connection State!\n");
608                                 break;
609                         }
610
611                         rb += sprintf(page+rb, "   Address %pISc %s", &conn->login_sockaddr,
612                                 (conn->network_transport == ISCSI_TCP) ?
613                                 "TCP" : "SCTP");
614                         rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
615                                 conn->stat_sn);
616                 }
617                 spin_unlock(&sess->conn_lock);
618         }
619         spin_unlock_bh(&se_nacl->nacl_sess_lock);
620
621         return rb;
622 }
623
624 static ssize_t lio_target_nacl_cmdsn_depth_show(struct config_item *item,
625                 char *page)
626 {
627         return sprintf(page, "%u\n", acl_to_nacl(item)->queue_depth);
628 }
629
630 static ssize_t lio_target_nacl_cmdsn_depth_store(struct config_item *item,
631                 const char *page, size_t count)
632 {
633         struct se_node_acl *se_nacl = acl_to_nacl(item);
634         struct se_portal_group *se_tpg = se_nacl->se_tpg;
635         struct iscsi_portal_group *tpg = container_of(se_tpg,
636                         struct iscsi_portal_group, tpg_se_tpg);
637         struct config_item *acl_ci, *tpg_ci, *wwn_ci;
638         u32 cmdsn_depth = 0;
639         int ret;
640
641         ret = kstrtou32(page, 0, &cmdsn_depth);
642         if (ret)
643                 return ret;
644         if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
645                 pr_err("Passed cmdsn_depth: %u exceeds"
646                         " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
647                         TA_DEFAULT_CMDSN_DEPTH_MAX);
648                 return -EINVAL;
649         }
650         acl_ci = &se_nacl->acl_group.cg_item;
651         if (!acl_ci) {
652                 pr_err("Unable to locatel acl_ci\n");
653                 return -EINVAL;
654         }
655         tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
656         if (!tpg_ci) {
657                 pr_err("Unable to locate tpg_ci\n");
658                 return -EINVAL;
659         }
660         wwn_ci = &tpg_ci->ci_group->cg_item;
661         if (!wwn_ci) {
662                 pr_err("Unable to locate config_item wwn_ci\n");
663                 return -EINVAL;
664         }
665
666         if (iscsit_get_tpg(tpg) < 0)
667                 return -EINVAL;
668
669         ret = core_tpg_set_initiator_node_queue_depth(se_nacl, cmdsn_depth);
670
671         pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
672                 "InitiatorName: %s\n", config_item_name(wwn_ci),
673                 config_item_name(tpg_ci), cmdsn_depth,
674                 config_item_name(acl_ci));
675
676         iscsit_put_tpg(tpg);
677         return (!ret) ? count : (ssize_t)ret;
678 }
679
680 static ssize_t lio_target_nacl_tag_show(struct config_item *item, char *page)
681 {
682         return snprintf(page, PAGE_SIZE, "%s", acl_to_nacl(item)->acl_tag);
683 }
684
685 static ssize_t lio_target_nacl_tag_store(struct config_item *item,
686                 const char *page, size_t count)
687 {
688         struct se_node_acl *se_nacl = acl_to_nacl(item);
689         int ret;
690
691         ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
692
693         if (ret < 0)
694                 return ret;
695         return count;
696 }
697
698 CONFIGFS_ATTR_RO(lio_target_nacl_, info);
699 CONFIGFS_ATTR(lio_target_nacl_, cmdsn_depth);
700 CONFIGFS_ATTR(lio_target_nacl_, tag);
701
702 static struct configfs_attribute *lio_target_initiator_attrs[] = {
703         &lio_target_nacl_attr_info,
704         &lio_target_nacl_attr_cmdsn_depth,
705         &lio_target_nacl_attr_tag,
706         NULL,
707 };
708
709 static int lio_target_init_nodeacl(struct se_node_acl *se_nacl,
710                 const char *name)
711 {
712         struct iscsi_node_acl *acl =
713                 container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
714
715         config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
716                         "iscsi_sess_stats", &iscsi_stat_sess_cit);
717         configfs_add_default_group(&acl->node_stat_grps.iscsi_sess_stats_group,
718                         &se_nacl->acl_fabric_stat_group);
719         return 0;
720 }
721
722 /* End items for lio_target_acl_cit */
723
724 /* Start items for lio_target_tpg_attrib_cit */
725
726 #define DEF_TPG_ATTRIB(name)                                            \
727                                                                         \
728 static ssize_t iscsi_tpg_attrib_##name##_show(struct config_item *item, \
729                 char *page)                                             \
730 {                                                                       \
731         struct se_portal_group *se_tpg = attrib_to_tpg(item);           \
732         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
733                         struct iscsi_portal_group, tpg_se_tpg); \
734         ssize_t rb;                                                     \
735                                                                         \
736         if (iscsit_get_tpg(tpg) < 0)                                    \
737                 return -EINVAL;                                         \
738                                                                         \
739         rb = sprintf(page, "%u\n", tpg->tpg_attrib.name);               \
740         iscsit_put_tpg(tpg);                                            \
741         return rb;                                                      \
742 }                                                                       \
743                                                                         \
744 static ssize_t iscsi_tpg_attrib_##name##_store(struct config_item *item,\
745                 const char *page, size_t count)                         \
746 {                                                                       \
747         struct se_portal_group *se_tpg = attrib_to_tpg(item);           \
748         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
749                         struct iscsi_portal_group, tpg_se_tpg); \
750         u32 val;                                                        \
751         int ret;                                                        \
752                                                                         \
753         if (iscsit_get_tpg(tpg) < 0)                                    \
754                 return -EINVAL;                                         \
755                                                                         \
756         ret = kstrtou32(page, 0, &val);                                 \
757         if (ret)                                                        \
758                 goto out;                                               \
759         ret = iscsit_ta_##name(tpg, val);                               \
760         if (ret < 0)                                                    \
761                 goto out;                                               \
762                                                                         \
763         iscsit_put_tpg(tpg);                                            \
764         return count;                                                   \
765 out:                                                                    \
766         iscsit_put_tpg(tpg);                                            \
767         return ret;                                                     \
768 }                                                                       \
769 CONFIGFS_ATTR(iscsi_tpg_attrib_, name)
770
771 DEF_TPG_ATTRIB(authentication);
772 DEF_TPG_ATTRIB(login_timeout);
773 DEF_TPG_ATTRIB(netif_timeout);
774 DEF_TPG_ATTRIB(generate_node_acls);
775 DEF_TPG_ATTRIB(default_cmdsn_depth);
776 DEF_TPG_ATTRIB(cache_dynamic_acls);
777 DEF_TPG_ATTRIB(demo_mode_write_protect);
778 DEF_TPG_ATTRIB(prod_mode_write_protect);
779 DEF_TPG_ATTRIB(demo_mode_discovery);
780 DEF_TPG_ATTRIB(default_erl);
781 DEF_TPG_ATTRIB(t10_pi);
782 DEF_TPG_ATTRIB(fabric_prot_type);
783 DEF_TPG_ATTRIB(tpg_enabled_sendtargets);
784 DEF_TPG_ATTRIB(login_keys_workaround);
785
786 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
787         &iscsi_tpg_attrib_attr_authentication,
788         &iscsi_tpg_attrib_attr_login_timeout,
789         &iscsi_tpg_attrib_attr_netif_timeout,
790         &iscsi_tpg_attrib_attr_generate_node_acls,
791         &iscsi_tpg_attrib_attr_default_cmdsn_depth,
792         &iscsi_tpg_attrib_attr_cache_dynamic_acls,
793         &iscsi_tpg_attrib_attr_demo_mode_write_protect,
794         &iscsi_tpg_attrib_attr_prod_mode_write_protect,
795         &iscsi_tpg_attrib_attr_demo_mode_discovery,
796         &iscsi_tpg_attrib_attr_default_erl,
797         &iscsi_tpg_attrib_attr_t10_pi,
798         &iscsi_tpg_attrib_attr_fabric_prot_type,
799         &iscsi_tpg_attrib_attr_tpg_enabled_sendtargets,
800         &iscsi_tpg_attrib_attr_login_keys_workaround,
801         NULL,
802 };
803
804 /* End items for lio_target_tpg_attrib_cit */
805
806 /* Start items for lio_target_tpg_auth_cit */
807
808 #define __DEF_TPG_AUTH_STR(prefix, name, flags)                                 \
809 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
810                 char *page)                                                     \
811 {                                                                               \
812         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
813                                 struct iscsi_portal_group, tpg_se_tpg);         \
814         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
815                                                                                 \
816         if (!capable(CAP_SYS_ADMIN))                                            \
817                 return -EPERM;                                                  \
818                                                                                 \
819         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);                   \
820 }                                                                               \
821                                                                                 \
822 static ssize_t __iscsi_##prefix##_##name##_store(struct se_portal_group *se_tpg,\
823                 const char *page, size_t count)                                 \
824 {                                                                               \
825         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
826                                 struct iscsi_portal_group, tpg_se_tpg);         \
827         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
828                                                                                 \
829         if (!capable(CAP_SYS_ADMIN))                                            \
830                 return -EPERM;                                                  \
831                                                                                 \
832         snprintf(auth->name, sizeof(auth->name), "%s", page);                   \
833         if (!(strncmp("NULL", auth->name, 4)))                                  \
834                 auth->naf_flags &= ~flags;                                      \
835         else                                                                    \
836                 auth->naf_flags |= flags;                                       \
837                                                                                 \
838         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                            \
839             (auth->naf_flags & NAF_PASSWORD_IN_SET))                            \
840                 auth->authenticate_target = 1;                                  \
841         else                                                                    \
842                 auth->authenticate_target = 0;                                  \
843                                                                                 \
844         return count;                                                           \
845 }
846
847 #define DEF_TPG_AUTH_STR(name, flags)                                           \
848         __DEF_TPG_AUTH_STR(tpg_auth, name, flags)                               \
849 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,           \
850                 char *page)                                                     \
851 {                                                                               \
852         return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);         \
853 }                                                                               \
854                                                                                 \
855 static ssize_t iscsi_tpg_auth_##name##_store(struct config_item *item,          \
856                 const char *page, size_t count)                                 \
857 {                                                                               \
858         return __iscsi_tpg_auth_##name##_store(auth_to_tpg(item), page, count); \
859 }                                                                               \
860                                                                                 \
861 CONFIGFS_ATTR(iscsi_tpg_auth_, name);
862
863
864 DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
865 DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
866 DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
867 DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
868
869 #define __DEF_TPG_AUTH_INT(prefix, name)                                        \
870 static ssize_t __iscsi_##prefix##_##name##_show(struct se_portal_group *se_tpg, \
871                 char *page)                                                             \
872 {                                                                               \
873         struct iscsi_portal_group *tpg = container_of(se_tpg,                   \
874                                 struct iscsi_portal_group, tpg_se_tpg);         \
875         struct iscsi_node_auth *auth = &tpg->tpg_demo_auth;                     \
876                                                                                 \
877         if (!capable(CAP_SYS_ADMIN))                                            \
878                 return -EPERM;                                                  \
879                                                                                 \
880         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);                   \
881 }
882
883 #define DEF_TPG_AUTH_INT(name)                                                  \
884         __DEF_TPG_AUTH_INT(tpg_auth, name)                                      \
885 static ssize_t iscsi_tpg_auth_##name##_show(struct config_item *item,           \
886                 char *page) \
887 {                                                                               \
888         return __iscsi_tpg_auth_##name##_show(auth_to_tpg(item), page);         \
889 }                                                                               \
890 CONFIGFS_ATTR_RO(iscsi_tpg_auth_, name);
891
892 DEF_TPG_AUTH_INT(authenticate_target);
893
894 static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
895         &iscsi_tpg_auth_attr_userid,
896         &iscsi_tpg_auth_attr_password,
897         &iscsi_tpg_auth_attr_authenticate_target,
898         &iscsi_tpg_auth_attr_userid_mutual,
899         &iscsi_tpg_auth_attr_password_mutual,
900         NULL,
901 };
902
903 /* End items for lio_target_tpg_auth_cit */
904
905 /* Start items for lio_target_tpg_param_cit */
906
907 #define DEF_TPG_PARAM(name)                                             \
908 static ssize_t iscsi_tpg_param_##name##_show(struct config_item *item,  \
909                 char *page)                                             \
910 {                                                                       \
911         struct se_portal_group *se_tpg = param_to_tpg(item);            \
912         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
913                         struct iscsi_portal_group, tpg_se_tpg);         \
914         struct iscsi_param *param;                                      \
915         ssize_t rb;                                                     \
916                                                                         \
917         if (iscsit_get_tpg(tpg) < 0)                                    \
918                 return -EINVAL;                                         \
919                                                                         \
920         param = iscsi_find_param_from_key(__stringify(name),            \
921                                 tpg->param_list);                       \
922         if (!param) {                                                   \
923                 iscsit_put_tpg(tpg);                                    \
924                 return -EINVAL;                                         \
925         }                                                               \
926         rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);           \
927                                                                         \
928         iscsit_put_tpg(tpg);                                            \
929         return rb;                                                      \
930 }                                                                       \
931 static ssize_t iscsi_tpg_param_##name##_store(struct config_item *item, \
932                 const char *page, size_t count)                         \
933 {                                                                       \
934         struct se_portal_group *se_tpg = param_to_tpg(item);            \
935         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
936                         struct iscsi_portal_group, tpg_se_tpg);         \
937         char *buf;                                                      \
938         int ret, len;                                                   \
939                                                                         \
940         buf = kzalloc(PAGE_SIZE, GFP_KERNEL);                           \
941         if (!buf)                                                       \
942                 return -ENOMEM;                                         \
943         len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);       \
944         if (isspace(buf[len-1]))                                        \
945                 buf[len-1] = '\0'; /* Kill newline */                   \
946                                                                         \
947         if (iscsit_get_tpg(tpg) < 0) {                                  \
948                 kfree(buf);                                             \
949                 return -EINVAL;                                         \
950         }                                                               \
951                                                                         \
952         ret = iscsi_change_param_value(buf, tpg->param_list, 1);        \
953         if (ret < 0)                                                    \
954                 goto out;                                               \
955                                                                         \
956         kfree(buf);                                                     \
957         iscsit_put_tpg(tpg);                                            \
958         return count;                                                   \
959 out:                                                                    \
960         kfree(buf);                                                     \
961         iscsit_put_tpg(tpg);                                            \
962         return -EINVAL;                                                 \
963 }                                                                       \
964 CONFIGFS_ATTR(iscsi_tpg_param_, name)
965
966 DEF_TPG_PARAM(AuthMethod);
967 DEF_TPG_PARAM(HeaderDigest);
968 DEF_TPG_PARAM(DataDigest);
969 DEF_TPG_PARAM(MaxConnections);
970 DEF_TPG_PARAM(TargetAlias);
971 DEF_TPG_PARAM(InitialR2T);
972 DEF_TPG_PARAM(ImmediateData);
973 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
974 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
975 DEF_TPG_PARAM(MaxBurstLength);
976 DEF_TPG_PARAM(FirstBurstLength);
977 DEF_TPG_PARAM(DefaultTime2Wait);
978 DEF_TPG_PARAM(DefaultTime2Retain);
979 DEF_TPG_PARAM(MaxOutstandingR2T);
980 DEF_TPG_PARAM(DataPDUInOrder);
981 DEF_TPG_PARAM(DataSequenceInOrder);
982 DEF_TPG_PARAM(ErrorRecoveryLevel);
983 DEF_TPG_PARAM(IFMarker);
984 DEF_TPG_PARAM(OFMarker);
985 DEF_TPG_PARAM(IFMarkInt);
986 DEF_TPG_PARAM(OFMarkInt);
987
988 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
989         &iscsi_tpg_param_attr_AuthMethod,
990         &iscsi_tpg_param_attr_HeaderDigest,
991         &iscsi_tpg_param_attr_DataDigest,
992         &iscsi_tpg_param_attr_MaxConnections,
993         &iscsi_tpg_param_attr_TargetAlias,
994         &iscsi_tpg_param_attr_InitialR2T,
995         &iscsi_tpg_param_attr_ImmediateData,
996         &iscsi_tpg_param_attr_MaxRecvDataSegmentLength,
997         &iscsi_tpg_param_attr_MaxXmitDataSegmentLength,
998         &iscsi_tpg_param_attr_MaxBurstLength,
999         &iscsi_tpg_param_attr_FirstBurstLength,
1000         &iscsi_tpg_param_attr_DefaultTime2Wait,
1001         &iscsi_tpg_param_attr_DefaultTime2Retain,
1002         &iscsi_tpg_param_attr_MaxOutstandingR2T,
1003         &iscsi_tpg_param_attr_DataPDUInOrder,
1004         &iscsi_tpg_param_attr_DataSequenceInOrder,
1005         &iscsi_tpg_param_attr_ErrorRecoveryLevel,
1006         &iscsi_tpg_param_attr_IFMarker,
1007         &iscsi_tpg_param_attr_OFMarker,
1008         &iscsi_tpg_param_attr_IFMarkInt,
1009         &iscsi_tpg_param_attr_OFMarkInt,
1010         NULL,
1011 };
1012
1013 /* End items for lio_target_tpg_param_cit */
1014
1015 /* Start items for lio_target_tpg_cit */
1016
1017 static ssize_t lio_target_tpg_enable_show(struct config_item *item, char *page)
1018 {
1019         struct se_portal_group *se_tpg = to_tpg(item);
1020         struct iscsi_portal_group *tpg = container_of(se_tpg,
1021                         struct iscsi_portal_group, tpg_se_tpg);
1022         ssize_t len;
1023
1024         spin_lock(&tpg->tpg_state_lock);
1025         len = sprintf(page, "%d\n",
1026                         (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1027         spin_unlock(&tpg->tpg_state_lock);
1028
1029         return len;
1030 }
1031
1032 static ssize_t lio_target_tpg_enable_store(struct config_item *item,
1033                 const char *page, size_t count)
1034 {
1035         struct se_portal_group *se_tpg = to_tpg(item);
1036         struct iscsi_portal_group *tpg = container_of(se_tpg,
1037                         struct iscsi_portal_group, tpg_se_tpg);
1038         u32 op;
1039         int ret;
1040
1041         ret = kstrtou32(page, 0, &op);
1042         if (ret)
1043                 return ret;
1044         if ((op != 1) && (op != 0)) {
1045                 pr_err("Illegal value for tpg_enable: %u\n", op);
1046                 return -EINVAL;
1047         }
1048
1049         ret = iscsit_get_tpg(tpg);
1050         if (ret < 0)
1051                 return -EINVAL;
1052
1053         if (op) {
1054                 ret = iscsit_tpg_enable_portal_group(tpg);
1055                 if (ret < 0)
1056                         goto out;
1057         } else {
1058                 /*
1059                  * iscsit_tpg_disable_portal_group() assumes force=1
1060                  */
1061                 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1062                 if (ret < 0)
1063                         goto out;
1064         }
1065
1066         iscsit_put_tpg(tpg);
1067         return count;
1068 out:
1069         iscsit_put_tpg(tpg);
1070         return -EINVAL;
1071 }
1072
1073
1074 static ssize_t lio_target_tpg_dynamic_sessions_show(struct config_item *item,
1075                 char *page)
1076 {
1077         return target_show_dynamic_sessions(to_tpg(item), page);
1078 }
1079
1080 CONFIGFS_ATTR(lio_target_tpg_, enable);
1081 CONFIGFS_ATTR_RO(lio_target_tpg_, dynamic_sessions);
1082
1083 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1084         &lio_target_tpg_attr_enable,
1085         &lio_target_tpg_attr_dynamic_sessions,
1086         NULL,
1087 };
1088
1089 /* End items for lio_target_tpg_cit */
1090
1091 /* Start items for lio_target_tiqn_cit */
1092
1093 static struct se_portal_group *lio_target_tiqn_addtpg(struct se_wwn *wwn,
1094                                                       const char *name)
1095 {
1096         struct iscsi_portal_group *tpg;
1097         struct iscsi_tiqn *tiqn;
1098         char *tpgt_str;
1099         int ret;
1100         u16 tpgt;
1101
1102         tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1103         /*
1104          * Only tpgt_# directory groups can be created below
1105          * target/iscsi/iqn.superturodiskarry/
1106          */
1107         tpgt_str = strstr(name, "tpgt_");
1108         if (!tpgt_str) {
1109                 pr_err("Unable to locate \"tpgt_#\" directory"
1110                                 " group\n");
1111                 return NULL;
1112         }
1113         tpgt_str += 5; /* Skip ahead of "tpgt_" */
1114         ret = kstrtou16(tpgt_str, 0, &tpgt);
1115         if (ret)
1116                 return NULL;
1117
1118         tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1119         if (!tpg)
1120                 return NULL;
1121
1122         ret = core_tpg_register(wwn, &tpg->tpg_se_tpg, SCSI_PROTOCOL_ISCSI);
1123         if (ret < 0)
1124                 goto free_out;
1125
1126         ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1127         if (ret != 0)
1128                 goto out;
1129
1130         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1131         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1132                         name);
1133         return &tpg->tpg_se_tpg;
1134 out:
1135         core_tpg_deregister(&tpg->tpg_se_tpg);
1136 free_out:
1137         kfree(tpg);
1138         return NULL;
1139 }
1140
1141 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1142 {
1143         struct iscsi_portal_group *tpg;
1144         struct iscsi_tiqn *tiqn;
1145
1146         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1147         tiqn = tpg->tpg_tiqn;
1148         /*
1149          * iscsit_tpg_del_portal_group() assumes force=1
1150          */
1151         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1152         iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1153 }
1154
1155 /* End items for lio_target_tiqn_cit */
1156
1157 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1158
1159 static ssize_t lio_target_wwn_lio_version_show(struct config_item *item,
1160                 char *page)
1161 {
1162         return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
1163 }
1164
1165 CONFIGFS_ATTR_RO(lio_target_wwn_, lio_version);
1166
1167 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1168         &lio_target_wwn_attr_lio_version,
1169         NULL,
1170 };
1171
1172 static struct se_wwn *lio_target_call_coreaddtiqn(
1173         struct target_fabric_configfs *tf,
1174         struct config_group *group,
1175         const char *name)
1176 {
1177         struct iscsi_tiqn *tiqn;
1178
1179         tiqn = iscsit_add_tiqn((unsigned char *)name);
1180         if (IS_ERR(tiqn))
1181                 return ERR_CAST(tiqn);
1182
1183         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1184         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1185                         " %s\n", name);
1186         return &tiqn->tiqn_wwn;
1187 }
1188
1189 static void lio_target_add_wwn_groups(struct se_wwn *wwn)
1190 {
1191         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1192
1193         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1194                         "iscsi_instance", &iscsi_stat_instance_cit);
1195         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_instance_group,
1196                         &tiqn->tiqn_wwn.fabric_stat_group);
1197
1198         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1199                         "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1200         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
1201                         &tiqn->tiqn_wwn.fabric_stat_group);
1202
1203         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1204                         "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1205         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
1206                         &tiqn->tiqn_wwn.fabric_stat_group);
1207
1208         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1209                         "iscsi_login_stats", &iscsi_stat_login_cit);
1210         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
1211                         &tiqn->tiqn_wwn.fabric_stat_group);
1212
1213         config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1214                         "iscsi_logout_stats", &iscsi_stat_logout_cit);
1215         configfs_add_default_group(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
1216                         &tiqn->tiqn_wwn.fabric_stat_group);
1217 }
1218
1219 static void lio_target_call_coredeltiqn(
1220         struct se_wwn *wwn)
1221 {
1222         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1223
1224         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1225                         tiqn->tiqn);
1226         iscsit_del_tiqn(tiqn);
1227 }
1228
1229 /* End LIO-Target TIQN struct contig_lio_target_cit */
1230
1231 /* Start lio_target_discovery_auth_cit */
1232
1233 #define DEF_DISC_AUTH_STR(name, flags)                                  \
1234         __DEF_NACL_AUTH_STR(disc, name, flags)                          \
1235 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1236 {                                                                       \
1237         return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl,\
1238                 page);                                                  \
1239 }                                                                       \
1240 static ssize_t iscsi_disc_##name##_store(struct config_item *item,      \
1241                 const char *page, size_t count)                         \
1242 {                                                                       \
1243         return __iscsi_disc_##name##_store(&iscsit_global->discovery_acl,       \
1244                 page, count);                                           \
1245                                                                         \
1246 }                                                                       \
1247 CONFIGFS_ATTR(iscsi_disc_, name)
1248
1249 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1250 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1251 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1252 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1253
1254 #define DEF_DISC_AUTH_INT(name)                                         \
1255         __DEF_NACL_AUTH_INT(disc, name)                                 \
1256 static ssize_t iscsi_disc_##name##_show(struct config_item *item, char *page) \
1257 {                                                                       \
1258         return __iscsi_disc_##name##_show(&iscsit_global->discovery_acl, \
1259                         page);                                          \
1260 }                                                                       \
1261 CONFIGFS_ATTR_RO(iscsi_disc_, name)
1262
1263 DEF_DISC_AUTH_INT(authenticate_target);
1264
1265
1266 static ssize_t iscsi_disc_enforce_discovery_auth_show(struct config_item *item,
1267                 char *page)
1268 {
1269         struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1270
1271         return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1272 }
1273
1274 static ssize_t iscsi_disc_enforce_discovery_auth_store(struct config_item *item,
1275                 const char *page, size_t count)
1276 {
1277         struct iscsi_param *param;
1278         struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1279         u32 op;
1280         int err;
1281
1282         err = kstrtou32(page, 0, &op);
1283         if (err)
1284                 return -EINVAL;
1285         if ((op != 1) && (op != 0)) {
1286                 pr_err("Illegal value for enforce_discovery_auth:"
1287                                 " %u\n", op);
1288                 return -EINVAL;
1289         }
1290
1291         if (!discovery_tpg) {
1292                 pr_err("iscsit_global->discovery_tpg is NULL\n");
1293                 return -EINVAL;
1294         }
1295
1296         param = iscsi_find_param_from_key(AUTHMETHOD,
1297                                 discovery_tpg->param_list);
1298         if (!param)
1299                 return -EINVAL;
1300
1301         if (op) {
1302                 /*
1303                  * Reset the AuthMethod key to CHAP.
1304                  */
1305                 if (iscsi_update_param_value(param, CHAP) < 0)
1306                         return -EINVAL;
1307
1308                 discovery_tpg->tpg_attrib.authentication = 1;
1309                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1310                 pr_debug("LIO-CORE[0] Successfully enabled"
1311                         " authentication enforcement for iSCSI"
1312                         " Discovery TPG\n");
1313         } else {
1314                 /*
1315                  * Reset the AuthMethod key to CHAP,None
1316                  */
1317                 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1318                         return -EINVAL;
1319
1320                 discovery_tpg->tpg_attrib.authentication = 0;
1321                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1322                 pr_debug("LIO-CORE[0] Successfully disabled"
1323                         " authentication enforcement for iSCSI"
1324                         " Discovery TPG\n");
1325         }
1326
1327         return count;
1328 }
1329
1330 CONFIGFS_ATTR(iscsi_disc_, enforce_discovery_auth);
1331
1332 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1333         &iscsi_disc_attr_userid,
1334         &iscsi_disc_attr_password,
1335         &iscsi_disc_attr_authenticate_target,
1336         &iscsi_disc_attr_userid_mutual,
1337         &iscsi_disc_attr_password_mutual,
1338         &iscsi_disc_attr_enforce_discovery_auth,
1339         NULL,
1340 };
1341
1342 /* End lio_target_discovery_auth_cit */
1343
1344 /* Start functions for target_core_fabric_ops */
1345
1346 static char *iscsi_get_fabric_name(void)
1347 {
1348         return "iSCSI";
1349 }
1350
1351 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1352 {
1353         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1354
1355         return cmd->i_state;
1356 }
1357
1358 static u32 lio_sess_get_index(struct se_session *se_sess)
1359 {
1360         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1361
1362         return sess->session_index;
1363 }
1364
1365 static u32 lio_sess_get_initiator_sid(
1366         struct se_session *se_sess,
1367         unsigned char *buf,
1368         u32 size)
1369 {
1370         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1371         /*
1372          * iSCSI Initiator Session Identifier from RFC-3720.
1373          */
1374         return snprintf(buf, size, "%6phN", sess->isid);
1375 }
1376
1377 static int lio_queue_data_in(struct se_cmd *se_cmd)
1378 {
1379         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1380         struct iscsi_conn *conn = cmd->conn;
1381
1382         cmd->i_state = ISTATE_SEND_DATAIN;
1383         return conn->conn_transport->iscsit_queue_data_in(conn, cmd);
1384 }
1385
1386 static int lio_write_pending(struct se_cmd *se_cmd)
1387 {
1388         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1389         struct iscsi_conn *conn = cmd->conn;
1390
1391         if (!cmd->immediate_data && !cmd->unsolicited_data)
1392                 return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1393
1394         return 0;
1395 }
1396
1397 static int lio_write_pending_status(struct se_cmd *se_cmd)
1398 {
1399         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1400         int ret;
1401
1402         spin_lock_bh(&cmd->istate_lock);
1403         ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1404         spin_unlock_bh(&cmd->istate_lock);
1405
1406         return ret;
1407 }
1408
1409 static int lio_queue_status(struct se_cmd *se_cmd)
1410 {
1411         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1412         struct iscsi_conn *conn = cmd->conn;
1413
1414         cmd->i_state = ISTATE_SEND_STATUS;
1415
1416         if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
1417                 return iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1418         }
1419         return conn->conn_transport->iscsit_queue_status(conn, cmd);
1420 }
1421
1422 static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
1423 {
1424         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1425
1426         cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1427         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1428 }
1429
1430 static void lio_aborted_task(struct se_cmd *se_cmd)
1431 {
1432         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1433
1434         cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
1435 }
1436
1437 static inline struct iscsi_portal_group *iscsi_tpg(struct se_portal_group *se_tpg)
1438 {
1439         return container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1440 }
1441
1442 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1443 {
1444         return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn;
1445 }
1446
1447 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1448 {
1449         return iscsi_tpg(se_tpg)->tpgt;
1450 }
1451
1452 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1453 {
1454         return iscsi_tpg(se_tpg)->tpg_attrib.default_cmdsn_depth;
1455 }
1456
1457 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1458 {
1459         return iscsi_tpg(se_tpg)->tpg_attrib.generate_node_acls;
1460 }
1461
1462 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1463 {
1464         return iscsi_tpg(se_tpg)->tpg_attrib.cache_dynamic_acls;
1465 }
1466
1467 static int lio_tpg_check_demo_mode_write_protect(
1468         struct se_portal_group *se_tpg)
1469 {
1470         return iscsi_tpg(se_tpg)->tpg_attrib.demo_mode_write_protect;
1471 }
1472
1473 static int lio_tpg_check_prod_mode_write_protect(
1474         struct se_portal_group *se_tpg)
1475 {
1476         return iscsi_tpg(se_tpg)->tpg_attrib.prod_mode_write_protect;
1477 }
1478
1479 static int lio_tpg_check_prot_fabric_only(
1480         struct se_portal_group *se_tpg)
1481 {
1482         /*
1483          * Only report fabric_prot_type if t10_pi has also been enabled
1484          * for incoming ib_isert sessions.
1485          */
1486         if (!iscsi_tpg(se_tpg)->tpg_attrib.t10_pi)
1487                 return 0;
1488         return iscsi_tpg(se_tpg)->tpg_attrib.fabric_prot_type;
1489 }
1490
1491 /*
1492  * This function calls iscsit_inc_session_usage_count() on the
1493  * struct iscsi_session in question.
1494  */
1495 static void lio_tpg_close_session(struct se_session *se_sess)
1496 {
1497         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1498         struct se_portal_group *se_tpg = &sess->tpg->tpg_se_tpg;
1499
1500         spin_lock_bh(&se_tpg->session_lock);
1501         spin_lock(&sess->conn_lock);
1502         if (atomic_read(&sess->session_fall_back_to_erl0) ||
1503             atomic_read(&sess->session_logout) ||
1504             atomic_read(&sess->session_close) ||
1505             (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1506                 spin_unlock(&sess->conn_lock);
1507                 spin_unlock_bh(&se_tpg->session_lock);
1508                 return;
1509         }
1510         iscsit_inc_session_usage_count(sess);
1511         atomic_set(&sess->session_reinstatement, 1);
1512         atomic_set(&sess->session_fall_back_to_erl0, 1);
1513         atomic_set(&sess->session_close, 1);
1514         spin_unlock(&sess->conn_lock);
1515
1516         iscsit_stop_time2retain_timer(sess);
1517         spin_unlock_bh(&se_tpg->session_lock);
1518
1519         iscsit_stop_session(sess, 1, 1);
1520         iscsit_dec_session_usage_count(sess);
1521 }
1522
1523 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1524 {
1525         return iscsi_tpg(se_tpg)->tpg_tiqn->tiqn_index;
1526 }
1527
1528 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1529 {
1530         struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1531                                 se_node_acl);
1532         struct se_portal_group *se_tpg = se_acl->se_tpg;
1533         struct iscsi_portal_group *tpg = container_of(se_tpg,
1534                                 struct iscsi_portal_group, tpg_se_tpg);
1535
1536         acl->node_attrib.nacl = acl;
1537         iscsit_set_default_node_attribues(acl, tpg);
1538 }
1539
1540 static int lio_check_stop_free(struct se_cmd *se_cmd)
1541 {
1542         return target_put_sess_cmd(se_cmd);
1543 }
1544
1545 static void lio_release_cmd(struct se_cmd *se_cmd)
1546 {
1547         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1548
1549         pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
1550         iscsit_release_cmd(cmd);
1551 }
1552
1553 const struct target_core_fabric_ops iscsi_ops = {
1554         .module                         = THIS_MODULE,
1555         .name                           = "iscsi",
1556         .node_acl_size                  = sizeof(struct iscsi_node_acl),
1557         .get_fabric_name                = iscsi_get_fabric_name,
1558         .tpg_get_wwn                    = lio_tpg_get_endpoint_wwn,
1559         .tpg_get_tag                    = lio_tpg_get_tag,
1560         .tpg_get_default_depth          = lio_tpg_get_default_depth,
1561         .tpg_check_demo_mode            = lio_tpg_check_demo_mode,
1562         .tpg_check_demo_mode_cache      = lio_tpg_check_demo_mode_cache,
1563         .tpg_check_demo_mode_write_protect =
1564                         lio_tpg_check_demo_mode_write_protect,
1565         .tpg_check_prod_mode_write_protect =
1566                         lio_tpg_check_prod_mode_write_protect,
1567         .tpg_check_prot_fabric_only     = &lio_tpg_check_prot_fabric_only,
1568         .tpg_get_inst_index             = lio_tpg_get_inst_index,
1569         .check_stop_free                = lio_check_stop_free,
1570         .release_cmd                    = lio_release_cmd,
1571         .close_session                  = lio_tpg_close_session,
1572         .sess_get_index                 = lio_sess_get_index,
1573         .sess_get_initiator_sid         = lio_sess_get_initiator_sid,
1574         .write_pending                  = lio_write_pending,
1575         .write_pending_status           = lio_write_pending_status,
1576         .set_default_node_attributes    = lio_set_default_node_attributes,
1577         .get_cmd_state                  = iscsi_get_cmd_state,
1578         .queue_data_in                  = lio_queue_data_in,
1579         .queue_status                   = lio_queue_status,
1580         .queue_tm_rsp                   = lio_queue_tm_rsp,
1581         .aborted_task                   = lio_aborted_task,
1582         .fabric_make_wwn                = lio_target_call_coreaddtiqn,
1583         .fabric_drop_wwn                = lio_target_call_coredeltiqn,
1584         .add_wwn_groups                 = lio_target_add_wwn_groups,
1585         .fabric_make_tpg                = lio_target_tiqn_addtpg,
1586         .fabric_drop_tpg                = lio_target_tiqn_deltpg,
1587         .fabric_make_np                 = lio_target_call_addnptotpg,
1588         .fabric_drop_np                 = lio_target_call_delnpfromtpg,
1589         .fabric_init_nodeacl            = lio_target_init_nodeacl,
1590
1591         .tfc_discovery_attrs            = lio_target_discovery_auth_attrs,
1592         .tfc_wwn_attrs                  = lio_target_wwn_attrs,
1593         .tfc_tpg_base_attrs             = lio_target_tpg_attrs,
1594         .tfc_tpg_attrib_attrs           = lio_target_tpg_attrib_attrs,
1595         .tfc_tpg_auth_attrs             = lio_target_tpg_auth_attrs,
1596         .tfc_tpg_param_attrs            = lio_target_tpg_param_attrs,
1597         .tfc_tpg_np_base_attrs          = lio_target_portal_attrs,
1598         .tfc_tpg_nacl_base_attrs        = lio_target_initiator_attrs,
1599         .tfc_tpg_nacl_attrib_attrs      = lio_target_nacl_attrib_attrs,
1600         .tfc_tpg_nacl_auth_attrs        = lio_target_nacl_auth_attrs,
1601         .tfc_tpg_nacl_param_attrs       = lio_target_nacl_param_attrs,
1602 };