target_core_fabric_configfs.c 36 KB

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