target_core_tpg.c 23 KB

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