blob: abaabbaebd881b74dad4ea341b58df572306a41c [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
2 * This file contains iSCSI Target Portal Group related functions.
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2007-2013 Datera, Inc.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00005 *
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
18
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080019#include <linux/slab.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000020#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050021#include <target/target_core_fabric.h>
Sagi Grimberg67f091f2015-01-07 14:57:31 +020022#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000023#include "iscsi_target_erl0.h"
24#include "iscsi_target_login.h"
25#include "iscsi_target_nodeattrib.h"
26#include "iscsi_target_tpg.h"
27#include "iscsi_target_util.h"
28#include "iscsi_target.h"
29#include "iscsi_target_parameters.h"
30
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080031#include <target/iscsi/iscsi_transport.h>
32
Nicholas Bellingere48354c2011-07-23 06:43:04 +000033struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
34{
35 struct iscsi_portal_group *tpg;
36
37 tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
38 if (!tpg) {
39 pr_err("Unable to allocate struct iscsi_portal_group\n");
40 return NULL;
41 }
42
43 tpg->tpgt = tpgt;
44 tpg->tpg_state = TPG_STATE_FREE;
45 tpg->tpg_tiqn = tiqn;
46 INIT_LIST_HEAD(&tpg->tpg_gnp_list);
47 INIT_LIST_HEAD(&tpg->tpg_list);
48 mutex_init(&tpg->tpg_access_lock);
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -070049 sema_init(&tpg->np_login_sem, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000050 spin_lock_init(&tpg->tpg_state_lock);
51 spin_lock_init(&tpg->tpg_np_lock);
52
53 return tpg;
54}
55
56static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
57
58int iscsit_load_discovery_tpg(void)
59{
60 struct iscsi_param *param;
61 struct iscsi_portal_group *tpg;
62 int ret;
63
64 tpg = iscsit_alloc_portal_group(NULL, 1);
65 if (!tpg) {
66 pr_err("Unable to allocate struct iscsi_portal_group\n");
67 return -1;
68 }
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -070069 /*
70 * Save iscsi_ops pointer for special case discovery TPG that
71 * doesn't exist as se_wwn->wwn_group within configfs.
72 */
73 tpg->tpg_se_tpg.se_tpg_tfo = &iscsi_ops;
74 ret = core_tpg_register(NULL, &tpg->tpg_se_tpg, -1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000075 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;
108out:
109 if (tpg->sid == 1)
110 core_tpg_deregister(&tpg->tpg_se_tpg);
111 kfree(tpg);
112 return -1;
113}
114
115void 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
128struct iscsi_portal_group *iscsit_get_tpg_from_np(
129 struct iscsi_tiqn *tiqn,
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700130 struct iscsi_np *np,
131 struct iscsi_tpg_np **tpg_np_out)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000132{
133 struct iscsi_portal_group *tpg = NULL;
134 struct iscsi_tpg_np *tpg_np;
135
136 spin_lock(&tiqn->tiqn_tpg_lock);
137 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
138
139 spin_lock(&tpg->tpg_state_lock);
Nicholas Bellingera2a99ce2014-02-26 03:09:41 -0800140 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000141 spin_unlock(&tpg->tpg_state_lock);
142 continue;
143 }
144 spin_unlock(&tpg->tpg_state_lock);
145
146 spin_lock(&tpg->tpg_np_lock);
147 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
148 if (tpg_np->tpg_np == np) {
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700149 *tpg_np_out = tpg_np;
150 kref_get(&tpg_np->tpg_np_kref);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 spin_unlock(&tpg->tpg_np_lock);
152 spin_unlock(&tiqn->tiqn_tpg_lock);
153 return tpg;
154 }
155 }
156 spin_unlock(&tpg->tpg_np_lock);
157 }
158 spin_unlock(&tiqn->tiqn_tpg_lock);
159
160 return NULL;
161}
162
163int iscsit_get_tpg(
164 struct iscsi_portal_group *tpg)
165{
Nicholas Bellingeree7619f2015-05-19 15:10:44 -0700166 return mutex_lock_interruptible(&tpg->tpg_access_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000167}
168
169void iscsit_put_tpg(struct iscsi_portal_group *tpg)
170{
171 mutex_unlock(&tpg->tpg_access_lock);
172}
173
174static void iscsit_clear_tpg_np_login_thread(
175 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700176 struct iscsi_portal_group *tpg,
177 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000178{
179 if (!tpg_np->tpg_np) {
180 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
181 return;
182 }
183
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700184 if (shutdown)
185 tpg_np->tpg_np->enabled = false;
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700186 iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000187}
188
Nicholas Bellinger5256ffd2014-06-03 18:28:25 -0700189static void iscsit_clear_tpg_np_login_threads(
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700190 struct iscsi_portal_group *tpg,
191 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000192{
193 struct iscsi_tpg_np *tpg_np;
194
195 spin_lock(&tpg->tpg_np_lock);
196 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
197 if (!tpg_np->tpg_np) {
198 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
199 continue;
200 }
201 spin_unlock(&tpg->tpg_np_lock);
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700202 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000203 spin_lock(&tpg->tpg_np_lock);
204 }
205 spin_unlock(&tpg->tpg_np_lock);
206}
207
208void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
209{
210 iscsi_print_params(tpg->param_list);
211}
212
213static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
214{
215 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
216
217 a->authentication = TA_AUTHENTICATION;
218 a->login_timeout = TA_LOGIN_TIMEOUT;
219 a->netif_timeout = TA_NETIF_TIMEOUT;
220 a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
221 a->generate_node_acls = TA_GENERATE_NODE_ACLS;
222 a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
223 a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
224 a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
Thomas Glanzmann4c54b6c2013-10-07 23:12:11 +0200225 a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
Nicholas Bellingerd1fa7a12013-11-20 11:57:18 -0800226 a->default_erl = TA_DEFAULT_ERL;
Sagi Grimberg5b168dc2014-02-19 17:50:19 +0200227 a->t10_pi = TA_DEFAULT_T10_PI;
Nicholas Bellinger901c04a2015-03-29 19:36:16 -0700228 a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
David Disseldorpa6415cd2015-08-01 00:10:12 -0700229 a->tpg_enabled_sendtargets = TA_DEFAULT_TPG_ENABLED_SENDTARGETS;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000230}
231
232int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
233{
234 if (tpg->tpg_state != TPG_STATE_FREE) {
235 pr_err("Unable to add iSCSI Target Portal Group: %d"
236 " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
237 return -EEXIST;
238 }
239 iscsit_set_default_tpg_attribs(tpg);
240
241 if (iscsi_create_default_params(&tpg->param_list) < 0)
242 goto err_out;
243
Andy Groverb7eec2c2013-10-09 11:05:57 -0700244 tpg->tpg_attrib.tpg = tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000245
246 spin_lock(&tpg->tpg_state_lock);
247 tpg->tpg_state = TPG_STATE_INACTIVE;
248 spin_unlock(&tpg->tpg_state_lock);
249
250 spin_lock(&tiqn->tiqn_tpg_lock);
251 list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
252 tiqn->tiqn_ntpgs++;
253 pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
254 tiqn->tiqn, tpg->tpgt);
255 spin_unlock(&tiqn->tiqn_tpg_lock);
256
257 return 0;
258err_out:
259 if (tpg->param_list) {
260 iscsi_release_param_list(tpg->param_list);
261 tpg->param_list = NULL;
262 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000263 return -ENOMEM;
264}
265
266int iscsit_tpg_del_portal_group(
267 struct iscsi_tiqn *tiqn,
268 struct iscsi_portal_group *tpg,
269 int force)
270{
271 u8 old_state = tpg->tpg_state;
272
273 spin_lock(&tpg->tpg_state_lock);
274 tpg->tpg_state = TPG_STATE_INACTIVE;
275 spin_unlock(&tpg->tpg_state_lock);
276
277 if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
278 pr_err("Unable to delete iSCSI Target Portal Group:"
279 " %hu while active sessions exist, and force=0\n",
280 tpg->tpgt);
281 tpg->tpg_state = old_state;
282 return -EPERM;
283 }
284
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000285 if (tpg->param_list) {
286 iscsi_release_param_list(tpg->param_list);
287 tpg->param_list = NULL;
288 }
289
290 core_tpg_deregister(&tpg->tpg_se_tpg);
291
292 spin_lock(&tpg->tpg_state_lock);
293 tpg->tpg_state = TPG_STATE_FREE;
294 spin_unlock(&tpg->tpg_state_lock);
295
296 spin_lock(&tiqn->tiqn_tpg_lock);
297 tiqn->tiqn_ntpgs--;
298 list_del(&tpg->tpg_list);
299 spin_unlock(&tiqn->tiqn_tpg_lock);
300
301 pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
302 tiqn->tiqn, tpg->tpgt);
303
304 kfree(tpg);
305 return 0;
306}
307
308int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
309{
310 struct iscsi_param *param;
311 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
Andy Grover617a0c22012-07-12 17:34:56 -0700312 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000313
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000314 if (tpg->tpg_state == TPG_STATE_ACTIVE) {
315 pr_err("iSCSI target portal group: %hu is already"
316 " active, ignoring request.\n", tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000317 return -EINVAL;
318 }
319 /*
320 * Make sure that AuthMethod does not contain None as an option
321 * unless explictly disabled. Set the default to CHAP if authentication
322 * is enforced (as per default), and remove the NONE option.
323 */
324 param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
Nicholas Bellingereceb4452017-06-01 20:21:33 -0700325 if (!param)
Andy Grover617a0c22012-07-12 17:34:56 -0700326 return -EINVAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000327
Andy Groverb7eec2c2013-10-09 11:05:57 -0700328 if (tpg->tpg_attrib.authentication) {
Andy Grover617a0c22012-07-12 17:34:56 -0700329 if (!strcmp(param->value, NONE)) {
330 ret = iscsi_update_param_value(param, CHAP);
331 if (ret)
332 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000333 }
Andy Grover617a0c22012-07-12 17:34:56 -0700334
335 ret = iscsit_ta_authentication(tpg, 1);
336 if (ret < 0)
337 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000338 }
339
Nicholas Bellingereceb4452017-06-01 20:21:33 -0700340 spin_lock(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000341 tpg->tpg_state = TPG_STATE_ACTIVE;
342 spin_unlock(&tpg->tpg_state_lock);
343
344 spin_lock(&tiqn->tiqn_tpg_lock);
345 tiqn->tiqn_active_tpgs++;
346 pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
347 tpg->tpgt);
348 spin_unlock(&tiqn->tiqn_tpg_lock);
349
350 return 0;
Andy Grover617a0c22012-07-12 17:34:56 -0700351
352err:
Andy Grover617a0c22012-07-12 17:34:56 -0700353 return ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000354}
355
356int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
357{
358 struct iscsi_tiqn *tiqn;
359 u8 old_state = tpg->tpg_state;
360
361 spin_lock(&tpg->tpg_state_lock);
362 if (tpg->tpg_state == TPG_STATE_INACTIVE) {
363 pr_err("iSCSI Target Portal Group: %hu is already"
364 " inactive, ignoring request.\n", tpg->tpgt);
365 spin_unlock(&tpg->tpg_state_lock);
366 return -EINVAL;
367 }
368 tpg->tpg_state = TPG_STATE_INACTIVE;
369 spin_unlock(&tpg->tpg_state_lock);
370
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700371 iscsit_clear_tpg_np_login_threads(tpg, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000372
373 if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
374 spin_lock(&tpg->tpg_state_lock);
375 tpg->tpg_state = old_state;
376 spin_unlock(&tpg->tpg_state_lock);
377 pr_err("Unable to disable iSCSI Target Portal Group:"
378 " %hu while active sessions exist, and force=0\n",
379 tpg->tpgt);
380 return -EPERM;
381 }
382
383 tiqn = tpg->tpg_tiqn;
384 if (!tiqn || (tpg == iscsit_global->discovery_tpg))
385 return 0;
386
387 spin_lock(&tiqn->tiqn_tpg_lock);
388 tiqn->tiqn_active_tpgs--;
389 pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
390 tpg->tpgt);
391 spin_unlock(&tiqn->tiqn_tpg_lock);
392
393 return 0;
394}
395
396struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
397 struct iscsi_session *sess)
398{
399 struct se_session *se_sess = sess->se_sess;
400 struct se_node_acl *se_nacl = se_sess->se_node_acl;
401 struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
402 se_node_acl);
403
404 return &acl->node_attrib;
405}
406
407struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
408 struct iscsi_tpg_np *tpg_np,
409 int network_transport)
410{
411 struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
412
413 spin_lock(&tpg_np->tpg_np_parent_lock);
414 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
415 &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
416 if (tpg_np_child->tpg_np->np_network_transport ==
417 network_transport) {
418 spin_unlock(&tpg_np->tpg_np_parent_lock);
419 return tpg_np_child;
420 }
421 }
422 spin_unlock(&tpg_np->tpg_np_parent_lock);
423
424 return NULL;
425}
426
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800427static bool iscsit_tpg_check_network_portal(
428 struct iscsi_tiqn *tiqn,
Andy Grover13a3cf02015-08-24 10:26:06 -0700429 struct sockaddr_storage *sockaddr,
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800430 int network_transport)
431{
432 struct iscsi_portal_group *tpg;
433 struct iscsi_tpg_np *tpg_np;
434 struct iscsi_np *np;
435 bool match = false;
436
437 spin_lock(&tiqn->tiqn_tpg_lock);
438 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
439
440 spin_lock(&tpg->tpg_np_lock);
441 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
442 np = tpg_np->tpg_np;
443
444 match = iscsit_check_np_match(sockaddr, np,
445 network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200446 if (match)
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800447 break;
448 }
449 spin_unlock(&tpg->tpg_np_lock);
450 }
451 spin_unlock(&tiqn->tiqn_tpg_lock);
452
453 return match;
454}
455
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000456struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
457 struct iscsi_portal_group *tpg,
Andy Grover13a3cf02015-08-24 10:26:06 -0700458 struct sockaddr_storage *sockaddr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000459 struct iscsi_tpg_np *tpg_np_parent,
460 int network_transport)
461{
462 struct iscsi_np *np;
463 struct iscsi_tpg_np *tpg_np;
464
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800465 if (!tpg_np_parent) {
466 if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200467 network_transport)) {
Andy Grover76c28f12015-08-24 10:26:03 -0700468 pr_err("Network Portal: %pISc already exists on a"
469 " different TPG on %s\n", sockaddr,
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800470 tpg->tpg_tiqn->tiqn);
471 return ERR_PTR(-EEXIST);
472 }
473 }
474
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000475 tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
476 if (!tpg_np) {
477 pr_err("Unable to allocate memory for"
478 " struct iscsi_tpg_np.\n");
479 return ERR_PTR(-ENOMEM);
480 }
481
Andy Grover76c28f12015-08-24 10:26:03 -0700482 np = iscsit_add_np(sockaddr, network_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000483 if (IS_ERR(np)) {
484 kfree(tpg_np);
485 return ERR_CAST(np);
486 }
487
488 INIT_LIST_HEAD(&tpg_np->tpg_np_list);
489 INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
490 INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
491 spin_lock_init(&tpg_np->tpg_np_parent_lock);
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700492 init_completion(&tpg_np->tpg_np_comp);
493 kref_init(&tpg_np->tpg_np_kref);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000494 tpg_np->tpg_np = np;
495 tpg_np->tpg = tpg;
496
497 spin_lock(&tpg->tpg_np_lock);
498 list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
499 tpg->num_tpg_nps++;
500 if (tpg->tpg_tiqn)
501 tpg->tpg_tiqn->tiqn_num_tpg_nps++;
502 spin_unlock(&tpg->tpg_np_lock);
503
504 if (tpg_np_parent) {
505 tpg_np->tpg_np_parent = tpg_np_parent;
506 spin_lock(&tpg_np_parent->tpg_np_parent_lock);
507 list_add_tail(&tpg_np->tpg_np_child_list,
508 &tpg_np_parent->tpg_np_parent_list);
509 spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
510 }
511
Andy Grover69d75572015-08-24 10:26:04 -0700512 pr_debug("CORE[%s] - Added Network Portal: %pISpc,%hu on %s\n",
513 tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800514 np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000515
516 return tpg_np;
517}
518
519static int iscsit_tpg_release_np(
520 struct iscsi_tpg_np *tpg_np,
521 struct iscsi_portal_group *tpg,
522 struct iscsi_np *np)
523{
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700524 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000525
Andy Grover69d75572015-08-24 10:26:04 -0700526 pr_debug("CORE[%s] - Removed Network Portal: %pISpc,%hu on %s\n",
527 tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800528 np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000529
530 tpg_np->tpg_np = NULL;
531 tpg_np->tpg = NULL;
532 kfree(tpg_np);
533 /*
534 * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
535 */
536 return iscsit_del_np(np);
537}
538
539int iscsit_tpg_del_network_portal(
540 struct iscsi_portal_group *tpg,
541 struct iscsi_tpg_np *tpg_np)
542{
543 struct iscsi_np *np;
544 struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
545 int ret = 0;
546
547 np = tpg_np->tpg_np;
548 if (!np) {
549 pr_err("Unable to locate struct iscsi_np from"
550 " struct iscsi_tpg_np\n");
551 return -EINVAL;
552 }
553
554 if (!tpg_np->tpg_np_parent) {
555 /*
556 * We are the parent tpg network portal. Release all of the
557 * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
558 * list first.
559 */
560 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
561 &tpg_np->tpg_np_parent_list,
562 tpg_np_child_list) {
563 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
564 if (ret < 0)
565 pr_err("iscsit_tpg_del_network_portal()"
566 " failed: %d\n", ret);
567 }
568 } else {
569 /*
570 * We are not the parent ISCSI_TCP tpg network portal. Release
571 * our own network portals from the child list.
572 */
573 spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
574 list_del(&tpg_np->tpg_np_child_list);
575 spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
576 }
577
578 spin_lock(&tpg->tpg_np_lock);
579 list_del(&tpg_np->tpg_np_list);
580 tpg->num_tpg_nps--;
581 if (tpg->tpg_tiqn)
582 tpg->tpg_tiqn->tiqn_num_tpg_nps--;
583 spin_unlock(&tpg->tpg_np_lock);
584
585 return iscsit_tpg_release_np(tpg_np, tpg, np);
586}
587
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000588int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
589{
590 unsigned char buf1[256], buf2[256], *none = NULL;
591 int len;
592 struct iscsi_param *param;
593 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
594
595 if ((authentication != 1) && (authentication != 0)) {
596 pr_err("Illegal value for authentication parameter:"
597 " %u, ignoring request.\n", authentication);
Andy Grover617a0c22012-07-12 17:34:56 -0700598 return -EINVAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000599 }
600
601 memset(buf1, 0, sizeof(buf1));
602 memset(buf2, 0, sizeof(buf2));
603
604 param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
605 if (!param)
606 return -EINVAL;
607
608 if (authentication) {
609 snprintf(buf1, sizeof(buf1), "%s", param->value);
610 none = strstr(buf1, NONE);
611 if (!none)
612 goto out;
613 if (!strncmp(none + 4, ",", 1)) {
614 if (!strcmp(buf1, none))
615 sprintf(buf2, "%s", none+5);
616 else {
617 none--;
618 *none = '\0';
619 len = sprintf(buf2, "%s", buf1);
620 none += 5;
621 sprintf(buf2 + len, "%s", none);
622 }
623 } else {
624 none--;
625 *none = '\0';
626 sprintf(buf2, "%s", buf1);
627 }
628 if (iscsi_update_param_value(param, buf2) < 0)
629 return -EINVAL;
630 } else {
631 snprintf(buf1, sizeof(buf1), "%s", param->value);
632 none = strstr(buf1, NONE);
Andy Groveree1b1b92012-07-12 17:34:54 -0700633 if (none)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000634 goto out;
635 strncat(buf1, ",", strlen(","));
636 strncat(buf1, NONE, strlen(NONE));
637 if (iscsi_update_param_value(param, buf1) < 0)
638 return -EINVAL;
639 }
640
641out:
642 a->authentication = authentication;
643 pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
644 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
645
646 return 0;
647}
648
649int iscsit_ta_login_timeout(
650 struct iscsi_portal_group *tpg,
651 u32 login_timeout)
652{
653 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
654
655 if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
656 pr_err("Requested Login Timeout %u larger than maximum"
657 " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
658 return -EINVAL;
659 } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
660 pr_err("Requested Logout Timeout %u smaller than"
661 " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
662 return -EINVAL;
663 }
664
665 a->login_timeout = login_timeout;
666 pr_debug("Set Logout Timeout to %u for Target Portal Group"
667 " %hu\n", a->login_timeout, tpg->tpgt);
668
669 return 0;
670}
671
672int iscsit_ta_netif_timeout(
673 struct iscsi_portal_group *tpg,
674 u32 netif_timeout)
675{
676 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
677
678 if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
679 pr_err("Requested Network Interface Timeout %u larger"
680 " than maximum %u\n", netif_timeout,
681 TA_NETIF_TIMEOUT_MAX);
682 return -EINVAL;
683 } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
684 pr_err("Requested Network Interface Timeout %u smaller"
685 " than minimum %u\n", netif_timeout,
686 TA_NETIF_TIMEOUT_MIN);
687 return -EINVAL;
688 }
689
690 a->netif_timeout = netif_timeout;
691 pr_debug("Set Network Interface Timeout to %u for"
692 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
693
694 return 0;
695}
696
697int iscsit_ta_generate_node_acls(
698 struct iscsi_portal_group *tpg,
699 u32 flag)
700{
701 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
702
703 if ((flag != 0) && (flag != 1)) {
704 pr_err("Illegal value %d\n", flag);
705 return -EINVAL;
706 }
707
708 a->generate_node_acls = flag;
709 pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
710 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
711
Nicholas Bellinger38b11ba2012-09-30 12:20:02 -0700712 if (flag == 1 && a->cache_dynamic_acls == 0) {
713 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
714 "generate_node_acls=1\n");
715 a->cache_dynamic_acls = 1;
716 }
717
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000718 return 0;
719}
720
721int iscsit_ta_default_cmdsn_depth(
722 struct iscsi_portal_group *tpg,
723 u32 tcq_depth)
724{
725 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
726
727 if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
728 pr_err("Requested Default Queue Depth: %u larger"
729 " than maximum %u\n", tcq_depth,
730 TA_DEFAULT_CMDSN_DEPTH_MAX);
731 return -EINVAL;
732 } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
733 pr_err("Requested Default Queue Depth: %u smaller"
734 " than minimum %u\n", tcq_depth,
735 TA_DEFAULT_CMDSN_DEPTH_MIN);
736 return -EINVAL;
737 }
738
739 a->default_cmdsn_depth = tcq_depth;
740 pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
741 tpg->tpgt, a->default_cmdsn_depth);
742
743 return 0;
744}
745
746int iscsit_ta_cache_dynamic_acls(
747 struct iscsi_portal_group *tpg,
748 u32 flag)
749{
750 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
751
752 if ((flag != 0) && (flag != 1)) {
753 pr_err("Illegal value %d\n", flag);
754 return -EINVAL;
755 }
756
Nicholas Bellinger38b11ba2012-09-30 12:20:02 -0700757 if (a->generate_node_acls == 1 && flag == 0) {
758 pr_debug("Skipping cache_dynamic_acls=0 when"
759 " generate_node_acls=1\n");
760 return 0;
761 }
762
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000763 a->cache_dynamic_acls = flag;
764 pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
765 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
766 "Enabled" : "Disabled");
767
768 return 0;
769}
770
771int iscsit_ta_demo_mode_write_protect(
772 struct iscsi_portal_group *tpg,
773 u32 flag)
774{
775 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
776
777 if ((flag != 0) && (flag != 1)) {
778 pr_err("Illegal value %d\n", flag);
779 return -EINVAL;
780 }
781
782 a->demo_mode_write_protect = flag;
783 pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
784 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
785
786 return 0;
787}
788
789int iscsit_ta_prod_mode_write_protect(
790 struct iscsi_portal_group *tpg,
791 u32 flag)
792{
793 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
794
795 if ((flag != 0) && (flag != 1)) {
796 pr_err("Illegal value %d\n", flag);
797 return -EINVAL;
798 }
799
800 a->prod_mode_write_protect = flag;
801 pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
802 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
803 "ON" : "OFF");
804
805 return 0;
806}
Thomas Glanzmann4c54b6c2013-10-07 23:12:11 +0200807
808int iscsit_ta_demo_mode_discovery(
809 struct iscsi_portal_group *tpg,
810 u32 flag)
811{
812 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
813
814 if ((flag != 0) && (flag != 1)) {
815 pr_err("Illegal value %d\n", flag);
816 return -EINVAL;
817 }
818
819 a->demo_mode_discovery = flag;
820 pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
821 " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
822 "ON" : "OFF");
823
824 return 0;
825}
Nicholas Bellingerd1fa7a12013-11-20 11:57:18 -0800826
827int iscsit_ta_default_erl(
828 struct iscsi_portal_group *tpg,
829 u32 default_erl)
830{
831 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
832
833 if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
834 pr_err("Illegal value for default_erl: %u\n", default_erl);
835 return -EINVAL;
836 }
837
838 a->default_erl = default_erl;
839 pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
840
841 return 0;
842}
Sagi Grimberg80851762014-02-19 17:50:20 +0200843
844int iscsit_ta_t10_pi(
845 struct iscsi_portal_group *tpg,
846 u32 flag)
847{
848 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
849
850 if ((flag != 0) && (flag != 1)) {
851 pr_err("Illegal value %d\n", flag);
852 return -EINVAL;
853 }
854
855 a->t10_pi = flag;
856 pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
857 " %s\n", tpg->tpgt, (a->t10_pi) ?
858 "ON" : "OFF");
859
860 return 0;
861}
Nicholas Bellinger901c04a2015-03-29 19:36:16 -0700862
863int iscsit_ta_fabric_prot_type(
864 struct iscsi_portal_group *tpg,
865 u32 prot_type)
866{
867 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
868
869 if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
870 pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
871 return -EINVAL;
872 }
873
874 a->fabric_prot_type = prot_type;
875 pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
876 tpg->tpgt, prot_type);
877
878 return 0;
879}
David Disseldorpa6415cd2015-08-01 00:10:12 -0700880
881int iscsit_ta_tpg_enabled_sendtargets(
882 struct iscsi_portal_group *tpg,
883 u32 flag)
884{
885 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
886
887 if ((flag != 0) && (flag != 1)) {
888 pr_err("Illegal value %d\n", flag);
889 return -EINVAL;
890 }
891
892 a->tpg_enabled_sendtargets = flag;
893 pr_debug("iSCSI_TPG[%hu] - TPG enabled bit required for SendTargets:"
894 " %s\n", tpg->tpgt, (a->tpg_enabled_sendtargets) ? "ON" : "OFF");
895
896 return 0;
897}