iscsi_target_tpg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. strncat(buf1, ",", strlen(","));
  544. strncat(buf1, NONE, strlen(NONE));
  545. if (iscsi_update_param_value(param, buf1) < 0)
  546. return -EINVAL;
  547. }
  548. out:
  549. a->authentication = authentication;
  550. pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
  551. a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
  552. return 0;
  553. }
  554. int iscsit_ta_login_timeout(
  555. struct iscsi_portal_group *tpg,
  556. u32 login_timeout)
  557. {
  558. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  559. if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
  560. pr_err("Requested Login Timeout %u larger than maximum"
  561. " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
  562. return -EINVAL;
  563. } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
  564. pr_err("Requested Logout Timeout %u smaller than"
  565. " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
  566. return -EINVAL;
  567. }
  568. a->login_timeout = login_timeout;
  569. pr_debug("Set Logout Timeout to %u for Target Portal Group"
  570. " %hu\n", a->login_timeout, tpg->tpgt);
  571. return 0;
  572. }
  573. int iscsit_ta_netif_timeout(
  574. struct iscsi_portal_group *tpg,
  575. u32 netif_timeout)
  576. {
  577. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  578. if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
  579. pr_err("Requested Network Interface Timeout %u larger"
  580. " than maximum %u\n", netif_timeout,
  581. TA_NETIF_TIMEOUT_MAX);
  582. return -EINVAL;
  583. } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
  584. pr_err("Requested Network Interface Timeout %u smaller"
  585. " than minimum %u\n", netif_timeout,
  586. TA_NETIF_TIMEOUT_MIN);
  587. return -EINVAL;
  588. }
  589. a->netif_timeout = netif_timeout;
  590. pr_debug("Set Network Interface Timeout to %u for"
  591. " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
  592. return 0;
  593. }
  594. int iscsit_ta_generate_node_acls(
  595. struct iscsi_portal_group *tpg,
  596. u32 flag)
  597. {
  598. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  599. if ((flag != 0) && (flag != 1)) {
  600. pr_err("Illegal value %d\n", flag);
  601. return -EINVAL;
  602. }
  603. a->generate_node_acls = flag;
  604. pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
  605. tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
  606. if (flag == 1 && a->cache_dynamic_acls == 0) {
  607. pr_debug("Explicitly setting cache_dynamic_acls=1 when "
  608. "generate_node_acls=1\n");
  609. a->cache_dynamic_acls = 1;
  610. }
  611. return 0;
  612. }
  613. int iscsit_ta_default_cmdsn_depth(
  614. struct iscsi_portal_group *tpg,
  615. u32 tcq_depth)
  616. {
  617. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  618. if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
  619. pr_err("Requested Default Queue Depth: %u larger"
  620. " than maximum %u\n", tcq_depth,
  621. TA_DEFAULT_CMDSN_DEPTH_MAX);
  622. return -EINVAL;
  623. } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
  624. pr_err("Requested Default Queue Depth: %u smaller"
  625. " than minimum %u\n", tcq_depth,
  626. TA_DEFAULT_CMDSN_DEPTH_MIN);
  627. return -EINVAL;
  628. }
  629. a->default_cmdsn_depth = tcq_depth;
  630. pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
  631. tpg->tpgt, a->default_cmdsn_depth);
  632. return 0;
  633. }
  634. int iscsit_ta_cache_dynamic_acls(
  635. struct iscsi_portal_group *tpg,
  636. u32 flag)
  637. {
  638. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  639. if ((flag != 0) && (flag != 1)) {
  640. pr_err("Illegal value %d\n", flag);
  641. return -EINVAL;
  642. }
  643. if (a->generate_node_acls == 1 && flag == 0) {
  644. pr_debug("Skipping cache_dynamic_acls=0 when"
  645. " generate_node_acls=1\n");
  646. return 0;
  647. }
  648. a->cache_dynamic_acls = flag;
  649. pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
  650. " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
  651. "Enabled" : "Disabled");
  652. return 0;
  653. }
  654. int iscsit_ta_demo_mode_write_protect(
  655. struct iscsi_portal_group *tpg,
  656. u32 flag)
  657. {
  658. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  659. if ((flag != 0) && (flag != 1)) {
  660. pr_err("Illegal value %d\n", flag);
  661. return -EINVAL;
  662. }
  663. a->demo_mode_write_protect = flag;
  664. pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
  665. tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
  666. return 0;
  667. }
  668. int iscsit_ta_prod_mode_write_protect(
  669. struct iscsi_portal_group *tpg,
  670. u32 flag)
  671. {
  672. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  673. if ((flag != 0) && (flag != 1)) {
  674. pr_err("Illegal value %d\n", flag);
  675. return -EINVAL;
  676. }
  677. a->prod_mode_write_protect = flag;
  678. pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
  679. " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
  680. "ON" : "OFF");
  681. return 0;
  682. }
  683. int iscsit_ta_demo_mode_discovery(
  684. struct iscsi_portal_group *tpg,
  685. u32 flag)
  686. {
  687. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  688. if ((flag != 0) && (flag != 1)) {
  689. pr_err("Illegal value %d\n", flag);
  690. return -EINVAL;
  691. }
  692. a->demo_mode_discovery = flag;
  693. pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
  694. " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
  695. "ON" : "OFF");
  696. return 0;
  697. }
  698. int iscsit_ta_default_erl(
  699. struct iscsi_portal_group *tpg,
  700. u32 default_erl)
  701. {
  702. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  703. if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
  704. pr_err("Illegal value for default_erl: %u\n", default_erl);
  705. return -EINVAL;
  706. }
  707. a->default_erl = default_erl;
  708. pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
  709. return 0;
  710. }
  711. int iscsit_ta_t10_pi(
  712. struct iscsi_portal_group *tpg,
  713. u32 flag)
  714. {
  715. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  716. if ((flag != 0) && (flag != 1)) {
  717. pr_err("Illegal value %d\n", flag);
  718. return -EINVAL;
  719. }
  720. a->t10_pi = flag;
  721. pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
  722. " %s\n", tpg->tpgt, (a->t10_pi) ?
  723. "ON" : "OFF");
  724. return 0;
  725. }
  726. int iscsit_ta_fabric_prot_type(
  727. struct iscsi_portal_group *tpg,
  728. u32 prot_type)
  729. {
  730. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  731. if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
  732. pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
  733. return -EINVAL;
  734. }
  735. a->fabric_prot_type = prot_type;
  736. pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
  737. tpg->tpgt, prot_type);
  738. return 0;
  739. }
  740. int iscsit_ta_tpg_enabled_sendtargets(
  741. struct iscsi_portal_group *tpg,
  742. u32 flag)
  743. {
  744. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  745. if ((flag != 0) && (flag != 1)) {
  746. pr_err("Illegal value %d\n", flag);
  747. return -EINVAL;
  748. }
  749. a->tpg_enabled_sendtargets = flag;
  750. pr_debug("iSCSI_TPG[%hu] - TPG enabled bit required for SendTargets:"
  751. " %s\n", tpg->tpgt, (a->tpg_enabled_sendtargets) ? "ON" : "OFF");
  752. return 0;
  753. }
  754. int iscsit_ta_login_keys_workaround(
  755. struct iscsi_portal_group *tpg,
  756. u32 flag)
  757. {
  758. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  759. if ((flag != 0) && (flag != 1)) {
  760. pr_err("Illegal value %d\n", flag);
  761. return -EINVAL;
  762. }
  763. a->login_keys_workaround = flag;
  764. pr_debug("iSCSI_TPG[%hu] - TPG enabled bit for login keys workaround: %s ",
  765. tpg->tpgt, (a->login_keys_workaround) ? "ON" : "OFF");
  766. return 0;
  767. }