iscsi_target_configfs.c 52 KB

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