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 
339c7d6a803SChristoph Hellwig void core_tpg_del_initiator_node_acl(struct se_node_acl *acl)
340c66ac9dbSNicholas Bellinger {
341c7d6a803SChristoph Hellwig 	struct se_portal_group *tpg = acl->se_tpg;
342337c0607SNicholas Bellinger 	LIST_HEAD(sess_list);
343c66ac9dbSNicholas Bellinger 	struct se_session *sess, *sess_tmp;
344140854cbSNicholas Bellinger 	unsigned long flags;
34528168905SJörn Engel 	int rc;
346c66ac9dbSNicholas Bellinger 
347403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
348c66ac9dbSNicholas Bellinger 	if (acl->dynamic_node_acl) {
349c66ac9dbSNicholas Bellinger 		acl->dynamic_node_acl = 0;
350c66ac9dbSNicholas Bellinger 	}
351c66ac9dbSNicholas Bellinger 	list_del(&acl->acl_list);
352403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
353c66ac9dbSNicholas Bellinger 
354337c0607SNicholas Bellinger 	spin_lock_irqsave(&acl->nacl_sess_lock, flags);
355337c0607SNicholas Bellinger 	acl->acl_stop = 1;
356337c0607SNicholas Bellinger 
357337c0607SNicholas Bellinger 	list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list,
358337c0607SNicholas Bellinger 				sess_acl_list) {
359337c0607SNicholas Bellinger 		if (sess->sess_tearing_down != 0)
360c66ac9dbSNicholas Bellinger 			continue;
361c66ac9dbSNicholas Bellinger 
362d36ad77fSNicholas Bellinger 		if (!target_get_session(sess))
363d36ad77fSNicholas Bellinger 			continue;
364337c0607SNicholas Bellinger 		list_move(&sess->sess_acl_list, &sess_list);
365c66ac9dbSNicholas Bellinger 	}
366337c0607SNicholas Bellinger 	spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
367337c0607SNicholas Bellinger 
368337c0607SNicholas Bellinger 	list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) {
369337c0607SNicholas Bellinger 		list_del(&sess->sess_acl_list);
370337c0607SNicholas Bellinger 
371337c0607SNicholas Bellinger 		rc = tpg->se_tpg_tfo->shutdown_session(sess);
372337c0607SNicholas Bellinger 		target_put_session(sess);
373337c0607SNicholas Bellinger 		if (!rc)
374337c0607SNicholas Bellinger 			continue;
375337c0607SNicholas Bellinger 		target_put_session(sess);
376337c0607SNicholas Bellinger 	}
377337c0607SNicholas Bellinger 	target_put_nacl(acl);
378337c0607SNicholas Bellinger 	/*
379337c0607SNicholas Bellinger 	 * Wait for last target_put_nacl() to complete in target_complete_nacl()
380337c0607SNicholas Bellinger 	 * for active fabric session transport_deregister_session() callbacks.
381337c0607SNicholas Bellinger 	 */
382337c0607SNicholas Bellinger 	wait_for_completion(&acl->acl_free_comp);
383c66ac9dbSNicholas Bellinger 
384c66ac9dbSNicholas Bellinger 	core_tpg_wait_for_nacl_pr_ref(acl);
385c66ac9dbSNicholas Bellinger 	core_free_device_list_for_node(acl, tpg);
386c66ac9dbSNicholas Bellinger 
3876708bb27SAndy Grover 	pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
388e3d6f909SAndy Grover 		" Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
389e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
390e3d6f909SAndy Grover 		tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
391c66ac9dbSNicholas Bellinger 
392144bc4c2SChristoph Hellwig 	kfree(acl);
393c66ac9dbSNicholas Bellinger }
394c66ac9dbSNicholas Bellinger 
395c66ac9dbSNicholas Bellinger /*	core_tpg_set_initiator_node_queue_depth():
396c66ac9dbSNicholas Bellinger  *
397c66ac9dbSNicholas Bellinger  *
398c66ac9dbSNicholas Bellinger  */
399c66ac9dbSNicholas Bellinger int core_tpg_set_initiator_node_queue_depth(
400d36ad77fSNicholas Bellinger 	struct se_node_acl *acl,
401d36ad77fSNicholas Bellinger 	u32 queue_depth)
402c66ac9dbSNicholas Bellinger {
403d36ad77fSNicholas Bellinger 	LIST_HEAD(sess_list);
404d36ad77fSNicholas Bellinger 	struct se_portal_group *tpg = acl->se_tpg;
405d36ad77fSNicholas Bellinger 	struct se_session *sess, *sess_tmp;
406140854cbSNicholas Bellinger 	unsigned long flags;
407d36ad77fSNicholas Bellinger 	int rc;
408c66ac9dbSNicholas Bellinger 
409c66ac9dbSNicholas Bellinger 	/*
410c66ac9dbSNicholas Bellinger 	 * User has requested to change the queue depth for a Initiator Node.
411c66ac9dbSNicholas Bellinger 	 * Change the value in the Node's struct se_node_acl, and call
412d36ad77fSNicholas Bellinger 	 * target_set_nacl_queue_depth() to set the new queue depth.
413d36ad77fSNicholas Bellinger 	 */
414d36ad77fSNicholas Bellinger 	target_set_nacl_queue_depth(tpg, acl, queue_depth);
415d36ad77fSNicholas Bellinger 
416d36ad77fSNicholas Bellinger 	spin_lock_irqsave(&acl->nacl_sess_lock, flags);
417d36ad77fSNicholas Bellinger 	list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list,
418d36ad77fSNicholas Bellinger 				 sess_acl_list) {
419d36ad77fSNicholas Bellinger 		if (sess->sess_tearing_down != 0)
420d36ad77fSNicholas Bellinger 			continue;
421d36ad77fSNicholas Bellinger 		if (!target_get_session(sess))
422d36ad77fSNicholas Bellinger 			continue;
423d36ad77fSNicholas Bellinger 		spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
424d36ad77fSNicholas Bellinger 
425d36ad77fSNicholas Bellinger 		/*
426e3d6f909SAndy Grover 		 * Finally call tpg->se_tpg_tfo->close_session() to force session
427c66ac9dbSNicholas Bellinger 		 * reinstatement to occur if there is an active session for the
428c66ac9dbSNicholas Bellinger 		 * $FABRIC_MOD Initiator Node in question.
429c66ac9dbSNicholas Bellinger 		 */
430d36ad77fSNicholas Bellinger 		rc = tpg->se_tpg_tfo->shutdown_session(sess);
431d36ad77fSNicholas Bellinger 		target_put_session(sess);
432d36ad77fSNicholas Bellinger 		if (!rc) {
433d36ad77fSNicholas Bellinger 			spin_lock_irqsave(&acl->nacl_sess_lock, flags);
434d36ad77fSNicholas Bellinger 			continue;
435c66ac9dbSNicholas Bellinger 		}
436d36ad77fSNicholas Bellinger 		target_put_session(sess);
437d36ad77fSNicholas Bellinger 		spin_lock_irqsave(&acl->nacl_sess_lock, flags);
438d36ad77fSNicholas Bellinger 	}
439d36ad77fSNicholas Bellinger 	spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
440c66ac9dbSNicholas Bellinger 
441bfb9035cSJoe Perches 	pr_debug("Successfully changed queue depth to: %d for Initiator"
442d36ad77fSNicholas Bellinger 		" Node: %s on %s Target Portal Group: %u\n", acl->queue_depth,
443d36ad77fSNicholas Bellinger 		acl->initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
444e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg));
445c66ac9dbSNicholas Bellinger 
446c66ac9dbSNicholas Bellinger 	return 0;
447c66ac9dbSNicholas Bellinger }
448c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
449c66ac9dbSNicholas Bellinger 
45079e62fc3SAndy Grover /*	core_tpg_set_initiator_node_tag():
45179e62fc3SAndy Grover  *
45279e62fc3SAndy Grover  *	Initiator nodeacl tags are not used internally, but may be used by
45379e62fc3SAndy Grover  *	userspace to emulate aliases or groups.
45479e62fc3SAndy Grover  *	Returns length of newly-set tag or -EINVAL.
45579e62fc3SAndy Grover  */
45679e62fc3SAndy Grover int core_tpg_set_initiator_node_tag(
45779e62fc3SAndy Grover 	struct se_portal_group *tpg,
45879e62fc3SAndy Grover 	struct se_node_acl *acl,
45979e62fc3SAndy Grover 	const char *new_tag)
46079e62fc3SAndy Grover {
46179e62fc3SAndy Grover 	if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
46279e62fc3SAndy Grover 		return -EINVAL;
46379e62fc3SAndy Grover 
46479e62fc3SAndy Grover 	if (!strncmp("NULL", new_tag, 4)) {
46579e62fc3SAndy Grover 		acl->acl_tag[0] = '\0';
46679e62fc3SAndy Grover 		return 0;
46779e62fc3SAndy Grover 	}
46879e62fc3SAndy Grover 
46979e62fc3SAndy Grover 	return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
47079e62fc3SAndy Grover }
47179e62fc3SAndy Grover EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
47279e62fc3SAndy Grover 
4735277797dSNicholas Bellinger static void core_tpg_lun_ref_release(struct percpu_ref *ref)
4745277797dSNicholas Bellinger {
4755277797dSNicholas Bellinger 	struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
4765277797dSNicholas Bellinger 
4775277797dSNicholas Bellinger 	complete(&lun->lun_ref_comp);
4785277797dSNicholas Bellinger }
4795277797dSNicholas Bellinger 
480c66ac9dbSNicholas Bellinger int core_tpg_register(
481c66ac9dbSNicholas Bellinger 	struct se_wwn *se_wwn,
482c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg,
483e4aae5afSChristoph Hellwig 	int proto_id)
484c66ac9dbSNicholas Bellinger {
485adf653f9SChristoph Hellwig 	int ret;
486c66ac9dbSNicholas Bellinger 
487bc0c94b1SNicholas Bellinger 	if (!se_tpg)
488bc0c94b1SNicholas Bellinger 		return -EINVAL;
489bc0c94b1SNicholas Bellinger 	/*
490bc0c94b1SNicholas Bellinger 	 * For the typical case where core_tpg_register() is called by a
491bc0c94b1SNicholas Bellinger 	 * fabric driver from target_core_fabric_ops->fabric_make_tpg()
492bc0c94b1SNicholas Bellinger 	 * configfs context, use the original tf_ops pointer already saved
493bc0c94b1SNicholas Bellinger 	 * by target-core in target_fabric_make_wwn().
494bc0c94b1SNicholas Bellinger 	 *
495bc0c94b1SNicholas Bellinger 	 * Otherwise, for special cases like iscsi-target discovery TPGs
496bc0c94b1SNicholas Bellinger 	 * the caller is responsible for setting ->se_tpg_tfo ahead of
497bc0c94b1SNicholas Bellinger 	 * calling core_tpg_register().
498bc0c94b1SNicholas Bellinger 	 */
499bc0c94b1SNicholas Bellinger 	if (se_wwn)
500bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops;
501bc0c94b1SNicholas Bellinger 
502bc0c94b1SNicholas Bellinger 	if (!se_tpg->se_tpg_tfo) {
503bc0c94b1SNicholas Bellinger 		pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n");
504bc0c94b1SNicholas Bellinger 		return -EINVAL;
505c66ac9dbSNicholas Bellinger 	}
506c66ac9dbSNicholas Bellinger 
5076bb82612SNicholas Bellinger 	INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
508e4aae5afSChristoph Hellwig 	se_tpg->proto_id = proto_id;
509c66ac9dbSNicholas Bellinger 	se_tpg->se_tpg_wwn = se_wwn;
510c66ac9dbSNicholas Bellinger 	atomic_set(&se_tpg->tpg_pr_ref_count, 0);
511c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->acl_node_list);
512e3d6f909SAndy Grover 	INIT_LIST_HEAD(&se_tpg->se_tpg_node);
513c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
514c66ac9dbSNicholas Bellinger 	spin_lock_init(&se_tpg->session_lock);
5156bb82612SNicholas Bellinger 	mutex_init(&se_tpg->tpg_lun_mutex);
516403edd78SNicholas Bellinger 	mutex_init(&se_tpg->acl_node_mutex);
517c66ac9dbSNicholas Bellinger 
518e4aae5afSChristoph Hellwig 	if (se_tpg->proto_id >= 0) {
519adf653f9SChristoph Hellwig 		se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
520adf653f9SChristoph Hellwig 		if (IS_ERR(se_tpg->tpg_virt_lun0))
521adf653f9SChristoph Hellwig 			return PTR_ERR(se_tpg->tpg_virt_lun0);
522adf653f9SChristoph Hellwig 
523adf653f9SChristoph Hellwig 		ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
52403a68b44SAndy Grover 				true, g_lun0_dev);
525adf653f9SChristoph Hellwig 		if (ret < 0) {
526adf653f9SChristoph Hellwig 			kfree(se_tpg->tpg_virt_lun0);
527adf653f9SChristoph Hellwig 			return ret;
528c66ac9dbSNicholas Bellinger 		}
529c66ac9dbSNicholas Bellinger 	}
530c66ac9dbSNicholas Bellinger 
531e3d6f909SAndy Grover 	spin_lock_bh(&tpg_lock);
532e3d6f909SAndy Grover 	list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
533e3d6f909SAndy Grover 	spin_unlock_bh(&tpg_lock);
534c66ac9dbSNicholas Bellinger 
535e4aae5afSChristoph Hellwig 	pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
536bc0c94b1SNicholas Bellinger 		 "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->get_fabric_name(),
537bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ?
538bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL,
539bc0c94b1SNicholas Bellinger 		se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
540c66ac9dbSNicholas Bellinger 
541c66ac9dbSNicholas Bellinger 	return 0;
542c66ac9dbSNicholas Bellinger }
543c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_register);
544c66ac9dbSNicholas Bellinger 
545c66ac9dbSNicholas Bellinger int core_tpg_deregister(struct se_portal_group *se_tpg)
546c66ac9dbSNicholas Bellinger {
547e4aae5afSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
548e89d15eeSNicholas Bellinger 	struct se_node_acl *nacl, *nacl_tmp;
54922793de5SNicholas Bellinger 	LIST_HEAD(node_list);
550e89d15eeSNicholas Bellinger 
551e4aae5afSChristoph Hellwig 	pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
552e4aae5afSChristoph Hellwig 		 "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(),
553e4aae5afSChristoph Hellwig 		tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
554e4aae5afSChristoph Hellwig 		se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
555c66ac9dbSNicholas Bellinger 
556e3d6f909SAndy Grover 	spin_lock_bh(&tpg_lock);
557e3d6f909SAndy Grover 	list_del(&se_tpg->se_tpg_node);
558e3d6f909SAndy Grover 	spin_unlock_bh(&tpg_lock);
559c66ac9dbSNicholas Bellinger 
560c66ac9dbSNicholas Bellinger 	while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
561c66ac9dbSNicholas Bellinger 		cpu_relax();
562403edd78SNicholas Bellinger 
56322793de5SNicholas Bellinger 	mutex_lock(&se_tpg->acl_node_mutex);
56422793de5SNicholas Bellinger 	list_splice_init(&se_tpg->acl_node_list, &node_list);
56522793de5SNicholas Bellinger 	mutex_unlock(&se_tpg->acl_node_mutex);
566e89d15eeSNicholas Bellinger 	/*
567e89d15eeSNicholas Bellinger 	 * Release any remaining demo-mode generated se_node_acl that have
568e89d15eeSNicholas Bellinger 	 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
569e89d15eeSNicholas Bellinger 	 * in transport_deregister_session().
570e89d15eeSNicholas Bellinger 	 */
57122793de5SNicholas Bellinger 	list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) {
572e89d15eeSNicholas Bellinger 		list_del(&nacl->acl_list);
573e89d15eeSNicholas Bellinger 
574e89d15eeSNicholas Bellinger 		core_tpg_wait_for_nacl_pr_ref(nacl);
575e89d15eeSNicholas Bellinger 		core_free_device_list_for_node(nacl, se_tpg);
576144bc4c2SChristoph Hellwig 		kfree(nacl);
577e89d15eeSNicholas Bellinger 	}
578c66ac9dbSNicholas Bellinger 
579adf653f9SChristoph Hellwig 	if (se_tpg->proto_id >= 0) {
580adf653f9SChristoph Hellwig 		core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
581adf653f9SChristoph Hellwig 		kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
582adf653f9SChristoph Hellwig 	}
583c66ac9dbSNicholas Bellinger 
584c66ac9dbSNicholas Bellinger 	return 0;
585c66ac9dbSNicholas Bellinger }
586c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_deregister);
587c66ac9dbSNicholas Bellinger 
588d344f8a1SAndy Grover struct se_lun *core_tpg_alloc_lun(
589c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
590f2d30680SHannes Reinecke 	u64 unpacked_lun)
591c66ac9dbSNicholas Bellinger {
592c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
593c66ac9dbSNicholas Bellinger 
5946bb82612SNicholas Bellinger 	lun = kzalloc(sizeof(*lun), GFP_KERNEL);
5956bb82612SNicholas Bellinger 	if (!lun) {
5966bb82612SNicholas Bellinger 		pr_err("Unable to allocate se_lun memory\n");
5976bb82612SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
598c66ac9dbSNicholas Bellinger 	}
5996bb82612SNicholas Bellinger 	lun->unpacked_lun = unpacked_lun;
6006bb82612SNicholas Bellinger 	lun->lun_link_magic = SE_LUN_LINK_MAGIC;
6016bb82612SNicholas Bellinger 	atomic_set(&lun->lun_acl_count, 0);
6026bb82612SNicholas Bellinger 	init_completion(&lun->lun_ref_comp);
603adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_deve_list);
604adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_dev_link);
605adf653f9SChristoph Hellwig 	atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
606adf653f9SChristoph Hellwig 	spin_lock_init(&lun->lun_deve_lock);
607adf653f9SChristoph Hellwig 	mutex_init(&lun->lun_tg_pt_md_mutex);
608adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
609adf653f9SChristoph Hellwig 	spin_lock_init(&lun->lun_tg_pt_gp_lock);
610adf653f9SChristoph Hellwig 	lun->lun_tpg = tpg;
611c66ac9dbSNicholas Bellinger 
612c66ac9dbSNicholas Bellinger 	return lun;
613c66ac9dbSNicholas Bellinger }
614c66ac9dbSNicholas Bellinger 
615d344f8a1SAndy Grover int core_tpg_add_lun(
616c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
617c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
61803a68b44SAndy Grover 	bool lun_access_ro,
619340dbf72SAndy Grover 	struct se_device *dev)
620c66ac9dbSNicholas Bellinger {
621e3d6f909SAndy Grover 	int ret;
622e3d6f909SAndy Grover 
6232aad2a86STejun Heo 	ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
624a34375efSTejun Heo 			      GFP_KERNEL);
625e3d6f909SAndy Grover 	if (ret < 0)
626adf653f9SChristoph Hellwig 		goto out;
627c66ac9dbSNicholas Bellinger 
628adf653f9SChristoph Hellwig 	ret = core_alloc_rtpi(lun, dev);
629adf653f9SChristoph Hellwig 	if (ret)
630adf653f9SChristoph Hellwig 		goto out_kill_ref;
6315277797dSNicholas Bellinger 
632adf653f9SChristoph Hellwig 	if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
633adf653f9SChristoph Hellwig 	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
634adf653f9SChristoph Hellwig 		target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
635c66ac9dbSNicholas Bellinger 
6366bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
637adf653f9SChristoph Hellwig 
638adf653f9SChristoph Hellwig 	spin_lock(&dev->se_port_lock);
6394cc987eaSNicholas Bellinger 	lun->lun_index = dev->dev_index;
640adf653f9SChristoph Hellwig 	rcu_assign_pointer(lun->lun_se_dev, dev);
641adf653f9SChristoph Hellwig 	dev->export_count++;
642adf653f9SChristoph Hellwig 	list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list);
643adf653f9SChristoph Hellwig 	spin_unlock(&dev->se_port_lock);
644adf653f9SChristoph Hellwig 
645eeeb9522SNicholas Bellinger 	if (dev->dev_flags & DF_READ_ONLY)
64603a68b44SAndy Grover 		lun->lun_access_ro = true;
647eeeb9522SNicholas Bellinger 	else
64803a68b44SAndy Grover 		lun->lun_access_ro = lun_access_ro;
6496bb82612SNicholas Bellinger 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
6506bb82612SNicholas Bellinger 		hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
6516bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
652c66ac9dbSNicholas Bellinger 
653c66ac9dbSNicholas Bellinger 	return 0;
654adf653f9SChristoph Hellwig 
655adf653f9SChristoph Hellwig out_kill_ref:
656adf653f9SChristoph Hellwig 	percpu_ref_exit(&lun->lun_ref);
657adf653f9SChristoph Hellwig out:
658adf653f9SChristoph Hellwig 	return ret;
659c66ac9dbSNicholas Bellinger }
660c66ac9dbSNicholas Bellinger 
661cd9d7cbaSAndy Grover void core_tpg_remove_lun(
662c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
663c66ac9dbSNicholas Bellinger 	struct se_lun *lun)
664c66ac9dbSNicholas Bellinger {
6654cc987eaSNicholas Bellinger 	/*
6664cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
6674cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
6684cc987eaSNicholas Bellinger 	 */
6694cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
6706bb82612SNicholas Bellinger 
6714a9a6c8dSNicholas Bellinger 	core_clear_lun_from_tpg(lun, tpg);
6729e37d042SNicholas Bellinger 	/*
6739e37d042SNicholas Bellinger 	 * Wait for any active I/O references to percpu se_lun->lun_ref to
6749e37d042SNicholas Bellinger 	 * be released.  Also, se_lun->lun_ref is now used by PR and ALUA
6759e37d042SNicholas Bellinger 	 * logic when referencing a remote target port during ALL_TGT_PT=1
6769e37d042SNicholas Bellinger 	 * and generating UNIT_ATTENTIONs for ALUA access state transition.
6779e37d042SNicholas Bellinger 	 */
6784a9a6c8dSNicholas Bellinger 	transport_clear_lun_ref(lun);
679c66ac9dbSNicholas Bellinger 
6806bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
681adf653f9SChristoph Hellwig 	if (lun->lun_se_dev) {
682adf653f9SChristoph Hellwig 		target_detach_tg_pt_gp(lun);
683c66ac9dbSNicholas Bellinger 
684adf653f9SChristoph Hellwig 		spin_lock(&dev->se_port_lock);
685adf653f9SChristoph Hellwig 		list_del(&lun->lun_dev_link);
686adf653f9SChristoph Hellwig 		dev->export_count--;
687adf653f9SChristoph Hellwig 		rcu_assign_pointer(lun->lun_se_dev, NULL);
688adf653f9SChristoph Hellwig 		spin_unlock(&dev->se_port_lock);
689adf653f9SChristoph Hellwig 	}
6906bb82612SNicholas Bellinger 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
6916bb82612SNicholas Bellinger 		hlist_del_rcu(&lun->link);
6926bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
693c66ac9dbSNicholas Bellinger 
6949a1049daSTejun Heo 	percpu_ref_exit(&lun->lun_ref);
695c66ac9dbSNicholas Bellinger }
696