target_core_tpg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*******************************************************************************
  2. * Filename: target_core_tpg.c
  3. *
  4. * This file contains generic Target Portal Group related functions.
  5. *
  6. * (c) Copyright 2002-2012 RisingTide Systems LLC.
  7. *
  8. * Nicholas A. Bellinger <nab@kernel.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. ******************************************************************************/
  25. #include <linux/net.h>
  26. #include <linux/string.h>
  27. #include <linux/timer.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/in.h>
  31. #include <linux/export.h>
  32. #include <net/sock.h>
  33. #include <net/tcp.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/scsi_cmnd.h>
  36. #include <target/target_core_base.h>
  37. #include <target/target_core_backend.h>
  38. #include <target/target_core_fabric.h>
  39. #include "target_core_internal.h"
  40. extern struct se_device *g_lun0_dev;
  41. static DEFINE_SPINLOCK(tpg_lock);
  42. static LIST_HEAD(tpg_list);
  43. /* core_clear_initiator_node_from_tpg():
  44. *
  45. *
  46. */
  47. static void core_clear_initiator_node_from_tpg(
  48. struct se_node_acl *nacl,
  49. struct se_portal_group *tpg)
  50. {
  51. int i;
  52. struct se_dev_entry *deve;
  53. struct se_lun *lun;
  54. spin_lock_irq(&nacl->device_list_lock);
  55. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  56. deve = nacl->device_list[i];
  57. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  58. continue;
  59. if (!deve->se_lun) {
  60. pr_err("%s device entries device pointer is"
  61. " NULL, but Initiator has access.\n",
  62. tpg->se_tpg_tfo->get_fabric_name());
  63. continue;
  64. }
  65. lun = deve->se_lun;
  66. spin_unlock_irq(&nacl->device_list_lock);
  67. core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
  68. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  69. spin_lock_irq(&nacl->device_list_lock);
  70. }
  71. spin_unlock_irq(&nacl->device_list_lock);
  72. }
  73. /* __core_tpg_get_initiator_node_acl():
  74. *
  75. * spin_lock_bh(&tpg->acl_node_lock); must be held when calling
  76. */
  77. struct se_node_acl *__core_tpg_get_initiator_node_acl(
  78. struct se_portal_group *tpg,
  79. const char *initiatorname)
  80. {
  81. struct se_node_acl *acl;
  82. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  83. if (!strcmp(acl->initiatorname, initiatorname))
  84. return acl;
  85. }
  86. return NULL;
  87. }
  88. /* core_tpg_get_initiator_node_acl():
  89. *
  90. *
  91. */
  92. struct se_node_acl *core_tpg_get_initiator_node_acl(
  93. struct se_portal_group *tpg,
  94. unsigned char *initiatorname)
  95. {
  96. struct se_node_acl *acl;
  97. spin_lock_irq(&tpg->acl_node_lock);
  98. acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
  99. spin_unlock_irq(&tpg->acl_node_lock);
  100. return acl;
  101. }
  102. /* core_tpg_add_node_to_devs():
  103. *
  104. *
  105. */
  106. void core_tpg_add_node_to_devs(
  107. struct se_node_acl *acl,
  108. struct se_portal_group *tpg)
  109. {
  110. int i = 0;
  111. u32 lun_access = 0;
  112. struct se_lun *lun;
  113. struct se_device *dev;
  114. spin_lock(&tpg->tpg_lun_lock);
  115. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  116. lun = tpg->tpg_lun_list[i];
  117. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
  118. continue;
  119. spin_unlock(&tpg->tpg_lun_lock);
  120. dev = lun->lun_se_dev;
  121. /*
  122. * By default in LIO-Target $FABRIC_MOD,
  123. * demo_mode_write_protect is ON, or READ_ONLY;
  124. */
  125. if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
  126. lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
  127. } else {
  128. /*
  129. * Allow only optical drives to issue R/W in default RO
  130. * demo mode.
  131. */
  132. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  133. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  134. else
  135. lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
  136. }
  137. pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
  138. " access for LUN in Demo Mode\n",
  139. tpg->se_tpg_tfo->get_fabric_name(),
  140. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  141. (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
  142. "READ-WRITE" : "READ-ONLY");
  143. core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
  144. lun_access, acl, tpg);
  145. spin_lock(&tpg->tpg_lun_lock);
  146. }
  147. spin_unlock(&tpg->tpg_lun_lock);
  148. }
  149. /* core_set_queue_depth_for_node():
  150. *
  151. *
  152. */
  153. static int core_set_queue_depth_for_node(
  154. struct se_portal_group *tpg,
  155. struct se_node_acl *acl)
  156. {
  157. if (!acl->queue_depth) {
  158. pr_err("Queue depth for %s Initiator Node: %s is 0,"
  159. "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
  160. acl->initiatorname);
  161. acl->queue_depth = 1;
  162. }
  163. return 0;
  164. }
  165. void array_free(void *array, int n)
  166. {
  167. void **a = array;
  168. int i;
  169. for (i = 0; i < n; i++)
  170. kfree(a[i]);
  171. kfree(a);
  172. }
  173. static void *array_zalloc(int n, size_t size, gfp_t flags)
  174. {
  175. void **a;
  176. int i;
  177. a = kzalloc(n * sizeof(void*), flags);
  178. if (!a)
  179. return NULL;
  180. for (i = 0; i < n; i++) {
  181. a[i] = kzalloc(size, flags);
  182. if (!a[i]) {
  183. array_free(a, n);
  184. return NULL;
  185. }
  186. }
  187. return a;
  188. }
  189. /* core_create_device_list_for_node():
  190. *
  191. *
  192. */
  193. static int core_create_device_list_for_node(struct se_node_acl *nacl)
  194. {
  195. struct se_dev_entry *deve;
  196. int i;
  197. nacl->device_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
  198. sizeof(struct se_dev_entry), GFP_KERNEL);
  199. if (!nacl->device_list) {
  200. pr_err("Unable to allocate memory for"
  201. " struct se_node_acl->device_list\n");
  202. return -ENOMEM;
  203. }
  204. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  205. deve = nacl->device_list[i];
  206. atomic_set(&deve->ua_count, 0);
  207. atomic_set(&deve->pr_ref_count, 0);
  208. spin_lock_init(&deve->ua_lock);
  209. INIT_LIST_HEAD(&deve->alua_port_list);
  210. INIT_LIST_HEAD(&deve->ua_list);
  211. }
  212. return 0;
  213. }
  214. /* core_tpg_check_initiator_node_acl()
  215. *
  216. *
  217. */
  218. struct se_node_acl *core_tpg_check_initiator_node_acl(
  219. struct se_portal_group *tpg,
  220. unsigned char *initiatorname)
  221. {
  222. struct se_node_acl *acl;
  223. acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
  224. if (acl)
  225. return acl;
  226. if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
  227. return NULL;
  228. acl = tpg->se_tpg_tfo->tpg_alloc_fabric_acl(tpg);
  229. if (!acl)
  230. return NULL;
  231. INIT_LIST_HEAD(&acl->acl_list);
  232. INIT_LIST_HEAD(&acl->acl_sess_list);
  233. kref_init(&acl->acl_kref);
  234. init_completion(&acl->acl_free_comp);
  235. spin_lock_init(&acl->device_list_lock);
  236. spin_lock_init(&acl->nacl_sess_lock);
  237. atomic_set(&acl->acl_pr_ref_count, 0);
  238. acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
  239. snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
  240. acl->se_tpg = tpg;
  241. acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
  242. spin_lock_init(&acl->stats_lock);
  243. acl->dynamic_node_acl = 1;
  244. tpg->se_tpg_tfo->set_default_node_attributes(acl);
  245. if (core_create_device_list_for_node(acl) < 0) {
  246. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  247. return NULL;
  248. }
  249. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  250. core_free_device_list_for_node(acl, tpg);
  251. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  252. return NULL;
  253. }
  254. /*
  255. * Here we only create demo-mode MappedLUNs from the active
  256. * TPG LUNs if the fabric is not explicitly asking for
  257. * tpg_check_demo_mode_login_only() == 1.
  258. */
  259. if ((tpg->se_tpg_tfo->tpg_check_demo_mode_login_only == NULL) ||
  260. (tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg) != 1))
  261. core_tpg_add_node_to_devs(acl, tpg);
  262. spin_lock_irq(&tpg->acl_node_lock);
  263. list_add_tail(&acl->acl_list, &tpg->acl_node_list);
  264. tpg->num_node_acls++;
  265. spin_unlock_irq(&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;
  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. 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_irq(&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_irq(&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_irq(&tpg->acl_node_lock);
  330. return ERR_PTR(-EEXIST);
  331. }
  332. spin_unlock_irq(&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. kref_init(&acl->acl_kref);
  346. init_completion(&acl->acl_free_comp);
  347. spin_lock_init(&acl->device_list_lock);
  348. spin_lock_init(&acl->nacl_sess_lock);
  349. atomic_set(&acl->acl_pr_ref_count, 0);
  350. acl->queue_depth = queue_depth;
  351. snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
  352. acl->se_tpg = tpg;
  353. acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
  354. spin_lock_init(&acl->stats_lock);
  355. tpg->se_tpg_tfo->set_default_node_attributes(acl);
  356. if (core_create_device_list_for_node(acl) < 0) {
  357. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  358. return ERR_PTR(-ENOMEM);
  359. }
  360. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  361. core_free_device_list_for_node(acl, tpg);
  362. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  363. return ERR_PTR(-EINVAL);
  364. }
  365. spin_lock_irq(&tpg->acl_node_lock);
  366. list_add_tail(&acl->acl_list, &tpg->acl_node_list);
  367. tpg->num_node_acls++;
  368. spin_unlock_irq(&tpg->acl_node_lock);
  369. done:
  370. pr_debug("%s_TPG[%hu] - Added ACL with TCQ Depth: %d for %s"
  371. " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  372. tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
  373. tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
  374. return acl;
  375. }
  376. EXPORT_SYMBOL(core_tpg_add_initiator_node_acl);
  377. /* core_tpg_del_initiator_node_acl():
  378. *
  379. *
  380. */
  381. int core_tpg_del_initiator_node_acl(
  382. struct se_portal_group *tpg,
  383. struct se_node_acl *acl,
  384. int force)
  385. {
  386. LIST_HEAD(sess_list);
  387. struct se_session *sess, *sess_tmp;
  388. unsigned long flags;
  389. int rc;
  390. spin_lock_irq(&tpg->acl_node_lock);
  391. if (acl->dynamic_node_acl) {
  392. acl->dynamic_node_acl = 0;
  393. }
  394. list_del(&acl->acl_list);
  395. tpg->num_node_acls--;
  396. spin_unlock_irq(&tpg->acl_node_lock);
  397. spin_lock_irqsave(&acl->nacl_sess_lock, flags);
  398. acl->acl_stop = 1;
  399. list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list,
  400. sess_acl_list) {
  401. if (sess->sess_tearing_down != 0)
  402. continue;
  403. target_get_session(sess);
  404. list_move(&sess->sess_acl_list, &sess_list);
  405. }
  406. spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
  407. list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) {
  408. list_del(&sess->sess_acl_list);
  409. rc = tpg->se_tpg_tfo->shutdown_session(sess);
  410. target_put_session(sess);
  411. if (!rc)
  412. continue;
  413. target_put_session(sess);
  414. }
  415. target_put_nacl(acl);
  416. /*
  417. * Wait for last target_put_nacl() to complete in target_complete_nacl()
  418. * for active fabric session transport_deregister_session() callbacks.
  419. */
  420. wait_for_completion(&acl->acl_free_comp);
  421. core_tpg_wait_for_nacl_pr_ref(acl);
  422. core_clear_initiator_node_from_tpg(acl, tpg);
  423. core_free_device_list_for_node(acl, tpg);
  424. pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
  425. " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  426. tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
  427. tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
  428. return 0;
  429. }
  430. EXPORT_SYMBOL(core_tpg_del_initiator_node_acl);
  431. /* core_tpg_set_initiator_node_queue_depth():
  432. *
  433. *
  434. */
  435. int core_tpg_set_initiator_node_queue_depth(
  436. struct se_portal_group *tpg,
  437. unsigned char *initiatorname,
  438. u32 queue_depth,
  439. int force)
  440. {
  441. struct se_session *sess, *init_sess = NULL;
  442. struct se_node_acl *acl;
  443. unsigned long flags;
  444. int dynamic_acl = 0;
  445. spin_lock_irq(&tpg->acl_node_lock);
  446. acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
  447. if (!acl) {
  448. pr_err("Access Control List entry for %s Initiator"
  449. " Node %s does not exists for TPG %hu, ignoring"
  450. " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
  451. initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
  452. spin_unlock_irq(&tpg->acl_node_lock);
  453. return -ENODEV;
  454. }
  455. if (acl->dynamic_node_acl) {
  456. acl->dynamic_node_acl = 0;
  457. dynamic_acl = 1;
  458. }
  459. spin_unlock_irq(&tpg->acl_node_lock);
  460. spin_lock_irqsave(&tpg->session_lock, flags);
  461. list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
  462. if (sess->se_node_acl != acl)
  463. continue;
  464. if (!force) {
  465. pr_err("Unable to change queue depth for %s"
  466. " Initiator Node: %s while session is"
  467. " operational. To forcefully change the queue"
  468. " depth and force session reinstatement"
  469. " use the \"force=1\" parameter.\n",
  470. tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
  471. spin_unlock_irqrestore(&tpg->session_lock, flags);
  472. spin_lock_irq(&tpg->acl_node_lock);
  473. if (dynamic_acl)
  474. acl->dynamic_node_acl = 1;
  475. spin_unlock_irq(&tpg->acl_node_lock);
  476. return -EEXIST;
  477. }
  478. /*
  479. * Determine if the session needs to be closed by our context.
  480. */
  481. if (!tpg->se_tpg_tfo->shutdown_session(sess))
  482. continue;
  483. init_sess = sess;
  484. break;
  485. }
  486. /*
  487. * User has requested to change the queue depth for a Initiator Node.
  488. * Change the value in the Node's struct se_node_acl, and call
  489. * core_set_queue_depth_for_node() to add the requested queue depth.
  490. *
  491. * Finally call tpg->se_tpg_tfo->close_session() to force session
  492. * reinstatement to occur if there is an active session for the
  493. * $FABRIC_MOD Initiator Node in question.
  494. */
  495. acl->queue_depth = queue_depth;
  496. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  497. spin_unlock_irqrestore(&tpg->session_lock, flags);
  498. /*
  499. * Force session reinstatement if
  500. * core_set_queue_depth_for_node() failed, because we assume
  501. * the $FABRIC_MOD has already the set session reinstatement
  502. * bit from tpg->se_tpg_tfo->shutdown_session() called above.
  503. */
  504. if (init_sess)
  505. tpg->se_tpg_tfo->close_session(init_sess);
  506. spin_lock_irq(&tpg->acl_node_lock);
  507. if (dynamic_acl)
  508. acl->dynamic_node_acl = 1;
  509. spin_unlock_irq(&tpg->acl_node_lock);
  510. return -EINVAL;
  511. }
  512. spin_unlock_irqrestore(&tpg->session_lock, flags);
  513. /*
  514. * If the $FABRIC_MOD session for the Initiator Node ACL exists,
  515. * forcefully shutdown the $FABRIC_MOD session/nexus.
  516. */
  517. if (init_sess)
  518. tpg->se_tpg_tfo->close_session(init_sess);
  519. pr_debug("Successfully changed queue depth to: %d for Initiator"
  520. " Node: %s on %s Target Portal Group: %u\n", queue_depth,
  521. initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
  522. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  523. spin_lock_irq(&tpg->acl_node_lock);
  524. if (dynamic_acl)
  525. acl->dynamic_node_acl = 1;
  526. spin_unlock_irq(&tpg->acl_node_lock);
  527. return 0;
  528. }
  529. EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
  530. /* core_tpg_set_initiator_node_tag():
  531. *
  532. * Initiator nodeacl tags are not used internally, but may be used by
  533. * userspace to emulate aliases or groups.
  534. * Returns length of newly-set tag or -EINVAL.
  535. */
  536. int core_tpg_set_initiator_node_tag(
  537. struct se_portal_group *tpg,
  538. struct se_node_acl *acl,
  539. const char *new_tag)
  540. {
  541. if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
  542. return -EINVAL;
  543. if (!strncmp("NULL", new_tag, 4)) {
  544. acl->acl_tag[0] = '\0';
  545. return 0;
  546. }
  547. return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
  548. }
  549. EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
  550. static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
  551. {
  552. /* Set in core_dev_setup_virtual_lun0() */
  553. struct se_device *dev = g_lun0_dev;
  554. struct se_lun *lun = &se_tpg->tpg_virt_lun0;
  555. u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  556. int ret;
  557. lun->unpacked_lun = 0;
  558. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  559. atomic_set(&lun->lun_acl_count, 0);
  560. init_completion(&lun->lun_shutdown_comp);
  561. INIT_LIST_HEAD(&lun->lun_acl_list);
  562. INIT_LIST_HEAD(&lun->lun_cmd_list);
  563. spin_lock_init(&lun->lun_acl_lock);
  564. spin_lock_init(&lun->lun_cmd_lock);
  565. spin_lock_init(&lun->lun_sep_lock);
  566. ret = core_tpg_post_addlun(se_tpg, lun, lun_access, dev);
  567. if (ret < 0)
  568. return ret;
  569. return 0;
  570. }
  571. static void core_tpg_release_virtual_lun0(struct se_portal_group *se_tpg)
  572. {
  573. struct se_lun *lun = &se_tpg->tpg_virt_lun0;
  574. core_tpg_post_dellun(se_tpg, lun);
  575. }
  576. int core_tpg_register(
  577. struct target_core_fabric_ops *tfo,
  578. struct se_wwn *se_wwn,
  579. struct se_portal_group *se_tpg,
  580. void *tpg_fabric_ptr,
  581. int se_tpg_type)
  582. {
  583. struct se_lun *lun;
  584. u32 i;
  585. se_tpg->tpg_lun_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
  586. sizeof(struct se_lun), GFP_KERNEL);
  587. if (!se_tpg->tpg_lun_list) {
  588. pr_err("Unable to allocate struct se_portal_group->"
  589. "tpg_lun_list\n");
  590. return -ENOMEM;
  591. }
  592. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  593. lun = se_tpg->tpg_lun_list[i];
  594. lun->unpacked_lun = i;
  595. lun->lun_link_magic = SE_LUN_LINK_MAGIC;
  596. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  597. atomic_set(&lun->lun_acl_count, 0);
  598. init_completion(&lun->lun_shutdown_comp);
  599. INIT_LIST_HEAD(&lun->lun_acl_list);
  600. INIT_LIST_HEAD(&lun->lun_cmd_list);
  601. spin_lock_init(&lun->lun_acl_lock);
  602. spin_lock_init(&lun->lun_cmd_lock);
  603. spin_lock_init(&lun->lun_sep_lock);
  604. }
  605. se_tpg->se_tpg_type = se_tpg_type;
  606. se_tpg->se_tpg_fabric_ptr = tpg_fabric_ptr;
  607. se_tpg->se_tpg_tfo = tfo;
  608. se_tpg->se_tpg_wwn = se_wwn;
  609. atomic_set(&se_tpg->tpg_pr_ref_count, 0);
  610. INIT_LIST_HEAD(&se_tpg->acl_node_list);
  611. INIT_LIST_HEAD(&se_tpg->se_tpg_node);
  612. INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
  613. spin_lock_init(&se_tpg->acl_node_lock);
  614. spin_lock_init(&se_tpg->session_lock);
  615. spin_lock_init(&se_tpg->tpg_lun_lock);
  616. if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) {
  617. if (core_tpg_setup_virtual_lun0(se_tpg) < 0) {
  618. array_free(se_tpg->tpg_lun_list,
  619. TRANSPORT_MAX_LUNS_PER_TPG);
  620. return -ENOMEM;
  621. }
  622. }
  623. spin_lock_bh(&tpg_lock);
  624. list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
  625. spin_unlock_bh(&tpg_lock);
  626. pr_debug("TARGET_CORE[%s]: Allocated %s struct se_portal_group for"
  627. " endpoint: %s, Portal Tag: %u\n", tfo->get_fabric_name(),
  628. (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
  629. "Normal" : "Discovery", (tfo->tpg_get_wwn(se_tpg) == NULL) ?
  630. "None" : tfo->tpg_get_wwn(se_tpg), tfo->tpg_get_tag(se_tpg));
  631. return 0;
  632. }
  633. EXPORT_SYMBOL(core_tpg_register);
  634. int core_tpg_deregister(struct se_portal_group *se_tpg)
  635. {
  636. struct se_node_acl *nacl, *nacl_tmp;
  637. pr_debug("TARGET_CORE[%s]: Deallocating %s struct se_portal_group"
  638. " for endpoint: %s Portal Tag %u\n",
  639. (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
  640. "Normal" : "Discovery", se_tpg->se_tpg_tfo->get_fabric_name(),
  641. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
  642. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  643. spin_lock_bh(&tpg_lock);
  644. list_del(&se_tpg->se_tpg_node);
  645. spin_unlock_bh(&tpg_lock);
  646. while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
  647. cpu_relax();
  648. /*
  649. * Release any remaining demo-mode generated se_node_acl that have
  650. * not been released because of TFO->tpg_check_demo_mode_cache() == 1
  651. * in transport_deregister_session().
  652. */
  653. spin_lock_irq(&se_tpg->acl_node_lock);
  654. list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list,
  655. acl_list) {
  656. list_del(&nacl->acl_list);
  657. se_tpg->num_node_acls--;
  658. spin_unlock_irq(&se_tpg->acl_node_lock);
  659. core_tpg_wait_for_nacl_pr_ref(nacl);
  660. core_free_device_list_for_node(nacl, se_tpg);
  661. se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, nacl);
  662. spin_lock_irq(&se_tpg->acl_node_lock);
  663. }
  664. spin_unlock_irq(&se_tpg->acl_node_lock);
  665. if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL)
  666. core_tpg_release_virtual_lun0(se_tpg);
  667. se_tpg->se_tpg_fabric_ptr = NULL;
  668. array_free(se_tpg->tpg_lun_list, TRANSPORT_MAX_LUNS_PER_TPG);
  669. return 0;
  670. }
  671. EXPORT_SYMBOL(core_tpg_deregister);
  672. struct se_lun *core_tpg_pre_addlun(
  673. struct se_portal_group *tpg,
  674. u32 unpacked_lun)
  675. {
  676. struct se_lun *lun;
  677. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  678. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
  679. "-1: %u for Target Portal Group: %u\n",
  680. tpg->se_tpg_tfo->get_fabric_name(),
  681. unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
  682. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  683. return ERR_PTR(-EOVERFLOW);
  684. }
  685. spin_lock(&tpg->tpg_lun_lock);
  686. lun = tpg->tpg_lun_list[unpacked_lun];
  687. if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) {
  688. pr_err("TPG Logical Unit Number: %u is already active"
  689. " on %s Target Portal Group: %u, ignoring request.\n",
  690. unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(),
  691. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  692. spin_unlock(&tpg->tpg_lun_lock);
  693. return ERR_PTR(-EINVAL);
  694. }
  695. spin_unlock(&tpg->tpg_lun_lock);
  696. return lun;
  697. }
  698. int core_tpg_post_addlun(
  699. struct se_portal_group *tpg,
  700. struct se_lun *lun,
  701. u32 lun_access,
  702. void *lun_ptr)
  703. {
  704. int ret;
  705. ret = core_dev_export(lun_ptr, tpg, lun);
  706. if (ret < 0)
  707. return ret;
  708. spin_lock(&tpg->tpg_lun_lock);
  709. lun->lun_access = lun_access;
  710. lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE;
  711. spin_unlock(&tpg->tpg_lun_lock);
  712. return 0;
  713. }
  714. static void core_tpg_shutdown_lun(
  715. struct se_portal_group *tpg,
  716. struct se_lun *lun)
  717. {
  718. core_clear_lun_from_tpg(lun, tpg);
  719. transport_clear_lun_from_sessions(lun);
  720. }
  721. struct se_lun *core_tpg_pre_dellun(
  722. struct se_portal_group *tpg,
  723. u32 unpacked_lun)
  724. {
  725. struct se_lun *lun;
  726. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  727. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
  728. "-1: %u for Target Portal Group: %u\n",
  729. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  730. TRANSPORT_MAX_LUNS_PER_TPG-1,
  731. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  732. return ERR_PTR(-EOVERFLOW);
  733. }
  734. spin_lock(&tpg->tpg_lun_lock);
  735. lun = tpg->tpg_lun_list[unpacked_lun];
  736. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
  737. pr_err("%s Logical Unit Number: %u is not active on"
  738. " Target Portal Group: %u, ignoring request.\n",
  739. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  740. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  741. spin_unlock(&tpg->tpg_lun_lock);
  742. return ERR_PTR(-ENODEV);
  743. }
  744. spin_unlock(&tpg->tpg_lun_lock);
  745. return lun;
  746. }
  747. int core_tpg_post_dellun(
  748. struct se_portal_group *tpg,
  749. struct se_lun *lun)
  750. {
  751. core_tpg_shutdown_lun(tpg, lun);
  752. core_dev_unexport(lun->lun_se_dev, tpg, lun);
  753. spin_lock(&tpg->tpg_lun_lock);
  754. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  755. spin_unlock(&tpg->tpg_lun_lock);
  756. return 0;
  757. }