target_core_tpg.c 23 KB

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