iscsi_target_configfs.c 56 KB

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