target_core_tpg.c 19 KB

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