target_core_tpg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*******************************************************************************
  2. * Filename: target_core_tpg.c
  3. *
  4. * This file contains generic Target Portal Group related functions.
  5. *
  6. * (c) Copyright 2002-2013 Datera, Inc.
  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. EXPORT_SYMBOL(core_tpg_get_initiator_node_acl);
  103. /* core_tpg_add_node_to_devs():
  104. *
  105. *
  106. */
  107. void core_tpg_add_node_to_devs(
  108. struct se_node_acl *acl,
  109. struct se_portal_group *tpg)
  110. {
  111. int i = 0;
  112. u32 lun_access = 0;
  113. struct se_lun *lun;
  114. struct se_device *dev;
  115. spin_lock(&tpg->tpg_lun_lock);
  116. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  117. lun = tpg->tpg_lun_list[i];
  118. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE)
  119. continue;
  120. spin_unlock(&tpg->tpg_lun_lock);
  121. dev = lun->lun_se_dev;
  122. /*
  123. * By default in LIO-Target $FABRIC_MOD,
  124. * demo_mode_write_protect is ON, or READ_ONLY;
  125. */
  126. if (!tpg->se_tpg_tfo->tpg_check_demo_mode_write_protect(tpg)) {
  127. lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
  128. } else {
  129. /*
  130. * Allow only optical drives to issue R/W in default RO
  131. * demo mode.
  132. */
  133. if (dev->transport->get_device_type(dev) == TYPE_DISK)
  134. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  135. else
  136. lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
  137. }
  138. pr_debug("TARGET_CORE[%s]->TPG[%u]_LUN[%u] - Adding %s"
  139. " access for LUN in Demo Mode\n",
  140. tpg->se_tpg_tfo->get_fabric_name(),
  141. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  142. (lun_access == TRANSPORT_LUNFLAGS_READ_WRITE) ?
  143. "READ-WRITE" : "READ-ONLY");
  144. core_enable_device_list_for_node(lun, NULL, lun->unpacked_lun,
  145. lun_access, acl, tpg);
  146. spin_lock(&tpg->tpg_lun_lock);
  147. }
  148. spin_unlock(&tpg->tpg_lun_lock);
  149. }
  150. /* core_set_queue_depth_for_node():
  151. *
  152. *
  153. */
  154. static int core_set_queue_depth_for_node(
  155. struct se_portal_group *tpg,
  156. struct se_node_acl *acl)
  157. {
  158. if (!acl->queue_depth) {
  159. pr_err("Queue depth for %s Initiator Node: %s is 0,"
  160. "defaulting to 1.\n", tpg->se_tpg_tfo->get_fabric_name(),
  161. acl->initiatorname);
  162. acl->queue_depth = 1;
  163. }
  164. return 0;
  165. }
  166. void array_free(void *array, int n)
  167. {
  168. void **a = array;
  169. int i;
  170. for (i = 0; i < n; i++)
  171. kfree(a[i]);
  172. kfree(a);
  173. }
  174. static void *array_zalloc(int n, size_t size, gfp_t flags)
  175. {
  176. void **a;
  177. int i;
  178. a = kzalloc(n * sizeof(void*), flags);
  179. if (!a)
  180. return NULL;
  181. for (i = 0; i < n; i++) {
  182. a[i] = kzalloc(size, flags);
  183. if (!a[i]) {
  184. array_free(a, n);
  185. return NULL;
  186. }
  187. }
  188. return a;
  189. }
  190. /* core_create_device_list_for_node():
  191. *
  192. *
  193. */
  194. static int core_create_device_list_for_node(struct se_node_acl *nacl)
  195. {
  196. struct se_dev_entry *deve;
  197. int i;
  198. nacl->device_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
  199. sizeof(struct se_dev_entry), GFP_KERNEL);
  200. if (!nacl->device_list) {
  201. pr_err("Unable to allocate memory for"
  202. " struct se_node_acl->device_list\n");
  203. return -ENOMEM;
  204. }
  205. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  206. deve = nacl->device_list[i];
  207. atomic_set(&deve->ua_count, 0);
  208. atomic_set(&deve->pr_ref_count, 0);
  209. spin_lock_init(&deve->ua_lock);
  210. INIT_LIST_HEAD(&deve->alua_port_list);
  211. INIT_LIST_HEAD(&deve->ua_list);
  212. }
  213. return 0;
  214. }
  215. /* core_tpg_check_initiator_node_acl()
  216. *
  217. *
  218. */
  219. struct se_node_acl *core_tpg_check_initiator_node_acl(
  220. struct se_portal_group *tpg,
  221. unsigned char *initiatorname)
  222. {
  223. struct se_node_acl *acl;
  224. acl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
  225. if (acl)
  226. return acl;
  227. if (!tpg->se_tpg_tfo->tpg_check_demo_mode(tpg))
  228. return NULL;
  229. acl = tpg->se_tpg_tfo->tpg_alloc_fabric_acl(tpg);
  230. if (!acl)
  231. return NULL;
  232. INIT_LIST_HEAD(&acl->acl_list);
  233. INIT_LIST_HEAD(&acl->acl_sess_list);
  234. kref_init(&acl->acl_kref);
  235. init_completion(&acl->acl_free_comp);
  236. spin_lock_init(&acl->device_list_lock);
  237. spin_lock_init(&acl->nacl_sess_lock);
  238. atomic_set(&acl->acl_pr_ref_count, 0);
  239. acl->queue_depth = tpg->se_tpg_tfo->tpg_get_default_depth(tpg);
  240. snprintf(acl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
  241. acl->se_tpg = tpg;
  242. acl->acl_index = scsi_get_new_index(SCSI_AUTH_INTR_INDEX);
  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. tpg->se_tpg_tfo->set_default_node_attributes(acl);
  355. if (core_create_device_list_for_node(acl) < 0) {
  356. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  357. return ERR_PTR(-ENOMEM);
  358. }
  359. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  360. core_free_device_list_for_node(acl, tpg);
  361. tpg->se_tpg_tfo->tpg_release_fabric_acl(tpg, acl);
  362. return ERR_PTR(-EINVAL);
  363. }
  364. spin_lock_irq(&tpg->acl_node_lock);
  365. list_add_tail(&acl->acl_list, &tpg->acl_node_list);
  366. tpg->num_node_acls++;
  367. spin_unlock_irq(&tpg->acl_node_lock);
  368. done:
  369. pr_debug("%s_TPG[%hu] - Added ACL with TCQ Depth: %d for %s"
  370. " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  371. tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
  372. tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
  373. return acl;
  374. }
  375. EXPORT_SYMBOL(core_tpg_add_initiator_node_acl);
  376. /* core_tpg_del_initiator_node_acl():
  377. *
  378. *
  379. */
  380. int core_tpg_del_initiator_node_acl(
  381. struct se_portal_group *tpg,
  382. struct se_node_acl *acl,
  383. int force)
  384. {
  385. LIST_HEAD(sess_list);
  386. struct se_session *sess, *sess_tmp;
  387. unsigned long flags;
  388. int rc;
  389. spin_lock_irq(&tpg->acl_node_lock);
  390. if (acl->dynamic_node_acl) {
  391. acl->dynamic_node_acl = 0;
  392. }
  393. list_del(&acl->acl_list);
  394. tpg->num_node_acls--;
  395. spin_unlock_irq(&tpg->acl_node_lock);
  396. spin_lock_irqsave(&acl->nacl_sess_lock, flags);
  397. acl->acl_stop = 1;
  398. list_for_each_entry_safe(sess, sess_tmp, &acl->acl_sess_list,
  399. sess_acl_list) {
  400. if (sess->sess_tearing_down != 0)
  401. continue;
  402. target_get_session(sess);
  403. list_move(&sess->sess_acl_list, &sess_list);
  404. }
  405. spin_unlock_irqrestore(&acl->nacl_sess_lock, flags);
  406. list_for_each_entry_safe(sess, sess_tmp, &sess_list, sess_acl_list) {
  407. list_del(&sess->sess_acl_list);
  408. rc = tpg->se_tpg_tfo->shutdown_session(sess);
  409. target_put_session(sess);
  410. if (!rc)
  411. continue;
  412. target_put_session(sess);
  413. }
  414. target_put_nacl(acl);
  415. /*
  416. * Wait for last target_put_nacl() to complete in target_complete_nacl()
  417. * for active fabric session transport_deregister_session() callbacks.
  418. */
  419. wait_for_completion(&acl->acl_free_comp);
  420. core_tpg_wait_for_nacl_pr_ref(acl);
  421. core_clear_initiator_node_from_tpg(acl, tpg);
  422. core_free_device_list_for_node(acl, tpg);
  423. pr_debug("%s_TPG[%hu] - Deleted ACL with TCQ Depth: %d for %s"
  424. " Initiator Node: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  425. tpg->se_tpg_tfo->tpg_get_tag(tpg), acl->queue_depth,
  426. tpg->se_tpg_tfo->get_fabric_name(), acl->initiatorname);
  427. return 0;
  428. }
  429. EXPORT_SYMBOL(core_tpg_del_initiator_node_acl);
  430. /* core_tpg_set_initiator_node_queue_depth():
  431. *
  432. *
  433. */
  434. int core_tpg_set_initiator_node_queue_depth(
  435. struct se_portal_group *tpg,
  436. unsigned char *initiatorname,
  437. u32 queue_depth,
  438. int force)
  439. {
  440. struct se_session *sess, *init_sess = NULL;
  441. struct se_node_acl *acl;
  442. unsigned long flags;
  443. int dynamic_acl = 0;
  444. spin_lock_irq(&tpg->acl_node_lock);
  445. acl = __core_tpg_get_initiator_node_acl(tpg, initiatorname);
  446. if (!acl) {
  447. pr_err("Access Control List entry for %s Initiator"
  448. " Node %s does not exists for TPG %hu, ignoring"
  449. " request.\n", tpg->se_tpg_tfo->get_fabric_name(),
  450. initiatorname, tpg->se_tpg_tfo->tpg_get_tag(tpg));
  451. spin_unlock_irq(&tpg->acl_node_lock);
  452. return -ENODEV;
  453. }
  454. if (acl->dynamic_node_acl) {
  455. acl->dynamic_node_acl = 0;
  456. dynamic_acl = 1;
  457. }
  458. spin_unlock_irq(&tpg->acl_node_lock);
  459. spin_lock_irqsave(&tpg->session_lock, flags);
  460. list_for_each_entry(sess, &tpg->tpg_sess_list, sess_list) {
  461. if (sess->se_node_acl != acl)
  462. continue;
  463. if (!force) {
  464. pr_err("Unable to change queue depth for %s"
  465. " Initiator Node: %s while session is"
  466. " operational. To forcefully change the queue"
  467. " depth and force session reinstatement"
  468. " use the \"force=1\" parameter.\n",
  469. tpg->se_tpg_tfo->get_fabric_name(), initiatorname);
  470. spin_unlock_irqrestore(&tpg->session_lock, flags);
  471. spin_lock_irq(&tpg->acl_node_lock);
  472. if (dynamic_acl)
  473. acl->dynamic_node_acl = 1;
  474. spin_unlock_irq(&tpg->acl_node_lock);
  475. return -EEXIST;
  476. }
  477. /*
  478. * Determine if the session needs to be closed by our context.
  479. */
  480. if (!tpg->se_tpg_tfo->shutdown_session(sess))
  481. continue;
  482. init_sess = sess;
  483. break;
  484. }
  485. /*
  486. * User has requested to change the queue depth for a Initiator Node.
  487. * Change the value in the Node's struct se_node_acl, and call
  488. * core_set_queue_depth_for_node() to add the requested queue depth.
  489. *
  490. * Finally call tpg->se_tpg_tfo->close_session() to force session
  491. * reinstatement to occur if there is an active session for the
  492. * $FABRIC_MOD Initiator Node in question.
  493. */
  494. acl->queue_depth = queue_depth;
  495. if (core_set_queue_depth_for_node(tpg, acl) < 0) {
  496. spin_unlock_irqrestore(&tpg->session_lock, flags);
  497. /*
  498. * Force session reinstatement if
  499. * core_set_queue_depth_for_node() failed, because we assume
  500. * the $FABRIC_MOD has already the set session reinstatement
  501. * bit from tpg->se_tpg_tfo->shutdown_session() called above.
  502. */
  503. if (init_sess)
  504. tpg->se_tpg_tfo->close_session(init_sess);
  505. spin_lock_irq(&tpg->acl_node_lock);
  506. if (dynamic_acl)
  507. acl->dynamic_node_acl = 1;
  508. spin_unlock_irq(&tpg->acl_node_lock);
  509. return -EINVAL;
  510. }
  511. spin_unlock_irqrestore(&tpg->session_lock, flags);
  512. /*
  513. * If the $FABRIC_MOD session for the Initiator Node ACL exists,
  514. * forcefully shutdown the $FABRIC_MOD session/nexus.
  515. */
  516. if (init_sess)
  517. tpg->se_tpg_tfo->close_session(init_sess);
  518. pr_debug("Successfully changed queue depth to: %d for Initiator"
  519. " Node: %s on %s Target Portal Group: %u\n", queue_depth,
  520. initiatorname, tpg->se_tpg_tfo->get_fabric_name(),
  521. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  522. spin_lock_irq(&tpg->acl_node_lock);
  523. if (dynamic_acl)
  524. acl->dynamic_node_acl = 1;
  525. spin_unlock_irq(&tpg->acl_node_lock);
  526. return 0;
  527. }
  528. EXPORT_SYMBOL(core_tpg_set_initiator_node_queue_depth);
  529. /* core_tpg_set_initiator_node_tag():
  530. *
  531. * Initiator nodeacl tags are not used internally, but may be used by
  532. * userspace to emulate aliases or groups.
  533. * Returns length of newly-set tag or -EINVAL.
  534. */
  535. int core_tpg_set_initiator_node_tag(
  536. struct se_portal_group *tpg,
  537. struct se_node_acl *acl,
  538. const char *new_tag)
  539. {
  540. if (strlen(new_tag) >= MAX_ACL_TAG_SIZE)
  541. return -EINVAL;
  542. if (!strncmp("NULL", new_tag, 4)) {
  543. acl->acl_tag[0] = '\0';
  544. return 0;
  545. }
  546. return snprintf(acl->acl_tag, MAX_ACL_TAG_SIZE, "%s", new_tag);
  547. }
  548. EXPORT_SYMBOL(core_tpg_set_initiator_node_tag);
  549. static void core_tpg_lun_ref_release(struct percpu_ref *ref)
  550. {
  551. struct se_lun *lun = container_of(ref, struct se_lun, lun_ref);
  552. complete(&lun->lun_ref_comp);
  553. }
  554. static int core_tpg_setup_virtual_lun0(struct se_portal_group *se_tpg)
  555. {
  556. /* Set in core_dev_setup_virtual_lun0() */
  557. struct se_device *dev = g_lun0_dev;
  558. struct se_lun *lun = &se_tpg->tpg_virt_lun0;
  559. u32 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  560. int ret;
  561. lun->unpacked_lun = 0;
  562. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  563. atomic_set(&lun->lun_acl_count, 0);
  564. init_completion(&lun->lun_shutdown_comp);
  565. INIT_LIST_HEAD(&lun->lun_acl_list);
  566. spin_lock_init(&lun->lun_acl_lock);
  567. spin_lock_init(&lun->lun_sep_lock);
  568. init_completion(&lun->lun_ref_comp);
  569. ret = core_tpg_post_addlun(se_tpg, lun, lun_access, dev);
  570. if (ret < 0)
  571. return ret;
  572. return 0;
  573. }
  574. static void core_tpg_release_virtual_lun0(struct se_portal_group *se_tpg)
  575. {
  576. struct se_lun *lun = &se_tpg->tpg_virt_lun0;
  577. core_tpg_post_dellun(se_tpg, lun);
  578. }
  579. int core_tpg_register(
  580. struct target_core_fabric_ops *tfo,
  581. struct se_wwn *se_wwn,
  582. struct se_portal_group *se_tpg,
  583. void *tpg_fabric_ptr,
  584. int se_tpg_type)
  585. {
  586. struct se_lun *lun;
  587. u32 i;
  588. se_tpg->tpg_lun_list = array_zalloc(TRANSPORT_MAX_LUNS_PER_TPG,
  589. sizeof(struct se_lun), GFP_KERNEL);
  590. if (!se_tpg->tpg_lun_list) {
  591. pr_err("Unable to allocate struct se_portal_group->"
  592. "tpg_lun_list\n");
  593. return -ENOMEM;
  594. }
  595. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  596. lun = se_tpg->tpg_lun_list[i];
  597. lun->unpacked_lun = i;
  598. lun->lun_link_magic = SE_LUN_LINK_MAGIC;
  599. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  600. atomic_set(&lun->lun_acl_count, 0);
  601. init_completion(&lun->lun_shutdown_comp);
  602. INIT_LIST_HEAD(&lun->lun_acl_list);
  603. spin_lock_init(&lun->lun_acl_lock);
  604. spin_lock_init(&lun->lun_sep_lock);
  605. init_completion(&lun->lun_ref_comp);
  606. }
  607. se_tpg->se_tpg_type = se_tpg_type;
  608. se_tpg->se_tpg_fabric_ptr = tpg_fabric_ptr;
  609. se_tpg->se_tpg_tfo = tfo;
  610. se_tpg->se_tpg_wwn = se_wwn;
  611. atomic_set(&se_tpg->tpg_pr_ref_count, 0);
  612. INIT_LIST_HEAD(&se_tpg->acl_node_list);
  613. INIT_LIST_HEAD(&se_tpg->se_tpg_node);
  614. INIT_LIST_HEAD(&se_tpg->tpg_sess_list);
  615. spin_lock_init(&se_tpg->acl_node_lock);
  616. spin_lock_init(&se_tpg->session_lock);
  617. spin_lock_init(&se_tpg->tpg_lun_lock);
  618. if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) {
  619. if (core_tpg_setup_virtual_lun0(se_tpg) < 0) {
  620. array_free(se_tpg->tpg_lun_list,
  621. TRANSPORT_MAX_LUNS_PER_TPG);
  622. return -ENOMEM;
  623. }
  624. }
  625. spin_lock_bh(&tpg_lock);
  626. list_add_tail(&se_tpg->se_tpg_node, &tpg_list);
  627. spin_unlock_bh(&tpg_lock);
  628. pr_debug("TARGET_CORE[%s]: Allocated %s struct se_portal_group for"
  629. " endpoint: %s, Portal Tag: %u\n", tfo->get_fabric_name(),
  630. (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
  631. "Normal" : "Discovery", (tfo->tpg_get_wwn(se_tpg) == NULL) ?
  632. "None" : tfo->tpg_get_wwn(se_tpg), tfo->tpg_get_tag(se_tpg));
  633. return 0;
  634. }
  635. EXPORT_SYMBOL(core_tpg_register);
  636. int core_tpg_deregister(struct se_portal_group *se_tpg)
  637. {
  638. struct se_node_acl *nacl, *nacl_tmp;
  639. pr_debug("TARGET_CORE[%s]: Deallocating %s struct se_portal_group"
  640. " for endpoint: %s Portal Tag %u\n",
  641. (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL) ?
  642. "Normal" : "Discovery", se_tpg->se_tpg_tfo->get_fabric_name(),
  643. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
  644. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  645. spin_lock_bh(&tpg_lock);
  646. list_del(&se_tpg->se_tpg_node);
  647. spin_unlock_bh(&tpg_lock);
  648. while (atomic_read(&se_tpg->tpg_pr_ref_count) != 0)
  649. cpu_relax();
  650. /*
  651. * Release any remaining demo-mode generated se_node_acl that have
  652. * not been released because of TFO->tpg_check_demo_mode_cache() == 1
  653. * in transport_deregister_session().
  654. */
  655. spin_lock_irq(&se_tpg->acl_node_lock);
  656. list_for_each_entry_safe(nacl, nacl_tmp, &se_tpg->acl_node_list,
  657. acl_list) {
  658. list_del(&nacl->acl_list);
  659. se_tpg->num_node_acls--;
  660. spin_unlock_irq(&se_tpg->acl_node_lock);
  661. core_tpg_wait_for_nacl_pr_ref(nacl);
  662. core_free_device_list_for_node(nacl, se_tpg);
  663. se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, nacl);
  664. spin_lock_irq(&se_tpg->acl_node_lock);
  665. }
  666. spin_unlock_irq(&se_tpg->acl_node_lock);
  667. if (se_tpg->se_tpg_type == TRANSPORT_TPG_TYPE_NORMAL)
  668. core_tpg_release_virtual_lun0(se_tpg);
  669. se_tpg->se_tpg_fabric_ptr = NULL;
  670. array_free(se_tpg->tpg_lun_list, TRANSPORT_MAX_LUNS_PER_TPG);
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(core_tpg_deregister);
  674. struct se_lun *core_tpg_pre_addlun(
  675. struct se_portal_group *tpg,
  676. u32 unpacked_lun)
  677. {
  678. struct se_lun *lun;
  679. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  680. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER_TPG"
  681. "-1: %u for Target Portal Group: %u\n",
  682. tpg->se_tpg_tfo->get_fabric_name(),
  683. unpacked_lun, TRANSPORT_MAX_LUNS_PER_TPG-1,
  684. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  685. return ERR_PTR(-EOVERFLOW);
  686. }
  687. spin_lock(&tpg->tpg_lun_lock);
  688. lun = tpg->tpg_lun_list[unpacked_lun];
  689. if (lun->lun_status == TRANSPORT_LUN_STATUS_ACTIVE) {
  690. pr_err("TPG Logical Unit Number: %u is already active"
  691. " on %s Target Portal Group: %u, ignoring request.\n",
  692. unpacked_lun, tpg->se_tpg_tfo->get_fabric_name(),
  693. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  694. spin_unlock(&tpg->tpg_lun_lock);
  695. return ERR_PTR(-EINVAL);
  696. }
  697. spin_unlock(&tpg->tpg_lun_lock);
  698. return lun;
  699. }
  700. int core_tpg_post_addlun(
  701. struct se_portal_group *tpg,
  702. struct se_lun *lun,
  703. u32 lun_access,
  704. void *lun_ptr)
  705. {
  706. int ret;
  707. ret = percpu_ref_init(&lun->lun_ref, core_tpg_lun_ref_release);
  708. if (ret < 0)
  709. return ret;
  710. ret = core_dev_export(lun_ptr, tpg, lun);
  711. if (ret < 0) {
  712. percpu_ref_cancel_init(&lun->lun_ref);
  713. return ret;
  714. }
  715. spin_lock(&tpg->tpg_lun_lock);
  716. lun->lun_access = lun_access;
  717. lun->lun_status = TRANSPORT_LUN_STATUS_ACTIVE;
  718. spin_unlock(&tpg->tpg_lun_lock);
  719. return 0;
  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_clear_lun_from_tpg(lun, tpg);
  752. transport_clear_lun_ref(lun);
  753. core_dev_unexport(lun->lun_se_dev, tpg, lun);
  754. spin_lock(&tpg->tpg_lun_lock);
  755. lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
  756. spin_unlock(&tpg->tpg_lun_lock);
  757. return 0;
  758. }