target_core_tpg.c 23 KB

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