iscsi_target_tpg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. }
  195. int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
  196. {
  197. if (tpg->tpg_state != TPG_STATE_FREE) {
  198. pr_err("Unable to add iSCSI Target Portal Group: %d"
  199. " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
  200. return -EEXIST;
  201. }
  202. iscsit_set_default_tpg_attribs(tpg);
  203. if (iscsi_create_default_params(&tpg->param_list) < 0)
  204. goto err_out;
  205. tpg->tpg_attrib.tpg = tpg;
  206. spin_lock(&tpg->tpg_state_lock);
  207. tpg->tpg_state = TPG_STATE_INACTIVE;
  208. spin_unlock(&tpg->tpg_state_lock);
  209. spin_lock(&tiqn->tiqn_tpg_lock);
  210. list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
  211. tiqn->tiqn_ntpgs++;
  212. pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
  213. tiqn->tiqn, tpg->tpgt);
  214. spin_unlock(&tiqn->tiqn_tpg_lock);
  215. return 0;
  216. err_out:
  217. if (tpg->param_list) {
  218. iscsi_release_param_list(tpg->param_list);
  219. tpg->param_list = NULL;
  220. }
  221. kfree(tpg);
  222. return -ENOMEM;
  223. }
  224. int iscsit_tpg_del_portal_group(
  225. struct iscsi_tiqn *tiqn,
  226. struct iscsi_portal_group *tpg,
  227. int force)
  228. {
  229. u8 old_state = tpg->tpg_state;
  230. spin_lock(&tpg->tpg_state_lock);
  231. tpg->tpg_state = TPG_STATE_INACTIVE;
  232. spin_unlock(&tpg->tpg_state_lock);
  233. if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
  234. pr_err("Unable to delete iSCSI Target Portal Group:"
  235. " %hu while active sessions exist, and force=0\n",
  236. tpg->tpgt);
  237. tpg->tpg_state = old_state;
  238. return -EPERM;
  239. }
  240. if (tpg->param_list) {
  241. iscsi_release_param_list(tpg->param_list);
  242. tpg->param_list = NULL;
  243. }
  244. core_tpg_deregister(&tpg->tpg_se_tpg);
  245. spin_lock(&tpg->tpg_state_lock);
  246. tpg->tpg_state = TPG_STATE_FREE;
  247. spin_unlock(&tpg->tpg_state_lock);
  248. spin_lock(&tiqn->tiqn_tpg_lock);
  249. tiqn->tiqn_ntpgs--;
  250. list_del(&tpg->tpg_list);
  251. spin_unlock(&tiqn->tiqn_tpg_lock);
  252. pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
  253. tiqn->tiqn, tpg->tpgt);
  254. kfree(tpg);
  255. return 0;
  256. }
  257. int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
  258. {
  259. struct iscsi_param *param;
  260. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  261. int ret;
  262. spin_lock(&tpg->tpg_state_lock);
  263. if (tpg->tpg_state == TPG_STATE_ACTIVE) {
  264. pr_err("iSCSI target portal group: %hu is already"
  265. " active, ignoring request.\n", tpg->tpgt);
  266. spin_unlock(&tpg->tpg_state_lock);
  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. spin_unlock(&tpg->tpg_state_lock);
  277. return -EINVAL;
  278. }
  279. if (tpg->tpg_attrib.authentication) {
  280. if (!strcmp(param->value, NONE)) {
  281. ret = iscsi_update_param_value(param, CHAP);
  282. if (ret)
  283. goto err;
  284. }
  285. ret = iscsit_ta_authentication(tpg, 1);
  286. if (ret < 0)
  287. goto err;
  288. }
  289. tpg->tpg_state = TPG_STATE_ACTIVE;
  290. spin_unlock(&tpg->tpg_state_lock);
  291. spin_lock(&tiqn->tiqn_tpg_lock);
  292. tiqn->tiqn_active_tpgs++;
  293. pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
  294. tpg->tpgt);
  295. spin_unlock(&tiqn->tiqn_tpg_lock);
  296. return 0;
  297. err:
  298. spin_unlock(&tpg->tpg_state_lock);
  299. return ret;
  300. }
  301. int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
  302. {
  303. struct iscsi_tiqn *tiqn;
  304. u8 old_state = tpg->tpg_state;
  305. spin_lock(&tpg->tpg_state_lock);
  306. if (tpg->tpg_state == TPG_STATE_INACTIVE) {
  307. pr_err("iSCSI Target Portal Group: %hu is already"
  308. " inactive, ignoring request.\n", tpg->tpgt);
  309. spin_unlock(&tpg->tpg_state_lock);
  310. return -EINVAL;
  311. }
  312. tpg->tpg_state = TPG_STATE_INACTIVE;
  313. spin_unlock(&tpg->tpg_state_lock);
  314. iscsit_clear_tpg_np_login_threads(tpg, false);
  315. if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
  316. spin_lock(&tpg->tpg_state_lock);
  317. tpg->tpg_state = old_state;
  318. spin_unlock(&tpg->tpg_state_lock);
  319. pr_err("Unable to disable iSCSI Target Portal Group:"
  320. " %hu while active sessions exist, and force=0\n",
  321. tpg->tpgt);
  322. return -EPERM;
  323. }
  324. tiqn = tpg->tpg_tiqn;
  325. if (!tiqn || (tpg == iscsit_global->discovery_tpg))
  326. return 0;
  327. spin_lock(&tiqn->tiqn_tpg_lock);
  328. tiqn->tiqn_active_tpgs--;
  329. pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
  330. tpg->tpgt);
  331. spin_unlock(&tiqn->tiqn_tpg_lock);
  332. return 0;
  333. }
  334. struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
  335. struct iscsi_session *sess)
  336. {
  337. struct se_session *se_sess = sess->se_sess;
  338. struct se_node_acl *se_nacl = se_sess->se_node_acl;
  339. struct iscsi_node_acl *acl = container_of(se_nacl, struct iscsi_node_acl,
  340. se_node_acl);
  341. return &acl->node_attrib;
  342. }
  343. struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
  344. struct iscsi_tpg_np *tpg_np,
  345. int network_transport)
  346. {
  347. struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
  348. spin_lock(&tpg_np->tpg_np_parent_lock);
  349. list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
  350. &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
  351. if (tpg_np_child->tpg_np->np_network_transport ==
  352. network_transport) {
  353. spin_unlock(&tpg_np->tpg_np_parent_lock);
  354. return tpg_np_child;
  355. }
  356. }
  357. spin_unlock(&tpg_np->tpg_np_parent_lock);
  358. return NULL;
  359. }
  360. static bool iscsit_tpg_check_network_portal(
  361. struct iscsi_tiqn *tiqn,
  362. struct __kernel_sockaddr_storage *sockaddr,
  363. int network_transport)
  364. {
  365. struct iscsi_portal_group *tpg;
  366. struct iscsi_tpg_np *tpg_np;
  367. struct iscsi_np *np;
  368. bool match = false;
  369. spin_lock(&tiqn->tiqn_tpg_lock);
  370. list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
  371. spin_lock(&tpg->tpg_np_lock);
  372. list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
  373. np = tpg_np->tpg_np;
  374. match = iscsit_check_np_match(sockaddr, np,
  375. network_transport);
  376. if (match)
  377. break;
  378. }
  379. spin_unlock(&tpg->tpg_np_lock);
  380. }
  381. spin_unlock(&tiqn->tiqn_tpg_lock);
  382. return match;
  383. }
  384. struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
  385. struct iscsi_portal_group *tpg,
  386. struct __kernel_sockaddr_storage *sockaddr,
  387. char *ip_str,
  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: %s already exists on a"
  397. " different TPG on %s\n", ip_str,
  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, ip_str, 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: %s:%hu,%hu on %s\n",
  435. tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, 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: %s:%hu,%hu on %s\n",
  446. tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, 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_tpg_set_initiator_node_queue_depth(
  501. struct iscsi_portal_group *tpg,
  502. unsigned char *initiatorname,
  503. u32 queue_depth,
  504. int force)
  505. {
  506. return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
  507. initiatorname, queue_depth, force);
  508. }
  509. int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
  510. {
  511. unsigned char buf1[256], buf2[256], *none = NULL;
  512. int len;
  513. struct iscsi_param *param;
  514. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  515. if ((authentication != 1) && (authentication != 0)) {
  516. pr_err("Illegal value for authentication parameter:"
  517. " %u, ignoring request.\n", authentication);
  518. return -EINVAL;
  519. }
  520. memset(buf1, 0, sizeof(buf1));
  521. memset(buf2, 0, sizeof(buf2));
  522. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  523. if (!param)
  524. return -EINVAL;
  525. if (authentication) {
  526. snprintf(buf1, sizeof(buf1), "%s", param->value);
  527. none = strstr(buf1, NONE);
  528. if (!none)
  529. goto out;
  530. if (!strncmp(none + 4, ",", 1)) {
  531. if (!strcmp(buf1, none))
  532. sprintf(buf2, "%s", none+5);
  533. else {
  534. none--;
  535. *none = '\0';
  536. len = sprintf(buf2, "%s", buf1);
  537. none += 5;
  538. sprintf(buf2 + len, "%s", none);
  539. }
  540. } else {
  541. none--;
  542. *none = '\0';
  543. sprintf(buf2, "%s", buf1);
  544. }
  545. if (iscsi_update_param_value(param, buf2) < 0)
  546. return -EINVAL;
  547. } else {
  548. snprintf(buf1, sizeof(buf1), "%s", param->value);
  549. none = strstr(buf1, NONE);
  550. if (none)
  551. goto out;
  552. strncat(buf1, ",", strlen(","));
  553. strncat(buf1, NONE, strlen(NONE));
  554. if (iscsi_update_param_value(param, buf1) < 0)
  555. return -EINVAL;
  556. }
  557. out:
  558. a->authentication = authentication;
  559. pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
  560. a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
  561. return 0;
  562. }
  563. int iscsit_ta_login_timeout(
  564. struct iscsi_portal_group *tpg,
  565. u32 login_timeout)
  566. {
  567. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  568. if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
  569. pr_err("Requested Login Timeout %u larger than maximum"
  570. " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
  571. return -EINVAL;
  572. } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
  573. pr_err("Requested Logout Timeout %u smaller than"
  574. " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
  575. return -EINVAL;
  576. }
  577. a->login_timeout = login_timeout;
  578. pr_debug("Set Logout Timeout to %u for Target Portal Group"
  579. " %hu\n", a->login_timeout, tpg->tpgt);
  580. return 0;
  581. }
  582. int iscsit_ta_netif_timeout(
  583. struct iscsi_portal_group *tpg,
  584. u32 netif_timeout)
  585. {
  586. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  587. if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
  588. pr_err("Requested Network Interface Timeout %u larger"
  589. " than maximum %u\n", netif_timeout,
  590. TA_NETIF_TIMEOUT_MAX);
  591. return -EINVAL;
  592. } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
  593. pr_err("Requested Network Interface Timeout %u smaller"
  594. " than minimum %u\n", netif_timeout,
  595. TA_NETIF_TIMEOUT_MIN);
  596. return -EINVAL;
  597. }
  598. a->netif_timeout = netif_timeout;
  599. pr_debug("Set Network Interface Timeout to %u for"
  600. " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
  601. return 0;
  602. }
  603. int iscsit_ta_generate_node_acls(
  604. struct iscsi_portal_group *tpg,
  605. u32 flag)
  606. {
  607. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  608. if ((flag != 0) && (flag != 1)) {
  609. pr_err("Illegal value %d\n", flag);
  610. return -EINVAL;
  611. }
  612. a->generate_node_acls = flag;
  613. pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
  614. tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
  615. if (flag == 1 && a->cache_dynamic_acls == 0) {
  616. pr_debug("Explicitly setting cache_dynamic_acls=1 when "
  617. "generate_node_acls=1\n");
  618. a->cache_dynamic_acls = 1;
  619. }
  620. return 0;
  621. }
  622. int iscsit_ta_default_cmdsn_depth(
  623. struct iscsi_portal_group *tpg,
  624. u32 tcq_depth)
  625. {
  626. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  627. if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
  628. pr_err("Requested Default Queue Depth: %u larger"
  629. " than maximum %u\n", tcq_depth,
  630. TA_DEFAULT_CMDSN_DEPTH_MAX);
  631. return -EINVAL;
  632. } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
  633. pr_err("Requested Default Queue Depth: %u smaller"
  634. " than minimum %u\n", tcq_depth,
  635. TA_DEFAULT_CMDSN_DEPTH_MIN);
  636. return -EINVAL;
  637. }
  638. a->default_cmdsn_depth = tcq_depth;
  639. pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
  640. tpg->tpgt, a->default_cmdsn_depth);
  641. return 0;
  642. }
  643. int iscsit_ta_cache_dynamic_acls(
  644. struct iscsi_portal_group *tpg,
  645. u32 flag)
  646. {
  647. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  648. if ((flag != 0) && (flag != 1)) {
  649. pr_err("Illegal value %d\n", flag);
  650. return -EINVAL;
  651. }
  652. if (a->generate_node_acls == 1 && flag == 0) {
  653. pr_debug("Skipping cache_dynamic_acls=0 when"
  654. " generate_node_acls=1\n");
  655. return 0;
  656. }
  657. a->cache_dynamic_acls = flag;
  658. pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
  659. " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
  660. "Enabled" : "Disabled");
  661. return 0;
  662. }
  663. int iscsit_ta_demo_mode_write_protect(
  664. struct iscsi_portal_group *tpg,
  665. u32 flag)
  666. {
  667. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  668. if ((flag != 0) && (flag != 1)) {
  669. pr_err("Illegal value %d\n", flag);
  670. return -EINVAL;
  671. }
  672. a->demo_mode_write_protect = flag;
  673. pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
  674. tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
  675. return 0;
  676. }
  677. int iscsit_ta_prod_mode_write_protect(
  678. struct iscsi_portal_group *tpg,
  679. u32 flag)
  680. {
  681. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  682. if ((flag != 0) && (flag != 1)) {
  683. pr_err("Illegal value %d\n", flag);
  684. return -EINVAL;
  685. }
  686. a->prod_mode_write_protect = flag;
  687. pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
  688. " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
  689. "ON" : "OFF");
  690. return 0;
  691. }
  692. int iscsit_ta_demo_mode_discovery(
  693. struct iscsi_portal_group *tpg,
  694. u32 flag)
  695. {
  696. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  697. if ((flag != 0) && (flag != 1)) {
  698. pr_err("Illegal value %d\n", flag);
  699. return -EINVAL;
  700. }
  701. a->demo_mode_discovery = flag;
  702. pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
  703. " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
  704. "ON" : "OFF");
  705. return 0;
  706. }
  707. int iscsit_ta_default_erl(
  708. struct iscsi_portal_group *tpg,
  709. u32 default_erl)
  710. {
  711. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  712. if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
  713. pr_err("Illegal value for default_erl: %u\n", default_erl);
  714. return -EINVAL;
  715. }
  716. a->default_erl = default_erl;
  717. pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
  718. return 0;
  719. }
  720. int iscsit_ta_t10_pi(
  721. struct iscsi_portal_group *tpg,
  722. u32 flag)
  723. {
  724. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  725. if ((flag != 0) && (flag != 1)) {
  726. pr_err("Illegal value %d\n", flag);
  727. return -EINVAL;
  728. }
  729. a->t10_pi = flag;
  730. pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
  731. " %s\n", tpg->tpgt, (a->t10_pi) ?
  732. "ON" : "OFF");
  733. return 0;
  734. }
  735. int iscsit_ta_fabric_prot_type(
  736. struct iscsi_portal_group *tpg,
  737. u32 prot_type)
  738. {
  739. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  740. if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
  741. pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
  742. return -EINVAL;
  743. }
  744. a->fabric_prot_type = prot_type;
  745. pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
  746. tpg->tpgt, prot_type);
  747. return 0;
  748. }