iscsi_target_tpg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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/target_core_configfs.h>
  21. #include "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. ret = core_tpg_register(
  61. &lio_target_fabric_configfs->tf_ops,
  62. NULL, &tpg->tpg_se_tpg, tpg,
  63. TRANSPORT_TPG_TYPE_DISCOVERY);
  64. if (ret < 0) {
  65. kfree(tpg);
  66. return -1;
  67. }
  68. tpg->sid = 1; /* First Assigned LIO Session ID */
  69. iscsit_set_default_tpg_attribs(tpg);
  70. if (iscsi_create_default_params(&tpg->param_list) < 0)
  71. goto out;
  72. /*
  73. * By default we disable authentication for discovery sessions,
  74. * this can be changed with:
  75. *
  76. * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
  77. */
  78. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  79. if (!param)
  80. goto out;
  81. if (iscsi_update_param_value(param, "CHAP,None") < 0)
  82. goto out;
  83. tpg->tpg_attrib.authentication = 0;
  84. spin_lock(&tpg->tpg_state_lock);
  85. tpg->tpg_state = TPG_STATE_ACTIVE;
  86. spin_unlock(&tpg->tpg_state_lock);
  87. iscsit_global->discovery_tpg = tpg;
  88. pr_debug("CORE[0] - Allocated Discovery TPG\n");
  89. return 0;
  90. out:
  91. if (tpg->sid == 1)
  92. core_tpg_deregister(&tpg->tpg_se_tpg);
  93. kfree(tpg);
  94. return -1;
  95. }
  96. void iscsit_release_discovery_tpg(void)
  97. {
  98. struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
  99. if (!tpg)
  100. return;
  101. core_tpg_deregister(&tpg->tpg_se_tpg);
  102. kfree(tpg);
  103. iscsit_global->discovery_tpg = NULL;
  104. }
  105. struct iscsi_portal_group *iscsit_get_tpg_from_np(
  106. struct iscsi_tiqn *tiqn,
  107. struct iscsi_np *np,
  108. struct iscsi_tpg_np **tpg_np_out)
  109. {
  110. struct iscsi_portal_group *tpg = NULL;
  111. struct iscsi_tpg_np *tpg_np;
  112. spin_lock(&tiqn->tiqn_tpg_lock);
  113. list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
  114. spin_lock(&tpg->tpg_state_lock);
  115. if (tpg->tpg_state != TPG_STATE_ACTIVE) {
  116. spin_unlock(&tpg->tpg_state_lock);
  117. continue;
  118. }
  119. spin_unlock(&tpg->tpg_state_lock);
  120. spin_lock(&tpg->tpg_np_lock);
  121. list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
  122. if (tpg_np->tpg_np == np) {
  123. *tpg_np_out = tpg_np;
  124. kref_get(&tpg_np->tpg_np_kref);
  125. spin_unlock(&tpg->tpg_np_lock);
  126. spin_unlock(&tiqn->tiqn_tpg_lock);
  127. return tpg;
  128. }
  129. }
  130. spin_unlock(&tpg->tpg_np_lock);
  131. }
  132. spin_unlock(&tiqn->tiqn_tpg_lock);
  133. return NULL;
  134. }
  135. int iscsit_get_tpg(
  136. struct iscsi_portal_group *tpg)
  137. {
  138. int ret;
  139. ret = mutex_lock_interruptible(&tpg->tpg_access_lock);
  140. return ((ret != 0) || signal_pending(current)) ? -1 : 0;
  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. }
  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. core_tpg_clear_object_luns(&tpg->tpg_se_tpg);
  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 __kernel_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 __kernel_sockaddr_storage *sockaddr,
  388. char *ip_str,
  389. struct iscsi_tpg_np *tpg_np_parent,
  390. int network_transport)
  391. {
  392. struct iscsi_np *np;
  393. struct iscsi_tpg_np *tpg_np;
  394. if (!tpg_np_parent) {
  395. if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
  396. network_transport)) {
  397. pr_err("Network Portal: %s already exists on a"
  398. " different TPG on %s\n", ip_str,
  399. tpg->tpg_tiqn->tiqn);
  400. return ERR_PTR(-EEXIST);
  401. }
  402. }
  403. tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
  404. if (!tpg_np) {
  405. pr_err("Unable to allocate memory for"
  406. " struct iscsi_tpg_np.\n");
  407. return ERR_PTR(-ENOMEM);
  408. }
  409. np = iscsit_add_np(sockaddr, ip_str, network_transport);
  410. if (IS_ERR(np)) {
  411. kfree(tpg_np);
  412. return ERR_CAST(np);
  413. }
  414. INIT_LIST_HEAD(&tpg_np->tpg_np_list);
  415. INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
  416. INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
  417. spin_lock_init(&tpg_np->tpg_np_parent_lock);
  418. init_completion(&tpg_np->tpg_np_comp);
  419. kref_init(&tpg_np->tpg_np_kref);
  420. tpg_np->tpg_np = np;
  421. tpg_np->tpg = tpg;
  422. spin_lock(&tpg->tpg_np_lock);
  423. list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
  424. tpg->num_tpg_nps++;
  425. if (tpg->tpg_tiqn)
  426. tpg->tpg_tiqn->tiqn_num_tpg_nps++;
  427. spin_unlock(&tpg->tpg_np_lock);
  428. if (tpg_np_parent) {
  429. tpg_np->tpg_np_parent = tpg_np_parent;
  430. spin_lock(&tpg_np_parent->tpg_np_parent_lock);
  431. list_add_tail(&tpg_np->tpg_np_child_list,
  432. &tpg_np_parent->tpg_np_parent_list);
  433. spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
  434. }
  435. pr_debug("CORE[%s] - Added Network Portal: %s:%hu,%hu on %s\n",
  436. tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
  437. np->np_transport->name);
  438. return tpg_np;
  439. }
  440. static int iscsit_tpg_release_np(
  441. struct iscsi_tpg_np *tpg_np,
  442. struct iscsi_portal_group *tpg,
  443. struct iscsi_np *np)
  444. {
  445. iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
  446. pr_debug("CORE[%s] - Removed Network Portal: %s:%hu,%hu on %s\n",
  447. tpg->tpg_tiqn->tiqn, np->np_ip, np->np_port, tpg->tpgt,
  448. np->np_transport->name);
  449. tpg_np->tpg_np = NULL;
  450. tpg_np->tpg = NULL;
  451. kfree(tpg_np);
  452. /*
  453. * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
  454. */
  455. return iscsit_del_np(np);
  456. }
  457. int iscsit_tpg_del_network_portal(
  458. struct iscsi_portal_group *tpg,
  459. struct iscsi_tpg_np *tpg_np)
  460. {
  461. struct iscsi_np *np;
  462. struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
  463. int ret = 0;
  464. np = tpg_np->tpg_np;
  465. if (!np) {
  466. pr_err("Unable to locate struct iscsi_np from"
  467. " struct iscsi_tpg_np\n");
  468. return -EINVAL;
  469. }
  470. if (!tpg_np->tpg_np_parent) {
  471. /*
  472. * We are the parent tpg network portal. Release all of the
  473. * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
  474. * list first.
  475. */
  476. list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
  477. &tpg_np->tpg_np_parent_list,
  478. tpg_np_child_list) {
  479. ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
  480. if (ret < 0)
  481. pr_err("iscsit_tpg_del_network_portal()"
  482. " failed: %d\n", ret);
  483. }
  484. } else {
  485. /*
  486. * We are not the parent ISCSI_TCP tpg network portal. Release
  487. * our own network portals from the child list.
  488. */
  489. spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
  490. list_del(&tpg_np->tpg_np_child_list);
  491. spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
  492. }
  493. spin_lock(&tpg->tpg_np_lock);
  494. list_del(&tpg_np->tpg_np_list);
  495. tpg->num_tpg_nps--;
  496. if (tpg->tpg_tiqn)
  497. tpg->tpg_tiqn->tiqn_num_tpg_nps--;
  498. spin_unlock(&tpg->tpg_np_lock);
  499. return iscsit_tpg_release_np(tpg_np, tpg, np);
  500. }
  501. int iscsit_tpg_set_initiator_node_queue_depth(
  502. struct iscsi_portal_group *tpg,
  503. unsigned char *initiatorname,
  504. u32 queue_depth,
  505. int force)
  506. {
  507. return core_tpg_set_initiator_node_queue_depth(&tpg->tpg_se_tpg,
  508. initiatorname, queue_depth, force);
  509. }
  510. int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
  511. {
  512. unsigned char buf1[256], buf2[256], *none = NULL;
  513. int len;
  514. struct iscsi_param *param;
  515. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  516. if ((authentication != 1) && (authentication != 0)) {
  517. pr_err("Illegal value for authentication parameter:"
  518. " %u, ignoring request.\n", authentication);
  519. return -EINVAL;
  520. }
  521. memset(buf1, 0, sizeof(buf1));
  522. memset(buf2, 0, sizeof(buf2));
  523. param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
  524. if (!param)
  525. return -EINVAL;
  526. if (authentication) {
  527. snprintf(buf1, sizeof(buf1), "%s", param->value);
  528. none = strstr(buf1, NONE);
  529. if (!none)
  530. goto out;
  531. if (!strncmp(none + 4, ",", 1)) {
  532. if (!strcmp(buf1, none))
  533. sprintf(buf2, "%s", none+5);
  534. else {
  535. none--;
  536. *none = '\0';
  537. len = sprintf(buf2, "%s", buf1);
  538. none += 5;
  539. sprintf(buf2 + len, "%s", none);
  540. }
  541. } else {
  542. none--;
  543. *none = '\0';
  544. sprintf(buf2, "%s", buf1);
  545. }
  546. if (iscsi_update_param_value(param, buf2) < 0)
  547. return -EINVAL;
  548. } else {
  549. snprintf(buf1, sizeof(buf1), "%s", param->value);
  550. none = strstr(buf1, NONE);
  551. if (none)
  552. goto out;
  553. strncat(buf1, ",", strlen(","));
  554. strncat(buf1, NONE, strlen(NONE));
  555. if (iscsi_update_param_value(param, buf1) < 0)
  556. return -EINVAL;
  557. }
  558. out:
  559. a->authentication = authentication;
  560. pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
  561. a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
  562. return 0;
  563. }
  564. int iscsit_ta_login_timeout(
  565. struct iscsi_portal_group *tpg,
  566. u32 login_timeout)
  567. {
  568. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  569. if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
  570. pr_err("Requested Login Timeout %u larger than maximum"
  571. " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
  572. return -EINVAL;
  573. } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
  574. pr_err("Requested Logout Timeout %u smaller than"
  575. " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
  576. return -EINVAL;
  577. }
  578. a->login_timeout = login_timeout;
  579. pr_debug("Set Logout Timeout to %u for Target Portal Group"
  580. " %hu\n", a->login_timeout, tpg->tpgt);
  581. return 0;
  582. }
  583. int iscsit_ta_netif_timeout(
  584. struct iscsi_portal_group *tpg,
  585. u32 netif_timeout)
  586. {
  587. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  588. if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
  589. pr_err("Requested Network Interface Timeout %u larger"
  590. " than maximum %u\n", netif_timeout,
  591. TA_NETIF_TIMEOUT_MAX);
  592. return -EINVAL;
  593. } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
  594. pr_err("Requested Network Interface Timeout %u smaller"
  595. " than minimum %u\n", netif_timeout,
  596. TA_NETIF_TIMEOUT_MIN);
  597. return -EINVAL;
  598. }
  599. a->netif_timeout = netif_timeout;
  600. pr_debug("Set Network Interface Timeout to %u for"
  601. " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
  602. return 0;
  603. }
  604. int iscsit_ta_generate_node_acls(
  605. struct iscsi_portal_group *tpg,
  606. u32 flag)
  607. {
  608. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  609. if ((flag != 0) && (flag != 1)) {
  610. pr_err("Illegal value %d\n", flag);
  611. return -EINVAL;
  612. }
  613. a->generate_node_acls = flag;
  614. pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
  615. tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
  616. if (flag == 1 && a->cache_dynamic_acls == 0) {
  617. pr_debug("Explicitly setting cache_dynamic_acls=1 when "
  618. "generate_node_acls=1\n");
  619. a->cache_dynamic_acls = 1;
  620. }
  621. return 0;
  622. }
  623. int iscsit_ta_default_cmdsn_depth(
  624. struct iscsi_portal_group *tpg,
  625. u32 tcq_depth)
  626. {
  627. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  628. if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
  629. pr_err("Requested Default Queue Depth: %u larger"
  630. " than maximum %u\n", tcq_depth,
  631. TA_DEFAULT_CMDSN_DEPTH_MAX);
  632. return -EINVAL;
  633. } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
  634. pr_err("Requested Default Queue Depth: %u smaller"
  635. " than minimum %u\n", tcq_depth,
  636. TA_DEFAULT_CMDSN_DEPTH_MIN);
  637. return -EINVAL;
  638. }
  639. a->default_cmdsn_depth = tcq_depth;
  640. pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
  641. tpg->tpgt, a->default_cmdsn_depth);
  642. return 0;
  643. }
  644. int iscsit_ta_cache_dynamic_acls(
  645. struct iscsi_portal_group *tpg,
  646. u32 flag)
  647. {
  648. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  649. if ((flag != 0) && (flag != 1)) {
  650. pr_err("Illegal value %d\n", flag);
  651. return -EINVAL;
  652. }
  653. if (a->generate_node_acls == 1 && flag == 0) {
  654. pr_debug("Skipping cache_dynamic_acls=0 when"
  655. " generate_node_acls=1\n");
  656. return 0;
  657. }
  658. a->cache_dynamic_acls = flag;
  659. pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
  660. " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
  661. "Enabled" : "Disabled");
  662. return 0;
  663. }
  664. int iscsit_ta_demo_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->demo_mode_write_protect = flag;
  674. pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
  675. tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
  676. return 0;
  677. }
  678. int iscsit_ta_prod_mode_write_protect(
  679. struct iscsi_portal_group *tpg,
  680. u32 flag)
  681. {
  682. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  683. if ((flag != 0) && (flag != 1)) {
  684. pr_err("Illegal value %d\n", flag);
  685. return -EINVAL;
  686. }
  687. a->prod_mode_write_protect = flag;
  688. pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
  689. " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
  690. "ON" : "OFF");
  691. return 0;
  692. }
  693. int iscsit_ta_demo_mode_discovery(
  694. struct iscsi_portal_group *tpg,
  695. u32 flag)
  696. {
  697. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  698. if ((flag != 0) && (flag != 1)) {
  699. pr_err("Illegal value %d\n", flag);
  700. return -EINVAL;
  701. }
  702. a->demo_mode_discovery = flag;
  703. pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
  704. " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
  705. "ON" : "OFF");
  706. return 0;
  707. }
  708. int iscsit_ta_default_erl(
  709. struct iscsi_portal_group *tpg,
  710. u32 default_erl)
  711. {
  712. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  713. if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
  714. pr_err("Illegal value for default_erl: %u\n", default_erl);
  715. return -EINVAL;
  716. }
  717. a->default_erl = default_erl;
  718. pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
  719. return 0;
  720. }
  721. int iscsit_ta_t10_pi(
  722. struct iscsi_portal_group *tpg,
  723. u32 flag)
  724. {
  725. struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
  726. if ((flag != 0) && (flag != 1)) {
  727. pr_err("Illegal value %d\n", flag);
  728. return -EINVAL;
  729. }
  730. a->t10_pi = flag;
  731. pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
  732. " %s\n", tpg->tpgt, (a->t10_pi) ?
  733. "ON" : "OFF");
  734. return 0;
  735. }