target_core_tpg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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.h>
  35. #include <scsi/scsi_cmnd.h>
  36. #include <target/target_core_base.h>
  37. #include <target/target_core_backend.h>
  38. #include <target/target_core_fabric.h>
  39. #include "target_core_internal.h"
  40. #include "target_core_alua.h"
  41. #include "target_core_pr.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. mutex_lock(&tpg->acl_node_mutex);
  70. acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
  71. mutex_unlock(&tpg->acl_node_mutex);
  72. return acl;
  73. }
  74. EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
  75. /* core_tpg_add_node_to_devs():
  76. *
  77. *
  78. */
  79. void core_tpg_add_node_to_devs(
  80. struct se_node_acl *acl,
  81. struct se_portal_group *tpg,
  82. struct se_lun *lun_orig)
  83. {
  84. u32 lun_access = 0;
  85. struct se_lun *lun;
  86. struct se_device *dev;
  87. mutex_lock(&tpg->tpg_lun_mutex);
  88. hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link) {
  89. if (lun_orig && lun != lun_orig)
  90. continue;
  91. dev = lun->lun_se_dev;
  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[%u] - 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. const struct target_core_fabric_ops *tfo,
  422. struct se_wwn *se_wwn,
  423. struct se_portal_group *se_tpg,
  424. int proto_id)
  425. {
  426. int ret;
  427. INIT_HLIST_HEAD(&se_tpg->tpg_lun_hlist);
  428. se_tpg->proto_id = proto_id;
  429. se_tpg->se_tpg_tfo = tfo;
  430. se_tpg->se_tpg_wwn = se_wwn;
  431. atomic_set(&se_tpg->tpg_pr_ref_count, 0);
  432. INIT_LIST_HEAD(&se_tpg->acl_node_list);
  433. INIT_LIST_HEAD(&se_tpg->se_tpg_node);
  434. INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
  435. spin_lock_init(&se_tpg->session_lock);
  436. mutex_init(&se_tpg->tpg_lun_mutex);
  437. mutex_init(&se_tpg->acl_node_mutex);
  438. if (se_tpg->proto_id >= 0) {
  439. se_tpg->tpg_virt_lun0 = core_tpg_alloc_lun(se_tpg, 0);
  440. if (IS_ERR(se_tpg->tpg_virt_lun0))
  441. return PTR_ERR(se_tpg->tpg_virt_lun0);
  442. ret = core_tpg_add_lun(se_tpg, se_tpg->tpg_virt_lun0,
  443. TRANSPORT_LUNFLAGS_READ_ONLY, g_lun0_dev);
  444. if (ret < 0) {
  445. kfree(se_tpg->tpg_virt_lun0);
  446. return ret;
  447. }
  448. }
  449. spin_lock_bh(&tpg_lock);
  450. list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
  451. spin_unlock_bh(&tpg_lock);
  452. pr_debug("TARGET_CORE[%s]: Allocated portal_group for endpoint: %s, "
  453. "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(),
  454. tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
  455. se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
  456. return 0;
  457. }
  458. EXPORT_SYMBOL(core_tpg_register);
  459. int core_tpg_deregister(struct se_portal_group *se_tpg)
  460. {
  461. const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
  462. struct se_node_acl *nacl, *nacl_tmp;
  463. LIST_HEAD(node_list);
  464. pr_debug("TARGET_CORE[%s]: Deallocating portal_group for endpoint: %s, "
  465. "Proto: %d, Portal Tag: %u\n", tfo->get_fabric_name(),
  466. tfo->tpg_get_wwn(se_tpg) ? tfo->tpg_get_wwn(se_tpg) : NULL,
  467. se_tpg->proto_id, tfo->tpg_get_tag(se_tpg));
  468. spin_lock_bh(&tpg_lock);
  469. list_del(&se_tpg->se_tpg_node);
  470. spin_unlock_bh(&tpg_lock);
  471. while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
  472. cpu_relax();
  473. mutex_lock(&se_tpg->acl_node_mutex);
  474. list_splice_init(&se_tpg->acl_node_list, &node_list);
  475. mutex_unlock(&se_tpg->acl_node_mutex);
  476. /*
  477. * Release any remaining demo-mode generated se_node_acl that have
  478. * not been released because of TFO->tpg_check_demo_mode_cache() == 1
  479. * in transport_deregister_session().
  480. */
  481. list_for_each_entry_safe(nacl, nacl_tmp, &node_list, acl_list) {
  482. list_del(&nacl->acl_list);
  483. se_tpg->num_node_acls--;
  484. core_tpg_wait_for_nacl_pr_ref(nacl);
  485. core_free_device_list_for_node(nacl, se_tpg);
  486. kfree(nacl);
  487. }
  488. if (se_tpg->proto_id >= 0) {
  489. core_tpg_remove_lun(se_tpg, se_tpg->tpg_virt_lun0);
  490. kfree_rcu(se_tpg->tpg_virt_lun0, rcu_head);
  491. }
  492. return 0;
  493. }
  494. EXPORT_SYMBOL(core_tpg_deregister);
  495. struct se_lun *core_tpg_alloc_lun(
  496. struct se_portal_group *tpg,
  497. u32 unpacked_lun)
  498. {
  499. struct se_lun *lun;
  500. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  501. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
  502. "-1: %u for Target Portal Group: %u\n",
  503. tpg->se_tpg_tfo->get_fabric_name(),
  504. unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
  505. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  506. return ERR_PTR(-EOVERFLOW);
  507. }
  508. lun = kzalloc(sizeof(*lun), GFP_KERNEL);
  509. if (!lun) {
  510. pr_err("Unable to allocate se_lun memory\n");
  511. return ERR_PTR(-ENOMEM);
  512. }
  513. lun->unpacked_lun = unpacked_lun;
  514. lun->lun_link_magic = SE_LUN_LINK_MAGIC;
  515. atomic_set(&lun->lun_acl_count, 0);
  516. spin_lock_init(&lun->lun_sep_lock);
  517. init_completion(&lun->lun_ref_comp);
  518. INIT_LIST_HEAD(&lun->lun_deve_list);
  519. INIT_LIST_HEAD(&lun->lun_dev_link);
  520. atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
  521. spin_lock_init(&lun->lun_deve_lock);
  522. mutex_init(&lun->lun_tg_pt_md_mutex);
  523. INIT_LIST_HEAD(&lun->lun_tg_pt_gp_link);
  524. spin_lock_init(&lun->lun_tg_pt_gp_lock);
  525. atomic_set(&lun->lun_active, 0);
  526. lun->lun_tpg = tpg;
  527. return lun;
  528. }
  529. int core_tpg_add_lun(
  530. struct se_portal_group *tpg,
  531. struct se_lun *lun,
  532. u32 lun_access,
  533. struct se_device *dev)
  534. {
  535. int ret;
  536. ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
  537. GFP_KERNEL);
  538. if (ret < 0)
  539. goto out;
  540. ret = core_alloc_rtpi(lun, dev);
  541. if (ret)
  542. goto out_kill_ref;
  543. if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
  544. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
  545. target_attach_tg_pt_gp(lun, dev->t10_alua.default_tg_pt_gp);
  546. mutex_lock(&tpg->tpg_lun_mutex);
  547. spin_lock(&lun->lun_sep_lock);
  548. lun->lun_index = dev->dev_index;
  549. lun->lun_se_dev = dev;
  550. spin_unlock(&lun->lun_sep_lock);
  551. spin_lock(&dev->se_port_lock);
  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. lun->lun_access = lun_access;
  557. if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
  558. hlist_add_head_rcu(&lun->link, &tpg->tpg_lun_hlist);
  559. mutex_unlock(&tpg->tpg_lun_mutex);
  560. return 0;
  561. out_kill_ref:
  562. percpu_ref_exit(&lun->lun_ref);
  563. out:
  564. return ret;
  565. }
  566. void core_tpg_remove_lun(
  567. struct se_portal_group *tpg,
  568. struct se_lun *lun)
  569. {
  570. struct se_device *dev = lun->lun_se_dev;
  571. core_clear_lun_from_tpg(lun, tpg);
  572. transport_clear_lun_ref(lun);
  573. mutex_lock(&tpg->tpg_lun_mutex);
  574. if (lun->lun_se_dev) {
  575. while (atomic_read(&lun->lun_active))
  576. cpu_relax();
  577. target_detach_tg_pt_gp(lun);
  578. spin_lock(&dev->se_port_lock);
  579. list_del(&lun->lun_dev_link);
  580. dev->export_count--;
  581. rcu_assign_pointer(lun->lun_se_dev, NULL);
  582. spin_unlock(&dev->se_port_lock);
  583. }
  584. if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
  585. hlist_del_rcu(&lun->link);
  586. mutex_unlock(&tpg->tpg_lun_mutex);
  587. percpu_ref_exit(&lun->lun_ref);
  588. }