1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_tpg.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains generic Target Portal Group related functions.
5c66ac9dbSNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2002-2013 Datera, Inc.
7c66ac9dbSNicholas Bellinger  *
8c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
11c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
12c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
13c66ac9dbSNicholas Bellinger  * (at your option) any later version.
14c66ac9dbSNicholas Bellinger  *
15c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
16c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
19c66ac9dbSNicholas Bellinger  *
20c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
21c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
22c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23c66ac9dbSNicholas Bellinger  *
24c66ac9dbSNicholas Bellinger  ******************************************************************************/
25c66ac9dbSNicholas Bellinger 
26c66ac9dbSNicholas Bellinger #include <linux/net.h>
27c66ac9dbSNicholas Bellinger #include <linux/string.h>
28c66ac9dbSNicholas Bellinger #include <linux/timer.h>
29c66ac9dbSNicholas Bellinger #include <linux/slab.h>
30c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
31c66ac9dbSNicholas Bellinger #include <linux/in.h>
32c53181afSPaul Gortmaker #include <linux/export.h>
33c66ac9dbSNicholas Bellinger #include <net/sock.h>
34c66ac9dbSNicholas Bellinger #include <net/tcp.h>
35ba929992SBart Van Assche #include <scsi/scsi_proto.h>
36c66ac9dbSNicholas Bellinger 
37c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
38c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
39c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
40c66ac9dbSNicholas Bellinger 
41e26d99aeSChristoph Hellwig #include "target_core_internal.h"
42adf653f9SChristoph Hellwig #include "target_core_alua.h"
43e2480563SNicholas Bellinger #include "target_core_pr.h"
44e986a35aSHannes Reinecke #include "target_core_ua.h"
45e3d6f909SAndy Grover 
46e3d6f909SAndy Grover extern struct se_device *g_lun0_dev;
47e3d6f909SAndy Grover 
48e3d6f909SAndy Grover static DEFINE_SPINLOCK(tpg_lock);
49e3d6f909SAndy Grover static LIST_HEAD(tpg_list);
50c66ac9dbSNicholas Bellinger 
51c66ac9dbSNicholas Bellinger /*	__core_tpg_get_initiator_node_acl():
52c66ac9dbSNicholas Bellinger  *
53403edd78SNicholas Bellinger  *	mutex_lock(&tpg->acl_node_mutex); must be held when calling
54c66ac9dbSNicholas Bellinger  */
55c66ac9dbSNicholas Bellinger struct se_node_acl *__core_tpg_get_initiator_node_acl(
56c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
57c66ac9dbSNicholas Bellinger 	const char *initiatorname)
58c66ac9dbSNicholas Bellinger {
59c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
60c66ac9dbSNicholas Bellinger 
61c66ac9dbSNicholas Bellinger 	list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
626708bb27SAndy Grover 		if (!strcmp(acl->initiatorname, initiatorname))
63c66ac9dbSNicholas Bellinger 			return acl;
64c66ac9dbSNicholas Bellinger 	}
65c66ac9dbSNicholas Bellinger 
66c66ac9dbSNicholas Bellinger 	return NULL;
67c66ac9dbSNicholas Bellinger }
68c66ac9dbSNicholas Bellinger 
69c66ac9dbSNicholas Bellinger /*	core_tpg_get_initiator_node_acl():
70c66ac9dbSNicholas Bellinger  *
71c66ac9dbSNicholas Bellinger  *
72c66ac9dbSNicholas Bellinger  */
73c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_get_initiator_node_acl(
74c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
75c66ac9dbSNicholas Bellinger 	unsigned char *initiatorname)
76c66ac9dbSNicholas Bellinger {
77c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
7821aaa23bSNicholas Bellinger 	/*
7921aaa23bSNicholas Bellinger 	 * Obtain se_node_acl->acl_kref using fabric driver provided
8021aaa23bSNicholas Bellinger 	 * initiatorname[] during node acl endpoint lookup driven by
8121aaa23bSNicholas Bellinger 	 * new se_session login.
8221aaa23bSNicholas Bellinger 	 *
8321aaa23bSNicholas Bellinger 	 * The reference is held until se_session shutdown -> release
8421aaa23bSNicholas Bellinger 	 * occurs via fabric driver invoked transport_deregister_session()
8521aaa23bSNicholas Bellinger 	 * or transport_free_session() code.
8621aaa23bSNicholas Bellinger 	 */
87403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
88fcf29481SNicholas Bellinger 	acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
8921aaa23bSNicholas Bellinger 	if (acl) {
9021aaa23bSNicholas Bellinger 		if (!kref_get_unless_zero(&acl->acl_kref))
9121aaa23bSNicholas Bellinger 			acl = NULL;
9221aaa23bSNicholas Bellinger 	}
93403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
94c66ac9dbSNicholas Bellinger 
95fcf29481SNicholas Bellinger 	return acl;
96c66ac9dbSNicholas Bellinger }
97b3fde035SThomas Glanzmann EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
98c66ac9dbSNicholas Bellinger 
99e986a35aSHannes Reinecke void core_allocate_nexus_loss_ua(
100e986a35aSHannes Reinecke 	struct se_node_acl *nacl)
101e986a35aSHannes Reinecke {
102e986a35aSHannes Reinecke 	struct se_dev_entry *deve;
103e986a35aSHannes Reinecke 
104e986a35aSHannes Reinecke 	if (!nacl)
105e986a35aSHannes Reinecke 		return;
106e986a35aSHannes Reinecke 
107e986a35aSHannes Reinecke 	rcu_read_lock();
108e986a35aSHannes Reinecke 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
109e986a35aSHannes Reinecke 		core_scsi3_ua_allocate(deve, 0x29,
110e986a35aSHannes Reinecke 			ASCQ_29H_NEXUS_LOSS_OCCURRED);
111e986a35aSHannes Reinecke 	rcu_read_unlock();
112e986a35aSHannes Reinecke }
113e986a35aSHannes Reinecke EXPORT_SYMBOL(core_allocate_nexus_loss_ua);
114e986a35aSHannes Reinecke 
115c66ac9dbSNicholas Bellinger /*	core_tpg_add_node_to_devs():
116c66ac9dbSNicholas Bellinger  *
117c66ac9dbSNicholas Bellinger  *
118c66ac9dbSNicholas Bellinger  */
119c66ac9dbSNicholas Bellinger void core_tpg_add_node_to_devs(
120c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl,
121df9766caSNicholas Bellinger 	struct se_portal_group *tpg,
122df9766caSNicholas Bellinger 	struct se_lun *lun_orig)
123c66ac9dbSNicholas Bellinger {
12403a68b44SAndy Grover 	bool lun_access_ro = true;
125c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
126c66ac9dbSNicholas Bellinger 	struct se_device *dev;
127c66ac9dbSNicholas Bellinger 
1286bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
1296bb82612SNicholas Bellinger 	hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) {
130df9766caSNicholas Bellinger 		if (lun_orig && lun != lun_orig)
131c66ac9dbSNicholas Bellinger 			continue;
132c66ac9dbSNicholas Bellinger 
1334cc987eaSNicholas Bellinger 		dev = rcu_dereference_check(lun->lun_se_dev,
1344cc987eaSNicholas Bellinger 					    lockdep_is_held(&tpg->tpg_lun_mutex));
135c66ac9dbSNicholas Bellinger 		/*
136c66ac9dbSNicholas Bellinger 		 * By default in LIO-Target $FABRIC_MOD,
137c66ac9dbSNicholas Bellinger 		 * demo_mode_write_protect is ON, or READ_ONLY;
138c66ac9dbSNicholas Bellinger 		 */
1396708bb27SAndy Grover 		if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
14003a68b44SAndy Grover 			lun_access_ro = false;
141c66ac9dbSNicholas Bellinger 		} else {
142c66ac9dbSNicholas Bellinger 			/*
143c66ac9dbSNicholas Bellinger 			 * Allow only optical drives to issue R/W in default RO
144c66ac9dbSNicholas Bellinger 			 * demo mode.
145c66ac9dbSNicholas Bellinger 			 */
146e3d6f909SAndy Grover 			if (dev->transport->get_device_type(dev) == TYPE_DISK)
14703a68b44SAndy Grover 				lun_access_ro = true;
148c66ac9dbSNicholas Bellinger 			else
14903a68b44SAndy Grover 				lun_access_ro = false;
150c66ac9dbSNicholas Bellinger 		}
151c66ac9dbSNicholas Bellinger 
152f2d30680SHannes Reinecke 		pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%llu] - Adding %s"
153c66ac9dbSNicholas Bellinger 			" access for LUN in Demo Mode\n",
154e3d6f909SAndy Grover 			tpg->se_tpg_tfo->get_fabric_name(),
155e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
15603a68b44SAndy Grover 			lun_access_ro ? "READ-ONLY" : "READ-WRITE");
157c66ac9dbSNicholas Bellinger 
158e80ac6c4SAndy Grover 		core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
15903a68b44SAndy Grover 						 lun_access_ro, acl, tpg);
160e2480563SNicholas Bellinger 		/*
161e2480563SNicholas Bellinger 		 * Check to see if there are any existing persistent reservation
162e2480563SNicholas Bellinger 		 * APTPL pre-registrations that need to be enabled for this dynamic
163e2480563SNicholas Bellinger 		 * LUN ACL now..
164e2480563SNicholas Bellinger 		 */
165e2480563SNicholas Bellinger 		core_scsi3_check_aptpl_registration(dev, tpg, lun, acl,
166e2480563SNicholas Bellinger 						    lun->unpacked_lun);
167c66ac9dbSNicholas Bellinger 	}
1686bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
169c66ac9dbSNicholas Bellinger }
170c66ac9dbSNicholas Bellinger 
171d36ad77fSNicholas Bellinger static void
172d36ad77fSNicholas Bellinger target_set_nacl_queue_depth(struct se_portal_group *tpg,
173d36ad77fSNicholas Bellinger 			    struct se_node_acl *acl, u32 queue_depth)
174c66ac9dbSNicholas Bellinger {
175d36ad77fSNicholas Bellinger 	acl->queue_depth = queue_depth;
176d36ad77fSNicholas Bellinger 
177c66ac9dbSNicholas Bellinger 	if (!acl->queue_depth) {
178d36ad77fSNicholas Bellinger 		pr_warn("Queue depth for %s Initiator Node: %s is 0,"
179e3d6f909SAndy Grover 			"defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
180c66ac9dbSNicholas Bellinger 			acl->initiatorname);
181c66ac9dbSNicholas Bellinger 		acl->queue_depth = 1;
182c66ac9dbSNicholas Bellinger 	}
183c66ac9dbSNicholas Bellinger }
184c66ac9dbSNicholas Bellinger 
185e413f472SChristoph Hellwig static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg,
186e413f472SChristoph Hellwig 		const unsigned char *initiatorname)
1874a5a75f3SJörn Engel {
188c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
189d36ad77fSNicholas Bellinger 	u32 queue_depth;
190c66ac9dbSNicholas Bellinger 
191144bc4c2SChristoph Hellwig 	acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size),
192144bc4c2SChristoph Hellwig 			GFP_KERNEL);
1936708bb27SAndy Grover 	if (!acl)
194c66ac9dbSNicholas Bellinger 		return NULL;
195c66ac9dbSNicholas Bellinger 
196c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&acl->acl_list);
197c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&acl->acl_sess_list);
19829a05deeSNicholas Bellinger 	INIT_HLIST_HEAD(&acl->lun_entry_hlist);
199afb999ffSNicholas Bellinger 	kref_init(&acl->acl_kref);
20001468346SNicholas Bellinger 	init_completion(&acl->acl_free_comp);
201c66ac9dbSNicholas Bellinger 	spin_lock_init(&acl->nacl_sess_lock);
20229a05deeSNicholas Bellinger 	mutex_init(&acl->lun_entry_mutex);
203c66ac9dbSNicholas Bellinger 	atomic_set(&acl->acl_pr_ref_count, 0);
204d36ad77fSNicholas Bellinger 
205e1750d20SChristoph Hellwig 	if (tpg->se_tpg_tfo->tpg_get_default_depth)
206d36ad77fSNicholas Bellinger 		queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
207e1750d20SChristoph Hellwig 	else
208d36ad77fSNicholas Bellinger 		queue_depth = 1;
209d36ad77fSNicholas Bellinger 	target_set_nacl_queue_depth(tpg, acl, queue_depth);
210d36ad77fSNicholas Bellinger 
211c66ac9dbSNicholas Bellinger 	snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
212c66ac9dbSNicholas Bellinger 	acl->se_tpg = tpg;
213c66ac9dbSNicholas Bellinger 	acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
214c66ac9dbSNicholas Bellinger 
215e3d6f909SAndy Grover 	tpg->se_tpg_tfo->set_default_node_attributes(acl);
216c66ac9dbSNicholas Bellinger 
217e413f472SChristoph Hellwig 	return acl;
218c66ac9dbSNicholas Bellinger }
219c66ac9dbSNicholas Bellinger 
220e413f472SChristoph Hellwig static void target_add_node_acl(struct se_node_acl *acl)
221c66ac9dbSNicholas Bellinger {
222e413f472SChristoph Hellwig 	struct se_portal_group *tpg = acl->se_tpg;
223c66ac9dbSNicholas Bellinger 
224403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
225e413f472SChristoph Hellwig 	list_add_tail(&acl->acl_list, &tpg->acl_node_list);
226403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
227c66ac9dbSNicholas Bellinger 
228e413f472SChristoph Hellwig 	pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s"
229e413f472SChristoph Hellwig 		" Initiator Node: %s\n",
230e413f472SChristoph Hellwig 		tpg->se_tpg_tfo->get_fabric_name(),
231e413f472SChristoph Hellwig 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
232e413f472SChristoph Hellwig 		acl->dynamic_node_acl ? "DYNAMIC" : "",
233e413f472SChristoph Hellwig 		acl->queue_depth,
234e413f472SChristoph Hellwig 		tpg->se_tpg_tfo->get_fabric_name(),
235e413f472SChristoph Hellwig 		acl->initiatorname);
236c66ac9dbSNicholas Bellinger }
237c66ac9dbSNicholas Bellinger 
23821aaa23bSNicholas Bellinger bool target_tpg_has_node_acl(struct se_portal_group *tpg,
23921aaa23bSNicholas Bellinger 			     const char *initiatorname)
24021aaa23bSNicholas Bellinger {
24121aaa23bSNicholas Bellinger 	struct se_node_acl *acl;
24221aaa23bSNicholas Bellinger 	bool found = false;
24321aaa23bSNicholas Bellinger 
24421aaa23bSNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
24521aaa23bSNicholas Bellinger 	list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
24621aaa23bSNicholas Bellinger 		if (!strcmp(acl->initiatorname, initiatorname)) {
24721aaa23bSNicholas Bellinger 			found = true;
24821aaa23bSNicholas Bellinger 			break;
24921aaa23bSNicholas Bellinger 		}
25021aaa23bSNicholas Bellinger 	}
25121aaa23bSNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
25221aaa23bSNicholas Bellinger 
25321aaa23bSNicholas Bellinger 	return found;
25421aaa23bSNicholas Bellinger }
25521aaa23bSNicholas Bellinger EXPORT_SYMBOL(target_tpg_has_node_acl);
25621aaa23bSNicholas Bellinger 
257c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_check_initiator_node_acl(
258c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
259c66ac9dbSNicholas Bellinger 	unsigned char *initiatorname)
260c66ac9dbSNicholas Bellinger {
261c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
262c66ac9dbSNicholas Bellinger 
263c66ac9dbSNicholas Bellinger 	acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
264c66ac9dbSNicholas Bellinger 	if (acl)
265c66ac9dbSNicholas Bellinger 		return acl;
266c66ac9dbSNicholas Bellinger 
267c66ac9dbSNicholas Bellinger 	if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
268c66ac9dbSNicholas Bellinger 		return NULL;
269c66ac9dbSNicholas Bellinger 
270e413f472SChristoph Hellwig 	acl = target_alloc_node_acl(tpg, initiatorname);
271c66ac9dbSNicholas Bellinger 	if (!acl)
272c66ac9dbSNicholas Bellinger 		return NULL;
27321aaa23bSNicholas Bellinger 	/*
27421aaa23bSNicholas Bellinger 	 * When allocating a dynamically generated node_acl, go ahead
27521aaa23bSNicholas Bellinger 	 * and take the extra kref now before returning to the fabric
27621aaa23bSNicholas Bellinger 	 * driver caller.
27721aaa23bSNicholas Bellinger 	 *
27821aaa23bSNicholas Bellinger 	 * Note this reference will be released at session shutdown
27921aaa23bSNicholas Bellinger 	 * time within transport_free_session() code.
28021aaa23bSNicholas Bellinger 	 */
28121aaa23bSNicholas Bellinger 	kref_get(&acl->acl_kref);
282c66ac9dbSNicholas Bellinger 	acl->dynamic_node_acl = 1;
283c66ac9dbSNicholas Bellinger 
284052605c6SNicholas Bellinger 	/*
285052605c6SNicholas Bellinger 	 * Here we only create demo-mode MappedLUNs from the active
28635d1efe8SMasanari Iida 	 * TPG LUNs if the fabric is not explicitly asking for
287052605c6SNicholas Bellinger 	 * tpg_check_demo_mode_login_only() == 1.
288052605c6SNicholas Bellinger 	 */
289cdf88a2fSAndy Grover 	if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) ||
290cdf88a2fSAndy Grover 	    (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1))
291df9766caSNicholas Bellinger 		core_tpg_add_node_to_devs(acl, tpg, NULL);
292c66ac9dbSNicholas Bellinger 
293e413f472SChristoph Hellwig 	target_add_node_acl(acl);
294c66ac9dbSNicholas Bellinger 	return acl;
295c66ac9dbSNicholas Bellinger }
296c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
297c66ac9dbSNicholas Bellinger 
298c66ac9dbSNicholas Bellinger void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
299c66ac9dbSNicholas Bellinger {
300c66ac9dbSNicholas Bellinger 	while (atomic_read(&nacl->acl_pr_ref_count) != 0)
301c66ac9dbSNicholas Bellinger 		cpu_relax();
302c66ac9dbSNicholas Bellinger }
303c66ac9dbSNicholas Bellinger 
304c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_add_initiator_node_acl(
305c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
306c7d6a803SChristoph Hellwig 	const char *initiatorname)
307c66ac9dbSNicholas Bellinger {
308c7d6a803SChristoph Hellwig 	struct se_node_acl *acl;
309c66ac9dbSNicholas Bellinger 
310403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
311c66ac9dbSNicholas Bellinger 	acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
3126708bb27SAndy Grover 	if (acl) {
313c66ac9dbSNicholas Bellinger 		if (acl->dynamic_node_acl) {
314c66ac9dbSNicholas Bellinger 			acl->dynamic_node_acl = 0;
3156708bb27SAndy Grover 			pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
316e3d6f909SAndy Grover 				" for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
317e3d6f909SAndy Grover 				tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
318403edd78SNicholas Bellinger 			mutex_unlock(&tpg->acl_node_mutex);
319e413f472SChristoph Hellwig 			return acl;
320c66ac9dbSNicholas Bellinger 		}
321c66ac9dbSNicholas Bellinger 
3226708bb27SAndy Grover 		pr_err("ACL entry for %s Initiator"
323c66ac9dbSNicholas Bellinger 			" Node %s already exists for TPG %u, ignoring"
324e3d6f909SAndy Grover 			" request.\n",  tpg->se_tpg_tfo->get_fabric_name(),
325e3d6f909SAndy Grover 			initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
326403edd78SNicholas Bellinger 		mutex_unlock(&tpg->acl_node_mutex);
327c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EEXIST);
328c66ac9dbSNicholas Bellinger 	}
329403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
330c66ac9dbSNicholas Bellinger 
331e413f472SChristoph Hellwig 	acl = target_alloc_node_acl(tpg, initiatorname);
332e413f472SChristoph Hellwig 	if (!acl)
333c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
334c66ac9dbSNicholas Bellinger 
335e413f472SChristoph Hellwig 	target_add_node_acl(acl);
336c66ac9dbSNicholas Bellinger 	return acl;
337c66ac9dbSNicholas Bellinger }
338c66ac9dbSNicholas Bellinger 
339fba81f88SChristoph Hellwig static void target_shutdown_sessions(struct se_node_acl *acl)
340bc6e6bb4SChristoph Hellwig {
341bc6e6bb4SChristoph Hellwig 	struct se_session *sess;
342bc6e6bb4SChristoph Hellwig 	unsigned long flags;
343bc6e6bb4SChristoph Hellwig 
344bc6e6bb4SChristoph Hellwig restart:
345fba81f88SChristoph Hellwig 	spin_lock_irqsave(&acl->nacl_sess_lock, flags);
346bc6e6bb4SChristoph Hellwig 	list_for_each_entry(sess, &acl->acl_sess_list, sess_acl_list) {
347bc6e6bb4SChristoph Hellwig 		if (sess->sess_tearing_down)
348bc6e6bb4SChristoph Hellwig 			continue;
349bc6e6bb4SChristoph Hellwig 
350fba81f88SChristoph Hellwig 		list_del_init(&sess->sess_acl_list);
351bc6e6bb4SChristoph Hellwig 		spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
352d94331faSChristoph Hellwig 
353d94331faSChristoph Hellwig 		if (acl->se_tpg->se_tpg_tfo->close_session)
354d94331faSChristoph Hellwig 			acl->se_tpg->se_tpg_tfo->close_session(sess);
355bc6e6bb4SChristoph Hellwig 		goto restart;
356bc6e6bb4SChristoph Hellwig 	}
357bc6e6bb4SChristoph Hellwig 	spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
358bc6e6bb4SChristoph Hellwig }
359bc6e6bb4SChristoph Hellwig 
360c7d6a803SChristoph Hellwig void core_tpg_del_initiator_node_acl(struct se_node_acl *acl)
361c66ac9dbSNicholas Bellinger {
362c7d6a803SChristoph Hellwig 	struct se_portal_group *tpg = acl->se_tpg;
363c66ac9dbSNicholas Bellinger 
364403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
365bc6e6bb4SChristoph Hellwig 	if (acl->dynamic_node_acl)
366c66ac9dbSNicholas Bellinger 		acl->dynamic_node_acl = 0;
367c66ac9dbSNicholas Bellinger 	list_del(&acl->acl_list);
368403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
369c66ac9dbSNicholas Bellinger 
370fba81f88SChristoph Hellwig 	target_shutdown_sessions(acl);
371337c0607SNicholas Bellinger 
372337c0607SNicholas Bellinger 	target_put_nacl(acl);
373337c0607SNicholas Bellinger 	/*
374337c0607SNicholas Bellinger 	 * Wait for last target_put_nacl() to complete in target_complete_nacl()
375337c0607SNicholas Bellinger 	 * for active fabric session transport_deregister_session() callbacks.
376337c0607SNicholas Bellinger 	 */
377337c0607SNicholas Bellinger 	wait_for_completion(&acl->acl_free_comp);
378c66ac9dbSNicholas Bellinger 
379c66ac9dbSNicholas Bellinger 	core_tpg_wait_for_nacl_pr_ref(acl);
380c66ac9dbSNicholas Bellinger 	core_free_device_list_for_node(acl, tpg);
381c66ac9dbSNicholas Bellinger 
3826708bb27SAndy Grover 	pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
383e3d6f909SAndy Grover 		" Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
384e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
385e3d6f909SAndy Grover 		tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
386c66ac9dbSNicholas Bellinger 
387144bc4c2SChristoph Hellwig 	kfree(acl);
388c66ac9dbSNicholas Bellinger }
389c66ac9dbSNicholas Bellinger 
390c66ac9dbSNicholas Bellinger /*	core_tpg_set_initiator_node_queue_depth():
391c66ac9dbSNicholas Bellinger  *
392c66ac9dbSNicholas Bellinger  *
393c66ac9dbSNicholas Bellinger  */
394c66ac9dbSNicholas Bellinger int core_tpg_set_initiator_node_queue_depth(
395d36ad77fSNicholas Bellinger 	struct se_node_acl *acl,
396d36ad77fSNicholas Bellinger 	u32 queue_depth)
397c66ac9dbSNicholas Bellinger {
398d36ad77fSNicholas Bellinger 	struct se_portal_group *tpg = acl->se_tpg;
399c66ac9dbSNicholas Bellinger 
400c66ac9dbSNicholas Bellinger 	/*
401c66ac9dbSNicholas Bellinger 	 * User has requested to change the queue depth for a Initiator Node.
402c66ac9dbSNicholas Bellinger 	 * Change the value in the Node's struct se_node_acl, and call
403d36ad77fSNicholas Bellinger 	 * target_set_nacl_queue_depth() to set the new queue depth.
404d36ad77fSNicholas Bellinger 	 */
405d36ad77fSNicholas Bellinger 	target_set_nacl_queue_depth(tpg, acl, queue_depth);
406d36ad77fSNicholas Bellinger 
407d36ad77fSNicholas Bellinger 	/*
408bc6e6bb4SChristoph Hellwig 	 * Shutdown all pending sessions to force session reinstatement.
409c66ac9dbSNicholas Bellinger 	 */
410fba81f88SChristoph Hellwig 	target_shutdown_sessions(acl);
411c66ac9dbSNicholas Bellinger 
412bfb9035cSJoe Perches 	pr_debug("Successfully changed queue depth to: %d for Initiator"
413d36ad77fSNicholas Bellinger 		" Node: %s on %s Target Portal Group: %u\n", acl->queue_depth,
414d36ad77fSNicholas Bellinger 		acl->initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
415e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg));
416c66ac9dbSNicholas Bellinger 
417c66ac9dbSNicholas Bellinger 	return 0;
418c66ac9dbSNicholas Bellinger }
419c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
420c66ac9dbSNicholas Bellinger 
42179e62fc3SAndy Grover /*	core_tpg_set_initiator_node_tag():
42279e62fc3SAndy Grover  *
42379e62fc3SAndy Grover  *	Initiator nodeacl tags are not used internally, but may be used by
42479e62fc3SAndy Grover  *	userspace to emulate aliases or groups.
42579e62fc3SAndy Grover  *	Returns length of newly-set tag or -EINVAL.
42679e62fc3SAndy Grover  */
42779e62fc3SAndy Grover int core_tpg_set_initiator_node_tag(
42879e62fc3SAndy Grover 	struct se_portal_group *tpg,
42979e62fc3SAndy Grover 	struct se_node_acl *acl,
43079e62fc3SAndy Grover 	const char *new_tag)
43179e62fc3SAndy Grover {
43279e62fc3SAndy Grover 	if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
43379e62fc3SAndy Grover 		return -EINVAL;
43479e62fc3SAndy Grover 
43579e62fc3SAndy Grover 	if (!strncmp("NULL", new_tag, 4)) {
43679e62fc3SAndy Grover 		acl->acl_tag[0] = '\0';
43779e62fc3SAndy Grover 		return 0;
43879e62fc3SAndy Grover 	}
43979e62fc3SAndy Grover 
44079e62fc3SAndy Grover 	return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
44179e62fc3SAndy Grover }
44279e62fc3SAndy Grover EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
44379e62fc3SAndy Grover 
4445277797dSNicholas Bellinger static void core_tpg_lun_ref_release(struct percpu_ref *ref)
4455277797dSNicholas Bellinger {
4465277797dSNicholas Bellinger 	struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
4475277797dSNicholas Bellinger 
4485277797dSNicholas Bellinger 	complete(&lun->lun_ref_comp);
4495277797dSNicholas Bellinger }
4505277797dSNicholas Bellinger 
4512bce1a6dSBart Van Assche /* Does not change se_wwn->priv. */
452c66ac9dbSNicholas Bellinger int core_tpg_register(
453c66ac9dbSNicholas Bellinger 	struct se_wwn *se_wwn,
454c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg,
455e4aae5afSChristoph Hellwig 	int proto_id)
456c66ac9dbSNicholas Bellinger {
457adf653f9SChristoph Hellwig 	int ret;
458c66ac9dbSNicholas Bellinger 
459bc0c94b1SNicholas Bellinger 	if (!se_tpg)
460bc0c94b1SNicholas Bellinger 		return -EINVAL;
461bc0c94b1SNicholas Bellinger 	/*
462bc0c94b1SNicholas Bellinger 	 * For the typical case where core_tpg_register() is called by a
463bc0c94b1SNicholas Bellinger 	 * fabric driver from target_core_fabric_ops->fabric_make_tpg()
464bc0c94b1SNicholas Bellinger 	 * configfs context, use the original tf_ops pointer already saved
465bc0c94b1SNicholas Bellinger 	 * by target-core in target_fabric_make_wwn().
466bc0c94b1SNicholas Bellinger 	 *
467bc0c94b1SNicholas Bellinger 	 * Otherwise, for special cases like iscsi-target discovery TPGs
468bc0c94b1SNicholas Bellinger 	 * the caller is responsible for setting ->se_tpg_tfo ahead of
469bc0c94b1SNicholas Bellinger 	 * calling core_tpg_register().
470bc0c94b1SNicholas Bellinger 	 */
471bc0c94b1SNicholas Bellinger 	if (se_wwn)
472bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops;
473bc0c94b1SNicholas Bellinger 
474bc0c94b1SNicholas Bellinger 	if (!se_tpg->se_tpg_tfo) {
475bc0c94b1SNicholas Bellinger 		pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n");
476bc0c94b1SNicholas Bellinger 		return -EINVAL;
477c66ac9dbSNicholas Bellinger 	}
478c66ac9dbSNicholas Bellinger 
4796bb82612SNicholas Bellinger 	INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
480e4aae5afSChristoph Hellwig 	se_tpg->proto_id = proto_id;
481c66ac9dbSNicholas Bellinger 	se_tpg->se_tpg_wwn = se_wwn;
482c66ac9dbSNicholas Bellinger 	atomic_set(&se_tpg->tpg_pr_ref_count, 0);
483c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->acl_node_list);
484e3d6f909SAndy Grover 	INIT_LIST_HEAD(&se_tpg->se_tpg_node);
485c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
486c66ac9dbSNicholas Bellinger 	spin_lock_init(&se_tpg->session_lock);
4876bb82612SNicholas Bellinger 	mutex_init(&se_tpg->tpg_lun_mutex);
488403edd78SNicholas Bellinger 	mutex_init(&se_tpg->acl_node_mutex);
489c66ac9dbSNicholas Bellinger 
490e4aae5afSChristoph Hellwig 	if (se_tpg->proto_id >= 0) {
491adf653f9SChristoph Hellwig 		se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
492adf653f9SChristoph Hellwig 		if (IS_ERR(se_tpg->tpg_virt_lun0))
493adf653f9SChristoph Hellwig 			return PTR_ERR(se_tpg->tpg_virt_lun0);
494adf653f9SChristoph Hellwig 
495adf653f9SChristoph Hellwig 		ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
49603a68b44SAndy Grover 				true, g_lun0_dev);
497adf653f9SChristoph Hellwig 		if (ret < 0) {
498adf653f9SChristoph Hellwig 			kfree(se_tpg->tpg_virt_lun0);
499adf653f9SChristoph Hellwig 			return ret;
500c66ac9dbSNicholas Bellinger 		}
501c66ac9dbSNicholas Bellinger 	}
502c66ac9dbSNicholas Bellinger 
503e3d6f909SAndy Grover 	spin_lock_bh(&tpg_lock);
504e3d6f909SAndy Grover 	list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
505e3d6f909SAndy Grover 	spin_unlock_bh(&tpg_lock);
506c66ac9dbSNicholas Bellinger 
507e4aae5afSChristoph Hellwig 	pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
508bc0c94b1SNicholas Bellinger 		 "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->get_fabric_name(),
509bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ?
510bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL,
511bc0c94b1SNicholas Bellinger 		se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
512c66ac9dbSNicholas Bellinger 
513c66ac9dbSNicholas Bellinger 	return 0;
514c66ac9dbSNicholas Bellinger }
515c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_register);
516c66ac9dbSNicholas Bellinger 
517c66ac9dbSNicholas Bellinger int core_tpg_deregister(struct se_portal_group *se_tpg)
518c66ac9dbSNicholas Bellinger {
519e4aae5afSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
520e89d15eeSNicholas Bellinger 	struct se_node_acl *nacl, *nacl_tmp;
52122793de5SNicholas Bellinger 	LIST_HEAD(node_list);
522e89d15eeSNicholas Bellinger 
523e4aae5afSChristoph Hellwig 	pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
524e4aae5afSChristoph Hellwig 		 "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(),
525e4aae5afSChristoph Hellwig 		tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
526e4aae5afSChristoph Hellwig 		se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
527c66ac9dbSNicholas Bellinger 
528e3d6f909SAndy Grover 	spin_lock_bh(&tpg_lock);
529e3d6f909SAndy Grover 	list_del(&se_tpg->se_tpg_node);
530e3d6f909SAndy Grover 	spin_unlock_bh(&tpg_lock);
531c66ac9dbSNicholas Bellinger 
532c66ac9dbSNicholas Bellinger 	while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
533c66ac9dbSNicholas Bellinger 		cpu_relax();
534403edd78SNicholas Bellinger 
53522793de5SNicholas Bellinger 	mutex_lock(&se_tpg->acl_node_mutex);
53622793de5SNicholas Bellinger 	list_splice_init(&se_tpg->acl_node_list, &node_list);
53722793de5SNicholas Bellinger 	mutex_unlock(&se_tpg->acl_node_mutex);
538e89d15eeSNicholas Bellinger 	/*
539e89d15eeSNicholas Bellinger 	 * Release any remaining demo-mode generated se_node_acl that have
540e89d15eeSNicholas Bellinger 	 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
541e89d15eeSNicholas Bellinger 	 * in transport_deregister_session().
542e89d15eeSNicholas Bellinger 	 */
54322793de5SNicholas Bellinger 	list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) {
544e89d15eeSNicholas Bellinger 		list_del(&nacl->acl_list);
545e89d15eeSNicholas Bellinger 
546e89d15eeSNicholas Bellinger 		core_tpg_wait_for_nacl_pr_ref(nacl);
547e89d15eeSNicholas Bellinger 		core_free_device_list_for_node(nacl, se_tpg);
548144bc4c2SChristoph Hellwig 		kfree(nacl);
549e89d15eeSNicholas Bellinger 	}
550c66ac9dbSNicholas Bellinger 
551adf653f9SChristoph Hellwig 	if (se_tpg->proto_id >= 0) {
552adf653f9SChristoph Hellwig 		core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
553adf653f9SChristoph Hellwig 		kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
554adf653f9SChristoph Hellwig 	}
555c66ac9dbSNicholas Bellinger 
556c66ac9dbSNicholas Bellinger 	return 0;
557c66ac9dbSNicholas Bellinger }
558c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_deregister);
559c66ac9dbSNicholas Bellinger 
560d344f8a1SAndy Grover struct se_lun *core_tpg_alloc_lun(
561c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
562f2d30680SHannes Reinecke 	u64 unpacked_lun)
563c66ac9dbSNicholas Bellinger {
564c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
565c66ac9dbSNicholas Bellinger 
5666bb82612SNicholas Bellinger 	lun = kzalloc(sizeof(*lun), GFP_KERNEL);
5676bb82612SNicholas Bellinger 	if (!lun) {
5686bb82612SNicholas Bellinger 		pr_err("Unable to allocate se_lun memory\n");
5696bb82612SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
570c66ac9dbSNicholas Bellinger 	}
5716bb82612SNicholas Bellinger 	lun->unpacked_lun = unpacked_lun;
5726bb82612SNicholas Bellinger 	lun->lun_link_magic = SE_LUN_LINK_MAGIC;
5736bb82612SNicholas Bellinger 	atomic_set(&lun->lun_acl_count, 0);
5746bb82612SNicholas Bellinger 	init_completion(&lun->lun_ref_comp);
575adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_deve_list);
576adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_dev_link);
577adf653f9SChristoph Hellwig 	atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
578adf653f9SChristoph Hellwig 	spin_lock_init(&lun->lun_deve_lock);
579adf653f9SChristoph Hellwig 	mutex_init(&lun->lun_tg_pt_md_mutex);
580adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
581adf653f9SChristoph Hellwig 	spin_lock_init(&lun->lun_tg_pt_gp_lock);
582adf653f9SChristoph Hellwig 	lun->lun_tpg = tpg;
583c66ac9dbSNicholas Bellinger 
584c66ac9dbSNicholas Bellinger 	return lun;
585c66ac9dbSNicholas Bellinger }
586c66ac9dbSNicholas Bellinger 
587d344f8a1SAndy Grover int core_tpg_add_lun(
588c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
589c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
59003a68b44SAndy Grover 	bool lun_access_ro,
591340dbf72SAndy Grover 	struct se_device *dev)
592c66ac9dbSNicholas Bellinger {
593e3d6f909SAndy Grover 	int ret;
594e3d6f909SAndy Grover 
5952aad2a86STejun Heo 	ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
596a34375efSTejun Heo 			      GFP_KERNEL);
597e3d6f909SAndy Grover 	if (ret < 0)
598adf653f9SChristoph Hellwig 		goto out;
599c66ac9dbSNicholas Bellinger 
600adf653f9SChristoph Hellwig 	ret = core_alloc_rtpi(lun, dev);
601adf653f9SChristoph Hellwig 	if (ret)
602adf653f9SChristoph Hellwig 		goto out_kill_ref;
6035277797dSNicholas Bellinger 
604adf653f9SChristoph Hellwig 	if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
605adf653f9SChristoph Hellwig 	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
606adf653f9SChristoph Hellwig 		target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
607c66ac9dbSNicholas Bellinger 
6086bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
609adf653f9SChristoph Hellwig 
610adf653f9SChristoph Hellwig 	spin_lock(&dev->se_port_lock);
6114cc987eaSNicholas Bellinger 	lun->lun_index = dev->dev_index;
612adf653f9SChristoph Hellwig 	rcu_assign_pointer(lun->lun_se_dev, dev);
613adf653f9SChristoph Hellwig 	dev->export_count++;
614adf653f9SChristoph Hellwig 	list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list);
615adf653f9SChristoph Hellwig 	spin_unlock(&dev->se_port_lock);
616adf653f9SChristoph Hellwig 
617eeeb9522SNicholas Bellinger 	if (dev->dev_flags & DF_READ_ONLY)
61803a68b44SAndy Grover 		lun->lun_access_ro = true;
619eeeb9522SNicholas Bellinger 	else
62003a68b44SAndy Grover 		lun->lun_access_ro = lun_access_ro;
6216bb82612SNicholas Bellinger 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
6226bb82612SNicholas Bellinger 		hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
6236bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
624c66ac9dbSNicholas Bellinger 
625c66ac9dbSNicholas Bellinger 	return 0;
626adf653f9SChristoph Hellwig 
627adf653f9SChristoph Hellwig out_kill_ref:
628adf653f9SChristoph Hellwig 	percpu_ref_exit(&lun->lun_ref);
629adf653f9SChristoph Hellwig out:
630adf653f9SChristoph Hellwig 	return ret;
631c66ac9dbSNicholas Bellinger }
632c66ac9dbSNicholas Bellinger 
633cd9d7cbaSAndy Grover void core_tpg_remove_lun(
634c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
635c66ac9dbSNicholas Bellinger 	struct se_lun *lun)
636c66ac9dbSNicholas Bellinger {
6374cc987eaSNicholas Bellinger 	/*
6384cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
6394cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
6404cc987eaSNicholas Bellinger 	 */
6414cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
6426bb82612SNicholas Bellinger 
6434a9a6c8dSNicholas Bellinger 	core_clear_lun_from_tpg(lun, tpg);
6449e37d042SNicholas Bellinger 	/*
6459e37d042SNicholas Bellinger 	 * Wait for any active I/O references to percpu se_lun->lun_ref to
6469e37d042SNicholas Bellinger 	 * be released.  Also, se_lun->lun_ref is now used by PR and ALUA
6479e37d042SNicholas Bellinger 	 * logic when referencing a remote target port during ALL_TGT_PT=1
6489e37d042SNicholas Bellinger 	 * and generating UNIT_ATTENTIONs for ALUA access state transition.
6499e37d042SNicholas Bellinger 	 */
6504a9a6c8dSNicholas Bellinger 	transport_clear_lun_ref(lun);
651c66ac9dbSNicholas Bellinger 
6526bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
653adf653f9SChristoph Hellwig 	if (lun->lun_se_dev) {
654adf653f9SChristoph Hellwig 		target_detach_tg_pt_gp(lun);
655c66ac9dbSNicholas Bellinger 
656adf653f9SChristoph Hellwig 		spin_lock(&dev->se_port_lock);
657adf653f9SChristoph Hellwig 		list_del(&lun->lun_dev_link);
658adf653f9SChristoph Hellwig 		dev->export_count--;
659adf653f9SChristoph Hellwig 		rcu_assign_pointer(lun->lun_se_dev, NULL);
660adf653f9SChristoph Hellwig 		spin_unlock(&dev->se_port_lock);
661adf653f9SChristoph Hellwig 	}
6626bb82612SNicholas Bellinger 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
6636bb82612SNicholas Bellinger 		hlist_del_rcu(&lun->link);
6646bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
665c66ac9dbSNicholas Bellinger 
6669a1049daSTejun Heo 	percpu_ref_exit(&lun->lun_ref);
667c66ac9dbSNicholas Bellinger }
668