iscsi_target_configfs.c 57 KB

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