target_core_tpg.c 23 KB

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