target_core_fabric_configfs.c 35 KB

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