]> Pileus Git - ~andy/linux/blob - drivers/target/iscsi/iscsi_target_tpg.c
Merge branch 'sfc-3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/bwh/sfc
[~andy/linux] / drivers / target / iscsi / iscsi_target_tpg.c
1 /*******************************************************************************
2  * This file contains iSCSI Target Portal Group related functions.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
23 #include <target/target_core_configfs.h>
24
25 #include "iscsi_target_core.h"
26 #include "iscsi_target_erl0.h"
27 #include "iscsi_target_login.h"
28 #include "iscsi_target_nodeattrib.h"
29 #include "iscsi_target_tpg.h"
30 #include "iscsi_target_util.h"
31 #include "iscsi_target.h"
32 #include "iscsi_target_parameters.h"
33
34 struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
35 {
36         struct iscsi_portal_group *tpg;
37
38         tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
39         if (!tpg) {
40                 pr_err("Unable to allocate struct iscsi_portal_group\n");
41                 return NULL;
42         }
43
44         tpg->tpgt = tpgt;
45         tpg->tpg_state = TPG_STATE_FREE;
46         tpg->tpg_tiqn = tiqn;
47         INIT_LIST_HEAD(&tpg->tpg_gnp_list);
48         INIT_LIST_HEAD(&tpg->tpg_list);
49         mutex_init(&tpg->tpg_access_lock);
50         mutex_init(&tpg->np_login_lock);
51         spin_lock_init(&tpg->tpg_state_lock);
52         spin_lock_init(&tpg->tpg_np_lock);
53
54         return tpg;
55 }
56
57 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
58
59 int iscsit_load_discovery_tpg(void)
60 {
61         struct iscsi_param *param;
62         struct iscsi_portal_group *tpg;
63         int ret;
64
65         tpg = iscsit_alloc_portal_group(NULL, 1);
66         if (!tpg) {
67                 pr_err("Unable to allocate struct iscsi_portal_group\n");
68                 return -1;
69         }
70
71         ret = core_tpg_register(
72                         &lio_target_fabric_configfs->tf_ops,
73                         NULL, &tpg->tpg_se_tpg, tpg,
74                         TRANSPORT_TPG_TYPE_DISCOVERY);
75         if (ret < 0) {
76                 kfree(tpg);
77                 return -1;
78         }
79
80         tpg->sid = 1; /* First Assigned LIO Session ID */
81         iscsit_set_default_tpg_attribs(tpg);
82
83         if (iscsi_create_default_params(&tpg->param_list) < 0)
84                 goto out;
85         /*
86          * By default we disable authentication for discovery sessions,
87          * this can be changed with:
88          *
89          * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
90          */
91         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
92         if (!param)
93                 goto out;
94
95         if (iscsi_update_param_value(param, "CHAP,None") < 0)
96                 goto out;
97
98         tpg->tpg_attrib.authentication = 0;
99
100         spin_lock(&tpg->tpg_state_lock);
101         tpg->tpg_state  = TPG_STATE_ACTIVE;
102         spin_unlock(&tpg->tpg_state_lock);
103
104         iscsit_global->discovery_tpg = tpg;
105         pr_debug("CORE[0] - Allocated Discovery TPG\n");
106
107         return 0;
108 out:
109         if (tpg->sid == 1)
110                 core_tpg_deregister(&tpg->tpg_se_tpg);
111         kfree(tpg);
112         return -1;
113 }
114
115 void iscsit_release_discovery_tpg(void)
116 {
117         struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
118
119         if (!tpg)
120                 return;
121
122         core_tpg_deregister(&tpg->tpg_se_tpg);
123
124         kfree(tpg);
125         iscsit_global->discovery_tpg = NULL;
126 }
127
128 struct iscsi_portal_group *iscsit_get_tpg_from_np(
129         struct iscsi_tiqn *tiqn,
130         struct iscsi_np *np)
131 {
132         struct iscsi_portal_group *tpg = NULL;
133         struct iscsi_tpg_np *tpg_np;
134
135         spin_lock(&tiqn->tiqn_tpg_lock);
136         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
137
138                 spin_lock(&tpg->tpg_state_lock);
139                 if (tpg->tpg_state == TPG_STATE_FREE) {
140                         spin_unlock(&tpg->tpg_state_lock);
141                         continue;
142                 }
143                 spin_unlock(&tpg->tpg_state_lock);
144
145                 spin_lock(&tpg->tpg_np_lock);
146                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
147                         if (tpg_np->tpg_np == np) {
148                                 spin_unlock(&tpg->tpg_np_lock);
149                                 spin_unlock(&tiqn->tiqn_tpg_lock);
150                                 return tpg;
151                         }
152                 }
153                 spin_unlock(&tpg->tpg_np_lock);
154         }
155         spin_unlock(&tiqn->tiqn_tpg_lock);
156
157         return NULL;
158 }
159
160 int iscsit_get_tpg(
161         struct iscsi_portal_group *tpg)
162 {
163         int ret;
164
165         ret = mutex_lock_interruptible(&tpg->tpg_access_lock);
166         return ((ret != 0) || signal_pending(current)) ? -1 : 0;
167 }
168
169 void iscsit_put_tpg(struct iscsi_portal_group *tpg)
170 {
171         mutex_unlock(&tpg->tpg_access_lock);
172 }
173
174 static void iscsit_clear_tpg_np_login_thread(
175         struct iscsi_tpg_np *tpg_np,
176         struct iscsi_portal_group *tpg)
177 {
178         if (!tpg_np->tpg_np) {
179                 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
180                 return;
181         }
182
183         iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg);
184 }
185
186 void iscsit_clear_tpg_np_login_threads(
187         struct iscsi_portal_group *tpg)
188 {
189         struct iscsi_tpg_np *tpg_np;
190
191         spin_lock(&tpg->tpg_np_lock);
192         list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
193                 if (!tpg_np->tpg_np) {
194                         pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
195                         continue;
196                 }
197                 spin_unlock(&tpg->tpg_np_lock);
198                 iscsit_clear_tpg_np_login_thread(tpg_np, tpg);
199                 spin_lock(&tpg->tpg_np_lock);
200         }
201         spin_unlock(&tpg->tpg_np_lock);
202 }
203
204 void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
205 {
206         iscsi_print_params(tpg->param_list);
207 }
208
209 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
210 {
211         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
212
213         a->authentication = TA_AUTHENTICATION;
214         a->login_timeout = TA_LOGIN_TIMEOUT;
215         a->netif_timeout = TA_NETIF_TIMEOUT;
216         a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
217         a->generate_node_acls = TA_GENERATE_NODE_ACLS;
218         a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
219         a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
220         a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
221 }
222
223 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
224 {
225         if (tpg->tpg_state != TPG_STATE_FREE) {
226                 pr_err("Unable to add iSCSI Target Portal Group: %d"
227                         " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
228                 return -EEXIST;
229         }
230         iscsit_set_default_tpg_attribs(tpg);
231
232         if (iscsi_create_default_params(&tpg->param_list) < 0)
233                 goto err_out;
234
235         ISCSI_TPG_ATTRIB(tpg)->tpg = tpg;
236
237         spin_lock(&tpg->tpg_state_lock);
238         tpg->tpg_state  = TPG_STATE_INACTIVE;
239         spin_unlock(&tpg->tpg_state_lock);
240
241         spin_lock(&tiqn->tiqn_tpg_lock);
242         list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
243         tiqn->tiqn_ntpgs++;
244         pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
245                         tiqn->tiqn, tpg->tpgt);
246         spin_unlock(&tiqn->tiqn_tpg_lock);
247
248         return 0;
249 err_out:
250         if (tpg->param_list) {
251                 iscsi_release_param_list(tpg->param_list);
252                 tpg->param_list = NULL;
253         }
254         kfree(tpg);
255         return -ENOMEM;
256 }
257
258 int iscsit_tpg_del_portal_group(
259         struct iscsi_tiqn *tiqn,
260         struct iscsi_portal_group *tpg,
261         int force)
262 {
263         u8 old_state = tpg->tpg_state;
264
265         spin_lock(&tpg->tpg_state_lock);
266         tpg->tpg_state = TPG_STATE_INACTIVE;
267         spin_unlock(&tpg->tpg_state_lock);
268
269         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
270                 pr_err("Unable to delete iSCSI Target Portal Group:"
271                         " %hu while active sessions exist, and force=0\n",
272                         tpg->tpgt);
273                 tpg->tpg_state = old_state;
274                 return -EPERM;
275         }
276
277         core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
278
279         if (tpg->param_list) {
280                 iscsi_release_param_list(tpg->param_list);
281                 tpg->param_list = NULL;
282         }
283
284         core_tpg_deregister(&tpg->tpg_se_tpg);
285
286         spin_lock(&tpg->tpg_state_lock);
287         tpg->tpg_state = TPG_STATE_FREE;
288         spin_unlock(&tpg->tpg_state_lock);
289
290         spin_lock(&tiqn->tiqn_tpg_lock);
291         tiqn->tiqn_ntpgs--;
292         list_del(&tpg->tpg_list);
293         spin_unlock(&tiqn->tiqn_tpg_lock);
294
295         pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
296                         tiqn->tiqn, tpg->tpgt);
297
298         kfree(tpg);
299         return 0;
300 }
301
302 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
303 {
304         struct iscsi_param *param;
305         struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
306         int ret;
307
308         spin_lock(&tpg->tpg_state_lock);
309         if (tpg->tpg_state == TPG_STATE_ACTIVE) {
310                 pr_err("iSCSI target portal group: %hu is already"
311                         " active, ignoring request.\n", tpg->tpgt);
312                 spin_unlock(&tpg->tpg_state_lock);
313                 return -EINVAL;
314         }
315         /*
316          * Make sure that AuthMethod does not contain None as an option
317          * unless explictly disabled.  Set the default to CHAP if authentication
318          * is enforced (as per default), and remove the NONE option.
319          */
320         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
321         if (!param) {
322                 spin_unlock(&tpg->tpg_state_lock);
323                 return -EINVAL;
324         }
325
326         if (ISCSI_TPG_ATTRIB(tpg)->authentication) {
327                 if (!strcmp(param->value, NONE)) {
328                         ret = iscsi_update_param_value(param, CHAP);
329                         if (ret)
330                                 goto err;
331                 }
332
333                 ret = iscsit_ta_authentication(tpg, 1);
334                 if (ret < 0)
335                         goto err;
336         }
337
338         tpg->tpg_state = TPG_STATE_ACTIVE;
339         spin_unlock(&tpg->tpg_state_lock);
340
341         spin_lock(&tiqn->tiqn_tpg_lock);
342         tiqn->tiqn_active_tpgs++;
343         pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
344                         tpg->tpgt);
345         spin_unlock(&tiqn->tiqn_tpg_lock);
346
347         return 0;
348
349 err:
350         spin_unlock(&tpg->tpg_state_lock);
351         return ret;
352 }
353
354 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
355 {
356         struct iscsi_tiqn *tiqn;
357         u8 old_state = tpg->tpg_state;
358
359         spin_lock(&tpg->tpg_state_lock);
360         if (tpg->tpg_state == TPG_STATE_INACTIVE) {
361                 pr_err("iSCSI Target Portal Group: %hu is already"
362                         " inactive, ignoring request.\n", tpg->tpgt);
363                 spin_unlock(&tpg->tpg_state_lock);
364                 return -EINVAL;
365         }
366         tpg->tpg_state = TPG_STATE_INACTIVE;
367         spin_unlock(&tpg->tpg_state_lock);
368
369         iscsit_clear_tpg_np_login_threads(tpg);
370
371         if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
372                 spin_lock(&tpg->tpg_state_lock);
373                 tpg->tpg_state = old_state;
374                 spin_unlock(&tpg->tpg_state_lock);
375                 pr_err("Unable to disable iSCSI Target Portal Group:"
376                         " %hu while active sessions exist, and force=0\n",
377                         tpg->tpgt);
378                 return -EPERM;
379         }
380
381         tiqn = tpg->tpg_tiqn;
382         if (!tiqn || (tpg == iscsit_global->discovery_tpg))
383                 return 0;
384
385         spin_lock(&tiqn->tiqn_tpg_lock);
386         tiqn->tiqn_active_tpgs--;
387         pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
388                         tpg->tpgt);
389         spin_unlock(&tiqn->tiqn_tpg_lock);
390
391         return 0;
392 }
393
394 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
395         struct iscsi_session *sess)
396 {
397         struct se_session *se_sess = sess->se_sess;
398         struct se_node_acl *se_nacl = se_sess->se_node_acl;
399         struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
400                                         se_node_acl);
401
402         return &acl->node_attrib;
403 }
404
405 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
406         struct iscsi_tpg_np *tpg_np,
407         int network_transport)
408 {
409         struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
410
411         spin_lock(&tpg_np->tpg_np_parent_lock);
412         list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
413                         &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
414                 if (tpg_np_child->tpg_np->np_network_transport ==
415                                 network_transport) {
416                         spin_unlock(&tpg_np->tpg_np_parent_lock);
417                         return tpg_np_child;
418                 }
419         }
420         spin_unlock(&tpg_np->tpg_np_parent_lock);
421
422         return NULL;
423 }
424
425 static bool iscsit_tpg_check_network_portal(
426         struct iscsi_tiqn *tiqn,
427         struct __kernel_sockaddr_storage *sockaddr,
428         int network_transport)
429 {
430         struct iscsi_portal_group *tpg;
431         struct iscsi_tpg_np *tpg_np;
432         struct iscsi_np *np;
433         bool match = false;
434
435         spin_lock(&tiqn->tiqn_tpg_lock);
436         list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
437
438                 spin_lock(&tpg->tpg_np_lock);
439                 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
440                         np = tpg_np->tpg_np;
441
442                         match = iscsit_check_np_match(sockaddr, np,
443                                                 network_transport);
444                         if (match == true)
445                                 break;
446                 }
447                 spin_unlock(&tpg->tpg_np_lock);
448         }
449         spin_unlock(&tiqn->tiqn_tpg_lock);
450
451         return match;
452 }
453
454 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
455         struct iscsi_portal_group *tpg,
456         struct __kernel_sockaddr_storage *sockaddr,
457         char *ip_str,
458         struct iscsi_tpg_np *tpg_np_parent,
459         int network_transport)
460 {
461         struct iscsi_np *np;
462         struct iscsi_tpg_np *tpg_np;
463
464         if (!tpg_np_parent) {
465                 if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
466                                 network_transport) == true) {
467                         pr_err("Network Portal: %s already exists on a"
468                                 " different TPG on %s\n", ip_str,
469                                 tpg->tpg_tiqn->tiqn);
470                         return ERR_PTR(-EEXIST);
471                 }
472         }
473
474         tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
475         if (!tpg_np) {
476                 pr_err("Unable to allocate memory for"
477                                 " struct iscsi_tpg_np.\n");
478                 return ERR_PTR(-ENOMEM);
479         }
480
481         np = iscsit_add_np(sockaddr, ip_str, network_transport);
482         if (IS_ERR(np)) {
483                 kfree(tpg_np);
484                 return ERR_CAST(np);
485         }
486
487         INIT_LIST_HEAD(&tpg_np->tpg_np_list);
488         INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
489         INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
490         spin_lock_init(&tpg_np->tpg_np_parent_lock);
491         tpg_np->tpg_np          = np;
492         tpg_np->tpg             = tpg;
493
494         spin_lock(&tpg->tpg_np_lock);
495         list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
496         tpg->num_tpg_nps++;
497         if (tpg->tpg_tiqn)
498                 tpg->tpg_tiqn->tiqn_num_tpg_nps++;
499         spin_unlock(&tpg->tpg_np_lock);
500
501         if (tpg_np_parent) {
502                 tpg_np->tpg_np_parent = tpg_np_parent;
503                 spin_lock(&tpg_np_parent->tpg_np_parent_lock);
504                 list_add_tail(&tpg_np->tpg_np_child_list,
505                         &tpg_np_parent->tpg_np_parent_list);
506                 spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
507         }
508
509         pr_debug("CORE[%s] - Added Network Portal: %s:%hu,%hu on %s\n",
510                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
511                 (np->np_network_transport == ISCSI_TCP) ? "TCP" : "SCTP");
512
513         return tpg_np;
514 }
515
516 static int iscsit_tpg_release_np(
517         struct iscsi_tpg_np *tpg_np,
518         struct iscsi_portal_group *tpg,
519         struct iscsi_np *np)
520 {
521         iscsit_clear_tpg_np_login_thread(tpg_np, tpg);
522
523         pr_debug("CORE[%s] - Removed Network Portal: %s:%hu,%hu on %s\n",
524                 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
525                 (np->np_network_transport == ISCSI_TCP) ? "TCP" : "SCTP");
526
527         tpg_np->tpg_np = NULL;
528         tpg_np->tpg = NULL;
529         kfree(tpg_np);
530         /*
531          * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
532          */
533         return iscsit_del_np(np);
534 }
535
536 int iscsit_tpg_del_network_portal(
537         struct iscsi_portal_group *tpg,
538         struct iscsi_tpg_np *tpg_np)
539 {
540         struct iscsi_np *np;
541         struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
542         int ret = 0;
543
544         np = tpg_np->tpg_np;
545         if (!np) {
546                 pr_err("Unable to locate struct iscsi_np from"
547                                 " struct iscsi_tpg_np\n");
548                 return -EINVAL;
549         }
550
551         if (!tpg_np->tpg_np_parent) {
552                 /*
553                  * We are the parent tpg network portal.  Release all of the
554                  * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
555                  * list first.
556                  */
557                 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
558                                 &tpg_np->tpg_np_parent_list,
559                                 tpg_np_child_list) {
560                         ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
561                         if (ret < 0)
562                                 pr_err("iscsit_tpg_del_network_portal()"
563                                         " failed: %d\n", ret);
564                 }
565         } else {
566                 /*
567                  * We are not the parent ISCSI_TCP tpg network portal.  Release
568                  * our own network portals from the child list.
569                  */
570                 spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
571                 list_del(&tpg_np->tpg_np_child_list);
572                 spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
573         }
574
575         spin_lock(&tpg->tpg_np_lock);
576         list_del(&tpg_np->tpg_np_list);
577         tpg->num_tpg_nps--;
578         if (tpg->tpg_tiqn)
579                 tpg->tpg_tiqn->tiqn_num_tpg_nps--;
580         spin_unlock(&tpg->tpg_np_lock);
581
582         return iscsit_tpg_release_np(tpg_np, tpg, np);
583 }
584
585 int iscsit_tpg_set_initiator_node_queue_depth(
586         struct iscsi_portal_group *tpg,
587         unsigned char *initiatorname,
588         u32 queue_depth,
589         int force)
590 {
591         return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
592                 initiatorname, queue_depth, force);
593 }
594
595 int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
596 {
597         unsigned char buf1[256], buf2[256], *none = NULL;
598         int len;
599         struct iscsi_param *param;
600         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
601
602         if ((authentication != 1) && (authentication != 0)) {
603                 pr_err("Illegal value for authentication parameter:"
604                         " %u, ignoring request.\n", authentication);
605                 return -EINVAL;
606         }
607
608         memset(buf1, 0, sizeof(buf1));
609         memset(buf2, 0, sizeof(buf2));
610
611         param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
612         if (!param)
613                 return -EINVAL;
614
615         if (authentication) {
616                 snprintf(buf1, sizeof(buf1), "%s", param->value);
617                 none = strstr(buf1, NONE);
618                 if (!none)
619                         goto out;
620                 if (!strncmp(none + 4, ",", 1)) {
621                         if (!strcmp(buf1, none))
622                                 sprintf(buf2, "%s", none+5);
623                         else {
624                                 none--;
625                                 *none = '\0';
626                                 len = sprintf(buf2, "%s", buf1);
627                                 none += 5;
628                                 sprintf(buf2 + len, "%s", none);
629                         }
630                 } else {
631                         none--;
632                         *none = '\0';
633                         sprintf(buf2, "%s", buf1);
634                 }
635                 if (iscsi_update_param_value(param, buf2) < 0)
636                         return -EINVAL;
637         } else {
638                 snprintf(buf1, sizeof(buf1), "%s", param->value);
639                 none = strstr(buf1, NONE);
640                 if (none)
641                         goto out;
642                 strncat(buf1, ",", strlen(","));
643                 strncat(buf1, NONE, strlen(NONE));
644                 if (iscsi_update_param_value(param, buf1) < 0)
645                         return -EINVAL;
646         }
647
648 out:
649         a->authentication = authentication;
650         pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
651                 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
652
653         return 0;
654 }
655
656 int iscsit_ta_login_timeout(
657         struct iscsi_portal_group *tpg,
658         u32 login_timeout)
659 {
660         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
661
662         if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
663                 pr_err("Requested Login Timeout %u larger than maximum"
664                         " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
665                 return -EINVAL;
666         } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
667                 pr_err("Requested Logout Timeout %u smaller than"
668                         " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
669                 return -EINVAL;
670         }
671
672         a->login_timeout = login_timeout;
673         pr_debug("Set Logout Timeout to %u for Target Portal Group"
674                 " %hu\n", a->login_timeout, tpg->tpgt);
675
676         return 0;
677 }
678
679 int iscsit_ta_netif_timeout(
680         struct iscsi_portal_group *tpg,
681         u32 netif_timeout)
682 {
683         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
684
685         if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
686                 pr_err("Requested Network Interface Timeout %u larger"
687                         " than maximum %u\n", netif_timeout,
688                                 TA_NETIF_TIMEOUT_MAX);
689                 return -EINVAL;
690         } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
691                 pr_err("Requested Network Interface Timeout %u smaller"
692                         " than minimum %u\n", netif_timeout,
693                                 TA_NETIF_TIMEOUT_MIN);
694                 return -EINVAL;
695         }
696
697         a->netif_timeout = netif_timeout;
698         pr_debug("Set Network Interface Timeout to %u for"
699                 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
700
701         return 0;
702 }
703
704 int iscsit_ta_generate_node_acls(
705         struct iscsi_portal_group *tpg,
706         u32 flag)
707 {
708         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
709
710         if ((flag != 0) && (flag != 1)) {
711                 pr_err("Illegal value %d\n", flag);
712                 return -EINVAL;
713         }
714
715         a->generate_node_acls = flag;
716         pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
717                 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
718
719         if (flag == 1 && a->cache_dynamic_acls == 0) {
720                 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
721                         "generate_node_acls=1\n");
722                 a->cache_dynamic_acls = 1;
723         }
724
725         return 0;
726 }
727
728 int iscsit_ta_default_cmdsn_depth(
729         struct iscsi_portal_group *tpg,
730         u32 tcq_depth)
731 {
732         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
733
734         if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
735                 pr_err("Requested Default Queue Depth: %u larger"
736                         " than maximum %u\n", tcq_depth,
737                                 TA_DEFAULT_CMDSN_DEPTH_MAX);
738                 return -EINVAL;
739         } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
740                 pr_err("Requested Default Queue Depth: %u smaller"
741                         " than minimum %u\n", tcq_depth,
742                                 TA_DEFAULT_CMDSN_DEPTH_MIN);
743                 return -EINVAL;
744         }
745
746         a->default_cmdsn_depth = tcq_depth;
747         pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
748                 tpg->tpgt, a->default_cmdsn_depth);
749
750         return 0;
751 }
752
753 int iscsit_ta_cache_dynamic_acls(
754         struct iscsi_portal_group *tpg,
755         u32 flag)
756 {
757         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
758
759         if ((flag != 0) && (flag != 1)) {
760                 pr_err("Illegal value %d\n", flag);
761                 return -EINVAL;
762         }
763
764         if (a->generate_node_acls == 1 && flag == 0) {
765                 pr_debug("Skipping cache_dynamic_acls=0 when"
766                         " generate_node_acls=1\n");
767                 return 0;
768         }
769
770         a->cache_dynamic_acls = flag;
771         pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
772                 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
773                 "Enabled" : "Disabled");
774
775         return 0;
776 }
777
778 int iscsit_ta_demo_mode_write_protect(
779         struct iscsi_portal_group *tpg,
780         u32 flag)
781 {
782         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
783
784         if ((flag != 0) && (flag != 1)) {
785                 pr_err("Illegal value %d\n", flag);
786                 return -EINVAL;
787         }
788
789         a->demo_mode_write_protect = flag;
790         pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
791                 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
792
793         return 0;
794 }
795
796 int iscsit_ta_prod_mode_write_protect(
797         struct iscsi_portal_group *tpg,
798         u32 flag)
799 {
800         struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
801
802         if ((flag != 0) && (flag != 1)) {
803                 pr_err("Illegal value %d\n", flag);
804                 return -EINVAL;
805         }
806
807         a->prod_mode_write_protect = flag;
808         pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
809                 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
810                 "ON" : "OFF");
811
812         return 0;
813 }