target_core_tpg.c 24 KB

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