iscsi_target_configfs.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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. if (count >= sizeof(auth->name)) \
  427. return -EINVAL; \
  428. snprintf(auth->name, sizeof(auth->name), "%s", page); \
  429. if (!strncmp("NULL", auth->name, 4)) \
  430. auth->naf_flags &= ~flags; \
  431. else \
  432. auth->naf_flags |= flags; \
  433. \
  434. if ((auth->naf_flags & NAF_USERID_IN_SET) && \
  435. (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
  436. auth->authenticate_target = 1; \
  437. else \
  438. auth->authenticate_target = 0; \
  439. \
  440. return count; \
  441. }
  442. #define __DEF_NACL_AUTH_INT(prefix, name) \
  443. static ssize_t __iscsi_##prefix##_show_##name( \
  444. struct iscsi_node_acl *nacl, \
  445. char *page) \
  446. { \
  447. struct iscsi_node_auth *auth = &nacl->node_auth; \
  448. \
  449. if (!capable(CAP_SYS_ADMIN)) \
  450. return -EPERM; \
  451. \
  452. return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
  453. }
  454. #define DEF_NACL_AUTH_STR(name, flags) \
  455. __DEF_NACL_AUTH_STR(nacl_auth, name, flags) \
  456. static ssize_t iscsi_nacl_auth_show_##name( \
  457. struct se_node_acl *nacl, \
  458. char *page) \
  459. { \
  460. return __iscsi_nacl_auth_show_##name(container_of(nacl, \
  461. struct iscsi_node_acl, se_node_acl), page); \
  462. } \
  463. static ssize_t iscsi_nacl_auth_store_##name( \
  464. struct se_node_acl *nacl, \
  465. const char *page, \
  466. size_t count) \
  467. { \
  468. return __iscsi_nacl_auth_store_##name(container_of(nacl, \
  469. struct iscsi_node_acl, se_node_acl), page, count); \
  470. }
  471. #define DEF_NACL_AUTH_INT(name) \
  472. __DEF_NACL_AUTH_INT(nacl_auth, name) \
  473. static ssize_t iscsi_nacl_auth_show_##name( \
  474. struct se_node_acl *nacl, \
  475. char *page) \
  476. { \
  477. return __iscsi_nacl_auth_show_##name(container_of(nacl, \
  478. struct iscsi_node_acl, se_node_acl), page); \
  479. }
  480. #define AUTH_ATTR(_name, _mode) TF_NACL_AUTH_ATTR(iscsi, _name, _mode);
  481. #define AUTH_ATTR_RO(_name) TF_NACL_AUTH_ATTR_RO(iscsi, _name);
  482. /*
  483. * One-way authentication userid
  484. */
  485. DEF_NACL_AUTH_STR(userid, NAF_USERID_SET);
  486. AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  487. /*
  488. * One-way authentication password
  489. */
  490. DEF_NACL_AUTH_STR(password, NAF_PASSWORD_SET);
  491. AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  492. /*
  493. * Enforce mutual authentication
  494. */
  495. DEF_NACL_AUTH_INT(authenticate_target);
  496. AUTH_ATTR_RO(authenticate_target);
  497. /*
  498. * Mutual authentication userid
  499. */
  500. DEF_NACL_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  501. AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  502. /*
  503. * Mutual authentication password
  504. */
  505. DEF_NACL_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  506. AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  507. static struct configfs_attribute *lio_target_nacl_auth_attrs[] = {
  508. &iscsi_nacl_auth_userid.attr,
  509. &iscsi_nacl_auth_password.attr,
  510. &iscsi_nacl_auth_authenticate_target.attr,
  511. &iscsi_nacl_auth_userid_mutual.attr,
  512. &iscsi_nacl_auth_password_mutual.attr,
  513. NULL,
  514. };
  515. /* End items for lio_target_nacl_auth_cit */
  516. /* Start items for lio_target_nacl_param_cit */
  517. #define DEF_NACL_PARAM(name) \
  518. static ssize_t iscsi_nacl_param_show_##name( \
  519. struct se_node_acl *se_nacl, \
  520. char *page) \
  521. { \
  522. struct iscsi_session *sess; \
  523. struct se_session *se_sess; \
  524. ssize_t rb; \
  525. \
  526. spin_lock_bh(&se_nacl->nacl_sess_lock); \
  527. se_sess = se_nacl->nacl_sess; \
  528. if (!se_sess) { \
  529. rb = snprintf(page, PAGE_SIZE, \
  530. "No Active iSCSI Session\n"); \
  531. } else { \
  532. sess = se_sess->fabric_sess_ptr; \
  533. rb = snprintf(page, PAGE_SIZE, "%u\n", \
  534. (u32)sess->sess_ops->name); \
  535. } \
  536. spin_unlock_bh(&se_nacl->nacl_sess_lock); \
  537. \
  538. return rb; \
  539. }
  540. #define NACL_PARAM_ATTR(_name) TF_NACL_PARAM_ATTR_RO(iscsi, _name);
  541. DEF_NACL_PARAM(MaxConnections);
  542. NACL_PARAM_ATTR(MaxConnections);
  543. DEF_NACL_PARAM(InitialR2T);
  544. NACL_PARAM_ATTR(InitialR2T);
  545. DEF_NACL_PARAM(ImmediateData);
  546. NACL_PARAM_ATTR(ImmediateData);
  547. DEF_NACL_PARAM(MaxBurstLength);
  548. NACL_PARAM_ATTR(MaxBurstLength);
  549. DEF_NACL_PARAM(FirstBurstLength);
  550. NACL_PARAM_ATTR(FirstBurstLength);
  551. DEF_NACL_PARAM(DefaultTime2Wait);
  552. NACL_PARAM_ATTR(DefaultTime2Wait);
  553. DEF_NACL_PARAM(DefaultTime2Retain);
  554. NACL_PARAM_ATTR(DefaultTime2Retain);
  555. DEF_NACL_PARAM(MaxOutstandingR2T);
  556. NACL_PARAM_ATTR(MaxOutstandingR2T);
  557. DEF_NACL_PARAM(DataPDUInOrder);
  558. NACL_PARAM_ATTR(DataPDUInOrder);
  559. DEF_NACL_PARAM(DataSequenceInOrder);
  560. NACL_PARAM_ATTR(DataSequenceInOrder);
  561. DEF_NACL_PARAM(ErrorRecoveryLevel);
  562. NACL_PARAM_ATTR(ErrorRecoveryLevel);
  563. static struct configfs_attribute *lio_target_nacl_param_attrs[] = {
  564. &iscsi_nacl_param_MaxConnections.attr,
  565. &iscsi_nacl_param_InitialR2T.attr,
  566. &iscsi_nacl_param_ImmediateData.attr,
  567. &iscsi_nacl_param_MaxBurstLength.attr,
  568. &iscsi_nacl_param_FirstBurstLength.attr,
  569. &iscsi_nacl_param_DefaultTime2Wait.attr,
  570. &iscsi_nacl_param_DefaultTime2Retain.attr,
  571. &iscsi_nacl_param_MaxOutstandingR2T.attr,
  572. &iscsi_nacl_param_DataPDUInOrder.attr,
  573. &iscsi_nacl_param_DataSequenceInOrder.attr,
  574. &iscsi_nacl_param_ErrorRecoveryLevel.attr,
  575. NULL,
  576. };
  577. /* End items for lio_target_nacl_param_cit */
  578. /* Start items for lio_target_acl_cit */
  579. static ssize_t lio_target_nacl_show_info(
  580. struct se_node_acl *se_nacl,
  581. char *page)
  582. {
  583. struct iscsi_session *sess;
  584. struct iscsi_conn *conn;
  585. struct se_session *se_sess;
  586. ssize_t rb = 0;
  587. spin_lock_bh(&se_nacl->nacl_sess_lock);
  588. se_sess = se_nacl->nacl_sess;
  589. if (!se_sess) {
  590. rb += sprintf(page+rb, "No active iSCSI Session for Initiator"
  591. " Endpoint: %s\n", se_nacl->initiatorname);
  592. } else {
  593. sess = se_sess->fabric_sess_ptr;
  594. rb += sprintf(page+rb, "InitiatorName: %s\n",
  595. sess->sess_ops->InitiatorName);
  596. rb += sprintf(page+rb, "InitiatorAlias: %s\n",
  597. sess->sess_ops->InitiatorAlias);
  598. rb += sprintf(page+rb, "LIO Session ID: %u "
  599. "ISID: 0x%02x %02x %02x %02x %02x %02x "
  600. "TSIH: %hu ", sess->sid,
  601. sess->isid[0], sess->isid[1], sess->isid[2],
  602. sess->isid[3], sess->isid[4], sess->isid[5],
  603. sess->tsih);
  604. rb += sprintf(page+rb, "SessionType: %s\n",
  605. (sess->sess_ops->SessionType) ?
  606. "Discovery" : "Normal");
  607. rb += sprintf(page+rb, "Session State: ");
  608. switch (sess->session_state) {
  609. case TARG_SESS_STATE_FREE:
  610. rb += sprintf(page+rb, "TARG_SESS_FREE\n");
  611. break;
  612. case TARG_SESS_STATE_ACTIVE:
  613. rb += sprintf(page+rb, "TARG_SESS_STATE_ACTIVE\n");
  614. break;
  615. case TARG_SESS_STATE_LOGGED_IN:
  616. rb += sprintf(page+rb, "TARG_SESS_STATE_LOGGED_IN\n");
  617. break;
  618. case TARG_SESS_STATE_FAILED:
  619. rb += sprintf(page+rb, "TARG_SESS_STATE_FAILED\n");
  620. break;
  621. case TARG_SESS_STATE_IN_CONTINUE:
  622. rb += sprintf(page+rb, "TARG_SESS_STATE_IN_CONTINUE\n");
  623. break;
  624. default:
  625. rb += sprintf(page+rb, "ERROR: Unknown Session"
  626. " State!\n");
  627. break;
  628. }
  629. rb += sprintf(page+rb, "---------------------[iSCSI Session"
  630. " Values]-----------------------\n");
  631. rb += sprintf(page+rb, " CmdSN/WR : CmdSN/WC : ExpCmdSN"
  632. " : MaxCmdSN : ITT : TTT\n");
  633. rb += sprintf(page+rb, " 0x%08x 0x%08x 0x%08x 0x%08x"
  634. " 0x%08x 0x%08x\n",
  635. sess->cmdsn_window,
  636. (sess->max_cmd_sn - sess->exp_cmd_sn) + 1,
  637. sess->exp_cmd_sn, sess->max_cmd_sn,
  638. sess->init_task_tag, sess->targ_xfer_tag);
  639. rb += sprintf(page+rb, "----------------------[iSCSI"
  640. " Connections]-------------------------\n");
  641. spin_lock(&sess->conn_lock);
  642. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  643. rb += sprintf(page+rb, "CID: %hu Connection"
  644. " State: ", conn->cid);
  645. switch (conn->conn_state) {
  646. case TARG_CONN_STATE_FREE:
  647. rb += sprintf(page+rb,
  648. "TARG_CONN_STATE_FREE\n");
  649. break;
  650. case TARG_CONN_STATE_XPT_UP:
  651. rb += sprintf(page+rb,
  652. "TARG_CONN_STATE_XPT_UP\n");
  653. break;
  654. case TARG_CONN_STATE_IN_LOGIN:
  655. rb += sprintf(page+rb,
  656. "TARG_CONN_STATE_IN_LOGIN\n");
  657. break;
  658. case TARG_CONN_STATE_LOGGED_IN:
  659. rb += sprintf(page+rb,
  660. "TARG_CONN_STATE_LOGGED_IN\n");
  661. break;
  662. case TARG_CONN_STATE_IN_LOGOUT:
  663. rb += sprintf(page+rb,
  664. "TARG_CONN_STATE_IN_LOGOUT\n");
  665. break;
  666. case TARG_CONN_STATE_LOGOUT_REQUESTED:
  667. rb += sprintf(page+rb,
  668. "TARG_CONN_STATE_LOGOUT_REQUESTED\n");
  669. break;
  670. case TARG_CONN_STATE_CLEANUP_WAIT:
  671. rb += sprintf(page+rb,
  672. "TARG_CONN_STATE_CLEANUP_WAIT\n");
  673. break;
  674. default:
  675. rb += sprintf(page+rb,
  676. "ERROR: Unknown Connection State!\n");
  677. break;
  678. }
  679. rb += sprintf(page+rb, " Address %s %s", conn->login_ip,
  680. (conn->network_transport == ISCSI_TCP) ?
  681. "TCP" : "SCTP");
  682. rb += sprintf(page+rb, " StatSN: 0x%08x\n",
  683. conn->stat_sn);
  684. }
  685. spin_unlock(&sess->conn_lock);
  686. }
  687. spin_unlock_bh(&se_nacl->nacl_sess_lock);
  688. return rb;
  689. }
  690. TF_NACL_BASE_ATTR_RO(lio_target, info);
  691. static ssize_t lio_target_nacl_show_cmdsn_depth(
  692. struct se_node_acl *se_nacl,
  693. char *page)
  694. {
  695. return sprintf(page, "%u\n", se_nacl->queue_depth);
  696. }
  697. static ssize_t lio_target_nacl_store_cmdsn_depth(
  698. struct se_node_acl *se_nacl,
  699. const char *page,
  700. size_t count)
  701. {
  702. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  703. struct iscsi_portal_group *tpg = container_of(se_tpg,
  704. struct iscsi_portal_group, tpg_se_tpg);
  705. struct config_item *acl_ci, *tpg_ci, *wwn_ci;
  706. u32 cmdsn_depth = 0;
  707. int ret;
  708. ret = kstrtou32(page, 0, &cmdsn_depth);
  709. if (ret)
  710. return ret;
  711. if (cmdsn_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
  712. pr_err("Passed cmdsn_depth: %u exceeds"
  713. " TA_DEFAULT_CMDSN_DEPTH_MAX: %u\n", cmdsn_depth,
  714. TA_DEFAULT_CMDSN_DEPTH_MAX);
  715. return -EINVAL;
  716. }
  717. acl_ci = &se_nacl->acl_group.cg_item;
  718. if (!acl_ci) {
  719. pr_err("Unable to locatel acl_ci\n");
  720. return -EINVAL;
  721. }
  722. tpg_ci = &acl_ci->ci_parent->ci_group->cg_item;
  723. if (!tpg_ci) {
  724. pr_err("Unable to locate tpg_ci\n");
  725. return -EINVAL;
  726. }
  727. wwn_ci = &tpg_ci->ci_group->cg_item;
  728. if (!wwn_ci) {
  729. pr_err("Unable to locate config_item wwn_ci\n");
  730. return -EINVAL;
  731. }
  732. if (iscsit_get_tpg(tpg) < 0)
  733. return -EINVAL;
  734. /*
  735. * iscsit_tpg_set_initiator_node_queue_depth() assumes force=1
  736. */
  737. ret = iscsit_tpg_set_initiator_node_queue_depth(tpg,
  738. config_item_name(acl_ci), cmdsn_depth, 1);
  739. pr_debug("LIO_Target_ConfigFS: %s/%s Set CmdSN Window: %u for"
  740. "InitiatorName: %s\n", config_item_name(wwn_ci),
  741. config_item_name(tpg_ci), cmdsn_depth,
  742. config_item_name(acl_ci));
  743. iscsit_put_tpg(tpg);
  744. return (!ret) ? count : (ssize_t)ret;
  745. }
  746. TF_NACL_BASE_ATTR(lio_target, cmdsn_depth, S_IRUGO | S_IWUSR);
  747. static ssize_t lio_target_nacl_show_tag(
  748. struct se_node_acl *se_nacl,
  749. char *page)
  750. {
  751. return snprintf(page, PAGE_SIZE, "%s", se_nacl->acl_tag);
  752. }
  753. static ssize_t lio_target_nacl_store_tag(
  754. struct se_node_acl *se_nacl,
  755. const char *page,
  756. size_t count)
  757. {
  758. int ret;
  759. ret = core_tpg_set_initiator_node_tag(se_nacl->se_tpg, se_nacl, page);
  760. if (ret < 0)
  761. return ret;
  762. return count;
  763. }
  764. TF_NACL_BASE_ATTR(lio_target, tag, S_IRUGO | S_IWUSR);
  765. static struct configfs_attribute *lio_target_initiator_attrs[] = {
  766. &lio_target_nacl_info.attr,
  767. &lio_target_nacl_cmdsn_depth.attr,
  768. &lio_target_nacl_tag.attr,
  769. NULL,
  770. };
  771. static struct se_node_acl *lio_tpg_alloc_fabric_acl(
  772. struct se_portal_group *se_tpg)
  773. {
  774. struct iscsi_node_acl *acl;
  775. acl = kzalloc(sizeof(struct iscsi_node_acl), GFP_KERNEL);
  776. if (!acl) {
  777. pr_err("Unable to allocate memory for struct iscsi_node_acl\n");
  778. return NULL;
  779. }
  780. return &acl->se_node_acl;
  781. }
  782. static struct se_node_acl *lio_target_make_nodeacl(
  783. struct se_portal_group *se_tpg,
  784. struct config_group *group,
  785. const char *name)
  786. {
  787. struct config_group *stats_cg;
  788. struct iscsi_node_acl *acl;
  789. struct se_node_acl *se_nacl_new, *se_nacl;
  790. struct iscsi_portal_group *tpg = container_of(se_tpg,
  791. struct iscsi_portal_group, tpg_se_tpg);
  792. u32 cmdsn_depth;
  793. se_nacl_new = lio_tpg_alloc_fabric_acl(se_tpg);
  794. if (!se_nacl_new)
  795. return ERR_PTR(-ENOMEM);
  796. cmdsn_depth = tpg->tpg_attrib.default_cmdsn_depth;
  797. /*
  798. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  799. * when converting a NdoeACL from demo mode -> explict
  800. */
  801. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  802. name, cmdsn_depth);
  803. if (IS_ERR(se_nacl))
  804. return se_nacl;
  805. acl = container_of(se_nacl, struct iscsi_node_acl, se_node_acl);
  806. stats_cg = &se_nacl->acl_fabric_stat_group;
  807. stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  808. GFP_KERNEL);
  809. if (!stats_cg->default_groups) {
  810. pr_err("Unable to allocate memory for"
  811. " stats_cg->default_groups\n");
  812. core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
  813. kfree(acl);
  814. return ERR_PTR(-ENOMEM);
  815. }
  816. stats_cg->default_groups[0] = &acl->node_stat_grps.iscsi_sess_stats_group;
  817. stats_cg->default_groups[1] = NULL;
  818. config_group_init_type_name(&acl->node_stat_grps.iscsi_sess_stats_group,
  819. "iscsi_sess_stats", &iscsi_stat_sess_cit);
  820. return se_nacl;
  821. }
  822. static void lio_target_drop_nodeacl(
  823. struct se_node_acl *se_nacl)
  824. {
  825. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  826. struct iscsi_node_acl *acl = container_of(se_nacl,
  827. struct iscsi_node_acl, se_node_acl);
  828. struct config_item *df_item;
  829. struct config_group *stats_cg;
  830. int i;
  831. stats_cg = &acl->se_node_acl.acl_fabric_stat_group;
  832. for (i = 0; stats_cg->default_groups[i]; i++) {
  833. df_item = &stats_cg->default_groups[i]->cg_item;
  834. stats_cg->default_groups[i] = NULL;
  835. config_item_put(df_item);
  836. }
  837. kfree(stats_cg->default_groups);
  838. core_tpg_del_initiator_node_acl(se_tpg, se_nacl, 1);
  839. kfree(acl);
  840. }
  841. /* End items for lio_target_acl_cit */
  842. /* Start items for lio_target_tpg_attrib_cit */
  843. #define DEF_TPG_ATTRIB(name) \
  844. \
  845. static ssize_t iscsi_tpg_attrib_show_##name( \
  846. struct se_portal_group *se_tpg, \
  847. char *page) \
  848. { \
  849. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  850. struct iscsi_portal_group, tpg_se_tpg); \
  851. ssize_t rb; \
  852. \
  853. if (iscsit_get_tpg(tpg) < 0) \
  854. return -EINVAL; \
  855. \
  856. rb = sprintf(page, "%u\n", tpg->tpg_attrib.name); \
  857. iscsit_put_tpg(tpg); \
  858. return rb; \
  859. } \
  860. \
  861. static ssize_t iscsi_tpg_attrib_store_##name( \
  862. struct se_portal_group *se_tpg, \
  863. const char *page, \
  864. size_t count) \
  865. { \
  866. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  867. struct iscsi_portal_group, tpg_se_tpg); \
  868. u32 val; \
  869. int ret; \
  870. \
  871. if (iscsit_get_tpg(tpg) < 0) \
  872. return -EINVAL; \
  873. \
  874. ret = kstrtou32(page, 0, &val); \
  875. if (ret) \
  876. goto out; \
  877. ret = iscsit_ta_##name(tpg, val); \
  878. if (ret < 0) \
  879. goto out; \
  880. \
  881. iscsit_put_tpg(tpg); \
  882. return count; \
  883. out: \
  884. iscsit_put_tpg(tpg); \
  885. return ret; \
  886. }
  887. #define TPG_ATTR(_name, _mode) TF_TPG_ATTRIB_ATTR(iscsi, _name, _mode);
  888. /*
  889. * Define iscsi_tpg_attrib_s_authentication
  890. */
  891. DEF_TPG_ATTRIB(authentication);
  892. TPG_ATTR(authentication, S_IRUGO | S_IWUSR);
  893. /*
  894. * Define iscsi_tpg_attrib_s_login_timeout
  895. */
  896. DEF_TPG_ATTRIB(login_timeout);
  897. TPG_ATTR(login_timeout, S_IRUGO | S_IWUSR);
  898. /*
  899. * Define iscsi_tpg_attrib_s_netif_timeout
  900. */
  901. DEF_TPG_ATTRIB(netif_timeout);
  902. TPG_ATTR(netif_timeout, S_IRUGO | S_IWUSR);
  903. /*
  904. * Define iscsi_tpg_attrib_s_generate_node_acls
  905. */
  906. DEF_TPG_ATTRIB(generate_node_acls);
  907. TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
  908. /*
  909. * Define iscsi_tpg_attrib_s_default_cmdsn_depth
  910. */
  911. DEF_TPG_ATTRIB(default_cmdsn_depth);
  912. TPG_ATTR(default_cmdsn_depth, S_IRUGO | S_IWUSR);
  913. /*
  914. Define iscsi_tpg_attrib_s_cache_dynamic_acls
  915. */
  916. DEF_TPG_ATTRIB(cache_dynamic_acls);
  917. TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
  918. /*
  919. * Define iscsi_tpg_attrib_s_demo_mode_write_protect
  920. */
  921. DEF_TPG_ATTRIB(demo_mode_write_protect);
  922. TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
  923. /*
  924. * Define iscsi_tpg_attrib_s_prod_mode_write_protect
  925. */
  926. DEF_TPG_ATTRIB(prod_mode_write_protect);
  927. TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
  928. /*
  929. * Define iscsi_tpg_attrib_s_demo_mode_discovery,
  930. */
  931. DEF_TPG_ATTRIB(demo_mode_discovery);
  932. TPG_ATTR(demo_mode_discovery, S_IRUGO | S_IWUSR);
  933. /*
  934. * Define iscsi_tpg_attrib_s_default_erl
  935. */
  936. DEF_TPG_ATTRIB(default_erl);
  937. TPG_ATTR(default_erl, S_IRUGO | S_IWUSR);
  938. /*
  939. * Define iscsi_tpg_attrib_s_t10_pi
  940. */
  941. DEF_TPG_ATTRIB(t10_pi);
  942. TPG_ATTR(t10_pi, S_IRUGO | S_IWUSR);
  943. static struct configfs_attribute *lio_target_tpg_attrib_attrs[] = {
  944. &iscsi_tpg_attrib_authentication.attr,
  945. &iscsi_tpg_attrib_login_timeout.attr,
  946. &iscsi_tpg_attrib_netif_timeout.attr,
  947. &iscsi_tpg_attrib_generate_node_acls.attr,
  948. &iscsi_tpg_attrib_default_cmdsn_depth.attr,
  949. &iscsi_tpg_attrib_cache_dynamic_acls.attr,
  950. &iscsi_tpg_attrib_demo_mode_write_protect.attr,
  951. &iscsi_tpg_attrib_prod_mode_write_protect.attr,
  952. &iscsi_tpg_attrib_demo_mode_discovery.attr,
  953. &iscsi_tpg_attrib_default_erl.attr,
  954. &iscsi_tpg_attrib_t10_pi.attr,
  955. NULL,
  956. };
  957. /* End items for lio_target_tpg_attrib_cit */
  958. /* Start items for lio_target_tpg_auth_cit */
  959. #define __DEF_TPG_AUTH_STR(prefix, name, flags) \
  960. static ssize_t __iscsi_##prefix##_show_##name( \
  961. struct se_portal_group *se_tpg, \
  962. char *page) \
  963. { \
  964. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  965. struct iscsi_portal_group, tpg_se_tpg); \
  966. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  967. \
  968. if (!capable(CAP_SYS_ADMIN)) \
  969. return -EPERM; \
  970. \
  971. return snprintf(page, PAGE_SIZE, "%s\n", auth->name); \
  972. } \
  973. \
  974. static ssize_t __iscsi_##prefix##_store_##name( \
  975. struct se_portal_group *se_tpg, \
  976. const char *page, \
  977. size_t count) \
  978. { \
  979. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  980. struct iscsi_portal_group, tpg_se_tpg); \
  981. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  982. \
  983. if (!capable(CAP_SYS_ADMIN)) \
  984. return -EPERM; \
  985. \
  986. snprintf(auth->name, sizeof(auth->name), "%s", page); \
  987. if (!(strncmp("NULL", auth->name, 4))) \
  988. auth->naf_flags &= ~flags; \
  989. else \
  990. auth->naf_flags |= flags; \
  991. \
  992. if ((auth->naf_flags & NAF_USERID_IN_SET) && \
  993. (auth->naf_flags & NAF_PASSWORD_IN_SET)) \
  994. auth->authenticate_target = 1; \
  995. else \
  996. auth->authenticate_target = 0; \
  997. \
  998. return count; \
  999. }
  1000. #define __DEF_TPG_AUTH_INT(prefix, name) \
  1001. static ssize_t __iscsi_##prefix##_show_##name( \
  1002. struct se_portal_group *se_tpg, \
  1003. char *page) \
  1004. { \
  1005. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  1006. struct iscsi_portal_group, tpg_se_tpg); \
  1007. struct iscsi_node_auth *auth = &tpg->tpg_demo_auth; \
  1008. \
  1009. if (!capable(CAP_SYS_ADMIN)) \
  1010. return -EPERM; \
  1011. \
  1012. return snprintf(page, PAGE_SIZE, "%d\n", auth->name); \
  1013. }
  1014. #define DEF_TPG_AUTH_STR(name, flags) \
  1015. __DEF_TPG_AUTH_STR(tpg_auth, name, flags) \
  1016. static ssize_t iscsi_tpg_auth_show_##name( \
  1017. struct se_portal_group *se_tpg, \
  1018. char *page) \
  1019. { \
  1020. return __iscsi_tpg_auth_show_##name(se_tpg, page); \
  1021. } \
  1022. \
  1023. static ssize_t iscsi_tpg_auth_store_##name( \
  1024. struct se_portal_group *se_tpg, \
  1025. const char *page, \
  1026. size_t count) \
  1027. { \
  1028. return __iscsi_tpg_auth_store_##name(se_tpg, page, count); \
  1029. }
  1030. #define DEF_TPG_AUTH_INT(name) \
  1031. __DEF_TPG_AUTH_INT(tpg_auth, name) \
  1032. static ssize_t iscsi_tpg_auth_show_##name( \
  1033. struct se_portal_group *se_tpg, \
  1034. char *page) \
  1035. { \
  1036. return __iscsi_tpg_auth_show_##name(se_tpg, page); \
  1037. }
  1038. #define TPG_AUTH_ATTR(_name, _mode) TF_TPG_AUTH_ATTR(iscsi, _name, _mode);
  1039. #define TPG_AUTH_ATTR_RO(_name) TF_TPG_AUTH_ATTR_RO(iscsi, _name);
  1040. /*
  1041. * * One-way authentication userid
  1042. * */
  1043. DEF_TPG_AUTH_STR(userid, NAF_USERID_SET);
  1044. TPG_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  1045. /*
  1046. * * One-way authentication password
  1047. * */
  1048. DEF_TPG_AUTH_STR(password, NAF_PASSWORD_SET);
  1049. TPG_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  1050. /*
  1051. * * Enforce mutual authentication
  1052. * */
  1053. DEF_TPG_AUTH_INT(authenticate_target);
  1054. TPG_AUTH_ATTR_RO(authenticate_target);
  1055. /*
  1056. * * Mutual authentication userid
  1057. * */
  1058. DEF_TPG_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  1059. TPG_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  1060. /*
  1061. * * Mutual authentication password
  1062. * */
  1063. DEF_TPG_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  1064. TPG_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  1065. static struct configfs_attribute *lio_target_tpg_auth_attrs[] = {
  1066. &iscsi_tpg_auth_userid.attr,
  1067. &iscsi_tpg_auth_password.attr,
  1068. &iscsi_tpg_auth_authenticate_target.attr,
  1069. &iscsi_tpg_auth_userid_mutual.attr,
  1070. &iscsi_tpg_auth_password_mutual.attr,
  1071. NULL,
  1072. };
  1073. /* End items for lio_target_tpg_auth_cit */
  1074. /* Start items for lio_target_tpg_param_cit */
  1075. #define DEF_TPG_PARAM(name) \
  1076. static ssize_t iscsi_tpg_param_show_##name( \
  1077. struct se_portal_group *se_tpg, \
  1078. char *page) \
  1079. { \
  1080. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  1081. struct iscsi_portal_group, tpg_se_tpg); \
  1082. struct iscsi_param *param; \
  1083. ssize_t rb; \
  1084. \
  1085. if (iscsit_get_tpg(tpg) < 0) \
  1086. return -EINVAL; \
  1087. \
  1088. param = iscsi_find_param_from_key(__stringify(name), \
  1089. tpg->param_list); \
  1090. if (!param) { \
  1091. iscsit_put_tpg(tpg); \
  1092. return -EINVAL; \
  1093. } \
  1094. rb = snprintf(page, PAGE_SIZE, "%s\n", param->value); \
  1095. \
  1096. iscsit_put_tpg(tpg); \
  1097. return rb; \
  1098. } \
  1099. static ssize_t iscsi_tpg_param_store_##name( \
  1100. struct se_portal_group *se_tpg, \
  1101. const char *page, \
  1102. size_t count) \
  1103. { \
  1104. struct iscsi_portal_group *tpg = container_of(se_tpg, \
  1105. struct iscsi_portal_group, tpg_se_tpg); \
  1106. char *buf; \
  1107. int ret, len; \
  1108. \
  1109. buf = kzalloc(PAGE_SIZE, GFP_KERNEL); \
  1110. if (!buf) \
  1111. return -ENOMEM; \
  1112. len = snprintf(buf, PAGE_SIZE, "%s=%s", __stringify(name), page); \
  1113. if (isspace(buf[len-1])) \
  1114. buf[len-1] = '\0'; /* Kill newline */ \
  1115. \
  1116. if (iscsit_get_tpg(tpg) < 0) { \
  1117. kfree(buf); \
  1118. return -EINVAL; \
  1119. } \
  1120. \
  1121. ret = iscsi_change_param_value(buf, tpg->param_list, 1); \
  1122. if (ret < 0) \
  1123. goto out; \
  1124. \
  1125. kfree(buf); \
  1126. iscsit_put_tpg(tpg); \
  1127. return count; \
  1128. out: \
  1129. kfree(buf); \
  1130. iscsit_put_tpg(tpg); \
  1131. return -EINVAL; \
  1132. }
  1133. #define TPG_PARAM_ATTR(_name, _mode) TF_TPG_PARAM_ATTR(iscsi, _name, _mode);
  1134. DEF_TPG_PARAM(AuthMethod);
  1135. TPG_PARAM_ATTR(AuthMethod, S_IRUGO | S_IWUSR);
  1136. DEF_TPG_PARAM(HeaderDigest);
  1137. TPG_PARAM_ATTR(HeaderDigest, S_IRUGO | S_IWUSR);
  1138. DEF_TPG_PARAM(DataDigest);
  1139. TPG_PARAM_ATTR(DataDigest, S_IRUGO | S_IWUSR);
  1140. DEF_TPG_PARAM(MaxConnections);
  1141. TPG_PARAM_ATTR(MaxConnections, S_IRUGO | S_IWUSR);
  1142. DEF_TPG_PARAM(TargetAlias);
  1143. TPG_PARAM_ATTR(TargetAlias, S_IRUGO | S_IWUSR);
  1144. DEF_TPG_PARAM(InitialR2T);
  1145. TPG_PARAM_ATTR(InitialR2T, S_IRUGO | S_IWUSR);
  1146. DEF_TPG_PARAM(ImmediateData);
  1147. TPG_PARAM_ATTR(ImmediateData, S_IRUGO | S_IWUSR);
  1148. DEF_TPG_PARAM(MaxRecvDataSegmentLength);
  1149. TPG_PARAM_ATTR(MaxRecvDataSegmentLength, S_IRUGO | S_IWUSR);
  1150. DEF_TPG_PARAM(MaxXmitDataSegmentLength);
  1151. TPG_PARAM_ATTR(MaxXmitDataSegmentLength, S_IRUGO | S_IWUSR);
  1152. DEF_TPG_PARAM(MaxBurstLength);
  1153. TPG_PARAM_ATTR(MaxBurstLength, S_IRUGO | S_IWUSR);
  1154. DEF_TPG_PARAM(FirstBurstLength);
  1155. TPG_PARAM_ATTR(FirstBurstLength, S_IRUGO | S_IWUSR);
  1156. DEF_TPG_PARAM(DefaultTime2Wait);
  1157. TPG_PARAM_ATTR(DefaultTime2Wait, S_IRUGO | S_IWUSR);
  1158. DEF_TPG_PARAM(DefaultTime2Retain);
  1159. TPG_PARAM_ATTR(DefaultTime2Retain, S_IRUGO | S_IWUSR);
  1160. DEF_TPG_PARAM(MaxOutstandingR2T);
  1161. TPG_PARAM_ATTR(MaxOutstandingR2T, S_IRUGO | S_IWUSR);
  1162. DEF_TPG_PARAM(DataPDUInOrder);
  1163. TPG_PARAM_ATTR(DataPDUInOrder, S_IRUGO | S_IWUSR);
  1164. DEF_TPG_PARAM(DataSequenceInOrder);
  1165. TPG_PARAM_ATTR(DataSequenceInOrder, S_IRUGO | S_IWUSR);
  1166. DEF_TPG_PARAM(ErrorRecoveryLevel);
  1167. TPG_PARAM_ATTR(ErrorRecoveryLevel, S_IRUGO | S_IWUSR);
  1168. DEF_TPG_PARAM(IFMarker);
  1169. TPG_PARAM_ATTR(IFMarker, S_IRUGO | S_IWUSR);
  1170. DEF_TPG_PARAM(OFMarker);
  1171. TPG_PARAM_ATTR(OFMarker, S_IRUGO | S_IWUSR);
  1172. DEF_TPG_PARAM(IFMarkInt);
  1173. TPG_PARAM_ATTR(IFMarkInt, S_IRUGO | S_IWUSR);
  1174. DEF_TPG_PARAM(OFMarkInt);
  1175. TPG_PARAM_ATTR(OFMarkInt, S_IRUGO | S_IWUSR);
  1176. static struct configfs_attribute *lio_target_tpg_param_attrs[] = {
  1177. &iscsi_tpg_param_AuthMethod.attr,
  1178. &iscsi_tpg_param_HeaderDigest.attr,
  1179. &iscsi_tpg_param_DataDigest.attr,
  1180. &iscsi_tpg_param_MaxConnections.attr,
  1181. &iscsi_tpg_param_TargetAlias.attr,
  1182. &iscsi_tpg_param_InitialR2T.attr,
  1183. &iscsi_tpg_param_ImmediateData.attr,
  1184. &iscsi_tpg_param_MaxRecvDataSegmentLength.attr,
  1185. &iscsi_tpg_param_MaxXmitDataSegmentLength.attr,
  1186. &iscsi_tpg_param_MaxBurstLength.attr,
  1187. &iscsi_tpg_param_FirstBurstLength.attr,
  1188. &iscsi_tpg_param_DefaultTime2Wait.attr,
  1189. &iscsi_tpg_param_DefaultTime2Retain.attr,
  1190. &iscsi_tpg_param_MaxOutstandingR2T.attr,
  1191. &iscsi_tpg_param_DataPDUInOrder.attr,
  1192. &iscsi_tpg_param_DataSequenceInOrder.attr,
  1193. &iscsi_tpg_param_ErrorRecoveryLevel.attr,
  1194. &iscsi_tpg_param_IFMarker.attr,
  1195. &iscsi_tpg_param_OFMarker.attr,
  1196. &iscsi_tpg_param_IFMarkInt.attr,
  1197. &iscsi_tpg_param_OFMarkInt.attr,
  1198. NULL,
  1199. };
  1200. /* End items for lio_target_tpg_param_cit */
  1201. /* Start items for lio_target_tpg_cit */
  1202. static ssize_t lio_target_tpg_show_enable(
  1203. struct se_portal_group *se_tpg,
  1204. char *page)
  1205. {
  1206. struct iscsi_portal_group *tpg = container_of(se_tpg,
  1207. struct iscsi_portal_group, tpg_se_tpg);
  1208. ssize_t len;
  1209. spin_lock(&tpg->tpg_state_lock);
  1210. len = sprintf(page, "%d\n",
  1211. (tpg->tpg_state == TPG_STATE_ACTIVE) ? 1 : 0);
  1212. spin_unlock(&tpg->tpg_state_lock);
  1213. return len;
  1214. }
  1215. static ssize_t lio_target_tpg_store_enable(
  1216. struct se_portal_group *se_tpg,
  1217. const char *page,
  1218. size_t count)
  1219. {
  1220. struct iscsi_portal_group *tpg = container_of(se_tpg,
  1221. struct iscsi_portal_group, tpg_se_tpg);
  1222. u32 op;
  1223. int ret;
  1224. ret = kstrtou32(page, 0, &op);
  1225. if (ret)
  1226. return ret;
  1227. if ((op != 1) && (op != 0)) {
  1228. pr_err("Illegal value for tpg_enable: %u\n", op);
  1229. return -EINVAL;
  1230. }
  1231. ret = iscsit_get_tpg(tpg);
  1232. if (ret < 0)
  1233. return -EINVAL;
  1234. if (op) {
  1235. ret = iscsit_tpg_enable_portal_group(tpg);
  1236. if (ret < 0)
  1237. goto out;
  1238. } else {
  1239. /*
  1240. * iscsit_tpg_disable_portal_group() assumes force=1
  1241. */
  1242. ret = iscsit_tpg_disable_portal_group(tpg, 1);
  1243. if (ret < 0)
  1244. goto out;
  1245. }
  1246. iscsit_put_tpg(tpg);
  1247. return count;
  1248. out:
  1249. iscsit_put_tpg(tpg);
  1250. return -EINVAL;
  1251. }
  1252. TF_TPG_BASE_ATTR(lio_target, enable, S_IRUGO | S_IWUSR);
  1253. static struct configfs_attribute *lio_target_tpg_attrs[] = {
  1254. &lio_target_tpg_enable.attr,
  1255. NULL,
  1256. };
  1257. /* End items for lio_target_tpg_cit */
  1258. /* Start items for lio_target_tiqn_cit */
  1259. static struct se_portal_group *lio_target_tiqn_addtpg(
  1260. struct se_wwn *wwn,
  1261. struct config_group *group,
  1262. const char *name)
  1263. {
  1264. struct iscsi_portal_group *tpg;
  1265. struct iscsi_tiqn *tiqn;
  1266. char *tpgt_str;
  1267. int ret;
  1268. u16 tpgt;
  1269. tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
  1270. /*
  1271. * Only tpgt_# directory groups can be created below
  1272. * target/iscsi/iqn.superturodiskarry/
  1273. */
  1274. tpgt_str = strstr(name, "tpgt_");
  1275. if (!tpgt_str) {
  1276. pr_err("Unable to locate \"tpgt_#\" directory"
  1277. " group\n");
  1278. return NULL;
  1279. }
  1280. tpgt_str += 5; /* Skip ahead of "tpgt_" */
  1281. ret = kstrtou16(tpgt_str, 0, &tpgt);
  1282. if (ret)
  1283. return NULL;
  1284. tpg = iscsit_alloc_portal_group(tiqn, tpgt);
  1285. if (!tpg)
  1286. return NULL;
  1287. ret = core_tpg_register(
  1288. &lio_target_fabric_configfs->tf_ops,
  1289. wwn, &tpg->tpg_se_tpg, tpg,
  1290. TRANSPORT_TPG_TYPE_NORMAL);
  1291. if (ret < 0)
  1292. return NULL;
  1293. ret = iscsit_tpg_add_portal_group(tiqn, tpg);
  1294. if (ret != 0)
  1295. goto out;
  1296. pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
  1297. pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated TPG: %s\n",
  1298. name);
  1299. return &tpg->tpg_se_tpg;
  1300. out:
  1301. core_tpg_deregister(&tpg->tpg_se_tpg);
  1302. kfree(tpg);
  1303. return NULL;
  1304. }
  1305. static void lio_target_tiqn_deltpg(struct se_portal_group *se_tpg)
  1306. {
  1307. struct iscsi_portal_group *tpg;
  1308. struct iscsi_tiqn *tiqn;
  1309. tpg = container_of(se_tpg, struct iscsi_portal_group, tpg_se_tpg);
  1310. tiqn = tpg->tpg_tiqn;
  1311. /*
  1312. * iscsit_tpg_del_portal_group() assumes force=1
  1313. */
  1314. pr_debug("LIO_Target_ConfigFS: DEREGISTER -> Releasing TPG\n");
  1315. iscsit_tpg_del_portal_group(tiqn, tpg, 1);
  1316. }
  1317. /* End items for lio_target_tiqn_cit */
  1318. /* Start LIO-Target TIQN struct contig_item lio_target_cit */
  1319. static ssize_t lio_target_wwn_show_attr_lio_version(
  1320. struct target_fabric_configfs *tf,
  1321. char *page)
  1322. {
  1323. return sprintf(page, "Datera Inc. iSCSI Target "ISCSIT_VERSION"\n");
  1324. }
  1325. TF_WWN_ATTR_RO(lio_target, lio_version);
  1326. static struct configfs_attribute *lio_target_wwn_attrs[] = {
  1327. &lio_target_wwn_lio_version.attr,
  1328. NULL,
  1329. };
  1330. static struct se_wwn *lio_target_call_coreaddtiqn(
  1331. struct target_fabric_configfs *tf,
  1332. struct config_group *group,
  1333. const char *name)
  1334. {
  1335. struct config_group *stats_cg;
  1336. struct iscsi_tiqn *tiqn;
  1337. tiqn = iscsit_add_tiqn((unsigned char *)name);
  1338. if (IS_ERR(tiqn))
  1339. return ERR_CAST(tiqn);
  1340. /*
  1341. * Setup struct iscsi_wwn_stat_grps for se_wwn->fabric_stat_group.
  1342. */
  1343. stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
  1344. stats_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
  1345. GFP_KERNEL);
  1346. if (!stats_cg->default_groups) {
  1347. pr_err("Unable to allocate memory for"
  1348. " stats_cg->default_groups\n");
  1349. iscsit_del_tiqn(tiqn);
  1350. return ERR_PTR(-ENOMEM);
  1351. }
  1352. stats_cg->default_groups[0] = &tiqn->tiqn_stat_grps.iscsi_instance_group;
  1353. stats_cg->default_groups[1] = &tiqn->tiqn_stat_grps.iscsi_sess_err_group;
  1354. stats_cg->default_groups[2] = &tiqn->tiqn_stat_grps.iscsi_tgt_attr_group;
  1355. stats_cg->default_groups[3] = &tiqn->tiqn_stat_grps.iscsi_login_stats_group;
  1356. stats_cg->default_groups[4] = &tiqn->tiqn_stat_grps.iscsi_logout_stats_group;
  1357. stats_cg->default_groups[5] = NULL;
  1358. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_instance_group,
  1359. "iscsi_instance", &iscsi_stat_instance_cit);
  1360. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_sess_err_group,
  1361. "iscsi_sess_err", &iscsi_stat_sess_err_cit);
  1362. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_tgt_attr_group,
  1363. "iscsi_tgt_attr", &iscsi_stat_tgt_attr_cit);
  1364. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_login_stats_group,
  1365. "iscsi_login_stats", &iscsi_stat_login_cit);
  1366. config_group_init_type_name(&tiqn->tiqn_stat_grps.iscsi_logout_stats_group,
  1367. "iscsi_logout_stats", &iscsi_stat_logout_cit);
  1368. pr_debug("LIO_Target_ConfigFS: REGISTER -> %s\n", tiqn->tiqn);
  1369. pr_debug("LIO_Target_ConfigFS: REGISTER -> Allocated Node:"
  1370. " %s\n", name);
  1371. return &tiqn->tiqn_wwn;
  1372. }
  1373. static void lio_target_call_coredeltiqn(
  1374. struct se_wwn *wwn)
  1375. {
  1376. struct iscsi_tiqn *tiqn = container_of(wwn, struct iscsi_tiqn, tiqn_wwn);
  1377. struct config_item *df_item;
  1378. struct config_group *stats_cg;
  1379. int i;
  1380. stats_cg = &tiqn->tiqn_wwn.fabric_stat_group;
  1381. for (i = 0; stats_cg->default_groups[i]; i++) {
  1382. df_item = &stats_cg->default_groups[i]->cg_item;
  1383. stats_cg->default_groups[i] = NULL;
  1384. config_item_put(df_item);
  1385. }
  1386. kfree(stats_cg->default_groups);
  1387. pr_debug("LIO_Target_ConfigFS: DEREGISTER -> %s\n",
  1388. tiqn->tiqn);
  1389. iscsit_del_tiqn(tiqn);
  1390. }
  1391. /* End LIO-Target TIQN struct contig_lio_target_cit */
  1392. /* Start lio_target_discovery_auth_cit */
  1393. #define DEF_DISC_AUTH_STR(name, flags) \
  1394. __DEF_NACL_AUTH_STR(disc, name, flags) \
  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. static ssize_t iscsi_disc_store_##name( \
  1403. struct target_fabric_configfs *tf, \
  1404. const char *page, \
  1405. size_t count) \
  1406. { \
  1407. return __iscsi_disc_store_##name(&iscsit_global->discovery_acl, \
  1408. page, count); \
  1409. }
  1410. #define DEF_DISC_AUTH_INT(name) \
  1411. __DEF_NACL_AUTH_INT(disc, name) \
  1412. static ssize_t iscsi_disc_show_##name( \
  1413. struct target_fabric_configfs *tf, \
  1414. char *page) \
  1415. { \
  1416. return __iscsi_disc_show_##name(&iscsit_global->discovery_acl, \
  1417. page); \
  1418. }
  1419. #define DISC_AUTH_ATTR(_name, _mode) TF_DISC_ATTR(iscsi, _name, _mode)
  1420. #define DISC_AUTH_ATTR_RO(_name) TF_DISC_ATTR_RO(iscsi, _name)
  1421. /*
  1422. * One-way authentication userid
  1423. */
  1424. DEF_DISC_AUTH_STR(userid, NAF_USERID_SET);
  1425. DISC_AUTH_ATTR(userid, S_IRUGO | S_IWUSR);
  1426. /*
  1427. * One-way authentication password
  1428. */
  1429. DEF_DISC_AUTH_STR(password, NAF_PASSWORD_SET);
  1430. DISC_AUTH_ATTR(password, S_IRUGO | S_IWUSR);
  1431. /*
  1432. * Enforce mutual authentication
  1433. */
  1434. DEF_DISC_AUTH_INT(authenticate_target);
  1435. DISC_AUTH_ATTR_RO(authenticate_target);
  1436. /*
  1437. * Mutual authentication userid
  1438. */
  1439. DEF_DISC_AUTH_STR(userid_mutual, NAF_USERID_IN_SET);
  1440. DISC_AUTH_ATTR(userid_mutual, S_IRUGO | S_IWUSR);
  1441. /*
  1442. * Mutual authentication password
  1443. */
  1444. DEF_DISC_AUTH_STR(password_mutual, NAF_PASSWORD_IN_SET);
  1445. DISC_AUTH_ATTR(password_mutual, S_IRUGO | S_IWUSR);
  1446. /*
  1447. * enforce_discovery_auth
  1448. */
  1449. static ssize_t iscsi_disc_show_enforce_discovery_auth(
  1450. struct target_fabric_configfs *tf,
  1451. char *page)
  1452. {
  1453. struct iscsi_node_auth *discovery_auth = &iscsit_global->discovery_acl.node_auth;
  1454. return sprintf(page, "%d\n", discovery_auth->enforce_discovery_auth);
  1455. }
  1456. static ssize_t iscsi_disc_store_enforce_discovery_auth(
  1457. struct target_fabric_configfs *tf,
  1458. const char *page,
  1459. size_t count)
  1460. {
  1461. struct iscsi_param *param;
  1462. struct iscsi_portal_group *discovery_tpg = iscsit_global->discovery_tpg;
  1463. u32 op;
  1464. int err;
  1465. err = kstrtou32(page, 0, &op);
  1466. if (err)
  1467. return -EINVAL;
  1468. if ((op != 1) && (op != 0)) {
  1469. pr_err("Illegal value for enforce_discovery_auth:"
  1470. " %u\n", op);
  1471. return -EINVAL;
  1472. }
  1473. if (!discovery_tpg) {
  1474. pr_err("iscsit_global->discovery_tpg is NULL\n");
  1475. return -EINVAL;
  1476. }
  1477. param = iscsi_find_param_from_key(AUTHMETHOD,
  1478. discovery_tpg->param_list);
  1479. if (!param)
  1480. return -EINVAL;
  1481. if (op) {
  1482. /*
  1483. * Reset the AuthMethod key to CHAP.
  1484. */
  1485. if (iscsi_update_param_value(param, CHAP) < 0)
  1486. return -EINVAL;
  1487. discovery_tpg->tpg_attrib.authentication = 1;
  1488. iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 1;
  1489. pr_debug("LIO-CORE[0] Successfully enabled"
  1490. " authentication enforcement for iSCSI"
  1491. " Discovery TPG\n");
  1492. } else {
  1493. /*
  1494. * Reset the AuthMethod key to CHAP,None
  1495. */
  1496. if (iscsi_update_param_value(param, "CHAP,None") < 0)
  1497. return -EINVAL;
  1498. discovery_tpg->tpg_attrib.authentication = 0;
  1499. iscsit_global->discovery_acl.node_auth.enforce_discovery_auth = 0;
  1500. pr_debug("LIO-CORE[0] Successfully disabled"
  1501. " authentication enforcement for iSCSI"
  1502. " Discovery TPG\n");
  1503. }
  1504. return count;
  1505. }
  1506. DISC_AUTH_ATTR(enforce_discovery_auth, S_IRUGO | S_IWUSR);
  1507. static struct configfs_attribute *lio_target_discovery_auth_attrs[] = {
  1508. &iscsi_disc_userid.attr,
  1509. &iscsi_disc_password.attr,
  1510. &iscsi_disc_authenticate_target.attr,
  1511. &iscsi_disc_userid_mutual.attr,
  1512. &iscsi_disc_password_mutual.attr,
  1513. &iscsi_disc_enforce_discovery_auth.attr,
  1514. NULL,
  1515. };
  1516. /* End lio_target_discovery_auth_cit */
  1517. /* Start functions for target_core_fabric_ops */
  1518. static char *iscsi_get_fabric_name(void)
  1519. {
  1520. return "iSCSI";
  1521. }
  1522. static u32 iscsi_get_task_tag(struct se_cmd *se_cmd)
  1523. {
  1524. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1525. /* only used for printks or comparism with ->ref_task_tag */
  1526. return (__force u32)cmd->init_task_tag;
  1527. }
  1528. static int iscsi_get_cmd_state(struct se_cmd *se_cmd)
  1529. {
  1530. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1531. return cmd->i_state;
  1532. }
  1533. static u32 lio_sess_get_index(struct se_session *se_sess)
  1534. {
  1535. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1536. return sess->session_index;
  1537. }
  1538. static u32 lio_sess_get_initiator_sid(
  1539. struct se_session *se_sess,
  1540. unsigned char *buf,
  1541. u32 size)
  1542. {
  1543. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1544. /*
  1545. * iSCSI Initiator Session Identifier from RFC-3720.
  1546. */
  1547. return snprintf(buf, size, "%02x%02x%02x%02x%02x%02x",
  1548. sess->isid[0], sess->isid[1], sess->isid[2],
  1549. sess->isid[3], sess->isid[4], sess->isid[5]);
  1550. }
  1551. static int lio_queue_data_in(struct se_cmd *se_cmd)
  1552. {
  1553. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1554. cmd->i_state = ISTATE_SEND_DATAIN;
  1555. cmd->conn->conn_transport->iscsit_queue_data_in(cmd->conn, cmd);
  1556. return 0;
  1557. }
  1558. static int lio_write_pending(struct se_cmd *se_cmd)
  1559. {
  1560. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1561. struct iscsi_conn *conn = cmd->conn;
  1562. if (!cmd->immediate_data && !cmd->unsolicited_data)
  1563. return conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
  1564. return 0;
  1565. }
  1566. static int lio_write_pending_status(struct se_cmd *se_cmd)
  1567. {
  1568. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1569. int ret;
  1570. spin_lock_bh(&cmd->istate_lock);
  1571. ret = !(cmd->cmd_flags & ICF_GOT_LAST_DATAOUT);
  1572. spin_unlock_bh(&cmd->istate_lock);
  1573. return ret;
  1574. }
  1575. static int lio_queue_status(struct se_cmd *se_cmd)
  1576. {
  1577. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1578. cmd->i_state = ISTATE_SEND_STATUS;
  1579. if (cmd->se_cmd.scsi_status || cmd->sense_reason) {
  1580. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  1581. return 0;
  1582. }
  1583. cmd->conn->conn_transport->iscsit_queue_status(cmd->conn, cmd);
  1584. return 0;
  1585. }
  1586. static void lio_queue_tm_rsp(struct se_cmd *se_cmd)
  1587. {
  1588. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1589. cmd->i_state = ISTATE_SEND_TASKMGTRSP;
  1590. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  1591. }
  1592. static void lio_aborted_task(struct se_cmd *se_cmd)
  1593. {
  1594. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1595. cmd->conn->conn_transport->iscsit_aborted_task(cmd->conn, cmd);
  1596. }
  1597. static char *lio_tpg_get_endpoint_wwn(struct se_portal_group *se_tpg)
  1598. {
  1599. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1600. return &tpg->tpg_tiqn->tiqn[0];
  1601. }
  1602. static u16 lio_tpg_get_tag(struct se_portal_group *se_tpg)
  1603. {
  1604. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1605. return tpg->tpgt;
  1606. }
  1607. static u32 lio_tpg_get_default_depth(struct se_portal_group *se_tpg)
  1608. {
  1609. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1610. return tpg->tpg_attrib.default_cmdsn_depth;
  1611. }
  1612. static int lio_tpg_check_demo_mode(struct se_portal_group *se_tpg)
  1613. {
  1614. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1615. return tpg->tpg_attrib.generate_node_acls;
  1616. }
  1617. static int lio_tpg_check_demo_mode_cache(struct se_portal_group *se_tpg)
  1618. {
  1619. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1620. return tpg->tpg_attrib.cache_dynamic_acls;
  1621. }
  1622. static int lio_tpg_check_demo_mode_write_protect(
  1623. struct se_portal_group *se_tpg)
  1624. {
  1625. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1626. return tpg->tpg_attrib.demo_mode_write_protect;
  1627. }
  1628. static int lio_tpg_check_prod_mode_write_protect(
  1629. struct se_portal_group *se_tpg)
  1630. {
  1631. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1632. return tpg->tpg_attrib.prod_mode_write_protect;
  1633. }
  1634. static void lio_tpg_release_fabric_acl(
  1635. struct se_portal_group *se_tpg,
  1636. struct se_node_acl *se_acl)
  1637. {
  1638. struct iscsi_node_acl *acl = container_of(se_acl,
  1639. struct iscsi_node_acl, se_node_acl);
  1640. kfree(acl);
  1641. }
  1642. /*
  1643. * Called with spin_lock_bh(struct se_portal_group->session_lock) held..
  1644. *
  1645. * Also, this function calls iscsit_inc_session_usage_count() on the
  1646. * struct iscsi_session in question.
  1647. */
  1648. static int lio_tpg_shutdown_session(struct se_session *se_sess)
  1649. {
  1650. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1651. spin_lock(&sess->conn_lock);
  1652. if (atomic_read(&sess->session_fall_back_to_erl0) ||
  1653. atomic_read(&sess->session_logout) ||
  1654. (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
  1655. spin_unlock(&sess->conn_lock);
  1656. return 0;
  1657. }
  1658. atomic_set(&sess->session_reinstatement, 1);
  1659. spin_unlock(&sess->conn_lock);
  1660. iscsit_stop_time2retain_timer(sess);
  1661. iscsit_stop_session(sess, 1, 1);
  1662. return 1;
  1663. }
  1664. /*
  1665. * Calls iscsit_dec_session_usage_count() as inverse of
  1666. * lio_tpg_shutdown_session()
  1667. */
  1668. static void lio_tpg_close_session(struct se_session *se_sess)
  1669. {
  1670. struct iscsi_session *sess = se_sess->fabric_sess_ptr;
  1671. /*
  1672. * If the iSCSI Session for the iSCSI Initiator Node exists,
  1673. * forcefully shutdown the iSCSI NEXUS.
  1674. */
  1675. iscsit_close_session(sess);
  1676. }
  1677. static u32 lio_tpg_get_inst_index(struct se_portal_group *se_tpg)
  1678. {
  1679. struct iscsi_portal_group *tpg = se_tpg->se_tpg_fabric_ptr;
  1680. return tpg->tpg_tiqn->tiqn_index;
  1681. }
  1682. static void lio_set_default_node_attributes(struct se_node_acl *se_acl)
  1683. {
  1684. struct iscsi_node_acl *acl = container_of(se_acl, struct iscsi_node_acl,
  1685. se_node_acl);
  1686. struct se_portal_group *se_tpg = se_acl->se_tpg;
  1687. struct iscsi_portal_group *tpg = container_of(se_tpg,
  1688. struct iscsi_portal_group, tpg_se_tpg);
  1689. acl->node_attrib.nacl = acl;
  1690. iscsit_set_default_node_attribues(acl, tpg);
  1691. }
  1692. static int lio_check_stop_free(struct se_cmd *se_cmd)
  1693. {
  1694. return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  1695. }
  1696. static void lio_release_cmd(struct se_cmd *se_cmd)
  1697. {
  1698. struct iscsi_cmd *cmd = container_of(se_cmd, struct iscsi_cmd, se_cmd);
  1699. pr_debug("Entering lio_release_cmd for se_cmd: %p\n", se_cmd);
  1700. iscsit_release_cmd(cmd);
  1701. }
  1702. /* End functions for target_core_fabric_ops */
  1703. int iscsi_target_register_configfs(void)
  1704. {
  1705. struct target_fabric_configfs *fabric;
  1706. int ret;
  1707. lio_target_fabric_configfs = NULL;
  1708. fabric = target_fabric_configfs_init(THIS_MODULE, "iscsi");
  1709. if (IS_ERR(fabric)) {
  1710. pr_err("target_fabric_configfs_init() for"
  1711. " LIO-Target failed!\n");
  1712. return PTR_ERR(fabric);
  1713. }
  1714. /*
  1715. * Setup the fabric API of function pointers used by target_core_mod..
  1716. */
  1717. fabric->tf_ops.get_fabric_name = &iscsi_get_fabric_name;
  1718. fabric->tf_ops.get_fabric_proto_ident = &iscsi_get_fabric_proto_ident;
  1719. fabric->tf_ops.tpg_get_wwn = &lio_tpg_get_endpoint_wwn;
  1720. fabric->tf_ops.tpg_get_tag = &lio_tpg_get_tag;
  1721. fabric->tf_ops.tpg_get_default_depth = &lio_tpg_get_default_depth;
  1722. fabric->tf_ops.tpg_get_pr_transport_id = &iscsi_get_pr_transport_id;
  1723. fabric->tf_ops.tpg_get_pr_transport_id_len =
  1724. &iscsi_get_pr_transport_id_len;
  1725. fabric->tf_ops.tpg_parse_pr_out_transport_id =
  1726. &iscsi_parse_pr_out_transport_id;
  1727. fabric->tf_ops.tpg_check_demo_mode = &lio_tpg_check_demo_mode;
  1728. fabric->tf_ops.tpg_check_demo_mode_cache =
  1729. &lio_tpg_check_demo_mode_cache;
  1730. fabric->tf_ops.tpg_check_demo_mode_write_protect =
  1731. &lio_tpg_check_demo_mode_write_protect;
  1732. fabric->tf_ops.tpg_check_prod_mode_write_protect =
  1733. &lio_tpg_check_prod_mode_write_protect;
  1734. fabric->tf_ops.tpg_alloc_fabric_acl = &lio_tpg_alloc_fabric_acl;
  1735. fabric->tf_ops.tpg_release_fabric_acl = &lio_tpg_release_fabric_acl;
  1736. fabric->tf_ops.tpg_get_inst_index = &lio_tpg_get_inst_index;
  1737. fabric->tf_ops.check_stop_free = &lio_check_stop_free,
  1738. fabric->tf_ops.release_cmd = &lio_release_cmd;
  1739. fabric->tf_ops.shutdown_session = &lio_tpg_shutdown_session;
  1740. fabric->tf_ops.close_session = &lio_tpg_close_session;
  1741. fabric->tf_ops.sess_get_index = &lio_sess_get_index;
  1742. fabric->tf_ops.sess_get_initiator_sid = &lio_sess_get_initiator_sid;
  1743. fabric->tf_ops.write_pending = &lio_write_pending;
  1744. fabric->tf_ops.write_pending_status = &lio_write_pending_status;
  1745. fabric->tf_ops.set_default_node_attributes =
  1746. &lio_set_default_node_attributes;
  1747. fabric->tf_ops.get_task_tag = &iscsi_get_task_tag;
  1748. fabric->tf_ops.get_cmd_state = &iscsi_get_cmd_state;
  1749. fabric->tf_ops.queue_data_in = &lio_queue_data_in;
  1750. fabric->tf_ops.queue_status = &lio_queue_status;
  1751. fabric->tf_ops.queue_tm_rsp = &lio_queue_tm_rsp;
  1752. fabric->tf_ops.aborted_task = &lio_aborted_task;
  1753. /*
  1754. * Setup function pointers for generic logic in target_core_fabric_configfs.c
  1755. */
  1756. fabric->tf_ops.fabric_make_wwn = &lio_target_call_coreaddtiqn;
  1757. fabric->tf_ops.fabric_drop_wwn = &lio_target_call_coredeltiqn;
  1758. fabric->tf_ops.fabric_make_tpg = &lio_target_tiqn_addtpg;
  1759. fabric->tf_ops.fabric_drop_tpg = &lio_target_tiqn_deltpg;
  1760. fabric->tf_ops.fabric_post_link = NULL;
  1761. fabric->tf_ops.fabric_pre_unlink = NULL;
  1762. fabric->tf_ops.fabric_make_np = &lio_target_call_addnptotpg;
  1763. fabric->tf_ops.fabric_drop_np = &lio_target_call_delnpfromtpg;
  1764. fabric->tf_ops.fabric_make_nodeacl = &lio_target_make_nodeacl;
  1765. fabric->tf_ops.fabric_drop_nodeacl = &lio_target_drop_nodeacl;
  1766. /*
  1767. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1768. * sturct config_item_type's
  1769. */
  1770. fabric->tf_cit_tmpl.tfc_discovery_cit.ct_attrs = lio_target_discovery_auth_attrs;
  1771. fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = lio_target_wwn_attrs;
  1772. fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = lio_target_tpg_attrs;
  1773. fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = lio_target_tpg_attrib_attrs;
  1774. fabric->tf_cit_tmpl.tfc_tpg_auth_cit.ct_attrs = lio_target_tpg_auth_attrs;
  1775. fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = lio_target_tpg_param_attrs;
  1776. fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = lio_target_portal_attrs;
  1777. fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = lio_target_initiator_attrs;
  1778. fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = lio_target_nacl_attrib_attrs;
  1779. fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = lio_target_nacl_auth_attrs;
  1780. fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = lio_target_nacl_param_attrs;
  1781. ret = target_fabric_configfs_register(fabric);
  1782. if (ret < 0) {
  1783. pr_err("target_fabric_configfs_register() for"
  1784. " LIO-Target failed!\n");
  1785. target_fabric_configfs_free(fabric);
  1786. return ret;
  1787. }
  1788. lio_target_fabric_configfs = fabric;
  1789. pr_debug("LIO_TARGET[0] - Set fabric ->"
  1790. " lio_target_fabric_configfs\n");
  1791. return 0;
  1792. }
  1793. void iscsi_target_deregister_configfs(void)
  1794. {
  1795. if (!lio_target_fabric_configfs)
  1796. return;
  1797. /*
  1798. * Shutdown discovery sessions and disable discovery TPG
  1799. */
  1800. if (iscsit_global->discovery_tpg)
  1801. iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
  1802. target_fabric_configfs_deregister(lio_target_fabric_configfs);
  1803. lio_target_fabric_configfs = NULL;
  1804. pr_debug("LIO_TARGET[0] - Cleared"
  1805. " lio_target_fabric_configfs\n");
  1806. }