target_core_tpg.c 17 KB

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