target_core_fabric_configfs.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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_core_internal.h"
  37. #include "target_core_alua.h"
  38. #include "target_core_pr.h"
  39. #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
  40. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  41. { \
  42. struct config_item_type *cit = &tf->tf_##_name##_cit; \
  43. \
  44. cit->ct_item_ops = _item_ops; \
  45. cit->ct_group_ops = _group_ops; \
  46. cit->ct_attrs = _attrs; \
  47. cit->ct_owner = tf->tf_ops->module; \
  48. pr_debug("Setup generic %s\n", __stringify(_name)); \
  49. }
  50. #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
  51. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  52. { \
  53. struct config_item_type *cit = &tf->tf_##_name##_cit; \
  54. struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
  55. \
  56. cit->ct_item_ops = _item_ops; \
  57. cit->ct_group_ops = _group_ops; \
  58. cit->ct_attrs = attrs; \
  59. cit->ct_owner = tf->tf_ops->module; \
  60. pr_debug("Setup generic %s\n", __stringify(_name)); \
  61. }
  62. /* Start of tfc_tpg_mappedlun_cit */
  63. static int target_fabric_mappedlun_link(
  64. struct config_item *lun_acl_ci,
  65. struct config_item *lun_ci)
  66. {
  67. struct se_dev_entry *deve;
  68. struct se_lun *lun = container_of(to_config_group(lun_ci),
  69. struct se_lun, lun_group);
  70. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  71. struct se_lun_acl, se_lun_group);
  72. struct se_portal_group *se_tpg;
  73. struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
  74. bool lun_access_ro;
  75. if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
  76. pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
  77. " %p to struct lun: %p\n", lun_ci, lun);
  78. return -EFAULT;
  79. }
  80. /*
  81. * Ensure that the source port exists
  82. */
  83. if (!lun->lun_se_dev) {
  84. pr_err("Source se_lun->lun_se_dev does not exist\n");
  85. return -EINVAL;
  86. }
  87. if (lun->lun_shutdown) {
  88. pr_err("Unable to create mappedlun symlink because"
  89. " lun->lun_shutdown=true\n");
  90. return -EINVAL;
  91. }
  92. se_tpg = lun->lun_tpg;
  93. nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
  94. tpg_ci = &nacl_ci->ci_group->cg_item;
  95. wwn_ci = &tpg_ci->ci_group->cg_item;
  96. tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
  97. wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
  98. /*
  99. * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
  100. */
  101. if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
  102. pr_err("Illegal Initiator ACL SymLink outside of %s\n",
  103. config_item_name(wwn_ci));
  104. return -EINVAL;
  105. }
  106. if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
  107. pr_err("Illegal Initiator ACL Symlink outside of %s"
  108. " TPGT: %s\n", config_item_name(wwn_ci),
  109. config_item_name(tpg_ci));
  110. return -EINVAL;
  111. }
  112. /*
  113. * If this struct se_node_acl was dynamically generated with
  114. * tpg_1/attrib/generate_node_acls=1, use the existing
  115. * deve->lun_access_ro value, which will be true when
  116. * tpg_1/attrib/demo_mode_write_protect=1
  117. */
  118. rcu_read_lock();
  119. deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
  120. if (deve)
  121. lun_access_ro = deve->lun_access_ro;
  122. else
  123. lun_access_ro =
  124. (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
  125. se_tpg)) ? true : false;
  126. rcu_read_unlock();
  127. /*
  128. * Determine the actual mapped LUN value user wants..
  129. *
  130. * This value is what the SCSI Initiator actually sees the
  131. * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
  132. */
  133. return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
  134. }
  135. static int target_fabric_mappedlun_unlink(
  136. struct config_item *lun_acl_ci,
  137. struct config_item *lun_ci)
  138. {
  139. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  140. struct se_lun_acl, se_lun_group);
  141. struct se_lun *lun = container_of(to_config_group(lun_ci),
  142. struct se_lun, lun_group);
  143. return core_dev_del_initiator_node_lun_acl(lun, lacl);
  144. }
  145. static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
  146. {
  147. return container_of(to_config_group(item), struct se_lun_acl,
  148. se_lun_group);
  149. }
  150. static ssize_t target_fabric_mappedlun_write_protect_show(
  151. struct config_item *item, char *page)
  152. {
  153. struct se_lun_acl *lacl = item_to_lun_acl(item);
  154. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  155. struct se_dev_entry *deve;
  156. ssize_t len = 0;
  157. rcu_read_lock();
  158. deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
  159. if (deve) {
  160. len = sprintf(page, "%d\n", deve->lun_access_ro);
  161. }
  162. rcu_read_unlock();
  163. return len;
  164. }
  165. static ssize_t target_fabric_mappedlun_write_protect_store(
  166. struct config_item *item, const char *page, size_t count)
  167. {
  168. struct se_lun_acl *lacl = item_to_lun_acl(item);
  169. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  170. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  171. unsigned long wp;
  172. int ret;
  173. ret = kstrtoul(page, 0, &wp);
  174. if (ret)
  175. return ret;
  176. if ((wp != 1) && (wp != 0))
  177. return -EINVAL;
  178. /* wp=1 means lun_access_ro=true */
  179. core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
  180. pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
  181. " Mapped LUN: %llu Write Protect bit to %s\n",
  182. se_tpg->se_tpg_tfo->get_fabric_name(),
  183. se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
  184. return count;
  185. }
  186. CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
  187. static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
  188. &target_fabric_mappedlun_attr_write_protect,
  189. NULL,
  190. };
  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_item_operations target_fabric_mappedlun_item_ops = {
  199. .release = target_fabric_mappedlun_release,
  200. .allow_link = target_fabric_mappedlun_link,
  201. .drop_link = target_fabric_mappedlun_unlink,
  202. };
  203. TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  204. target_fabric_mappedlun_attrs);
  205. /* End of tfc_tpg_mappedlun_cit */
  206. /* Start of tfc_tpg_mappedlun_port_cit */
  207. static struct config_group *target_core_mappedlun_stat_mkdir(
  208. struct config_group *group,
  209. const char *name)
  210. {
  211. return ERR_PTR(-ENOSYS);
  212. }
  213. static void target_core_mappedlun_stat_rmdir(
  214. struct config_group *group,
  215. struct config_item *item)
  216. {
  217. return;
  218. }
  219. static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
  220. .make_group = target_core_mappedlun_stat_mkdir,
  221. .drop_item = target_core_mappedlun_stat_rmdir,
  222. };
  223. TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
  224. NULL);
  225. /* End of tfc_tpg_mappedlun_port_cit */
  226. TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
  227. TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
  228. TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
  229. /* Start of tfc_tpg_nacl_base_cit */
  230. static struct config_group *target_fabric_make_mappedlun(
  231. struct config_group *group,
  232. const char *name)
  233. {
  234. struct se_node_acl *se_nacl = container_of(group,
  235. struct se_node_acl, acl_group);
  236. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  237. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  238. struct se_lun_acl *lacl = NULL;
  239. char *buf;
  240. unsigned long long mapped_lun;
  241. int ret = 0;
  242. buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
  243. if (!buf) {
  244. pr_err("Unable to allocate memory for name buf\n");
  245. return ERR_PTR(-ENOMEM);
  246. }
  247. snprintf(buf, strlen(name) + 1, "%s", name);
  248. /*
  249. * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  250. */
  251. if (strstr(buf, "lun_") != buf) {
  252. pr_err("Unable to locate \"lun_\" from buf: %s"
  253. " name: %s\n", buf, name);
  254. ret = -EINVAL;
  255. goto out;
  256. }
  257. /*
  258. * Determine the Mapped LUN value. This is what the SCSI Initiator
  259. * Port will actually see.
  260. */
  261. ret = kstrtoull(buf + 4, 0, &mapped_lun);
  262. if (ret)
  263. goto out;
  264. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
  265. mapped_lun, &ret);
  266. if (!lacl) {
  267. ret = -EINVAL;
  268. goto out;
  269. }
  270. config_group_init_type_name(&lacl->se_lun_group, name,
  271. &tf->tf_tpg_mappedlun_cit);
  272. config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  273. "statistics", &tf->tf_tpg_mappedlun_stat_cit);
  274. configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
  275. &lacl->se_lun_group);
  276. target_stat_setup_mappedlun_default_groups(lacl);
  277. kfree(buf);
  278. return &lacl->se_lun_group;
  279. out:
  280. kfree(lacl);
  281. kfree(buf);
  282. return ERR_PTR(ret);
  283. }
  284. static void target_fabric_drop_mappedlun(
  285. struct config_group *group,
  286. struct config_item *item)
  287. {
  288. struct se_lun_acl *lacl = container_of(to_config_group(item),
  289. struct se_lun_acl, se_lun_group);
  290. configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
  291. configfs_remove_default_groups(&lacl->se_lun_group);
  292. config_item_put(item);
  293. }
  294. static void target_fabric_nacl_base_release(struct config_item *item)
  295. {
  296. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  297. struct se_node_acl, acl_group);
  298. configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
  299. core_tpg_del_initiator_node_acl(se_nacl);
  300. }
  301. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  302. .release = target_fabric_nacl_base_release,
  303. };
  304. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  305. .make_group = target_fabric_make_mappedlun,
  306. .drop_item = target_fabric_drop_mappedlun,
  307. };
  308. TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  309. &target_fabric_nacl_base_group_ops);
  310. /* End of tfc_tpg_nacl_base_cit */
  311. /* Start of tfc_node_fabric_stats_cit */
  312. /*
  313. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  314. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  315. */
  316. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  317. /* End of tfc_wwn_fabric_stats_cit */
  318. /* Start of tfc_tpg_nacl_cit */
  319. static struct config_group *target_fabric_make_nodeacl(
  320. struct config_group *group,
  321. const char *name)
  322. {
  323. struct se_portal_group *se_tpg = container_of(group,
  324. struct se_portal_group, tpg_acl_group);
  325. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  326. struct se_node_acl *se_nacl;
  327. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
  328. if (IS_ERR(se_nacl))
  329. return ERR_CAST(se_nacl);
  330. config_group_init_type_name(&se_nacl->acl_group, name,
  331. &tf->tf_tpg_nacl_base_cit);
  332. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  333. &tf->tf_tpg_nacl_attrib_cit);
  334. configfs_add_default_group(&se_nacl->acl_attrib_group,
  335. &se_nacl->acl_group);
  336. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  337. &tf->tf_tpg_nacl_auth_cit);
  338. configfs_add_default_group(&se_nacl->acl_auth_group,
  339. &se_nacl->acl_group);
  340. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  341. &tf->tf_tpg_nacl_param_cit);
  342. configfs_add_default_group(&se_nacl->acl_param_group,
  343. &se_nacl->acl_group);
  344. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  345. "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
  346. configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
  347. &se_nacl->acl_group);
  348. if (tf->tf_ops->fabric_init_nodeacl) {
  349. int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
  350. if (ret) {
  351. configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
  352. core_tpg_del_initiator_node_acl(se_nacl);
  353. return ERR_PTR(ret);
  354. }
  355. }
  356. return &se_nacl->acl_group;
  357. }
  358. static void target_fabric_drop_nodeacl(
  359. struct config_group *group,
  360. struct config_item *item)
  361. {
  362. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  363. struct se_node_acl, acl_group);
  364. configfs_remove_default_groups(&se_nacl->acl_group);
  365. /*
  366. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  367. */
  368. config_item_put(item);
  369. }
  370. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  371. .make_group = target_fabric_make_nodeacl,
  372. .drop_item = target_fabric_drop_nodeacl,
  373. };
  374. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  375. /* End of tfc_tpg_nacl_cit */
  376. /* Start of tfc_tpg_np_base_cit */
  377. static void target_fabric_np_base_release(struct config_item *item)
  378. {
  379. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  380. struct se_tpg_np, tpg_np_group);
  381. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  382. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  383. tf->tf_ops->fabric_drop_np(se_tpg_np);
  384. }
  385. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  386. .release = target_fabric_np_base_release,
  387. };
  388. TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
  389. /* End of tfc_tpg_np_base_cit */
  390. /* Start of tfc_tpg_np_cit */
  391. static struct config_group *target_fabric_make_np(
  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_np_group);
  397. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  398. struct se_tpg_np *se_tpg_np;
  399. if (!tf->tf_ops->fabric_make_np) {
  400. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  401. return ERR_PTR(-ENOSYS);
  402. }
  403. se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
  404. if (!se_tpg_np || IS_ERR(se_tpg_np))
  405. return ERR_PTR(-EINVAL);
  406. se_tpg_np->tpg_np_parent = se_tpg;
  407. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  408. &tf->tf_tpg_np_base_cit);
  409. return &se_tpg_np->tpg_np_group;
  410. }
  411. static void target_fabric_drop_np(
  412. struct config_group *group,
  413. struct config_item *item)
  414. {
  415. /*
  416. * struct se_tpg_np is released via target_fabric_np_base_release()
  417. */
  418. config_item_put(item);
  419. }
  420. static struct configfs_group_operations target_fabric_np_group_ops = {
  421. .make_group = &target_fabric_make_np,
  422. .drop_item = &target_fabric_drop_np,
  423. };
  424. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  425. /* End of tfc_tpg_np_cit */
  426. /* Start of tfc_tpg_port_cit */
  427. static struct se_lun *item_to_lun(struct config_item *item)
  428. {
  429. return container_of(to_config_group(item), struct se_lun,
  430. lun_group);
  431. }
  432. static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
  433. char *page)
  434. {
  435. struct se_lun *lun = item_to_lun(item);
  436. if (!lun || !lun->lun_se_dev)
  437. return -ENODEV;
  438. return core_alua_show_tg_pt_gp_info(lun, page);
  439. }
  440. static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
  441. const char *page, size_t count)
  442. {
  443. struct se_lun *lun = item_to_lun(item);
  444. if (!lun || !lun->lun_se_dev)
  445. return -ENODEV;
  446. return core_alua_store_tg_pt_gp_info(lun, page, count);
  447. }
  448. static ssize_t target_fabric_port_alua_tg_pt_offline_show(
  449. struct config_item *item, char *page)
  450. {
  451. struct se_lun *lun = item_to_lun(item);
  452. if (!lun || !lun->lun_se_dev)
  453. return -ENODEV;
  454. return core_alua_show_offline_bit(lun, page);
  455. }
  456. static ssize_t target_fabric_port_alua_tg_pt_offline_store(
  457. struct config_item *item, const char *page, size_t count)
  458. {
  459. struct se_lun *lun = item_to_lun(item);
  460. if (!lun || !lun->lun_se_dev)
  461. return -ENODEV;
  462. return core_alua_store_offline_bit(lun, page, count);
  463. }
  464. static ssize_t target_fabric_port_alua_tg_pt_status_show(
  465. struct config_item *item, char *page)
  466. {
  467. struct se_lun *lun = item_to_lun(item);
  468. if (!lun || !lun->lun_se_dev)
  469. return -ENODEV;
  470. return core_alua_show_secondary_status(lun, page);
  471. }
  472. static ssize_t target_fabric_port_alua_tg_pt_status_store(
  473. struct config_item *item, const char *page, size_t count)
  474. {
  475. struct se_lun *lun = item_to_lun(item);
  476. if (!lun || !lun->lun_se_dev)
  477. return -ENODEV;
  478. return core_alua_store_secondary_status(lun, page, count);
  479. }
  480. static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
  481. struct config_item *item, char *page)
  482. {
  483. struct se_lun *lun = item_to_lun(item);
  484. if (!lun || !lun->lun_se_dev)
  485. return -ENODEV;
  486. return core_alua_show_secondary_write_metadata(lun, page);
  487. }
  488. static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
  489. struct config_item *item, const char *page, size_t count)
  490. {
  491. struct se_lun *lun = item_to_lun(item);
  492. if (!lun || !lun->lun_se_dev)
  493. return -ENODEV;
  494. return core_alua_store_secondary_write_metadata(lun, page, count);
  495. }
  496. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
  497. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
  498. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
  499. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
  500. static struct configfs_attribute *target_fabric_port_attrs[] = {
  501. &target_fabric_port_attr_alua_tg_pt_gp,
  502. &target_fabric_port_attr_alua_tg_pt_offline,
  503. &target_fabric_port_attr_alua_tg_pt_status,
  504. &target_fabric_port_attr_alua_tg_pt_write_md,
  505. NULL,
  506. };
  507. static int target_fabric_port_link(
  508. struct config_item *lun_ci,
  509. struct config_item *se_dev_ci)
  510. {
  511. struct config_item *tpg_ci;
  512. struct se_lun *lun = container_of(to_config_group(lun_ci),
  513. struct se_lun, lun_group);
  514. struct se_portal_group *se_tpg;
  515. struct se_device *dev =
  516. container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  517. struct target_fabric_configfs *tf;
  518. int ret;
  519. if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
  520. pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
  521. " %p to struct se_device: %p\n", se_dev_ci, dev);
  522. return -EFAULT;
  523. }
  524. if (!(dev->dev_flags & DF_CONFIGURED)) {
  525. pr_err("se_device not configured yet, cannot port link\n");
  526. return -ENODEV;
  527. }
  528. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  529. se_tpg = container_of(to_config_group(tpg_ci),
  530. struct se_portal_group, tpg_group);
  531. tf = se_tpg->se_tpg_wwn->wwn_tf;
  532. if (lun->lun_se_dev != NULL) {
  533. pr_err("Port Symlink already exists\n");
  534. return -EEXIST;
  535. }
  536. ret = core_dev_add_lun(se_tpg, dev, lun);
  537. if (ret) {
  538. pr_err("core_dev_add_lun() failed: %d\n", ret);
  539. goto out;
  540. }
  541. if (tf->tf_ops->fabric_post_link) {
  542. /*
  543. * Call the optional fabric_post_link() to allow a
  544. * fabric module to setup any additional state once
  545. * core_dev_add_lun() has been called..
  546. */
  547. tf->tf_ops->fabric_post_link(se_tpg, lun);
  548. }
  549. return 0;
  550. out:
  551. return ret;
  552. }
  553. static int target_fabric_port_unlink(
  554. struct config_item *lun_ci,
  555. struct config_item *se_dev_ci)
  556. {
  557. struct se_lun *lun = container_of(to_config_group(lun_ci),
  558. struct se_lun, lun_group);
  559. struct se_portal_group *se_tpg = lun->lun_tpg;
  560. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  561. if (tf->tf_ops->fabric_pre_unlink) {
  562. /*
  563. * Call the optional fabric_pre_unlink() to allow a
  564. * fabric module to release any additional stat before
  565. * core_dev_del_lun() is called.
  566. */
  567. tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
  568. }
  569. core_dev_del_lun(se_tpg, lun);
  570. return 0;
  571. }
  572. static void target_fabric_port_release(struct config_item *item)
  573. {
  574. struct se_lun *lun = container_of(to_config_group(item),
  575. struct se_lun, lun_group);
  576. kfree_rcu(lun, rcu_head);
  577. }
  578. static struct configfs_item_operations target_fabric_port_item_ops = {
  579. .release = target_fabric_port_release,
  580. .allow_link = target_fabric_port_link,
  581. .drop_link = target_fabric_port_unlink,
  582. };
  583. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  584. /* End of tfc_tpg_port_cit */
  585. /* Start of tfc_tpg_port_stat_cit */
  586. static struct config_group *target_core_port_stat_mkdir(
  587. struct config_group *group,
  588. const char *name)
  589. {
  590. return ERR_PTR(-ENOSYS);
  591. }
  592. static void target_core_port_stat_rmdir(
  593. struct config_group *group,
  594. struct config_item *item)
  595. {
  596. return;
  597. }
  598. static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  599. .make_group = target_core_port_stat_mkdir,
  600. .drop_item = target_core_port_stat_rmdir,
  601. };
  602. TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  603. /* End of tfc_tpg_port_stat_cit */
  604. /* Start of tfc_tpg_lun_cit */
  605. static struct config_group *target_fabric_make_lun(
  606. struct config_group *group,
  607. const char *name)
  608. {
  609. struct se_lun *lun;
  610. struct se_portal_group *se_tpg = container_of(group,
  611. struct se_portal_group, tpg_lun_group);
  612. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  613. unsigned long long unpacked_lun;
  614. int errno;
  615. if (strstr(name, "lun_") != name) {
  616. pr_err("Unable to locate \'_\" in"
  617. " \"lun_$LUN_NUMBER\"\n");
  618. return ERR_PTR(-EINVAL);
  619. }
  620. errno = kstrtoull(name + 4, 0, &unpacked_lun);
  621. if (errno)
  622. return ERR_PTR(errno);
  623. lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
  624. if (IS_ERR(lun))
  625. return ERR_CAST(lun);
  626. config_group_init_type_name(&lun->lun_group, name,
  627. &tf->tf_tpg_port_cit);
  628. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  629. "statistics", &tf->tf_tpg_port_stat_cit);
  630. configfs_add_default_group(&lun->port_stat_grps.stat_group,
  631. &lun->lun_group);
  632. target_stat_setup_port_default_groups(lun);
  633. return &lun->lun_group;
  634. }
  635. static void target_fabric_drop_lun(
  636. struct config_group *group,
  637. struct config_item *item)
  638. {
  639. struct se_lun *lun = container_of(to_config_group(item),
  640. struct se_lun, lun_group);
  641. configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
  642. configfs_remove_default_groups(&lun->lun_group);
  643. config_item_put(item);
  644. }
  645. static struct configfs_group_operations target_fabric_lun_group_ops = {
  646. .make_group = &target_fabric_make_lun,
  647. .drop_item = &target_fabric_drop_lun,
  648. };
  649. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  650. /* End of tfc_tpg_lun_cit */
  651. TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
  652. TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
  653. TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
  654. /* Start of tfc_tpg_base_cit */
  655. static void target_fabric_tpg_release(struct config_item *item)
  656. {
  657. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  658. struct se_portal_group, tpg_group);
  659. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  660. struct target_fabric_configfs *tf = wwn->wwn_tf;
  661. tf->tf_ops->fabric_drop_tpg(se_tpg);
  662. }
  663. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  664. .release = target_fabric_tpg_release,
  665. };
  666. TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
  667. /* End of tfc_tpg_base_cit */
  668. /* Start of tfc_tpg_cit */
  669. static struct config_group *target_fabric_make_tpg(
  670. struct config_group *group,
  671. const char *name)
  672. {
  673. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  674. struct target_fabric_configfs *tf = wwn->wwn_tf;
  675. struct se_portal_group *se_tpg;
  676. if (!tf->tf_ops->fabric_make_tpg) {
  677. pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
  678. return ERR_PTR(-ENOSYS);
  679. }
  680. se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
  681. if (!se_tpg || IS_ERR(se_tpg))
  682. return ERR_PTR(-EINVAL);
  683. config_group_init_type_name(&se_tpg->tpg_group, name,
  684. &tf->tf_tpg_base_cit);
  685. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  686. &tf->tf_tpg_lun_cit);
  687. configfs_add_default_group(&se_tpg->tpg_lun_group,
  688. &se_tpg->tpg_group);
  689. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  690. &tf->tf_tpg_np_cit);
  691. configfs_add_default_group(&se_tpg->tpg_np_group,
  692. &se_tpg->tpg_group);
  693. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  694. &tf->tf_tpg_nacl_cit);
  695. configfs_add_default_group(&se_tpg->tpg_acl_group,
  696. &se_tpg->tpg_group);
  697. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  698. &tf->tf_tpg_attrib_cit);
  699. configfs_add_default_group(&se_tpg->tpg_attrib_group,
  700. &se_tpg->tpg_group);
  701. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  702. &tf->tf_tpg_auth_cit);
  703. configfs_add_default_group(&se_tpg->tpg_auth_group,
  704. &se_tpg->tpg_group);
  705. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  706. &tf->tf_tpg_param_cit);
  707. configfs_add_default_group(&se_tpg->tpg_param_group,
  708. &se_tpg->tpg_group);
  709. return &se_tpg->tpg_group;
  710. }
  711. static void target_fabric_drop_tpg(
  712. struct config_group *group,
  713. struct config_item *item)
  714. {
  715. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  716. struct se_portal_group, tpg_group);
  717. configfs_remove_default_groups(&se_tpg->tpg_group);
  718. config_item_put(item);
  719. }
  720. static void target_fabric_release_wwn(struct config_item *item)
  721. {
  722. struct se_wwn *wwn = container_of(to_config_group(item),
  723. struct se_wwn, wwn_group);
  724. struct target_fabric_configfs *tf = wwn->wwn_tf;
  725. configfs_remove_default_groups(&wwn->fabric_stat_group);
  726. tf->tf_ops->fabric_drop_wwn(wwn);
  727. }
  728. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  729. .release = target_fabric_release_wwn,
  730. };
  731. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  732. .make_group = target_fabric_make_tpg,
  733. .drop_item = target_fabric_drop_tpg,
  734. };
  735. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  736. NULL);
  737. /* End of tfc_tpg_cit */
  738. /* Start of tfc_wwn_fabric_stats_cit */
  739. /*
  740. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  741. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  742. */
  743. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  744. /* End of tfc_wwn_fabric_stats_cit */
  745. /* Start of tfc_wwn_cit */
  746. static struct config_group *target_fabric_make_wwn(
  747. struct config_group *group,
  748. const char *name)
  749. {
  750. struct target_fabric_configfs *tf = container_of(group,
  751. struct target_fabric_configfs, tf_group);
  752. struct se_wwn *wwn;
  753. if (!tf->tf_ops->fabric_make_wwn) {
  754. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  755. return ERR_PTR(-ENOSYS);
  756. }
  757. wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
  758. if (!wwn || IS_ERR(wwn))
  759. return ERR_PTR(-EINVAL);
  760. wwn->wwn_tf = tf;
  761. config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
  762. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  763. &tf->tf_wwn_fabric_stats_cit);
  764. configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
  765. if (tf->tf_ops->add_wwn_groups)
  766. tf->tf_ops->add_wwn_groups(wwn);
  767. return &wwn->wwn_group;
  768. }
  769. static void target_fabric_drop_wwn(
  770. struct config_group *group,
  771. struct config_item *item)
  772. {
  773. struct se_wwn *wwn = container_of(to_config_group(item),
  774. struct se_wwn, wwn_group);
  775. configfs_remove_default_groups(&wwn->wwn_group);
  776. config_item_put(item);
  777. }
  778. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  779. .make_group = target_fabric_make_wwn,
  780. .drop_item = target_fabric_drop_wwn,
  781. };
  782. TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
  783. TF_CIT_SETUP_DRV(discovery, NULL, NULL);
  784. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  785. {
  786. target_fabric_setup_discovery_cit(tf);
  787. target_fabric_setup_wwn_cit(tf);
  788. target_fabric_setup_wwn_fabric_stats_cit(tf);
  789. target_fabric_setup_tpg_cit(tf);
  790. target_fabric_setup_tpg_base_cit(tf);
  791. target_fabric_setup_tpg_port_cit(tf);
  792. target_fabric_setup_tpg_port_stat_cit(tf);
  793. target_fabric_setup_tpg_lun_cit(tf);
  794. target_fabric_setup_tpg_np_cit(tf);
  795. target_fabric_setup_tpg_np_base_cit(tf);
  796. target_fabric_setup_tpg_attrib_cit(tf);
  797. target_fabric_setup_tpg_auth_cit(tf);
  798. target_fabric_setup_tpg_param_cit(tf);
  799. target_fabric_setup_tpg_nacl_cit(tf);
  800. target_fabric_setup_tpg_nacl_base_cit(tf);
  801. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  802. target_fabric_setup_tpg_nacl_auth_cit(tf);
  803. target_fabric_setup_tpg_nacl_param_cit(tf);
  804. target_fabric_setup_tpg_nacl_stat_cit(tf);
  805. target_fabric_setup_tpg_mappedlun_cit(tf);
  806. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  807. return 0;
  808. }