target_core_tpg.c 23 KB

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