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 <target/target_core_base.h>
  19. #include <target/target_core_fabric.h>
  20. #include <target/iscsi/iscsi_target_core.h>
  21. #include "iscsi_target_erl0.h"
  22. #include "iscsi_target_login.h"
  23. #include "iscsi_target_nodeattrib.h"
  24. #include "iscsi_target_tpg.h"
  25. #include "iscsi_target_util.h"
  26. #include "iscsi_target.h"
  27. #include "iscsi_target_parameters.h"
  28. #include <target/iscsi/iscsi_transport.h>
  29. struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
  30. {
  31. struct iscsi_portal_group *tpg;
  32. tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
  33. if (!tpg) {
  34. pr_err("Unable to allocate struct iscsi_portal_group\n");
  35. return NULL;
  36. }
  37. tpg->tpgt = tpgt;
  38. tpg->tpg_state = TPG_STATE_FREE;
  39. tpg->tpg_tiqn = tiqn;
  40. INIT_LIST_HEAD(&tpg->tpg_gnp_list);
  41. INIT_LIST_HEAD(&tpg->tpg_list);
  42. mutex_init(&tpg->tpg_access_lock);
  43. sema_init(&tpg->np_login_sem, 1);
  44. spin_lock_init(&tpg->tpg_state_lock);
  45. spin_lock_init(&tpg->tpg_np_lock);
  46. return tpg;
  47. }
  48. static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
  49. int iscsit_load_discovery_tpg(void)
  50. {
  51. struct iscsi_param *param;
  52. struct iscsi_portal_group *tpg;
  53. int ret;
  54. tpg = iscsit_alloc_portal_group(NULL, 1);
  55. if (!tpg) {
  56. pr_err("Unable to allocate struct iscsi_portal_group\n");
  57. return -1;
  58. }
  59. /*
  60. * Save iscsi_ops pointer for special case discovery TPG that
  61. * doesn't exist as se_wwn->wwn_group within configfs.
  62. */
  63. tpg->tpg_se_tpg.se_tpg_tfo = &iscsi_ops;
  64. ret = core_tpg_register(NULL, &tpg->tpg_se_tpg, -1);
  65. if (ret < 0) {
  66. kfree(tpg);
  67. return -1;
  68. }
  69. tpg->sid = 1; /* First Assigned LIO Session ID */
  70. iscsit_set_default_tpg_attribs(tpg);
  71. if (iscsi_create_default_params(&tpg->param_list) < 0)
  72. goto out;
  73. /*
  74. * By default we disable authentication for discovery sessions,
  75. * this can be changed with:
  76. *
  77. * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
  78. */
  79. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  80. if (!param)
  81. goto out;
  82. if (iscsi_update_param_value(param, "CHAP,None") < 0)
  83. goto out;
  84. tpg->tpg_attrib.authentication = 0;
  85. spin_lock(&tpg->tpg_state_lock);
  86. tpg->tpg_state = TPG_STATE_ACTIVE;
  87. spin_unlock(&tpg->tpg_state_lock);
  88. iscsit_global->discovery_tpg = tpg;
  89. pr_debug("CORE[0] - Allocated Discovery TPG\n");
  90. return 0;
  91. out:
  92. if (tpg->sid == 1)
  93. core_tpg_deregister(&tpg->tpg_se_tpg);
  94. kfree(tpg);
  95. return -1;
  96. }
  97. void iscsit_release_discovery_tpg(void)
  98. {
  99. struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
  100. if (!tpg)
  101. return;
  102. core_tpg_deregister(&tpg->tpg_se_tpg);
  103. kfree(tpg);
  104. iscsit_global->discovery_tpg = NULL;
  105. }
  106. struct iscsi_portal_group *iscsit_get_tpg_from_np(
  107. struct iscsi_tiqn *tiqn,
  108. struct iscsi_np *np,
  109. struct iscsi_tpg_np **tpg_np_out)
  110. {
  111. struct iscsi_portal_group *tpg = NULL;
  112. struct iscsi_tpg_np *tpg_np;
  113. spin_lock(&tiqn->tiqn_tpg_lock);
  114. list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
  115. spin_lock(&tpg->tpg_state_lock);
  116. if (tpg->tpg_state != TPG_STATE_ACTIVE) {
  117. spin_unlock(&tpg->tpg_state_lock);
  118. continue;
  119. }
  120. spin_unlock(&tpg->tpg_state_lock);
  121. spin_lock(&tpg->tpg_np_lock);
  122. list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
  123. if (tpg_np->tpg_np == np) {
  124. *tpg_np_out = tpg_np;
  125. kref_get(&tpg_np->tpg_np_kref);
  126. spin_unlock(&tpg->tpg_np_lock);
  127. spin_unlock(&tiqn->tiqn_tpg_lock);
  128. return tpg;
  129. }
  130. }
  131. spin_unlock(&tpg->tpg_np_lock);
  132. }
  133. spin_unlock(&tiqn->tiqn_tpg_lock);
  134. return NULL;
  135. }
  136. int iscsit_get_tpg(
  137. struct iscsi_portal_group *tpg)
  138. {
  139. return mutex_lock_interruptible(&tpg->tpg_access_lock);
  140. }
  141. void iscsit_put_tpg(struct iscsi_portal_group *tpg)
  142. {
  143. mutex_unlock(&tpg->tpg_access_lock);
  144. }
  145. static void iscsit_clear_tpg_np_login_thread(
  146. struct iscsi_tpg_np *tpg_np,
  147. struct iscsi_portal_group *tpg,
  148. bool shutdown)
  149. {
  150. if (!tpg_np->tpg_np) {
  151. pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
  152. return;
  153. }
  154. if (shutdown)
  155. tpg_np->tpg_np->enabled = false;
  156. iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
  157. }
  158. static void iscsit_clear_tpg_np_login_threads(
  159. struct iscsi_portal_group *tpg,
  160. bool shutdown)
  161. {
  162. struct iscsi_tpg_np *tpg_np;
  163. spin_lock(&tpg->tpg_np_lock);
  164. list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
  165. if (!tpg_np->tpg_np) {
  166. pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
  167. continue;
  168. }
  169. spin_unlock(&tpg->tpg_np_lock);
  170. iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
  171. spin_lock(&tpg->tpg_np_lock);
  172. }
  173. spin_unlock(&tpg->tpg_np_lock);
  174. }
  175. void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
  176. {
  177. iscsi_print_params(tpg->param_list);
  178. }
  179. static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
  180. {
  181. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  182. a->authentication = TA_AUTHENTICATION;
  183. a->login_timeout = TA_LOGIN_TIMEOUT;
  184. a->netif_timeout = TA_NETIF_TIMEOUT;
  185. a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
  186. a->generate_node_acls = TA_GENERATE_NODE_ACLS;
  187. a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
  188. a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
  189. a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
  190. a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
  191. a->default_erl = TA_DEFAULT_ERL;
  192. a->t10_pi = TA_DEFAULT_T10_PI;
  193. a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
  194. a->tpg_enabled_sendtargets = TA_DEFAULT_TPG_ENABLED_SENDTARGETS;
  195. a->login_keys_workaround = TA_DEFAULT_LOGIN_KEYS_WORKAROUND;
  196. }
  197. int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
  198. {
  199. if (tpg->tpg_state != TPG_STATE_FREE) {
  200. pr_err("Unable to add iSCSI Target Portal Group: %d"
  201. " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
  202. return -EEXIST;
  203. }
  204. iscsit_set_default_tpg_attribs(tpg);
  205. if (iscsi_create_default_params(&tpg->param_list) < 0)
  206. goto err_out;
  207. tpg->tpg_attrib.tpg = tpg;
  208. spin_lock(&tpg->tpg_state_lock);
  209. tpg->tpg_state = TPG_STATE_INACTIVE;
  210. spin_unlock(&tpg->tpg_state_lock);
  211. spin_lock(&tiqn->tiqn_tpg_lock);
  212. list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
  213. tiqn->tiqn_ntpgs++;
  214. pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
  215. tiqn->tiqn, tpg->tpgt);
  216. spin_unlock(&tiqn->tiqn_tpg_lock);
  217. return 0;
  218. err_out:
  219. if (tpg->param_list) {
  220. iscsi_release_param_list(tpg->param_list);
  221. tpg->param_list = NULL;
  222. }
  223. return -ENOMEM;
  224. }
  225. int iscsit_tpg_del_portal_group(
  226. struct iscsi_tiqn *tiqn,
  227. struct iscsi_portal_group *tpg,
  228. int force)
  229. {
  230. u8 old_state = tpg->tpg_state;
  231. spin_lock(&tpg->tpg_state_lock);
  232. tpg->tpg_state = TPG_STATE_INACTIVE;
  233. spin_unlock(&tpg->tpg_state_lock);
  234. if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
  235. pr_err("Unable to delete iSCSI Target Portal Group:"
  236. " %hu while active sessions exist, and force=0\n",
  237. tpg->tpgt);
  238. tpg->tpg_state = old_state;
  239. return -EPERM;
  240. }
  241. if (tpg->param_list) {
  242. iscsi_release_param_list(tpg->param_list);
  243. tpg->param_list = NULL;
  244. }
  245. core_tpg_deregister(&tpg->tpg_se_tpg);
  246. spin_lock(&tpg->tpg_state_lock);
  247. tpg->tpg_state = TPG_STATE_FREE;
  248. spin_unlock(&tpg->tpg_state_lock);
  249. spin_lock(&tiqn->tiqn_tpg_lock);
  250. tiqn->tiqn_ntpgs--;
  251. list_del(&tpg->tpg_list);
  252. spin_unlock(&tiqn->tiqn_tpg_lock);
  253. pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
  254. tiqn->tiqn, tpg->tpgt);
  255. kfree(tpg);
  256. return 0;
  257. }
  258. int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
  259. {
  260. struct iscsi_param *param;
  261. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  262. int ret;
  263. spin_lock(&tpg->tpg_state_lock);
  264. if (tpg->tpg_state == TPG_STATE_ACTIVE) {
  265. pr_err("iSCSI target portal group: %hu is already"
  266. " active, ignoring request.\n", tpg->tpgt);
  267. spin_unlock(&tpg->tpg_state_lock);
  268. return -EINVAL;
  269. }
  270. /*
  271. * Make sure that AuthMethod does not contain None as an option
  272. * unless explictly disabled. Set the default to CHAP if authentication
  273. * is enforced (as per default), and remove the NONE option.
  274. */
  275. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  276. if (!param) {
  277. spin_unlock(&tpg->tpg_state_lock);
  278. return -EINVAL;
  279. }
  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. tpg->tpg_state = TPG_STATE_ACTIVE;
  291. spin_unlock(&tpg->tpg_state_lock);
  292. spin_lock(&tiqn->tiqn_tpg_lock);
  293. tiqn->tiqn_active_tpgs++;
  294. pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
  295. tpg->tpgt);
  296. spin_unlock(&tiqn->tiqn_tpg_lock);
  297. return 0;
  298. err:
  299. spin_unlock(&tpg->tpg_state_lock);
  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. }