target_core_tpg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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_pr.h"
  41. extern struct se_device *g_lun0_dev;
  42. static DEFINE_SPINLOCK(tpg_lock);
  43. static LIST_HEAD(tpg_list);
  44. /* core_clear_initiator_node_from_tpg():
  45. *
  46. *
  47. */
  48. static void core_clear_initiator_node_from_tpg(
  49. struct se_node_acl *nacl,
  50. struct se_portal_group *tpg)
  51. {
  52. int i;
  53. struct se_dev_entry *deve;
  54. struct se_lun *lun;
  55. spin_lock_irq(&nacl->device_list_lock);
  56. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  57. deve = nacl->device_list[i];
  58. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  59. continue;
  60. if (!deve->se_lun) {
  61. pr_err("%s device entries device pointer is"
  62. " NULL, but Initiator has access.\n",
  63. tpg->se_tpg_tfo->get_fabric_name());
  64. continue;
  65. }
  66. lun = deve->se_lun;
  67. spin_unlock_irq(&nacl->device_list_lock);
  68. core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
  69. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  70. spin_lock_irq(&nacl->device_list_lock);
  71. }
  72. spin_unlock_irq(&nacl->device_list_lock);
  73. }
  74. /* __core_tpg_get_initiator_node_acl():
  75. *
  76. * spin_lock_bh(&tpg->acl_node_lock); must be held when calling
  77. */
  78. struct se_node_acl *__core_tpg_get_initiator_node_acl(
  79. struct se_portal_group *tpg,
  80. const char *initiatorname)
  81. {
  82. struct se_node_acl *acl;
  83. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  84. if (!strcmp(acl->initiatorname, initiatorname))
  85. return acl;
  86. }
  87. return NULL;
  88. }
  89. /* core_tpg_get_initiator_node_acl():
  90. *
  91. *
  92. */
  93. struct se_node_acl *core_tpg_get_initiator_node_acl(
  94. struct se_portal_group *tpg,
  95. unsigned char *initiatorname)
  96. {
  97. struct se_node_acl *acl;
  98. spin_lock_irq(&tpg->acl_node_lock);
  99. acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
  100. spin_unlock_irq(&tpg->acl_node_lock);
  101. return acl;
  102. }
  103. EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
  104. /* core_tpg_add_node_to_devs():
  105. *
  106. *
  107. */
  108. void core_tpg_add_node_to_devs(
  109. struct se_node_acl *acl,
  110. struct se_portal_group *tpg)
  111. {
  112. int i = 0;
  113. u32 lun_access = 0;
  114. struct se_lun *lun;
  115. struct se_device *dev;
  116. spin_lock(&tpg->tpg_lun_lock);
  117. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  118. lun = tpg->tpg_lun_list[i];
  119. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
  120. continue;
  121. spin_unlock(&tpg->tpg_lun_lock);
  122. dev = lun->lun_se_dev;
  123. /*
  124. * By default in LIO-Target $FABRIC_MOD,
  125. * demo_mode_write_protect is ON, or READ_ONLY;
  126. */
  127. if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
  128. lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
  129. } else {
  130. /*
  131. * Allow only optical drives to issue R/W in default RO
  132. * demo mode.
  133. */
  134. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  135. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  136. else
  137. lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
  138. }
  139. pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
  140. " access for LUN in Demo Mode\n",
  141. tpg->se_tpg_tfo->get_fabric_name(),
  142. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  143. (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
  144. "READ-WRITE" : "READ-ONLY");
  145. core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
  146. lun_access, acl, tpg);
  147. /*
  148. * Check to see if there are any existing persistent reservation
  149. * APTPL pre-registrations that need to be enabled for this dynamic
  150. * LUN ACL now..
  151. */
  152. core_scsi3_check_aptpl_registration(dev, tpg, lun, acl,
  153. lun->unpacked_lun);
  154. spin_lock(&tpg->tpg_lun_lock);
  155. }
  156. spin_unlock(&tpg->tpg_lun_lock);
  157. }
  158. /* core_set_queue_depth_for_node():
  159. *
  160. *
  161. */
  162. static int core_set_queue_depth_for_node(
  163. struct se_portal_group *tpg,
  164. struct se_node_acl *acl)
  165. {
  166. if (!acl->queue_depth) {
  167. pr_err("Queue depth for %s Initiator Node: %s is 0,"
  168. "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
  169. acl->initiatorname);
  170. acl->queue_depth = 1;
  171. }
  172. return 0;
  173. }
  174. void array_free(void *array, int n)
  175. {
  176. void **a = array;
  177. int i;
  178. for (i = 0; i < n; i++)
  179. kfree(a[i]);
  180. kfree(a);
  181. }
  182. static void *array_zalloc(int n, size_t size, gfp_t flags)
  183. {
  184. void **a;
  185. int i;
  186. a = kzalloc(n * sizeof(void*), flags);
  187. if (!a)
  188. return NULL;
  189. for (i = 0; i < n; i++) {
  190. a[i] = kzalloc(size, flags);
  191. if (!a[i]) {
  192. array_free(a, n);
  193. return NULL;
  194. }
  195. }
  196. return a;
  197. }
  198. /* core_create_device_list_for_node():
  199. *
  200. *
  201. */
  202. static int core_create_device_list_for_node(struct se_node_acl *nacl)
  203. {
  204. struct se_dev_entry *deve;
  205. int i;
  206. nacl->device_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
  207. sizeof(struct se_dev_entry), GFP_KERNEL);
  208. if (!nacl->device_list) {
  209. pr_err("Unable to allocate memory for"
  210. " struct se_node_acl->device_list\n");
  211. return -ENOMEM;
  212. }
  213. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  214. deve = nacl->device_list[i];
  215. atomic_set(&deve->ua_count, 0);
  216. atomic_set(&deve->pr_ref_count, 0);
  217. spin_lock_init(&deve->ua_lock);
  218. INIT_LIST_HEAD(&deve->alua_port_list);
  219. INIT_LIST_HEAD(&deve->ua_list);
  220. }
  221. return 0;
  222. }
  223. /* core_tpg_check_initiator_node_acl()
  224. *
  225. *
  226. */
  227. struct se_node_acl *core_tpg_check_initiator_node_acl(
  228. struct se_portal_group *tpg,
  229. unsigned char *initiatorname)
  230. {
  231. struct se_node_acl *acl;
  232. acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
  233. if (acl)
  234. return acl;
  235. if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
  236. return NULL;
  237. acl = tpg->se_tpg_tfo->tpg_alloc_fabric_acl(tpg);
  238. if (!acl)
  239. return NULL;
  240. INIT_LIST_HEAD(&acl->acl_list);
  241. INIT_LIST_HEAD(&acl->acl_sess_list);
  242. kref_init(&acl->acl_kref);
  243. init_completion(&acl->acl_free_comp);
  244. spin_lock_init(&acl->device_list_lock);
  245. spin_lock_init(&acl->nacl_sess_lock);
  246. atomic_set(&acl->acl_pr_ref_count, 0);
  247. acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
  248. snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
  249. acl->se_tpg = tpg;
  250. acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
  251. acl->dynamic_node_acl = 1;
  252. tpg->se_tpg_tfo->set_default_node_attributes(acl);
  253. if (core_create_device_list_for_node(acl) < 0) {
  254. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  255. return NULL;
  256. }
  257. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  258. core_free_device_list_for_node(acl, tpg);
  259. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  260. return NULL;
  261. }
  262. /*
  263. * Here we only create demo-mode MappedLUNs from the active
  264. * TPG LUNs if the fabric is not explicitly asking for
  265. * tpg_check_demo_mode_login_only() == 1.
  266. */
  267. if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) ||
  268. (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1))
  269. core_tpg_add_node_to_devs(acl, tpg);
  270. spin_lock_irq(&tpg->acl_node_lock);
  271. list_add_tail(&acl->acl_list, &tpg->acl_node_list);
  272. tpg->num_node_acls++;
  273. spin_unlock_irq(&tpg->acl_node_lock);
  274. pr_debug("%s_TPG[%u] - Added DYNAMIC ACL with TCQ Depth: %d for %s"
  275. " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  276. tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
  277. tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
  278. return acl;
  279. }
  280. EXPORT_SYMBOL(core_tpg_check_initiator_node_acl);
  281. void core_tpg_wait_for_nacl_pr_ref(struct se_node_acl *nacl)
  282. {
  283. while (atomic_read(&nacl->acl_pr_ref_count) != 0)
  284. cpu_relax();
  285. }
  286. void core_tpg_clear_object_luns(struct se_portal_group *tpg)
  287. {
  288. int i;
  289. struct se_lun *lun;
  290. spin_lock(&tpg->tpg_lun_lock);
  291. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  292. lun = tpg->tpg_lun_list[i];
  293. if ((lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) ||
  294. (lun->lun_se_dev == NULL))
  295. continue;
  296. spin_unlock(&tpg->tpg_lun_lock);
  297. core_dev_del_lun(tpg, lun);
  298. spin_lock(&tpg->tpg_lun_lock);
  299. }
  300. spin_unlock(&tpg->tpg_lun_lock);
  301. }
  302. EXPORT_SYMBOL(core_tpg_clear_object_luns);
  303. /* core_tpg_add_initiator_node_acl():
  304. *
  305. *
  306. */
  307. struct se_node_acl *core_tpg_add_initiator_node_acl(
  308. struct se_portal_group *tpg,
  309. struct se_node_acl *se_nacl,
  310. const char *initiatorname,
  311. u32 queue_depth)
  312. {
  313. struct se_node_acl *acl = NULL;
  314. spin_lock_irq(&tpg->acl_node_lock);
  315. acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
  316. if (acl) {
  317. if (acl->dynamic_node_acl) {
  318. acl->dynamic_node_acl = 0;
  319. pr_debug("%s_TPG[%u] - Replacing dynamic ACL"
  320. " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  321. tpg->se_tpg_tfo->tpg_get_tag(tpg), initiatorname);
  322. spin_unlock_irq(&tpg->acl_node_lock);
  323. /*
  324. * Release the locally allocated struct se_node_acl
  325. * because * core_tpg_add_initiator_node_acl() returned
  326. * a pointer to an existing demo mode node ACL.
  327. */
  328. if (se_nacl)
  329. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg,
  330. se_nacl);
  331. goto done;
  332. }
  333. pr_err("ACL entry for %s Initiator"
  334. " Node %s already exists for TPG %u, ignoring"
  335. " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
  336. initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
  337. spin_unlock_irq(&tpg->acl_node_lock);
  338. return ERR_PTR(-EEXIST);
  339. }
  340. spin_unlock_irq(&tpg->acl_node_lock);
  341. if (!se_nacl) {
  342. pr_err("struct se_node_acl pointer is NULL\n");
  343. return ERR_PTR(-EINVAL);
  344. }
  345. /*
  346. * For v4.x logic the se_node_acl_s is hanging off a fabric
  347. * dependent structure allocated via
  348. * struct target_core_fabric_ops->fabric_make_nodeacl()
  349. */
  350. acl = se_nacl;
  351. INIT_LIST_HEAD(&acl->acl_list);
  352. INIT_LIST_HEAD(&acl->acl_sess_list);
  353. kref_init(&acl->acl_kref);
  354. init_completion(&acl->acl_free_comp);
  355. spin_lock_init(&acl->device_list_lock);
  356. spin_lock_init(&acl->nacl_sess_lock);
  357. atomic_set(&acl->acl_pr_ref_count, 0);
  358. acl->queue_depth = queue_depth;
  359. snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
  360. acl->se_tpg = tpg;
  361. acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
  362. tpg->se_tpg_tfo->set_default_node_attributes(acl);
  363. if (core_create_device_list_for_node(acl) < 0) {
  364. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  365. return ERR_PTR(-ENOMEM);
  366. }
  367. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  368. core_free_device_list_for_node(acl, tpg);
  369. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  370. return ERR_PTR(-EINVAL);
  371. }
  372. spin_lock_irq(&tpg->acl_node_lock);
  373. list_add_tail(&acl->acl_list, &tpg->acl_node_list);
  374. tpg->num_node_acls++;
  375. spin_unlock_irq(&tpg->acl_node_lock);
  376. done:
  377. pr_debug("%s_TPG[%hu] - Added ACL with TCQ Depth: %d for %s"
  378. " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  379. tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
  380. tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
  381. return acl;
  382. }
  383. EXPORT_SYMBOL(core_tpg_add_initiator_node_acl);
  384. /* core_tpg_del_initiator_node_acl():
  385. *
  386. *
  387. */
  388. int core_tpg_del_initiator_node_acl(
  389. struct se_portal_group *tpg,
  390. struct se_node_acl *acl,
  391. int force)
  392. {
  393. LIST_HEAD(sess_list);
  394. struct se_session *sess, *sess_tmp;
  395. unsigned long flags;
  396. int rc;
  397. spin_lock_irq(&tpg->acl_node_lock);
  398. if (acl->dynamic_node_acl) {
  399. acl->dynamic_node_acl = 0;
  400. }
  401. list_del(&acl->acl_list);
  402. tpg->num_node_acls--;
  403. spin_unlock_irq(&tpg->acl_node_lock);
  404. spin_lock_irqsave(&acl->nacl_sess_lock, flags);
  405. acl->acl_stop = 1;
  406. list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list,
  407. sess_acl_list) {
  408. if (sess->sess_tearing_down != 0)
  409. continue;
  410. target_get_session(sess);
  411. list_move(&sess->sess_acl_list, &sess_list);
  412. }
  413. spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
  414. list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) {
  415. list_del(&sess->sess_acl_list);
  416. rc = tpg->se_tpg_tfo->shutdown_session(sess);
  417. target_put_session(sess);
  418. if (!rc)
  419. continue;
  420. target_put_session(sess);
  421. }
  422. target_put_nacl(acl);
  423. /*
  424. * Wait for last target_put_nacl() to complete in target_complete_nacl()
  425. * for active fabric session transport_deregister_session() callbacks.
  426. */
  427. wait_for_completion(&acl->acl_free_comp);
  428. core_tpg_wait_for_nacl_pr_ref(acl);
  429. core_clear_initiator_node_from_tpg(acl, tpg);
  430. core_free_device_list_for_node(acl, tpg);
  431. pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
  432. " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  433. tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
  434. tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
  435. return 0;
  436. }
  437. EXPORT_SYMBOL(core_tpg_del_initiator_node_acl);
  438. /* core_tpg_set_initiator_node_queue_depth():
  439. *
  440. *
  441. */
  442. int core_tpg_set_initiator_node_queue_depth(
  443. struct se_portal_group *tpg,
  444. unsigned char *initiatorname,
  445. u32 queue_depth,
  446. int force)
  447. {
  448. struct se_session *sess, *init_sess = NULL;
  449. struct se_node_acl *acl;
  450. unsigned long flags;
  451. int dynamic_acl = 0;
  452. spin_lock_irq(&tpg->acl_node_lock);
  453. acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
  454. if (!acl) {
  455. pr_err("Access Control List entry for %s Initiator"
  456. " Node %s does not exists for TPG %hu, ignoring"
  457. " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
  458. initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
  459. spin_unlock_irq(&tpg->acl_node_lock);
  460. return -ENODEV;
  461. }
  462. if (acl->dynamic_node_acl) {
  463. acl->dynamic_node_acl = 0;
  464. dynamic_acl = 1;
  465. }
  466. spin_unlock_irq(&tpg->acl_node_lock);
  467. spin_lock_irqsave(&tpg->session_lock, flags);
  468. list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
  469. if (sess->se_node_acl != acl)
  470. continue;
  471. if (!force) {
  472. pr_err("Unable to change queue depth for %s"
  473. " Initiator Node: %s while session is"
  474. " operational. To forcefully change the queue"
  475. " depth and force session reinstatement"
  476. " use the \"force=1\" parameter.\n",
  477. tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
  478. spin_unlock_irqrestore(&tpg->session_lock, flags);
  479. spin_lock_irq(&tpg->acl_node_lock);
  480. if (dynamic_acl)
  481. acl->dynamic_node_acl = 1;
  482. spin_unlock_irq(&tpg->acl_node_lock);
  483. return -EEXIST;
  484. }
  485. /*
  486. * Determine if the session needs to be closed by our context.
  487. */
  488. if (!tpg->se_tpg_tfo->shutdown_session(sess))
  489. continue;
  490. init_sess = sess;
  491. break;
  492. }
  493. /*
  494. * User has requested to change the queue depth for a Initiator Node.
  495. * Change the value in the Node's struct se_node_acl, and call
  496. * core_set_queue_depth_for_node() to add the requested queue depth.
  497. *
  498. * Finally call tpg->se_tpg_tfo->close_session() to force session
  499. * reinstatement to occur if there is an active session for the
  500. * $FABRIC_MOD Initiator Node in question.
  501. */
  502. acl->queue_depth = queue_depth;
  503. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  504. spin_unlock_irqrestore(&tpg->session_lock, flags);
  505. /*
  506. * Force session reinstatement if
  507. * core_set_queue_depth_for_node() failed, because we assume
  508. * the $FABRIC_MOD has already the set session reinstatement
  509. * bit from tpg->se_tpg_tfo->shutdown_session() called above.
  510. */
  511. if (init_sess)
  512. tpg->se_tpg_tfo->close_session(init_sess);
  513. spin_lock_irq(&tpg->acl_node_lock);
  514. if (dynamic_acl)
  515. acl->dynamic_node_acl = 1;
  516. spin_unlock_irq(&tpg->acl_node_lock);
  517. return -EINVAL;
  518. }
  519. spin_unlock_irqrestore(&tpg->session_lock, flags);
  520. /*
  521. * If the $FABRIC_MOD session for the Initiator Node ACL exists,
  522. * forcefully shutdown the $FABRIC_MOD session/nexus.
  523. */
  524. if (init_sess)
  525. tpg->se_tpg_tfo->close_session(init_sess);
  526. pr_debug("Successfully changed queue depth to: %d for Initiator"
  527. " Node: %s on %s Target Portal Group: %u\n", queue_depth,
  528. initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
  529. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  530. spin_lock_irq(&tpg->acl_node_lock);
  531. if (dynamic_acl)
  532. acl->dynamic_node_acl = 1;
  533. spin_unlock_irq(&tpg->acl_node_lock);
  534. return 0;
  535. }
  536. EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
  537. /* core_tpg_set_initiator_node_tag():
  538. *
  539. * Initiator nodeacl tags are not used internally, but may be used by
  540. * userspace to emulate aliases or groups.
  541. * Returns length of newly-set tag or -EINVAL.
  542. */
  543. int core_tpg_set_initiator_node_tag(
  544. struct se_portal_group *tpg,
  545. struct se_node_acl *acl,
  546. const char *new_tag)
  547. {
  548. if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
  549. return -EINVAL;
  550. if (!strncmp("NULL", new_tag, 4)) {
  551. acl->acl_tag[0] = '\0';
  552. return 0;
  553. }
  554. return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
  555. }
  556. EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
  557. static void core_tpg_lun_ref_release(struct percpu_ref *ref)
  558. {
  559. struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
  560. complete(&lun->lun_ref_comp);
  561. }
  562. static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
  563. {
  564. /* Set in core_dev_setup_virtual_lun0() */
  565. struct se_device *dev = g_lun0_dev;
  566. struct se_lun *lun = &se_tpg->tpg_virt_lun0;
  567. u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  568. int ret;
  569. lun->unpacked_lun = 0;
  570. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  571. atomic_set(&lun->lun_acl_count, 0);
  572. init_completion(&lun->lun_shutdown_comp);
  573. INIT_LIST_HEAD(&lun->lun_acl_list);
  574. spin_lock_init(&lun->lun_acl_lock);
  575. spin_lock_init(&lun->lun_sep_lock);
  576. init_completion(&lun->lun_ref_comp);
  577. ret = core_tpg_add_lun(se_tpg, lun, lun_access, dev);
  578. if (ret < 0)
  579. return ret;
  580. return 0;
  581. }
  582. int core_tpg_register(
  583. const struct target_core_fabric_ops *tfo,
  584. struct se_wwn *se_wwn,
  585. struct se_portal_group *se_tpg,
  586. void *tpg_fabric_ptr,
  587. int se_tpg_type)
  588. {
  589. struct se_lun *lun;
  590. u32 i;
  591. se_tpg->tpg_lun_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
  592. sizeof(struct se_lun), GFP_KERNEL);
  593. if (!se_tpg->tpg_lun_list) {
  594. pr_err("Unable to allocate struct se_portal_group->"
  595. "tpg_lun_list\n");
  596. return -ENOMEM;
  597. }
  598. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  599. lun = se_tpg->tpg_lun_list[i];
  600. lun->unpacked_lun = i;
  601. lun->lun_link_magic = SE_LUN_LINK_MAGIC;
  602. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  603. atomic_set(&lun->lun_acl_count, 0);
  604. init_completion(&lun->lun_shutdown_comp);
  605. INIT_LIST_HEAD(&lun->lun_acl_list);
  606. spin_lock_init(&lun->lun_acl_lock);
  607. spin_lock_init(&lun->lun_sep_lock);
  608. init_completion(&lun->lun_ref_comp);
  609. }
  610. se_tpg->se_tpg_type = se_tpg_type;
  611. se_tpg->se_tpg_fabric_ptr = tpg_fabric_ptr;
  612. se_tpg->se_tpg_tfo = tfo;
  613. se_tpg->se_tpg_wwn = se_wwn;
  614. atomic_set(&se_tpg->tpg_pr_ref_count, 0);
  615. INIT_LIST_HEAD(&se_tpg->acl_node_list);
  616. INIT_LIST_HEAD(&se_tpg->se_tpg_node);
  617. INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
  618. spin_lock_init(&se_tpg->acl_node_lock);
  619. spin_lock_init(&se_tpg->session_lock);
  620. spin_lock_init(&se_tpg->tpg_lun_lock);
  621. if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) {
  622. if (core_tpg_setup_virtual_lun0(se_tpg) < 0) {
  623. array_free(se_tpg->tpg_lun_list,
  624. TRANSPORT_MAX_LUNS_PER_TPG);
  625. return -ENOMEM;
  626. }
  627. }
  628. spin_lock_bh(&tpg_lock);
  629. list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
  630. spin_unlock_bh(&tpg_lock);
  631. pr_debug("TARGET_CORE[%s]: Allocated %s struct se_portal_group for"
  632. " endpoint: %s, Portal Tag: %u\n", tfo->get_fabric_name(),
  633. (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
  634. "Normal" : "Discovery", (tfo->tpg_get_wwn(se_tpg) == NULL) ?
  635. "None" : tfo->tpg_get_wwn(se_tpg), tfo->tpg_get_tag(se_tpg));
  636. return 0;
  637. }
  638. EXPORT_SYMBOL(core_tpg_register);
  639. int core_tpg_deregister(struct se_portal_group *se_tpg)
  640. {
  641. struct se_node_acl *nacl, *nacl_tmp;
  642. pr_debug("TARGET_CORE[%s]: Deallocating %s struct se_portal_group"
  643. " for endpoint: %s Portal Tag %u\n",
  644. (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
  645. "Normal" : "Discovery", se_tpg->se_tpg_tfo->get_fabric_name(),
  646. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
  647. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  648. spin_lock_bh(&tpg_lock);
  649. list_del(&se_tpg->se_tpg_node);
  650. spin_unlock_bh(&tpg_lock);
  651. while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
  652. cpu_relax();
  653. /*
  654. * Release any remaining demo-mode generated se_node_acl that have
  655. * not been released because of TFO->tpg_check_demo_mode_cache() == 1
  656. * in transport_deregister_session().
  657. */
  658. spin_lock_irq(&se_tpg->acl_node_lock);
  659. list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list,
  660. acl_list) {
  661. list_del(&nacl->acl_list);
  662. se_tpg->num_node_acls--;
  663. spin_unlock_irq(&se_tpg->acl_node_lock);
  664. core_tpg_wait_for_nacl_pr_ref(nacl);
  665. core_free_device_list_for_node(nacl, se_tpg);
  666. se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, nacl);
  667. spin_lock_irq(&se_tpg->acl_node_lock);
  668. }
  669. spin_unlock_irq(&se_tpg->acl_node_lock);
  670. if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL)
  671. core_tpg_remove_lun(se_tpg, &se_tpg->tpg_virt_lun0);
  672. se_tpg->se_tpg_fabric_ptr = NULL;
  673. array_free(se_tpg->tpg_lun_list, TRANSPORT_MAX_LUNS_PER_TPG);
  674. return 0;
  675. }
  676. EXPORT_SYMBOL(core_tpg_deregister);
  677. struct se_lun *core_tpg_alloc_lun(
  678. struct se_portal_group *tpg,
  679. u32 unpacked_lun)
  680. {
  681. struct se_lun *lun;
  682. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  683. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
  684. "-1: %u for Target Portal Group: %u\n",
  685. tpg->se_tpg_tfo->get_fabric_name(),
  686. unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
  687. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  688. return ERR_PTR(-EOVERFLOW);
  689. }
  690. spin_lock(&tpg->tpg_lun_lock);
  691. lun = tpg->tpg_lun_list[unpacked_lun];
  692. if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) {
  693. pr_err("TPG Logical Unit Number: %u is already active"
  694. " on %s Target Portal Group: %u, ignoring request.\n",
  695. unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(),
  696. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  697. spin_unlock(&tpg->tpg_lun_lock);
  698. return ERR_PTR(-EINVAL);
  699. }
  700. spin_unlock(&tpg->tpg_lun_lock);
  701. return lun;
  702. }
  703. int core_tpg_add_lun(
  704. struct se_portal_group *tpg,
  705. struct se_lun *lun,
  706. u32 lun_access,
  707. struct se_device *dev)
  708. {
  709. int ret;
  710. ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release, 0,
  711. GFP_KERNEL);
  712. if (ret < 0)
  713. return ret;
  714. ret = core_dev_export(dev, tpg, lun);
  715. if (ret < 0) {
  716. percpu_ref_exit(&lun->lun_ref);
  717. return ret;
  718. }
  719. spin_lock(&tpg->tpg_lun_lock);
  720. lun->lun_access = lun_access;
  721. lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE;
  722. spin_unlock(&tpg->tpg_lun_lock);
  723. return 0;
  724. }
  725. void core_tpg_remove_lun(
  726. struct se_portal_group *tpg,
  727. struct se_lun *lun)
  728. {
  729. core_clear_lun_from_tpg(lun, tpg);
  730. transport_clear_lun_ref(lun);
  731. core_dev_unexport(lun->lun_se_dev, tpg, lun);
  732. spin_lock(&tpg->tpg_lun_lock);
  733. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  734. spin_unlock(&tpg->tpg_lun_lock);
  735. percpu_ref_exit(&lun->lun_ref);
  736. }