iscsi_target_configfs.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. /*******************************************************************************
  2. * This file contains the configfs implementation for iSCSI Target mode
  3. * from the LIO-Target Project.
  4. *
  5. * (c) Copyright 2007-2013 Datera, Inc.
  6. *
  7. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. ****************************************************************************/
  19. #include <linux/configfs.h>
  20. #include <linux/ctype.h>
  21. #include <linux/export.h>
  22. #include <linux/inet.h>
  23. #include <target/target_core_base.h>
  24. #include <target/target_core_fabric.h>
  25. #include <target/target_core_fabric_configfs.h>
  26. #include <target/target_core_configfs.h>
  27. #include <target/configfs_macros.h>
  28. #include <target/iscsi/iscsi_transport.h>
  29. #include "iscsi_target_core.h"
  30. #include "iscsi_target_parameters.h"
  31. #include "iscsi_target_device.h"
  32. #include "iscsi_target_erl0.h"
  33. #include "iscsi_target_nodeattrib.h"
  34. #include "iscsi_target_tpg.h"
  35. #include "iscsi_target_util.h"
  36. #include "iscsi_target.h"
  37. #include "iscsi_target_stat.h"
  38. #include "iscsi_target_configfs.h"
  39. struct target_fabric_configfs *lio_target_fabric_configfs;
  40. struct lio_target_configfs_attribute {
  41. struct configfs_attribute attr;
  42. ssize_t (*show)(void *, char *);
  43. ssize_t (*store)(void *, const char *, size_t);
  44. };
  45. /* Start items for lio_target_portal_cit */
  46. static ssize_t lio_target_np_show_sctp(
  47. struct se_tpg_np *se_tpg_np,
  48. char *page)
  49. {
  50. struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
  51. struct iscsi_tpg_np, se_tpg_np);
  52. struct iscsi_tpg_np *tpg_np_sctp;
  53. ssize_t rb;
  54. tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
  55. if (tpg_np_sctp)
  56. rb = sprintf(page, "1\n");
  57. else
  58. rb = sprintf(page, "0\n");
  59. return rb;
  60. }
  61. static ssize_t lio_target_np_store_sctp(
  62. struct se_tpg_np *se_tpg_np,
  63. const char *page,
  64. size_t count)
  65. {
  66. struct iscsi_np *np;
  67. struct iscsi_portal_group *tpg;
  68. struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
  69. struct iscsi_tpg_np, se_tpg_np);
  70. struct iscsi_tpg_np *tpg_np_sctp = NULL;
  71. u32 op;
  72. int ret;
  73. ret = kstrtou32(page, 0, &op);
  74. if (ret)
  75. return ret;
  76. if ((op != 1) && (op != 0)) {
  77. pr_err("Illegal value for tpg_enable: %u\n", op);
  78. return -EINVAL;
  79. }
  80. np = tpg_np->tpg_np;
  81. if (!np) {
  82. pr_err("Unable to locate struct iscsi_np from"
  83. " struct iscsi_tpg_np\n");
  84. return -EINVAL;
  85. }
  86. tpg = tpg_np->tpg;
  87. if (iscsit_get_tpg(tpg) < 0)
  88. return -EINVAL;
  89. if (op) {
  90. /*
  91. * Use existing np->np_sockaddr for SCTP network portal reference
  92. */
  93. tpg_np_sctp = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
  94. np->np_ip, tpg_np, ISCSI_SCTP_TCP);
  95. if (!tpg_np_sctp || IS_ERR(tpg_np_sctp))
  96. goto out;
  97. } else {
  98. tpg_np_sctp = iscsit_tpg_locate_child_np(tpg_np, ISCSI_SCTP_TCP);
  99. if (!tpg_np_sctp)
  100. goto out;
  101. ret = iscsit_tpg_del_network_portal(tpg, tpg_np_sctp);
  102. if (ret < 0)
  103. goto out;
  104. }
  105. iscsit_put_tpg(tpg);
  106. return count;
  107. out:
  108. iscsit_put_tpg(tpg);
  109. return -EINVAL;
  110. }
  111. TF_NP_BASE_ATTR(lio_target, sctp, S_IRUGO | S_IWUSR);
  112. static ssize_t lio_target_np_show_iser(
  113. struct se_tpg_np *se_tpg_np,
  114. char *page)
  115. {
  116. struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
  117. struct iscsi_tpg_np, se_tpg_np);
  118. struct iscsi_tpg_np *tpg_np_iser;
  119. ssize_t rb;
  120. tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
  121. if (tpg_np_iser)
  122. rb = sprintf(page, "1\n");
  123. else
  124. rb = sprintf(page, "0\n");
  125. return rb;
  126. }
  127. static ssize_t lio_target_np_store_iser(
  128. struct se_tpg_np *se_tpg_np,
  129. const char *page,
  130. size_t count)
  131. {
  132. struct iscsi_np *np;
  133. struct iscsi_portal_group *tpg;
  134. struct iscsi_tpg_np *tpg_np = container_of(se_tpg_np,
  135. struct iscsi_tpg_np, se_tpg_np);
  136. struct iscsi_tpg_np *tpg_np_iser = NULL;
  137. char *endptr;
  138. u32 op;
  139. int rc = 0;
  140. op = simple_strtoul(page, &endptr, 0);
  141. if ((op != 1) && (op != 0)) {
  142. pr_err("Illegal value for tpg_enable: %u\n", op);
  143. return -EINVAL;
  144. }
  145. np = tpg_np->tpg_np;
  146. if (!np) {
  147. pr_err("Unable to locate struct iscsi_np from"
  148. " struct iscsi_tpg_np\n");
  149. return -EINVAL;
  150. }
  151. tpg = tpg_np->tpg;
  152. if (iscsit_get_tpg(tpg) < 0)
  153. return -EINVAL;
  154. if (op) {
  155. rc = request_module("ib_isert");
  156. if (rc != 0) {
  157. pr_warn("Unable to request_module for ib_isert\n");
  158. rc = 0;
  159. }
  160. tpg_np_iser = iscsit_tpg_add_network_portal(tpg, &np->np_sockaddr,
  161. np->np_ip, tpg_np, ISCSI_INFINIBAND);
  162. if (IS_ERR(tpg_np_iser)) {
  163. rc = PTR_ERR(tpg_np_iser);
  164. goto out;
  165. }
  166. } else {
  167. tpg_np_iser = iscsit_tpg_locate_child_np(tpg_np, ISCSI_INFINIBAND);
  168. if (tpg_np_iser) {
  169. rc = iscsit_tpg_del_network_portal(tpg, tpg_np_iser);
  170. if (rc < 0)
  171. goto out;
  172. }
  173. }
  174. iscsit_put_tpg(tpg);
  175. return count;
  176. out:
  177. iscsit_put_tpg(tpg);
  178. return rc;
  179. }
  180. TF_NP_BASE_ATTR(lio_target, iser, S_IRUGO | S_IWUSR);
  181. static struct configfs_attribute *lio_target_portal_attrs[] = {
  182. &lio_target_np_sctp.attr,
  183. &lio_target_np_iser.attr,
  184. NULL,
  185. };
  186. /* Stop items for lio_target_portal_cit */
  187. /* Start items for lio_target_np_cit */
  188. #define MAX_PORTAL_LEN 256
  189. static struct se_tpg_np *lio_target_call_addnptotpg(
  190. struct se_portal_group *se_tpg,
  191. struct config_group *group,
  192. const char *name)
  193. {
  194. struct iscsi_portal_group *tpg;
  195. struct iscsi_tpg_np *tpg_np;
  196. char *str, *str2, *ip_str, *port_str;
  197. struct __kernel_sockaddr_storage sockaddr;
  198. struct sockaddr_in *sock_in;
  199. struct sockaddr_in6 *sock_in6;
  200. unsigned long port;
  201. int ret;
  202. char buf[MAX_PORTAL_LEN + 1];
  203. if (strlen(name) > MAX_PORTAL_LEN) {
  204. pr_err("strlen(name): %d exceeds MAX_PORTAL_LEN: %d\n",
  205. (int)strlen(name), MAX_PORTAL_LEN);
  206. return ERR_PTR(-EOVERFLOW);
  207. }
  208. memset(buf, 0, MAX_PORTAL_LEN + 1);
  209. snprintf(buf, MAX_PORTAL_LEN + 1, "%s", name);
  210. memset(&sockaddr, 0, sizeof(struct __kernel_sockaddr_storage));
  211. str = strstr(buf, "[");
  212. if (str) {
  213. const char *end;
  214. str2 = strstr(str, "]");
  215. if (!str2) {
  216. pr_err("Unable to locate trailing \"]\""
  217. " in IPv6 iSCSI network portal address\n");
  218. return ERR_PTR(-EINVAL);
  219. }
  220. str++; /* Skip over leading "[" */
  221. *str2 = '\0'; /* Terminate the IPv6 address */
  222. str2++; /* Skip over the "]" */
  223. port_str = strstr(str2, ":");
  224. if (!port_str) {
  225. pr_err("Unable to locate \":port\""
  226. " in IPv6 iSCSI network portal address\n");
  227. return ERR_PTR(-EINVAL);
  228. }
  229. *port_str = '\0'; /* Terminate string for IP */
  230. port_str++; /* Skip over ":" */
  231. ret = kstrtoul(port_str, 0, &port);
  232. if (ret < 0) {
  233. pr_err("kstrtoul() failed for port_str: %d\n", ret);
  234. return ERR_PTR(ret);
  235. }
  236. sock_in6 = (struct sockaddr_in6 *)&sockaddr;
  237. sock_in6->sin6_family = AF_INET6;
  238. sock_in6->sin6_port = htons((unsigned short)port);
  239. ret = in6_pton(str, IPV6_ADDRESS_SPACE,
  240. (void *)&sock_in6->sin6_addr.in6_u, -1, &end);
  241. if (ret <= 0) {
  242. pr_err("in6_pton returned: %d\n", ret);
  243. return ERR_PTR(-EINVAL);
  244. }
  245. } else {
  246. str = ip_str = &buf[0];
  247. port_str = strstr(ip_str, ":");
  248. if (!port_str) {
  249. pr_err("Unable to locate \":port\""
  250. " in IPv4 iSCSI network portal address\n");
  251. return ERR_PTR(-EINVAL);
  252. }
  253. *port_str = '\0'; /* Terminate string for IP */
  254. port_str++; /* Skip over ":" */
  255. ret = kstrtoul(port_str, 0, &port);
  256. if (ret < 0) {
  257. pr_err("kstrtoul() failed for port_str: %d\n", ret);
  258. return ERR_PTR(ret);
  259. }
  260. sock_in = (struct sockaddr_in *)&sockaddr;
  261. sock_in->sin_family = AF_INET;
  262. sock_in->sin_port = htons((unsigned short)port);
  263. sock_in->sin_addr.s_addr = in_aton(ip_str);
  264. }
  265. tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
  266. ret = iscsit_get_tpg(tpg);
  267. if (ret < 0)
  268. return ERR_PTR(-EINVAL);
  269. pr_debug("LIO_Target_ConfigFS: REGISTER -> %s TPGT: %hu"
  270. " PORTAL: %s\n",
  271. config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
  272. tpg->tpgt, name);
  273. /*
  274. * Assume ISCSI_TCP by default. Other network portals for other
  275. * iSCSI fabrics:
  276. *
  277. * Traditional iSCSI over SCTP (initial support)
  278. * iSER/TCP (TODO, hardware available)
  279. * iSER/SCTP (TODO, software emulation with osc-iwarp)
  280. * iSER/IB (TODO, hardware available)
  281. *
  282. * can be enabled with attributes under
  283. * sys/kernel/config/iscsi/$IQN/$TPG/np/$IP:$PORT/
  284. *
  285. */
  286. tpg_np = iscsit_tpg_add_network_portal(tpg, &sockaddr, str, NULL,
  287. ISCSI_TCP);
  288. if (IS_ERR(tpg_np)) {
  289. iscsit_put_tpg(tpg);
  290. return ERR_CAST(tpg_np);
  291. }
  292. pr_debug("LIO_Target_ConfigFS: addnptotpg done!\n");
  293. iscsit_put_tpg(tpg);
  294. return &tpg_np->se_tpg_np;
  295. }
  296. static void lio_target_call_delnpfromtpg(
  297. struct se_tpg_np *se_tpg_np)
  298. {
  299. struct iscsi_portal_group *tpg;
  300. struct iscsi_tpg_np *tpg_np;
  301. struct se_portal_group *se_tpg;
  302. int ret;
  303. tpg_np = container_of(se_tpg_np, struct iscsi_tpg_np, se_tpg_np);
  304. tpg = tpg_np->tpg;
  305. ret = iscsit_get_tpg(tpg);
  306. if (ret < 0)
  307. return;
  308. se_tpg = &tpg->tpg_se_tpg;
  309. pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s TPGT: %hu"
  310. " PORTAL: %s:%hu\n", config_item_name(&se_tpg->se_tpg_wwn->wwn_group.cg_item),
  311. tpg->tpgt, tpg_np->tpg_np->np_ip, tpg_np->tpg_np->np_port);
  312. ret = iscsit_tpg_del_network_portal(tpg, tpg_np);
  313. if (ret < 0)
  314. goto out;
  315. pr_debug("LIO_Target_ConfigFS: delnpfromtpg done!\n");
  316. out:
  317. iscsit_put_tpg(tpg);
  318. }
  319. /* End items for lio_target_np_cit */
  320. /* Start items for lio_target_nacl_attrib_cit */
  321. #define DEF_NACL_ATTRIB(name) \
  322. static ssize_t iscsi_nacl_attrib_show_##name( \
  323. struct se_node_acl *se_nacl, \
  324. char *page) \
  325. { \
  326. struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
  327. se_node_acl); \
  328. \
  329. return sprintf(page, "%u\n", nacl->node_attrib.name); \
  330. } \
  331. \
  332. static ssize_t iscsi_nacl_attrib_store_##name( \
  333. struct se_node_acl *se_nacl, \
  334. const char *page, \
  335. size_t count) \
  336. { \
  337. struct iscsi_node_acl *nacl = container_of(se_nacl, struct iscsi_node_acl, \
  338. se_node_acl); \
  339. u32 val; \
  340. int ret; \
  341. \
  342. ret = kstrtou32(page, 0, &val); \
  343. if (ret) \
  344. return ret; \
  345. ret = iscsit_na_##name(nacl, val); \
  346. if (ret < 0) \
  347. return ret; \
  348. \
  349. return count; \
  350. }
  351. #define NACL_ATTR(_name, _mode) TF_NACL_ATTRIB_ATTR(iscsi, _name, _mode);
  352. /*
  353. * Define iscsi_node_attrib_s_dataout_timeout
  354. */
  355. DEF_NACL_ATTRIB(dataout_timeout);
  356. NACL_ATTR(dataout_timeout, S_IRUGO | S_IWUSR);
  357. /*
  358. * Define iscsi_node_attrib_s_dataout_timeout_retries
  359. */
  360. DEF_NACL_ATTRIB(dataout_timeout_retries);
  361. NACL_ATTR(dataout_timeout_retries, S_IRUGO | S_IWUSR);
  362. /*
  363. * Define iscsi_node_attrib_s_default_erl
  364. */
  365. DEF_NACL_ATTRIB(default_erl);
  366. NACL_ATTR(default_erl, S_IRUGO | S_IWUSR);
  367. /*
  368. * Define iscsi_node_attrib_s_nopin_timeout
  369. */
  370. DEF_NACL_ATTRIB(nopin_timeout);
  371. NACL_ATTR(nopin_timeout, S_IRUGO | S_IWUSR);
  372. /*
  373. * Define iscsi_node_attrib_s_nopin_response_timeout
  374. */
  375. DEF_NACL_ATTRIB(nopin_response_timeout);
  376. NACL_ATTR(nopin_response_timeout, S_IRUGO | S_IWUSR);
  377. /*
  378. * Define iscsi_node_attrib_s_random_datain_pdu_offsets
  379. */
  380. DEF_NACL_ATTRIB(random_datain_pdu_offsets);
  381. NACL_ATTR(random_datain_pdu_offsets, S_IRUGO | S_IWUSR);
  382. /*
  383. * Define iscsi_node_attrib_s_random_datain_seq_offsets
  384. */
  385. DEF_NACL_ATTRIB(random_datain_seq_offsets);
  386. NACL_ATTR(random_datain_seq_offsets, S_IRUGO | S_IWUSR);
  387. /*
  388. * Define iscsi_node_attrib_s_random_r2t_offsets
  389. */
  390. DEF_NACL_ATTRIB(random_r2t_offsets);
  391. NACL_ATTR(random_r2t_offsets, S_IRUGO | S_IWUSR);
  392. static struct configfs_attribute *lio_target_nacl_attrib_attrs[] = {
  393. &iscsi_nacl_attrib_dataout_timeout.attr,
  394. &iscsi_nacl_attrib_dataout_timeout_retries.attr,
  395. &iscsi_nacl_attrib_default_erl.attr,
  396. &iscsi_nacl_attrib_nopin_timeout.attr,
  397. &iscsi_nacl_attrib_nopin_response_timeout.attr,
  398. &iscsi_nacl_attrib_random_datain_pdu_offsets.attr,
  399. &iscsi_nacl_attrib_random_datain_seq_offsets.attr,
  400. &iscsi_nacl_attrib_random_r2t_offsets.attr,
  401. NULL,
  402. };
  403. /* End items for lio_target_nacl_attrib_cit */
  404. /* Start items for lio_target_nacl_auth_cit */
  405. #define __DEF_NACL_AUTH_STR(prefix, name, flags) \
  406. static ssize_t __iscsi_##prefix##_show_##name( \
  407. struct iscsi_node_acl *nacl, \
  408. char *page) \
  409. { \
  410. struct iscsi_node_auth *auth = &nacl->node_auth; \
  411. \
  412. if (!capable(CAP_SYS_ADMIN)) \
  413. return -EPERM; \
  414. return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
  415. } \
  416. \
  417. static ssize_t __iscsi_##prefix##_store_##name( \
  418. struct iscsi_node_acl *nacl, \
  419. const char *page, \
  420. size_t count) \
  421. { \
  422. struct iscsi_node_auth *auth = &nacl->node_auth; \
  423. \
  424. if (!capable(CAP_SYS_ADMIN)) \
  425. return -EPERM; \
  426. \
  427. snprintf(auth->name, sizeof(auth->name), "%s", page); \
  428. if (!strncmp("NULL", auth->name, 4)) \
  429. auth->naf_flags &= ~flags; \
  430. else \
  431. auth->naf_flags |= flags; \
  432. \
  433. if ((auth->naf_flags & NAF_USERID_IN_SET) && \
  434. (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
  435. auth->authenticate_target = 1; \
  436. else \
  437. auth->authenticate_target = 0; \
  438. \
  439. return count; \
  440. }
  441. #define __DEF_NACL_AUTH_INT(prefix, name) \
  442. static ssize_t __iscsi_##prefix##_show_##name( \
  443. struct iscsi_node_acl *nacl, \
  444. char *page) \
  445. { \
  446. struct iscsi_node_auth *auth = &nacl->node_auth; \
  447. \
  448. if (!capable(CAP_SYS_ADMIN)) \
  449. return -EPERM; \
  450. \
  451. return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
  452. }
  453. #define DEF_NACL_AUTH_STR(name, flags) \
  454. __DEF_NACL_AUTH_STR(nacl_auth, name, flags) \
  455. static ssize_t iscsi_nacl_auth_show_##name( \
  456. struct se_node_acl *nacl, \
  457. char *page) \
  458. { \
  459. return __iscsi_nacl_auth_show_##name(container_of(nacl, \
  460. struct iscsi_node_acl, se_node_acl), page); \
  461. } \
  462. static ssize_t iscsi_nacl_auth_store_##name( \
  463. struct se_node_acl *nacl, \
  464. const char *page, \
  465. size_t count) \
  466. { \
  467. return __iscsi_nacl_auth_store_##name(container_of(nacl, \
  468. struct iscsi_node_acl, se_node_acl), page, count); \
  469. }
  470. #define DEF_NACL_AUTH_INT(name) \
  471. __DEF_NACL_AUTH_INT(nacl_auth, name) \
  472. static ssize_t iscsi_nacl_auth_show_##name( \
  473. struct se_node_acl *nacl, \
  474. char *page) \
  475. { \
  476. return __iscsi_nacl_auth_show_##name(container_of(nacl, \
  477. struct iscsi_node_acl, se_node_acl), page); \
  478. }
  479. #define AUTH_ATTR(_name, _mode) TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
  480. #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
  481. /*
  482. * One-way authentication userid
  483. */
  484. DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
  485. AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  486. /*
  487. * One-way authentication password
  488. */
  489. DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
  490. AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  491. /*
  492. * Enforce mutual authentication
  493. */
  494. DEF_NACL_AUTH_INT(authenticate_target);
  495. AUTH_ATTR_RO(authenticate_target);
  496. /*
  497. * Mutual authentication userid
  498. */
  499. DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  500. AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  501. /*
  502. * Mutual authentication password
  503. */
  504. DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  505. AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  506. static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
  507. &iscsi_nacl_auth_userid.attr,
  508. &iscsi_nacl_auth_password.attr,
  509. &iscsi_nacl_auth_authenticate_target.attr,
  510. &iscsi_nacl_auth_userid_mutual.attr,
  511. &iscsi_nacl_auth_password_mutual.attr,
  512. NULL,
  513. };
  514. /* End items for lio_target_nacl_auth_cit */
  515. /* Start items for lio_target_nacl_param_cit */
  516. #define DEF_NACL_PARAM(name) \
  517. static ssize_t iscsi_nacl_param_show_##name( \
  518. struct se_node_acl *se_nacl, \
  519. char *page) \
  520. { \
  521. struct iscsi_session *sess; \
  522. struct se_session *se_sess; \
  523. ssize_t rb; \
  524. \
  525. spin_lock_bh(&se_nacl->nacl_sess_lock); \
  526. se_sess = se_nacl->nacl_sess; \
  527. if (!se_sess) { \
  528. rb = snprintf(page, PAGE_SIZE, \
  529. "No Active iSCSI Session\n"); \
  530. } else { \
  531. sess = se_sess->fabric_sess_ptr; \
  532. rb = snprintf(page, PAGE_SIZE, "%u\n", \
  533. (u32)sess->sess_ops->name); \
  534. } \
  535. spin_unlock_bh(&se_nacl->nacl_sess_lock); \
  536. \
  537. return rb; \
  538. }
  539. #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
  540. DEF_NACL_PARAM(MaxConnections);
  541. NACL_PARAM_ATTR(MaxConnections);
  542. DEF_NACL_PARAM(InitialR2T);
  543. NACL_PARAM_ATTR(InitialR2T);
  544. DEF_NACL_PARAM(ImmediateData);
  545. NACL_PARAM_ATTR(ImmediateData);
  546. DEF_NACL_PARAM(MaxBurstLength);
  547. NACL_PARAM_ATTR(MaxBurstLength);
  548. DEF_NACL_PARAM(FirstBurstLength);
  549. NACL_PARAM_ATTR(FirstBurstLength);
  550. DEF_NACL_PARAM(DefaultTime2Wait);
  551. NACL_PARAM_ATTR(DefaultTime2Wait);
  552. DEF_NACL_PARAM(DefaultTime2Retain);
  553. NACL_PARAM_ATTR(DefaultTime2Retain);
  554. DEF_NACL_PARAM(MaxOutstandingR2T);
  555. NACL_PARAM_ATTR(MaxOutstandingR2T);
  556. DEF_NACL_PARAM(DataPDUInOrder);
  557. NACL_PARAM_ATTR(DataPDUInOrder);
  558. DEF_NACL_PARAM(DataSequenceInOrder);
  559. NACL_PARAM_ATTR(DataSequenceInOrder);
  560. DEF_NACL_PARAM(ErrorRecoveryLevel);
  561. NACL_PARAM_ATTR(ErrorRecoveryLevel);
  562. static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
  563. &iscsi_nacl_param_MaxConnections.attr,
  564. &iscsi_nacl_param_InitialR2T.attr,
  565. &iscsi_nacl_param_ImmediateData.attr,
  566. &iscsi_nacl_param_MaxBurstLength.attr,
  567. &iscsi_nacl_param_FirstBurstLength.attr,
  568. &iscsi_nacl_param_DefaultTime2Wait.attr,
  569. &iscsi_nacl_param_DefaultTime2Retain.attr,
  570. &iscsi_nacl_param_MaxOutstandingR2T.attr,
  571. &iscsi_nacl_param_DataPDUInOrder.attr,
  572. &iscsi_nacl_param_DataSequenceInOrder.attr,
  573. &iscsi_nacl_param_ErrorRecoveryLevel.attr,
  574. NULL,
  575. };
  576. /* End items for lio_target_nacl_param_cit */
  577. /* Start items for lio_target_acl_cit */
  578. static ssize_t lio_target_nacl_show_info(
  579. struct se_node_acl *se_nacl,
  580. char *page)
  581. {
  582. struct iscsi_session *sess;
  583. struct iscsi_conn *conn;
  584. struct se_session *se_sess;
  585. ssize_t rb = 0;
  586. spin_lock_bh(&se_nacl->nacl_sess_lock);
  587. se_sess = se_nacl->nacl_sess;
  588. if (!se_sess) {
  589. rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
  590. " Endpoint: %s\n", se_nacl->initiatorname);
  591. } else {
  592. sess = se_sess->fabric_sess_ptr;
  593. if (sess->sess_ops->InitiatorName)
  594. rb += sprintf(page+rb, "InitiatorName: %s\n",
  595. sess->sess_ops->InitiatorName);
  596. if (sess->sess_ops->InitiatorAlias)
  597. rb += sprintf(page+rb, "InitiatorAlias: %s\n",
  598. sess->sess_ops->InitiatorAlias);
  599. rb += sprintf(page+rb, "LIO Session ID: %u "
  600. "ISID: 0x%02x %02x %02x %02x %02x %02x "
  601. "TSIH: %hu ", sess->sid,
  602. sess->isid[0], sess->isid[1], sess->isid[2],
  603. sess->isid[3], sess->isid[4], sess->isid[5],
  604. sess->tsih);
  605. rb += sprintf(page+rb, "SessionType: %s\n",
  606. (sess->sess_ops->SessionType) ?
  607. "Discovery" : "Normal");
  608. rb += sprintf(page+rb, "Session State: ");
  609. switch (sess->session_state) {
  610. case TARG_SESS_STATE_FREE:
  611. rb += sprintf(page+rb, "TARG_SESS_FREE\n");
  612. break;
  613. case TARG_SESS_STATE_ACTIVE:
  614. rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
  615. break;
  616. case TARG_SESS_STATE_LOGGED_IN:
  617. rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
  618. break;
  619. case TARG_SESS_STATE_FAILED:
  620. rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
  621. break;
  622. case TARG_SESS_STATE_IN_CONTINUE:
  623. rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
  624. break;
  625. default:
  626. rb += sprintf(page+rb, "ERROR: Unknown Session"
  627. " State!\n");
  628. break;
  629. }
  630. rb += sprintf(page+rb, "---------------------[iSCSI Session"
  631. " Values]-----------------------\n");
  632. rb += sprintf(page+rb, " CmdSN/WR : CmdSN/WC : ExpCmdSN"
  633. " : MaxCmdSN : ITT : TTT\n");
  634. rb += sprintf(page+rb, " 0x%08x 0x%08x 0x%08x 0x%08x"
  635. " 0x%08x 0x%08x\n",
  636. sess->cmdsn_window,
  637. (sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
  638. sess->exp_cmd_sn, sess->max_cmd_sn,
  639. sess->init_task_tag, sess->targ_xfer_tag);
  640. rb += sprintf(page+rb, "----------------------[iSCSI"
  641. " Connections]-------------------------\n");
  642. spin_lock(&sess->conn_lock);
  643. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  644. rb += sprintf(page+rb, "CID: %hu Connection"
  645. " State: ", conn->cid);
  646. switch (conn->conn_state) {
  647. case TARG_CONN_STATE_FREE:
  648. rb += sprintf(page+rb,
  649. "TARG_CONN_STATE_FREE\n");
  650. break;
  651. case TARG_CONN_STATE_XPT_UP:
  652. rb += sprintf(page+rb,
  653. "TARG_CONN_STATE_XPT_UP\n");
  654. break;
  655. case TARG_CONN_STATE_IN_LOGIN:
  656. rb += sprintf(page+rb,
  657. "TARG_CONN_STATE_IN_LOGIN\n");
  658. break;
  659. case TARG_CONN_STATE_LOGGED_IN:
  660. rb += sprintf(page+rb,
  661. "TARG_CONN_STATE_LOGGED_IN\n");
  662. break;
  663. case TARG_CONN_STATE_IN_LOGOUT:
  664. rb += sprintf(page+rb,
  665. "TARG_CONN_STATE_IN_LOGOUT\n");
  666. break;
  667. case TARG_CONN_STATE_LOGOUT_REQUESTED:
  668. rb += sprintf(page+rb,
  669. "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
  670. break;
  671. case TARG_CONN_STATE_CLEANUP_WAIT:
  672. rb += sprintf(page+rb,
  673. "TARG_CONN_STATE_CLEANUP_WAIT\n");
  674. break;
  675. default:
  676. rb += sprintf(page+rb,
  677. "ERROR: Unknown Connection State!\n");
  678. break;
  679. }
  680. rb += sprintf(page+rb, " Address %s %s", conn->login_ip,
  681. (conn->network_transport == ISCSI_TCP) ?
  682. "TCP" : "SCTP");
  683. rb += sprintf(page+rb, " StatSN: 0x%08x\n",
  684. conn->stat_sn);
  685. }
  686. spin_unlock(&sess->conn_lock);
  687. }
  688. spin_unlock_bh(&se_nacl->nacl_sess_lock);
  689. return rb;
  690. }
  691. TF_NACL_BASE_ATTR_RO(lio_target, info);
  692. static ssize_t lio_target_nacl_show_cmdsn_depth(
  693. struct se_node_acl *se_nacl,
  694. char *page)
  695. {
  696. return sprintf(page, "%u\n", se_nacl->queue_depth);
  697. }
  698. static ssize_t lio_target_nacl_store_cmdsn_depth(
  699. struct se_node_acl *se_nacl,
  700. const char *page,
  701. size_t count)
  702. {
  703. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  704. struct iscsi_portal_group *tpg = container_of(se_tpg,
  705. struct iscsi_portal_group, tpg_se_tpg);
  706. struct config_item *acl_ci, *tpg_ci, *wwn_ci;
  707. u32 cmdsn_depth = 0;
  708. int ret;
  709. ret = kstrtou32(page, 0, &cmdsn_depth);
  710. if (ret)
  711. return ret;
  712. if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
  713. pr_err("Passed cmdsn_depth: %u exceeds"
  714. " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
  715. TA_DEFAULT_CMDSN_DEPTH_MAX);
  716. return -EINVAL;
  717. }
  718. acl_ci = &se_nacl->acl_group.cg_item;
  719. if (!acl_ci) {
  720. pr_err("Unable to locatel acl_ci\n");
  721. return -EINVAL;
  722. }
  723. tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
  724. if (!tpg_ci) {
  725. pr_err("Unable to locate tpg_ci\n");
  726. return -EINVAL;
  727. }
  728. wwn_ci = &tpg_ci->ci_group->cg_item;
  729. if (!wwn_ci) {
  730. pr_err("Unable to locate config_item wwn_ci\n");
  731. return -EINVAL;
  732. }
  733. if (iscsit_get_tpg(tpg) < 0)
  734. return -EINVAL;
  735. /*
  736. * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
  737. */
  738. ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
  739. config_item_name(acl_ci), cmdsn_depth, 1);
  740. pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
  741. "InitiatorName: %s\n", config_item_name(wwn_ci),
  742. config_item_name(tpg_ci), cmdsn_depth,
  743. config_item_name(acl_ci));
  744. iscsit_put_tpg(tpg);
  745. return (!ret) ? count : (ssize_t)ret;
  746. }
  747. TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
  748. static ssize_t lio_target_nacl_show_tag(
  749. struct se_node_acl *se_nacl,
  750. char *page)
  751. {
  752. return snprintf(page, PAGE_SIZE, "%s", se_nacl->acl_tag);
  753. }
  754. static ssize_t lio_target_nacl_store_tag(
  755. struct se_node_acl *se_nacl,
  756. const char *page,
  757. size_t count)
  758. {
  759. int ret;
  760. ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
  761. if (ret < 0)
  762. return ret;
  763. return count;
  764. }
  765. TF_NACL_BASE_ATTR(lio_target, tag, S_IRUGO | S_IWUSR);
  766. static struct configfs_attribute *lio_target_initiator_attrs[] = {
  767. &lio_target_nacl_info.attr,
  768. &lio_target_nacl_cmdsn_depth.attr,
  769. &lio_target_nacl_tag.attr,
  770. NULL,
  771. };
  772. static struct se_node_acl *lio_tpg_alloc_fabric_acl(
  773. struct se_portal_group *se_tpg)
  774. {
  775. struct iscsi_node_acl *acl;
  776. acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
  777. if (!acl) {
  778. pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
  779. return NULL;
  780. }
  781. return &acl->se_node_acl;
  782. }
  783. static struct se_node_acl *lio_target_make_nodeacl(
  784. struct se_portal_group *se_tpg,
  785. struct config_group *group,
  786. const char *name)
  787. {
  788. struct config_group *stats_cg;
  789. struct iscsi_node_acl *acl;
  790. struct se_node_acl *se_nacl_new, *se_nacl;
  791. struct iscsi_portal_group *tpg = container_of(se_tpg,
  792. struct iscsi_portal_group, tpg_se_tpg);
  793. u32 cmdsn_depth;
  794. se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
  795. if (!se_nacl_new)
  796. return ERR_PTR(-ENOMEM);
  797. cmdsn_depth = tpg->tpg_attrib.default_cmdsn_depth;
  798. /*
  799. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  800. * when converting a NdoeACL from demo mode -> explict
  801. */
  802. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  803. name, cmdsn_depth);
  804. if (IS_ERR(se_nacl))
  805. return se_nacl;
  806. acl = container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
  807. stats_cg = &se_nacl->acl_fabric_stat_group;
  808. stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  809. GFP_KERNEL);
  810. if (!stats_cg->default_groups) {
  811. pr_err("Unable to allocate memory for"
  812. " stats_cg->default_groups\n");
  813. core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
  814. kfree(acl);
  815. return ERR_PTR(-ENOMEM);
  816. }
  817. stats_cg->default_groups[0] = &acl->node_stat_grps.iscsi_sess_stats_group;
  818. stats_cg->default_groups[1] = NULL;
  819. config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
  820. "iscsi_sess_stats", &iscsi_stat_sess_cit);
  821. return se_nacl;
  822. }
  823. static void lio_target_drop_nodeacl(
  824. struct se_node_acl *se_nacl)
  825. {
  826. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  827. struct iscsi_node_acl *acl = container_of(se_nacl,
  828. struct iscsi_node_acl, se_node_acl);
  829. struct config_item *df_item;
  830. struct config_group *stats_cg;
  831. int i;
  832. stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
  833. for (i = 0; stats_cg->default_groups[i]; i++) {
  834. df_item = &stats_cg->default_groups[i]->cg_item;
  835. stats_cg->default_groups[i] = NULL;
  836. config_item_put(df_item);
  837. }
  838. kfree(stats_cg->default_groups);
  839. core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
  840. kfree(acl);
  841. }
  842. /* End items for lio_target_acl_cit */
  843. /* Start items for lio_target_tpg_attrib_cit */
  844. #define DEF_TPG_ATTRIB(name) \
  845. \
  846. static ssize_t iscsi_tpg_attrib_show_##name( \
  847. struct se_portal_group *se_tpg, \
  848. char *page) \
  849. { \
  850. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  851. struct iscsi_portal_group, tpg_se_tpg); \
  852. ssize_t rb; \
  853. \
  854. if (iscsit_get_tpg(tpg) < 0) \
  855. return -EINVAL; \
  856. \
  857. rb = sprintf(page, "%u\n", tpg->tpg_attrib.name); \
  858. iscsit_put_tpg(tpg); \
  859. return rb; \
  860. } \
  861. \
  862. static ssize_t iscsi_tpg_attrib_store_##name( \
  863. struct se_portal_group *se_tpg, \
  864. const char *page, \
  865. size_t count) \
  866. { \
  867. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  868. struct iscsi_portal_group, tpg_se_tpg); \
  869. u32 val; \
  870. int ret; \
  871. \
  872. if (iscsit_get_tpg(tpg) < 0) \
  873. return -EINVAL; \
  874. \
  875. ret = kstrtou32(page, 0, &val); \
  876. if (ret) \
  877. goto out; \
  878. ret = iscsit_ta_##name(tpg, val); \
  879. if (ret < 0) \
  880. goto out; \
  881. \
  882. iscsit_put_tpg(tpg); \
  883. return count; \
  884. out: \
  885. iscsit_put_tpg(tpg); \
  886. return ret; \
  887. }
  888. #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
  889. /*
  890. * Define iscsi_tpg_attrib_s_authentication
  891. */
  892. DEF_TPG_ATTRIB(authentication);
  893. TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
  894. /*
  895. * Define iscsi_tpg_attrib_s_login_timeout
  896. */
  897. DEF_TPG_ATTRIB(login_timeout);
  898. TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
  899. /*
  900. * Define iscsi_tpg_attrib_s_netif_timeout
  901. */
  902. DEF_TPG_ATTRIB(netif_timeout);
  903. TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
  904. /*
  905. * Define iscsi_tpg_attrib_s_generate_node_acls
  906. */
  907. DEF_TPG_ATTRIB(generate_node_acls);
  908. TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
  909. /*
  910. * Define iscsi_tpg_attrib_s_default_cmdsn_depth
  911. */
  912. DEF_TPG_ATTRIB(default_cmdsn_depth);
  913. TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
  914. /*
  915. Define iscsi_tpg_attrib_s_cache_dynamic_acls
  916. */
  917. DEF_TPG_ATTRIB(cache_dynamic_acls);
  918. TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
  919. /*
  920. * Define iscsi_tpg_attrib_s_demo_mode_write_protect
  921. */
  922. DEF_TPG_ATTRIB(demo_mode_write_protect);
  923. TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
  924. /*
  925. * Define iscsi_tpg_attrib_s_prod_mode_write_protect
  926. */
  927. DEF_TPG_ATTRIB(prod_mode_write_protect);
  928. TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
  929. /*
  930. * Define iscsi_tpg_attrib_s_demo_mode_discovery,
  931. */
  932. DEF_TPG_ATTRIB(demo_mode_discovery);
  933. TPG_ATTR(demo_mode_discovery, S_IRUGO | S_IWUSR);
  934. static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
  935. &iscsi_tpg_attrib_authentication.attr,
  936. &iscsi_tpg_attrib_login_timeout.attr,
  937. &iscsi_tpg_attrib_netif_timeout.attr,
  938. &iscsi_tpg_attrib_generate_node_acls.attr,
  939. &iscsi_tpg_attrib_default_cmdsn_depth.attr,
  940. &iscsi_tpg_attrib_cache_dynamic_acls.attr,
  941. &iscsi_tpg_attrib_demo_mode_write_protect.attr,
  942. &iscsi_tpg_attrib_prod_mode_write_protect.attr,
  943. &iscsi_tpg_attrib_demo_mode_discovery.attr,
  944. NULL,
  945. };
  946. /* End items for lio_target_tpg_attrib_cit */
  947. /* Start items for lio_target_tpg_auth_cit */
  948. #define __DEF_TPG_AUTH_STR(prefix, name, flags) \
  949. static ssize_t __iscsi_##prefix##_show_##name( \
  950. struct se_portal_group *se_tpg, \
  951. char *page) \
  952. { \
  953. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  954. struct iscsi_portal_group, tpg_se_tpg); \
  955. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  956. \
  957. if (!capable(CAP_SYS_ADMIN)) \
  958. return -EPERM; \
  959. \
  960. return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
  961. } \
  962. \
  963. static ssize_t __iscsi_##prefix##_store_##name( \
  964. struct se_portal_group *se_tpg, \
  965. const char *page, \
  966. size_t count) \
  967. { \
  968. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  969. struct iscsi_portal_group, tpg_se_tpg); \
  970. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  971. \
  972. if (!capable(CAP_SYS_ADMIN)) \
  973. return -EPERM; \
  974. \
  975. snprintf(auth->name, sizeof(auth->name), "%s", page); \
  976. if (!(strncmp("NULL", auth->name, 4))) \
  977. auth->naf_flags &= ~flags; \
  978. else \
  979. auth->naf_flags |= flags; \
  980. \
  981. if ((auth->naf_flags & NAF_USERID_IN_SET) && \
  982. (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
  983. auth->authenticate_target = 1; \
  984. else \
  985. auth->authenticate_target = 0; \
  986. \
  987. return count; \
  988. }
  989. #define __DEF_TPG_AUTH_INT(prefix, name) \
  990. static ssize_t __iscsi_##prefix##_show_##name( \
  991. struct se_portal_group *se_tpg, \
  992. char *page) \
  993. { \
  994. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  995. struct iscsi_portal_group, tpg_se_tpg); \
  996. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  997. \
  998. if (!capable(CAP_SYS_ADMIN)) \
  999. return -EPERM; \
  1000. \
  1001. return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
  1002. }
  1003. #define DEF_TPG_AUTH_STR(name, flags) \
  1004. __DEF_TPG_AUTH_STR(tpg_auth, name, flags) \
  1005. static ssize_t iscsi_tpg_auth_show_##name( \
  1006. struct se_portal_group *se_tpg, \
  1007. char *page) \
  1008. { \
  1009. return __iscsi_tpg_auth_show_##name(se_tpg, page); \
  1010. } \
  1011. \
  1012. static ssize_t iscsi_tpg_auth_store_##name( \
  1013. struct se_portal_group *se_tpg, \
  1014. const char *page, \
  1015. size_t count) \
  1016. { \
  1017. return __iscsi_tpg_auth_store_##name(se_tpg, page, count); \
  1018. }
  1019. #define DEF_TPG_AUTH_INT(name) \
  1020. __DEF_TPG_AUTH_INT(tpg_auth, name) \
  1021. static ssize_t iscsi_tpg_auth_show_##name( \
  1022. struct se_portal_group *se_tpg, \
  1023. char *page) \
  1024. { \
  1025. return __iscsi_tpg_auth_show_##name(se_tpg, page); \
  1026. }
  1027. #define TPG_AUTH_ATTR(_name, _mode) TF_TPG_AUTH_ATTR(iscsi, _name, _mode);
  1028. #define TPG_AUTH_ATTR_RO(_name) TF_TPG_AUTH_ATTR_RO(iscsi, _name);
  1029. /*
  1030. * * One-way authentication userid
  1031. * */
  1032. DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
  1033. TPG_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  1034. /*
  1035. * * One-way authentication password
  1036. * */
  1037. DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
  1038. TPG_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  1039. /*
  1040. * * Enforce mutual authentication
  1041. * */
  1042. DEF_TPG_AUTH_INT(authenticate_target);
  1043. TPG_AUTH_ATTR_RO(authenticate_target);
  1044. /*
  1045. * * Mutual authentication userid
  1046. * */
  1047. DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  1048. TPG_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  1049. /*
  1050. * * Mutual authentication password
  1051. * */
  1052. DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  1053. TPG_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  1054. static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
  1055. &iscsi_tpg_auth_userid.attr,
  1056. &iscsi_tpg_auth_password.attr,
  1057. &iscsi_tpg_auth_authenticate_target.attr,
  1058. &iscsi_tpg_auth_userid_mutual.attr,
  1059. &iscsi_tpg_auth_password_mutual.attr,
  1060. NULL,
  1061. };
  1062. /* End items for lio_target_tpg_auth_cit */
  1063. /* Start items for lio_target_tpg_param_cit */
  1064. #define DEF_TPG_PARAM(name) \
  1065. static ssize_t iscsi_tpg_param_show_##name( \
  1066. struct se_portal_group *se_tpg, \
  1067. char *page) \
  1068. { \
  1069. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  1070. struct iscsi_portal_group, tpg_se_tpg); \
  1071. struct iscsi_param *param; \
  1072. ssize_t rb; \
  1073. \
  1074. if (iscsit_get_tpg(tpg) < 0) \
  1075. return -EINVAL; \
  1076. \
  1077. param = iscsi_find_param_from_key(__stringify(name), \
  1078. tpg->param_list); \
  1079. if (!param) { \
  1080. iscsit_put_tpg(tpg); \
  1081. return -EINVAL; \
  1082. } \
  1083. rb = snprintf(page, PAGE_SIZE, "%s\n", param->value); \
  1084. \
  1085. iscsit_put_tpg(tpg); \
  1086. return rb; \
  1087. } \
  1088. static ssize_t iscsi_tpg_param_store_##name( \
  1089. struct se_portal_group *se_tpg, \
  1090. const char *page, \
  1091. size_t count) \
  1092. { \
  1093. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  1094. struct iscsi_portal_group, tpg_se_tpg); \
  1095. char *buf; \
  1096. int ret, len; \
  1097. \
  1098. buf = kzalloc(PAGE_SIZE, GFP_KERNEL); \
  1099. if (!buf) \
  1100. return -ENOMEM; \
  1101. len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page); \
  1102. if (isspace(buf[len-1])) \
  1103. buf[len-1] = '\0'; /* Kill newline */ \
  1104. \
  1105. if (iscsit_get_tpg(tpg) < 0) { \
  1106. kfree(buf); \
  1107. return -EINVAL; \
  1108. } \
  1109. \
  1110. ret = iscsi_change_param_value(buf, tpg->param_list, 1); \
  1111. if (ret < 0) \
  1112. goto out; \
  1113. \
  1114. kfree(buf); \
  1115. iscsit_put_tpg(tpg); \
  1116. return count; \
  1117. out: \
  1118. kfree(buf); \
  1119. iscsit_put_tpg(tpg); \
  1120. return -EINVAL; \
  1121. }
  1122. #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
  1123. DEF_TPG_PARAM(AuthMethod);
  1124. TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
  1125. DEF_TPG_PARAM(HeaderDigest);
  1126. TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
  1127. DEF_TPG_PARAM(DataDigest);
  1128. TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
  1129. DEF_TPG_PARAM(MaxConnections);
  1130. TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
  1131. DEF_TPG_PARAM(TargetAlias);
  1132. TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
  1133. DEF_TPG_PARAM(InitialR2T);
  1134. TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
  1135. DEF_TPG_PARAM(ImmediateData);
  1136. TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
  1137. DEF_TPG_PARAM(MaxRecvDataSegmentLength);
  1138. TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
  1139. DEF_TPG_PARAM(MaxXmitDataSegmentLength);
  1140. TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
  1141. DEF_TPG_PARAM(MaxBurstLength);
  1142. TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
  1143. DEF_TPG_PARAM(FirstBurstLength);
  1144. TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
  1145. DEF_TPG_PARAM(DefaultTime2Wait);
  1146. TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
  1147. DEF_TPG_PARAM(DefaultTime2Retain);
  1148. TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
  1149. DEF_TPG_PARAM(MaxOutstandingR2T);
  1150. TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
  1151. DEF_TPG_PARAM(DataPDUInOrder);
  1152. TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
  1153. DEF_TPG_PARAM(DataSequenceInOrder);
  1154. TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
  1155. DEF_TPG_PARAM(ErrorRecoveryLevel);
  1156. TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
  1157. DEF_TPG_PARAM(IFMarker);
  1158. TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
  1159. DEF_TPG_PARAM(OFMarker);
  1160. TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
  1161. DEF_TPG_PARAM(IFMarkInt);
  1162. TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
  1163. DEF_TPG_PARAM(OFMarkInt);
  1164. TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
  1165. static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
  1166. &iscsi_tpg_param_AuthMethod.attr,
  1167. &iscsi_tpg_param_HeaderDigest.attr,
  1168. &iscsi_tpg_param_DataDigest.attr,
  1169. &iscsi_tpg_param_MaxConnections.attr,
  1170. &iscsi_tpg_param_TargetAlias.attr,
  1171. &iscsi_tpg_param_InitialR2T.attr,
  1172. &iscsi_tpg_param_ImmediateData.attr,
  1173. &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
  1174. &iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
  1175. &iscsi_tpg_param_MaxBurstLength.attr,
  1176. &iscsi_tpg_param_FirstBurstLength.attr,
  1177. &iscsi_tpg_param_DefaultTime2Wait.attr,
  1178. &iscsi_tpg_param_DefaultTime2Retain.attr,
  1179. &iscsi_tpg_param_MaxOutstandingR2T.attr,
  1180. &iscsi_tpg_param_DataPDUInOrder.attr,
  1181. &iscsi_tpg_param_DataSequenceInOrder.attr,
  1182. &iscsi_tpg_param_ErrorRecoveryLevel.attr,
  1183. &iscsi_tpg_param_IFMarker.attr,
  1184. &iscsi_tpg_param_OFMarker.attr,
  1185. &iscsi_tpg_param_IFMarkInt.attr,
  1186. &iscsi_tpg_param_OFMarkInt.attr,
  1187. NULL,
  1188. };
  1189. /* End items for lio_target_tpg_param_cit */
  1190. /* Start items for lio_target_tpg_cit */
  1191. static ssize_t lio_target_tpg_show_enable(
  1192. struct se_portal_group *se_tpg,
  1193. char *page)
  1194. {
  1195. struct iscsi_portal_group *tpg = container_of(se_tpg,
  1196. struct iscsi_portal_group, tpg_se_tpg);
  1197. ssize_t len;
  1198. spin_lock(&tpg->tpg_state_lock);
  1199. len = sprintf(page, "%d\n",
  1200. (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
  1201. spin_unlock(&tpg->tpg_state_lock);
  1202. return len;
  1203. }
  1204. static ssize_t lio_target_tpg_store_enable(
  1205. struct se_portal_group *se_tpg,
  1206. const char *page,
  1207. size_t count)
  1208. {
  1209. struct iscsi_portal_group *tpg = container_of(se_tpg,
  1210. struct iscsi_portal_group, tpg_se_tpg);
  1211. u32 op;
  1212. int ret;
  1213. ret = kstrtou32(page, 0, &op);
  1214. if (ret)
  1215. return ret;
  1216. if ((op != 1) && (op != 0)) {
  1217. pr_err("Illegal value for tpg_enable: %u\n", op);
  1218. return -EINVAL;
  1219. }
  1220. ret = iscsit_get_tpg(tpg);
  1221. if (ret < 0)
  1222. return -EINVAL;
  1223. if (op) {
  1224. ret = iscsit_tpg_enable_portal_group(tpg);
  1225. if (ret < 0)
  1226. goto out;
  1227. } else {
  1228. /*
  1229. * iscsit_tpg_disable_portal_group() assumes force=1
  1230. */
  1231. ret = iscsit_tpg_disable_portal_group(tpg, 1);
  1232. if (ret < 0)
  1233. goto out;
  1234. }
  1235. iscsit_put_tpg(tpg);
  1236. return count;
  1237. out:
  1238. iscsit_put_tpg(tpg);
  1239. return -EINVAL;
  1240. }
  1241. TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
  1242. static struct configfs_attribute *lio_target_tpg_attrs[] = {
  1243. &lio_target_tpg_enable.attr,
  1244. NULL,
  1245. };
  1246. /* End items for lio_target_tpg_cit */
  1247. /* Start items for lio_target_tiqn_cit */
  1248. static struct se_portal_group *lio_target_tiqn_addtpg(
  1249. struct se_wwn *wwn,
  1250. struct config_group *group,
  1251. const char *name)
  1252. {
  1253. struct iscsi_portal_group *tpg;
  1254. struct iscsi_tiqn *tiqn;
  1255. char *tpgt_str;
  1256. int ret;
  1257. u16 tpgt;
  1258. tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
  1259. /*
  1260. * Only tpgt_# directory groups can be created below
  1261. * target/iscsi/iqn.superturodiskarry/
  1262. */
  1263. tpgt_str = strstr(name, "tpgt_");
  1264. if (!tpgt_str) {
  1265. pr_err("Unable to locate \"tpgt_#\" directory"
  1266. " group\n");
  1267. return NULL;
  1268. }
  1269. tpgt_str += 5; /* Skip ahead of "tpgt_" */
  1270. ret = kstrtou16(tpgt_str, 0, &tpgt);
  1271. if (ret)
  1272. return NULL;
  1273. tpg = iscsit_alloc_portal_group(tiqn, tpgt);
  1274. if (!tpg)
  1275. return NULL;
  1276. ret = core_tpg_register(
  1277. &lio_target_fabric_configfs->tf_ops,
  1278. wwn, &tpg->tpg_se_tpg, tpg,
  1279. TRANSPORT_TPG_TYPE_NORMAL);
  1280. if (ret < 0)
  1281. return NULL;
  1282. ret = iscsit_tpg_add_portal_group(tiqn, tpg);
  1283. if (ret != 0)
  1284. goto out;
  1285. pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
  1286. pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
  1287. name);
  1288. return &tpg->tpg_se_tpg;
  1289. out:
  1290. core_tpg_deregister(&tpg->tpg_se_tpg);
  1291. kfree(tpg);
  1292. return NULL;
  1293. }
  1294. static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
  1295. {
  1296. struct iscsi_portal_group *tpg;
  1297. struct iscsi_tiqn *tiqn;
  1298. tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
  1299. tiqn = tpg->tpg_tiqn;
  1300. /*
  1301. * iscsit_tpg_del_portal_group() assumes force=1
  1302. */
  1303. pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
  1304. iscsit_tpg_del_portal_group(tiqn, tpg, 1);
  1305. }
  1306. /* End items for lio_target_tiqn_cit */
  1307. /* Start LIO-Target TIQN struct contig_item lio_target_cit */
  1308. static ssize_t lio_target_wwn_show_attr_lio_version(
  1309. struct target_fabric_configfs *tf,
  1310. char *page)
  1311. {
  1312. return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
  1313. }
  1314. TF_WWN_ATTR_RO(lio_target, lio_version);
  1315. static struct configfs_attribute *lio_target_wwn_attrs[] = {
  1316. &lio_target_wwn_lio_version.attr,
  1317. NULL,
  1318. };
  1319. static struct se_wwn *lio_target_call_coreaddtiqn(
  1320. struct target_fabric_configfs *tf,
  1321. struct config_group *group,
  1322. const char *name)
  1323. {
  1324. struct config_group *stats_cg;
  1325. struct iscsi_tiqn *tiqn;
  1326. tiqn = iscsit_add_tiqn((unsigned char *)name);
  1327. if (IS_ERR(tiqn))
  1328. return ERR_CAST(tiqn);
  1329. /*
  1330. * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
  1331. */
  1332. stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
  1333. stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
  1334. GFP_KERNEL);
  1335. if (!stats_cg->default_groups) {
  1336. pr_err("Unable to allocate memory for"
  1337. " stats_cg->default_groups\n");
  1338. iscsit_del_tiqn(tiqn);
  1339. return ERR_PTR(-ENOMEM);
  1340. }
  1341. stats_cg->default_groups[0] = &tiqn->tiqn_stat_grps.iscsi_instance_group;
  1342. stats_cg->default_groups[1] = &tiqn->tiqn_stat_grps.iscsi_sess_err_group;
  1343. stats_cg->default_groups[2] = &tiqn->tiqn_stat_grps.iscsi_tgt_attr_group;
  1344. stats_cg->default_groups[3] = &tiqn->tiqn_stat_grps.iscsi_login_stats_group;
  1345. stats_cg->default_groups[4] = &tiqn->tiqn_stat_grps.iscsi_logout_stats_group;
  1346. stats_cg->default_groups[5] = NULL;
  1347. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
  1348. "iscsi_instance", &iscsi_stat_instance_cit);
  1349. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
  1350. "iscsi_sess_err", &iscsi_stat_sess_err_cit);
  1351. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
  1352. "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
  1353. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
  1354. "iscsi_login_stats", &iscsi_stat_login_cit);
  1355. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
  1356. "iscsi_logout_stats", &iscsi_stat_logout_cit);
  1357. pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
  1358. pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
  1359. " %s\n", name);
  1360. return &tiqn->tiqn_wwn;
  1361. }
  1362. static void lio_target_call_coredeltiqn(
  1363. struct se_wwn *wwn)
  1364. {
  1365. struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
  1366. struct config_item *df_item;
  1367. struct config_group *stats_cg;
  1368. int i;
  1369. stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
  1370. for (i = 0; stats_cg->default_groups[i]; i++) {
  1371. df_item = &stats_cg->default_groups[i]->cg_item;
  1372. stats_cg->default_groups[i] = NULL;
  1373. config_item_put(df_item);
  1374. }
  1375. kfree(stats_cg->default_groups);
  1376. pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
  1377. tiqn->tiqn);
  1378. iscsit_del_tiqn(tiqn);
  1379. }
  1380. /* End LIO-Target TIQN struct contig_lio_target_cit */
  1381. /* Start lio_target_discovery_auth_cit */
  1382. #define DEF_DISC_AUTH_STR(name, flags) \
  1383. __DEF_NACL_AUTH_STR(disc, name, flags) \
  1384. static ssize_t iscsi_disc_show_##name( \
  1385. struct target_fabric_configfs *tf, \
  1386. char *page) \
  1387. { \
  1388. return __iscsi_disc_show_##name(&iscsit_global->discovery_acl, \
  1389. page); \
  1390. } \
  1391. static ssize_t iscsi_disc_store_##name( \
  1392. struct target_fabric_configfs *tf, \
  1393. const char *page, \
  1394. size_t count) \
  1395. { \
  1396. return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
  1397. page, count); \
  1398. }
  1399. #define DEF_DISC_AUTH_INT(name) \
  1400. __DEF_NACL_AUTH_INT(disc, name) \
  1401. static ssize_t iscsi_disc_show_##name( \
  1402. struct target_fabric_configfs *tf, \
  1403. char *page) \
  1404. { \
  1405. return __iscsi_disc_show_##name(&iscsit_global->discovery_acl, \
  1406. page); \
  1407. }
  1408. #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
  1409. #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
  1410. /*
  1411. * One-way authentication userid
  1412. */
  1413. DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
  1414. DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  1415. /*
  1416. * One-way authentication password
  1417. */
  1418. DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
  1419. DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  1420. /*
  1421. * Enforce mutual authentication
  1422. */
  1423. DEF_DISC_AUTH_INT(authenticate_target);
  1424. DISC_AUTH_ATTR_RO(authenticate_target);
  1425. /*
  1426. * Mutual authentication userid
  1427. */
  1428. DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  1429. DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  1430. /*
  1431. * Mutual authentication password
  1432. */
  1433. DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  1434. DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  1435. /*
  1436. * enforce_discovery_auth
  1437. */
  1438. static ssize_t iscsi_disc_show_enforce_discovery_auth(
  1439. struct target_fabric_configfs *tf,
  1440. char *page)
  1441. {
  1442. struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
  1443. return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
  1444. }
  1445. static ssize_t iscsi_disc_store_enforce_discovery_auth(
  1446. struct target_fabric_configfs *tf,
  1447. const char *page,
  1448. size_t count)
  1449. {
  1450. struct iscsi_param *param;
  1451. struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
  1452. u32 op;
  1453. int err;
  1454. err = kstrtou32(page, 0, &op);
  1455. if (err)
  1456. return -EINVAL;
  1457. if ((op != 1) && (op != 0)) {
  1458. pr_err("Illegal value for enforce_discovery_auth:"
  1459. " %u\n", op);
  1460. return -EINVAL;
  1461. }
  1462. if (!discovery_tpg) {
  1463. pr_err("iscsit_global->discovery_tpg is NULL\n");
  1464. return -EINVAL;
  1465. }
  1466. param = iscsi_find_param_from_key(AUTHMETHOD,
  1467. discovery_tpg->param_list);
  1468. if (!param)
  1469. return -EINVAL;
  1470. if (op) {
  1471. /*
  1472. * Reset the AuthMethod key to CHAP.
  1473. */
  1474. if (iscsi_update_param_value(param, CHAP) < 0)
  1475. return -EINVAL;
  1476. discovery_tpg->tpg_attrib.authentication = 1;
  1477. iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
  1478. pr_debug("LIO-CORE[0] Successfully enabled"
  1479. " authentication enforcement for iSCSI"
  1480. " Discovery TPG\n");
  1481. } else {
  1482. /*
  1483. * Reset the AuthMethod key to CHAP,None
  1484. */
  1485. if (iscsi_update_param_value(param, "CHAP,None") < 0)
  1486. return -EINVAL;
  1487. discovery_tpg->tpg_attrib.authentication = 0;
  1488. iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
  1489. pr_debug("LIO-CORE[0] Successfully disabled"
  1490. " authentication enforcement for iSCSI"
  1491. " Discovery TPG\n");
  1492. }
  1493. return count;
  1494. }
  1495. DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
  1496. static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
  1497. &iscsi_disc_userid.attr,
  1498. &iscsi_disc_password.attr,
  1499. &iscsi_disc_authenticate_target.attr,
  1500. &iscsi_disc_userid_mutual.attr,
  1501. &iscsi_disc_password_mutual.attr,
  1502. &iscsi_disc_enforce_discovery_auth.attr,
  1503. NULL,
  1504. };
  1505. /* End lio_target_discovery_auth_cit */
  1506. /* Start functions for target_core_fabric_ops */
  1507. static char *iscsi_get_fabric_name(void)
  1508. {
  1509. return "iSCSI";
  1510. }
  1511. static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
  1512. {
  1513. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1514. /* only used for printks or comparism with ->ref_task_tag */
  1515. return (__force u32)cmd->init_task_tag;
  1516. }
  1517. static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
  1518. {
  1519. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1520. return cmd->i_state;
  1521. }
  1522. static u32 lio_sess_get_index(struct se_session *se_sess)
  1523. {
  1524. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1525. return sess->session_index;
  1526. }
  1527. static u32 lio_sess_get_initiator_sid(
  1528. struct se_session *se_sess,
  1529. unsigned char *buf,
  1530. u32 size)
  1531. {
  1532. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1533. /*
  1534. * iSCSI Initiator Session Identifier from RFC-3720.
  1535. */
  1536. return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
  1537. sess->isid[0], sess->isid[1], sess->isid[2],
  1538. sess->isid[3], sess->isid[4], sess->isid[5]);
  1539. }
  1540. static int lio_queue_data_in(struct se_cmd *se_cmd)
  1541. {
  1542. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1543. cmd->i_state = ISTATE_SEND_DATAIN;
  1544. cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
  1545. return 0;
  1546. }
  1547. static int lio_write_pending(struct se_cmd *se_cmd)
  1548. {
  1549. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1550. struct iscsi_conn *conn = cmd->conn;
  1551. if (!cmd->immediate_data && !cmd->unsolicited_data)
  1552. return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
  1553. return 0;
  1554. }
  1555. static int lio_write_pending_status(struct se_cmd *se_cmd)
  1556. {
  1557. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1558. int ret;
  1559. spin_lock_bh(&cmd->istate_lock);
  1560. ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
  1561. spin_unlock_bh(&cmd->istate_lock);
  1562. return ret;
  1563. }
  1564. static int lio_queue_status(struct se_cmd *se_cmd)
  1565. {
  1566. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1567. cmd->i_state = ISTATE_SEND_STATUS;
  1568. cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
  1569. return 0;
  1570. }
  1571. static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
  1572. {
  1573. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1574. cmd->i_state = ISTATE_SEND_TASKMGTRSP;
  1575. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  1576. }
  1577. static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
  1578. {
  1579. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1580. return &tpg->tpg_tiqn->tiqn[0];
  1581. }
  1582. static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
  1583. {
  1584. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1585. return tpg->tpgt;
  1586. }
  1587. static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
  1588. {
  1589. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1590. return tpg->tpg_attrib.default_cmdsn_depth;
  1591. }
  1592. static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
  1593. {
  1594. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1595. return tpg->tpg_attrib.generate_node_acls;
  1596. }
  1597. static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
  1598. {
  1599. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1600. return tpg->tpg_attrib.cache_dynamic_acls;
  1601. }
  1602. static int lio_tpg_check_demo_mode_write_protect(
  1603. struct se_portal_group *se_tpg)
  1604. {
  1605. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1606. return tpg->tpg_attrib.demo_mode_write_protect;
  1607. }
  1608. static int lio_tpg_check_prod_mode_write_protect(
  1609. struct se_portal_group *se_tpg)
  1610. {
  1611. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1612. return tpg->tpg_attrib.prod_mode_write_protect;
  1613. }
  1614. static void lio_tpg_release_fabric_acl(
  1615. struct se_portal_group *se_tpg,
  1616. struct se_node_acl *se_acl)
  1617. {
  1618. struct iscsi_node_acl *acl = container_of(se_acl,
  1619. struct iscsi_node_acl, se_node_acl);
  1620. kfree(acl);
  1621. }
  1622. /*
  1623. * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
  1624. *
  1625. * Also, this function calls iscsit_inc_session_usage_count() on the
  1626. * struct iscsi_session in question.
  1627. */
  1628. static int lio_tpg_shutdown_session(struct se_session *se_sess)
  1629. {
  1630. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1631. spin_lock(&sess->conn_lock);
  1632. if (atomic_read(&sess->session_fall_back_to_erl0) ||
  1633. atomic_read(&sess->session_logout) ||
  1634. (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
  1635. spin_unlock(&sess->conn_lock);
  1636. return 0;
  1637. }
  1638. atomic_set(&sess->session_reinstatement, 1);
  1639. spin_unlock(&sess->conn_lock);
  1640. iscsit_stop_time2retain_timer(sess);
  1641. iscsit_stop_session(sess, 1, 1);
  1642. return 1;
  1643. }
  1644. /*
  1645. * Calls iscsit_dec_session_usage_count() as inverse of
  1646. * lio_tpg_shutdown_session()
  1647. */
  1648. static void lio_tpg_close_session(struct se_session *se_sess)
  1649. {
  1650. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1651. /*
  1652. * If the iSCSI Session for the iSCSI Initiator Node exists,
  1653. * forcefully shutdown the iSCSI NEXUS.
  1654. */
  1655. iscsit_close_session(sess);
  1656. }
  1657. static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
  1658. {
  1659. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1660. return tpg->tpg_tiqn->tiqn_index;
  1661. }
  1662. static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
  1663. {
  1664. struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
  1665. se_node_acl);
  1666. acl->node_attrib.nacl = acl;
  1667. iscsit_set_default_node_attribues(acl);
  1668. }
  1669. static int lio_check_stop_free(struct se_cmd *se_cmd)
  1670. {
  1671. return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  1672. }
  1673. static void lio_release_cmd(struct se_cmd *se_cmd)
  1674. {
  1675. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1676. pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
  1677. iscsit_release_cmd(cmd);
  1678. }
  1679. /* End functions for target_core_fabric_ops */
  1680. int iscsi_target_register_configfs(void)
  1681. {
  1682. struct target_fabric_configfs *fabric;
  1683. int ret;
  1684. lio_target_fabric_configfs = NULL;
  1685. fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
  1686. if (IS_ERR(fabric)) {
  1687. pr_err("target_fabric_configfs_init() for"
  1688. " LIO-Target failed!\n");
  1689. return PTR_ERR(fabric);
  1690. }
  1691. /*
  1692. * Setup the fabric API of function pointers used by target_core_mod..
  1693. */
  1694. fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
  1695. fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
  1696. fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
  1697. fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
  1698. fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
  1699. fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
  1700. fabric->tf_ops.tpg_get_pr_transport_id_len =
  1701. &iscsi_get_pr_transport_id_len;
  1702. fabric->tf_ops.tpg_parse_pr_out_transport_id =
  1703. &iscsi_parse_pr_out_transport_id;
  1704. fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
  1705. fabric->tf_ops.tpg_check_demo_mode_cache =
  1706. &lio_tpg_check_demo_mode_cache;
  1707. fabric->tf_ops.tpg_check_demo_mode_write_protect =
  1708. &lio_tpg_check_demo_mode_write_protect;
  1709. fabric->tf_ops.tpg_check_prod_mode_write_protect =
  1710. &lio_tpg_check_prod_mode_write_protect;
  1711. fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
  1712. fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
  1713. fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
  1714. fabric->tf_ops.check_stop_free = &lio_check_stop_free,
  1715. fabric->tf_ops.release_cmd = &lio_release_cmd;
  1716. fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
  1717. fabric->tf_ops.close_session = &lio_tpg_close_session;
  1718. fabric->tf_ops.sess_get_index = &lio_sess_get_index;
  1719. fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
  1720. fabric->tf_ops.write_pending = &lio_write_pending;
  1721. fabric->tf_ops.write_pending_status = &lio_write_pending_status;
  1722. fabric->tf_ops.set_default_node_attributes =
  1723. &lio_set_default_node_attributes;
  1724. fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
  1725. fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
  1726. fabric->tf_ops.queue_data_in = &lio_queue_data_in;
  1727. fabric->tf_ops.queue_status = &lio_queue_status;
  1728. fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
  1729. /*
  1730. * Setup function pointers for generic logic in target_core_fabric_configfs.c
  1731. */
  1732. fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
  1733. fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
  1734. fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
  1735. fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
  1736. fabric->tf_ops.fabric_post_link = NULL;
  1737. fabric->tf_ops.fabric_pre_unlink = NULL;
  1738. fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
  1739. fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
  1740. fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
  1741. fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
  1742. /*
  1743. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1744. * sturct config_item_type's
  1745. */
  1746. fabric->tf_cit_tmpl.tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
  1747. fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
  1748. fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
  1749. fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
  1750. fabric->tf_cit_tmpl.tfc_tpg_auth_cit.ct_attrs = lio_target_tpg_auth_attrs;
  1751. fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
  1752. fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
  1753. fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
  1754. fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
  1755. fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
  1756. fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
  1757. ret = target_fabric_configfs_register(fabric);
  1758. if (ret < 0) {
  1759. pr_err("target_fabric_configfs_register() for"
  1760. " LIO-Target failed!\n");
  1761. target_fabric_configfs_free(fabric);
  1762. return ret;
  1763. }
  1764. lio_target_fabric_configfs = fabric;
  1765. pr_debug("LIO_TARGET[0] - Set fabric ->"
  1766. " lio_target_fabric_configfs\n");
  1767. return 0;
  1768. }
  1769. void iscsi_target_deregister_configfs(void)
  1770. {
  1771. if (!lio_target_fabric_configfs)
  1772. return;
  1773. /*
  1774. * Shutdown discovery sessions and disable discovery TPG
  1775. */
  1776. if (iscsit_global->discovery_tpg)
  1777. iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
  1778. target_fabric_configfs_deregister(lio_target_fabric_configfs);
  1779. lio_target_fabric_configfs = NULL;
  1780. pr_debug("LIO_TARGET[0] - Cleared"
  1781. " lio_target_fabric_configfs\n");
  1782. }