iscsi_target_configfs.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  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", ISCSI_NODE_ATTRIB(nacl)->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 = ISCSI_TPG_ATTRIB(tpg)->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] = &NODE_STAT_GRPS(acl)->iscsi_sess_stats_group;
  818. stats_cg->default_groups[1] = NULL;
  819. config_group_init_type_name(&NODE_STAT_GRPS(acl)->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", ISCSI_TPG_ATTRIB(tpg)->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. static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
  930. &iscsi_tpg_attrib_authentication.attr,
  931. &iscsi_tpg_attrib_login_timeout.attr,
  932. &iscsi_tpg_attrib_netif_timeout.attr,
  933. &iscsi_tpg_attrib_generate_node_acls.attr,
  934. &iscsi_tpg_attrib_default_cmdsn_depth.attr,
  935. &iscsi_tpg_attrib_cache_dynamic_acls.attr,
  936. &iscsi_tpg_attrib_demo_mode_write_protect.attr,
  937. &iscsi_tpg_attrib_prod_mode_write_protect.attr,
  938. NULL,
  939. };
  940. /* End items for lio_target_tpg_attrib_cit */
  941. /* Start items for lio_target_tpg_auth_cit */
  942. #define __DEF_TPG_AUTH_STR(prefix, name, flags) \
  943. static ssize_t __iscsi_##prefix##_show_##name( \
  944. struct se_portal_group *se_tpg, \
  945. char *page) \
  946. { \
  947. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  948. struct iscsi_portal_group, tpg_se_tpg); \
  949. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  950. \
  951. if (!capable(CAP_SYS_ADMIN)) \
  952. return -EPERM; \
  953. \
  954. return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
  955. } \
  956. \
  957. static ssize_t __iscsi_##prefix##_store_##name( \
  958. struct se_portal_group *se_tpg, \
  959. const char *page, \
  960. size_t count) \
  961. { \
  962. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  963. struct iscsi_portal_group, tpg_se_tpg); \
  964. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  965. \
  966. if (!capable(CAP_SYS_ADMIN)) \
  967. return -EPERM; \
  968. \
  969. snprintf(auth->name, sizeof(auth->name), "%s", page); \
  970. if (!(strncmp("NULL", auth->name, 4))) \
  971. auth->naf_flags &= ~flags; \
  972. else \
  973. auth->naf_flags |= flags; \
  974. \
  975. if ((auth->naf_flags & NAF_USERID_IN_SET) && \
  976. (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
  977. auth->authenticate_target = 1; \
  978. else \
  979. auth->authenticate_target = 0; \
  980. \
  981. return count; \
  982. }
  983. #define __DEF_TPG_AUTH_INT(prefix, name) \
  984. static ssize_t __iscsi_##prefix##_show_##name( \
  985. struct se_portal_group *se_tpg, \
  986. char *page) \
  987. { \
  988. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  989. struct iscsi_portal_group, tpg_se_tpg); \
  990. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  991. \
  992. if (!capable(CAP_SYS_ADMIN)) \
  993. return -EPERM; \
  994. \
  995. return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
  996. }
  997. #define DEF_TPG_AUTH_STR(name, flags) \
  998. __DEF_TPG_AUTH_STR(tpg_auth, name, flags) \
  999. static ssize_t iscsi_tpg_auth_show_##name( \
  1000. struct se_portal_group *se_tpg, \
  1001. char *page) \
  1002. { \
  1003. return __iscsi_tpg_auth_show_##name(se_tpg, page); \
  1004. } \
  1005. \
  1006. static ssize_t iscsi_tpg_auth_store_##name( \
  1007. struct se_portal_group *se_tpg, \
  1008. const char *page, \
  1009. size_t count) \
  1010. { \
  1011. return __iscsi_tpg_auth_store_##name(se_tpg, page, count); \
  1012. }
  1013. #define DEF_TPG_AUTH_INT(name) \
  1014. __DEF_TPG_AUTH_INT(tpg_auth, name) \
  1015. static ssize_t iscsi_tpg_auth_show_##name( \
  1016. struct se_portal_group *se_tpg, \
  1017. char *page) \
  1018. { \
  1019. return __iscsi_tpg_auth_show_##name(se_tpg, page); \
  1020. }
  1021. #define TPG_AUTH_ATTR(_name, _mode) TF_TPG_AUTH_ATTR(iscsi, _name, _mode);
  1022. #define TPG_AUTH_ATTR_RO(_name) TF_TPG_AUTH_ATTR_RO(iscsi, _name);
  1023. /*
  1024. * * One-way authentication userid
  1025. * */
  1026. DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
  1027. TPG_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  1028. /*
  1029. * * One-way authentication password
  1030. * */
  1031. DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
  1032. TPG_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  1033. /*
  1034. * * Enforce mutual authentication
  1035. * */
  1036. DEF_TPG_AUTH_INT(authenticate_target);
  1037. TPG_AUTH_ATTR_RO(authenticate_target);
  1038. /*
  1039. * * Mutual authentication userid
  1040. * */
  1041. DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  1042. TPG_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  1043. /*
  1044. * * Mutual authentication password
  1045. * */
  1046. DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  1047. TPG_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  1048. static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
  1049. &iscsi_tpg_auth_userid.attr,
  1050. &iscsi_tpg_auth_password.attr,
  1051. &iscsi_tpg_auth_authenticate_target.attr,
  1052. &iscsi_tpg_auth_userid_mutual.attr,
  1053. &iscsi_tpg_auth_password_mutual.attr,
  1054. NULL,
  1055. };
  1056. /* End items for lio_target_tpg_auth_cit */
  1057. /* Start items for lio_target_tpg_param_cit */
  1058. #define DEF_TPG_PARAM(name) \
  1059. static ssize_t iscsi_tpg_param_show_##name( \
  1060. struct se_portal_group *se_tpg, \
  1061. char *page) \
  1062. { \
  1063. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  1064. struct iscsi_portal_group, tpg_se_tpg); \
  1065. struct iscsi_param *param; \
  1066. ssize_t rb; \
  1067. \
  1068. if (iscsit_get_tpg(tpg) < 0) \
  1069. return -EINVAL; \
  1070. \
  1071. param = iscsi_find_param_from_key(__stringify(name), \
  1072. tpg->param_list); \
  1073. if (!param) { \
  1074. iscsit_put_tpg(tpg); \
  1075. return -EINVAL; \
  1076. } \
  1077. rb = snprintf(page, PAGE_SIZE, "%s\n", param->value); \
  1078. \
  1079. iscsit_put_tpg(tpg); \
  1080. return rb; \
  1081. } \
  1082. static ssize_t iscsi_tpg_param_store_##name( \
  1083. struct se_portal_group *se_tpg, \
  1084. const char *page, \
  1085. size_t count) \
  1086. { \
  1087. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  1088. struct iscsi_portal_group, tpg_se_tpg); \
  1089. char *buf; \
  1090. int ret, len; \
  1091. \
  1092. buf = kzalloc(PAGE_SIZE, GFP_KERNEL); \
  1093. if (!buf) \
  1094. return -ENOMEM; \
  1095. len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page); \
  1096. if (isspace(buf[len-1])) \
  1097. buf[len-1] = '\0'; /* Kill newline */ \
  1098. \
  1099. if (iscsit_get_tpg(tpg) < 0) { \
  1100. kfree(buf); \
  1101. return -EINVAL; \
  1102. } \
  1103. \
  1104. ret = iscsi_change_param_value(buf, tpg->param_list, 1); \
  1105. if (ret < 0) \
  1106. goto out; \
  1107. \
  1108. kfree(buf); \
  1109. iscsit_put_tpg(tpg); \
  1110. return count; \
  1111. out: \
  1112. kfree(buf); \
  1113. iscsit_put_tpg(tpg); \
  1114. return -EINVAL; \
  1115. }
  1116. #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
  1117. DEF_TPG_PARAM(AuthMethod);
  1118. TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
  1119. DEF_TPG_PARAM(HeaderDigest);
  1120. TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
  1121. DEF_TPG_PARAM(DataDigest);
  1122. TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
  1123. DEF_TPG_PARAM(MaxConnections);
  1124. TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
  1125. DEF_TPG_PARAM(TargetAlias);
  1126. TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
  1127. DEF_TPG_PARAM(InitialR2T);
  1128. TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
  1129. DEF_TPG_PARAM(ImmediateData);
  1130. TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
  1131. DEF_TPG_PARAM(MaxRecvDataSegmentLength);
  1132. TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
  1133. DEF_TPG_PARAM(MaxXmitDataSegmentLength);
  1134. TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
  1135. DEF_TPG_PARAM(MaxBurstLength);
  1136. TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
  1137. DEF_TPG_PARAM(FirstBurstLength);
  1138. TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
  1139. DEF_TPG_PARAM(DefaultTime2Wait);
  1140. TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
  1141. DEF_TPG_PARAM(DefaultTime2Retain);
  1142. TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
  1143. DEF_TPG_PARAM(MaxOutstandingR2T);
  1144. TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
  1145. DEF_TPG_PARAM(DataPDUInOrder);
  1146. TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
  1147. DEF_TPG_PARAM(DataSequenceInOrder);
  1148. TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
  1149. DEF_TPG_PARAM(ErrorRecoveryLevel);
  1150. TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
  1151. DEF_TPG_PARAM(IFMarker);
  1152. TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
  1153. DEF_TPG_PARAM(OFMarker);
  1154. TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
  1155. DEF_TPG_PARAM(IFMarkInt);
  1156. TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
  1157. DEF_TPG_PARAM(OFMarkInt);
  1158. TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
  1159. static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
  1160. &iscsi_tpg_param_AuthMethod.attr,
  1161. &iscsi_tpg_param_HeaderDigest.attr,
  1162. &iscsi_tpg_param_DataDigest.attr,
  1163. &iscsi_tpg_param_MaxConnections.attr,
  1164. &iscsi_tpg_param_TargetAlias.attr,
  1165. &iscsi_tpg_param_InitialR2T.attr,
  1166. &iscsi_tpg_param_ImmediateData.attr,
  1167. &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
  1168. &iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
  1169. &iscsi_tpg_param_MaxBurstLength.attr,
  1170. &iscsi_tpg_param_FirstBurstLength.attr,
  1171. &iscsi_tpg_param_DefaultTime2Wait.attr,
  1172. &iscsi_tpg_param_DefaultTime2Retain.attr,
  1173. &iscsi_tpg_param_MaxOutstandingR2T.attr,
  1174. &iscsi_tpg_param_DataPDUInOrder.attr,
  1175. &iscsi_tpg_param_DataSequenceInOrder.attr,
  1176. &iscsi_tpg_param_ErrorRecoveryLevel.attr,
  1177. &iscsi_tpg_param_IFMarker.attr,
  1178. &iscsi_tpg_param_OFMarker.attr,
  1179. &iscsi_tpg_param_IFMarkInt.attr,
  1180. &iscsi_tpg_param_OFMarkInt.attr,
  1181. NULL,
  1182. };
  1183. /* End items for lio_target_tpg_param_cit */
  1184. /* Start items for lio_target_tpg_cit */
  1185. static ssize_t lio_target_tpg_show_enable(
  1186. struct se_portal_group *se_tpg,
  1187. char *page)
  1188. {
  1189. struct iscsi_portal_group *tpg = container_of(se_tpg,
  1190. struct iscsi_portal_group, tpg_se_tpg);
  1191. ssize_t len;
  1192. spin_lock(&tpg->tpg_state_lock);
  1193. len = sprintf(page, "%d\n",
  1194. (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
  1195. spin_unlock(&tpg->tpg_state_lock);
  1196. return len;
  1197. }
  1198. static ssize_t lio_target_tpg_store_enable(
  1199. struct se_portal_group *se_tpg,
  1200. const char *page,
  1201. size_t count)
  1202. {
  1203. struct iscsi_portal_group *tpg = container_of(se_tpg,
  1204. struct iscsi_portal_group, tpg_se_tpg);
  1205. u32 op;
  1206. int ret;
  1207. ret = kstrtou32(page, 0, &op);
  1208. if (ret)
  1209. return ret;
  1210. if ((op != 1) && (op != 0)) {
  1211. pr_err("Illegal value for tpg_enable: %u\n", op);
  1212. return -EINVAL;
  1213. }
  1214. ret = iscsit_get_tpg(tpg);
  1215. if (ret < 0)
  1216. return -EINVAL;
  1217. if (op) {
  1218. ret = iscsit_tpg_enable_portal_group(tpg);
  1219. if (ret < 0)
  1220. goto out;
  1221. } else {
  1222. /*
  1223. * iscsit_tpg_disable_portal_group() assumes force=1
  1224. */
  1225. ret = iscsit_tpg_disable_portal_group(tpg, 1);
  1226. if (ret < 0)
  1227. goto out;
  1228. }
  1229. iscsit_put_tpg(tpg);
  1230. return count;
  1231. out:
  1232. iscsit_put_tpg(tpg);
  1233. return -EINVAL;
  1234. }
  1235. TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
  1236. static struct configfs_attribute *lio_target_tpg_attrs[] = {
  1237. &lio_target_tpg_enable.attr,
  1238. NULL,
  1239. };
  1240. /* End items for lio_target_tpg_cit */
  1241. /* Start items for lio_target_tiqn_cit */
  1242. static struct se_portal_group *lio_target_tiqn_addtpg(
  1243. struct se_wwn *wwn,
  1244. struct config_group *group,
  1245. const char *name)
  1246. {
  1247. struct iscsi_portal_group *tpg;
  1248. struct iscsi_tiqn *tiqn;
  1249. char *tpgt_str;
  1250. int ret;
  1251. u16 tpgt;
  1252. tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
  1253. /*
  1254. * Only tpgt_# directory groups can be created below
  1255. * target/iscsi/iqn.superturodiskarry/
  1256. */
  1257. tpgt_str = strstr(name, "tpgt_");
  1258. if (!tpgt_str) {
  1259. pr_err("Unable to locate \"tpgt_#\" directory"
  1260. " group\n");
  1261. return NULL;
  1262. }
  1263. tpgt_str += 5; /* Skip ahead of "tpgt_" */
  1264. ret = kstrtou16(tpgt_str, 0, &tpgt);
  1265. if (ret)
  1266. return NULL;
  1267. tpg = iscsit_alloc_portal_group(tiqn, tpgt);
  1268. if (!tpg)
  1269. return NULL;
  1270. ret = core_tpg_register(
  1271. &lio_target_fabric_configfs->tf_ops,
  1272. wwn, &tpg->tpg_se_tpg, tpg,
  1273. TRANSPORT_TPG_TYPE_NORMAL);
  1274. if (ret < 0)
  1275. return NULL;
  1276. ret = iscsit_tpg_add_portal_group(tiqn, tpg);
  1277. if (ret != 0)
  1278. goto out;
  1279. pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
  1280. pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
  1281. name);
  1282. return &tpg->tpg_se_tpg;
  1283. out:
  1284. core_tpg_deregister(&tpg->tpg_se_tpg);
  1285. kfree(tpg);
  1286. return NULL;
  1287. }
  1288. static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
  1289. {
  1290. struct iscsi_portal_group *tpg;
  1291. struct iscsi_tiqn *tiqn;
  1292. tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
  1293. tiqn = tpg->tpg_tiqn;
  1294. /*
  1295. * iscsit_tpg_del_portal_group() assumes force=1
  1296. */
  1297. pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
  1298. iscsit_tpg_del_portal_group(tiqn, tpg, 1);
  1299. }
  1300. /* End items for lio_target_tiqn_cit */
  1301. /* Start LIO-Target TIQN struct contig_item lio_target_cit */
  1302. static ssize_t lio_target_wwn_show_attr_lio_version(
  1303. struct target_fabric_configfs *tf,
  1304. char *page)
  1305. {
  1306. return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
  1307. }
  1308. TF_WWN_ATTR_RO(lio_target, lio_version);
  1309. static struct configfs_attribute *lio_target_wwn_attrs[] = {
  1310. &lio_target_wwn_lio_version.attr,
  1311. NULL,
  1312. };
  1313. static struct se_wwn *lio_target_call_coreaddtiqn(
  1314. struct target_fabric_configfs *tf,
  1315. struct config_group *group,
  1316. const char *name)
  1317. {
  1318. struct config_group *stats_cg;
  1319. struct iscsi_tiqn *tiqn;
  1320. tiqn = iscsit_add_tiqn((unsigned char *)name);
  1321. if (IS_ERR(tiqn))
  1322. return ERR_CAST(tiqn);
  1323. /*
  1324. * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
  1325. */
  1326. stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
  1327. stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
  1328. GFP_KERNEL);
  1329. if (!stats_cg->default_groups) {
  1330. pr_err("Unable to allocate memory for"
  1331. " stats_cg->default_groups\n");
  1332. iscsit_del_tiqn(tiqn);
  1333. return ERR_PTR(-ENOMEM);
  1334. }
  1335. stats_cg->default_groups[0] = &WWN_STAT_GRPS(tiqn)->iscsi_instance_group;
  1336. stats_cg->default_groups[1] = &WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group;
  1337. stats_cg->default_groups[2] = &WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group;
  1338. stats_cg->default_groups[3] = &WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group;
  1339. stats_cg->default_groups[4] = &WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group;
  1340. stats_cg->default_groups[5] = NULL;
  1341. config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_instance_group,
  1342. "iscsi_instance", &iscsi_stat_instance_cit);
  1343. config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_sess_err_group,
  1344. "iscsi_sess_err", &iscsi_stat_sess_err_cit);
  1345. config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_tgt_attr_group,
  1346. "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
  1347. config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_login_stats_group,
  1348. "iscsi_login_stats", &iscsi_stat_login_cit);
  1349. config_group_init_type_name(&WWN_STAT_GRPS(tiqn)->iscsi_logout_stats_group,
  1350. "iscsi_logout_stats", &iscsi_stat_logout_cit);
  1351. pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
  1352. pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
  1353. " %s\n", name);
  1354. return &tiqn->tiqn_wwn;
  1355. }
  1356. static void lio_target_call_coredeltiqn(
  1357. struct se_wwn *wwn)
  1358. {
  1359. struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
  1360. struct config_item *df_item;
  1361. struct config_group *stats_cg;
  1362. int i;
  1363. stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
  1364. for (i = 0; stats_cg->default_groups[i]; i++) {
  1365. df_item = &stats_cg->default_groups[i]->cg_item;
  1366. stats_cg->default_groups[i] = NULL;
  1367. config_item_put(df_item);
  1368. }
  1369. kfree(stats_cg->default_groups);
  1370. pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
  1371. tiqn->tiqn);
  1372. iscsit_del_tiqn(tiqn);
  1373. }
  1374. /* End LIO-Target TIQN struct contig_lio_target_cit */
  1375. /* Start lio_target_discovery_auth_cit */
  1376. #define DEF_DISC_AUTH_STR(name, flags) \
  1377. __DEF_NACL_AUTH_STR(disc, name, flags) \
  1378. static ssize_t iscsi_disc_show_##name( \
  1379. struct target_fabric_configfs *tf, \
  1380. char *page) \
  1381. { \
  1382. return __iscsi_disc_show_##name(&iscsit_global->discovery_acl, \
  1383. page); \
  1384. } \
  1385. static ssize_t iscsi_disc_store_##name( \
  1386. struct target_fabric_configfs *tf, \
  1387. const char *page, \
  1388. size_t count) \
  1389. { \
  1390. return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
  1391. page, count); \
  1392. }
  1393. #define DEF_DISC_AUTH_INT(name) \
  1394. __DEF_NACL_AUTH_INT(disc, name) \
  1395. static ssize_t iscsi_disc_show_##name( \
  1396. struct target_fabric_configfs *tf, \
  1397. char *page) \
  1398. { \
  1399. return __iscsi_disc_show_##name(&iscsit_global->discovery_acl, \
  1400. page); \
  1401. }
  1402. #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
  1403. #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
  1404. /*
  1405. * One-way authentication userid
  1406. */
  1407. DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
  1408. DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  1409. /*
  1410. * One-way authentication password
  1411. */
  1412. DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
  1413. DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  1414. /*
  1415. * Enforce mutual authentication
  1416. */
  1417. DEF_DISC_AUTH_INT(authenticate_target);
  1418. DISC_AUTH_ATTR_RO(authenticate_target);
  1419. /*
  1420. * Mutual authentication userid
  1421. */
  1422. DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  1423. DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  1424. /*
  1425. * Mutual authentication password
  1426. */
  1427. DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  1428. DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  1429. /*
  1430. * enforce_discovery_auth
  1431. */
  1432. static ssize_t iscsi_disc_show_enforce_discovery_auth(
  1433. struct target_fabric_configfs *tf,
  1434. char *page)
  1435. {
  1436. struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
  1437. return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
  1438. }
  1439. static ssize_t iscsi_disc_store_enforce_discovery_auth(
  1440. struct target_fabric_configfs *tf,
  1441. const char *page,
  1442. size_t count)
  1443. {
  1444. struct iscsi_param *param;
  1445. struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
  1446. u32 op;
  1447. int err;
  1448. err = kstrtou32(page, 0, &op);
  1449. if (err)
  1450. return -EINVAL;
  1451. if ((op != 1) && (op != 0)) {
  1452. pr_err("Illegal value for enforce_discovery_auth:"
  1453. " %u\n", op);
  1454. return -EINVAL;
  1455. }
  1456. if (!discovery_tpg) {
  1457. pr_err("iscsit_global->discovery_tpg is NULL\n");
  1458. return -EINVAL;
  1459. }
  1460. param = iscsi_find_param_from_key(AUTHMETHOD,
  1461. discovery_tpg->param_list);
  1462. if (!param)
  1463. return -EINVAL;
  1464. if (op) {
  1465. /*
  1466. * Reset the AuthMethod key to CHAP.
  1467. */
  1468. if (iscsi_update_param_value(param, CHAP) < 0)
  1469. return -EINVAL;
  1470. discovery_tpg->tpg_attrib.authentication = 1;
  1471. iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
  1472. pr_debug("LIO-CORE[0] Successfully enabled"
  1473. " authentication enforcement for iSCSI"
  1474. " Discovery TPG\n");
  1475. } else {
  1476. /*
  1477. * Reset the AuthMethod key to CHAP,None
  1478. */
  1479. if (iscsi_update_param_value(param, "CHAP,None") < 0)
  1480. return -EINVAL;
  1481. discovery_tpg->tpg_attrib.authentication = 0;
  1482. iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
  1483. pr_debug("LIO-CORE[0] Successfully disabled"
  1484. " authentication enforcement for iSCSI"
  1485. " Discovery TPG\n");
  1486. }
  1487. return count;
  1488. }
  1489. DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
  1490. static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
  1491. &iscsi_disc_userid.attr,
  1492. &iscsi_disc_password.attr,
  1493. &iscsi_disc_authenticate_target.attr,
  1494. &iscsi_disc_userid_mutual.attr,
  1495. &iscsi_disc_password_mutual.attr,
  1496. &iscsi_disc_enforce_discovery_auth.attr,
  1497. NULL,
  1498. };
  1499. /* End lio_target_discovery_auth_cit */
  1500. /* Start functions for target_core_fabric_ops */
  1501. static char *iscsi_get_fabric_name(void)
  1502. {
  1503. return "iSCSI";
  1504. }
  1505. static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
  1506. {
  1507. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1508. /* only used for printks or comparism with ->ref_task_tag */
  1509. return (__force u32)cmd->init_task_tag;
  1510. }
  1511. static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
  1512. {
  1513. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1514. return cmd->i_state;
  1515. }
  1516. static u32 lio_sess_get_index(struct se_session *se_sess)
  1517. {
  1518. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1519. return sess->session_index;
  1520. }
  1521. static u32 lio_sess_get_initiator_sid(
  1522. struct se_session *se_sess,
  1523. unsigned char *buf,
  1524. u32 size)
  1525. {
  1526. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1527. /*
  1528. * iSCSI Initiator Session Identifier from RFC-3720.
  1529. */
  1530. return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
  1531. sess->isid[0], sess->isid[1], sess->isid[2],
  1532. sess->isid[3], sess->isid[4], sess->isid[5]);
  1533. }
  1534. static int lio_queue_data_in(struct se_cmd *se_cmd)
  1535. {
  1536. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1537. cmd->i_state = ISTATE_SEND_DATAIN;
  1538. cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
  1539. return 0;
  1540. }
  1541. static int lio_write_pending(struct se_cmd *se_cmd)
  1542. {
  1543. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1544. struct iscsi_conn *conn = cmd->conn;
  1545. if (!cmd->immediate_data && !cmd->unsolicited_data)
  1546. return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
  1547. return 0;
  1548. }
  1549. static int lio_write_pending_status(struct se_cmd *se_cmd)
  1550. {
  1551. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1552. int ret;
  1553. spin_lock_bh(&cmd->istate_lock);
  1554. ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
  1555. spin_unlock_bh(&cmd->istate_lock);
  1556. return ret;
  1557. }
  1558. static int lio_queue_status(struct se_cmd *se_cmd)
  1559. {
  1560. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1561. cmd->i_state = ISTATE_SEND_STATUS;
  1562. cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
  1563. return 0;
  1564. }
  1565. static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
  1566. {
  1567. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1568. cmd->i_state = ISTATE_SEND_TASKMGTRSP;
  1569. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  1570. }
  1571. static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
  1572. {
  1573. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1574. return &tpg->tpg_tiqn->tiqn[0];
  1575. }
  1576. static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
  1577. {
  1578. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1579. return tpg->tpgt;
  1580. }
  1581. static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
  1582. {
  1583. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1584. return ISCSI_TPG_ATTRIB(tpg)->default_cmdsn_depth;
  1585. }
  1586. static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
  1587. {
  1588. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1589. return ISCSI_TPG_ATTRIB(tpg)->generate_node_acls;
  1590. }
  1591. static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
  1592. {
  1593. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1594. return ISCSI_TPG_ATTRIB(tpg)->cache_dynamic_acls;
  1595. }
  1596. static int lio_tpg_check_demo_mode_write_protect(
  1597. struct se_portal_group *se_tpg)
  1598. {
  1599. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1600. return ISCSI_TPG_ATTRIB(tpg)->demo_mode_write_protect;
  1601. }
  1602. static int lio_tpg_check_prod_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 ISCSI_TPG_ATTRIB(tpg)->prod_mode_write_protect;
  1607. }
  1608. static void lio_tpg_release_fabric_acl(
  1609. struct se_portal_group *se_tpg,
  1610. struct se_node_acl *se_acl)
  1611. {
  1612. struct iscsi_node_acl *acl = container_of(se_acl,
  1613. struct iscsi_node_acl, se_node_acl);
  1614. kfree(acl);
  1615. }
  1616. /*
  1617. * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
  1618. *
  1619. * Also, this function calls iscsit_inc_session_usage_count() on the
  1620. * struct iscsi_session in question.
  1621. */
  1622. static int lio_tpg_shutdown_session(struct se_session *se_sess)
  1623. {
  1624. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1625. spin_lock(&sess->conn_lock);
  1626. if (atomic_read(&sess->session_fall_back_to_erl0) ||
  1627. atomic_read(&sess->session_logout) ||
  1628. (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
  1629. spin_unlock(&sess->conn_lock);
  1630. return 0;
  1631. }
  1632. atomic_set(&sess->session_reinstatement, 1);
  1633. spin_unlock(&sess->conn_lock);
  1634. iscsit_stop_time2retain_timer(sess);
  1635. iscsit_stop_session(sess, 1, 1);
  1636. return 1;
  1637. }
  1638. /*
  1639. * Calls iscsit_dec_session_usage_count() as inverse of
  1640. * lio_tpg_shutdown_session()
  1641. */
  1642. static void lio_tpg_close_session(struct se_session *se_sess)
  1643. {
  1644. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1645. /*
  1646. * If the iSCSI Session for the iSCSI Initiator Node exists,
  1647. * forcefully shutdown the iSCSI NEXUS.
  1648. */
  1649. iscsit_close_session(sess);
  1650. }
  1651. static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
  1652. {
  1653. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1654. return tpg->tpg_tiqn->tiqn_index;
  1655. }
  1656. static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
  1657. {
  1658. struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
  1659. se_node_acl);
  1660. ISCSI_NODE_ATTRIB(acl)->nacl = acl;
  1661. iscsit_set_default_node_attribues(acl);
  1662. }
  1663. static int lio_check_stop_free(struct se_cmd *se_cmd)
  1664. {
  1665. return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  1666. }
  1667. static void lio_release_cmd(struct se_cmd *se_cmd)
  1668. {
  1669. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1670. pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
  1671. iscsit_release_cmd(cmd);
  1672. }
  1673. /* End functions for target_core_fabric_ops */
  1674. int iscsi_target_register_configfs(void)
  1675. {
  1676. struct target_fabric_configfs *fabric;
  1677. int ret;
  1678. lio_target_fabric_configfs = NULL;
  1679. fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
  1680. if (IS_ERR(fabric)) {
  1681. pr_err("target_fabric_configfs_init() for"
  1682. " LIO-Target failed!\n");
  1683. return PTR_ERR(fabric);
  1684. }
  1685. /*
  1686. * Setup the fabric API of function pointers used by target_core_mod..
  1687. */
  1688. fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
  1689. fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
  1690. fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
  1691. fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
  1692. fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
  1693. fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
  1694. fabric->tf_ops.tpg_get_pr_transport_id_len =
  1695. &iscsi_get_pr_transport_id_len;
  1696. fabric->tf_ops.tpg_parse_pr_out_transport_id =
  1697. &iscsi_parse_pr_out_transport_id;
  1698. fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
  1699. fabric->tf_ops.tpg_check_demo_mode_cache =
  1700. &lio_tpg_check_demo_mode_cache;
  1701. fabric->tf_ops.tpg_check_demo_mode_write_protect =
  1702. &lio_tpg_check_demo_mode_write_protect;
  1703. fabric->tf_ops.tpg_check_prod_mode_write_protect =
  1704. &lio_tpg_check_prod_mode_write_protect;
  1705. fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
  1706. fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
  1707. fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
  1708. fabric->tf_ops.check_stop_free = &lio_check_stop_free,
  1709. fabric->tf_ops.release_cmd = &lio_release_cmd;
  1710. fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
  1711. fabric->tf_ops.close_session = &lio_tpg_close_session;
  1712. fabric->tf_ops.sess_get_index = &lio_sess_get_index;
  1713. fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
  1714. fabric->tf_ops.write_pending = &lio_write_pending;
  1715. fabric->tf_ops.write_pending_status = &lio_write_pending_status;
  1716. fabric->tf_ops.set_default_node_attributes =
  1717. &lio_set_default_node_attributes;
  1718. fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
  1719. fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
  1720. fabric->tf_ops.queue_data_in = &lio_queue_data_in;
  1721. fabric->tf_ops.queue_status = &lio_queue_status;
  1722. fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
  1723. /*
  1724. * Setup function pointers for generic logic in target_core_fabric_configfs.c
  1725. */
  1726. fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
  1727. fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
  1728. fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
  1729. fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
  1730. fabric->tf_ops.fabric_post_link = NULL;
  1731. fabric->tf_ops.fabric_pre_unlink = NULL;
  1732. fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
  1733. fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
  1734. fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
  1735. fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
  1736. /*
  1737. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1738. * sturct config_item_type's
  1739. */
  1740. TF_CIT_TMPL(fabric)->tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
  1741. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
  1742. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
  1743. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
  1744. TF_CIT_TMPL(fabric)->tfc_tpg_auth_cit.ct_attrs = lio_target_tpg_auth_attrs;
  1745. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
  1746. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
  1747. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
  1748. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
  1749. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
  1750. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
  1751. ret = target_fabric_configfs_register(fabric);
  1752. if (ret < 0) {
  1753. pr_err("target_fabric_configfs_register() for"
  1754. " LIO-Target failed!\n");
  1755. target_fabric_configfs_free(fabric);
  1756. return ret;
  1757. }
  1758. lio_target_fabric_configfs = fabric;
  1759. pr_debug("LIO_TARGET[0] - Set fabric ->"
  1760. " lio_target_fabric_configfs\n");
  1761. return 0;
  1762. }
  1763. void iscsi_target_deregister_configfs(void)
  1764. {
  1765. if (!lio_target_fabric_configfs)
  1766. return;
  1767. /*
  1768. * Shutdown discovery sessions and disable discovery TPG
  1769. */
  1770. if (iscsit_global->discovery_tpg)
  1771. iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
  1772. target_fabric_configfs_deregister(lio_target_fabric_configfs);
  1773. lio_target_fabric_configfs = NULL;
  1774. pr_debug("LIO_TARGET[0] - Cleared"
  1775. " lio_target_fabric_configfs\n");
  1776. }