target_core_tpg.c 18 KB

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