]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/target/iscsi/iscsi_target_configfs.c
mm/bootmem.c: remove unused wrapper function reserve_bootmem_generic()
[can-eth-gw-linux.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  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6  *
7  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8  *
9  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  ****************************************************************************/
21
22 #include <linux/configfs.h>
23 #include <linux/export.h>
24 #include <linux/inet.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_fabric.h>
27 #include <target/target_core_fabric_configfs.h>
28 #include <target/target_core_configfs.h>
29 #include <target/configfs_macros.h>
30
31 #include "iscsi_target_core.h"
32 #include "iscsi_target_parameters.h"
33 #include "iscsi_target_device.h"
34 #include "iscsi_target_erl0.h"
35 #include "iscsi_target_nodeattrib.h"
36 #include "iscsi_target_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39 #include "iscsi_target_stat.h"
40 #include "iscsi_target_configfs.h"
41
42 struct target_fabric_configfs *lio_target_fabric_configfs;
43
44 struct lio_target_configfs_attribute {
45         struct configfs_attribute attr;
46         ssize_t (*show)(void *, char *);
47         ssize_t (*store)(void *, const char *, size_t);
48 };
49
50 /* Start items for lio_target_portal_cit */
51
52 static ssize_t lio_target_np_show_sctp(
53         struct se_tpg_np *se_tpg_np,
54         char *page)
55 {
56         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
57                                 struct iscsi_tpg_np, se_tpg_np);
58         struct iscsi_tpg_np *tpg_np_sctp;
59         ssize_t rb;
60
61         tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
62         if (tpg_np_sctp)
63                 rb = sprintf(page, "1\n");
64         else
65                 rb = sprintf(page, "0\n");
66
67         return rb;
68 }
69
70 static ssize_t lio_target_np_store_sctp(
71         struct se_tpg_np *se_tpg_np,
72         const char *page,
73         size_t count)
74 {
75         struct iscsi_np *np;
76         struct iscsi_portal_group *tpg;
77         struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
78                                 struct iscsi_tpg_np, se_tpg_np);
79         struct iscsi_tpg_np *tpg_np_sctp = NULL;
80         char *endptr;
81         u32 op;
82         int ret;
83
84         op = simple_strtoul(page, &endptr, 0);
85         if ((op != 1) && (op != 0)) {
86                 pr_err("Illegal value for tpg_enable: %u\n", op);
87                 return -EINVAL;
88         }
89         np = tpg_np->tpg_np;
90         if (!np) {
91                 pr_err("Unable to locate struct iscsi_np from"
92                                 " struct iscsi_tpg_np\n");
93                 return -EINVAL;
94         }
95
96         tpg = tpg_np->tpg;
97         if (iscsit_get_tpg(tpg) < 0)
98                 return -EINVAL;
99
100         if (op) {
101                 /*
102                  * Use existing np->np_sockaddr for SCTP network portal reference
103                  */
104                 tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
105                                         np->np_ip, tpg_np, ISCSI_SCTP_TCP);
106                 if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
107                         goto out;
108         } else {
109                 tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
110                 if (!tpg_np_sctp)
111                         goto out;
112
113                 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
114                 if (ret < 0)
115                         goto out;
116         }
117
118         iscsit_put_tpg(tpg);
119         return count;
120 out:
121         iscsit_put_tpg(tpg);
122         return -EINVAL;
123 }
124
125 TF_NP_BASE_ATTR(lio_target, sctp, S_IRUGO | S_IWUSR);
126
127 static struct configfs_attribute *lio_target_portal_attrs[] = {
128         &lio_target_np_sctp.attr,
129         NULL,
130 };
131
132 /* Stop items for lio_target_portal_cit */
133
134 /* Start items for lio_target_np_cit */
135
136 #define MAX_PORTAL_LEN          256
137
138 static struct se_tpg_np *lio_target_call_addnptotpg(
139         struct se_portal_group *se_tpg,
140         struct config_group *group,
141         const char *name)
142 {
143         struct iscsi_portal_group *tpg;
144         struct iscsi_tpg_np *tpg_np;
145         char *str, *str2, *ip_str, *port_str;
146         struct __kernel_sockaddr_storage sockaddr;
147         struct sockaddr_in *sock_in;
148         struct sockaddr_in6 *sock_in6;
149         unsigned long port;
150         int ret;
151         char buf[MAX_PORTAL_LEN + 1];
152
153         if (strlen(name) > MAX_PORTAL_LEN) {
154                 pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
155                         (int)strlen(name), MAX_PORTAL_LEN);
156                 return ERR_PTR(-EOVERFLOW);
157         }
158         memset(buf, 0, MAX_PORTAL_LEN + 1);
159         snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
160
161         memset(&sockaddr, 0, sizeof(struct __kernel_sockaddr_storage));
162
163         str = strstr(buf, "[");
164         if (str) {
165                 const char *end;
166
167                 str2 = strstr(str, "]");
168                 if (!str2) {
169                         pr_err("Unable to locate trailing \"]\""
170                                 " in IPv6 iSCSI network portal address\n");
171                         return ERR_PTR(-EINVAL);
172                 }
173                 str++; /* Skip over leading "[" */
174                 *str2 = '\0'; /* Terminate the IPv6 address */
175                 str2++; /* Skip over the "]" */
176                 port_str = strstr(str2, ":");
177                 if (!port_str) {
178                         pr_err("Unable to locate \":port\""
179                                 " in IPv6 iSCSI network portal address\n");
180                         return ERR_PTR(-EINVAL);
181                 }
182                 *port_str = '\0'; /* Terminate string for IP */
183                 port_str++; /* Skip over ":" */
184
185                 ret = strict_strtoul(port_str, 0, &port);
186                 if (ret < 0) {
187                         pr_err("strict_strtoul() failed for port_str: %d\n", ret);
188                         return ERR_PTR(ret);
189                 }
190                 sock_in6 = (struct sockaddr_in6 *)&sockaddr;
191                 sock_in6->sin6_family = AF_INET6;
192                 sock_in6->sin6_port = htons((unsigned short)port);
193                 ret = in6_pton(str, IPV6_ADDRESS_SPACE,
194                                 (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
195                 if (ret <= 0) {
196                         pr_err("in6_pton returned: %d\n", ret);
197                         return ERR_PTR(-EINVAL);
198                 }
199         } else {
200                 str = ip_str = &buf[0];
201                 port_str = strstr(ip_str, ":");
202                 if (!port_str) {
203                         pr_err("Unable to locate \":port\""
204                                 " in IPv4 iSCSI network portal address\n");
205                         return ERR_PTR(-EINVAL);
206                 }
207                 *port_str = '\0'; /* Terminate string for IP */
208                 port_str++; /* Skip over ":" */
209
210                 ret = strict_strtoul(port_str, 0, &port);
211                 if (ret < 0) {
212                         pr_err("strict_strtoul() failed for port_str: %d\n", ret);
213                         return ERR_PTR(ret);
214                 }
215                 sock_in = (struct sockaddr_in *)&sockaddr;
216                 sock_in->sin_family = AF_INET;
217                 sock_in->sin_port = htons((unsigned short)port);
218                 sock_in->sin_addr.s_addr = in_aton(ip_str);
219         }
220         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
221         ret = iscsit_get_tpg(tpg);
222         if (ret < 0)
223                 return ERR_PTR(-EINVAL);
224
225         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
226                 " PORTAL: %s\n",
227                 config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
228                 tpg->tpgt, name);
229         /*
230          * Assume ISCSI_TCP by default.  Other network portals for other
231          * iSCSI fabrics:
232          *
233          * Traditional iSCSI over SCTP (initial support)
234          * iSER/TCP (TODO, hardware available)
235          * iSER/SCTP (TODO, software emulation with osc-iwarp)
236          * iSER/IB (TODO, hardware available)
237          *
238          * can be enabled with atributes under
239          * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
240          *
241          */
242         tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, str, NULL,
243                                 ISCSI_TCP);
244         if (IS_ERR(tpg_np)) {
245                 iscsit_put_tpg(tpg);
246                 return ERR_CAST(tpg_np);
247         }
248         pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
249
250         iscsit_put_tpg(tpg);
251         return &tpg_np->se_tpg_np;
252 }
253
254 static void lio_target_call_delnpfromtpg(
255         struct se_tpg_np *se_tpg_np)
256 {
257         struct iscsi_portal_group *tpg;
258         struct iscsi_tpg_np *tpg_np;
259         struct se_portal_group *se_tpg;
260         int ret;
261
262         tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
263         tpg = tpg_np->tpg;
264         ret = iscsit_get_tpg(tpg);
265         if (ret < 0)
266                 return;
267
268         se_tpg = &tpg->tpg_se_tpg;
269         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
270                 " PORTAL: %s:%hu\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
271                 tpg->tpgt, tpg_np->tpg_np->np_ip, tpg_np->tpg_np->np_port);
272
273         ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
274         if (ret < 0)
275                 goto out;
276
277         pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
278 out:
279         iscsit_put_tpg(tpg);
280 }
281
282 /* End items for lio_target_np_cit */
283
284 /* Start items for lio_target_nacl_attrib_cit */
285
286 #define DEF_NACL_ATTRIB(name)                                           \
287 static ssize_t iscsi_nacl_attrib_show_##name(                           \
288         struct se_node_acl *se_nacl,                                    \
289         char *page)                                                     \
290 {                                                                       \
291         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
292                                         se_node_acl);                   \
293                                                                         \
294         return sprintf(page, "%u\n", ISCSI_NODE_ATTRIB(nacl)->name);    \
295 }                                                                       \
296                                                                         \
297 static ssize_t iscsi_nacl_attrib_store_##name(                          \
298         struct se_node_acl *se_nacl,                                    \
299         const char *page,                                               \
300         size_t count)                                                   \
301 {                                                                       \
302         struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
303                                         se_node_acl);                   \
304         char *endptr;                                                   \
305         u32 val;                                                        \
306         int ret;                                                        \
307                                                                         \
308         val = simple_strtoul(page, &endptr, 0);                         \
309         ret = iscsit_na_##name(nacl, val);                              \
310         if (ret < 0)                                                    \
311                 return ret;                                             \
312                                                                         \
313         return count;                                                   \
314 }
315
316 #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
317 /*
318  * Define iscsi_node_attrib_s_dataout_timeout
319  */
320 DEF_NACL_ATTRIB(dataout_timeout);
321 NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
322 /*
323  * Define iscsi_node_attrib_s_dataout_timeout_retries
324  */
325 DEF_NACL_ATTRIB(dataout_timeout_retries);
326 NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
327 /*
328  * Define iscsi_node_attrib_s_default_erl
329  */
330 DEF_NACL_ATTRIB(default_erl);
331 NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
332 /*
333  * Define iscsi_node_attrib_s_nopin_timeout
334  */
335 DEF_NACL_ATTRIB(nopin_timeout);
336 NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
337 /*
338  * Define iscsi_node_attrib_s_nopin_response_timeout
339  */
340 DEF_NACL_ATTRIB(nopin_response_timeout);
341 NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
342 /*
343  * Define iscsi_node_attrib_s_random_datain_pdu_offsets
344  */
345 DEF_NACL_ATTRIB(random_datain_pdu_offsets);
346 NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
347 /*
348  * Define iscsi_node_attrib_s_random_datain_seq_offsets
349  */
350 DEF_NACL_ATTRIB(random_datain_seq_offsets);
351 NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
352 /*
353  * Define iscsi_node_attrib_s_random_r2t_offsets
354  */
355 DEF_NACL_ATTRIB(random_r2t_offsets);
356 NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
357
358 static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
359         &iscsi_nacl_attrib_dataout_timeout.attr,
360         &iscsi_nacl_attrib_dataout_timeout_retries.attr,
361         &iscsi_nacl_attrib_default_erl.attr,
362         &iscsi_nacl_attrib_nopin_timeout.attr,
363         &iscsi_nacl_attrib_nopin_response_timeout.attr,
364         &iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
365         &iscsi_nacl_attrib_random_datain_seq_offsets.attr,
366         &iscsi_nacl_attrib_random_r2t_offsets.attr,
367         NULL,
368 };
369
370 /* End items for lio_target_nacl_attrib_cit */
371
372 /* Start items for lio_target_nacl_auth_cit */
373
374 #define __DEF_NACL_AUTH_STR(prefix, name, flags)                        \
375 static ssize_t __iscsi_##prefix##_show_##name(                          \
376         struct iscsi_node_acl *nacl,                                    \
377         char *page)                                                     \
378 {                                                                       \
379         struct iscsi_node_auth *auth = &nacl->node_auth;                \
380                                                                         \
381         if (!capable(CAP_SYS_ADMIN))                                    \
382                 return -EPERM;                                          \
383         return snprintf(page, PAGE_SIZE, "%s\n", auth->name);           \
384 }                                                                       \
385                                                                         \
386 static ssize_t __iscsi_##prefix##_store_##name(                         \
387         struct iscsi_node_acl *nacl,                                    \
388         const char *page,                                               \
389         size_t count)                                                   \
390 {                                                                       \
391         struct iscsi_node_auth *auth = &nacl->node_auth;                \
392                                                                         \
393         if (!capable(CAP_SYS_ADMIN))                                    \
394                 return -EPERM;                                          \
395                                                                         \
396         snprintf(auth->name, PAGE_SIZE, "%s", page);                    \
397         if (!strncmp("NULL", auth->name, 4))                            \
398                 auth->naf_flags &= ~flags;                              \
399         else                                                            \
400                 auth->naf_flags |= flags;                               \
401                                                                         \
402         if ((auth->naf_flags & NAF_USERID_IN_SET) &&                    \
403             (auth->naf_flags & NAF_PASSWORD_IN_SET))                    \
404                 auth->authenticate_target = 1;                          \
405         else                                                            \
406                 auth->authenticate_target = 0;                          \
407                                                                         \
408         return count;                                                   \
409 }
410
411 #define __DEF_NACL_AUTH_INT(prefix, name)                               \
412 static ssize_t __iscsi_##prefix##_show_##name(                          \
413         struct iscsi_node_acl *nacl,                                    \
414         char *page)                                                     \
415 {                                                                       \
416         struct iscsi_node_auth *auth = &nacl->node_auth;                \
417                                                                         \
418         if (!capable(CAP_SYS_ADMIN))                                    \
419                 return -EPERM;                                          \
420                                                                         \
421         return snprintf(page, PAGE_SIZE, "%d\n", auth->name);           \
422 }
423
424 #define DEF_NACL_AUTH_STR(name, flags)                                  \
425         __DEF_NACL_AUTH_STR(nacl_auth, name, flags)                     \
426 static ssize_t iscsi_nacl_auth_show_##name(                             \
427         struct se_node_acl *nacl,                                       \
428         char *page)                                                     \
429 {                                                                       \
430         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
431                         struct iscsi_node_acl, se_node_acl), page);             \
432 }                                                                       \
433 static ssize_t iscsi_nacl_auth_store_##name(                            \
434         struct se_node_acl *nacl,                                       \
435         const char *page,                                               \
436         size_t count)                                                   \
437 {                                                                       \
438         return __iscsi_nacl_auth_store_##name(container_of(nacl,        \
439                         struct iscsi_node_acl, se_node_acl), page, count);      \
440 }
441
442 #define DEF_NACL_AUTH_INT(name)                                         \
443         __DEF_NACL_AUTH_INT(nacl_auth, name)                            \
444 static ssize_t iscsi_nacl_auth_show_##name(                             \
445         struct se_node_acl *nacl,                                       \
446         char *page)                                                     \
447 {                                                                       \
448         return __iscsi_nacl_auth_show_##name(container_of(nacl,         \
449                         struct iscsi_node_acl, se_node_acl), page);             \
450 }
451
452 #define AUTH_ATTR(_name, _mode) TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
453 #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
454
455 /*
456  * One-way authentication userid
457  */
458 DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
459 AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
460 /*
461  * One-way authentication password
462  */
463 DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
464 AUTH_ATTR(password, S_IRUGO | S_IWUSR);
465 /*
466  * Enforce mutual authentication
467  */
468 DEF_NACL_AUTH_INT(authenticate_target);
469 AUTH_ATTR_RO(authenticate_target);
470 /*
471  * Mutual authentication userid
472  */
473 DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
474 AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
475 /*
476  * Mutual authentication password
477  */
478 DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
479 AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
480
481 static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
482         &iscsi_nacl_auth_userid.attr,
483         &iscsi_nacl_auth_password.attr,
484         &iscsi_nacl_auth_authenticate_target.attr,
485         &iscsi_nacl_auth_userid_mutual.attr,
486         &iscsi_nacl_auth_password_mutual.attr,
487         NULL,
488 };
489
490 /* End items for lio_target_nacl_auth_cit */
491
492 /* Start items for lio_target_nacl_param_cit */
493
494 #define DEF_NACL_PARAM(name)                                            \
495 static ssize_t iscsi_nacl_param_show_##name(                            \
496         struct se_node_acl *se_nacl,                                    \
497         char *page)                                                     \
498 {                                                                       \
499         struct iscsi_session *sess;                                     \
500         struct se_session *se_sess;                                     \
501         ssize_t rb;                                                     \
502                                                                         \
503         spin_lock_bh(&se_nacl->nacl_sess_lock);                         \
504         se_sess = se_nacl->nacl_sess;                                   \
505         if (!se_sess) {                                                 \
506                 rb = snprintf(page, PAGE_SIZE,                          \
507                         "No Active iSCSI Session\n");                   \
508         } else {                                                        \
509                 sess = se_sess->fabric_sess_ptr;                        \
510                 rb = snprintf(page, PAGE_SIZE, "%u\n",                  \
511                         (u32)sess->sess_ops->name);                     \
512         }                                                               \
513         spin_unlock_bh(&se_nacl->nacl_sess_lock);                       \
514                                                                         \
515         return rb;                                                      \
516 }
517
518 #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
519
520 DEF_NACL_PARAM(MaxConnections);
521 NACL_PARAM_ATTR(MaxConnections);
522
523 DEF_NACL_PARAM(InitialR2T);
524 NACL_PARAM_ATTR(InitialR2T);
525
526 DEF_NACL_PARAM(ImmediateData);
527 NACL_PARAM_ATTR(ImmediateData);
528
529 DEF_NACL_PARAM(MaxBurstLength);
530 NACL_PARAM_ATTR(MaxBurstLength);
531
532 DEF_NACL_PARAM(FirstBurstLength);
533 NACL_PARAM_ATTR(FirstBurstLength);
534
535 DEF_NACL_PARAM(DefaultTime2Wait);
536 NACL_PARAM_ATTR(DefaultTime2Wait);
537
538 DEF_NACL_PARAM(DefaultTime2Retain);
539 NACL_PARAM_ATTR(DefaultTime2Retain);
540
541 DEF_NACL_PARAM(MaxOutstandingR2T);
542 NACL_PARAM_ATTR(MaxOutstandingR2T);
543
544 DEF_NACL_PARAM(DataPDUInOrder);
545 NACL_PARAM_ATTR(DataPDUInOrder);
546
547 DEF_NACL_PARAM(DataSequenceInOrder);
548 NACL_PARAM_ATTR(DataSequenceInOrder);
549
550 DEF_NACL_PARAM(ErrorRecoveryLevel);
551 NACL_PARAM_ATTR(ErrorRecoveryLevel);
552
553 static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
554         &iscsi_nacl_param_MaxConnections.attr,
555         &iscsi_nacl_param_InitialR2T.attr,
556         &iscsi_nacl_param_ImmediateData.attr,
557         &iscsi_nacl_param_MaxBurstLength.attr,
558         &iscsi_nacl_param_FirstBurstLength.attr,
559         &iscsi_nacl_param_DefaultTime2Wait.attr,
560         &iscsi_nacl_param_DefaultTime2Retain.attr,
561         &iscsi_nacl_param_MaxOutstandingR2T.attr,
562         &iscsi_nacl_param_DataPDUInOrder.attr,
563         &iscsi_nacl_param_DataSequenceInOrder.attr,
564         &iscsi_nacl_param_ErrorRecoveryLevel.attr,
565         NULL,
566 };
567
568 /* End items for lio_target_nacl_param_cit */
569
570 /* Start items for lio_target_acl_cit */
571
572 static ssize_t lio_target_nacl_show_info(
573         struct se_node_acl *se_nacl,
574         char *page)
575 {
576         struct iscsi_session *sess;
577         struct iscsi_conn *conn;
578         struct se_session *se_sess;
579         ssize_t rb = 0;
580
581         spin_lock_bh(&se_nacl->nacl_sess_lock);
582         se_sess = se_nacl->nacl_sess;
583         if (!se_sess) {
584                 rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
585                         " Endpoint: %s\n", se_nacl->initiatorname);
586         } else {
587                 sess = se_sess->fabric_sess_ptr;
588
589                 if (sess->sess_ops->InitiatorName)
590                         rb += sprintf(page+rb, "InitiatorName: %s\n",
591                                 sess->sess_ops->InitiatorName);
592                 if (sess->sess_ops->InitiatorAlias)
593                         rb += sprintf(page+rb, "InitiatorAlias: %s\n",
594                                 sess->sess_ops->InitiatorAlias);
595
596                 rb += sprintf(page+rb, "LIO Session ID: %u   "
597                         "ISID: 0x%02x %02x %02x %02x %02x %02x  "
598                         "TSIH: %hu  ", sess->sid,
599                         sess->isid[0], sess->isid[1], sess->isid[2],
600                         sess->isid[3], sess->isid[4], sess->isid[5],
601                         sess->tsih);
602                 rb += sprintf(page+rb, "SessionType: %s\n",
603                                 (sess->sess_ops->SessionType) ?
604                                 "Discovery" : "Normal");
605                 rb += sprintf(page+rb, "Session State: ");
606                 switch (sess->session_state) {
607                 case TARG_SESS_STATE_FREE:
608                         rb += sprintf(page+rb, "TARG_SESS_FREE\n");
609                         break;
610                 case TARG_SESS_STATE_ACTIVE:
611                         rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
612                         break;
613                 case TARG_SESS_STATE_LOGGED_IN:
614                         rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
615                         break;
616                 case TARG_SESS_STATE_FAILED:
617                         rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
618                         break;
619                 case TARG_SESS_STATE_IN_CONTINUE:
620                         rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
621                         break;
622                 default:
623                         rb += sprintf(page+rb, "ERROR: Unknown Session"
624                                         " State!\n");
625                         break;
626                 }
627
628                 rb += sprintf(page+rb, "---------------------[iSCSI Session"
629                                 " Values]-----------------------\n");
630                 rb += sprintf(page+rb, "  CmdSN/WR  :  CmdSN/WC  :  ExpCmdSN"
631                                 "  :  MaxCmdSN  :     ITT    :     TTT\n");
632                 rb += sprintf(page+rb, " 0x%08x   0x%08x   0x%08x   0x%08x"
633                                 "   0x%08x   0x%08x\n",
634                         sess->cmdsn_window,
635                         (sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
636                         sess->exp_cmd_sn, sess->max_cmd_sn,
637                         sess->init_task_tag, sess->targ_xfer_tag);
638                 rb += sprintf(page+rb, "----------------------[iSCSI"
639                                 " Connections]-------------------------\n");
640
641                 spin_lock(&sess->conn_lock);
642                 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
643                         rb += sprintf(page+rb, "CID: %hu  Connection"
644                                         " State: ", conn->cid);
645                         switch (conn->conn_state) {
646                         case TARG_CONN_STATE_FREE:
647                                 rb += sprintf(page+rb,
648                                         "TARG_CONN_STATE_FREE\n");
649                                 break;
650                         case TARG_CONN_STATE_XPT_UP:
651                                 rb += sprintf(page+rb,
652                                         "TARG_CONN_STATE_XPT_UP\n");
653                                 break;
654                         case TARG_CONN_STATE_IN_LOGIN:
655                                 rb += sprintf(page+rb,
656                                         "TARG_CONN_STATE_IN_LOGIN\n");
657                                 break;
658                         case TARG_CONN_STATE_LOGGED_IN:
659                                 rb += sprintf(page+rb,
660                                         "TARG_CONN_STATE_LOGGED_IN\n");
661                                 break;
662                         case TARG_CONN_STATE_IN_LOGOUT:
663                                 rb += sprintf(page+rb,
664                                         "TARG_CONN_STATE_IN_LOGOUT\n");
665                                 break;
666                         case TARG_CONN_STATE_LOGOUT_REQUESTED:
667                                 rb += sprintf(page+rb,
668                                         "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
669                                 break;
670                         case TARG_CONN_STATE_CLEANUP_WAIT:
671                                 rb += sprintf(page+rb,
672                                         "TARG_CONN_STATE_CLEANUP_WAIT\n");
673                                 break;
674                         default:
675                                 rb += sprintf(page+rb,
676                                         "ERROR: Unknown Connection State!\n");
677                                 break;
678                         }
679
680                         rb += sprintf(page+rb, "   Address %s %s", conn->login_ip,
681                                 (conn->network_transport == ISCSI_TCP) ?
682                                 "TCP" : "SCTP");
683                         rb += sprintf(page+rb, "  StatSN: 0x%08x\n",
684                                 conn->stat_sn);
685                 }
686                 spin_unlock(&sess->conn_lock);
687         }
688         spin_unlock_bh(&se_nacl->nacl_sess_lock);
689
690         return rb;
691 }
692
693 TF_NACL_BASE_ATTR_RO(lio_target, info);
694
695 static ssize_t lio_target_nacl_show_cmdsn_depth(
696         struct se_node_acl *se_nacl,
697         char *page)
698 {
699         return sprintf(page, "%u\n", se_nacl->queue_depth);
700 }
701
702 static ssize_t lio_target_nacl_store_cmdsn_depth(
703         struct se_node_acl *se_nacl,
704         const char *page,
705         size_t count)
706 {
707         struct se_portal_group *se_tpg = se_nacl->se_tpg;
708         struct iscsi_portal_group *tpg = container_of(se_tpg,
709                         struct iscsi_portal_group, tpg_se_tpg);
710         struct config_item *acl_ci, *tpg_ci, *wwn_ci;
711         char *endptr;
712         u32 cmdsn_depth = 0;
713         int ret;
714
715         cmdsn_depth = simple_strtoul(page, &endptr, 0);
716         if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
717                 pr_err("Passed cmdsn_depth: %u exceeds"
718                         " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
719                         TA_DEFAULT_CMDSN_DEPTH_MAX);
720                 return -EINVAL;
721         }
722         acl_ci = &se_nacl->acl_group.cg_item;
723         if (!acl_ci) {
724                 pr_err("Unable to locatel acl_ci\n");
725                 return -EINVAL;
726         }
727         tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
728         if (!tpg_ci) {
729                 pr_err("Unable to locate tpg_ci\n");
730                 return -EINVAL;
731         }
732         wwn_ci = &tpg_ci->ci_group->cg_item;
733         if (!wwn_ci) {
734                 pr_err("Unable to locate config_item wwn_ci\n");
735                 return -EINVAL;
736         }
737
738         if (iscsit_get_tpg(tpg) < 0)
739                 return -EINVAL;
740         /*
741          * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
742          */
743         ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
744                                 config_item_name(acl_ci), cmdsn_depth, 1);
745
746         pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
747                 "InitiatorName: %s\n", config_item_name(wwn_ci),
748                 config_item_name(tpg_ci), cmdsn_depth,
749                 config_item_name(acl_ci));
750
751         iscsit_put_tpg(tpg);
752         return (!ret) ? count : (ssize_t)ret;
753 }
754
755 TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
756
757 static struct configfs_attribute *lio_target_initiator_attrs[] = {
758         &lio_target_nacl_info.attr,
759         &lio_target_nacl_cmdsn_depth.attr,
760         NULL,
761 };
762
763 static struct se_node_acl *lio_tpg_alloc_fabric_acl(
764         struct se_portal_group *se_tpg)
765 {
766         struct iscsi_node_acl *acl;
767
768         acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
769         if (!acl) {
770                 pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
771                 return NULL;
772         }
773
774         return &acl->se_node_acl;
775 }
776
777 static struct se_node_acl *lio_target_make_nodeacl(
778         struct se_portal_group *se_tpg,
779         struct config_group *group,
780         const char *name)
781 {
782         struct config_group *stats_cg;
783         struct iscsi_node_acl *acl;
784         struct se_node_acl *se_nacl_new, *se_nacl;
785         struct iscsi_portal_group *tpg = container_of(se_tpg,
786                         struct iscsi_portal_group, tpg_se_tpg);
787         u32 cmdsn_depth;
788
789         se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
790         if (!se_nacl_new)
791                 return ERR_PTR(-ENOMEM);
792
793         cmdsn_depth = ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
794         /*
795          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
796          * when converting a NdoeACL from demo mode -> explict
797          */
798         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
799                                 name, cmdsn_depth);
800         if (IS_ERR(se_nacl))
801                 return se_nacl;
802
803         acl = container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
804         stats_cg = &se_nacl->acl_fabric_stat_group;
805
806         stats_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
807                                 GFP_KERNEL);
808         if (!stats_cg->default_groups) {
809                 pr_err("Unable to allocate memory for"
810                                 " stats_cg->default_groups\n");
811                 core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
812                 kfree(acl);
813                 return ERR_PTR(-ENOMEM);
814         }
815
816         stats_cg->default_groups[0] = &NODE_STAT_GRPS(acl)->iscsi_sess_stats_group;
817         stats_cg->default_groups[1] = NULL;
818         config_group_init_type_name(&NODE_STAT_GRPS(acl)->iscsi_sess_stats_group,
819                         "iscsi_sess_stats", &iscsi_stat_sess_cit);
820
821         return se_nacl;
822 }
823
824 static void lio_target_drop_nodeacl(
825         struct se_node_acl *se_nacl)
826 {
827         struct se_portal_group *se_tpg = se_nacl->se_tpg;
828         struct iscsi_node_acl *acl = container_of(se_nacl,
829                         struct iscsi_node_acl, se_node_acl);
830         struct config_item *df_item;
831         struct config_group *stats_cg;
832         int i;
833
834         stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
835         for (i = 0; stats_cg->default_groups[i]; i++) {
836                 df_item = &stats_cg->default_groups[i]->cg_item;
837                 stats_cg->default_groups[i] = NULL;
838                 config_item_put(df_item);
839         }
840         kfree(stats_cg->default_groups);
841
842         core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
843         kfree(acl);
844 }
845
846 /* End items for lio_target_acl_cit */
847
848 /* Start items for lio_target_tpg_attrib_cit */
849
850 #define DEF_TPG_ATTRIB(name)                                            \
851                                                                         \
852 static ssize_t iscsi_tpg_attrib_show_##name(                            \
853         struct se_portal_group *se_tpg,                         \
854         char *page)                                                     \
855 {                                                                       \
856         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
857                         struct iscsi_portal_group, tpg_se_tpg); \
858         ssize_t rb;                                                     \
859                                                                         \
860         if (iscsit_get_tpg(tpg) < 0)                                    \
861                 return -EINVAL;                                         \
862                                                                         \
863         rb = sprintf(page, "%u\n", ISCSI_TPG_ATTRIB(tpg)->name);        \
864         iscsit_put_tpg(tpg);                                            \
865         return rb;                                                      \
866 }                                                                       \
867                                                                         \
868 static ssize_t iscsi_tpg_attrib_store_##name(                           \
869         struct se_portal_group *se_tpg,                         \
870         const char *page,                                               \
871         size_t count)                                                   \
872 {                                                                       \
873         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
874                         struct iscsi_portal_group, tpg_se_tpg); \
875         char *endptr;                                                   \
876         u32 val;                                                        \
877         int ret;                                                        \
878                                                                         \
879         if (iscsit_get_tpg(tpg) < 0)                                    \
880                 return -EINVAL;                                         \
881                                                                         \
882         val = simple_strtoul(page, &endptr, 0);                         \
883         ret = iscsit_ta_##name(tpg, val);                               \
884         if (ret < 0)                                                    \
885                 goto out;                                               \
886                                                                         \
887         iscsit_put_tpg(tpg);                                            \
888         return count;                                                   \
889 out:                                                                    \
890         iscsit_put_tpg(tpg);                                            \
891         return ret;                                                     \
892 }
893
894 #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
895
896 /*
897  * Define iscsi_tpg_attrib_s_authentication
898  */
899 DEF_TPG_ATTRIB(authentication);
900 TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
901 /*
902  * Define iscsi_tpg_attrib_s_login_timeout
903  */
904 DEF_TPG_ATTRIB(login_timeout);
905 TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
906 /*
907  * Define iscsi_tpg_attrib_s_netif_timeout
908  */
909 DEF_TPG_ATTRIB(netif_timeout);
910 TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
911 /*
912  * Define iscsi_tpg_attrib_s_generate_node_acls
913  */
914 DEF_TPG_ATTRIB(generate_node_acls);
915 TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
916 /*
917  * Define iscsi_tpg_attrib_s_default_cmdsn_depth
918  */
919 DEF_TPG_ATTRIB(default_cmdsn_depth);
920 TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
921 /*
922  Define iscsi_tpg_attrib_s_cache_dynamic_acls
923  */
924 DEF_TPG_ATTRIB(cache_dynamic_acls);
925 TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
926 /*
927  * Define iscsi_tpg_attrib_s_demo_mode_write_protect
928  */
929 DEF_TPG_ATTRIB(demo_mode_write_protect);
930 TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
931 /*
932  * Define iscsi_tpg_attrib_s_prod_mode_write_protect
933  */
934 DEF_TPG_ATTRIB(prod_mode_write_protect);
935 TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
936
937 static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
938         &iscsi_tpg_attrib_authentication.attr,
939         &iscsi_tpg_attrib_login_timeout.attr,
940         &iscsi_tpg_attrib_netif_timeout.attr,
941         &iscsi_tpg_attrib_generate_node_acls.attr,
942         &iscsi_tpg_attrib_default_cmdsn_depth.attr,
943         &iscsi_tpg_attrib_cache_dynamic_acls.attr,
944         &iscsi_tpg_attrib_demo_mode_write_protect.attr,
945         &iscsi_tpg_attrib_prod_mode_write_protect.attr,
946         NULL,
947 };
948
949 /* End items for lio_target_tpg_attrib_cit */
950
951 /* Start items for lio_target_tpg_param_cit */
952
953 #define DEF_TPG_PARAM(name)                                             \
954 static ssize_t iscsi_tpg_param_show_##name(                             \
955         struct se_portal_group *se_tpg,                                 \
956         char *page)                                                     \
957 {                                                                       \
958         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
959                         struct iscsi_portal_group, tpg_se_tpg);         \
960         struct iscsi_param *param;                                      \
961         ssize_t rb;                                                     \
962                                                                         \
963         if (iscsit_get_tpg(tpg) < 0)                                    \
964                 return -EINVAL;                                         \
965                                                                         \
966         param = iscsi_find_param_from_key(__stringify(name),            \
967                                 tpg->param_list);                       \
968         if (!param) {                                                   \
969                 iscsit_put_tpg(tpg);                                    \
970                 return -EINVAL;                                         \
971         }                                                               \
972         rb = snprintf(page, PAGE_SIZE, "%s\n", param->value);           \
973                                                                         \
974         iscsit_put_tpg(tpg);                                            \
975         return rb;                                                      \
976 }                                                                       \
977 static ssize_t iscsi_tpg_param_store_##name(                            \
978         struct se_portal_group *se_tpg,                         \
979         const char *page,                                               \
980         size_t count)                                                   \
981 {                                                                       \
982         struct iscsi_portal_group *tpg = container_of(se_tpg,           \
983                         struct iscsi_portal_group, tpg_se_tpg);         \
984         char *buf;                                                      \
985         int ret;                                                        \
986                                                                         \
987         buf = kzalloc(PAGE_SIZE, GFP_KERNEL);                           \
988         if (!buf)                                                       \
989                 return -ENOMEM;                                         \
990         snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page);     \
991         buf[strlen(buf)-1] = '\0'; /* Kill newline */                   \
992                                                                         \
993         if (iscsit_get_tpg(tpg) < 0) {                                  \
994                 kfree(buf);                                             \
995                 return -EINVAL;                                         \
996         }                                                               \
997                                                                         \
998         ret = iscsi_change_param_value(buf, tpg->param_list, 1);        \
999         if (ret < 0)                                                    \
1000                 goto out;                                               \
1001                                                                         \
1002         kfree(buf);                                                     \
1003         iscsit_put_tpg(tpg);                                            \
1004         return count;                                                   \
1005 out:                                                                    \
1006         kfree(buf);                                                     \
1007         iscsit_put_tpg(tpg);                                            \
1008         return -EINVAL;                                         \
1009 }
1010
1011 #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
1012
1013 DEF_TPG_PARAM(AuthMethod);
1014 TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
1015
1016 DEF_TPG_PARAM(HeaderDigest);
1017 TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
1018
1019 DEF_TPG_PARAM(DataDigest);
1020 TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
1021
1022 DEF_TPG_PARAM(MaxConnections);
1023 TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
1024
1025 DEF_TPG_PARAM(TargetAlias);
1026 TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
1027
1028 DEF_TPG_PARAM(InitialR2T);
1029 TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
1030
1031 DEF_TPG_PARAM(ImmediateData);
1032 TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
1033
1034 DEF_TPG_PARAM(MaxRecvDataSegmentLength);
1035 TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
1036
1037 DEF_TPG_PARAM(MaxXmitDataSegmentLength);
1038 TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
1039
1040 DEF_TPG_PARAM(MaxBurstLength);
1041 TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
1042
1043 DEF_TPG_PARAM(FirstBurstLength);
1044 TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
1045
1046 DEF_TPG_PARAM(DefaultTime2Wait);
1047 TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
1048
1049 DEF_TPG_PARAM(DefaultTime2Retain);
1050 TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
1051
1052 DEF_TPG_PARAM(MaxOutstandingR2T);
1053 TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
1054
1055 DEF_TPG_PARAM(DataPDUInOrder);
1056 TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
1057
1058 DEF_TPG_PARAM(DataSequenceInOrder);
1059 TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
1060
1061 DEF_TPG_PARAM(ErrorRecoveryLevel);
1062 TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
1063
1064 DEF_TPG_PARAM(IFMarker);
1065 TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
1066
1067 DEF_TPG_PARAM(OFMarker);
1068 TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
1069
1070 DEF_TPG_PARAM(IFMarkInt);
1071 TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
1072
1073 DEF_TPG_PARAM(OFMarkInt);
1074 TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
1075
1076 static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
1077         &iscsi_tpg_param_AuthMethod.attr,
1078         &iscsi_tpg_param_HeaderDigest.attr,
1079         &iscsi_tpg_param_DataDigest.attr,
1080         &iscsi_tpg_param_MaxConnections.attr,
1081         &iscsi_tpg_param_TargetAlias.attr,
1082         &iscsi_tpg_param_InitialR2T.attr,
1083         &iscsi_tpg_param_ImmediateData.attr,
1084         &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
1085         &iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
1086         &iscsi_tpg_param_MaxBurstLength.attr,
1087         &iscsi_tpg_param_FirstBurstLength.attr,
1088         &iscsi_tpg_param_DefaultTime2Wait.attr,
1089         &iscsi_tpg_param_DefaultTime2Retain.attr,
1090         &iscsi_tpg_param_MaxOutstandingR2T.attr,
1091         &iscsi_tpg_param_DataPDUInOrder.attr,
1092         &iscsi_tpg_param_DataSequenceInOrder.attr,
1093         &iscsi_tpg_param_ErrorRecoveryLevel.attr,
1094         &iscsi_tpg_param_IFMarker.attr,
1095         &iscsi_tpg_param_OFMarker.attr,
1096         &iscsi_tpg_param_IFMarkInt.attr,
1097         &iscsi_tpg_param_OFMarkInt.attr,
1098         NULL,
1099 };
1100
1101 /* End items for lio_target_tpg_param_cit */
1102
1103 /* Start items for lio_target_tpg_cit */
1104
1105 static ssize_t lio_target_tpg_show_enable(
1106         struct se_portal_group *se_tpg,
1107         char *page)
1108 {
1109         struct iscsi_portal_group *tpg = container_of(se_tpg,
1110                         struct iscsi_portal_group, tpg_se_tpg);
1111         ssize_t len;
1112
1113         spin_lock(&tpg->tpg_state_lock);
1114         len = sprintf(page, "%d\n",
1115                         (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
1116         spin_unlock(&tpg->tpg_state_lock);
1117
1118         return len;
1119 }
1120
1121 static ssize_t lio_target_tpg_store_enable(
1122         struct se_portal_group *se_tpg,
1123         const char *page,
1124         size_t count)
1125 {
1126         struct iscsi_portal_group *tpg = container_of(se_tpg,
1127                         struct iscsi_portal_group, tpg_se_tpg);
1128         char *endptr;
1129         u32 op;
1130         int ret = 0;
1131
1132         op = simple_strtoul(page, &endptr, 0);
1133         if ((op != 1) && (op != 0)) {
1134                 pr_err("Illegal value for tpg_enable: %u\n", op);
1135                 return -EINVAL;
1136         }
1137
1138         ret = iscsit_get_tpg(tpg);
1139         if (ret < 0)
1140                 return -EINVAL;
1141
1142         if (op) {
1143                 ret = iscsit_tpg_enable_portal_group(tpg);
1144                 if (ret < 0)
1145                         goto out;
1146         } else {
1147                 /*
1148                  * iscsit_tpg_disable_portal_group() assumes force=1
1149                  */
1150                 ret = iscsit_tpg_disable_portal_group(tpg, 1);
1151                 if (ret < 0)
1152                         goto out;
1153         }
1154
1155         iscsit_put_tpg(tpg);
1156         return count;
1157 out:
1158         iscsit_put_tpg(tpg);
1159         return -EINVAL;
1160 }
1161
1162 TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
1163
1164 static struct configfs_attribute *lio_target_tpg_attrs[] = {
1165         &lio_target_tpg_enable.attr,
1166         NULL,
1167 };
1168
1169 /* End items for lio_target_tpg_cit */
1170
1171 /* Start items for lio_target_tiqn_cit */
1172
1173 static struct se_portal_group *lio_target_tiqn_addtpg(
1174         struct se_wwn *wwn,
1175         struct config_group *group,
1176         const char *name)
1177 {
1178         struct iscsi_portal_group *tpg;
1179         struct iscsi_tiqn *tiqn;
1180         char *tpgt_str, *end_ptr;
1181         int ret = 0;
1182         unsigned short int tpgt;
1183
1184         tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1185         /*
1186          * Only tpgt_# directory groups can be created below
1187          * target/iscsi/iqn.superturodiskarry/
1188         */
1189         tpgt_str = strstr(name, "tpgt_");
1190         if (!tpgt_str) {
1191                 pr_err("Unable to locate \"tpgt_#\" directory"
1192                                 " group\n");
1193                 return NULL;
1194         }
1195         tpgt_str += 5; /* Skip ahead of "tpgt_" */
1196         tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
1197
1198         tpg = iscsit_alloc_portal_group(tiqn, tpgt);
1199         if (!tpg)
1200                 return NULL;
1201
1202         ret = core_tpg_register(
1203                         &lio_target_fabric_configfs->tf_ops,
1204                         wwn, &tpg->tpg_se_tpg, tpg,
1205                         TRANSPORT_TPG_TYPE_NORMAL);
1206         if (ret < 0)
1207                 return NULL;
1208
1209         ret = iscsit_tpg_add_portal_group(tiqn, tpg);
1210         if (ret != 0)
1211                 goto out;
1212
1213         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1214         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
1215                         name);
1216         return &tpg->tpg_se_tpg;
1217 out:
1218         core_tpg_deregister(&tpg->tpg_se_tpg);
1219         kfree(tpg);
1220         return NULL;
1221 }
1222
1223 static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
1224 {
1225         struct iscsi_portal_group *tpg;
1226         struct iscsi_tiqn *tiqn;
1227
1228         tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
1229         tiqn = tpg->tpg_tiqn;
1230         /*
1231          * iscsit_tpg_del_portal_group() assumes force=1
1232          */
1233         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
1234         iscsit_tpg_del_portal_group(tiqn, tpg, 1);
1235 }
1236
1237 /* End items for lio_target_tiqn_cit */
1238
1239 /* Start LIO-Target TIQN struct contig_item lio_target_cit */
1240
1241 static ssize_t lio_target_wwn_show_attr_lio_version(
1242         struct target_fabric_configfs *tf,
1243         char *page)
1244 {
1245         return sprintf(page, "RisingTide Systems Linux-iSCSI Target "ISCSIT_VERSION"\n");
1246 }
1247
1248 TF_WWN_ATTR_RO(lio_target, lio_version);
1249
1250 static struct configfs_attribute *lio_target_wwn_attrs[] = {
1251         &lio_target_wwn_lio_version.attr,
1252         NULL,
1253 };
1254
1255 static struct se_wwn *lio_target_call_coreaddtiqn(
1256         struct target_fabric_configfs *tf,
1257         struct config_group *group,
1258         const char *name)
1259 {
1260         struct config_group *stats_cg;
1261         struct iscsi_tiqn *tiqn;
1262
1263         tiqn = iscsit_add_tiqn((unsigned char *)name);
1264         if (IS_ERR(tiqn))
1265                 return ERR_CAST(tiqn);
1266         /*
1267          * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
1268          */
1269         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1270
1271         stats_cg->default_groups = kzalloc(sizeof(struct config_group) * 6,
1272                                 GFP_KERNEL);
1273         if (!stats_cg->default_groups) {
1274                 pr_err("Unable to allocate memory for"
1275                                 " stats_cg->default_groups\n");
1276                 iscsit_del_tiqn(tiqn);
1277                 return ERR_PTR(-ENOMEM);
1278         }
1279
1280         stats_cg->default_groups[0] = &WWN_STAT_GRPS(tiqn)->iscsi_instance_group;
1281         stats_cg->default_groups[1] = &WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group;
1282         stats_cg->default_groups[2] = &WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group;
1283         stats_cg->default_groups[3] = &WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group;
1284         stats_cg->default_groups[4] = &WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group;
1285         stats_cg->default_groups[5] = NULL;
1286         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_instance_group,
1287                         "iscsi_instance", &iscsi_stat_instance_cit);
1288         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group,
1289                         "iscsi_sess_err", &iscsi_stat_sess_err_cit);
1290         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group,
1291                         "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
1292         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group,
1293                         "iscsi_login_stats", &iscsi_stat_login_cit);
1294         config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group,
1295                         "iscsi_logout_stats", &iscsi_stat_logout_cit);
1296
1297         pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
1298         pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
1299                         " %s\n", name);
1300         return &tiqn->tiqn_wwn;
1301 }
1302
1303 static void lio_target_call_coredeltiqn(
1304         struct se_wwn *wwn)
1305 {
1306         struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
1307         struct config_item *df_item;
1308         struct config_group *stats_cg;
1309         int i;
1310
1311         stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
1312         for (i = 0; stats_cg->default_groups[i]; i++) {
1313                 df_item = &stats_cg->default_groups[i]->cg_item;
1314                 stats_cg->default_groups[i] = NULL;
1315                 config_item_put(df_item);
1316         }
1317         kfree(stats_cg->default_groups);
1318
1319         pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
1320                         tiqn->tiqn);
1321         iscsit_del_tiqn(tiqn);
1322 }
1323
1324 /* End LIO-Target TIQN struct contig_lio_target_cit */
1325
1326 /* Start lio_target_discovery_auth_cit */
1327
1328 #define DEF_DISC_AUTH_STR(name, flags)                                  \
1329         __DEF_NACL_AUTH_STR(disc, name, flags)                          \
1330 static ssize_t iscsi_disc_show_##name(                                  \
1331         struct target_fabric_configfs *tf,                              \
1332         char *page)                                                     \
1333 {                                                                       \
1334         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1335                 page);                                                  \
1336 }                                                                       \
1337 static ssize_t iscsi_disc_store_##name(                                 \
1338         struct target_fabric_configfs *tf,                              \
1339         const char *page,                                               \
1340         size_t count)                                                   \
1341 {                                                                       \
1342         return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
1343                 page, count);                                           \
1344 }
1345
1346 #define DEF_DISC_AUTH_INT(name)                                         \
1347         __DEF_NACL_AUTH_INT(disc, name)                                 \
1348 static ssize_t iscsi_disc_show_##name(                                  \
1349         struct target_fabric_configfs *tf,                              \
1350         char *page)                                                     \
1351 {                                                                       \
1352         return __iscsi_disc_show_##name(&iscsit_global->discovery_acl,  \
1353                         page);                                          \
1354 }
1355
1356 #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
1357 #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
1358
1359 /*
1360  * One-way authentication userid
1361  */
1362 DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
1363 DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
1364 /*
1365  * One-way authentication password
1366  */
1367 DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
1368 DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
1369 /*
1370  * Enforce mutual authentication
1371  */
1372 DEF_DISC_AUTH_INT(authenticate_target);
1373 DISC_AUTH_ATTR_RO(authenticate_target);
1374 /*
1375  * Mutual authentication userid
1376  */
1377 DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
1378 DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
1379 /*
1380  * Mutual authentication password
1381  */
1382 DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
1383 DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
1384
1385 /*
1386  * enforce_discovery_auth
1387  */
1388 static ssize_t iscsi_disc_show_enforce_discovery_auth(
1389         struct target_fabric_configfs *tf,
1390         char *page)
1391 {
1392         struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
1393
1394         return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
1395 }
1396
1397 static ssize_t iscsi_disc_store_enforce_discovery_auth(
1398         struct target_fabric_configfs *tf,
1399         const char *page,
1400         size_t count)
1401 {
1402         struct iscsi_param *param;
1403         struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
1404         char *endptr;
1405         u32 op;
1406
1407         op = simple_strtoul(page, &endptr, 0);
1408         if ((op != 1) && (op != 0)) {
1409                 pr_err("Illegal value for enforce_discovery_auth:"
1410                                 " %u\n", op);
1411                 return -EINVAL;
1412         }
1413
1414         if (!discovery_tpg) {
1415                 pr_err("iscsit_global->discovery_tpg is NULL\n");
1416                 return -EINVAL;
1417         }
1418
1419         param = iscsi_find_param_from_key(AUTHMETHOD,
1420                                 discovery_tpg->param_list);
1421         if (!param)
1422                 return -EINVAL;
1423
1424         if (op) {
1425                 /*
1426                  * Reset the AuthMethod key to CHAP.
1427                  */
1428                 if (iscsi_update_param_value(param, CHAP) < 0)
1429                         return -EINVAL;
1430
1431                 discovery_tpg->tpg_attrib.authentication = 1;
1432                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
1433                 pr_debug("LIO-CORE[0] Successfully enabled"
1434                         " authentication enforcement for iSCSI"
1435                         " Discovery TPG\n");
1436         } else {
1437                 /*
1438                  * Reset the AuthMethod key to CHAP,None
1439                  */
1440                 if (iscsi_update_param_value(param, "CHAP,None") < 0)
1441                         return -EINVAL;
1442
1443                 discovery_tpg->tpg_attrib.authentication = 0;
1444                 iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
1445                 pr_debug("LIO-CORE[0] Successfully disabled"
1446                         " authentication enforcement for iSCSI"
1447                         " Discovery TPG\n");
1448         }
1449
1450         return count;
1451 }
1452
1453 DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
1454
1455 static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
1456         &iscsi_disc_userid.attr,
1457         &iscsi_disc_password.attr,
1458         &iscsi_disc_authenticate_target.attr,
1459         &iscsi_disc_userid_mutual.attr,
1460         &iscsi_disc_password_mutual.attr,
1461         &iscsi_disc_enforce_discovery_auth.attr,
1462         NULL,
1463 };
1464
1465 /* End lio_target_discovery_auth_cit */
1466
1467 /* Start functions for target_core_fabric_ops */
1468
1469 static char *iscsi_get_fabric_name(void)
1470 {
1471         return "iSCSI";
1472 }
1473
1474 static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
1475 {
1476         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1477
1478         /* only used for printks or comparism with ->ref_task_tag */
1479         return (__force u32)cmd->init_task_tag;
1480 }
1481
1482 static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
1483 {
1484         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1485
1486         return cmd->i_state;
1487 }
1488
1489 static u32 lio_sess_get_index(struct se_session *se_sess)
1490 {
1491         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1492
1493         return sess->session_index;
1494 }
1495
1496 static u32 lio_sess_get_initiator_sid(
1497         struct se_session *se_sess,
1498         unsigned char *buf,
1499         u32 size)
1500 {
1501         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1502         /*
1503          * iSCSI Initiator Session Identifier from RFC-3720.
1504          */
1505         return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
1506                 sess->isid[0], sess->isid[1], sess->isid[2],
1507                 sess->isid[3], sess->isid[4], sess->isid[5]);
1508 }
1509
1510 static int lio_queue_data_in(struct se_cmd *se_cmd)
1511 {
1512         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1513
1514         cmd->i_state = ISTATE_SEND_DATAIN;
1515         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1516         return 0;
1517 }
1518
1519 static int lio_write_pending(struct se_cmd *se_cmd)
1520 {
1521         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1522
1523         if (!cmd->immediate_data && !cmd->unsolicited_data)
1524                 return iscsit_build_r2ts_for_cmd(cmd, cmd->conn, false);
1525
1526         return 0;
1527 }
1528
1529 static int lio_write_pending_status(struct se_cmd *se_cmd)
1530 {
1531         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1532         int ret;
1533
1534         spin_lock_bh(&cmd->istate_lock);
1535         ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
1536         spin_unlock_bh(&cmd->istate_lock);
1537
1538         return ret;
1539 }
1540
1541 static int lio_queue_status(struct se_cmd *se_cmd)
1542 {
1543         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1544
1545         cmd->i_state = ISTATE_SEND_STATUS;
1546         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1547         return 0;
1548 }
1549
1550 static int lio_queue_tm_rsp(struct se_cmd *se_cmd)
1551 {
1552         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1553
1554         cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1555         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1556         return 0;
1557 }
1558
1559 static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
1560 {
1561         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1562
1563         return &tpg->tpg_tiqn->tiqn[0];
1564 }
1565
1566 static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
1567 {
1568         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1569
1570         return tpg->tpgt;
1571 }
1572
1573 static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
1574 {
1575         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1576
1577         return ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
1578 }
1579
1580 static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
1581 {
1582         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1583
1584         return ISCSI_TPG_ATTRIB(tpg)->generate_node_acls;
1585 }
1586
1587 static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
1588 {
1589         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1590
1591         return ISCSI_TPG_ATTRIB(tpg)->cache_dynamic_acls;
1592 }
1593
1594 static int lio_tpg_check_demo_mode_write_protect(
1595         struct se_portal_group *se_tpg)
1596 {
1597         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1598
1599         return ISCSI_TPG_ATTRIB(tpg)->demo_mode_write_protect;
1600 }
1601
1602 static int lio_tpg_check_prod_mode_write_protect(
1603         struct se_portal_group *se_tpg)
1604 {
1605         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1606
1607         return ISCSI_TPG_ATTRIB(tpg)->prod_mode_write_protect;
1608 }
1609
1610 static void lio_tpg_release_fabric_acl(
1611         struct se_portal_group *se_tpg,
1612         struct se_node_acl *se_acl)
1613 {
1614         struct iscsi_node_acl *acl = container_of(se_acl,
1615                                 struct iscsi_node_acl, se_node_acl);
1616         kfree(acl);
1617 }
1618
1619 /*
1620  * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
1621  *
1622  * Also, this function calls iscsit_inc_session_usage_count() on the
1623  * struct iscsi_session in question.
1624  */
1625 static int lio_tpg_shutdown_session(struct se_session *se_sess)
1626 {
1627         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1628
1629         spin_lock(&sess->conn_lock);
1630         if (atomic_read(&sess->session_fall_back_to_erl0) ||
1631             atomic_read(&sess->session_logout) ||
1632             (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
1633                 spin_unlock(&sess->conn_lock);
1634                 return 0;
1635         }
1636         atomic_set(&sess->session_reinstatement, 1);
1637         spin_unlock(&sess->conn_lock);
1638
1639         iscsit_stop_time2retain_timer(sess);
1640         iscsit_stop_session(sess, 1, 1);
1641
1642         return 1;
1643 }
1644
1645 /*
1646  * Calls iscsit_dec_session_usage_count() as inverse of
1647  * lio_tpg_shutdown_session()
1648  */
1649 static void lio_tpg_close_session(struct se_session *se_sess)
1650 {
1651         struct iscsi_session *sess = se_sess->fabric_sess_ptr;
1652         /*
1653          * If the iSCSI Session for the iSCSI Initiator Node exists,
1654          * forcefully shutdown the iSCSI NEXUS.
1655          */
1656         iscsit_close_session(sess);
1657 }
1658
1659 static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
1660 {
1661         struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
1662
1663         return tpg->tpg_tiqn->tiqn_index;
1664 }
1665
1666 static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
1667 {
1668         struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
1669                                 se_node_acl);
1670
1671         ISCSI_NODE_ATTRIB(acl)->nacl = acl;
1672         iscsit_set_default_node_attribues(acl);
1673 }
1674
1675 static void lio_release_cmd(struct se_cmd *se_cmd)
1676 {
1677         struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
1678
1679         iscsit_release_cmd(cmd);
1680 }
1681
1682 /* End functions for target_core_fabric_ops */
1683
1684 int iscsi_target_register_configfs(void)
1685 {
1686         struct target_fabric_configfs *fabric;
1687         int ret;
1688
1689         lio_target_fabric_configfs = NULL;
1690         fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
1691         if (IS_ERR(fabric)) {
1692                 pr_err("target_fabric_configfs_init() for"
1693                                 " LIO-Target failed!\n");
1694                 return PTR_ERR(fabric);
1695         }
1696         /*
1697          * Setup the fabric API of function pointers used by target_core_mod..
1698          */
1699         fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
1700         fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
1701         fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
1702         fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
1703         fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
1704         fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
1705         fabric->tf_ops.tpg_get_pr_transport_id_len =
1706                                 &iscsi_get_pr_transport_id_len;
1707         fabric->tf_ops.tpg_parse_pr_out_transport_id =
1708                                 &iscsi_parse_pr_out_transport_id;
1709         fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
1710         fabric->tf_ops.tpg_check_demo_mode_cache =
1711                                 &lio_tpg_check_demo_mode_cache;
1712         fabric->tf_ops.tpg_check_demo_mode_write_protect =
1713                                 &lio_tpg_check_demo_mode_write_protect;
1714         fabric->tf_ops.tpg_check_prod_mode_write_protect =
1715                                 &lio_tpg_check_prod_mode_write_protect;
1716         fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
1717         fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
1718         fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
1719         fabric->tf_ops.release_cmd = &lio_release_cmd;
1720         fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
1721         fabric->tf_ops.close_session = &lio_tpg_close_session;
1722         fabric->tf_ops.sess_get_index = &lio_sess_get_index;
1723         fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
1724         fabric->tf_ops.write_pending = &lio_write_pending;
1725         fabric->tf_ops.write_pending_status = &lio_write_pending_status;
1726         fabric->tf_ops.set_default_node_attributes =
1727                                 &lio_set_default_node_attributes;
1728         fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
1729         fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
1730         fabric->tf_ops.queue_data_in = &lio_queue_data_in;
1731         fabric->tf_ops.queue_status = &lio_queue_status;
1732         fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
1733         /*
1734          * Setup function pointers for generic logic in target_core_fabric_configfs.c
1735          */
1736         fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
1737         fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
1738         fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
1739         fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
1740         fabric->tf_ops.fabric_post_link = NULL;
1741         fabric->tf_ops.fabric_pre_unlink = NULL;
1742         fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
1743         fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
1744         fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
1745         fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
1746         /*
1747          * Setup default attribute lists for various fabric->tf_cit_tmpl
1748          * sturct config_item_type's
1749          */
1750         TF_CIT_TMPL(fabric)->tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
1751         TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
1752         TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
1753         TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
1754         TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
1755         TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
1756         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
1757         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
1758         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
1759         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
1760
1761         ret = target_fabric_configfs_register(fabric);
1762         if (ret < 0) {
1763                 pr_err("target_fabric_configfs_register() for"
1764                                 " LIO-Target failed!\n");
1765                 target_fabric_configfs_free(fabric);
1766                 return ret;
1767         }
1768
1769         lio_target_fabric_configfs = fabric;
1770         pr_debug("LIO_TARGET[0] - Set fabric ->"
1771                         " lio_target_fabric_configfs\n");
1772         return 0;
1773 }
1774
1775
1776 void iscsi_target_deregister_configfs(void)
1777 {
1778         if (!lio_target_fabric_configfs)
1779                 return;
1780         /*
1781          * Shutdown discovery sessions and disable discovery TPG
1782          */
1783         if (iscsit_global->discovery_tpg)
1784                 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
1785
1786         target_fabric_configfs_deregister(lio_target_fabric_configfs);
1787         lio_target_fabric_configfs = NULL;
1788         pr_debug("LIO_TARGET[0] - Cleared"
1789                                 " lio_target_fabric_configfs\n");
1790 }