iscsi_target_tpg.c 23 KB

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