target_core_tpg.c 19 KB

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