target_core_tpg.c 19 KB

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