iscsi_target_tpg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*******************************************************************************
  2. * This file contains iSCSI Target Portal Group related functions.
  3. *
  4. * (c) Copyright 2007-2013 Datera, Inc.
  5. *
  6. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. ******************************************************************************/
  18. #include <linux/slab.h>
  19. #include <target/target_core_base.h>
  20. #include <target/target_core_fabric.h>
  21. #include <target/iscsi/iscsi_target_core.h>
  22. #include "iscsi_target_erl0.h"
  23. #include "iscsi_target_login.h"
  24. #include "iscsi_target_nodeattrib.h"
  25. #include "iscsi_target_tpg.h"
  26. #include "iscsi_target_util.h"
  27. #include "iscsi_target.h"
  28. #include "iscsi_target_parameters.h"
  29. #include <target/iscsi/iscsi_transport.h>
  30. struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
  31. {
  32. struct iscsi_portal_group *tpg;
  33. tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
  34. if (!tpg) {
  35. pr_err("Unable to allocate struct iscsi_portal_group\n");
  36. return NULL;
  37. }
  38. tpg->tpgt = tpgt;
  39. tpg->tpg_state = TPG_STATE_FREE;
  40. tpg->tpg_tiqn = tiqn;
  41. INIT_LIST_HEAD(&tpg->tpg_gnp_list);
  42. INIT_LIST_HEAD(&tpg->tpg_list);
  43. mutex_init(&tpg->tpg_access_lock);
  44. sema_init(&tpg->np_login_sem, 1);
  45. spin_lock_init(&tpg->tpg_state_lock);
  46. spin_lock_init(&tpg->tpg_np_lock);
  47. return tpg;
  48. }
  49. static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
  50. int iscsit_load_discovery_tpg(void)
  51. {
  52. struct iscsi_param *param;
  53. struct iscsi_portal_group *tpg;
  54. int ret;
  55. tpg = iscsit_alloc_portal_group(NULL, 1);
  56. if (!tpg) {
  57. pr_err("Unable to allocate struct iscsi_portal_group\n");
  58. return -1;
  59. }
  60. /*
  61. * Save iscsi_ops pointer for special case discovery TPG that
  62. * doesn't exist as se_wwn->wwn_group within configfs.
  63. */
  64. tpg->tpg_se_tpg.se_tpg_tfo = &iscsi_ops;
  65. ret = core_tpg_register(NULL, &tpg->tpg_se_tpg, -1);
  66. if (ret < 0) {
  67. kfree(tpg);
  68. return -1;
  69. }
  70. tpg->sid = 1; /* First Assigned LIO Session ID */
  71. iscsit_set_default_tpg_attribs(tpg);
  72. if (iscsi_create_default_params(&tpg->param_list) < 0)
  73. goto out;
  74. /*
  75. * By default we disable authentication for discovery sessions,
  76. * this can be changed with:
  77. *
  78. * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
  79. */
  80. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  81. if (!param)
  82. goto free_pl_out;
  83. if (iscsi_update_param_value(param, "CHAP,None") < 0)
  84. goto free_pl_out;
  85. tpg->tpg_attrib.authentication = 0;
  86. spin_lock(&tpg->tpg_state_lock);
  87. tpg->tpg_state = TPG_STATE_ACTIVE;
  88. spin_unlock(&tpg->tpg_state_lock);
  89. iscsit_global->discovery_tpg = tpg;
  90. pr_debug("CORE[0] - Allocated Discovery TPG\n");
  91. return 0;
  92. free_pl_out:
  93. iscsi_release_param_list(tpg->param_list);
  94. out:
  95. if (tpg->sid == 1)
  96. core_tpg_deregister(&tpg->tpg_se_tpg);
  97. kfree(tpg);
  98. return -1;
  99. }
  100. void iscsit_release_discovery_tpg(void)
  101. {
  102. struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
  103. if (!tpg)
  104. return;
  105. iscsi_release_param_list(tpg->param_list);
  106. core_tpg_deregister(&tpg->tpg_se_tpg);
  107. kfree(tpg);
  108. iscsit_global->discovery_tpg = NULL;
  109. }
  110. struct iscsi_portal_group *iscsit_get_tpg_from_np(
  111. struct iscsi_tiqn *tiqn,
  112. struct iscsi_np *np,
  113. struct iscsi_tpg_np **tpg_np_out)
  114. {
  115. struct iscsi_portal_group *tpg = NULL;
  116. struct iscsi_tpg_np *tpg_np;
  117. spin_lock(&tiqn->tiqn_tpg_lock);
  118. list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
  119. spin_lock(&tpg->tpg_state_lock);
  120. if (tpg->tpg_state != TPG_STATE_ACTIVE) {
  121. spin_unlock(&tpg->tpg_state_lock);
  122. continue;
  123. }
  124. spin_unlock(&tpg->tpg_state_lock);
  125. spin_lock(&tpg->tpg_np_lock);
  126. list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
  127. if (tpg_np->tpg_np == np) {
  128. *tpg_np_out = tpg_np;
  129. kref_get(&tpg_np->tpg_np_kref);
  130. spin_unlock(&tpg->tpg_np_lock);
  131. spin_unlock(&tiqn->tiqn_tpg_lock);
  132. return tpg;
  133. }
  134. }
  135. spin_unlock(&tpg->tpg_np_lock);
  136. }
  137. spin_unlock(&tiqn->tiqn_tpg_lock);
  138. return NULL;
  139. }
  140. int iscsit_get_tpg(
  141. struct iscsi_portal_group *tpg)
  142. {
  143. return mutex_lock_interruptible(&tpg->tpg_access_lock);
  144. }
  145. void iscsit_put_tpg(struct iscsi_portal_group *tpg)
  146. {
  147. mutex_unlock(&tpg->tpg_access_lock);
  148. }
  149. static void iscsit_clear_tpg_np_login_thread(
  150. struct iscsi_tpg_np *tpg_np,
  151. struct iscsi_portal_group *tpg,
  152. bool shutdown)
  153. {
  154. if (!tpg_np->tpg_np) {
  155. pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
  156. return;
  157. }
  158. if (shutdown)
  159. tpg_np->tpg_np->enabled = false;
  160. iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
  161. }
  162. static void iscsit_clear_tpg_np_login_threads(
  163. struct iscsi_portal_group *tpg,
  164. bool shutdown)
  165. {
  166. struct iscsi_tpg_np *tpg_np;
  167. spin_lock(&tpg->tpg_np_lock);
  168. list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
  169. if (!tpg_np->tpg_np) {
  170. pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
  171. continue;
  172. }
  173. spin_unlock(&tpg->tpg_np_lock);
  174. iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
  175. spin_lock(&tpg->tpg_np_lock);
  176. }
  177. spin_unlock(&tpg->tpg_np_lock);
  178. }
  179. void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
  180. {
  181. iscsi_print_params(tpg->param_list);
  182. }
  183. static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
  184. {
  185. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  186. a->authentication = TA_AUTHENTICATION;
  187. a->login_timeout = TA_LOGIN_TIMEOUT;
  188. a->netif_timeout = TA_NETIF_TIMEOUT;
  189. a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
  190. a->generate_node_acls = TA_GENERATE_NODE_ACLS;
  191. a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
  192. a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
  193. a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
  194. a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
  195. a->default_erl = TA_DEFAULT_ERL;
  196. a->t10_pi = TA_DEFAULT_T10_PI;
  197. a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
  198. a->tpg_enabled_sendtargets = TA_DEFAULT_TPG_ENABLED_SENDTARGETS;
  199. a->login_keys_workaround = TA_DEFAULT_LOGIN_KEYS_WORKAROUND;
  200. }
  201. int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
  202. {
  203. if (tpg->tpg_state != TPG_STATE_FREE) {
  204. pr_err("Unable to add iSCSI Target Portal Group: %d"
  205. " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
  206. return -EEXIST;
  207. }
  208. iscsit_set_default_tpg_attribs(tpg);
  209. if (iscsi_create_default_params(&tpg->param_list) < 0)
  210. goto err_out;
  211. tpg->tpg_attrib.tpg = tpg;
  212. spin_lock(&tpg->tpg_state_lock);
  213. tpg->tpg_state = TPG_STATE_INACTIVE;
  214. spin_unlock(&tpg->tpg_state_lock);
  215. spin_lock(&tiqn->tiqn_tpg_lock);
  216. list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
  217. tiqn->tiqn_ntpgs++;
  218. pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
  219. tiqn->tiqn, tpg->tpgt);
  220. spin_unlock(&tiqn->tiqn_tpg_lock);
  221. return 0;
  222. err_out:
  223. if (tpg->param_list) {
  224. iscsi_release_param_list(tpg->param_list);
  225. tpg->param_list = NULL;
  226. }
  227. return -ENOMEM;
  228. }
  229. int iscsit_tpg_del_portal_group(
  230. struct iscsi_tiqn *tiqn,
  231. struct iscsi_portal_group *tpg,
  232. int force)
  233. {
  234. u8 old_state = tpg->tpg_state;
  235. spin_lock(&tpg->tpg_state_lock);
  236. tpg->tpg_state = TPG_STATE_INACTIVE;
  237. spin_unlock(&tpg->tpg_state_lock);
  238. if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
  239. pr_err("Unable to delete iSCSI Target Portal Group:"
  240. " %hu while active sessions exist, and force=0\n",
  241. tpg->tpgt);
  242. tpg->tpg_state = old_state;
  243. return -EPERM;
  244. }
  245. if (tpg->param_list) {
  246. iscsi_release_param_list(tpg->param_list);
  247. tpg->param_list = NULL;
  248. }
  249. core_tpg_deregister(&tpg->tpg_se_tpg);
  250. spin_lock(&tpg->tpg_state_lock);
  251. tpg->tpg_state = TPG_STATE_FREE;
  252. spin_unlock(&tpg->tpg_state_lock);
  253. spin_lock(&tiqn->tiqn_tpg_lock);
  254. tiqn->tiqn_ntpgs--;
  255. list_del(&tpg->tpg_list);
  256. spin_unlock(&tiqn->tiqn_tpg_lock);
  257. pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
  258. tiqn->tiqn, tpg->tpgt);
  259. kfree(tpg);
  260. return 0;
  261. }
  262. int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
  263. {
  264. struct iscsi_param *param;
  265. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  266. int ret;
  267. if (tpg->tpg_state == TPG_STATE_ACTIVE) {
  268. pr_err("iSCSI target portal group: %hu is already"
  269. " active, ignoring request.\n", tpg->tpgt);
  270. return -EINVAL;
  271. }
  272. /*
  273. * Make sure that AuthMethod does not contain None as an option
  274. * unless explictly disabled. Set the default to CHAP if authentication
  275. * is enforced (as per default), and remove the NONE option.
  276. */
  277. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  278. if (!param)
  279. return -EINVAL;
  280. if (tpg->tpg_attrib.authentication) {
  281. if (!strcmp(param->value, NONE)) {
  282. ret = iscsi_update_param_value(param, CHAP);
  283. if (ret)
  284. goto err;
  285. }
  286. ret = iscsit_ta_authentication(tpg, 1);
  287. if (ret < 0)
  288. goto err;
  289. }
  290. spin_lock(&tpg->tpg_state_lock);
  291. tpg->tpg_state = TPG_STATE_ACTIVE;
  292. spin_unlock(&tpg->tpg_state_lock);
  293. spin_lock(&tiqn->tiqn_tpg_lock);
  294. tiqn->tiqn_active_tpgs++;
  295. pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
  296. tpg->tpgt);
  297. spin_unlock(&tiqn->tiqn_tpg_lock);
  298. return 0;
  299. err:
  300. return ret;
  301. }
  302. int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
  303. {
  304. struct iscsi_tiqn *tiqn;
  305. u8 old_state = tpg->tpg_state;
  306. spin_lock(&tpg->tpg_state_lock);
  307. if (tpg->tpg_state == TPG_STATE_INACTIVE) {
  308. pr_err("iSCSI Target Portal Group: %hu is already"
  309. " inactive, ignoring request.\n", tpg->tpgt);
  310. spin_unlock(&tpg->tpg_state_lock);
  311. return -EINVAL;
  312. }
  313. tpg->tpg_state = TPG_STATE_INACTIVE;
  314. spin_unlock(&tpg->tpg_state_lock);
  315. iscsit_clear_tpg_np_login_threads(tpg, false);
  316. if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
  317. spin_lock(&tpg->tpg_state_lock);
  318. tpg->tpg_state = old_state;
  319. spin_unlock(&tpg->tpg_state_lock);
  320. pr_err("Unable to disable iSCSI Target Portal Group:"
  321. " %hu while active sessions exist, and force=0\n",
  322. tpg->tpgt);
  323. return -EPERM;
  324. }
  325. tiqn = tpg->tpg_tiqn;
  326. if (!tiqn || (tpg == iscsit_global->discovery_tpg))
  327. return 0;
  328. spin_lock(&tiqn->tiqn_tpg_lock);
  329. tiqn->tiqn_active_tpgs--;
  330. pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
  331. tpg->tpgt);
  332. spin_unlock(&tiqn->tiqn_tpg_lock);
  333. return 0;
  334. }
  335. struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
  336. struct iscsi_session *sess)
  337. {
  338. struct se_session *se_sess = sess->se_sess;
  339. struct se_node_acl *se_nacl = se_sess->se_node_acl;
  340. struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
  341. se_node_acl);
  342. return &acl->node_attrib;
  343. }
  344. struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
  345. struct iscsi_tpg_np *tpg_np,
  346. int network_transport)
  347. {
  348. struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
  349. spin_lock(&tpg_np->tpg_np_parent_lock);
  350. list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
  351. &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
  352. if (tpg_np_child->tpg_np->np_network_transport ==
  353. network_transport) {
  354. spin_unlock(&tpg_np->tpg_np_parent_lock);
  355. return tpg_np_child;
  356. }
  357. }
  358. spin_unlock(&tpg_np->tpg_np_parent_lock);
  359. return NULL;
  360. }
  361. static bool iscsit_tpg_check_network_portal(
  362. struct iscsi_tiqn *tiqn,
  363. struct sockaddr_storage *sockaddr,
  364. int network_transport)
  365. {
  366. struct iscsi_portal_group *tpg;
  367. struct iscsi_tpg_np *tpg_np;
  368. struct iscsi_np *np;
  369. bool match = false;
  370. spin_lock(&tiqn->tiqn_tpg_lock);
  371. list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
  372. spin_lock(&tpg->tpg_np_lock);
  373. list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
  374. np = tpg_np->tpg_np;
  375. match = iscsit_check_np_match(sockaddr, np,
  376. network_transport);
  377. if (match)
  378. break;
  379. }
  380. spin_unlock(&tpg->tpg_np_lock);
  381. }
  382. spin_unlock(&tiqn->tiqn_tpg_lock);
  383. return match;
  384. }
  385. struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
  386. struct iscsi_portal_group *tpg,
  387. struct sockaddr_storage *sockaddr,
  388. struct iscsi_tpg_np *tpg_np_parent,
  389. int network_transport)
  390. {
  391. struct iscsi_np *np;
  392. struct iscsi_tpg_np *tpg_np;
  393. if (!tpg_np_parent) {
  394. if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
  395. network_transport)) {
  396. pr_err("Network Portal: %pISc already exists on a"
  397. " different TPG on %s\n", sockaddr,
  398. tpg->tpg_tiqn->tiqn);
  399. return ERR_PTR(-EEXIST);
  400. }
  401. }
  402. tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
  403. if (!tpg_np) {
  404. pr_err("Unable to allocate memory for"
  405. " struct iscsi_tpg_np.\n");
  406. return ERR_PTR(-ENOMEM);
  407. }
  408. np = iscsit_add_np(sockaddr, network_transport);
  409. if (IS_ERR(np)) {
  410. kfree(tpg_np);
  411. return ERR_CAST(np);
  412. }
  413. INIT_LIST_HEAD(&tpg_np->tpg_np_list);
  414. INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
  415. INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
  416. spin_lock_init(&tpg_np->tpg_np_parent_lock);
  417. init_completion(&tpg_np->tpg_np_comp);
  418. kref_init(&tpg_np->tpg_np_kref);
  419. tpg_np->tpg_np = np;
  420. tpg_np->tpg = tpg;
  421. spin_lock(&tpg->tpg_np_lock);
  422. list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
  423. tpg->num_tpg_nps++;
  424. if (tpg->tpg_tiqn)
  425. tpg->tpg_tiqn->tiqn_num_tpg_nps++;
  426. spin_unlock(&tpg->tpg_np_lock);
  427. if (tpg_np_parent) {
  428. tpg_np->tpg_np_parent = tpg_np_parent;
  429. spin_lock(&tpg_np_parent->tpg_np_parent_lock);
  430. list_add_tail(&tpg_np->tpg_np_child_list,
  431. &tpg_np_parent->tpg_np_parent_list);
  432. spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
  433. }
  434. pr_debug("CORE[%s] - Added Network Portal: %pISpc,%hu on %s\n",
  435. tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
  436. np->np_transport->name);
  437. return tpg_np;
  438. }
  439. static int iscsit_tpg_release_np(
  440. struct iscsi_tpg_np *tpg_np,
  441. struct iscsi_portal_group *tpg,
  442. struct iscsi_np *np)
  443. {
  444. iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
  445. pr_debug("CORE[%s] - Removed Network Portal: %pISpc,%hu on %s\n",
  446. tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
  447. np->np_transport->name);
  448. tpg_np->tpg_np = NULL;
  449. tpg_np->tpg = NULL;
  450. kfree(tpg_np);
  451. /*
  452. * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
  453. */
  454. return iscsit_del_np(np);
  455. }
  456. int iscsit_tpg_del_network_portal(
  457. struct iscsi_portal_group *tpg,
  458. struct iscsi_tpg_np *tpg_np)
  459. {
  460. struct iscsi_np *np;
  461. struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
  462. int ret = 0;
  463. np = tpg_np->tpg_np;
  464. if (!np) {
  465. pr_err("Unable to locate struct iscsi_np from"
  466. " struct iscsi_tpg_np\n");
  467. return -EINVAL;
  468. }
  469. if (!tpg_np->tpg_np_parent) {
  470. /*
  471. * We are the parent tpg network portal. Release all of the
  472. * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
  473. * list first.
  474. */
  475. list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
  476. &tpg_np->tpg_np_parent_list,
  477. tpg_np_child_list) {
  478. ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
  479. if (ret < 0)
  480. pr_err("iscsit_tpg_del_network_portal()"
  481. " failed: %d\n", ret);
  482. }
  483. } else {
  484. /*
  485. * We are not the parent ISCSI_TCP tpg network portal. Release
  486. * our own network portals from the child list.
  487. */
  488. spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
  489. list_del(&tpg_np->tpg_np_child_list);
  490. spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
  491. }
  492. spin_lock(&tpg->tpg_np_lock);
  493. list_del(&tpg_np->tpg_np_list);
  494. tpg->num_tpg_nps--;
  495. if (tpg->tpg_tiqn)
  496. tpg->tpg_tiqn->tiqn_num_tpg_nps--;
  497. spin_unlock(&tpg->tpg_np_lock);
  498. return iscsit_tpg_release_np(tpg_np, tpg, np);
  499. }
  500. int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
  501. {
  502. unsigned char buf1[256], buf2[256], *none = NULL;
  503. int len;
  504. struct iscsi_param *param;
  505. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  506. if ((authentication != 1) && (authentication != 0)) {
  507. pr_err("Illegal value for authentication parameter:"
  508. " %u, ignoring request.\n", authentication);
  509. return -EINVAL;
  510. }
  511. memset(buf1, 0, sizeof(buf1));
  512. memset(buf2, 0, sizeof(buf2));
  513. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  514. if (!param)
  515. return -EINVAL;
  516. if (authentication) {
  517. snprintf(buf1, sizeof(buf1), "%s", param->value);
  518. none = strstr(buf1, NONE);
  519. if (!none)
  520. goto out;
  521. if (!strncmp(none + 4, ",", 1)) {
  522. if (!strcmp(buf1, none))
  523. sprintf(buf2, "%s", none+5);
  524. else {
  525. none--;
  526. *none = '\0';
  527. len = sprintf(buf2, "%s", buf1);
  528. none += 5;
  529. sprintf(buf2 + len, "%s", none);
  530. }
  531. } else {
  532. none--;
  533. *none = '\0';
  534. sprintf(buf2, "%s", buf1);
  535. }
  536. if (iscsi_update_param_value(param, buf2) < 0)
  537. return -EINVAL;
  538. } else {
  539. snprintf(buf1, sizeof(buf1), "%s", param->value);
  540. none = strstr(buf1, NONE);
  541. if (none)
  542. goto out;
  543. strlcat(buf1, "," NONE, sizeof(buf1));
  544. if (iscsi_update_param_value(param, buf1) < 0)
  545. return -EINVAL;
  546. }
  547. out:
  548. a->authentication = authentication;
  549. pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
  550. a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
  551. return 0;
  552. }
  553. int iscsit_ta_login_timeout(
  554. struct iscsi_portal_group *tpg,
  555. u32 login_timeout)
  556. {
  557. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  558. if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
  559. pr_err("Requested Login Timeout %u larger than maximum"
  560. " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
  561. return -EINVAL;
  562. } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
  563. pr_err("Requested Logout Timeout %u smaller than"
  564. " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
  565. return -EINVAL;
  566. }
  567. a->login_timeout = login_timeout;
  568. pr_debug("Set Logout Timeout to %u for Target Portal Group"
  569. " %hu\n", a->login_timeout, tpg->tpgt);
  570. return 0;
  571. }
  572. int iscsit_ta_netif_timeout(
  573. struct iscsi_portal_group *tpg,
  574. u32 netif_timeout)
  575. {
  576. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  577. if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
  578. pr_err("Requested Network Interface Timeout %u larger"
  579. " than maximum %u\n", netif_timeout,
  580. TA_NETIF_TIMEOUT_MAX);
  581. return -EINVAL;
  582. } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
  583. pr_err("Requested Network Interface Timeout %u smaller"
  584. " than minimum %u\n", netif_timeout,
  585. TA_NETIF_TIMEOUT_MIN);
  586. return -EINVAL;
  587. }
  588. a->netif_timeout = netif_timeout;
  589. pr_debug("Set Network Interface Timeout to %u for"
  590. " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
  591. return 0;
  592. }
  593. int iscsit_ta_generate_node_acls(
  594. struct iscsi_portal_group *tpg,
  595. u32 flag)
  596. {
  597. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  598. if ((flag != 0) && (flag != 1)) {
  599. pr_err("Illegal value %d\n", flag);
  600. return -EINVAL;
  601. }
  602. a->generate_node_acls = flag;
  603. pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
  604. tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
  605. if (flag == 1 && a->cache_dynamic_acls == 0) {
  606. pr_debug("Explicitly setting cache_dynamic_acls=1 when "
  607. "generate_node_acls=1\n");
  608. a->cache_dynamic_acls = 1;
  609. }
  610. return 0;
  611. }
  612. int iscsit_ta_default_cmdsn_depth(
  613. struct iscsi_portal_group *tpg,
  614. u32 tcq_depth)
  615. {
  616. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  617. if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
  618. pr_err("Requested Default Queue Depth: %u larger"
  619. " than maximum %u\n", tcq_depth,
  620. TA_DEFAULT_CMDSN_DEPTH_MAX);
  621. return -EINVAL;
  622. } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
  623. pr_err("Requested Default Queue Depth: %u smaller"
  624. " than minimum %u\n", tcq_depth,
  625. TA_DEFAULT_CMDSN_DEPTH_MIN);
  626. return -EINVAL;
  627. }
  628. a->default_cmdsn_depth = tcq_depth;
  629. pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
  630. tpg->tpgt, a->default_cmdsn_depth);
  631. return 0;
  632. }
  633. int iscsit_ta_cache_dynamic_acls(
  634. struct iscsi_portal_group *tpg,
  635. u32 flag)
  636. {
  637. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  638. if ((flag != 0) && (flag != 1)) {
  639. pr_err("Illegal value %d\n", flag);
  640. return -EINVAL;
  641. }
  642. if (a->generate_node_acls == 1 && flag == 0) {
  643. pr_debug("Skipping cache_dynamic_acls=0 when"
  644. " generate_node_acls=1\n");
  645. return 0;
  646. }
  647. a->cache_dynamic_acls = flag;
  648. pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
  649. " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
  650. "Enabled" : "Disabled");
  651. return 0;
  652. }
  653. int iscsit_ta_demo_mode_write_protect(
  654. struct iscsi_portal_group *tpg,
  655. u32 flag)
  656. {
  657. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  658. if ((flag != 0) && (flag != 1)) {
  659. pr_err("Illegal value %d\n", flag);
  660. return -EINVAL;
  661. }
  662. a->demo_mode_write_protect = flag;
  663. pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
  664. tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
  665. return 0;
  666. }
  667. int iscsit_ta_prod_mode_write_protect(
  668. struct iscsi_portal_group *tpg,
  669. u32 flag)
  670. {
  671. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  672. if ((flag != 0) && (flag != 1)) {
  673. pr_err("Illegal value %d\n", flag);
  674. return -EINVAL;
  675. }
  676. a->prod_mode_write_protect = flag;
  677. pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
  678. " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
  679. "ON" : "OFF");
  680. return 0;
  681. }
  682. int iscsit_ta_demo_mode_discovery(
  683. struct iscsi_portal_group *tpg,
  684. u32 flag)
  685. {
  686. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  687. if ((flag != 0) && (flag != 1)) {
  688. pr_err("Illegal value %d\n", flag);
  689. return -EINVAL;
  690. }
  691. a->demo_mode_discovery = flag;
  692. pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
  693. " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
  694. "ON" : "OFF");
  695. return 0;
  696. }
  697. int iscsit_ta_default_erl(
  698. struct iscsi_portal_group *tpg,
  699. u32 default_erl)
  700. {
  701. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  702. if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
  703. pr_err("Illegal value for default_erl: %u\n", default_erl);
  704. return -EINVAL;
  705. }
  706. a->default_erl = default_erl;
  707. pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
  708. return 0;
  709. }
  710. int iscsit_ta_t10_pi(
  711. struct iscsi_portal_group *tpg,
  712. u32 flag)
  713. {
  714. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  715. if ((flag != 0) && (flag != 1)) {
  716. pr_err("Illegal value %d\n", flag);
  717. return -EINVAL;
  718. }
  719. a->t10_pi = flag;
  720. pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
  721. " %s\n", tpg->tpgt, (a->t10_pi) ?
  722. "ON" : "OFF");
  723. return 0;
  724. }
  725. int iscsit_ta_fabric_prot_type(
  726. struct iscsi_portal_group *tpg,
  727. u32 prot_type)
  728. {
  729. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  730. if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
  731. pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
  732. return -EINVAL;
  733. }
  734. a->fabric_prot_type = prot_type;
  735. pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
  736. tpg->tpgt, prot_type);
  737. return 0;
  738. }
  739. int iscsit_ta_tpg_enabled_sendtargets(
  740. struct iscsi_portal_group *tpg,
  741. u32 flag)
  742. {
  743. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  744. if ((flag != 0) && (flag != 1)) {
  745. pr_err("Illegal value %d\n", flag);
  746. return -EINVAL;
  747. }
  748. a->tpg_enabled_sendtargets = flag;
  749. pr_debug("iSCSI_TPG[%hu] - TPG enabled bit required for SendTargets:"
  750. " %s\n", tpg->tpgt, (a->tpg_enabled_sendtargets) ? "ON" : "OFF");
  751. return 0;
  752. }
  753. int iscsit_ta_login_keys_workaround(
  754. struct iscsi_portal_group *tpg,
  755. u32 flag)
  756. {
  757. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  758. if ((flag != 0) && (flag != 1)) {
  759. pr_err("Illegal value %d\n", flag);
  760. return -EINVAL;
  761. }
  762. a->login_keys_workaround = flag;
  763. pr_debug("iSCSI_TPG[%hu] - TPG enabled bit for login keys workaround: %s ",
  764. tpg->tpgt, (a->login_keys_workaround) ? "ON" : "OFF");
  765. return 0;
  766. }