target_core_fabric_configfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 void 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. 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 void 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. }
  571. static void target_fabric_port_release(struct config_item *item)
  572. {
  573. struct se_lun *lun = container_of(to_config_group(item),
  574. struct se_lun, lun_group);
  575. kfree_rcu(lun, rcu_head);
  576. }
  577. static struct configfs_item_operations target_fabric_port_item_ops = {
  578. .release = target_fabric_port_release,
  579. .allow_link = target_fabric_port_link,
  580. .drop_link = target_fabric_port_unlink,
  581. };
  582. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  583. /* End of tfc_tpg_port_cit */
  584. /* Start of tfc_tpg_port_stat_cit */
  585. static struct config_group *target_core_port_stat_mkdir(
  586. struct config_group *group,
  587. const char *name)
  588. {
  589. return ERR_PTR(-ENOSYS);
  590. }
  591. static void target_core_port_stat_rmdir(
  592. struct config_group *group,
  593. struct config_item *item)
  594. {
  595. return;
  596. }
  597. static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  598. .make_group = target_core_port_stat_mkdir,
  599. .drop_item = target_core_port_stat_rmdir,
  600. };
  601. TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  602. /* End of tfc_tpg_port_stat_cit */
  603. /* Start of tfc_tpg_lun_cit */
  604. static struct config_group *target_fabric_make_lun(
  605. struct config_group *group,
  606. const char *name)
  607. {
  608. struct se_lun *lun;
  609. struct se_portal_group *se_tpg = container_of(group,
  610. struct se_portal_group, tpg_lun_group);
  611. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  612. unsigned long long unpacked_lun;
  613. int errno;
  614. if (strstr(name, "lun_") != name) {
  615. pr_err("Unable to locate \'_\" in"
  616. " \"lun_$LUN_NUMBER\"\n");
  617. return ERR_PTR(-EINVAL);
  618. }
  619. errno = kstrtoull(name + 4, 0, &unpacked_lun);
  620. if (errno)
  621. return ERR_PTR(errno);
  622. lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
  623. if (IS_ERR(lun))
  624. return ERR_CAST(lun);
  625. config_group_init_type_name(&lun->lun_group, name,
  626. &tf->tf_tpg_port_cit);
  627. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  628. "statistics", &tf->tf_tpg_port_stat_cit);
  629. configfs_add_default_group(&lun->port_stat_grps.stat_group,
  630. &lun->lun_group);
  631. target_stat_setup_port_default_groups(lun);
  632. return &lun->lun_group;
  633. }
  634. static void target_fabric_drop_lun(
  635. struct config_group *group,
  636. struct config_item *item)
  637. {
  638. struct se_lun *lun = container_of(to_config_group(item),
  639. struct se_lun, lun_group);
  640. configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
  641. configfs_remove_default_groups(&lun->lun_group);
  642. config_item_put(item);
  643. }
  644. static struct configfs_group_operations target_fabric_lun_group_ops = {
  645. .make_group = &target_fabric_make_lun,
  646. .drop_item = &target_fabric_drop_lun,
  647. };
  648. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  649. /* End of tfc_tpg_lun_cit */
  650. TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
  651. TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
  652. TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
  653. /* Start of tfc_tpg_base_cit */
  654. static void target_fabric_tpg_release(struct config_item *item)
  655. {
  656. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  657. struct se_portal_group, tpg_group);
  658. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  659. struct target_fabric_configfs *tf = wwn->wwn_tf;
  660. tf->tf_ops->fabric_drop_tpg(se_tpg);
  661. }
  662. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  663. .release = target_fabric_tpg_release,
  664. };
  665. TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
  666. /* End of tfc_tpg_base_cit */
  667. /* Start of tfc_tpg_cit */
  668. static struct config_group *target_fabric_make_tpg(
  669. struct config_group *group,
  670. const char *name)
  671. {
  672. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  673. struct target_fabric_configfs *tf = wwn->wwn_tf;
  674. struct se_portal_group *se_tpg;
  675. if (!tf->tf_ops->fabric_make_tpg) {
  676. pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
  677. return ERR_PTR(-ENOSYS);
  678. }
  679. se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
  680. if (!se_tpg || IS_ERR(se_tpg))
  681. return ERR_PTR(-EINVAL);
  682. config_group_init_type_name(&se_tpg->tpg_group, name,
  683. &tf->tf_tpg_base_cit);
  684. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  685. &tf->tf_tpg_lun_cit);
  686. configfs_add_default_group(&se_tpg->tpg_lun_group,
  687. &se_tpg->tpg_group);
  688. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  689. &tf->tf_tpg_np_cit);
  690. configfs_add_default_group(&se_tpg->tpg_np_group,
  691. &se_tpg->tpg_group);
  692. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  693. &tf->tf_tpg_nacl_cit);
  694. configfs_add_default_group(&se_tpg->tpg_acl_group,
  695. &se_tpg->tpg_group);
  696. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  697. &tf->tf_tpg_attrib_cit);
  698. configfs_add_default_group(&se_tpg->tpg_attrib_group,
  699. &se_tpg->tpg_group);
  700. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  701. &tf->tf_tpg_auth_cit);
  702. configfs_add_default_group(&se_tpg->tpg_auth_group,
  703. &se_tpg->tpg_group);
  704. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  705. &tf->tf_tpg_param_cit);
  706. configfs_add_default_group(&se_tpg->tpg_param_group,
  707. &se_tpg->tpg_group);
  708. return &se_tpg->tpg_group;
  709. }
  710. static void target_fabric_drop_tpg(
  711. struct config_group *group,
  712. struct config_item *item)
  713. {
  714. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  715. struct se_portal_group, tpg_group);
  716. configfs_remove_default_groups(&se_tpg->tpg_group);
  717. config_item_put(item);
  718. }
  719. static void target_fabric_release_wwn(struct config_item *item)
  720. {
  721. struct se_wwn *wwn = container_of(to_config_group(item),
  722. struct se_wwn, wwn_group);
  723. struct target_fabric_configfs *tf = wwn->wwn_tf;
  724. configfs_remove_default_groups(&wwn->fabric_stat_group);
  725. tf->tf_ops->fabric_drop_wwn(wwn);
  726. }
  727. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  728. .release = target_fabric_release_wwn,
  729. };
  730. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  731. .make_group = target_fabric_make_tpg,
  732. .drop_item = target_fabric_drop_tpg,
  733. };
  734. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  735. NULL);
  736. /* End of tfc_tpg_cit */
  737. /* Start of tfc_wwn_fabric_stats_cit */
  738. /*
  739. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  740. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  741. */
  742. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  743. /* End of tfc_wwn_fabric_stats_cit */
  744. /* Start of tfc_wwn_cit */
  745. static struct config_group *target_fabric_make_wwn(
  746. struct config_group *group,
  747. const char *name)
  748. {
  749. struct target_fabric_configfs *tf = container_of(group,
  750. struct target_fabric_configfs, tf_group);
  751. struct se_wwn *wwn;
  752. if (!tf->tf_ops->fabric_make_wwn) {
  753. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  754. return ERR_PTR(-ENOSYS);
  755. }
  756. wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
  757. if (!wwn || IS_ERR(wwn))
  758. return ERR_PTR(-EINVAL);
  759. wwn->wwn_tf = tf;
  760. config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
  761. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  762. &tf->tf_wwn_fabric_stats_cit);
  763. configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
  764. if (tf->tf_ops->add_wwn_groups)
  765. tf->tf_ops->add_wwn_groups(wwn);
  766. return &wwn->wwn_group;
  767. }
  768. static void target_fabric_drop_wwn(
  769. struct config_group *group,
  770. struct config_item *item)
  771. {
  772. struct se_wwn *wwn = container_of(to_config_group(item),
  773. struct se_wwn, wwn_group);
  774. configfs_remove_default_groups(&wwn->wwn_group);
  775. config_item_put(item);
  776. }
  777. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  778. .make_group = target_fabric_make_wwn,
  779. .drop_item = target_fabric_drop_wwn,
  780. };
  781. TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
  782. TF_CIT_SETUP_DRV(discovery, NULL, NULL);
  783. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  784. {
  785. target_fabric_setup_discovery_cit(tf);
  786. target_fabric_setup_wwn_cit(tf);
  787. target_fabric_setup_wwn_fabric_stats_cit(tf);
  788. target_fabric_setup_tpg_cit(tf);
  789. target_fabric_setup_tpg_base_cit(tf);
  790. target_fabric_setup_tpg_port_cit(tf);
  791. target_fabric_setup_tpg_port_stat_cit(tf);
  792. target_fabric_setup_tpg_lun_cit(tf);
  793. target_fabric_setup_tpg_np_cit(tf);
  794. target_fabric_setup_tpg_np_base_cit(tf);
  795. target_fabric_setup_tpg_attrib_cit(tf);
  796. target_fabric_setup_tpg_auth_cit(tf);
  797. target_fabric_setup_tpg_param_cit(tf);
  798. target_fabric_setup_tpg_nacl_cit(tf);
  799. target_fabric_setup_tpg_nacl_base_cit(tf);
  800. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  801. target_fabric_setup_tpg_nacl_auth_cit(tf);
  802. target_fabric_setup_tpg_nacl_param_cit(tf);
  803. target_fabric_setup_tpg_nacl_stat_cit(tf);
  804. target_fabric_setup_tpg_mappedlun_cit(tf);
  805. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  806. return 0;
  807. }