target_core_fabric_configfs.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /*******************************************************************************
  2. * Filename: target_core_fabric_configfs.c
  3. *
  4. * This file contains generic fabric module configfs infrastructure for
  5. * TCM v4.x code
  6. *
  7. * (c) Copyright 2010-2012 RisingTide Systems LLC.
  8. *
  9. * 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/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/utsname.h>
  24. #include <linux/init.h>
  25. #include <linux/fs.h>
  26. #include <linux/namei.h>
  27. #include <linux/slab.h>
  28. #include <linux/types.h>
  29. #include <linux/delay.h>
  30. #include <linux/unistd.h>
  31. #include <linux/string.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/configfs.h>
  34. #include <target/target_core_base.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_fabric_configfs.h>
  37. #include <target/target_core_configfs.h>
  38. #include <target/configfs_macros.h>
  39. #include "target_core_internal.h"
  40. #include "target_core_alua.h"
  41. #include "target_core_pr.h"
  42. #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
  43. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  44. { \
  45. struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
  46. struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
  47. \
  48. cit->ct_item_ops = _item_ops; \
  49. cit->ct_group_ops = _group_ops; \
  50. cit->ct_attrs = _attrs; \
  51. cit->ct_owner = tf->tf_module; \
  52. pr_debug("Setup generic %s\n", __stringify(_name)); \
  53. }
  54. /* Start of tfc_tpg_mappedlun_cit */
  55. static int target_fabric_mappedlun_link(
  56. struct config_item *lun_acl_ci,
  57. struct config_item *lun_ci)
  58. {
  59. struct se_dev_entry *deve;
  60. struct se_lun *lun = container_of(to_config_group(lun_ci),
  61. struct se_lun, lun_group);
  62. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  63. struct se_lun_acl, se_lun_group);
  64. struct se_portal_group *se_tpg;
  65. struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
  66. int ret = 0, lun_access;
  67. if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
  68. pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
  69. " %p to struct lun: %p\n", lun_ci, lun);
  70. return -EFAULT;
  71. }
  72. /*
  73. * Ensure that the source port exists
  74. */
  75. if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
  76. pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
  77. "_tpg does not exist\n");
  78. return -EINVAL;
  79. }
  80. se_tpg = lun->lun_sep->sep_tpg;
  81. nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
  82. tpg_ci = &nacl_ci->ci_group->cg_item;
  83. wwn_ci = &tpg_ci->ci_group->cg_item;
  84. tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
  85. wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
  86. /*
  87. * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
  88. */
  89. if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
  90. pr_err("Illegal Initiator ACL SymLink outside of %s\n",
  91. config_item_name(wwn_ci));
  92. return -EINVAL;
  93. }
  94. if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
  95. pr_err("Illegal Initiator ACL Symlink outside of %s"
  96. " TPGT: %s\n", config_item_name(wwn_ci),
  97. config_item_name(tpg_ci));
  98. return -EINVAL;
  99. }
  100. /*
  101. * If this struct se_node_acl was dynamically generated with
  102. * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
  103. * which be will write protected (READ-ONLY) when
  104. * tpg_1/attrib/demo_mode_write_protect=1
  105. */
  106. spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
  107. deve = lacl->se_lun_nacl->device_list[lacl->mapped_lun];
  108. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
  109. lun_access = deve->lun_flags;
  110. else
  111. lun_access =
  112. (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
  113. se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
  114. TRANSPORT_LUNFLAGS_READ_WRITE;
  115. spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
  116. /*
  117. * Determine the actual mapped LUN value user wants..
  118. *
  119. * This value is what the SCSI Initiator actually sees the
  120. * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
  121. */
  122. ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
  123. lun->unpacked_lun, lun_access);
  124. return (ret < 0) ? -EINVAL : 0;
  125. }
  126. static int target_fabric_mappedlun_unlink(
  127. struct config_item *lun_acl_ci,
  128. struct config_item *lun_ci)
  129. {
  130. struct se_lun *lun;
  131. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  132. struct se_lun_acl, se_lun_group);
  133. struct se_node_acl *nacl = lacl->se_lun_nacl;
  134. struct se_dev_entry *deve = nacl->device_list[lacl->mapped_lun];
  135. struct se_portal_group *se_tpg;
  136. /*
  137. * Determine if the underlying MappedLUN has already been released..
  138. */
  139. if (!deve->se_lun)
  140. return 0;
  141. lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
  142. se_tpg = lun->lun_sep->sep_tpg;
  143. core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
  144. return 0;
  145. }
  146. CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
  147. #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
  148. static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
  149. __CONFIGFS_EATTR(_name, _mode, \
  150. target_fabric_mappedlun_show_##_name, \
  151. target_fabric_mappedlun_store_##_name);
  152. static ssize_t target_fabric_mappedlun_show_write_protect(
  153. struct se_lun_acl *lacl,
  154. char *page)
  155. {
  156. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  157. struct se_dev_entry *deve;
  158. ssize_t len;
  159. spin_lock_irq(&se_nacl->device_list_lock);
  160. deve = se_nacl->device_list[lacl->mapped_lun];
  161. len = sprintf(page, "%d\n",
  162. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
  163. 1 : 0);
  164. spin_unlock_irq(&se_nacl->device_list_lock);
  165. return len;
  166. }
  167. static ssize_t target_fabric_mappedlun_store_write_protect(
  168. struct se_lun_acl *lacl,
  169. const char *page,
  170. size_t count)
  171. {
  172. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  173. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  174. unsigned long op;
  175. int ret;
  176. ret = kstrtoul(page, 0, &op);
  177. if (ret)
  178. return ret;
  179. if ((op != 1) && (op != 0))
  180. return -EINVAL;
  181. core_update_device_list_access(lacl->mapped_lun, (op) ?
  182. TRANSPORT_LUNFLAGS_READ_ONLY :
  183. TRANSPORT_LUNFLAGS_READ_WRITE,
  184. lacl->se_lun_nacl);
  185. pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
  186. " Mapped LUN: %u Write Protect bit to %s\n",
  187. se_tpg->se_tpg_tfo->get_fabric_name(),
  188. lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
  189. return count;
  190. }
  191. TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
  192. CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
  193. static void target_fabric_mappedlun_release(struct config_item *item)
  194. {
  195. struct se_lun_acl *lacl = container_of(to_config_group(item),
  196. struct se_lun_acl, se_lun_group);
  197. struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
  198. core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
  199. }
  200. static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
  201. &target_fabric_mappedlun_write_protect.attr,
  202. NULL,
  203. };
  204. static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
  205. .release = target_fabric_mappedlun_release,
  206. .show_attribute = target_fabric_mappedlun_attr_show,
  207. .store_attribute = target_fabric_mappedlun_attr_store,
  208. .allow_link = target_fabric_mappedlun_link,
  209. .drop_link = target_fabric_mappedlun_unlink,
  210. };
  211. TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  212. target_fabric_mappedlun_attrs);
  213. /* End of tfc_tpg_mappedlun_cit */
  214. /* Start of tfc_tpg_mappedlun_port_cit */
  215. static struct config_group *target_core_mappedlun_stat_mkdir(
  216. struct config_group *group,
  217. const char *name)
  218. {
  219. return ERR_PTR(-ENOSYS);
  220. }
  221. static void target_core_mappedlun_stat_rmdir(
  222. struct config_group *group,
  223. struct config_item *item)
  224. {
  225. return;
  226. }
  227. static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
  228. .make_group = target_core_mappedlun_stat_mkdir,
  229. .drop_item = target_core_mappedlun_stat_rmdir,
  230. };
  231. TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
  232. NULL);
  233. /* End of tfc_tpg_mappedlun_port_cit */
  234. /* Start of tfc_tpg_nacl_attrib_cit */
  235. CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
  236. static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
  237. .show_attribute = target_fabric_nacl_attrib_attr_show,
  238. .store_attribute = target_fabric_nacl_attrib_attr_store,
  239. };
  240. TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
  241. /* End of tfc_tpg_nacl_attrib_cit */
  242. /* Start of tfc_tpg_nacl_auth_cit */
  243. CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
  244. static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
  245. .show_attribute = target_fabric_nacl_auth_attr_show,
  246. .store_attribute = target_fabric_nacl_auth_attr_store,
  247. };
  248. TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
  249. /* End of tfc_tpg_nacl_auth_cit */
  250. /* Start of tfc_tpg_nacl_param_cit */
  251. CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
  252. static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
  253. .show_attribute = target_fabric_nacl_param_attr_show,
  254. .store_attribute = target_fabric_nacl_param_attr_store,
  255. };
  256. TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
  257. /* End of tfc_tpg_nacl_param_cit */
  258. /* Start of tfc_tpg_nacl_base_cit */
  259. CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
  260. static struct config_group *target_fabric_make_mappedlun(
  261. struct config_group *group,
  262. const char *name)
  263. {
  264. struct se_node_acl *se_nacl = container_of(group,
  265. struct se_node_acl, acl_group);
  266. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  267. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  268. struct se_lun_acl *lacl;
  269. struct config_item *acl_ci;
  270. struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  271. char *buf;
  272. unsigned long mapped_lun;
  273. int ret = 0;
  274. acl_ci = &group->cg_item;
  275. if (!acl_ci) {
  276. pr_err("Unable to locatel acl_ci\n");
  277. return NULL;
  278. }
  279. buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
  280. if (!buf) {
  281. pr_err("Unable to allocate memory for name buf\n");
  282. return ERR_PTR(-ENOMEM);
  283. }
  284. snprintf(buf, strlen(name) + 1, "%s", name);
  285. /*
  286. * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  287. */
  288. if (strstr(buf, "lun_") != buf) {
  289. pr_err("Unable to locate \"lun_\" from buf: %s"
  290. " name: %s\n", buf, name);
  291. ret = -EINVAL;
  292. goto out;
  293. }
  294. /*
  295. * Determine the Mapped LUN value. This is what the SCSI Initiator
  296. * Port will actually see.
  297. */
  298. ret = kstrtoul(buf + 4, 0, &mapped_lun);
  299. if (ret)
  300. goto out;
  301. if (mapped_lun > UINT_MAX) {
  302. ret = -EINVAL;
  303. goto out;
  304. }
  305. if (mapped_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  306. pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
  307. "-1: %u for Target Portal Group: %u\n", mapped_lun,
  308. TRANSPORT_MAX_LUNS_PER_TPG-1,
  309. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  310. ret = -EINVAL;
  311. goto out;
  312. }
  313. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
  314. mapped_lun, &ret);
  315. if (!lacl) {
  316. ret = -EINVAL;
  317. goto out;
  318. }
  319. lacl_cg = &lacl->se_lun_group;
  320. lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  321. GFP_KERNEL);
  322. if (!lacl_cg->default_groups) {
  323. pr_err("Unable to allocate lacl_cg->default_groups\n");
  324. ret = -ENOMEM;
  325. goto out;
  326. }
  327. config_group_init_type_name(&lacl->se_lun_group, name,
  328. &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
  329. config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  330. "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_stat_cit);
  331. lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
  332. lacl_cg->default_groups[1] = NULL;
  333. ml_stat_grp = &lacl->ml_stat_grps.stat_group;
  334. ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
  335. GFP_KERNEL);
  336. if (!ml_stat_grp->default_groups) {
  337. pr_err("Unable to allocate ml_stat_grp->default_groups\n");
  338. ret = -ENOMEM;
  339. goto out;
  340. }
  341. target_stat_setup_mappedlun_default_groups(lacl);
  342. kfree(buf);
  343. return &lacl->se_lun_group;
  344. out:
  345. if (lacl_cg)
  346. kfree(lacl_cg->default_groups);
  347. kfree(buf);
  348. return ERR_PTR(ret);
  349. }
  350. static void target_fabric_drop_mappedlun(
  351. struct config_group *group,
  352. struct config_item *item)
  353. {
  354. struct se_lun_acl *lacl = container_of(to_config_group(item),
  355. struct se_lun_acl, se_lun_group);
  356. struct config_item *df_item;
  357. struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  358. int i;
  359. ml_stat_grp = &lacl->ml_stat_grps.stat_group;
  360. for (i = 0; ml_stat_grp->default_groups[i]; i++) {
  361. df_item = &ml_stat_grp->default_groups[i]->cg_item;
  362. ml_stat_grp->default_groups[i] = NULL;
  363. config_item_put(df_item);
  364. }
  365. kfree(ml_stat_grp->default_groups);
  366. lacl_cg = &lacl->se_lun_group;
  367. for (i = 0; lacl_cg->default_groups[i]; i++) {
  368. df_item = &lacl_cg->default_groups[i]->cg_item;
  369. lacl_cg->default_groups[i] = NULL;
  370. config_item_put(df_item);
  371. }
  372. kfree(lacl_cg->default_groups);
  373. config_item_put(item);
  374. }
  375. static void target_fabric_nacl_base_release(struct config_item *item)
  376. {
  377. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  378. struct se_node_acl, acl_group);
  379. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  380. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  381. tf->tf_ops.fabric_drop_nodeacl(se_nacl);
  382. }
  383. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  384. .release = target_fabric_nacl_base_release,
  385. .show_attribute = target_fabric_nacl_base_attr_show,
  386. .store_attribute = target_fabric_nacl_base_attr_store,
  387. };
  388. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  389. .make_group = target_fabric_make_mappedlun,
  390. .drop_item = target_fabric_drop_mappedlun,
  391. };
  392. TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  393. &target_fabric_nacl_base_group_ops, NULL);
  394. /* End of tfc_tpg_nacl_base_cit */
  395. /* Start of tfc_node_fabric_stats_cit */
  396. /*
  397. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  398. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  399. */
  400. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  401. /* End of tfc_wwn_fabric_stats_cit */
  402. /* Start of tfc_tpg_nacl_cit */
  403. static struct config_group *target_fabric_make_nodeacl(
  404. struct config_group *group,
  405. const char *name)
  406. {
  407. struct se_portal_group *se_tpg = container_of(group,
  408. struct se_portal_group, tpg_acl_group);
  409. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  410. struct se_node_acl *se_nacl;
  411. struct config_group *nacl_cg;
  412. if (!tf->tf_ops.fabric_make_nodeacl) {
  413. pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
  414. return ERR_PTR(-ENOSYS);
  415. }
  416. se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
  417. if (IS_ERR(se_nacl))
  418. return ERR_CAST(se_nacl);
  419. nacl_cg = &se_nacl->acl_group;
  420. nacl_cg->default_groups = se_nacl->acl_default_groups;
  421. nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
  422. nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
  423. nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
  424. nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
  425. nacl_cg->default_groups[4] = NULL;
  426. config_group_init_type_name(&se_nacl->acl_group, name,
  427. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
  428. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  429. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
  430. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  431. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
  432. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  433. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
  434. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  435. "fabric_statistics",
  436. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_stat_cit);
  437. return &se_nacl->acl_group;
  438. }
  439. static void target_fabric_drop_nodeacl(
  440. struct config_group *group,
  441. struct config_item *item)
  442. {
  443. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  444. struct se_node_acl, acl_group);
  445. struct config_item *df_item;
  446. struct config_group *nacl_cg;
  447. int i;
  448. nacl_cg = &se_nacl->acl_group;
  449. for (i = 0; nacl_cg->default_groups[i]; i++) {
  450. df_item = &nacl_cg->default_groups[i]->cg_item;
  451. nacl_cg->default_groups[i] = NULL;
  452. config_item_put(df_item);
  453. }
  454. /*
  455. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  456. */
  457. config_item_put(item);
  458. }
  459. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  460. .make_group = target_fabric_make_nodeacl,
  461. .drop_item = target_fabric_drop_nodeacl,
  462. };
  463. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  464. /* End of tfc_tpg_nacl_cit */
  465. /* Start of tfc_tpg_np_base_cit */
  466. CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
  467. static void target_fabric_np_base_release(struct config_item *item)
  468. {
  469. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  470. struct se_tpg_np, tpg_np_group);
  471. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  472. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  473. tf->tf_ops.fabric_drop_np(se_tpg_np);
  474. }
  475. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  476. .release = target_fabric_np_base_release,
  477. .show_attribute = target_fabric_np_base_attr_show,
  478. .store_attribute = target_fabric_np_base_attr_store,
  479. };
  480. TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
  481. /* End of tfc_tpg_np_base_cit */
  482. /* Start of tfc_tpg_np_cit */
  483. static struct config_group *target_fabric_make_np(
  484. struct config_group *group,
  485. const char *name)
  486. {
  487. struct se_portal_group *se_tpg = container_of(group,
  488. struct se_portal_group, tpg_np_group);
  489. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  490. struct se_tpg_np *se_tpg_np;
  491. if (!tf->tf_ops.fabric_make_np) {
  492. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  493. return ERR_PTR(-ENOSYS);
  494. }
  495. se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
  496. if (!se_tpg_np || IS_ERR(se_tpg_np))
  497. return ERR_PTR(-EINVAL);
  498. se_tpg_np->tpg_np_parent = se_tpg;
  499. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  500. &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
  501. return &se_tpg_np->tpg_np_group;
  502. }
  503. static void target_fabric_drop_np(
  504. struct config_group *group,
  505. struct config_item *item)
  506. {
  507. /*
  508. * struct se_tpg_np is released via target_fabric_np_base_release()
  509. */
  510. config_item_put(item);
  511. }
  512. static struct configfs_group_operations target_fabric_np_group_ops = {
  513. .make_group = &target_fabric_make_np,
  514. .drop_item = &target_fabric_drop_np,
  515. };
  516. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  517. /* End of tfc_tpg_np_cit */
  518. /* Start of tfc_tpg_port_cit */
  519. CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
  520. #define TCM_PORT_ATTR(_name, _mode) \
  521. static struct target_fabric_port_attribute target_fabric_port_##_name = \
  522. __CONFIGFS_EATTR(_name, _mode, \
  523. target_fabric_port_show_attr_##_name, \
  524. target_fabric_port_store_attr_##_name);
  525. #define TCM_PORT_ATTOR_RO(_name) \
  526. __CONFIGFS_EATTR_RO(_name, \
  527. target_fabric_port_show_attr_##_name);
  528. /*
  529. * alua_tg_pt_gp
  530. */
  531. static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
  532. struct se_lun *lun,
  533. char *page)
  534. {
  535. if (!lun || !lun->lun_sep)
  536. return -ENODEV;
  537. return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
  538. }
  539. static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
  540. struct se_lun *lun,
  541. const char *page,
  542. size_t count)
  543. {
  544. if (!lun || !lun->lun_sep)
  545. return -ENODEV;
  546. return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
  547. }
  548. TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
  549. /*
  550. * alua_tg_pt_offline
  551. */
  552. static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
  553. struct se_lun *lun,
  554. char *page)
  555. {
  556. if (!lun || !lun->lun_sep)
  557. return -ENODEV;
  558. return core_alua_show_offline_bit(lun, page);
  559. }
  560. static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
  561. struct se_lun *lun,
  562. const char *page,
  563. size_t count)
  564. {
  565. if (!lun || !lun->lun_sep)
  566. return -ENODEV;
  567. return core_alua_store_offline_bit(lun, page, count);
  568. }
  569. TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
  570. /*
  571. * alua_tg_pt_status
  572. */
  573. static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
  574. struct se_lun *lun,
  575. char *page)
  576. {
  577. if (!lun || !lun->lun_sep)
  578. return -ENODEV;
  579. return core_alua_show_secondary_status(lun, page);
  580. }
  581. static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
  582. struct se_lun *lun,
  583. const char *page,
  584. size_t count)
  585. {
  586. if (!lun || !lun->lun_sep)
  587. return -ENODEV;
  588. return core_alua_store_secondary_status(lun, page, count);
  589. }
  590. TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
  591. /*
  592. * alua_tg_pt_write_md
  593. */
  594. static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
  595. struct se_lun *lun,
  596. char *page)
  597. {
  598. if (!lun || !lun->lun_sep)
  599. return -ENODEV;
  600. return core_alua_show_secondary_write_metadata(lun, page);
  601. }
  602. static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
  603. struct se_lun *lun,
  604. const char *page,
  605. size_t count)
  606. {
  607. if (!lun || !lun->lun_sep)
  608. return -ENODEV;
  609. return core_alua_store_secondary_write_metadata(lun, page, count);
  610. }
  611. TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
  612. static struct configfs_attribute *target_fabric_port_attrs[] = {
  613. &target_fabric_port_alua_tg_pt_gp.attr,
  614. &target_fabric_port_alua_tg_pt_offline.attr,
  615. &target_fabric_port_alua_tg_pt_status.attr,
  616. &target_fabric_port_alua_tg_pt_write_md.attr,
  617. NULL,
  618. };
  619. CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
  620. static int target_fabric_port_link(
  621. struct config_item *lun_ci,
  622. struct config_item *se_dev_ci)
  623. {
  624. struct config_item *tpg_ci;
  625. struct se_lun *lun = container_of(to_config_group(lun_ci),
  626. struct se_lun, lun_group);
  627. struct se_lun *lun_p;
  628. struct se_portal_group *se_tpg;
  629. struct se_device *dev =
  630. container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  631. struct target_fabric_configfs *tf;
  632. int ret;
  633. if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
  634. pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
  635. " %p to struct se_device: %p\n", se_dev_ci, dev);
  636. return -EFAULT;
  637. }
  638. if (!(dev->dev_flags & DF_CONFIGURED)) {
  639. pr_err("se_device not configured yet, cannot port link\n");
  640. return -ENODEV;
  641. }
  642. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  643. se_tpg = container_of(to_config_group(tpg_ci),
  644. struct se_portal_group, tpg_group);
  645. tf = se_tpg->se_tpg_wwn->wwn_tf;
  646. if (lun->lun_se_dev != NULL) {
  647. pr_err("Port Symlink already exists\n");
  648. return -EEXIST;
  649. }
  650. lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
  651. if (IS_ERR(lun_p)) {
  652. pr_err("core_dev_add_lun() failed\n");
  653. ret = PTR_ERR(lun_p);
  654. goto out;
  655. }
  656. if (tf->tf_ops.fabric_post_link) {
  657. /*
  658. * Call the optional fabric_post_link() to allow a
  659. * fabric module to setup any additional state once
  660. * core_dev_add_lun() has been called..
  661. */
  662. tf->tf_ops.fabric_post_link(se_tpg, lun);
  663. }
  664. return 0;
  665. out:
  666. return ret;
  667. }
  668. static int target_fabric_port_unlink(
  669. struct config_item *lun_ci,
  670. struct config_item *se_dev_ci)
  671. {
  672. struct se_lun *lun = container_of(to_config_group(lun_ci),
  673. struct se_lun, lun_group);
  674. struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
  675. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  676. if (tf->tf_ops.fabric_pre_unlink) {
  677. /*
  678. * Call the optional fabric_pre_unlink() to allow a
  679. * fabric module to release any additional stat before
  680. * core_dev_del_lun() is called.
  681. */
  682. tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
  683. }
  684. core_dev_del_lun(se_tpg, lun->unpacked_lun);
  685. return 0;
  686. }
  687. static struct configfs_item_operations target_fabric_port_item_ops = {
  688. .show_attribute = target_fabric_port_attr_show,
  689. .store_attribute = target_fabric_port_attr_store,
  690. .allow_link = target_fabric_port_link,
  691. .drop_link = target_fabric_port_unlink,
  692. };
  693. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  694. /* End of tfc_tpg_port_cit */
  695. /* Start of tfc_tpg_port_stat_cit */
  696. static struct config_group *target_core_port_stat_mkdir(
  697. struct config_group *group,
  698. const char *name)
  699. {
  700. return ERR_PTR(-ENOSYS);
  701. }
  702. static void target_core_port_stat_rmdir(
  703. struct config_group *group,
  704. struct config_item *item)
  705. {
  706. return;
  707. }
  708. static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  709. .make_group = target_core_port_stat_mkdir,
  710. .drop_item = target_core_port_stat_rmdir,
  711. };
  712. TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  713. /* End of tfc_tpg_port_stat_cit */
  714. /* Start of tfc_tpg_lun_cit */
  715. static struct config_group *target_fabric_make_lun(
  716. struct config_group *group,
  717. const char *name)
  718. {
  719. struct se_lun *lun;
  720. struct se_portal_group *se_tpg = container_of(group,
  721. struct se_portal_group, tpg_lun_group);
  722. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  723. struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
  724. unsigned long unpacked_lun;
  725. int errno;
  726. if (strstr(name, "lun_") != name) {
  727. pr_err("Unable to locate \'_\" in"
  728. " \"lun_$LUN_NUMBER\"\n");
  729. return ERR_PTR(-EINVAL);
  730. }
  731. errno = kstrtoul(name + 4, 0, &unpacked_lun);
  732. if (errno)
  733. return ERR_PTR(errno);
  734. if (unpacked_lun > UINT_MAX)
  735. return ERR_PTR(-EINVAL);
  736. lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
  737. if (!lun)
  738. return ERR_PTR(-EINVAL);
  739. lun_cg = &lun->lun_group;
  740. lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  741. GFP_KERNEL);
  742. if (!lun_cg->default_groups) {
  743. pr_err("Unable to allocate lun_cg->default_groups\n");
  744. return ERR_PTR(-ENOMEM);
  745. }
  746. config_group_init_type_name(&lun->lun_group, name,
  747. &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
  748. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  749. "statistics", &TF_CIT_TMPL(tf)->tfc_tpg_port_stat_cit);
  750. lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
  751. lun_cg->default_groups[1] = NULL;
  752. port_stat_grp = &lun->port_stat_grps.stat_group;
  753. port_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 3,
  754. GFP_KERNEL);
  755. if (!port_stat_grp->default_groups) {
  756. pr_err("Unable to allocate port_stat_grp->default_groups\n");
  757. errno = -ENOMEM;
  758. goto out;
  759. }
  760. target_stat_setup_port_default_groups(lun);
  761. return &lun->lun_group;
  762. out:
  763. if (lun_cg)
  764. kfree(lun_cg->default_groups);
  765. return ERR_PTR(errno);
  766. }
  767. static void target_fabric_drop_lun(
  768. struct config_group *group,
  769. struct config_item *item)
  770. {
  771. struct se_lun *lun = container_of(to_config_group(item),
  772. struct se_lun, lun_group);
  773. struct config_item *df_item;
  774. struct config_group *lun_cg, *port_stat_grp;
  775. int i;
  776. port_stat_grp = &lun->port_stat_grps.stat_group;
  777. for (i = 0; port_stat_grp->default_groups[i]; i++) {
  778. df_item = &port_stat_grp->default_groups[i]->cg_item;
  779. port_stat_grp->default_groups[i] = NULL;
  780. config_item_put(df_item);
  781. }
  782. kfree(port_stat_grp->default_groups);
  783. lun_cg = &lun->lun_group;
  784. for (i = 0; lun_cg->default_groups[i]; i++) {
  785. df_item = &lun_cg->default_groups[i]->cg_item;
  786. lun_cg->default_groups[i] = NULL;
  787. config_item_put(df_item);
  788. }
  789. kfree(lun_cg->default_groups);
  790. config_item_put(item);
  791. }
  792. static struct configfs_group_operations target_fabric_lun_group_ops = {
  793. .make_group = &target_fabric_make_lun,
  794. .drop_item = &target_fabric_drop_lun,
  795. };
  796. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  797. /* End of tfc_tpg_lun_cit */
  798. /* Start of tfc_tpg_attrib_cit */
  799. CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
  800. static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
  801. .show_attribute = target_fabric_tpg_attrib_attr_show,
  802. .store_attribute = target_fabric_tpg_attrib_attr_store,
  803. };
  804. TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
  805. /* End of tfc_tpg_attrib_cit */
  806. /* Start of tfc_tpg_auth_cit */
  807. CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
  808. static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
  809. .show_attribute = target_fabric_tpg_auth_attr_show,
  810. .store_attribute = target_fabric_tpg_auth_attr_store,
  811. };
  812. TF_CIT_SETUP(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL, NULL);
  813. /* End of tfc_tpg_attrib_cit */
  814. /* Start of tfc_tpg_param_cit */
  815. CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
  816. static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
  817. .show_attribute = target_fabric_tpg_param_attr_show,
  818. .store_attribute = target_fabric_tpg_param_attr_store,
  819. };
  820. TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
  821. /* End of tfc_tpg_param_cit */
  822. /* Start of tfc_tpg_base_cit */
  823. /*
  824. * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
  825. */
  826. CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
  827. static void target_fabric_tpg_release(struct config_item *item)
  828. {
  829. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  830. struct se_portal_group, tpg_group);
  831. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  832. struct target_fabric_configfs *tf = wwn->wwn_tf;
  833. tf->tf_ops.fabric_drop_tpg(se_tpg);
  834. }
  835. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  836. .release = target_fabric_tpg_release,
  837. .show_attribute = target_fabric_tpg_attr_show,
  838. .store_attribute = target_fabric_tpg_attr_store,
  839. };
  840. TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
  841. /* End of tfc_tpg_base_cit */
  842. /* Start of tfc_tpg_cit */
  843. static struct config_group *target_fabric_make_tpg(
  844. struct config_group *group,
  845. const char *name)
  846. {
  847. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  848. struct target_fabric_configfs *tf = wwn->wwn_tf;
  849. struct se_portal_group *se_tpg;
  850. if (!tf->tf_ops.fabric_make_tpg) {
  851. pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
  852. return ERR_PTR(-ENOSYS);
  853. }
  854. se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
  855. if (!se_tpg || IS_ERR(se_tpg))
  856. return ERR_PTR(-EINVAL);
  857. /*
  858. * Setup default groups from pre-allocated se_tpg->tpg_default_groups
  859. */
  860. se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
  861. se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
  862. se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
  863. se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
  864. se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
  865. se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
  866. se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
  867. se_tpg->tpg_group.default_groups[6] = NULL;
  868. config_group_init_type_name(&se_tpg->tpg_group, name,
  869. &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
  870. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  871. &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
  872. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  873. &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
  874. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  875. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
  876. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  877. &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
  878. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  879. &TF_CIT_TMPL(tf)->tfc_tpg_auth_cit);
  880. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  881. &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
  882. return &se_tpg->tpg_group;
  883. }
  884. static void target_fabric_drop_tpg(
  885. struct config_group *group,
  886. struct config_item *item)
  887. {
  888. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  889. struct se_portal_group, tpg_group);
  890. struct config_group *tpg_cg = &se_tpg->tpg_group;
  891. struct config_item *df_item;
  892. int i;
  893. /*
  894. * Release default groups, but do not release tpg_cg->default_groups
  895. * memory as it is statically allocated at se_tpg->tpg_default_groups.
  896. */
  897. for (i = 0; tpg_cg->default_groups[i]; i++) {
  898. df_item = &tpg_cg->default_groups[i]->cg_item;
  899. tpg_cg->default_groups[i] = NULL;
  900. config_item_put(df_item);
  901. }
  902. config_item_put(item);
  903. }
  904. static void target_fabric_release_wwn(struct config_item *item)
  905. {
  906. struct se_wwn *wwn = container_of(to_config_group(item),
  907. struct se_wwn, wwn_group);
  908. struct target_fabric_configfs *tf = wwn->wwn_tf;
  909. tf->tf_ops.fabric_drop_wwn(wwn);
  910. }
  911. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  912. .release = target_fabric_release_wwn,
  913. };
  914. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  915. .make_group = target_fabric_make_tpg,
  916. .drop_item = target_fabric_drop_tpg,
  917. };
  918. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  919. NULL);
  920. /* End of tfc_tpg_cit */
  921. /* Start of tfc_wwn_fabric_stats_cit */
  922. /*
  923. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  924. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  925. */
  926. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  927. /* End of tfc_wwn_fabric_stats_cit */
  928. /* Start of tfc_wwn_cit */
  929. static struct config_group *target_fabric_make_wwn(
  930. struct config_group *group,
  931. const char *name)
  932. {
  933. struct target_fabric_configfs *tf = container_of(group,
  934. struct target_fabric_configfs, tf_group);
  935. struct se_wwn *wwn;
  936. if (!tf->tf_ops.fabric_make_wwn) {
  937. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  938. return ERR_PTR(-ENOSYS);
  939. }
  940. wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
  941. if (!wwn || IS_ERR(wwn))
  942. return ERR_PTR(-EINVAL);
  943. wwn->wwn_tf = tf;
  944. /*
  945. * Setup default groups from pre-allocated wwn->wwn_default_groups
  946. */
  947. wwn->wwn_group.default_groups = wwn->wwn_default_groups;
  948. wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
  949. wwn->wwn_group.default_groups[1] = NULL;
  950. config_group_init_type_name(&wwn->wwn_group, name,
  951. &TF_CIT_TMPL(tf)->tfc_tpg_cit);
  952. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  953. &TF_CIT_TMPL(tf)->tfc_wwn_fabric_stats_cit);
  954. return &wwn->wwn_group;
  955. }
  956. static void target_fabric_drop_wwn(
  957. struct config_group *group,
  958. struct config_item *item)
  959. {
  960. struct se_wwn *wwn = container_of(to_config_group(item),
  961. struct se_wwn, wwn_group);
  962. struct config_item *df_item;
  963. struct config_group *cg = &wwn->wwn_group;
  964. int i;
  965. for (i = 0; cg->default_groups[i]; i++) {
  966. df_item = &cg->default_groups[i]->cg_item;
  967. cg->default_groups[i] = NULL;
  968. config_item_put(df_item);
  969. }
  970. config_item_put(item);
  971. }
  972. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  973. .make_group = target_fabric_make_wwn,
  974. .drop_item = target_fabric_drop_wwn,
  975. };
  976. /*
  977. * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
  978. */
  979. CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
  980. static struct configfs_item_operations target_fabric_wwn_item_ops = {
  981. .show_attribute = target_fabric_wwn_attr_show,
  982. .store_attribute = target_fabric_wwn_attr_store,
  983. };
  984. TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
  985. /* End of tfc_wwn_cit */
  986. /* Start of tfc_discovery_cit */
  987. CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
  988. tf_disc_group);
  989. static struct configfs_item_operations target_fabric_discovery_item_ops = {
  990. .show_attribute = target_fabric_discovery_attr_show,
  991. .store_attribute = target_fabric_discovery_attr_store,
  992. };
  993. TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
  994. /* End of tfc_discovery_cit */
  995. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  996. {
  997. target_fabric_setup_discovery_cit(tf);
  998. target_fabric_setup_wwn_cit(tf);
  999. target_fabric_setup_wwn_fabric_stats_cit(tf);
  1000. target_fabric_setup_tpg_cit(tf);
  1001. target_fabric_setup_tpg_base_cit(tf);
  1002. target_fabric_setup_tpg_port_cit(tf);
  1003. target_fabric_setup_tpg_port_stat_cit(tf);
  1004. target_fabric_setup_tpg_lun_cit(tf);
  1005. target_fabric_setup_tpg_np_cit(tf);
  1006. target_fabric_setup_tpg_np_base_cit(tf);
  1007. target_fabric_setup_tpg_attrib_cit(tf);
  1008. target_fabric_setup_tpg_auth_cit(tf);
  1009. target_fabric_setup_tpg_param_cit(tf);
  1010. target_fabric_setup_tpg_nacl_cit(tf);
  1011. target_fabric_setup_tpg_nacl_base_cit(tf);
  1012. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  1013. target_fabric_setup_tpg_nacl_auth_cit(tf);
  1014. target_fabric_setup_tpg_nacl_param_cit(tf);
  1015. target_fabric_setup_tpg_nacl_stat_cit(tf);
  1016. target_fabric_setup_tpg_mappedlun_cit(tf);
  1017. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  1018. return 0;
  1019. }