target_core_fabric_configfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. static struct configfs_item_operations target_fabric_port_item_ops;
  63. /* Start of tfc_tpg_mappedlun_cit */
  64. static int target_fabric_mappedlun_link(
  65. struct config_item *lun_acl_ci,
  66. struct config_item *lun_ci)
  67. {
  68. struct se_dev_entry *deve;
  69. struct se_lun *lun;
  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_ci->ci_type ||
  76. lun_ci->ci_type->ct_item_ops != &target_fabric_port_item_ops) {
  77. pr_err("Bad lun_ci, not a valid lun_ci pointer: %p\n", lun_ci);
  78. return -EFAULT;
  79. }
  80. lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
  81. /*
  82. * Ensure that the source port exists
  83. */
  84. if (!lun->lun_se_dev) {
  85. pr_err("Source se_lun->lun_se_dev does not exist\n");
  86. return -EINVAL;
  87. }
  88. if (lun->lun_shutdown) {
  89. pr_err("Unable to create mappedlun symlink because"
  90. " lun->lun_shutdown=true\n");
  91. return -EINVAL;
  92. }
  93. se_tpg = lun->lun_tpg;
  94. nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
  95. tpg_ci = &nacl_ci->ci_group->cg_item;
  96. wwn_ci = &tpg_ci->ci_group->cg_item;
  97. tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
  98. wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
  99. /*
  100. * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
  101. */
  102. if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
  103. pr_err("Illegal Initiator ACL SymLink outside of %s\n",
  104. config_item_name(wwn_ci));
  105. return -EINVAL;
  106. }
  107. if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
  108. pr_err("Illegal Initiator ACL Symlink outside of %s"
  109. " TPGT: %s\n", config_item_name(wwn_ci),
  110. config_item_name(tpg_ci));
  111. return -EINVAL;
  112. }
  113. /*
  114. * If this struct se_node_acl was dynamically generated with
  115. * tpg_1/attrib/generate_node_acls=1, use the existing
  116. * deve->lun_access_ro value, which will be true when
  117. * tpg_1/attrib/demo_mode_write_protect=1
  118. */
  119. rcu_read_lock();
  120. deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
  121. if (deve)
  122. lun_access_ro = deve->lun_access_ro;
  123. else
  124. lun_access_ro =
  125. (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
  126. se_tpg)) ? true : false;
  127. rcu_read_unlock();
  128. /*
  129. * Determine the actual mapped LUN value user wants..
  130. *
  131. * This value is what the SCSI Initiator actually sees the
  132. * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
  133. */
  134. return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
  135. }
  136. static void target_fabric_mappedlun_unlink(
  137. struct config_item *lun_acl_ci,
  138. struct config_item *lun_ci)
  139. {
  140. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  141. struct se_lun_acl, se_lun_group);
  142. struct se_lun *lun = container_of(to_config_group(lun_ci),
  143. struct se_lun, lun_group);
  144. core_dev_del_initiator_node_lun_acl(lun, lacl);
  145. }
  146. static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
  147. {
  148. return container_of(to_config_group(item), struct se_lun_acl,
  149. se_lun_group);
  150. }
  151. static ssize_t target_fabric_mappedlun_write_protect_show(
  152. struct config_item *item, char *page)
  153. {
  154. struct se_lun_acl *lacl = item_to_lun_acl(item);
  155. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  156. struct se_dev_entry *deve;
  157. ssize_t len = 0;
  158. rcu_read_lock();
  159. deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
  160. if (deve) {
  161. len = sprintf(page, "%d\n", deve->lun_access_ro);
  162. }
  163. rcu_read_unlock();
  164. return len;
  165. }
  166. static ssize_t target_fabric_mappedlun_write_protect_store(
  167. struct config_item *item, const char *page, size_t count)
  168. {
  169. struct se_lun_acl *lacl = item_to_lun_acl(item);
  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 wp;
  173. int ret;
  174. ret = kstrtoul(page, 0, &wp);
  175. if (ret)
  176. return ret;
  177. if ((wp != 1) && (wp != 0))
  178. return -EINVAL;
  179. /* wp=1 means lun_access_ro=true */
  180. core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
  181. pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
  182. " Mapped LUN: %llu Write Protect bit to %s\n",
  183. se_tpg->se_tpg_tfo->get_fabric_name(),
  184. se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
  185. return count;
  186. }
  187. CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
  188. static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
  189. &target_fabric_mappedlun_attr_write_protect,
  190. NULL,
  191. };
  192. static void target_fabric_mappedlun_release(struct config_item *item)
  193. {
  194. struct se_lun_acl *lacl = container_of(to_config_group(item),
  195. struct se_lun_acl, se_lun_group);
  196. struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
  197. core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
  198. }
  199. static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
  200. .release = target_fabric_mappedlun_release,
  201. .allow_link = target_fabric_mappedlun_link,
  202. .drop_link = target_fabric_mappedlun_unlink,
  203. };
  204. TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  205. target_fabric_mappedlun_attrs);
  206. /* End of tfc_tpg_mappedlun_cit */
  207. /* Start of tfc_tpg_mappedlun_port_cit */
  208. static struct config_group *target_core_mappedlun_stat_mkdir(
  209. struct config_group *group,
  210. const char *name)
  211. {
  212. return ERR_PTR(-ENOSYS);
  213. }
  214. static void target_core_mappedlun_stat_rmdir(
  215. struct config_group *group,
  216. struct config_item *item)
  217. {
  218. return;
  219. }
  220. static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
  221. .make_group = target_core_mappedlun_stat_mkdir,
  222. .drop_item = target_core_mappedlun_stat_rmdir,
  223. };
  224. TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
  225. NULL);
  226. /* End of tfc_tpg_mappedlun_port_cit */
  227. TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
  228. TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
  229. TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
  230. /* Start of tfc_tpg_nacl_base_cit */
  231. static struct config_group *target_fabric_make_mappedlun(
  232. struct config_group *group,
  233. const char *name)
  234. {
  235. struct se_node_acl *se_nacl = container_of(group,
  236. struct se_node_acl, acl_group);
  237. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  238. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  239. struct se_lun_acl *lacl = NULL;
  240. char *buf;
  241. unsigned long long mapped_lun;
  242. int ret = 0;
  243. buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
  244. if (!buf) {
  245. pr_err("Unable to allocate memory for name buf\n");
  246. return ERR_PTR(-ENOMEM);
  247. }
  248. snprintf(buf, strlen(name) + 1, "%s", name);
  249. /*
  250. * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  251. */
  252. if (strstr(buf, "lun_") != buf) {
  253. pr_err("Unable to locate \"lun_\" from buf: %s"
  254. " name: %s\n", buf, name);
  255. ret = -EINVAL;
  256. goto out;
  257. }
  258. /*
  259. * Determine the Mapped LUN value. This is what the SCSI Initiator
  260. * Port will actually see.
  261. */
  262. ret = kstrtoull(buf + 4, 0, &mapped_lun);
  263. if (ret)
  264. goto out;
  265. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
  266. mapped_lun, &ret);
  267. if (!lacl) {
  268. ret = -EINVAL;
  269. goto out;
  270. }
  271. config_group_init_type_name(&lacl->se_lun_group, name,
  272. &tf->tf_tpg_mappedlun_cit);
  273. config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  274. "statistics", &tf->tf_tpg_mappedlun_stat_cit);
  275. configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
  276. &lacl->se_lun_group);
  277. target_stat_setup_mappedlun_default_groups(lacl);
  278. kfree(buf);
  279. return &lacl->se_lun_group;
  280. out:
  281. kfree(lacl);
  282. kfree(buf);
  283. return ERR_PTR(ret);
  284. }
  285. static void target_fabric_drop_mappedlun(
  286. struct config_group *group,
  287. struct config_item *item)
  288. {
  289. struct se_lun_acl *lacl = container_of(to_config_group(item),
  290. struct se_lun_acl, se_lun_group);
  291. configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
  292. configfs_remove_default_groups(&lacl->se_lun_group);
  293. config_item_put(item);
  294. }
  295. static void target_fabric_nacl_base_release(struct config_item *item)
  296. {
  297. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  298. struct se_node_acl, acl_group);
  299. configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
  300. core_tpg_del_initiator_node_acl(se_nacl);
  301. }
  302. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  303. .release = target_fabric_nacl_base_release,
  304. };
  305. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  306. .make_group = target_fabric_make_mappedlun,
  307. .drop_item = target_fabric_drop_mappedlun,
  308. };
  309. TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  310. &target_fabric_nacl_base_group_ops);
  311. /* End of tfc_tpg_nacl_base_cit */
  312. /* Start of tfc_node_fabric_stats_cit */
  313. /*
  314. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  315. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  316. */
  317. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  318. /* End of tfc_wwn_fabric_stats_cit */
  319. /* Start of tfc_tpg_nacl_cit */
  320. static struct config_group *target_fabric_make_nodeacl(
  321. struct config_group *group,
  322. const char *name)
  323. {
  324. struct se_portal_group *se_tpg = container_of(group,
  325. struct se_portal_group, tpg_acl_group);
  326. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  327. struct se_node_acl *se_nacl;
  328. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
  329. if (IS_ERR(se_nacl))
  330. return ERR_CAST(se_nacl);
  331. config_group_init_type_name(&se_nacl->acl_group, name,
  332. &tf->tf_tpg_nacl_base_cit);
  333. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  334. &tf->tf_tpg_nacl_attrib_cit);
  335. configfs_add_default_group(&se_nacl->acl_attrib_group,
  336. &se_nacl->acl_group);
  337. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  338. &tf->tf_tpg_nacl_auth_cit);
  339. configfs_add_default_group(&se_nacl->acl_auth_group,
  340. &se_nacl->acl_group);
  341. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  342. &tf->tf_tpg_nacl_param_cit);
  343. configfs_add_default_group(&se_nacl->acl_param_group,
  344. &se_nacl->acl_group);
  345. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  346. "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
  347. configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
  348. &se_nacl->acl_group);
  349. if (tf->tf_ops->fabric_init_nodeacl) {
  350. int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
  351. if (ret) {
  352. configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
  353. core_tpg_del_initiator_node_acl(se_nacl);
  354. return ERR_PTR(ret);
  355. }
  356. }
  357. return &se_nacl->acl_group;
  358. }
  359. static void target_fabric_drop_nodeacl(
  360. struct config_group *group,
  361. struct config_item *item)
  362. {
  363. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  364. struct se_node_acl, acl_group);
  365. configfs_remove_default_groups(&se_nacl->acl_group);
  366. /*
  367. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  368. */
  369. config_item_put(item);
  370. }
  371. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  372. .make_group = target_fabric_make_nodeacl,
  373. .drop_item = target_fabric_drop_nodeacl,
  374. };
  375. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  376. /* End of tfc_tpg_nacl_cit */
  377. /* Start of tfc_tpg_np_base_cit */
  378. static void target_fabric_np_base_release(struct config_item *item)
  379. {
  380. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  381. struct se_tpg_np, tpg_np_group);
  382. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  383. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  384. tf->tf_ops->fabric_drop_np(se_tpg_np);
  385. }
  386. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  387. .release = target_fabric_np_base_release,
  388. };
  389. TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
  390. /* End of tfc_tpg_np_base_cit */
  391. /* Start of tfc_tpg_np_cit */
  392. static struct config_group *target_fabric_make_np(
  393. struct config_group *group,
  394. const char *name)
  395. {
  396. struct se_portal_group *se_tpg = container_of(group,
  397. struct se_portal_group, tpg_np_group);
  398. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  399. struct se_tpg_np *se_tpg_np;
  400. if (!tf->tf_ops->fabric_make_np) {
  401. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  402. return ERR_PTR(-ENOSYS);
  403. }
  404. se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
  405. if (!se_tpg_np || IS_ERR(se_tpg_np))
  406. return ERR_PTR(-EINVAL);
  407. se_tpg_np->tpg_np_parent = se_tpg;
  408. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  409. &tf->tf_tpg_np_base_cit);
  410. return &se_tpg_np->tpg_np_group;
  411. }
  412. static void target_fabric_drop_np(
  413. struct config_group *group,
  414. struct config_item *item)
  415. {
  416. /*
  417. * struct se_tpg_np is released via target_fabric_np_base_release()
  418. */
  419. config_item_put(item);
  420. }
  421. static struct configfs_group_operations target_fabric_np_group_ops = {
  422. .make_group = &target_fabric_make_np,
  423. .drop_item = &target_fabric_drop_np,
  424. };
  425. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  426. /* End of tfc_tpg_np_cit */
  427. /* Start of tfc_tpg_port_cit */
  428. static struct se_lun *item_to_lun(struct config_item *item)
  429. {
  430. return container_of(to_config_group(item), struct se_lun,
  431. lun_group);
  432. }
  433. static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
  434. char *page)
  435. {
  436. struct se_lun *lun = item_to_lun(item);
  437. if (!lun || !lun->lun_se_dev)
  438. return -ENODEV;
  439. return core_alua_show_tg_pt_gp_info(lun, page);
  440. }
  441. static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
  442. const char *page, size_t count)
  443. {
  444. struct se_lun *lun = item_to_lun(item);
  445. if (!lun || !lun->lun_se_dev)
  446. return -ENODEV;
  447. return core_alua_store_tg_pt_gp_info(lun, page, count);
  448. }
  449. static ssize_t target_fabric_port_alua_tg_pt_offline_show(
  450. struct config_item *item, char *page)
  451. {
  452. struct se_lun *lun = item_to_lun(item);
  453. if (!lun || !lun->lun_se_dev)
  454. return -ENODEV;
  455. return core_alua_show_offline_bit(lun, page);
  456. }
  457. static ssize_t target_fabric_port_alua_tg_pt_offline_store(
  458. struct config_item *item, const char *page, size_t count)
  459. {
  460. struct se_lun *lun = item_to_lun(item);
  461. if (!lun || !lun->lun_se_dev)
  462. return -ENODEV;
  463. return core_alua_store_offline_bit(lun, page, count);
  464. }
  465. static ssize_t target_fabric_port_alua_tg_pt_status_show(
  466. struct config_item *item, char *page)
  467. {
  468. struct se_lun *lun = item_to_lun(item);
  469. if (!lun || !lun->lun_se_dev)
  470. return -ENODEV;
  471. return core_alua_show_secondary_status(lun, page);
  472. }
  473. static ssize_t target_fabric_port_alua_tg_pt_status_store(
  474. struct config_item *item, const char *page, size_t count)
  475. {
  476. struct se_lun *lun = item_to_lun(item);
  477. if (!lun || !lun->lun_se_dev)
  478. return -ENODEV;
  479. return core_alua_store_secondary_status(lun, page, count);
  480. }
  481. static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
  482. struct config_item *item, char *page)
  483. {
  484. struct se_lun *lun = item_to_lun(item);
  485. if (!lun || !lun->lun_se_dev)
  486. return -ENODEV;
  487. return core_alua_show_secondary_write_metadata(lun, page);
  488. }
  489. static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
  490. struct config_item *item, const char *page, size_t count)
  491. {
  492. struct se_lun *lun = item_to_lun(item);
  493. if (!lun || !lun->lun_se_dev)
  494. return -ENODEV;
  495. return core_alua_store_secondary_write_metadata(lun, page, count);
  496. }
  497. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
  498. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
  499. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
  500. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
  501. static struct configfs_attribute *target_fabric_port_attrs[] = {
  502. &target_fabric_port_attr_alua_tg_pt_gp,
  503. &target_fabric_port_attr_alua_tg_pt_offline,
  504. &target_fabric_port_attr_alua_tg_pt_status,
  505. &target_fabric_port_attr_alua_tg_pt_write_md,
  506. NULL,
  507. };
  508. static int target_fabric_port_link(
  509. struct config_item *lun_ci,
  510. struct config_item *se_dev_ci)
  511. {
  512. struct config_item *tpg_ci;
  513. struct se_lun *lun = container_of(to_config_group(lun_ci),
  514. struct se_lun, lun_group);
  515. struct se_portal_group *se_tpg;
  516. struct se_device *dev;
  517. struct target_fabric_configfs *tf;
  518. int ret;
  519. if (!se_dev_ci->ci_type ||
  520. se_dev_ci->ci_type->ct_item_ops != &target_core_dev_item_ops) {
  521. pr_err("Bad se_dev_ci, not a valid se_dev_ci pointer: %p\n", se_dev_ci);
  522. return -EFAULT;
  523. }
  524. dev = container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  525. if (!(dev->dev_flags & DF_CONFIGURED)) {
  526. pr_err("se_device not configured yet, cannot port link\n");
  527. return -ENODEV;
  528. }
  529. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  530. se_tpg = container_of(to_config_group(tpg_ci),
  531. struct se_portal_group, tpg_group);
  532. tf = se_tpg->se_tpg_wwn->wwn_tf;
  533. if (lun->lun_se_dev != NULL) {
  534. pr_err("Port Symlink already exists\n");
  535. return -EEXIST;
  536. }
  537. ret = core_dev_add_lun(se_tpg, dev, lun);
  538. if (ret) {
  539. pr_err("core_dev_add_lun() failed: %d\n", ret);
  540. goto out;
  541. }
  542. if (tf->tf_ops->fabric_post_link) {
  543. /*
  544. * Call the optional fabric_post_link() to allow a
  545. * fabric module to setup any additional state once
  546. * core_dev_add_lun() has been called..
  547. */
  548. tf->tf_ops->fabric_post_link(se_tpg, lun);
  549. }
  550. return 0;
  551. out:
  552. return ret;
  553. }
  554. static void target_fabric_port_unlink(
  555. struct config_item *lun_ci,
  556. struct config_item *se_dev_ci)
  557. {
  558. struct se_lun *lun = container_of(to_config_group(lun_ci),
  559. struct se_lun, lun_group);
  560. struct se_portal_group *se_tpg = lun->lun_tpg;
  561. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  562. if (tf->tf_ops->fabric_pre_unlink) {
  563. /*
  564. * Call the optional fabric_pre_unlink() to allow a
  565. * fabric module to release any additional stat before
  566. * core_dev_del_lun() is called.
  567. */
  568. tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
  569. }
  570. core_dev_del_lun(se_tpg, lun);
  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. }