target_core_fabric_configfs.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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-2013 Datera, Inc.
  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 = NULL;
  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->tf_cit_tmpl.tfc_tpg_mappedlun_cit);
  329. config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  330. "statistics", &tf->tf_cit_tmpl.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(lacl);
  348. kfree(buf);
  349. return ERR_PTR(ret);
  350. }
  351. static void target_fabric_drop_mappedlun(
  352. struct config_group *group,
  353. struct config_item *item)
  354. {
  355. struct se_lun_acl *lacl = container_of(to_config_group(item),
  356. struct se_lun_acl, se_lun_group);
  357. struct config_item *df_item;
  358. struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  359. int i;
  360. ml_stat_grp = &lacl->ml_stat_grps.stat_group;
  361. for (i = 0; ml_stat_grp->default_groups[i]; i++) {
  362. df_item = &ml_stat_grp->default_groups[i]->cg_item;
  363. ml_stat_grp->default_groups[i] = NULL;
  364. config_item_put(df_item);
  365. }
  366. kfree(ml_stat_grp->default_groups);
  367. lacl_cg = &lacl->se_lun_group;
  368. for (i = 0; lacl_cg->default_groups[i]; i++) {
  369. df_item = &lacl_cg->default_groups[i]->cg_item;
  370. lacl_cg->default_groups[i] = NULL;
  371. config_item_put(df_item);
  372. }
  373. kfree(lacl_cg->default_groups);
  374. config_item_put(item);
  375. }
  376. static void target_fabric_nacl_base_release(struct config_item *item)
  377. {
  378. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  379. struct se_node_acl, acl_group);
  380. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  381. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  382. tf->tf_ops.fabric_drop_nodeacl(se_nacl);
  383. }
  384. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  385. .release = target_fabric_nacl_base_release,
  386. .show_attribute = target_fabric_nacl_base_attr_show,
  387. .store_attribute = target_fabric_nacl_base_attr_store,
  388. };
  389. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  390. .make_group = target_fabric_make_mappedlun,
  391. .drop_item = target_fabric_drop_mappedlun,
  392. };
  393. TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  394. &target_fabric_nacl_base_group_ops, NULL);
  395. /* End of tfc_tpg_nacl_base_cit */
  396. /* Start of tfc_node_fabric_stats_cit */
  397. /*
  398. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  399. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  400. */
  401. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  402. /* End of tfc_wwn_fabric_stats_cit */
  403. /* Start of tfc_tpg_nacl_cit */
  404. static struct config_group *target_fabric_make_nodeacl(
  405. struct config_group *group,
  406. const char *name)
  407. {
  408. struct se_portal_group *se_tpg = container_of(group,
  409. struct se_portal_group, tpg_acl_group);
  410. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  411. struct se_node_acl *se_nacl;
  412. struct config_group *nacl_cg;
  413. if (!tf->tf_ops.fabric_make_nodeacl) {
  414. pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
  415. return ERR_PTR(-ENOSYS);
  416. }
  417. se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
  418. if (IS_ERR(se_nacl))
  419. return ERR_CAST(se_nacl);
  420. nacl_cg = &se_nacl->acl_group;
  421. nacl_cg->default_groups = se_nacl->acl_default_groups;
  422. nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
  423. nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
  424. nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
  425. nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
  426. nacl_cg->default_groups[4] = NULL;
  427. config_group_init_type_name(&se_nacl->acl_group, name,
  428. &tf->tf_cit_tmpl.tfc_tpg_nacl_base_cit);
  429. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  430. &tf->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit);
  431. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  432. &tf->tf_cit_tmpl.tfc_tpg_nacl_auth_cit);
  433. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  434. &tf->tf_cit_tmpl.tfc_tpg_nacl_param_cit);
  435. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  436. "fabric_statistics",
  437. &tf->tf_cit_tmpl.tfc_tpg_nacl_stat_cit);
  438. return &se_nacl->acl_group;
  439. }
  440. static void target_fabric_drop_nodeacl(
  441. struct config_group *group,
  442. struct config_item *item)
  443. {
  444. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  445. struct se_node_acl, acl_group);
  446. struct config_item *df_item;
  447. struct config_group *nacl_cg;
  448. int i;
  449. nacl_cg = &se_nacl->acl_group;
  450. for (i = 0; nacl_cg->default_groups[i]; i++) {
  451. df_item = &nacl_cg->default_groups[i]->cg_item;
  452. nacl_cg->default_groups[i] = NULL;
  453. config_item_put(df_item);
  454. }
  455. /*
  456. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  457. */
  458. config_item_put(item);
  459. }
  460. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  461. .make_group = target_fabric_make_nodeacl,
  462. .drop_item = target_fabric_drop_nodeacl,
  463. };
  464. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  465. /* End of tfc_tpg_nacl_cit */
  466. /* Start of tfc_tpg_np_base_cit */
  467. CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
  468. static void target_fabric_np_base_release(struct config_item *item)
  469. {
  470. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  471. struct se_tpg_np, tpg_np_group);
  472. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  473. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  474. tf->tf_ops.fabric_drop_np(se_tpg_np);
  475. }
  476. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  477. .release = target_fabric_np_base_release,
  478. .show_attribute = target_fabric_np_base_attr_show,
  479. .store_attribute = target_fabric_np_base_attr_store,
  480. };
  481. TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
  482. /* End of tfc_tpg_np_base_cit */
  483. /* Start of tfc_tpg_np_cit */
  484. static struct config_group *target_fabric_make_np(
  485. struct config_group *group,
  486. const char *name)
  487. {
  488. struct se_portal_group *se_tpg = container_of(group,
  489. struct se_portal_group, tpg_np_group);
  490. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  491. struct se_tpg_np *se_tpg_np;
  492. if (!tf->tf_ops.fabric_make_np) {
  493. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  494. return ERR_PTR(-ENOSYS);
  495. }
  496. se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
  497. if (!se_tpg_np || IS_ERR(se_tpg_np))
  498. return ERR_PTR(-EINVAL);
  499. se_tpg_np->tpg_np_parent = se_tpg;
  500. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  501. &tf->tf_cit_tmpl.tfc_tpg_np_base_cit);
  502. return &se_tpg_np->tpg_np_group;
  503. }
  504. static void target_fabric_drop_np(
  505. struct config_group *group,
  506. struct config_item *item)
  507. {
  508. /*
  509. * struct se_tpg_np is released via target_fabric_np_base_release()
  510. */
  511. config_item_put(item);
  512. }
  513. static struct configfs_group_operations target_fabric_np_group_ops = {
  514. .make_group = &target_fabric_make_np,
  515. .drop_item = &target_fabric_drop_np,
  516. };
  517. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  518. /* End of tfc_tpg_np_cit */
  519. /* Start of tfc_tpg_port_cit */
  520. CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
  521. #define TCM_PORT_ATTR(_name, _mode) \
  522. static struct target_fabric_port_attribute target_fabric_port_##_name = \
  523. __CONFIGFS_EATTR(_name, _mode, \
  524. target_fabric_port_show_attr_##_name, \
  525. target_fabric_port_store_attr_##_name);
  526. #define TCM_PORT_ATTOR_RO(_name) \
  527. __CONFIGFS_EATTR_RO(_name, \
  528. target_fabric_port_show_attr_##_name);
  529. /*
  530. * alua_tg_pt_gp
  531. */
  532. static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
  533. struct se_lun *lun,
  534. char *page)
  535. {
  536. if (!lun || !lun->lun_sep)
  537. return -ENODEV;
  538. return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
  539. }
  540. static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
  541. struct se_lun *lun,
  542. const char *page,
  543. size_t count)
  544. {
  545. if (!lun || !lun->lun_sep)
  546. return -ENODEV;
  547. return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
  548. }
  549. TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
  550. /*
  551. * alua_tg_pt_offline
  552. */
  553. static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
  554. struct se_lun *lun,
  555. char *page)
  556. {
  557. if (!lun || !lun->lun_sep)
  558. return -ENODEV;
  559. return core_alua_show_offline_bit(lun, page);
  560. }
  561. static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
  562. struct se_lun *lun,
  563. const char *page,
  564. size_t count)
  565. {
  566. if (!lun || !lun->lun_sep)
  567. return -ENODEV;
  568. return core_alua_store_offline_bit(lun, page, count);
  569. }
  570. TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
  571. /*
  572. * alua_tg_pt_status
  573. */
  574. static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
  575. struct se_lun *lun,
  576. char *page)
  577. {
  578. if (!lun || !lun->lun_sep)
  579. return -ENODEV;
  580. return core_alua_show_secondary_status(lun, page);
  581. }
  582. static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
  583. struct se_lun *lun,
  584. const char *page,
  585. size_t count)
  586. {
  587. if (!lun || !lun->lun_sep)
  588. return -ENODEV;
  589. return core_alua_store_secondary_status(lun, page, count);
  590. }
  591. TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
  592. /*
  593. * alua_tg_pt_write_md
  594. */
  595. static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
  596. struct se_lun *lun,
  597. char *page)
  598. {
  599. if (!lun || !lun->lun_sep)
  600. return -ENODEV;
  601. return core_alua_show_secondary_write_metadata(lun, page);
  602. }
  603. static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
  604. struct se_lun *lun,
  605. const char *page,
  606. size_t count)
  607. {
  608. if (!lun || !lun->lun_sep)
  609. return -ENODEV;
  610. return core_alua_store_secondary_write_metadata(lun, page, count);
  611. }
  612. TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
  613. static struct configfs_attribute *target_fabric_port_attrs[] = {
  614. &target_fabric_port_alua_tg_pt_gp.attr,
  615. &target_fabric_port_alua_tg_pt_offline.attr,
  616. &target_fabric_port_alua_tg_pt_status.attr,
  617. &target_fabric_port_alua_tg_pt_write_md.attr,
  618. NULL,
  619. };
  620. CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
  621. static int target_fabric_port_link(
  622. struct config_item *lun_ci,
  623. struct config_item *se_dev_ci)
  624. {
  625. struct config_item *tpg_ci;
  626. struct se_lun *lun = container_of(to_config_group(lun_ci),
  627. struct se_lun, lun_group);
  628. struct se_lun *lun_p;
  629. struct se_portal_group *se_tpg;
  630. struct se_device *dev =
  631. container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  632. struct target_fabric_configfs *tf;
  633. int ret;
  634. if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
  635. pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
  636. " %p to struct se_device: %p\n", se_dev_ci, dev);
  637. return -EFAULT;
  638. }
  639. if (!(dev->dev_flags & DF_CONFIGURED)) {
  640. pr_err("se_device not configured yet, cannot port link\n");
  641. return -ENODEV;
  642. }
  643. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  644. se_tpg = container_of(to_config_group(tpg_ci),
  645. struct se_portal_group, tpg_group);
  646. tf = se_tpg->se_tpg_wwn->wwn_tf;
  647. if (lun->lun_se_dev != NULL) {
  648. pr_err("Port Symlink already exists\n");
  649. return -EEXIST;
  650. }
  651. lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
  652. if (IS_ERR(lun_p)) {
  653. pr_err("core_dev_add_lun() failed\n");
  654. ret = PTR_ERR(lun_p);
  655. goto out;
  656. }
  657. if (tf->tf_ops.fabric_post_link) {
  658. /*
  659. * Call the optional fabric_post_link() to allow a
  660. * fabric module to setup any additional state once
  661. * core_dev_add_lun() has been called..
  662. */
  663. tf->tf_ops.fabric_post_link(se_tpg, lun);
  664. }
  665. return 0;
  666. out:
  667. return ret;
  668. }
  669. static int target_fabric_port_unlink(
  670. struct config_item *lun_ci,
  671. struct config_item *se_dev_ci)
  672. {
  673. struct se_lun *lun = container_of(to_config_group(lun_ci),
  674. struct se_lun, lun_group);
  675. struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
  676. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  677. if (tf->tf_ops.fabric_pre_unlink) {
  678. /*
  679. * Call the optional fabric_pre_unlink() to allow a
  680. * fabric module to release any additional stat before
  681. * core_dev_del_lun() is called.
  682. */
  683. tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
  684. }
  685. core_dev_del_lun(se_tpg, lun->unpacked_lun);
  686. return 0;
  687. }
  688. static struct configfs_item_operations target_fabric_port_item_ops = {
  689. .show_attribute = target_fabric_port_attr_show,
  690. .store_attribute = target_fabric_port_attr_store,
  691. .allow_link = target_fabric_port_link,
  692. .drop_link = target_fabric_port_unlink,
  693. };
  694. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  695. /* End of tfc_tpg_port_cit */
  696. /* Start of tfc_tpg_port_stat_cit */
  697. static struct config_group *target_core_port_stat_mkdir(
  698. struct config_group *group,
  699. const char *name)
  700. {
  701. return ERR_PTR(-ENOSYS);
  702. }
  703. static void target_core_port_stat_rmdir(
  704. struct config_group *group,
  705. struct config_item *item)
  706. {
  707. return;
  708. }
  709. static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  710. .make_group = target_core_port_stat_mkdir,
  711. .drop_item = target_core_port_stat_rmdir,
  712. };
  713. TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  714. /* End of tfc_tpg_port_stat_cit */
  715. /* Start of tfc_tpg_lun_cit */
  716. static struct config_group *target_fabric_make_lun(
  717. struct config_group *group,
  718. const char *name)
  719. {
  720. struct se_lun *lun;
  721. struct se_portal_group *se_tpg = container_of(group,
  722. struct se_portal_group, tpg_lun_group);
  723. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  724. struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
  725. unsigned long unpacked_lun;
  726. int errno;
  727. if (strstr(name, "lun_") != name) {
  728. pr_err("Unable to locate \'_\" in"
  729. " \"lun_$LUN_NUMBER\"\n");
  730. return ERR_PTR(-EINVAL);
  731. }
  732. errno = kstrtoul(name + 4, 0, &unpacked_lun);
  733. if (errno)
  734. return ERR_PTR(errno);
  735. if (unpacked_lun > UINT_MAX)
  736. return ERR_PTR(-EINVAL);
  737. lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
  738. if (!lun)
  739. return ERR_PTR(-EINVAL);
  740. lun_cg = &lun->lun_group;
  741. lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  742. GFP_KERNEL);
  743. if (!lun_cg->default_groups) {
  744. pr_err("Unable to allocate lun_cg->default_groups\n");
  745. return ERR_PTR(-ENOMEM);
  746. }
  747. config_group_init_type_name(&lun->lun_group, name,
  748. &tf->tf_cit_tmpl.tfc_tpg_port_cit);
  749. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  750. "statistics", &tf->tf_cit_tmpl.tfc_tpg_port_stat_cit);
  751. lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
  752. lun_cg->default_groups[1] = NULL;
  753. port_stat_grp = &lun->port_stat_grps.stat_group;
  754. port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
  755. GFP_KERNEL);
  756. if (!port_stat_grp->default_groups) {
  757. pr_err("Unable to allocate port_stat_grp->default_groups\n");
  758. kfree(lun_cg->default_groups);
  759. return ERR_PTR(-ENOMEM);
  760. }
  761. target_stat_setup_port_default_groups(lun);
  762. return &lun->lun_group;
  763. }
  764. static void target_fabric_drop_lun(
  765. struct config_group *group,
  766. struct config_item *item)
  767. {
  768. struct se_lun *lun = container_of(to_config_group(item),
  769. struct se_lun, lun_group);
  770. struct config_item *df_item;
  771. struct config_group *lun_cg, *port_stat_grp;
  772. int i;
  773. port_stat_grp = &lun->port_stat_grps.stat_group;
  774. for (i = 0; port_stat_grp->default_groups[i]; i++) {
  775. df_item = &port_stat_grp->default_groups[i]->cg_item;
  776. port_stat_grp->default_groups[i] = NULL;
  777. config_item_put(df_item);
  778. }
  779. kfree(port_stat_grp->default_groups);
  780. lun_cg = &lun->lun_group;
  781. for (i = 0; lun_cg->default_groups[i]; i++) {
  782. df_item = &lun_cg->default_groups[i]->cg_item;
  783. lun_cg->default_groups[i] = NULL;
  784. config_item_put(df_item);
  785. }
  786. kfree(lun_cg->default_groups);
  787. config_item_put(item);
  788. }
  789. static struct configfs_group_operations target_fabric_lun_group_ops = {
  790. .make_group = &target_fabric_make_lun,
  791. .drop_item = &target_fabric_drop_lun,
  792. };
  793. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  794. /* End of tfc_tpg_lun_cit */
  795. /* Start of tfc_tpg_attrib_cit */
  796. CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
  797. static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
  798. .show_attribute = target_fabric_tpg_attrib_attr_show,
  799. .store_attribute = target_fabric_tpg_attrib_attr_store,
  800. };
  801. TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
  802. /* End of tfc_tpg_attrib_cit */
  803. /* Start of tfc_tpg_auth_cit */
  804. CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
  805. static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
  806. .show_attribute = target_fabric_tpg_auth_attr_show,
  807. .store_attribute = target_fabric_tpg_auth_attr_store,
  808. };
  809. TF_CIT_SETUP(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL, NULL);
  810. /* End of tfc_tpg_attrib_cit */
  811. /* Start of tfc_tpg_param_cit */
  812. CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
  813. static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
  814. .show_attribute = target_fabric_tpg_param_attr_show,
  815. .store_attribute = target_fabric_tpg_param_attr_store,
  816. };
  817. TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
  818. /* End of tfc_tpg_param_cit */
  819. /* Start of tfc_tpg_base_cit */
  820. /*
  821. * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
  822. */
  823. CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
  824. static void target_fabric_tpg_release(struct config_item *item)
  825. {
  826. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  827. struct se_portal_group, tpg_group);
  828. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  829. struct target_fabric_configfs *tf = wwn->wwn_tf;
  830. tf->tf_ops.fabric_drop_tpg(se_tpg);
  831. }
  832. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  833. .release = target_fabric_tpg_release,
  834. .show_attribute = target_fabric_tpg_attr_show,
  835. .store_attribute = target_fabric_tpg_attr_store,
  836. };
  837. TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
  838. /* End of tfc_tpg_base_cit */
  839. /* Start of tfc_tpg_cit */
  840. static struct config_group *target_fabric_make_tpg(
  841. struct config_group *group,
  842. const char *name)
  843. {
  844. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  845. struct target_fabric_configfs *tf = wwn->wwn_tf;
  846. struct se_portal_group *se_tpg;
  847. if (!tf->tf_ops.fabric_make_tpg) {
  848. pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
  849. return ERR_PTR(-ENOSYS);
  850. }
  851. se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
  852. if (!se_tpg || IS_ERR(se_tpg))
  853. return ERR_PTR(-EINVAL);
  854. /*
  855. * Setup default groups from pre-allocated se_tpg->tpg_default_groups
  856. */
  857. se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
  858. se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
  859. se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
  860. se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
  861. se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
  862. se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
  863. se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
  864. se_tpg->tpg_group.default_groups[6] = NULL;
  865. config_group_init_type_name(&se_tpg->tpg_group, name,
  866. &tf->tf_cit_tmpl.tfc_tpg_base_cit);
  867. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  868. &tf->tf_cit_tmpl.tfc_tpg_lun_cit);
  869. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  870. &tf->tf_cit_tmpl.tfc_tpg_np_cit);
  871. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  872. &tf->tf_cit_tmpl.tfc_tpg_nacl_cit);
  873. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  874. &tf->tf_cit_tmpl.tfc_tpg_attrib_cit);
  875. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  876. &tf->tf_cit_tmpl.tfc_tpg_auth_cit);
  877. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  878. &tf->tf_cit_tmpl.tfc_tpg_param_cit);
  879. return &se_tpg->tpg_group;
  880. }
  881. static void target_fabric_drop_tpg(
  882. struct config_group *group,
  883. struct config_item *item)
  884. {
  885. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  886. struct se_portal_group, tpg_group);
  887. struct config_group *tpg_cg = &se_tpg->tpg_group;
  888. struct config_item *df_item;
  889. int i;
  890. /*
  891. * Release default groups, but do not release tpg_cg->default_groups
  892. * memory as it is statically allocated at se_tpg->tpg_default_groups.
  893. */
  894. for (i = 0; tpg_cg->default_groups[i]; i++) {
  895. df_item = &tpg_cg->default_groups[i]->cg_item;
  896. tpg_cg->default_groups[i] = NULL;
  897. config_item_put(df_item);
  898. }
  899. config_item_put(item);
  900. }
  901. static void target_fabric_release_wwn(struct config_item *item)
  902. {
  903. struct se_wwn *wwn = container_of(to_config_group(item),
  904. struct se_wwn, wwn_group);
  905. struct target_fabric_configfs *tf = wwn->wwn_tf;
  906. tf->tf_ops.fabric_drop_wwn(wwn);
  907. }
  908. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  909. .release = target_fabric_release_wwn,
  910. };
  911. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  912. .make_group = target_fabric_make_tpg,
  913. .drop_item = target_fabric_drop_tpg,
  914. };
  915. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  916. NULL);
  917. /* End of tfc_tpg_cit */
  918. /* Start of tfc_wwn_fabric_stats_cit */
  919. /*
  920. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  921. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  922. */
  923. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  924. /* End of tfc_wwn_fabric_stats_cit */
  925. /* Start of tfc_wwn_cit */
  926. static struct config_group *target_fabric_make_wwn(
  927. struct config_group *group,
  928. const char *name)
  929. {
  930. struct target_fabric_configfs *tf = container_of(group,
  931. struct target_fabric_configfs, tf_group);
  932. struct se_wwn *wwn;
  933. if (!tf->tf_ops.fabric_make_wwn) {
  934. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  935. return ERR_PTR(-ENOSYS);
  936. }
  937. wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
  938. if (!wwn || IS_ERR(wwn))
  939. return ERR_PTR(-EINVAL);
  940. wwn->wwn_tf = tf;
  941. /*
  942. * Setup default groups from pre-allocated wwn->wwn_default_groups
  943. */
  944. wwn->wwn_group.default_groups = wwn->wwn_default_groups;
  945. wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
  946. wwn->wwn_group.default_groups[1] = NULL;
  947. config_group_init_type_name(&wwn->wwn_group, name,
  948. &tf->tf_cit_tmpl.tfc_tpg_cit);
  949. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  950. &tf->tf_cit_tmpl.tfc_wwn_fabric_stats_cit);
  951. return &wwn->wwn_group;
  952. }
  953. static void target_fabric_drop_wwn(
  954. struct config_group *group,
  955. struct config_item *item)
  956. {
  957. struct se_wwn *wwn = container_of(to_config_group(item),
  958. struct se_wwn, wwn_group);
  959. struct config_item *df_item;
  960. struct config_group *cg = &wwn->wwn_group;
  961. int i;
  962. for (i = 0; cg->default_groups[i]; i++) {
  963. df_item = &cg->default_groups[i]->cg_item;
  964. cg->default_groups[i] = NULL;
  965. config_item_put(df_item);
  966. }
  967. config_item_put(item);
  968. }
  969. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  970. .make_group = target_fabric_make_wwn,
  971. .drop_item = target_fabric_drop_wwn,
  972. };
  973. /*
  974. * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
  975. */
  976. CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
  977. static struct configfs_item_operations target_fabric_wwn_item_ops = {
  978. .show_attribute = target_fabric_wwn_attr_show,
  979. .store_attribute = target_fabric_wwn_attr_store,
  980. };
  981. TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
  982. /* End of tfc_wwn_cit */
  983. /* Start of tfc_discovery_cit */
  984. CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
  985. tf_disc_group);
  986. static struct configfs_item_operations target_fabric_discovery_item_ops = {
  987. .show_attribute = target_fabric_discovery_attr_show,
  988. .store_attribute = target_fabric_discovery_attr_store,
  989. };
  990. TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
  991. /* End of tfc_discovery_cit */
  992. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  993. {
  994. target_fabric_setup_discovery_cit(tf);
  995. target_fabric_setup_wwn_cit(tf);
  996. target_fabric_setup_wwn_fabric_stats_cit(tf);
  997. target_fabric_setup_tpg_cit(tf);
  998. target_fabric_setup_tpg_base_cit(tf);
  999. target_fabric_setup_tpg_port_cit(tf);
  1000. target_fabric_setup_tpg_port_stat_cit(tf);
  1001. target_fabric_setup_tpg_lun_cit(tf);
  1002. target_fabric_setup_tpg_np_cit(tf);
  1003. target_fabric_setup_tpg_np_base_cit(tf);
  1004. target_fabric_setup_tpg_attrib_cit(tf);
  1005. target_fabric_setup_tpg_auth_cit(tf);
  1006. target_fabric_setup_tpg_param_cit(tf);
  1007. target_fabric_setup_tpg_nacl_cit(tf);
  1008. target_fabric_setup_tpg_nacl_base_cit(tf);
  1009. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  1010. target_fabric_setup_tpg_nacl_auth_cit(tf);
  1011. target_fabric_setup_tpg_nacl_param_cit(tf);
  1012. target_fabric_setup_tpg_nacl_stat_cit(tf);
  1013. target_fabric_setup_tpg_mappedlun_cit(tf);
  1014. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  1015. return 0;
  1016. }