xref: /openbmc/linux/drivers/target/target_core_tpg.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger  * Filename:  target_core_tpg.c
4c66ac9dbSNicholas Bellinger  *
5c66ac9dbSNicholas Bellinger  * This file contains generic Target Portal Group related functions.
6c66ac9dbSNicholas Bellinger  *
74c76251eSNicholas Bellinger  * (c) Copyright 2002-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  ******************************************************************************/
12c66ac9dbSNicholas Bellinger 
13c66ac9dbSNicholas Bellinger #include <linux/net.h>
14c66ac9dbSNicholas Bellinger #include <linux/string.h>
15c66ac9dbSNicholas Bellinger #include <linux/timer.h>
16c66ac9dbSNicholas Bellinger #include <linux/slab.h>
17c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
18c66ac9dbSNicholas Bellinger #include <linux/in.h>
19c53181afSPaul Gortmaker #include <linux/export.h>
20c66ac9dbSNicholas Bellinger #include <net/sock.h>
21c66ac9dbSNicholas Bellinger #include <net/tcp.h>
22ba929992SBart Van Assche #include <scsi/scsi_proto.h>
23c66ac9dbSNicholas Bellinger 
24c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
25c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
26c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
27c66ac9dbSNicholas Bellinger 
28e26d99aeSChristoph Hellwig #include "target_core_internal.h"
29adf653f9SChristoph Hellwig #include "target_core_alua.h"
30e2480563SNicholas Bellinger #include "target_core_pr.h"
31e986a35aSHannes Reinecke #include "target_core_ua.h"
32e3d6f909SAndy Grover 
33e3d6f909SAndy Grover extern struct se_device *g_lun0_dev;
343f4b9cb4SDmitry Bogdanov static DEFINE_XARRAY_ALLOC(tpg_xa);
35e3d6f909SAndy Grover 
36c66ac9dbSNicholas Bellinger /*	__core_tpg_get_initiator_node_acl():
37c66ac9dbSNicholas Bellinger  *
38403edd78SNicholas Bellinger  *	mutex_lock(&tpg->acl_node_mutex); must be held when calling
39c66ac9dbSNicholas Bellinger  */
__core_tpg_get_initiator_node_acl(struct se_portal_group * tpg,const char * initiatorname)40c66ac9dbSNicholas Bellinger struct se_node_acl *__core_tpg_get_initiator_node_acl(
41c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
42c66ac9dbSNicholas Bellinger 	const char *initiatorname)
43c66ac9dbSNicholas Bellinger {
44c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
45c66ac9dbSNicholas Bellinger 
46c66ac9dbSNicholas Bellinger 	list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
476708bb27SAndy Grover 		if (!strcmp(acl->initiatorname, initiatorname))
48c66ac9dbSNicholas Bellinger 			return acl;
49c66ac9dbSNicholas Bellinger 	}
50c66ac9dbSNicholas Bellinger 
51c66ac9dbSNicholas Bellinger 	return NULL;
52c66ac9dbSNicholas Bellinger }
53c66ac9dbSNicholas Bellinger 
54c66ac9dbSNicholas Bellinger /*	core_tpg_get_initiator_node_acl():
55c66ac9dbSNicholas Bellinger  *
56c66ac9dbSNicholas Bellinger  *
57c66ac9dbSNicholas Bellinger  */
core_tpg_get_initiator_node_acl(struct se_portal_group * tpg,unsigned char * initiatorname)58c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_get_initiator_node_acl(
59c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
60c66ac9dbSNicholas Bellinger 	unsigned char *initiatorname)
61c66ac9dbSNicholas Bellinger {
62c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
6321aaa23bSNicholas Bellinger 	/*
6421aaa23bSNicholas Bellinger 	 * Obtain se_node_acl->acl_kref using fabric driver provided
6521aaa23bSNicholas Bellinger 	 * initiatorname[] during node acl endpoint lookup driven by
6621aaa23bSNicholas Bellinger 	 * new se_session login.
6721aaa23bSNicholas Bellinger 	 *
6821aaa23bSNicholas Bellinger 	 * The reference is held until se_session shutdown -> release
6921aaa23bSNicholas Bellinger 	 * occurs via fabric driver invoked transport_deregister_session()
7021aaa23bSNicholas Bellinger 	 * or transport_free_session() code.
7121aaa23bSNicholas Bellinger 	 */
72403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
73fcf29481SNicholas Bellinger 	acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
7421aaa23bSNicholas Bellinger 	if (acl) {
7521aaa23bSNicholas Bellinger 		if (!kref_get_unless_zero(&acl->acl_kref))
7621aaa23bSNicholas Bellinger 			acl = NULL;
7721aaa23bSNicholas Bellinger 	}
78403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
79c66ac9dbSNicholas Bellinger 
80fcf29481SNicholas Bellinger 	return acl;
81c66ac9dbSNicholas Bellinger }
82b3fde035SThomas Glanzmann EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
83c66ac9dbSNicholas Bellinger 
core_allocate_nexus_loss_ua(struct se_node_acl * nacl)84e986a35aSHannes Reinecke void core_allocate_nexus_loss_ua(
85e986a35aSHannes Reinecke 	struct se_node_acl *nacl)
86e986a35aSHannes Reinecke {
87e986a35aSHannes Reinecke 	struct se_dev_entry *deve;
88e986a35aSHannes Reinecke 
89e986a35aSHannes Reinecke 	if (!nacl)
90e986a35aSHannes Reinecke 		return;
91e986a35aSHannes Reinecke 
92e986a35aSHannes Reinecke 	rcu_read_lock();
93e986a35aSHannes Reinecke 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
94e986a35aSHannes Reinecke 		core_scsi3_ua_allocate(deve, 0x29,
95e986a35aSHannes Reinecke 			ASCQ_29H_NEXUS_LOSS_OCCURRED);
96e986a35aSHannes Reinecke 	rcu_read_unlock();
97e986a35aSHannes Reinecke }
98e986a35aSHannes Reinecke EXPORT_SYMBOL(core_allocate_nexus_loss_ua);
99e986a35aSHannes Reinecke 
100c66ac9dbSNicholas Bellinger /*	core_tpg_add_node_to_devs():
101c66ac9dbSNicholas Bellinger  *
102c66ac9dbSNicholas Bellinger  *
103c66ac9dbSNicholas Bellinger  */
core_tpg_add_node_to_devs(struct se_node_acl * acl,struct se_portal_group * tpg,struct se_lun * lun_orig)104c66ac9dbSNicholas Bellinger void core_tpg_add_node_to_devs(
105c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl,
106df9766caSNicholas Bellinger 	struct se_portal_group *tpg,
107df9766caSNicholas Bellinger 	struct se_lun *lun_orig)
108c66ac9dbSNicholas Bellinger {
10903a68b44SAndy Grover 	bool lun_access_ro = true;
110c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
111c66ac9dbSNicholas Bellinger 	struct se_device *dev;
112c66ac9dbSNicholas Bellinger 
1136bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
1146bb82612SNicholas Bellinger 	hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) {
115df9766caSNicholas Bellinger 		if (lun_orig && lun != lun_orig)
116c66ac9dbSNicholas Bellinger 			continue;
117c66ac9dbSNicholas Bellinger 
1184cc987eaSNicholas Bellinger 		dev = rcu_dereference_check(lun->lun_se_dev,
1194cc987eaSNicholas Bellinger 					    lockdep_is_held(&tpg->tpg_lun_mutex));
120c66ac9dbSNicholas Bellinger 		/*
121c66ac9dbSNicholas Bellinger 		 * By default in LIO-Target $FABRIC_MOD,
122c66ac9dbSNicholas Bellinger 		 * demo_mode_write_protect is ON, or READ_ONLY;
123c66ac9dbSNicholas Bellinger 		 */
1246708bb27SAndy Grover 		if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
12503a68b44SAndy Grover 			lun_access_ro = false;
126c66ac9dbSNicholas Bellinger 		} else {
127c66ac9dbSNicholas Bellinger 			/*
128c66ac9dbSNicholas Bellinger 			 * Allow only optical drives to issue R/W in default RO
129c66ac9dbSNicholas Bellinger 			 * demo mode.
130c66ac9dbSNicholas Bellinger 			 */
131e3d6f909SAndy Grover 			if (dev->transport->get_device_type(dev) == TYPE_DISK)
13203a68b44SAndy Grover 				lun_access_ro = true;
133c66ac9dbSNicholas Bellinger 			else
13403a68b44SAndy Grover 				lun_access_ro = false;
135c66ac9dbSNicholas Bellinger 		}
136c66ac9dbSNicholas Bellinger 
137f2d30680SHannes Reinecke 		pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%llu] - Adding %s"
138c66ac9dbSNicholas Bellinger 			" access for LUN in Demo Mode\n",
13930c7ca93SDavid Disseldorp 			tpg->se_tpg_tfo->fabric_name,
140e3d6f909SAndy Grover 			tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
14103a68b44SAndy Grover 			lun_access_ro ? "READ-ONLY" : "READ-WRITE");
142c66ac9dbSNicholas Bellinger 
143e80ac6c4SAndy Grover 		core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
14403a68b44SAndy Grover 						 lun_access_ro, acl, tpg);
145e2480563SNicholas Bellinger 		/*
146e2480563SNicholas Bellinger 		 * Check to see if there are any existing persistent reservation
147e2480563SNicholas Bellinger 		 * APTPL pre-registrations that need to be enabled for this dynamic
148e2480563SNicholas Bellinger 		 * LUN ACL now..
149e2480563SNicholas Bellinger 		 */
150e2480563SNicholas Bellinger 		core_scsi3_check_aptpl_registration(dev, tpg, lun, acl,
151e2480563SNicholas Bellinger 						    lun->unpacked_lun);
152c66ac9dbSNicholas Bellinger 	}
1536bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
154c66ac9dbSNicholas Bellinger }
155c66ac9dbSNicholas Bellinger 
156d36ad77fSNicholas Bellinger static void
target_set_nacl_queue_depth(struct se_portal_group * tpg,struct se_node_acl * acl,u32 queue_depth)157d36ad77fSNicholas Bellinger target_set_nacl_queue_depth(struct se_portal_group *tpg,
158d36ad77fSNicholas Bellinger 			    struct se_node_acl *acl, u32 queue_depth)
159c66ac9dbSNicholas Bellinger {
160d36ad77fSNicholas Bellinger 	acl->queue_depth = queue_depth;
161d36ad77fSNicholas Bellinger 
162c66ac9dbSNicholas Bellinger 	if (!acl->queue_depth) {
163d36ad77fSNicholas Bellinger 		pr_warn("Queue depth for %s Initiator Node: %s is 0,"
16430c7ca93SDavid Disseldorp 			"defaulting to 1.\n", tpg->se_tpg_tfo->fabric_name,
165c66ac9dbSNicholas Bellinger 			acl->initiatorname);
166c66ac9dbSNicholas Bellinger 		acl->queue_depth = 1;
167c66ac9dbSNicholas Bellinger 	}
168c66ac9dbSNicholas Bellinger }
169c66ac9dbSNicholas Bellinger 
target_alloc_node_acl(struct se_portal_group * tpg,const unsigned char * initiatorname)170e413f472SChristoph Hellwig static struct se_node_acl *target_alloc_node_acl(struct se_portal_group *tpg,
171e413f472SChristoph Hellwig 		const unsigned char *initiatorname)
1724a5a75f3SJörn Engel {
173c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
174d36ad77fSNicholas Bellinger 	u32 queue_depth;
175c66ac9dbSNicholas Bellinger 
176144bc4c2SChristoph Hellwig 	acl = kzalloc(max(sizeof(*acl), tpg->se_tpg_tfo->node_acl_size),
177144bc4c2SChristoph Hellwig 			GFP_KERNEL);
1786708bb27SAndy Grover 	if (!acl)
179c66ac9dbSNicholas Bellinger 		return NULL;
180c66ac9dbSNicholas Bellinger 
181c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&acl->acl_list);
182c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&acl->acl_sess_list);
18329a05deeSNicholas Bellinger 	INIT_HLIST_HEAD(&acl->lun_entry_hlist);
184afb999ffSNicholas Bellinger 	kref_init(&acl->acl_kref);
18501468346SNicholas Bellinger 	init_completion(&acl->acl_free_comp);
186c66ac9dbSNicholas Bellinger 	spin_lock_init(&acl->nacl_sess_lock);
18729a05deeSNicholas Bellinger 	mutex_init(&acl->lun_entry_mutex);
188c66ac9dbSNicholas Bellinger 	atomic_set(&acl->acl_pr_ref_count, 0);
189d36ad77fSNicholas Bellinger 
190e1750d20SChristoph Hellwig 	if (tpg->se_tpg_tfo->tpg_get_default_depth)
191d36ad77fSNicholas Bellinger 		queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
192e1750d20SChristoph Hellwig 	else
193d36ad77fSNicholas Bellinger 		queue_depth = 1;
194d36ad77fSNicholas Bellinger 	target_set_nacl_queue_depth(tpg, acl, queue_depth);
195d36ad77fSNicholas Bellinger 
196c66ac9dbSNicholas Bellinger 	snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
197c66ac9dbSNicholas Bellinger 	acl->se_tpg = tpg;
198c66ac9dbSNicholas Bellinger 	acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
199c66ac9dbSNicholas Bellinger 
200e3d6f909SAndy Grover 	tpg->se_tpg_tfo->set_default_node_attributes(acl);
201c66ac9dbSNicholas Bellinger 
202e413f472SChristoph Hellwig 	return acl;
203c66ac9dbSNicholas Bellinger }
204c66ac9dbSNicholas Bellinger 
target_add_node_acl(struct se_node_acl * acl)205e413f472SChristoph Hellwig static void target_add_node_acl(struct se_node_acl *acl)
206c66ac9dbSNicholas Bellinger {
207e413f472SChristoph Hellwig 	struct se_portal_group *tpg = acl->se_tpg;
208c66ac9dbSNicholas Bellinger 
209403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
210e413f472SChristoph Hellwig 	list_add_tail(&acl->acl_list, &tpg->acl_node_list);
211403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
212c66ac9dbSNicholas Bellinger 
213e413f472SChristoph Hellwig 	pr_debug("%s_TPG[%hu] - Added %s ACL with TCQ Depth: %d for %s"
214e413f472SChristoph Hellwig 		" Initiator Node: %s\n",
21530c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
216e413f472SChristoph Hellwig 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
217e413f472SChristoph Hellwig 		acl->dynamic_node_acl ? "DYNAMIC" : "",
218e413f472SChristoph Hellwig 		acl->queue_depth,
21930c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
220e413f472SChristoph Hellwig 		acl->initiatorname);
221c66ac9dbSNicholas Bellinger }
222c66ac9dbSNicholas Bellinger 
target_tpg_has_node_acl(struct se_portal_group * tpg,const char * initiatorname)22321aaa23bSNicholas Bellinger bool target_tpg_has_node_acl(struct se_portal_group *tpg,
22421aaa23bSNicholas Bellinger 			     const char *initiatorname)
22521aaa23bSNicholas Bellinger {
22621aaa23bSNicholas Bellinger 	struct se_node_acl *acl;
22721aaa23bSNicholas Bellinger 	bool found = false;
22821aaa23bSNicholas Bellinger 
22921aaa23bSNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
23021aaa23bSNicholas Bellinger 	list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
23121aaa23bSNicholas Bellinger 		if (!strcmp(acl->initiatorname, initiatorname)) {
23221aaa23bSNicholas Bellinger 			found = true;
23321aaa23bSNicholas Bellinger 			break;
23421aaa23bSNicholas Bellinger 		}
23521aaa23bSNicholas Bellinger 	}
23621aaa23bSNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
23721aaa23bSNicholas Bellinger 
23821aaa23bSNicholas Bellinger 	return found;
23921aaa23bSNicholas Bellinger }
24021aaa23bSNicholas Bellinger EXPORT_SYMBOL(target_tpg_has_node_acl);
24121aaa23bSNicholas Bellinger 
core_tpg_check_initiator_node_acl(struct se_portal_group * tpg,unsigned char * initiatorname)242c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_check_initiator_node_acl(
243c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
244c66ac9dbSNicholas Bellinger 	unsigned char *initiatorname)
245c66ac9dbSNicholas Bellinger {
246c66ac9dbSNicholas Bellinger 	struct se_node_acl *acl;
247c66ac9dbSNicholas Bellinger 
248c66ac9dbSNicholas Bellinger 	acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
249c66ac9dbSNicholas Bellinger 	if (acl)
250c66ac9dbSNicholas Bellinger 		return acl;
251c66ac9dbSNicholas Bellinger 
252c66ac9dbSNicholas Bellinger 	if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
253c66ac9dbSNicholas Bellinger 		return NULL;
254c66ac9dbSNicholas Bellinger 
255e413f472SChristoph Hellwig 	acl = target_alloc_node_acl(tpg, initiatorname);
256c66ac9dbSNicholas Bellinger 	if (!acl)
257c66ac9dbSNicholas Bellinger 		return NULL;
25821aaa23bSNicholas Bellinger 	/*
25921aaa23bSNicholas Bellinger 	 * When allocating a dynamically generated node_acl, go ahead
26021aaa23bSNicholas Bellinger 	 * and take the extra kref now before returning to the fabric
26121aaa23bSNicholas Bellinger 	 * driver caller.
26221aaa23bSNicholas Bellinger 	 *
26321aaa23bSNicholas Bellinger 	 * Note this reference will be released at session shutdown
26421aaa23bSNicholas Bellinger 	 * time within transport_free_session() code.
26521aaa23bSNicholas Bellinger 	 */
26621aaa23bSNicholas Bellinger 	kref_get(&acl->acl_kref);
267c66ac9dbSNicholas Bellinger 	acl->dynamic_node_acl = 1;
268c66ac9dbSNicholas Bellinger 
269052605c6SNicholas Bellinger 	/*
270052605c6SNicholas Bellinger 	 * Here we only create demo-mode MappedLUNs from the active
27135d1efe8SMasanari Iida 	 * TPG LUNs if the fabric is not explicitly asking for
272052605c6SNicholas Bellinger 	 * tpg_check_demo_mode_login_only() == 1.
273052605c6SNicholas Bellinger 	 */
274cdf88a2fSAndy Grover 	if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) ||
275cdf88a2fSAndy Grover 	    (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1))
276df9766caSNicholas Bellinger 		core_tpg_add_node_to_devs(acl, tpg, NULL);
277c66ac9dbSNicholas Bellinger 
278e413f472SChristoph Hellwig 	target_add_node_acl(acl);
279c66ac9dbSNicholas Bellinger 	return acl;
280c66ac9dbSNicholas Bellinger }
281c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
282c66ac9dbSNicholas Bellinger 
core_tpg_wait_for_nacl_pr_ref(struct se_node_acl * nacl)283c66ac9dbSNicholas Bellinger void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
284c66ac9dbSNicholas Bellinger {
285c66ac9dbSNicholas Bellinger 	while (atomic_read(&nacl->acl_pr_ref_count) != 0)
286c66ac9dbSNicholas Bellinger 		cpu_relax();
287c66ac9dbSNicholas Bellinger }
288c66ac9dbSNicholas Bellinger 
core_tpg_add_initiator_node_acl(struct se_portal_group * tpg,const char * initiatorname)289c66ac9dbSNicholas Bellinger struct se_node_acl *core_tpg_add_initiator_node_acl(
290c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
291c7d6a803SChristoph Hellwig 	const char *initiatorname)
292c66ac9dbSNicholas Bellinger {
293c7d6a803SChristoph Hellwig 	struct se_node_acl *acl;
294c66ac9dbSNicholas Bellinger 
295403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
296c66ac9dbSNicholas Bellinger 	acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
2976708bb27SAndy Grover 	if (acl) {
298c66ac9dbSNicholas Bellinger 		if (acl->dynamic_node_acl) {
299c66ac9dbSNicholas Bellinger 			acl->dynamic_node_acl = 0;
3006708bb27SAndy Grover 			pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
30130c7ca93SDavid Disseldorp 				" for %s\n", tpg->se_tpg_tfo->fabric_name,
302e3d6f909SAndy Grover 				tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
303403edd78SNicholas Bellinger 			mutex_unlock(&tpg->acl_node_mutex);
304e413f472SChristoph Hellwig 			return acl;
305c66ac9dbSNicholas Bellinger 		}
306c66ac9dbSNicholas Bellinger 
3076708bb27SAndy Grover 		pr_err("ACL entry for %s Initiator"
308c66ac9dbSNicholas Bellinger 			" Node %s already exists for TPG %u, ignoring"
30930c7ca93SDavid Disseldorp 			" request.\n",  tpg->se_tpg_tfo->fabric_name,
310e3d6f909SAndy Grover 			initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
311403edd78SNicholas Bellinger 		mutex_unlock(&tpg->acl_node_mutex);
312c66ac9dbSNicholas Bellinger 		return ERR_PTR(-EEXIST);
313c66ac9dbSNicholas Bellinger 	}
314403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
315c66ac9dbSNicholas Bellinger 
316e413f472SChristoph Hellwig 	acl = target_alloc_node_acl(tpg, initiatorname);
317e413f472SChristoph Hellwig 	if (!acl)
318c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
319c66ac9dbSNicholas Bellinger 
320e413f472SChristoph Hellwig 	target_add_node_acl(acl);
321c66ac9dbSNicholas Bellinger 	return acl;
322c66ac9dbSNicholas Bellinger }
323c66ac9dbSNicholas Bellinger 
target_shutdown_sessions(struct se_node_acl * acl)324fba81f88SChristoph Hellwig static void target_shutdown_sessions(struct se_node_acl *acl)
325bc6e6bb4SChristoph Hellwig {
326bc6e6bb4SChristoph Hellwig 	struct se_session *sess;
327bc6e6bb4SChristoph Hellwig 	unsigned long flags;
328bc6e6bb4SChristoph Hellwig 
329bc6e6bb4SChristoph Hellwig restart:
330fba81f88SChristoph Hellwig 	spin_lock_irqsave(&acl->nacl_sess_lock, flags);
331bc6e6bb4SChristoph Hellwig 	list_for_each_entry(sess, &acl->acl_sess_list, sess_acl_list) {
332*becd9be6SMike Christie 		if (sess->cmd_cnt && atomic_read(&sess->cmd_cnt->stopped))
333bc6e6bb4SChristoph Hellwig 			continue;
334bc6e6bb4SChristoph Hellwig 
335fba81f88SChristoph Hellwig 		list_del_init(&sess->sess_acl_list);
336bc6e6bb4SChristoph Hellwig 		spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
337d94331faSChristoph Hellwig 
338d94331faSChristoph Hellwig 		if (acl->se_tpg->se_tpg_tfo->close_session)
339d94331faSChristoph Hellwig 			acl->se_tpg->se_tpg_tfo->close_session(sess);
340bc6e6bb4SChristoph Hellwig 		goto restart;
341bc6e6bb4SChristoph Hellwig 	}
342bc6e6bb4SChristoph Hellwig 	spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
343bc6e6bb4SChristoph Hellwig }
344bc6e6bb4SChristoph Hellwig 
core_tpg_del_initiator_node_acl(struct se_node_acl * acl)345c7d6a803SChristoph Hellwig void core_tpg_del_initiator_node_acl(struct se_node_acl *acl)
346c66ac9dbSNicholas Bellinger {
347c7d6a803SChristoph Hellwig 	struct se_portal_group *tpg = acl->se_tpg;
348c66ac9dbSNicholas Bellinger 
349403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
350bc6e6bb4SChristoph Hellwig 	if (acl->dynamic_node_acl)
351c66ac9dbSNicholas Bellinger 		acl->dynamic_node_acl = 0;
3526f48655fSNicholas Bellinger 	list_del_init(&acl->acl_list);
353403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
354c66ac9dbSNicholas Bellinger 
355fba81f88SChristoph Hellwig 	target_shutdown_sessions(acl);
356337c0607SNicholas Bellinger 
357337c0607SNicholas Bellinger 	target_put_nacl(acl);
358337c0607SNicholas Bellinger 	/*
359337c0607SNicholas Bellinger 	 * Wait for last target_put_nacl() to complete in target_complete_nacl()
360337c0607SNicholas Bellinger 	 * for active fabric session transport_deregister_session() callbacks.
361337c0607SNicholas Bellinger 	 */
362337c0607SNicholas Bellinger 	wait_for_completion(&acl->acl_free_comp);
363c66ac9dbSNicholas Bellinger 
364c66ac9dbSNicholas Bellinger 	core_tpg_wait_for_nacl_pr_ref(acl);
365c66ac9dbSNicholas Bellinger 	core_free_device_list_for_node(acl, tpg);
366c66ac9dbSNicholas Bellinger 
3676708bb27SAndy Grover 	pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
36830c7ca93SDavid Disseldorp 		" Initiator Node: %s\n", tpg->se_tpg_tfo->fabric_name,
369e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
37030c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name, acl->initiatorname);
371c66ac9dbSNicholas Bellinger 
372144bc4c2SChristoph Hellwig 	kfree(acl);
373c66ac9dbSNicholas Bellinger }
374c66ac9dbSNicholas Bellinger 
375c66ac9dbSNicholas Bellinger /*	core_tpg_set_initiator_node_queue_depth():
376c66ac9dbSNicholas Bellinger  *
377c66ac9dbSNicholas Bellinger  *
378c66ac9dbSNicholas Bellinger  */
core_tpg_set_initiator_node_queue_depth(struct se_node_acl * acl,u32 queue_depth)379c66ac9dbSNicholas Bellinger int core_tpg_set_initiator_node_queue_depth(
380d36ad77fSNicholas Bellinger 	struct se_node_acl *acl,
381d36ad77fSNicholas Bellinger 	u32 queue_depth)
382c66ac9dbSNicholas Bellinger {
383d36ad77fSNicholas Bellinger 	struct se_portal_group *tpg = acl->se_tpg;
384c66ac9dbSNicholas Bellinger 
385c66ac9dbSNicholas Bellinger 	/*
38646861cddSNicholas Bellinger 	 * Allow the setting of se_node_acl queue_depth to be idempotent,
38746861cddSNicholas Bellinger 	 * and not force a session shutdown event if the value is not
38846861cddSNicholas Bellinger 	 * changing.
38946861cddSNicholas Bellinger 	 */
39046861cddSNicholas Bellinger 	if (acl->queue_depth == queue_depth)
39146861cddSNicholas Bellinger 		return 0;
39246861cddSNicholas Bellinger 	/*
393c66ac9dbSNicholas Bellinger 	 * User has requested to change the queue depth for a Initiator Node.
394c66ac9dbSNicholas Bellinger 	 * Change the value in the Node's struct se_node_acl, and call
395d36ad77fSNicholas Bellinger 	 * target_set_nacl_queue_depth() to set the new queue depth.
396d36ad77fSNicholas Bellinger 	 */
397d36ad77fSNicholas Bellinger 	target_set_nacl_queue_depth(tpg, acl, queue_depth);
398d36ad77fSNicholas Bellinger 
399d36ad77fSNicholas Bellinger 	/*
400bc6e6bb4SChristoph Hellwig 	 * Shutdown all pending sessions to force session reinstatement.
401c66ac9dbSNicholas Bellinger 	 */
402fba81f88SChristoph Hellwig 	target_shutdown_sessions(acl);
403c66ac9dbSNicholas Bellinger 
404bfb9035cSJoe Perches 	pr_debug("Successfully changed queue depth to: %d for Initiator"
405d36ad77fSNicholas Bellinger 		" Node: %s on %s Target Portal Group: %u\n", acl->queue_depth,
40630c7ca93SDavid Disseldorp 		acl->initiatorname, tpg->se_tpg_tfo->fabric_name,
407e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg));
408c66ac9dbSNicholas Bellinger 
409c66ac9dbSNicholas Bellinger 	return 0;
410c66ac9dbSNicholas Bellinger }
411c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
412c66ac9dbSNicholas Bellinger 
41379e62fc3SAndy Grover /*	core_tpg_set_initiator_node_tag():
41479e62fc3SAndy Grover  *
41579e62fc3SAndy Grover  *	Initiator nodeacl tags are not used internally, but may be used by
41679e62fc3SAndy Grover  *	userspace to emulate aliases or groups.
41779e62fc3SAndy Grover  *	Returns length of newly-set tag or -EINVAL.
41879e62fc3SAndy Grover  */
core_tpg_set_initiator_node_tag(struct se_portal_group * tpg,struct se_node_acl * acl,const char * new_tag)41979e62fc3SAndy Grover int core_tpg_set_initiator_node_tag(
42079e62fc3SAndy Grover 	struct se_portal_group *tpg,
42179e62fc3SAndy Grover 	struct se_node_acl *acl,
42279e62fc3SAndy Grover 	const char *new_tag)
42379e62fc3SAndy Grover {
42479e62fc3SAndy Grover 	if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
42579e62fc3SAndy Grover 		return -EINVAL;
42679e62fc3SAndy Grover 
42779e62fc3SAndy Grover 	if (!strncmp("NULL", new_tag, 4)) {
42879e62fc3SAndy Grover 		acl->acl_tag[0] = '\0';
42979e62fc3SAndy Grover 		return 0;
43079e62fc3SAndy Grover 	}
43179e62fc3SAndy Grover 
43279e62fc3SAndy Grover 	return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
43379e62fc3SAndy Grover }
43479e62fc3SAndy Grover EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
43579e62fc3SAndy Grover 
core_tpg_lun_ref_release(struct percpu_ref * ref)4365277797dSNicholas Bellinger static void core_tpg_lun_ref_release(struct percpu_ref *ref)
4375277797dSNicholas Bellinger {
4385277797dSNicholas Bellinger 	struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
4395277797dSNicholas Bellinger 
440bd4e2d29SNicholas Bellinger 	complete(&lun->lun_shutdown_comp);
4415277797dSNicholas Bellinger }
4425277797dSNicholas Bellinger 
target_tpg_register_rtpi(struct se_portal_group * se_tpg)4433f4b9cb4SDmitry Bogdanov static int target_tpg_register_rtpi(struct se_portal_group *se_tpg)
4443f4b9cb4SDmitry Bogdanov {
4453f4b9cb4SDmitry Bogdanov 	u32 val;
4463f4b9cb4SDmitry Bogdanov 	int ret;
4473f4b9cb4SDmitry Bogdanov 
44831177b74SDmitry Bogdanov 	if (se_tpg->rtpi_manual) {
44931177b74SDmitry Bogdanov 		ret = xa_insert(&tpg_xa, se_tpg->tpg_rtpi, se_tpg, GFP_KERNEL);
45031177b74SDmitry Bogdanov 		if (ret) {
45131177b74SDmitry Bogdanov 			pr_info("%s_TPG[%hu] - Can not set RTPI %#x, it is already busy",
45231177b74SDmitry Bogdanov 				se_tpg->se_tpg_tfo->fabric_name,
45331177b74SDmitry Bogdanov 				se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg),
45431177b74SDmitry Bogdanov 				se_tpg->tpg_rtpi);
45531177b74SDmitry Bogdanov 			return -EINVAL;
45631177b74SDmitry Bogdanov 		}
45731177b74SDmitry Bogdanov 	} else {
4583f4b9cb4SDmitry Bogdanov 		ret = xa_alloc(&tpg_xa, &val, se_tpg,
4593f4b9cb4SDmitry Bogdanov 			       XA_LIMIT(1, USHRT_MAX), GFP_KERNEL);
4603f4b9cb4SDmitry Bogdanov 		if (!ret)
4613f4b9cb4SDmitry Bogdanov 			se_tpg->tpg_rtpi = val;
46231177b74SDmitry Bogdanov 	}
4633f4b9cb4SDmitry Bogdanov 
4643f4b9cb4SDmitry Bogdanov 	return ret;
4653f4b9cb4SDmitry Bogdanov }
4663f4b9cb4SDmitry Bogdanov 
target_tpg_deregister_rtpi(struct se_portal_group * se_tpg)4673f4b9cb4SDmitry Bogdanov static void target_tpg_deregister_rtpi(struct se_portal_group *se_tpg)
4683f4b9cb4SDmitry Bogdanov {
4693f4b9cb4SDmitry Bogdanov 	if (se_tpg->tpg_rtpi && se_tpg->enabled)
4703f4b9cb4SDmitry Bogdanov 		xa_erase(&tpg_xa, se_tpg->tpg_rtpi);
4713f4b9cb4SDmitry Bogdanov }
4723f4b9cb4SDmitry Bogdanov 
target_tpg_enable(struct se_portal_group * se_tpg)4733f4b9cb4SDmitry Bogdanov int target_tpg_enable(struct se_portal_group *se_tpg)
4743f4b9cb4SDmitry Bogdanov {
4753f4b9cb4SDmitry Bogdanov 	int ret;
4763f4b9cb4SDmitry Bogdanov 
4773f4b9cb4SDmitry Bogdanov 	ret = target_tpg_register_rtpi(se_tpg);
4783f4b9cb4SDmitry Bogdanov 	if (ret)
4793f4b9cb4SDmitry Bogdanov 		return ret;
4803f4b9cb4SDmitry Bogdanov 
4813f4b9cb4SDmitry Bogdanov 	ret = se_tpg->se_tpg_tfo->fabric_enable_tpg(se_tpg, true);
4823f4b9cb4SDmitry Bogdanov 	if (ret) {
4833f4b9cb4SDmitry Bogdanov 		target_tpg_deregister_rtpi(se_tpg);
4843f4b9cb4SDmitry Bogdanov 		return ret;
4853f4b9cb4SDmitry Bogdanov 	}
4863f4b9cb4SDmitry Bogdanov 
4873f4b9cb4SDmitry Bogdanov 	se_tpg->enabled = true;
4883f4b9cb4SDmitry Bogdanov 
4893f4b9cb4SDmitry Bogdanov 	return 0;
4903f4b9cb4SDmitry Bogdanov }
4913f4b9cb4SDmitry Bogdanov 
target_tpg_disable(struct se_portal_group * se_tpg)4923f4b9cb4SDmitry Bogdanov int target_tpg_disable(struct se_portal_group *se_tpg)
4933f4b9cb4SDmitry Bogdanov {
4943f4b9cb4SDmitry Bogdanov 	int ret;
4953f4b9cb4SDmitry Bogdanov 
4963f4b9cb4SDmitry Bogdanov 	target_tpg_deregister_rtpi(se_tpg);
4973f4b9cb4SDmitry Bogdanov 
4983f4b9cb4SDmitry Bogdanov 	ret = se_tpg->se_tpg_tfo->fabric_enable_tpg(se_tpg, false);
4993f4b9cb4SDmitry Bogdanov 	if (!ret)
5003f4b9cb4SDmitry Bogdanov 		se_tpg->enabled = false;
5013f4b9cb4SDmitry Bogdanov 
5023f4b9cb4SDmitry Bogdanov 	return ret;
5033f4b9cb4SDmitry Bogdanov }
5043f4b9cb4SDmitry Bogdanov 
5052bce1a6dSBart Van Assche /* Does not change se_wwn->priv. */
core_tpg_register(struct se_wwn * se_wwn,struct se_portal_group * se_tpg,int proto_id)506c66ac9dbSNicholas Bellinger int core_tpg_register(
507c66ac9dbSNicholas Bellinger 	struct se_wwn *se_wwn,
508c66ac9dbSNicholas Bellinger 	struct se_portal_group *se_tpg,
509e4aae5afSChristoph Hellwig 	int proto_id)
510c66ac9dbSNicholas Bellinger {
511adf653f9SChristoph Hellwig 	int ret;
512c66ac9dbSNicholas Bellinger 
513bc0c94b1SNicholas Bellinger 	if (!se_tpg)
514bc0c94b1SNicholas Bellinger 		return -EINVAL;
515bc0c94b1SNicholas Bellinger 	/*
516bc0c94b1SNicholas Bellinger 	 * For the typical case where core_tpg_register() is called by a
517bc0c94b1SNicholas Bellinger 	 * fabric driver from target_core_fabric_ops->fabric_make_tpg()
518bc0c94b1SNicholas Bellinger 	 * configfs context, use the original tf_ops pointer already saved
519bc0c94b1SNicholas Bellinger 	 * by target-core in target_fabric_make_wwn().
520bc0c94b1SNicholas Bellinger 	 *
521bc0c94b1SNicholas Bellinger 	 * Otherwise, for special cases like iscsi-target discovery TPGs
522bc0c94b1SNicholas Bellinger 	 * the caller is responsible for setting ->se_tpg_tfo ahead of
523bc0c94b1SNicholas Bellinger 	 * calling core_tpg_register().
524bc0c94b1SNicholas Bellinger 	 */
525bc0c94b1SNicholas Bellinger 	if (se_wwn)
526bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo = se_wwn->wwn_tf->tf_ops;
527bc0c94b1SNicholas Bellinger 
528bc0c94b1SNicholas Bellinger 	if (!se_tpg->se_tpg_tfo) {
529bc0c94b1SNicholas Bellinger 		pr_err("Unable to locate se_tpg->se_tpg_tfo pointer\n");
530bc0c94b1SNicholas Bellinger 		return -EINVAL;
531c66ac9dbSNicholas Bellinger 	}
532c66ac9dbSNicholas Bellinger 
5336bb82612SNicholas Bellinger 	INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
534e4aae5afSChristoph Hellwig 	se_tpg->proto_id = proto_id;
535c66ac9dbSNicholas Bellinger 	se_tpg->se_tpg_wwn = se_wwn;
536c66ac9dbSNicholas Bellinger 	atomic_set(&se_tpg->tpg_pr_ref_count, 0);
537c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->acl_node_list);
538c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
539c66ac9dbSNicholas Bellinger 	spin_lock_init(&se_tpg->session_lock);
5406bb82612SNicholas Bellinger 	mutex_init(&se_tpg->tpg_lun_mutex);
541403edd78SNicholas Bellinger 	mutex_init(&se_tpg->acl_node_mutex);
542c66ac9dbSNicholas Bellinger 
543e4aae5afSChristoph Hellwig 	if (se_tpg->proto_id >= 0) {
544adf653f9SChristoph Hellwig 		se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
545adf653f9SChristoph Hellwig 		if (IS_ERR(se_tpg->tpg_virt_lun0))
546adf653f9SChristoph Hellwig 			return PTR_ERR(se_tpg->tpg_virt_lun0);
547adf653f9SChristoph Hellwig 
548adf653f9SChristoph Hellwig 		ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
54903a68b44SAndy Grover 				true, g_lun0_dev);
550adf653f9SChristoph Hellwig 		if (ret < 0) {
551adf653f9SChristoph Hellwig 			kfree(se_tpg->tpg_virt_lun0);
552adf653f9SChristoph Hellwig 			return ret;
553c66ac9dbSNicholas Bellinger 		}
554c66ac9dbSNicholas Bellinger 	}
555c66ac9dbSNicholas Bellinger 
556e4aae5afSChristoph Hellwig 	pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
55730c7ca93SDavid Disseldorp 		 "Proto: %d, Portal Tag: %u\n", se_tpg->se_tpg_tfo->fabric_name,
558bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) ?
559bc0c94b1SNicholas Bellinger 		se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg) : NULL,
560bc0c94b1SNicholas Bellinger 		se_tpg->proto_id, se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
561c66ac9dbSNicholas Bellinger 
562c66ac9dbSNicholas Bellinger 	return 0;
563c66ac9dbSNicholas Bellinger }
564c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_register);
565c66ac9dbSNicholas Bellinger 
core_tpg_deregister(struct se_portal_group * se_tpg)566c66ac9dbSNicholas Bellinger int core_tpg_deregister(struct se_portal_group *se_tpg)
567c66ac9dbSNicholas Bellinger {
568e4aae5afSChristoph Hellwig 	const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
569e89d15eeSNicholas Bellinger 	struct se_node_acl *nacl, *nacl_tmp;
57022793de5SNicholas Bellinger 	LIST_HEAD(node_list);
571e89d15eeSNicholas Bellinger 
572e4aae5afSChristoph Hellwig 	pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
57330c7ca93SDavid Disseldorp 		 "Proto: %d, Portal Tag: %u\n", tfo->fabric_name,
574e4aae5afSChristoph Hellwig 		tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
575e4aae5afSChristoph Hellwig 		se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
576c66ac9dbSNicholas Bellinger 
577c66ac9dbSNicholas Bellinger 	while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
578c66ac9dbSNicholas Bellinger 		cpu_relax();
579403edd78SNicholas Bellinger 
58022793de5SNicholas Bellinger 	mutex_lock(&se_tpg->acl_node_mutex);
58122793de5SNicholas Bellinger 	list_splice_init(&se_tpg->acl_node_list, &node_list);
58222793de5SNicholas Bellinger 	mutex_unlock(&se_tpg->acl_node_mutex);
583e89d15eeSNicholas Bellinger 	/*
584e89d15eeSNicholas Bellinger 	 * Release any remaining demo-mode generated se_node_acl that have
585e89d15eeSNicholas Bellinger 	 * not been released because of TFO->tpg_check_demo_mode_cache() == 1
586e89d15eeSNicholas Bellinger 	 * in transport_deregister_session().
587e89d15eeSNicholas Bellinger 	 */
58822793de5SNicholas Bellinger 	list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) {
5896f48655fSNicholas Bellinger 		list_del_init(&nacl->acl_list);
590e89d15eeSNicholas Bellinger 
591e89d15eeSNicholas Bellinger 		core_tpg_wait_for_nacl_pr_ref(nacl);
592e89d15eeSNicholas Bellinger 		core_free_device_list_for_node(nacl, se_tpg);
593144bc4c2SChristoph Hellwig 		kfree(nacl);
594e89d15eeSNicholas Bellinger 	}
595c66ac9dbSNicholas Bellinger 
596adf653f9SChristoph Hellwig 	if (se_tpg->proto_id >= 0) {
597adf653f9SChristoph Hellwig 		core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
598adf653f9SChristoph Hellwig 		kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
599adf653f9SChristoph Hellwig 	}
600c66ac9dbSNicholas Bellinger 
6013f4b9cb4SDmitry Bogdanov 	target_tpg_deregister_rtpi(se_tpg);
6023f4b9cb4SDmitry Bogdanov 
603c66ac9dbSNicholas Bellinger 	return 0;
604c66ac9dbSNicholas Bellinger }
605c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tpg_deregister);
606c66ac9dbSNicholas Bellinger 
core_tpg_alloc_lun(struct se_portal_group * tpg,u64 unpacked_lun)607d344f8a1SAndy Grover struct se_lun *core_tpg_alloc_lun(
608c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
609f2d30680SHannes Reinecke 	u64 unpacked_lun)
610c66ac9dbSNicholas Bellinger {
611c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
612c66ac9dbSNicholas Bellinger 
6136bb82612SNicholas Bellinger 	lun = kzalloc(sizeof(*lun), GFP_KERNEL);
6146bb82612SNicholas Bellinger 	if (!lun) {
6156bb82612SNicholas Bellinger 		pr_err("Unable to allocate se_lun memory\n");
6166bb82612SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
617c66ac9dbSNicholas Bellinger 	}
6186bb82612SNicholas Bellinger 	lun->unpacked_lun = unpacked_lun;
6196bb82612SNicholas Bellinger 	atomic_set(&lun->lun_acl_count, 0);
620bd4e2d29SNicholas Bellinger 	init_completion(&lun->lun_shutdown_comp);
621adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_deve_list);
622adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_dev_link);
623adf653f9SChristoph Hellwig 	atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
624adf653f9SChristoph Hellwig 	spin_lock_init(&lun->lun_deve_lock);
625adf653f9SChristoph Hellwig 	mutex_init(&lun->lun_tg_pt_md_mutex);
626adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
627adf653f9SChristoph Hellwig 	spin_lock_init(&lun->lun_tg_pt_gp_lock);
628adf653f9SChristoph Hellwig 	lun->lun_tpg = tpg;
629c66ac9dbSNicholas Bellinger 
630c66ac9dbSNicholas Bellinger 	return lun;
631c66ac9dbSNicholas Bellinger }
632c66ac9dbSNicholas Bellinger 
core_tpg_add_lun(struct se_portal_group * tpg,struct se_lun * lun,bool lun_access_ro,struct se_device * dev)633d344f8a1SAndy Grover int core_tpg_add_lun(
634c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
635c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
63603a68b44SAndy Grover 	bool lun_access_ro,
637340dbf72SAndy Grover 	struct se_device *dev)
638c66ac9dbSNicholas Bellinger {
639e3d6f909SAndy Grover 	int ret;
640e3d6f909SAndy Grover 
6412aad2a86STejun Heo 	ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
642a34375efSTejun Heo 			      GFP_KERNEL);
643e3d6f909SAndy Grover 	if (ret < 0)
644adf653f9SChristoph Hellwig 		goto out;
645c66ac9dbSNicholas Bellinger 
64669088a04SBodo Stroesser 	if (!(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
647adf653f9SChristoph Hellwig 	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
648adf653f9SChristoph Hellwig 		target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
6495277797dSNicholas Bellinger 
6506bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
651adf653f9SChristoph Hellwig 
652adf653f9SChristoph Hellwig 	spin_lock(&dev->se_port_lock);
6534cc987eaSNicholas Bellinger 	lun->lun_index = dev->dev_index;
654adf653f9SChristoph Hellwig 	rcu_assign_pointer(lun->lun_se_dev, dev);
655adf653f9SChristoph Hellwig 	dev->export_count++;
656adf653f9SChristoph Hellwig 	list_add_tail(&lun->lun_dev_link, &dev->dev_sep_list);
657adf653f9SChristoph Hellwig 	spin_unlock(&dev->se_port_lock);
658adf653f9SChristoph Hellwig 
659eeeb9522SNicholas Bellinger 	if (dev->dev_flags & DF_READ_ONLY)
66003a68b44SAndy Grover 		lun->lun_access_ro = true;
661eeeb9522SNicholas Bellinger 	else
66203a68b44SAndy Grover 		lun->lun_access_ro = lun_access_ro;
6636bb82612SNicholas Bellinger 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
6646bb82612SNicholas Bellinger 		hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
6656bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
666c66ac9dbSNicholas Bellinger 
667c66ac9dbSNicholas Bellinger 	return 0;
668adf653f9SChristoph Hellwig 
669adf653f9SChristoph Hellwig out:
670adf653f9SChristoph Hellwig 	return ret;
671c66ac9dbSNicholas Bellinger }
672c66ac9dbSNicholas Bellinger 
core_tpg_remove_lun(struct se_portal_group * tpg,struct se_lun * lun)673cd9d7cbaSAndy Grover void core_tpg_remove_lun(
674c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
675c66ac9dbSNicholas Bellinger 	struct se_lun *lun)
676c66ac9dbSNicholas Bellinger {
6774cc987eaSNicholas Bellinger 	/*
6784cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
6794cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
6804cc987eaSNicholas Bellinger 	 */
6814cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
6826bb82612SNicholas Bellinger 
68349cb77e2SNicholas Bellinger 	lun->lun_shutdown = true;
68449cb77e2SNicholas Bellinger 
6854a9a6c8dSNicholas Bellinger 	core_clear_lun_from_tpg(lun, tpg);
6869e37d042SNicholas Bellinger 	/*
6879e37d042SNicholas Bellinger 	 * Wait for any active I/O references to percpu se_lun->lun_ref to
6889e37d042SNicholas Bellinger 	 * be released.  Also, se_lun->lun_ref is now used by PR and ALUA
6899e37d042SNicholas Bellinger 	 * logic when referencing a remote target port during ALL_TGT_PT=1
6909e37d042SNicholas Bellinger 	 * and generating UNIT_ATTENTIONs for ALUA access state transition.
6919e37d042SNicholas Bellinger 	 */
6924a9a6c8dSNicholas Bellinger 	transport_clear_lun_ref(lun);
693c66ac9dbSNicholas Bellinger 
6946bb82612SNicholas Bellinger 	mutex_lock(&tpg->tpg_lun_mutex);
695adf653f9SChristoph Hellwig 	if (lun->lun_se_dev) {
696adf653f9SChristoph Hellwig 		target_detach_tg_pt_gp(lun);
697c66ac9dbSNicholas Bellinger 
698adf653f9SChristoph Hellwig 		spin_lock(&dev->se_port_lock);
699adf653f9SChristoph Hellwig 		list_del(&lun->lun_dev_link);
700adf653f9SChristoph Hellwig 		dev->export_count--;
701adf653f9SChristoph Hellwig 		rcu_assign_pointer(lun->lun_se_dev, NULL);
702adf653f9SChristoph Hellwig 		spin_unlock(&dev->se_port_lock);
703adf653f9SChristoph Hellwig 	}
7046bb82612SNicholas Bellinger 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
7056bb82612SNicholas Bellinger 		hlist_del_rcu(&lun->link);
70649cb77e2SNicholas Bellinger 
70749cb77e2SNicholas Bellinger 	lun->lun_shutdown = false;
7086bb82612SNicholas Bellinger 	mutex_unlock(&tpg->tpg_lun_mutex);
709c66ac9dbSNicholas Bellinger 
7109a1049daSTejun Heo 	percpu_ref_exit(&lun->lun_ref);
711c66ac9dbSNicholas Bellinger }
712