target_core_fabric_configfs.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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/configfs_macros.h>
  38. #include "target_core_internal.h"
  39. #include "target_core_alua.h"
  40. #include "target_core_pr.h"
  41. #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
  42. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  43. { \
  44. struct config_item_type *cit = &tf->tf_##_name##_cit; \
  45. \
  46. cit->ct_item_ops = _item_ops; \
  47. cit->ct_group_ops = _group_ops; \
  48. cit->ct_attrs = _attrs; \
  49. cit->ct_owner = tf->tf_ops->module; \
  50. pr_debug("Setup generic %s\n", __stringify(_name)); \
  51. }
  52. #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
  53. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  54. { \
  55. struct config_item_type *cit = &tf->tf_##_name##_cit; \
  56. struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
  57. \
  58. cit->ct_item_ops = _item_ops; \
  59. cit->ct_group_ops = _group_ops; \
  60. cit->ct_attrs = attrs; \
  61. cit->ct_owner = tf->tf_ops->module; \
  62. pr_debug("Setup generic %s\n", __stringify(_name)); \
  63. }
  64. /* Start of tfc_tpg_mappedlun_cit */
  65. static int target_fabric_mappedlun_link(
  66. struct config_item *lun_acl_ci,
  67. struct config_item *lun_ci)
  68. {
  69. struct se_dev_entry *deve;
  70. struct se_lun *lun = container_of(to_config_group(lun_ci),
  71. struct se_lun, lun_group);
  72. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  73. struct se_lun_acl, se_lun_group);
  74. struct se_portal_group *se_tpg;
  75. struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
  76. int lun_access;
  77. if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
  78. pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
  79. " %p to struct lun: %p\n", lun_ci, lun);
  80. return -EFAULT;
  81. }
  82. /*
  83. * Ensure that the source port exists
  84. */
  85. if (!lun->lun_se_dev) {
  86. pr_err("Source se_lun->lun_se_dev does not exist\n");
  87. return -EINVAL;
  88. }
  89. se_tpg = lun->lun_tpg;
  90. nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
  91. tpg_ci = &nacl_ci->ci_group->cg_item;
  92. wwn_ci = &tpg_ci->ci_group->cg_item;
  93. tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
  94. wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
  95. /*
  96. * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
  97. */
  98. if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
  99. pr_err("Illegal Initiator ACL SymLink outside of %s\n",
  100. config_item_name(wwn_ci));
  101. return -EINVAL;
  102. }
  103. if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
  104. pr_err("Illegal Initiator ACL Symlink outside of %s"
  105. " TPGT: %s\n", config_item_name(wwn_ci),
  106. config_item_name(tpg_ci));
  107. return -EINVAL;
  108. }
  109. /*
  110. * If this struct se_node_acl was dynamically generated with
  111. * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
  112. * which be will write protected (READ-ONLY) when
  113. * tpg_1/attrib/demo_mode_write_protect=1
  114. */
  115. rcu_read_lock();
  116. deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
  117. if (deve)
  118. lun_access = deve->lun_flags;
  119. else
  120. lun_access =
  121. (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
  122. se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
  123. TRANSPORT_LUNFLAGS_READ_WRITE;
  124. rcu_read_unlock();
  125. /*
  126. * Determine the actual mapped LUN value user wants..
  127. *
  128. * This value is what the SCSI Initiator actually sees the
  129. * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
  130. */
  131. return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
  132. }
  133. static int target_fabric_mappedlun_unlink(
  134. struct config_item *lun_acl_ci,
  135. struct config_item *lun_ci)
  136. {
  137. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  138. struct se_lun_acl, se_lun_group);
  139. struct se_lun *lun = container_of(to_config_group(lun_ci),
  140. struct se_lun, lun_group);
  141. return core_dev_del_initiator_node_lun_acl(lun, lacl);
  142. }
  143. CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
  144. #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
  145. static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
  146. __CONFIGFS_EATTR(_name, _mode, \
  147. target_fabric_mappedlun_show_##_name, \
  148. target_fabric_mappedlun_store_##_name);
  149. static ssize_t target_fabric_mappedlun_show_write_protect(
  150. struct se_lun_acl *lacl,
  151. char *page)
  152. {
  153. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  154. struct se_dev_entry *deve;
  155. ssize_t len = 0;
  156. rcu_read_lock();
  157. deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
  158. if (deve) {
  159. len = sprintf(page, "%d\n",
  160. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
  161. }
  162. rcu_read_unlock();
  163. return len;
  164. }
  165. static ssize_t target_fabric_mappedlun_store_write_protect(
  166. struct se_lun_acl *lacl,
  167. const char *page,
  168. size_t count)
  169. {
  170. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  171. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  172. unsigned long op;
  173. int ret;
  174. ret = kstrtoul(page, 0, &op);
  175. if (ret)
  176. return ret;
  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: %llu 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_DRV(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, 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_DRV(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, 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_DRV(tpg_nacl_param, &target_fabric_nacl_param_item_ops, 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 = NULL;
  267. struct config_item *acl_ci;
  268. struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  269. char *buf;
  270. unsigned long 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. ret = kstrtoull(buf + 4, 0, &mapped_lun);
  297. if (ret)
  298. goto out;
  299. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
  300. mapped_lun, &ret);
  301. if (!lacl) {
  302. ret = -EINVAL;
  303. goto out;
  304. }
  305. lacl_cg = &lacl->se_lun_group;
  306. lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  307. GFP_KERNEL);
  308. if (!lacl_cg->default_groups) {
  309. pr_err("Unable to allocate lacl_cg->default_groups\n");
  310. ret = -ENOMEM;
  311. goto out;
  312. }
  313. config_group_init_type_name(&lacl->se_lun_group, name,
  314. &tf->tf_tpg_mappedlun_cit);
  315. config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  316. "statistics", &tf->tf_tpg_mappedlun_stat_cit);
  317. lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
  318. lacl_cg->default_groups[1] = NULL;
  319. ml_stat_grp = &lacl->ml_stat_grps.stat_group;
  320. ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
  321. GFP_KERNEL);
  322. if (!ml_stat_grp->default_groups) {
  323. pr_err("Unable to allocate ml_stat_grp->default_groups\n");
  324. ret = -ENOMEM;
  325. goto out;
  326. }
  327. target_stat_setup_mappedlun_default_groups(lacl);
  328. kfree(buf);
  329. return &lacl->se_lun_group;
  330. out:
  331. if (lacl_cg)
  332. kfree(lacl_cg->default_groups);
  333. kfree(lacl);
  334. kfree(buf);
  335. return ERR_PTR(ret);
  336. }
  337. static void target_fabric_drop_mappedlun(
  338. struct config_group *group,
  339. struct config_item *item)
  340. {
  341. struct se_lun_acl *lacl = container_of(to_config_group(item),
  342. struct se_lun_acl, se_lun_group);
  343. struct config_item *df_item;
  344. struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  345. int i;
  346. ml_stat_grp = &lacl->ml_stat_grps.stat_group;
  347. for (i = 0; ml_stat_grp->default_groups[i]; i++) {
  348. df_item = &ml_stat_grp->default_groups[i]->cg_item;
  349. ml_stat_grp->default_groups[i] = NULL;
  350. config_item_put(df_item);
  351. }
  352. kfree(ml_stat_grp->default_groups);
  353. lacl_cg = &lacl->se_lun_group;
  354. for (i = 0; lacl_cg->default_groups[i]; i++) {
  355. df_item = &lacl_cg->default_groups[i]->cg_item;
  356. lacl_cg->default_groups[i] = NULL;
  357. config_item_put(df_item);
  358. }
  359. kfree(lacl_cg->default_groups);
  360. config_item_put(item);
  361. }
  362. static void target_fabric_nacl_base_release(struct config_item *item)
  363. {
  364. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  365. struct se_node_acl, acl_group);
  366. struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
  367. if (tf->tf_ops->fabric_cleanup_nodeacl)
  368. tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
  369. core_tpg_del_initiator_node_acl(se_nacl);
  370. }
  371. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  372. .release = target_fabric_nacl_base_release,
  373. .show_attribute = target_fabric_nacl_base_attr_show,
  374. .store_attribute = target_fabric_nacl_base_attr_store,
  375. };
  376. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  377. .make_group = target_fabric_make_mappedlun,
  378. .drop_item = target_fabric_drop_mappedlun,
  379. };
  380. TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  381. &target_fabric_nacl_base_group_ops);
  382. /* End of tfc_tpg_nacl_base_cit */
  383. /* Start of tfc_node_fabric_stats_cit */
  384. /*
  385. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  386. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  387. */
  388. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  389. /* End of tfc_wwn_fabric_stats_cit */
  390. /* Start of tfc_tpg_nacl_cit */
  391. static struct config_group *target_fabric_make_nodeacl(
  392. struct config_group *group,
  393. const char *name)
  394. {
  395. struct se_portal_group *se_tpg = container_of(group,
  396. struct se_portal_group, tpg_acl_group);
  397. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  398. struct se_node_acl *se_nacl;
  399. struct config_group *nacl_cg;
  400. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
  401. if (IS_ERR(se_nacl))
  402. return ERR_CAST(se_nacl);
  403. if (tf->tf_ops->fabric_init_nodeacl) {
  404. int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
  405. if (ret) {
  406. core_tpg_del_initiator_node_acl(se_nacl);
  407. return ERR_PTR(ret);
  408. }
  409. }
  410. nacl_cg = &se_nacl->acl_group;
  411. nacl_cg->default_groups = se_nacl->acl_default_groups;
  412. nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
  413. nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
  414. nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
  415. nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
  416. nacl_cg->default_groups[4] = NULL;
  417. config_group_init_type_name(&se_nacl->acl_group, name,
  418. &tf->tf_tpg_nacl_base_cit);
  419. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  420. &tf->tf_tpg_nacl_attrib_cit);
  421. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  422. &tf->tf_tpg_nacl_auth_cit);
  423. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  424. &tf->tf_tpg_nacl_param_cit);
  425. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  426. "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
  427. return &se_nacl->acl_group;
  428. }
  429. static void target_fabric_drop_nodeacl(
  430. struct config_group *group,
  431. struct config_item *item)
  432. {
  433. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  434. struct se_node_acl, acl_group);
  435. struct config_item *df_item;
  436. struct config_group *nacl_cg;
  437. int i;
  438. nacl_cg = &se_nacl->acl_group;
  439. for (i = 0; nacl_cg->default_groups[i]; i++) {
  440. df_item = &nacl_cg->default_groups[i]->cg_item;
  441. nacl_cg->default_groups[i] = NULL;
  442. config_item_put(df_item);
  443. }
  444. /*
  445. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  446. */
  447. config_item_put(item);
  448. }
  449. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  450. .make_group = target_fabric_make_nodeacl,
  451. .drop_item = target_fabric_drop_nodeacl,
  452. };
  453. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  454. /* End of tfc_tpg_nacl_cit */
  455. /* Start of tfc_tpg_np_base_cit */
  456. CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
  457. static void target_fabric_np_base_release(struct config_item *item)
  458. {
  459. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  460. struct se_tpg_np, tpg_np_group);
  461. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  462. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  463. tf->tf_ops->fabric_drop_np(se_tpg_np);
  464. }
  465. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  466. .release = target_fabric_np_base_release,
  467. .show_attribute = target_fabric_np_base_attr_show,
  468. .store_attribute = target_fabric_np_base_attr_store,
  469. };
  470. TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
  471. /* End of tfc_tpg_np_base_cit */
  472. /* Start of tfc_tpg_np_cit */
  473. static struct config_group *target_fabric_make_np(
  474. struct config_group *group,
  475. const char *name)
  476. {
  477. struct se_portal_group *se_tpg = container_of(group,
  478. struct se_portal_group, tpg_np_group);
  479. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  480. struct se_tpg_np *se_tpg_np;
  481. if (!tf->tf_ops->fabric_make_np) {
  482. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  483. return ERR_PTR(-ENOSYS);
  484. }
  485. se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
  486. if (!se_tpg_np || IS_ERR(se_tpg_np))
  487. return ERR_PTR(-EINVAL);
  488. se_tpg_np->tpg_np_parent = se_tpg;
  489. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  490. &tf->tf_tpg_np_base_cit);
  491. return &se_tpg_np->tpg_np_group;
  492. }
  493. static void target_fabric_drop_np(
  494. struct config_group *group,
  495. struct config_item *item)
  496. {
  497. /*
  498. * struct se_tpg_np is released via target_fabric_np_base_release()
  499. */
  500. config_item_put(item);
  501. }
  502. static struct configfs_group_operations target_fabric_np_group_ops = {
  503. .make_group = &target_fabric_make_np,
  504. .drop_item = &target_fabric_drop_np,
  505. };
  506. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  507. /* End of tfc_tpg_np_cit */
  508. /* Start of tfc_tpg_port_cit */
  509. CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
  510. #define TCM_PORT_ATTR(_name, _mode) \
  511. static struct target_fabric_port_attribute target_fabric_port_##_name = \
  512. __CONFIGFS_EATTR(_name, _mode, \
  513. target_fabric_port_show_attr_##_name, \
  514. target_fabric_port_store_attr_##_name);
  515. #define TCM_PORT_ATTOR_RO(_name) \
  516. __CONFIGFS_EATTR_RO(_name, \
  517. target_fabric_port_show_attr_##_name);
  518. /*
  519. * alua_tg_pt_gp
  520. */
  521. static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
  522. struct se_lun *lun,
  523. char *page)
  524. {
  525. if (!lun || !lun->lun_se_dev)
  526. return -ENODEV;
  527. return core_alua_show_tg_pt_gp_info(lun, page);
  528. }
  529. static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
  530. struct se_lun *lun,
  531. const char *page,
  532. size_t count)
  533. {
  534. if (!lun || !lun->lun_se_dev)
  535. return -ENODEV;
  536. return core_alua_store_tg_pt_gp_info(lun, page, count);
  537. }
  538. TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
  539. /*
  540. * alua_tg_pt_offline
  541. */
  542. static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
  543. struct se_lun *lun,
  544. char *page)
  545. {
  546. if (!lun || !lun->lun_se_dev)
  547. return -ENODEV;
  548. return core_alua_show_offline_bit(lun, page);
  549. }
  550. static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
  551. struct se_lun *lun,
  552. const char *page,
  553. size_t count)
  554. {
  555. if (!lun || !lun->lun_se_dev)
  556. return -ENODEV;
  557. return core_alua_store_offline_bit(lun, page, count);
  558. }
  559. TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
  560. /*
  561. * alua_tg_pt_status
  562. */
  563. static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
  564. struct se_lun *lun,
  565. char *page)
  566. {
  567. if (!lun || !lun->lun_se_dev)
  568. return -ENODEV;
  569. return core_alua_show_secondary_status(lun, page);
  570. }
  571. static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
  572. struct se_lun *lun,
  573. const char *page,
  574. size_t count)
  575. {
  576. if (!lun || !lun->lun_se_dev)
  577. return -ENODEV;
  578. return core_alua_store_secondary_status(lun, page, count);
  579. }
  580. TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
  581. /*
  582. * alua_tg_pt_write_md
  583. */
  584. static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
  585. struct se_lun *lun,
  586. char *page)
  587. {
  588. if (!lun || !lun->lun_se_dev)
  589. return -ENODEV;
  590. return core_alua_show_secondary_write_metadata(lun, page);
  591. }
  592. static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
  593. struct se_lun *lun,
  594. const char *page,
  595. size_t count)
  596. {
  597. if (!lun || !lun->lun_se_dev)
  598. return -ENODEV;
  599. return core_alua_store_secondary_write_metadata(lun, page, count);
  600. }
  601. TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
  602. static struct configfs_attribute *target_fabric_port_attrs[] = {
  603. &target_fabric_port_alua_tg_pt_gp.attr,
  604. &target_fabric_port_alua_tg_pt_offline.attr,
  605. &target_fabric_port_alua_tg_pt_status.attr,
  606. &target_fabric_port_alua_tg_pt_write_md.attr,
  607. NULL,
  608. };
  609. CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
  610. static int target_fabric_port_link(
  611. struct config_item *lun_ci,
  612. struct config_item *se_dev_ci)
  613. {
  614. struct config_item *tpg_ci;
  615. struct se_lun *lun = container_of(to_config_group(lun_ci),
  616. struct se_lun, lun_group);
  617. struct se_portal_group *se_tpg;
  618. struct se_device *dev =
  619. container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  620. struct target_fabric_configfs *tf;
  621. int ret;
  622. if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
  623. pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
  624. " %p to struct se_device: %p\n", se_dev_ci, dev);
  625. return -EFAULT;
  626. }
  627. if (!(dev->dev_flags & DF_CONFIGURED)) {
  628. pr_err("se_device not configured yet, cannot port link\n");
  629. return -ENODEV;
  630. }
  631. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  632. se_tpg = container_of(to_config_group(tpg_ci),
  633. struct se_portal_group, tpg_group);
  634. tf = se_tpg->se_tpg_wwn->wwn_tf;
  635. if (lun->lun_se_dev != NULL) {
  636. pr_err("Port Symlink already exists\n");
  637. return -EEXIST;
  638. }
  639. ret = core_dev_add_lun(se_tpg, dev, lun);
  640. if (ret) {
  641. pr_err("core_dev_add_lun() failed: %d\n", ret);
  642. goto out;
  643. }
  644. if (tf->tf_ops->fabric_post_link) {
  645. /*
  646. * Call the optional fabric_post_link() to allow a
  647. * fabric module to setup any additional state once
  648. * core_dev_add_lun() has been called..
  649. */
  650. tf->tf_ops->fabric_post_link(se_tpg, lun);
  651. }
  652. return 0;
  653. out:
  654. return ret;
  655. }
  656. static int target_fabric_port_unlink(
  657. struct config_item *lun_ci,
  658. struct config_item *se_dev_ci)
  659. {
  660. struct se_lun *lun = container_of(to_config_group(lun_ci),
  661. struct se_lun, lun_group);
  662. struct se_portal_group *se_tpg = lun->lun_tpg;
  663. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  664. if (tf->tf_ops->fabric_pre_unlink) {
  665. /*
  666. * Call the optional fabric_pre_unlink() to allow a
  667. * fabric module to release any additional stat before
  668. * core_dev_del_lun() is called.
  669. */
  670. tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
  671. }
  672. core_dev_del_lun(se_tpg, lun);
  673. return 0;
  674. }
  675. static void target_fabric_port_release(struct config_item *item)
  676. {
  677. struct se_lun *lun = container_of(to_config_group(item),
  678. struct se_lun, lun_group);
  679. kfree_rcu(lun, rcu_head);
  680. }
  681. static struct configfs_item_operations target_fabric_port_item_ops = {
  682. .show_attribute = target_fabric_port_attr_show,
  683. .store_attribute = target_fabric_port_attr_store,
  684. .release = target_fabric_port_release,
  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 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. errno = kstrtoull(name + 4, 0, &unpacked_lun);
  727. if (errno)
  728. return ERR_PTR(errno);
  729. lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
  730. if (IS_ERR(lun))
  731. return ERR_CAST(lun);
  732. lun_cg = &lun->lun_group;
  733. lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  734. GFP_KERNEL);
  735. if (!lun_cg->default_groups) {
  736. pr_err("Unable to allocate lun_cg->default_groups\n");
  737. kfree(lun);
  738. return ERR_PTR(-ENOMEM);
  739. }
  740. config_group_init_type_name(&lun->lun_group, name,
  741. &tf->tf_tpg_port_cit);
  742. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  743. "statistics", &tf->tf_tpg_port_stat_cit);
  744. lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
  745. lun_cg->default_groups[1] = NULL;
  746. port_stat_grp = &lun->port_stat_grps.stat_group;
  747. port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
  748. GFP_KERNEL);
  749. if (!port_stat_grp->default_groups) {
  750. pr_err("Unable to allocate port_stat_grp->default_groups\n");
  751. kfree(lun_cg->default_groups);
  752. kfree(lun);
  753. return ERR_PTR(-ENOMEM);
  754. }
  755. target_stat_setup_port_default_groups(lun);
  756. return &lun->lun_group;
  757. }
  758. static void target_fabric_drop_lun(
  759. struct config_group *group,
  760. struct config_item *item)
  761. {
  762. struct se_lun *lun = container_of(to_config_group(item),
  763. struct se_lun, lun_group);
  764. struct config_item *df_item;
  765. struct config_group *lun_cg, *port_stat_grp;
  766. int i;
  767. port_stat_grp = &lun->port_stat_grps.stat_group;
  768. for (i = 0; port_stat_grp->default_groups[i]; i++) {
  769. df_item = &port_stat_grp->default_groups[i]->cg_item;
  770. port_stat_grp->default_groups[i] = NULL;
  771. config_item_put(df_item);
  772. }
  773. kfree(port_stat_grp->default_groups);
  774. lun_cg = &lun->lun_group;
  775. for (i = 0; lun_cg->default_groups[i]; i++) {
  776. df_item = &lun_cg->default_groups[i]->cg_item;
  777. lun_cg->default_groups[i] = NULL;
  778. config_item_put(df_item);
  779. }
  780. kfree(lun_cg->default_groups);
  781. config_item_put(item);
  782. }
  783. static struct configfs_group_operations target_fabric_lun_group_ops = {
  784. .make_group = &target_fabric_make_lun,
  785. .drop_item = &target_fabric_drop_lun,
  786. };
  787. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  788. /* End of tfc_tpg_lun_cit */
  789. /* Start of tfc_tpg_attrib_cit */
  790. CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
  791. static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
  792. .show_attribute = target_fabric_tpg_attrib_attr_show,
  793. .store_attribute = target_fabric_tpg_attrib_attr_store,
  794. };
  795. TF_CIT_SETUP_DRV(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL);
  796. /* End of tfc_tpg_attrib_cit */
  797. /* Start of tfc_tpg_auth_cit */
  798. CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
  799. static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
  800. .show_attribute = target_fabric_tpg_auth_attr_show,
  801. .store_attribute = target_fabric_tpg_auth_attr_store,
  802. };
  803. TF_CIT_SETUP_DRV(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL);
  804. /* End of tfc_tpg_attrib_cit */
  805. /* Start of tfc_tpg_param_cit */
  806. CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
  807. static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
  808. .show_attribute = target_fabric_tpg_param_attr_show,
  809. .store_attribute = target_fabric_tpg_param_attr_store,
  810. };
  811. TF_CIT_SETUP_DRV(tpg_param, &target_fabric_tpg_param_item_ops, NULL);
  812. /* End of tfc_tpg_param_cit */
  813. /* Start of tfc_tpg_base_cit */
  814. /*
  815. * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
  816. */
  817. CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
  818. static void target_fabric_tpg_release(struct config_item *item)
  819. {
  820. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  821. struct se_portal_group, tpg_group);
  822. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  823. struct target_fabric_configfs *tf = wwn->wwn_tf;
  824. tf->tf_ops->fabric_drop_tpg(se_tpg);
  825. }
  826. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  827. .release = target_fabric_tpg_release,
  828. .show_attribute = target_fabric_tpg_attr_show,
  829. .store_attribute = target_fabric_tpg_attr_store,
  830. };
  831. TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
  832. /* End of tfc_tpg_base_cit */
  833. /* Start of tfc_tpg_cit */
  834. static struct config_group *target_fabric_make_tpg(
  835. struct config_group *group,
  836. const char *name)
  837. {
  838. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  839. struct target_fabric_configfs *tf = wwn->wwn_tf;
  840. struct se_portal_group *se_tpg;
  841. if (!tf->tf_ops->fabric_make_tpg) {
  842. pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
  843. return ERR_PTR(-ENOSYS);
  844. }
  845. se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
  846. if (!se_tpg || IS_ERR(se_tpg))
  847. return ERR_PTR(-EINVAL);
  848. /*
  849. * Setup default groups from pre-allocated se_tpg->tpg_default_groups
  850. */
  851. se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
  852. se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
  853. se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
  854. se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
  855. se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
  856. se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
  857. se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
  858. se_tpg->tpg_group.default_groups[6] = NULL;
  859. config_group_init_type_name(&se_tpg->tpg_group, name,
  860. &tf->tf_tpg_base_cit);
  861. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  862. &tf->tf_tpg_lun_cit);
  863. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  864. &tf->tf_tpg_np_cit);
  865. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  866. &tf->tf_tpg_nacl_cit);
  867. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  868. &tf->tf_tpg_attrib_cit);
  869. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  870. &tf->tf_tpg_auth_cit);
  871. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  872. &tf->tf_tpg_param_cit);
  873. return &se_tpg->tpg_group;
  874. }
  875. static void target_fabric_drop_tpg(
  876. struct config_group *group,
  877. struct config_item *item)
  878. {
  879. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  880. struct se_portal_group, tpg_group);
  881. struct config_group *tpg_cg = &se_tpg->tpg_group;
  882. struct config_item *df_item;
  883. int i;
  884. /*
  885. * Release default groups, but do not release tpg_cg->default_groups
  886. * memory as it is statically allocated at se_tpg->tpg_default_groups.
  887. */
  888. for (i = 0; tpg_cg->default_groups[i]; i++) {
  889. df_item = &tpg_cg->default_groups[i]->cg_item;
  890. tpg_cg->default_groups[i] = NULL;
  891. config_item_put(df_item);
  892. }
  893. config_item_put(item);
  894. }
  895. static void target_fabric_release_wwn(struct config_item *item)
  896. {
  897. struct se_wwn *wwn = container_of(to_config_group(item),
  898. struct se_wwn, wwn_group);
  899. struct target_fabric_configfs *tf = wwn->wwn_tf;
  900. tf->tf_ops->fabric_drop_wwn(wwn);
  901. }
  902. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  903. .release = target_fabric_release_wwn,
  904. };
  905. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  906. .make_group = target_fabric_make_tpg,
  907. .drop_item = target_fabric_drop_tpg,
  908. };
  909. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  910. NULL);
  911. /* End of tfc_tpg_cit */
  912. /* Start of tfc_wwn_fabric_stats_cit */
  913. /*
  914. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  915. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  916. */
  917. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  918. /* End of tfc_wwn_fabric_stats_cit */
  919. /* Start of tfc_wwn_cit */
  920. static struct config_group *target_fabric_make_wwn(
  921. struct config_group *group,
  922. const char *name)
  923. {
  924. struct target_fabric_configfs *tf = container_of(group,
  925. struct target_fabric_configfs, tf_group);
  926. struct se_wwn *wwn;
  927. if (!tf->tf_ops->fabric_make_wwn) {
  928. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  929. return ERR_PTR(-ENOSYS);
  930. }
  931. wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
  932. if (!wwn || IS_ERR(wwn))
  933. return ERR_PTR(-EINVAL);
  934. wwn->wwn_tf = tf;
  935. /*
  936. * Setup default groups from pre-allocated wwn->wwn_default_groups
  937. */
  938. wwn->wwn_group.default_groups = wwn->wwn_default_groups;
  939. wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
  940. wwn->wwn_group.default_groups[1] = NULL;
  941. config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
  942. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  943. &tf->tf_wwn_fabric_stats_cit);
  944. return &wwn->wwn_group;
  945. }
  946. static void target_fabric_drop_wwn(
  947. struct config_group *group,
  948. struct config_item *item)
  949. {
  950. struct se_wwn *wwn = container_of(to_config_group(item),
  951. struct se_wwn, wwn_group);
  952. struct config_item *df_item;
  953. struct config_group *cg = &wwn->wwn_group;
  954. int i;
  955. for (i = 0; cg->default_groups[i]; i++) {
  956. df_item = &cg->default_groups[i]->cg_item;
  957. cg->default_groups[i] = NULL;
  958. config_item_put(df_item);
  959. }
  960. config_item_put(item);
  961. }
  962. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  963. .make_group = target_fabric_make_wwn,
  964. .drop_item = target_fabric_drop_wwn,
  965. };
  966. /*
  967. * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
  968. */
  969. CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
  970. static struct configfs_item_operations target_fabric_wwn_item_ops = {
  971. .show_attribute = target_fabric_wwn_attr_show,
  972. .store_attribute = target_fabric_wwn_attr_store,
  973. };
  974. TF_CIT_SETUP_DRV(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops);
  975. /* End of tfc_wwn_cit */
  976. /* Start of tfc_discovery_cit */
  977. CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
  978. tf_disc_group);
  979. static struct configfs_item_operations target_fabric_discovery_item_ops = {
  980. .show_attribute = target_fabric_discovery_attr_show,
  981. .store_attribute = target_fabric_discovery_attr_store,
  982. };
  983. TF_CIT_SETUP_DRV(discovery, &target_fabric_discovery_item_ops, NULL);
  984. /* End of tfc_discovery_cit */
  985. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  986. {
  987. target_fabric_setup_discovery_cit(tf);
  988. target_fabric_setup_wwn_cit(tf);
  989. target_fabric_setup_wwn_fabric_stats_cit(tf);
  990. target_fabric_setup_tpg_cit(tf);
  991. target_fabric_setup_tpg_base_cit(tf);
  992. target_fabric_setup_tpg_port_cit(tf);
  993. target_fabric_setup_tpg_port_stat_cit(tf);
  994. target_fabric_setup_tpg_lun_cit(tf);
  995. target_fabric_setup_tpg_np_cit(tf);
  996. target_fabric_setup_tpg_np_base_cit(tf);
  997. target_fabric_setup_tpg_attrib_cit(tf);
  998. target_fabric_setup_tpg_auth_cit(tf);
  999. target_fabric_setup_tpg_param_cit(tf);
  1000. target_fabric_setup_tpg_nacl_cit(tf);
  1001. target_fabric_setup_tpg_nacl_base_cit(tf);
  1002. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  1003. target_fabric_setup_tpg_nacl_auth_cit(tf);
  1004. target_fabric_setup_tpg_nacl_param_cit(tf);
  1005. target_fabric_setup_tpg_nacl_stat_cit(tf);
  1006. target_fabric_setup_tpg_mappedlun_cit(tf);
  1007. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  1008. return 0;
  1009. }