blob: eb96b20dc09e13ffe32e226df38b73241f176a63 [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
19#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050020#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000021#include <target/target_core_configfs.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000022
23#include "iscsi_target_core.h"
24#include "iscsi_target_erl0.h"
25#include "iscsi_target_login.h"
26#include "iscsi_target_nodeattrib.h"
27#include "iscsi_target_tpg.h"
28#include "iscsi_target_util.h"
29#include "iscsi_target.h"
30#include "iscsi_target_parameters.h"
31
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080032#include <target/iscsi/iscsi_transport.h>
33
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034struct 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);
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -070050 sema_init(&tpg->np_login_sem, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000051 spin_lock_init(&tpg->tpg_state_lock);
52 spin_lock_init(&tpg->tpg_np_lock);
53
54 return tpg;
55}
56
57static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
58
59int 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,
Jörn Engel8359cf42011-11-24 02:05:51 +010073 NULL, &tpg->tpg_se_tpg, tpg,
Nicholas Bellingere48354c2011-07-23 06:43:04 +000074 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;
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{
166 int ret;
167
168 ret = mutex_lock_interruptible(&tpg->tpg_access_lock);
169 return ((ret != 0) || signal_pending(current)) ? -1 : 0;
170}
171
172void iscsit_put_tpg(struct iscsi_portal_group *tpg)
173{
174 mutex_unlock(&tpg->tpg_access_lock);
175}
176
177static void iscsit_clear_tpg_np_login_thread(
178 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700179 struct iscsi_portal_group *tpg,
180 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000181{
182 if (!tpg_np->tpg_np) {
183 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
184 return;
185 }
186
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700187 iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000188}
189
190void iscsit_clear_tpg_np_login_threads(
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700191 struct iscsi_portal_group *tpg,
192 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000193{
194 struct iscsi_tpg_np *tpg_np;
195
196 spin_lock(&tpg->tpg_np_lock);
197 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
198 if (!tpg_np->tpg_np) {
199 pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
200 continue;
201 }
202 spin_unlock(&tpg->tpg_np_lock);
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700203 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000204 spin_lock(&tpg->tpg_np_lock);
205 }
206 spin_unlock(&tpg->tpg_np_lock);
207}
208
209void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
210{
211 iscsi_print_params(tpg->param_list);
212}
213
214static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
215{
216 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
217
218 a->authentication = TA_AUTHENTICATION;
219 a->login_timeout = TA_LOGIN_TIMEOUT;
220 a->netif_timeout = TA_NETIF_TIMEOUT;
221 a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
222 a->generate_node_acls = TA_GENERATE_NODE_ACLS;
223 a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
224 a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
225 a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
Thomas Glanzmann4c54b6c2013-10-07 23:12:11 +0200226 a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
Nicholas Bellingerd1fa7a12013-11-20 11:57:18 -0800227 a->default_erl = TA_DEFAULT_ERL;
Sagi Grimberg5b168dc2014-02-19 17:50:19 +0200228 a->t10_pi = TA_DEFAULT_T10_PI;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000229}
230
231int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
232{
233 if (tpg->tpg_state != TPG_STATE_FREE) {
234 pr_err("Unable to add iSCSI Target Portal Group: %d"
235 " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
236 return -EEXIST;
237 }
238 iscsit_set_default_tpg_attribs(tpg);
239
240 if (iscsi_create_default_params(&tpg->param_list) < 0)
241 goto err_out;
242
Andy Groverb7eec2c2013-10-09 11:05:57 -0700243 tpg->tpg_attrib.tpg = tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000244
245 spin_lock(&tpg->tpg_state_lock);
246 tpg->tpg_state = TPG_STATE_INACTIVE;
247 spin_unlock(&tpg->tpg_state_lock);
248
249 spin_lock(&tiqn->tiqn_tpg_lock);
250 list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
251 tiqn->tiqn_ntpgs++;
252 pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
253 tiqn->tiqn, tpg->tpgt);
254 spin_unlock(&tiqn->tiqn_tpg_lock);
255
256 return 0;
257err_out:
258 if (tpg->param_list) {
259 iscsi_release_param_list(tpg->param_list);
260 tpg->param_list = NULL;
261 }
262 kfree(tpg);
263 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
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700277 iscsit_clear_tpg_np_login_threads(tpg, true);
278
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000279 if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
280 pr_err("Unable to delete iSCSI Target Portal Group:"
281 " %hu while active sessions exist, and force=0\n",
282 tpg->tpgt);
283 tpg->tpg_state = old_state;
284 return -EPERM;
285 }
286
287 core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
288
289 if (tpg->param_list) {
290 iscsi_release_param_list(tpg->param_list);
291 tpg->param_list = NULL;
292 }
293
294 core_tpg_deregister(&tpg->tpg_se_tpg);
295
296 spin_lock(&tpg->tpg_state_lock);
297 tpg->tpg_state = TPG_STATE_FREE;
298 spin_unlock(&tpg->tpg_state_lock);
299
300 spin_lock(&tiqn->tiqn_tpg_lock);
301 tiqn->tiqn_ntpgs--;
302 list_del(&tpg->tpg_list);
303 spin_unlock(&tiqn->tiqn_tpg_lock);
304
305 pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
306 tiqn->tiqn, tpg->tpgt);
307
308 kfree(tpg);
309 return 0;
310}
311
312int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
313{
314 struct iscsi_param *param;
315 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
Andy Grover617a0c22012-07-12 17:34:56 -0700316 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000317
318 spin_lock(&tpg->tpg_state_lock);
319 if (tpg->tpg_state == TPG_STATE_ACTIVE) {
320 pr_err("iSCSI target portal group: %hu is already"
321 " active, ignoring request.\n", tpg->tpgt);
322 spin_unlock(&tpg->tpg_state_lock);
323 return -EINVAL;
324 }
325 /*
326 * Make sure that AuthMethod does not contain None as an option
327 * unless explictly disabled. Set the default to CHAP if authentication
328 * is enforced (as per default), and remove the NONE option.
329 */
330 param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
331 if (!param) {
332 spin_unlock(&tpg->tpg_state_lock);
Andy Grover617a0c22012-07-12 17:34:56 -0700333 return -EINVAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000334 }
335
Andy Groverb7eec2c2013-10-09 11:05:57 -0700336 if (tpg->tpg_attrib.authentication) {
Andy Grover617a0c22012-07-12 17:34:56 -0700337 if (!strcmp(param->value, NONE)) {
338 ret = iscsi_update_param_value(param, CHAP);
339 if (ret)
340 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000341 }
Andy Grover617a0c22012-07-12 17:34:56 -0700342
343 ret = iscsit_ta_authentication(tpg, 1);
344 if (ret < 0)
345 goto err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000346 }
347
348 tpg->tpg_state = TPG_STATE_ACTIVE;
349 spin_unlock(&tpg->tpg_state_lock);
350
351 spin_lock(&tiqn->tiqn_tpg_lock);
352 tiqn->tiqn_active_tpgs++;
353 pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
354 tpg->tpgt);
355 spin_unlock(&tiqn->tiqn_tpg_lock);
356
357 return 0;
Andy Grover617a0c22012-07-12 17:34:56 -0700358
359err:
360 spin_unlock(&tpg->tpg_state_lock);
361 return ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000362}
363
364int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
365{
366 struct iscsi_tiqn *tiqn;
367 u8 old_state = tpg->tpg_state;
368
369 spin_lock(&tpg->tpg_state_lock);
370 if (tpg->tpg_state == TPG_STATE_INACTIVE) {
371 pr_err("iSCSI Target Portal Group: %hu is already"
372 " inactive, ignoring request.\n", tpg->tpgt);
373 spin_unlock(&tpg->tpg_state_lock);
374 return -EINVAL;
375 }
376 tpg->tpg_state = TPG_STATE_INACTIVE;
377 spin_unlock(&tpg->tpg_state_lock);
378
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700379 iscsit_clear_tpg_np_login_threads(tpg, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000380
381 if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
382 spin_lock(&tpg->tpg_state_lock);
383 tpg->tpg_state = old_state;
384 spin_unlock(&tpg->tpg_state_lock);
385 pr_err("Unable to disable iSCSI Target Portal Group:"
386 " %hu while active sessions exist, and force=0\n",
387 tpg->tpgt);
388 return -EPERM;
389 }
390
391 tiqn = tpg->tpg_tiqn;
392 if (!tiqn || (tpg == iscsit_global->discovery_tpg))
393 return 0;
394
395 spin_lock(&tiqn->tiqn_tpg_lock);
396 tiqn->tiqn_active_tpgs--;
397 pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
398 tpg->tpgt);
399 spin_unlock(&tiqn->tiqn_tpg_lock);
400
401 return 0;
402}
403
404struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
405 struct iscsi_session *sess)
406{
407 struct se_session *se_sess = sess->se_sess;
408 struct se_node_acl *se_nacl = se_sess->se_node_acl;
409 struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
410 se_node_acl);
411
412 return &acl->node_attrib;
413}
414
415struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
416 struct iscsi_tpg_np *tpg_np,
417 int network_transport)
418{
419 struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
420
421 spin_lock(&tpg_np->tpg_np_parent_lock);
422 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
423 &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
424 if (tpg_np_child->tpg_np->np_network_transport ==
425 network_transport) {
426 spin_unlock(&tpg_np->tpg_np_parent_lock);
427 return tpg_np_child;
428 }
429 }
430 spin_unlock(&tpg_np->tpg_np_parent_lock);
431
432 return NULL;
433}
434
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800435static bool iscsit_tpg_check_network_portal(
436 struct iscsi_tiqn *tiqn,
437 struct __kernel_sockaddr_storage *sockaddr,
438 int network_transport)
439{
440 struct iscsi_portal_group *tpg;
441 struct iscsi_tpg_np *tpg_np;
442 struct iscsi_np *np;
443 bool match = false;
444
445 spin_lock(&tiqn->tiqn_tpg_lock);
446 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
447
448 spin_lock(&tpg->tpg_np_lock);
449 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
450 np = tpg_np->tpg_np;
451
452 match = iscsit_check_np_match(sockaddr, np,
453 network_transport);
454 if (match == true)
455 break;
456 }
457 spin_unlock(&tpg->tpg_np_lock);
458 }
459 spin_unlock(&tiqn->tiqn_tpg_lock);
460
461 return match;
462}
463
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000464struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
465 struct iscsi_portal_group *tpg,
466 struct __kernel_sockaddr_storage *sockaddr,
467 char *ip_str,
468 struct iscsi_tpg_np *tpg_np_parent,
469 int network_transport)
470{
471 struct iscsi_np *np;
472 struct iscsi_tpg_np *tpg_np;
473
Nicholas Bellinger6e545932013-02-18 21:01:02 -0800474 if (!tpg_np_parent) {
475 if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
476 network_transport) == true) {
477 pr_err("Network Portal: %s already exists on a"
478 " different TPG on %s\n", ip_str,
479 tpg->tpg_tiqn->tiqn);
480 return ERR_PTR(-EEXIST);
481 }
482 }
483
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000484 tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
485 if (!tpg_np) {
486 pr_err("Unable to allocate memory for"
487 " struct iscsi_tpg_np.\n");
488 return ERR_PTR(-ENOMEM);
489 }
490
491 np = iscsit_add_np(sockaddr, ip_str, network_transport);
492 if (IS_ERR(np)) {
493 kfree(tpg_np);
494 return ERR_CAST(np);
495 }
496
497 INIT_LIST_HEAD(&tpg_np->tpg_np_list);
498 INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
499 INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
500 spin_lock_init(&tpg_np->tpg_np_parent_lock);
Nicholas Bellingerd381a802013-08-15 13:40:17 -0700501 init_completion(&tpg_np->tpg_np_comp);
502 kref_init(&tpg_np->tpg_np_kref);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000503 tpg_np->tpg_np = np;
Sagi Grimberg5b168dc2014-02-19 17:50:19 +0200504 np->tpg_np = tpg_np;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000505 tpg_np->tpg = tpg;
506
507 spin_lock(&tpg->tpg_np_lock);
508 list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
509 tpg->num_tpg_nps++;
510 if (tpg->tpg_tiqn)
511 tpg->tpg_tiqn->tiqn_num_tpg_nps++;
512 spin_unlock(&tpg->tpg_np_lock);
513
514 if (tpg_np_parent) {
515 tpg_np->tpg_np_parent = tpg_np_parent;
516 spin_lock(&tpg_np_parent->tpg_np_parent_lock);
517 list_add_tail(&tpg_np->tpg_np_child_list,
518 &tpg_np_parent->tpg_np_parent_list);
519 spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
520 }
521
522 pr_debug("CORE[%s] - Added Network Portal: %s:%hu,%hu on %s\n",
523 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800524 np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000525
526 return tpg_np;
527}
528
529static int iscsit_tpg_release_np(
530 struct iscsi_tpg_np *tpg_np,
531 struct iscsi_portal_group *tpg,
532 struct iscsi_np *np)
533{
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700534 iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000535
536 pr_debug("CORE[%s] - Removed Network Portal: %s:%hu,%hu on %s\n",
537 tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800538 np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000539
540 tpg_np->tpg_np = NULL;
541 tpg_np->tpg = NULL;
542 kfree(tpg_np);
543 /*
544 * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
545 */
546 return iscsit_del_np(np);
547}
548
549int iscsit_tpg_del_network_portal(
550 struct iscsi_portal_group *tpg,
551 struct iscsi_tpg_np *tpg_np)
552{
553 struct iscsi_np *np;
554 struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
555 int ret = 0;
556
557 np = tpg_np->tpg_np;
558 if (!np) {
559 pr_err("Unable to locate struct iscsi_np from"
560 " struct iscsi_tpg_np\n");
561 return -EINVAL;
562 }
563
564 if (!tpg_np->tpg_np_parent) {
565 /*
566 * We are the parent tpg network portal. Release all of the
567 * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
568 * list first.
569 */
570 list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
571 &tpg_np->tpg_np_parent_list,
572 tpg_np_child_list) {
573 ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
574 if (ret < 0)
575 pr_err("iscsit_tpg_del_network_portal()"
576 " failed: %d\n", ret);
577 }
578 } else {
579 /*
580 * We are not the parent ISCSI_TCP tpg network portal. Release
581 * our own network portals from the child list.
582 */
583 spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
584 list_del(&tpg_np->tpg_np_child_list);
585 spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
586 }
587
588 spin_lock(&tpg->tpg_np_lock);
589 list_del(&tpg_np->tpg_np_list);
590 tpg->num_tpg_nps--;
591 if (tpg->tpg_tiqn)
592 tpg->tpg_tiqn->tiqn_num_tpg_nps--;
593 spin_unlock(&tpg->tpg_np_lock);
594
595 return iscsit_tpg_release_np(tpg_np, tpg, np);
596}
597
598int iscsit_tpg_set_initiator_node_queue_depth(
599 struct iscsi_portal_group *tpg,
600 unsigned char *initiatorname,
601 u32 queue_depth,
602 int force)
603{
604 return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
605 initiatorname, queue_depth, force);
606}
607
608int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
609{
610 unsigned char buf1[256], buf2[256], *none = NULL;
611 int len;
612 struct iscsi_param *param;
613 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
614
615 if ((authentication != 1) && (authentication != 0)) {
616 pr_err("Illegal value for authentication parameter:"
617 " %u, ignoring request.\n", authentication);
Andy Grover617a0c22012-07-12 17:34:56 -0700618 return -EINVAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000619 }
620
621 memset(buf1, 0, sizeof(buf1));
622 memset(buf2, 0, sizeof(buf2));
623
624 param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
625 if (!param)
626 return -EINVAL;
627
628 if (authentication) {
629 snprintf(buf1, sizeof(buf1), "%s", param->value);
630 none = strstr(buf1, NONE);
631 if (!none)
632 goto out;
633 if (!strncmp(none + 4, ",", 1)) {
634 if (!strcmp(buf1, none))
635 sprintf(buf2, "%s", none+5);
636 else {
637 none--;
638 *none = '\0';
639 len = sprintf(buf2, "%s", buf1);
640 none += 5;
641 sprintf(buf2 + len, "%s", none);
642 }
643 } else {
644 none--;
645 *none = '\0';
646 sprintf(buf2, "%s", buf1);
647 }
648 if (iscsi_update_param_value(param, buf2) < 0)
649 return -EINVAL;
650 } else {
651 snprintf(buf1, sizeof(buf1), "%s", param->value);
652 none = strstr(buf1, NONE);
Andy Groveree1b1b92012-07-12 17:34:54 -0700653 if (none)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000654 goto out;
655 strncat(buf1, ",", strlen(","));
656 strncat(buf1, NONE, strlen(NONE));
657 if (iscsi_update_param_value(param, buf1) < 0)
658 return -EINVAL;
659 }
660
661out:
662 a->authentication = authentication;
663 pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
664 a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
665
666 return 0;
667}
668
669int iscsit_ta_login_timeout(
670 struct iscsi_portal_group *tpg,
671 u32 login_timeout)
672{
673 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
674
675 if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
676 pr_err("Requested Login Timeout %u larger than maximum"
677 " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
678 return -EINVAL;
679 } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
680 pr_err("Requested Logout Timeout %u smaller than"
681 " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
682 return -EINVAL;
683 }
684
685 a->login_timeout = login_timeout;
686 pr_debug("Set Logout Timeout to %u for Target Portal Group"
687 " %hu\n", a->login_timeout, tpg->tpgt);
688
689 return 0;
690}
691
692int iscsit_ta_netif_timeout(
693 struct iscsi_portal_group *tpg,
694 u32 netif_timeout)
695{
696 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
697
698 if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
699 pr_err("Requested Network Interface Timeout %u larger"
700 " than maximum %u\n", netif_timeout,
701 TA_NETIF_TIMEOUT_MAX);
702 return -EINVAL;
703 } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
704 pr_err("Requested Network Interface Timeout %u smaller"
705 " than minimum %u\n", netif_timeout,
706 TA_NETIF_TIMEOUT_MIN);
707 return -EINVAL;
708 }
709
710 a->netif_timeout = netif_timeout;
711 pr_debug("Set Network Interface Timeout to %u for"
712 " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
713
714 return 0;
715}
716
717int iscsit_ta_generate_node_acls(
718 struct iscsi_portal_group *tpg,
719 u32 flag)
720{
721 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
722
723 if ((flag != 0) && (flag != 1)) {
724 pr_err("Illegal value %d\n", flag);
725 return -EINVAL;
726 }
727
728 a->generate_node_acls = flag;
729 pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
730 tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
731
Nicholas Bellinger38b11ba2012-09-30 12:20:02 -0700732 if (flag == 1 && a->cache_dynamic_acls == 0) {
733 pr_debug("Explicitly setting cache_dynamic_acls=1 when "
734 "generate_node_acls=1\n");
735 a->cache_dynamic_acls = 1;
736 }
737
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000738 return 0;
739}
740
741int iscsit_ta_default_cmdsn_depth(
742 struct iscsi_portal_group *tpg,
743 u32 tcq_depth)
744{
745 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
746
747 if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
748 pr_err("Requested Default Queue Depth: %u larger"
749 " than maximum %u\n", tcq_depth,
750 TA_DEFAULT_CMDSN_DEPTH_MAX);
751 return -EINVAL;
752 } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
753 pr_err("Requested Default Queue Depth: %u smaller"
754 " than minimum %u\n", tcq_depth,
755 TA_DEFAULT_CMDSN_DEPTH_MIN);
756 return -EINVAL;
757 }
758
759 a->default_cmdsn_depth = tcq_depth;
760 pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
761 tpg->tpgt, a->default_cmdsn_depth);
762
763 return 0;
764}
765
766int iscsit_ta_cache_dynamic_acls(
767 struct iscsi_portal_group *tpg,
768 u32 flag)
769{
770 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
771
772 if ((flag != 0) && (flag != 1)) {
773 pr_err("Illegal value %d\n", flag);
774 return -EINVAL;
775 }
776
Nicholas Bellinger38b11ba2012-09-30 12:20:02 -0700777 if (a->generate_node_acls == 1 && flag == 0) {
778 pr_debug("Skipping cache_dynamic_acls=0 when"
779 " generate_node_acls=1\n");
780 return 0;
781 }
782
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000783 a->cache_dynamic_acls = flag;
784 pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
785 " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
786 "Enabled" : "Disabled");
787
788 return 0;
789}
790
791int iscsit_ta_demo_mode_write_protect(
792 struct iscsi_portal_group *tpg,
793 u32 flag)
794{
795 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
796
797 if ((flag != 0) && (flag != 1)) {
798 pr_err("Illegal value %d\n", flag);
799 return -EINVAL;
800 }
801
802 a->demo_mode_write_protect = flag;
803 pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
804 tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
805
806 return 0;
807}
808
809int iscsit_ta_prod_mode_write_protect(
810 struct iscsi_portal_group *tpg,
811 u32 flag)
812{
813 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
814
815 if ((flag != 0) && (flag != 1)) {
816 pr_err("Illegal value %d\n", flag);
817 return -EINVAL;
818 }
819
820 a->prod_mode_write_protect = flag;
821 pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
822 " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
823 "ON" : "OFF");
824
825 return 0;
826}
Thomas Glanzmann4c54b6c2013-10-07 23:12:11 +0200827
828int iscsit_ta_demo_mode_discovery(
829 struct iscsi_portal_group *tpg,
830 u32 flag)
831{
832 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
833
834 if ((flag != 0) && (flag != 1)) {
835 pr_err("Illegal value %d\n", flag);
836 return -EINVAL;
837 }
838
839 a->demo_mode_discovery = flag;
840 pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
841 " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
842 "ON" : "OFF");
843
844 return 0;
845}
Nicholas Bellingerd1fa7a12013-11-20 11:57:18 -0800846
847int iscsit_ta_default_erl(
848 struct iscsi_portal_group *tpg,
849 u32 default_erl)
850{
851 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
852
853 if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
854 pr_err("Illegal value for default_erl: %u\n", default_erl);
855 return -EINVAL;
856 }
857
858 a->default_erl = default_erl;
859 pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
860
861 return 0;
862}
Sagi Grimberg80851762014-02-19 17:50:20 +0200863
864int iscsit_ta_t10_pi(
865 struct iscsi_portal_group *tpg,
866 u32 flag)
867{
868 struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
869
870 if ((flag != 0) && (flag != 1)) {
871 pr_err("Illegal value %d\n", flag);
872 return -EINVAL;
873 }
874
875 a->t10_pi = flag;
876 pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
877 " %s\n", tpg->tpgt, (a->t10_pi) ?
878 "ON" : "OFF");
879
880 return 0;
881}