target_core_tpg.c 23 KB

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