Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.git] / drivers / target / target_core_tpg.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
c66ac9db
NB
2/*******************************************************************************
3 * Filename: target_core_tpg.c
4 *
5 * This file contains generic Target Portal Group related functions.
6 *
4c76251e 7 * (c) Copyright 2002-2013 Datera, Inc.
c66ac9db
NB
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
c66ac9db
NB
11 ******************************************************************************/
12
13#include <linux/net.h>
14#include <linux/string.h>
15#include <linux/timer.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
c66ac9db 18#include <linux/in.h>
c53181af 19#include <linux/export.h>
c66ac9db
NB
20#include <net/sock.h>
21#include <net/tcp.h>
ba929992 22#include <scsi/scsi_proto.h>
c66ac9db
NB
23
24#include <target/target_core_base.h>
c4795fb2
CH
25#include <target/target_core_backend.h>
26#include <target/target_core_fabric.h>
c66ac9db 27
e26d99ae 28#include "target_core_internal.h"
adf653f9 29#include "target_core_alua.h"
e2480563 30#include "target_core_pr.h"
e986a35a 31#include "target_core_ua.h"
e3d6f909
AG
32
33extern struct se_device *g_lun0_dev;
34
35static DEFINE_SPINLOCK(tpg_lock);
36static LIST_HEAD(tpg_list);
c66ac9db 37
c66ac9db
NB
38/* __core_tpg_get_initiator_node_acl():
39 *
403edd78 40 * mutex_lock(&tpg->acl_node_mutex); must be held when calling
c66ac9db
NB
41 */
42struct se_node_acl *__core_tpg_get_initiator_node_acl(
43 struct se_portal_group *tpg,
44 const char *initiatorname)
45{
46 struct se_node_acl *acl;
47
48 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
6708bb27 49 if (!strcmp(acl->initiatorname, initiatorname))
c66ac9db
NB
50 return acl;
51 }
52
53 return NULL;
54}
55
56/* core_tpg_get_initiator_node_acl():
57 *
58 *
59 */
60struct se_node_acl *core_tpg_get_initiator_node_acl(
61 struct se_portal_group *tpg,
62 unsigned char *initiatorname)
63{
64 struct se_node_acl *acl;
21aaa23b
NB
65 /*
66 * Obtain se_node_acl->acl_kref using fabric driver provided
67 * initiatorname[] during node acl endpoint lookup driven by
68 * new se_session login.
69 *
70 * The reference is held until se_session shutdown -> release
71 * occurs via fabric driver invoked transport_deregister_session()
72 * or transport_free_session() code.
73 */
403edd78 74 mutex_lock(&tpg->acl_node_mutex);
fcf29481 75 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
21aaa23b
NB
76 if (acl) {
77 if (!kref_get_unless_zero(&acl->acl_kref))
78 acl = NULL;
79 }
403edd78 80 mutex_unlock(&tpg->acl_node_mutex);
c66ac9db 81
fcf29481 82 return acl;
c66ac9db 83}
b3fde035 84EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
c66ac9db 85
e986a35a
HR
86void core_allocate_nexus_loss_ua(
87 struct se_node_acl *nacl)
88{
89 struct se_dev_entry *deve;
90
91 if (!nacl)
92 return;
93
94 rcu_read_lock();
95 hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
96 core_scsi3_ua_allocate(deve, 0x29,
97 ASCQ_29H_NEXUS_LOSS_OCCURRED);
98 rcu_read_unlock();
99}
100EXPORT_SYMBOL(core_allocate_nexus_loss_ua);
101
c66ac9db
NB
102/* core_tpg_add_node_to_devs():
103 *
104 *
105 */
106void core_tpg_add_node_to_devs(
107 struct se_node_acl *acl,
df9766ca
NB
108 struct se_portal_group *tpg,
109 struct se_lun *lun_orig)
c66ac9db 110{
03a68b44 111 bool lun_access_ro = true;
c66ac9db
NB
112 struct se_lun *lun;
113 struct se_device *dev;
114
6bb82612
NB
115 mutex_lock(&tpg->tpg_lun_mutex);
116 hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) {
df9766ca 117 if (lun_orig && lun != lun_orig)
c66ac9db
NB
118 continue;
119
4cc987ea
NB
120 dev = rcu_dereference_check(lun->lun_se_dev,
121 lockdep_is_held(&tpg->tpg_lun_mutex));
c66ac9db
NB
122 /*
123 * By default in LIO-Target $FABRIC_MOD,
124 * demo_mode_write_protect is ON, or READ_ONLY;
125 */
6708bb27 126 if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
03a68b44 127 lun_access_ro = false;
c66ac9db
NB
128 } else {
129 /*
130 * Allow only optical drives to issue R/W in default RO
131 * demo mode.
132 */
e3d6f909 133 if (dev->transport->get_device_type(dev) == TYPE_DISK)
03a68b44 134 lun_access_ro = true;
c66ac9db 135 else
03a68b44 136 lun_access_ro = false;
c66ac9db
NB
137 }
138
f2d30680 139 pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%llu] - Adding %s"
c66ac9db 140 " access for LUN in Demo Mode\n",
30c7ca93 141 tpg->se_tpg_tfo->fabric_name,
e3d6f909 142 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
03a68b44 143 lun_access_ro ? "READ-ONLY" : "READ-WRITE");
c66ac9db 144
e80ac6c4 145 core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
03a68b44 146 lun_access_ro, acl, tpg);
e2480563
NB
147 /*
148 * Check to see if there are any existing persistent reservation
149 * APTPL pre-registrations that need to be enabled for this dynamic
150 * LUN ACL now..
151 */
152 core_scsi3_check_aptpl_registration(dev, tpg, lun, acl,
153 lun->unpacked_lun);
c66ac9db 154 }
6bb82612 155 mutex_unlock(&tpg->tpg_lun_mutex);
c66ac9db
NB
156}
157
d36ad77f
NB
158static void
159target_set_nacl_queue_depth(struct se_portal_group *tpg,
160 struct se_node_acl *acl, u32 queue_depth)
c66ac9db 161{
d36ad77f
NB
162 acl->queue_depth = queue_depth;
163
c66ac9db 164 if (!acl->queue_depth) {
d36ad77f 165 pr_warn("Queue depth for %s Initiator Node: %s is 0,"
30c7ca93 166 "defaulting to 1.\n", tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
167 acl->initiatorname);
168 acl->queue_depth = 1;
169 }
c66ac9db
NB
170}
171
e413f472
CH
172static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg,
173 const unsigned char *initiatorname)
4a5a75f3 174{
c66ac9db 175 struct se_node_acl *acl;
d36ad77f 176 u32 queue_depth;
4a5a75f3 177
144bc4c2
CH
178 acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size),
179 GFP_KERNEL);
6708bb27 180 if (!acl)
c66ac9db 181 return NULL;
4a5a75f3 182
c66ac9db
NB
183 INIT_LIST_HEAD(&acl->acl_list);
184 INIT_LIST_HEAD(&acl->acl_sess_list);
29a05dee 185 INIT_HLIST_HEAD(&acl->lun_entry_hlist);
afb999ff 186 kref_init(&acl->acl_kref);
01468346 187 init_completion(&acl->acl_free_comp);
c66ac9db 188 spin_lock_init(&acl->nacl_sess_lock);
29a05dee 189 mutex_init(&acl->lun_entry_mutex);
c66ac9db 190 atomic_set(&acl->acl_pr_ref_count, 0);
d36ad77f 191
e1750d20 192 if (tpg->se_tpg_tfo->tpg_get_default_depth)
d36ad77f 193 queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
e1750d20 194 else
d36ad77f
NB
195 queue_depth = 1;
196 target_set_nacl_queue_depth(tpg, acl, queue_depth);
197
c66ac9db
NB
198 snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
199 acl->se_tpg = tpg;
200 acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
4a5a75f3 201
e3d6f909 202 tpg->se_tpg_tfo->set_default_node_attributes(acl);
c66ac9db 203
e413f472 204 return acl;
4a5a75f3
JE
205}
206
e413f472 207static void target_add_node_acl(struct se_node_acl *acl)
c66ac9db 208{
e413f472 209 struct se_portal_group *tpg = acl->se_tpg;
c66ac9db 210
403edd78 211 mutex_lock(&tpg->acl_node_mutex);
e413f472 212 list_add_tail(&acl->acl_list, &tpg->acl_node_list);
403edd78 213 mutex_unlock(&tpg->acl_node_mutex);
e413f472
CH
214
215 pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s"
216 " Initiator Node: %s\n",
30c7ca93 217 tpg->se_tpg_tfo->fabric_name,
e413f472
CH
218 tpg->se_tpg_tfo->tpg_get_tag(tpg),
219 acl->dynamic_node_acl ? "DYNAMIC" : "",
220 acl->queue_depth,
30c7ca93 221 tpg->se_tpg_tfo->fabric_name,
e413f472 222 acl->initiatorname);
c66ac9db
NB
223}
224
21aaa23b
NB
225bool target_tpg_has_node_acl(struct se_portal_group *tpg,
226 const char *initiatorname)
227{
228 struct se_node_acl *acl;
229 bool found = false;
230
231 mutex_lock(&tpg->acl_node_mutex);
232 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
233 if (!strcmp(acl->initiatorname, initiatorname)) {
234 found = true;
235 break;
236 }
237 }
238 mutex_unlock(&tpg->acl_node_mutex);
239
240 return found;
241}
242EXPORT_SYMBOL(target_tpg_has_node_acl);
243
c66ac9db
NB
244struct se_node_acl *core_tpg_check_initiator_node_acl(
245 struct se_portal_group *tpg,
246 unsigned char *initiatorname)
247{
248 struct se_node_acl *acl;
249
250 acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
6708bb27 251 if (acl)
c66ac9db
NB
252 return acl;
253
6708bb27 254 if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
c66ac9db
NB
255 return NULL;
256
e413f472 257 acl = target_alloc_node_acl(tpg, initiatorname);
6708bb27 258 if (!acl)
c66ac9db 259 return NULL;
21aaa23b
NB
260 /*
261 * When allocating a dynamically generated node_acl, go ahead
262 * and take the extra kref now before returning to the fabric
263 * driver caller.
264 *
265 * Note this reference will be released at session shutdown
266 * time within transport_free_session() code.
267 */
268 kref_get(&acl->acl_kref);
c66ac9db
NB
269 acl->dynamic_node_acl = 1;
270
052605c6
NB
271 /*
272 * Here we only create demo-mode MappedLUNs from the active
35d1efe8 273 * TPG LUNs if the fabric is not explicitly asking for
052605c6
NB
274 * tpg_check_demo_mode_login_only() == 1.
275 */
cdf88a2f
AG
276 if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) ||
277 (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1))
df9766ca 278 core_tpg_add_node_to_devs(acl, tpg, NULL);
c66ac9db 279
e413f472 280 target_add_node_acl(acl);
c66ac9db
NB
281 return acl;
282}
283EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
284
285void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
286{
287 while (atomic_read(&nacl->acl_pr_ref_count) != 0)
288 cpu_relax();
289}
290
c66ac9db
NB
291struct se_node_acl *core_tpg_add_initiator_node_acl(
292 struct se_portal_group *tpg,
c7d6a803 293 const char *initiatorname)
c66ac9db 294{
c7d6a803 295 struct se_node_acl *acl;
c66ac9db 296
403edd78 297 mutex_lock(&tpg->acl_node_mutex);
c66ac9db 298 acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
6708bb27 299 if (acl) {
c66ac9db
NB
300 if (acl->dynamic_node_acl) {
301 acl->dynamic_node_acl = 0;
6708bb27 302 pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
30c7ca93 303 " for %s\n", tpg->se_tpg_tfo->fabric_name,
e3d6f909 304 tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
403edd78 305 mutex_unlock(&tpg->acl_node_mutex);
e413f472 306 return acl;
c66ac9db
NB
307 }
308
6708bb27 309 pr_err("ACL entry for %s Initiator"
c66ac9db 310 " Node %s already exists for TPG %u, ignoring"
30c7ca93 311 " request.\n", tpg->se_tpg_tfo->fabric_name,
e3d6f909 312 initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
403edd78 313 mutex_unlock(&tpg->acl_node_mutex);
c66ac9db
NB
314 return ERR_PTR(-EEXIST);
315 }
403edd78 316 mutex_unlock(&tpg->acl_node_mutex);
c66ac9db 317
e413f472
CH
318 acl = target_alloc_node_acl(tpg, initiatorname);
319 if (!acl)
c66ac9db 320 return ERR_PTR(-ENOMEM);
c66ac9db 321
e413f472 322 target_add_node_acl(acl);
c66ac9db
NB
323 return acl;
324}
c66ac9db 325
fba81f88 326static void target_shutdown_sessions(struct se_node_acl *acl)
c66ac9db 327{
bc6e6bb4 328 struct se_session *sess;
140854cb 329 unsigned long flags;
c66ac9db 330
bc6e6bb4 331restart:
fba81f88 332 spin_lock_irqsave(&acl->nacl_sess_lock, flags);
bc6e6bb4
CH
333 list_for_each_entry(sess, &acl->acl_sess_list, sess_acl_list) {
334 if (sess->sess_tearing_down)
c66ac9db 335 continue;
337c0607 336
fba81f88 337 list_del_init(&sess->sess_acl_list);
bc6e6bb4 338 spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
d94331fa
CH
339
340 if (acl->se_tpg->se_tpg_tfo->close_session)
341 acl->se_tpg->se_tpg_tfo->close_session(sess);
bc6e6bb4 342 goto restart;
c66ac9db 343 }
bc6e6bb4
CH
344 spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
345}
346
347void core_tpg_del_initiator_node_acl(struct se_node_acl *acl)
348{
349 struct se_portal_group *tpg = acl->se_tpg;
350
351 mutex_lock(&tpg->acl_node_mutex);
352 if (acl->dynamic_node_acl)
353 acl->dynamic_node_acl = 0;
6f48655f 354 list_del_init(&acl->acl_list);
bc6e6bb4
CH
355 mutex_unlock(&tpg->acl_node_mutex);
356
fba81f88 357 target_shutdown_sessions(acl);
bc6e6bb4 358
337c0607
NB
359 target_put_nacl(acl);
360 /*
361 * Wait for last target_put_nacl() to complete in target_complete_nacl()
362 * for active fabric session transport_deregister_session() callbacks.
363 */
364 wait_for_completion(&acl->acl_free_comp);
c66ac9db
NB
365
366 core_tpg_wait_for_nacl_pr_ref(acl);
c66ac9db
NB
367 core_free_device_list_for_node(acl, tpg);
368
6708bb27 369 pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
30c7ca93 370 " Initiator Node: %s\n", tpg->se_tpg_tfo->fabric_name,
e3d6f909 371 tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
30c7ca93 372 tpg->se_tpg_tfo->fabric_name, acl->initiatorname);
c66ac9db 373
144bc4c2 374 kfree(acl);
c66ac9db 375}
c66ac9db
NB
376
377/* core_tpg_set_initiator_node_queue_depth():
378 *
379 *
380 */
381int core_tpg_set_initiator_node_queue_depth(
d36ad77f
NB
382 struct se_node_acl *acl,
383 u32 queue_depth)
c66ac9db 384{
d36ad77f 385 struct se_portal_group *tpg = acl->se_tpg;
c66ac9db 386
46861cdd
NB
387 /*
388 * Allow the setting of se_node_acl queue_depth to be idempotent,
389 * and not force a session shutdown event if the value is not
390 * changing.
391 */
392 if (acl->queue_depth == queue_depth)
393 return 0;
c66ac9db
NB
394 /*
395 * User has requested to change the queue depth for a Initiator Node.
396 * Change the value in the Node's struct se_node_acl, and call
d36ad77f 397 * target_set_nacl_queue_depth() to set the new queue depth.
c66ac9db 398 */
d36ad77f
NB
399 target_set_nacl_queue_depth(tpg, acl, queue_depth);
400
bc6e6bb4
CH
401 /*
402 * Shutdown all pending sessions to force session reinstatement.
403 */
fba81f88 404 target_shutdown_sessions(acl);
c66ac9db 405
bfb9035c 406 pr_debug("Successfully changed queue depth to: %d for Initiator"
d36ad77f 407 " Node: %s on %s Target Portal Group: %u\n", acl->queue_depth,
30c7ca93 408 acl->initiatorname, tpg->se_tpg_tfo->fabric_name,
e3d6f909 409 tpg->se_tpg_tfo->tpg_get_tag(tpg));
c66ac9db 410
c66ac9db
NB
411 return 0;
412}
413EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
414
79e62fc3
AG
415/* core_tpg_set_initiator_node_tag():
416 *
417 * Initiator nodeacl tags are not used internally, but may be used by
418 * userspace to emulate aliases or groups.
419 * Returns length of newly-set tag or -EINVAL.
420 */
421int core_tpg_set_initiator_node_tag(
422 struct se_portal_group *tpg,
423 struct se_node_acl *acl,
424 const char *new_tag)
425{
426 if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
427 return -EINVAL;
428
429 if (!strncmp("NULL", new_tag, 4)) {
430 acl->acl_tag[0] = '\0';
431 return 0;
432 }
433
434 return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
435}
436EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
437
5277797d
NB
438static void core_tpg_lun_ref_release(struct percpu_ref *ref)
439{
440 struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
441
bd4e2d29 442 complete(&lun->lun_shutdown_comp);
5277797d
NB
443}
444
2bce1a6d 445/* Does not change se_wwn->priv. */
c66ac9db 446int core_tpg_register(
c66ac9db
NB
447 struct se_wwn *se_wwn,
448 struct se_portal_group *se_tpg,
e4aae5af 449 int proto_id)
c66ac9db 450{
adf653f9
CH
451 int ret;
452
bc0c94b1
NB
453 if (!se_tpg)
454 return -EINVAL;
455 /*
456 * For the typical case where core_tpg_register() is called by a
457 * fabric driver from target_core_fabric_ops->fabric_make_tpg()
458 * configfs context, use the original tf_ops pointer already saved
459 * by target-core in target_fabric_make_wwn().
460 *
461 * Otherwise, for special cases like iscsi-target discovery TPGs
462 * the caller is responsible for setting ->se_tpg_tfo ahead of
463 * calling core_tpg_register().
464 */
465 if (se_wwn)
466 se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops;
c66ac9db 467
bc0c94b1
NB
468 if (!se_tpg->se_tpg_tfo) {
469 pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n");
470 return -EINVAL;
c66ac9db
NB
471 }
472
6bb82612 473 INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
e4aae5af 474 se_tpg->proto_id = proto_id;
c66ac9db
NB
475 se_tpg->se_tpg_wwn = se_wwn;
476 atomic_set(&se_tpg->tpg_pr_ref_count, 0);
477 INIT_LIST_HEAD(&se_tpg->acl_node_list);
e3d6f909 478 INIT_LIST_HEAD(&se_tpg->se_tpg_node);
c66ac9db 479 INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
c66ac9db 480 spin_lock_init(&se_tpg->session_lock);
6bb82612 481 mutex_init(&se_tpg->tpg_lun_mutex);
403edd78 482 mutex_init(&se_tpg->acl_node_mutex);
c66ac9db 483
e4aae5af 484 if (se_tpg->proto_id >= 0) {
adf653f9
CH
485 se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
486 if (IS_ERR(se_tpg->tpg_virt_lun0))
487 return PTR_ERR(se_tpg->tpg_virt_lun0);
488
489 ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
03a68b44 490 true, g_lun0_dev);
adf653f9
CH
491 if (ret < 0) {
492 kfree(se_tpg->tpg_virt_lun0);
493 return ret;
c66ac9db
NB
494 }
495 }
496
e3d6f909
AG
497 spin_lock_bh(&tpg_lock);
498 list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
499 spin_unlock_bh(&tpg_lock);
c66ac9db 500
e4aae5af 501 pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
30c7ca93 502 "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->fabric_name,
bc0c94b1
NB
503 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ?
504 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL,
505 se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
c66ac9db
NB
506
507 return 0;
508}
509EXPORT_SYMBOL(core_tpg_register);
510
511int core_tpg_deregister(struct se_portal_group *se_tpg)
512{
e4aae5af 513 const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
e89d15ee 514 struct se_node_acl *nacl, *nacl_tmp;
22793de5 515 LIST_HEAD(node_list);
e89d15ee 516
e4aae5af 517 pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
30c7ca93 518 "Proto: %d, Portal Tag: %u\n", tfo->fabric_name,
e4aae5af
CH
519 tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
520 se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
c66ac9db 521
e3d6f909
AG
522 spin_lock_bh(&tpg_lock);
523 list_del(&se_tpg->se_tpg_node);
524 spin_unlock_bh(&tpg_lock);
c66ac9db
NB
525
526 while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
527 cpu_relax();
403edd78 528
22793de5
NB
529 mutex_lock(&se_tpg->acl_node_mutex);
530 list_splice_init(&se_tpg->acl_node_list, &node_list);
531 mutex_unlock(&se_tpg->acl_node_mutex);
e89d15ee
NB
532 /*
533 * Release any remaining demo-mode generated se_node_acl that have
534 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
535 * in transport_deregister_session().
536 */
22793de5 537 list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) {
6f48655f 538 list_del_init(&nacl->acl_list);
e89d15ee
NB
539
540 core_tpg_wait_for_nacl_pr_ref(nacl);
541 core_free_device_list_for_node(nacl, se_tpg);
144bc4c2 542 kfree(nacl);
e89d15ee 543 }
c66ac9db 544
adf653f9
CH
545 if (se_tpg->proto_id >= 0) {
546 core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
547 kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
548 }
c66ac9db 549
c66ac9db
NB
550 return 0;
551}
552EXPORT_SYMBOL(core_tpg_deregister);
553
d344f8a1 554struct se_lun *core_tpg_alloc_lun(
c66ac9db 555 struct se_portal_group *tpg,
f2d30680 556 u64 unpacked_lun)
c66ac9db
NB
557{
558 struct se_lun *lun;
559
6bb82612
NB
560 lun = kzalloc(sizeof(*lun), GFP_KERNEL);
561 if (!lun) {
562 pr_err("Unable to allocate se_lun memory\n");
563 return ERR_PTR(-ENOMEM);
c66ac9db 564 }
6bb82612 565 lun->unpacked_lun = unpacked_lun;
6bb82612 566 atomic_set(&lun->lun_acl_count, 0);
bd4e2d29 567 init_completion(&lun->lun_shutdown_comp);
adf653f9
CH
568 INIT_LIST_HEAD(&lun->lun_deve_list);
569 INIT_LIST_HEAD(&lun->lun_dev_link);
570 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
571 spin_lock_init(&lun->lun_deve_lock);
572 mutex_init(&lun->lun_tg_pt_md_mutex);
573 INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
574 spin_lock_init(&lun->lun_tg_pt_gp_lock);
adf653f9 575 lun->lun_tpg = tpg;
c66ac9db
NB
576
577 return lun;
578}
579
d344f8a1 580int core_tpg_add_lun(
c66ac9db
NB
581 struct se_portal_group *tpg,
582 struct se_lun *lun,
03a68b44 583 bool lun_access_ro,
340dbf72 584 struct se_device *dev)
c66ac9db 585{
e3d6f909
AG
586 int ret;
587
2aad2a86 588 ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
a34375ef 589 GFP_KERNEL);
e3d6f909 590 if (ret < 0)
adf653f9 591 goto out;
c66ac9db 592
adf653f9
CH
593 ret = core_alloc_rtpi(lun, dev);
594 if (ret)
595 goto out_kill_ref;
596
530c6891
MC
597 if (!(dev->transport->transport_flags &
598 TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
adf653f9
CH
599 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
600 target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
5277797d 601
6bb82612 602 mutex_lock(&tpg->tpg_lun_mutex);
adf653f9 603
adf653f9 604 spin_lock(&dev->se_port_lock);
4cc987ea 605 lun->lun_index = dev->dev_index;
adf653f9
CH
606 rcu_assign_pointer(lun->lun_se_dev, dev);
607 dev->export_count++;
608 list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list);
609 spin_unlock(&dev->se_port_lock);
5277797d 610
eeeb9522 611 if (dev->dev_flags & DF_READ_ONLY)
03a68b44 612 lun->lun_access_ro = true;
eeeb9522 613 else
03a68b44 614 lun->lun_access_ro = lun_access_ro;
6bb82612
NB
615 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
616 hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
617 mutex_unlock(&tpg->tpg_lun_mutex);
c66ac9db
NB
618
619 return 0;
adf653f9
CH
620
621out_kill_ref:
622 percpu_ref_exit(&lun->lun_ref);
623out:
624 return ret;
c66ac9db
NB
625}
626
cd9d7cba 627void core_tpg_remove_lun(
c66ac9db
NB
628 struct se_portal_group *tpg,
629 struct se_lun *lun)
630{
4cc987ea
NB
631 /*
632 * rcu_dereference_raw protected by se_lun->lun_group symlink
633 * reference to se_device->dev_group.
634 */
635 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
6bb82612 636
49cb77e2
NB
637 lun->lun_shutdown = true;
638
4a9a6c8d 639 core_clear_lun_from_tpg(lun, tpg);
9e37d042
NB
640 /*
641 * Wait for any active I/O references to percpu se_lun->lun_ref to
642 * be released. Also, se_lun->lun_ref is now used by PR and ALUA
643 * logic when referencing a remote target port during ALL_TGT_PT=1
644 * and generating UNIT_ATTENTIONs for ALUA access state transition.
645 */
4a9a6c8d 646 transport_clear_lun_ref(lun);
c66ac9db 647
6bb82612 648 mutex_lock(&tpg->tpg_lun_mutex);
adf653f9 649 if (lun->lun_se_dev) {
adf653f9 650 target_detach_tg_pt_gp(lun);
c66ac9db 651
adf653f9
CH
652 spin_lock(&dev->se_port_lock);
653 list_del(&lun->lun_dev_link);
654 dev->export_count--;
655 rcu_assign_pointer(lun->lun_se_dev, NULL);
656 spin_unlock(&dev->se_port_lock);
657 }
6bb82612
NB
658 if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
659 hlist_del_rcu(&lun->link);
49cb77e2
NB
660
661 lun->lun_shutdown = false;
6bb82612 662 mutex_unlock(&tpg->tpg_lun_mutex);
c66ac9db 663
9a1049da 664 percpu_ref_exit(&lun->lun_ref);
c66ac9db 665}