target_core_fabric_configfs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. * Copyright (c) 2010 Rising Tide Systems
  8. * Copyright (c) 2010 Linux-iSCSI.org
  9. *
  10. * Copyright (c) 2010 Nicholas A. Bellinger <nab@linux-iscsi.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. ****************************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/version.h>
  25. #include <generated/utsrelease.h>
  26. #include <linux/utsname.h>
  27. #include <linux/init.h>
  28. #include <linux/fs.h>
  29. #include <linux/namei.h>
  30. #include <linux/slab.h>
  31. #include <linux/types.h>
  32. #include <linux/delay.h>
  33. #include <linux/unistd.h>
  34. #include <linux/string.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/configfs.h>
  37. #include <target/target_core_base.h>
  38. #include <target/target_core_device.h>
  39. #include <target/target_core_tpg.h>
  40. #include <target/target_core_transport.h>
  41. #include <target/target_core_fabric_ops.h>
  42. #include <target/target_core_fabric_configfs.h>
  43. #include <target/target_core_configfs.h>
  44. #include <target/configfs_macros.h>
  45. #include "target_core_alua.h"
  46. #include "target_core_hba.h"
  47. #include "target_core_pr.h"
  48. #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
  49. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  50. { \
  51. struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
  52. struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
  53. \
  54. cit->ct_item_ops = _item_ops; \
  55. cit->ct_group_ops = _group_ops; \
  56. cit->ct_attrs = _attrs; \
  57. cit->ct_owner = tf->tf_module; \
  58. printk("Setup generic %s\n", __stringify(_name)); \
  59. }
  60. /* Start of tfc_tpg_mappedlun_cit */
  61. static int target_fabric_mappedlun_link(
  62. struct config_item *lun_acl_ci,
  63. struct config_item *lun_ci)
  64. {
  65. struct se_dev_entry *deve;
  66. struct se_lun *lun = container_of(to_config_group(lun_ci),
  67. struct se_lun, lun_group);
  68. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  69. struct se_lun_acl, se_lun_group);
  70. struct se_portal_group *se_tpg;
  71. struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
  72. int ret = 0, lun_access;
  73. /*
  74. * Ensure that the source port exists
  75. */
  76. if (!(lun->lun_sep) || !(lun->lun_sep->sep_tpg)) {
  77. printk(KERN_ERR "Source se_lun->lun_sep or lun->lun_sep->sep"
  78. "_tpg does not exist\n");
  79. return -EINVAL;
  80. }
  81. se_tpg = lun->lun_sep->sep_tpg;
  82. nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
  83. tpg_ci = &nacl_ci->ci_group->cg_item;
  84. wwn_ci = &tpg_ci->ci_group->cg_item;
  85. tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
  86. wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
  87. /*
  88. * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
  89. */
  90. if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
  91. printk(KERN_ERR "Illegal Initiator ACL SymLink outside of %s\n",
  92. config_item_name(wwn_ci));
  93. return -EINVAL;
  94. }
  95. if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
  96. printk(KERN_ERR "Illegal Initiator ACL Symlink outside of %s"
  97. " TPGT: %s\n", config_item_name(wwn_ci),
  98. config_item_name(tpg_ci));
  99. return -EINVAL;
  100. }
  101. /*
  102. * If this struct se_node_acl was dynamically generated with
  103. * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
  104. * which be will write protected (READ-ONLY) when
  105. * tpg_1/attrib/demo_mode_write_protect=1
  106. */
  107. spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
  108. deve = &lacl->se_lun_nacl->device_list[lacl->mapped_lun];
  109. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
  110. lun_access = deve->lun_flags;
  111. else
  112. lun_access =
  113. (TPG_TFO(se_tpg)->tpg_check_prod_mode_write_protect(
  114. se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
  115. TRANSPORT_LUNFLAGS_READ_WRITE;
  116. spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
  117. /*
  118. * Determine the actual mapped LUN value user wants..
  119. *
  120. * This value is what the SCSI Initiator actually sees the
  121. * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
  122. */
  123. ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
  124. lun->unpacked_lun, lun_access);
  125. return (ret < 0) ? -EINVAL : 0;
  126. }
  127. static int target_fabric_mappedlun_unlink(
  128. struct config_item *lun_acl_ci,
  129. struct config_item *lun_ci)
  130. {
  131. struct se_lun *lun;
  132. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  133. struct se_lun_acl, se_lun_group);
  134. struct se_node_acl *nacl = lacl->se_lun_nacl;
  135. struct se_dev_entry *deve = &nacl->device_list[lacl->mapped_lun];
  136. struct se_portal_group *se_tpg;
  137. /*
  138. * Determine if the underlying MappedLUN has already been released..
  139. */
  140. if (!(deve->se_lun))
  141. return 0;
  142. lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
  143. se_tpg = lun->lun_sep->sep_tpg;
  144. core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
  145. return 0;
  146. }
  147. CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
  148. #define TCM_MAPPEDLUN_ATTR(_name, _mode) \
  149. static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
  150. __CONFIGFS_EATTR(_name, _mode, \
  151. target_fabric_mappedlun_show_##_name, \
  152. target_fabric_mappedlun_store_##_name);
  153. static ssize_t target_fabric_mappedlun_show_write_protect(
  154. struct se_lun_acl *lacl,
  155. char *page)
  156. {
  157. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  158. struct se_dev_entry *deve;
  159. ssize_t len;
  160. spin_lock_irq(&se_nacl->device_list_lock);
  161. deve = &se_nacl->device_list[lacl->mapped_lun];
  162. len = sprintf(page, "%d\n",
  163. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
  164. 1 : 0);
  165. spin_unlock_irq(&se_nacl->device_list_lock);
  166. return len;
  167. }
  168. static ssize_t target_fabric_mappedlun_store_write_protect(
  169. struct se_lun_acl *lacl,
  170. const char *page,
  171. size_t count)
  172. {
  173. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  174. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  175. unsigned long op;
  176. if (strict_strtoul(page, 0, &op))
  177. return -EINVAL;
  178. if ((op != 1) && (op != 0))
  179. return -EINVAL;
  180. core_update_device_list_access(lacl->mapped_lun, (op) ?
  181. TRANSPORT_LUNFLAGS_READ_ONLY :
  182. TRANSPORT_LUNFLAGS_READ_WRITE,
  183. lacl->se_lun_nacl);
  184. printk(KERN_INFO "%s_ConfigFS: Changed Initiator ACL: %s"
  185. " Mapped LUN: %u Write Protect bit to %s\n",
  186. TPG_TFO(se_tpg)->get_fabric_name(),
  187. lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
  188. return count;
  189. }
  190. TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
  191. CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
  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_attribute *target_fabric_mappedlun_attrs[] = {
  200. &target_fabric_mappedlun_write_protect.attr,
  201. NULL,
  202. };
  203. static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
  204. .release = target_fabric_mappedlun_release,
  205. .show_attribute = target_fabric_mappedlun_attr_show,
  206. .store_attribute = target_fabric_mappedlun_attr_store,
  207. .allow_link = target_fabric_mappedlun_link,
  208. .drop_link = target_fabric_mappedlun_unlink,
  209. };
  210. TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  211. target_fabric_mappedlun_attrs);
  212. /* End of tfc_tpg_mappedlun_cit */
  213. /* Start of tfc_tpg_nacl_attrib_cit */
  214. CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
  215. static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
  216. .show_attribute = target_fabric_nacl_attrib_attr_show,
  217. .store_attribute = target_fabric_nacl_attrib_attr_store,
  218. };
  219. TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
  220. /* End of tfc_tpg_nacl_attrib_cit */
  221. /* Start of tfc_tpg_nacl_auth_cit */
  222. CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
  223. static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
  224. .show_attribute = target_fabric_nacl_auth_attr_show,
  225. .store_attribute = target_fabric_nacl_auth_attr_store,
  226. };
  227. TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
  228. /* End of tfc_tpg_nacl_auth_cit */
  229. /* Start of tfc_tpg_nacl_param_cit */
  230. CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
  231. static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
  232. .show_attribute = target_fabric_nacl_param_attr_show,
  233. .store_attribute = target_fabric_nacl_param_attr_store,
  234. };
  235. TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
  236. /* End of tfc_tpg_nacl_param_cit */
  237. /* Start of tfc_tpg_nacl_base_cit */
  238. CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
  239. static struct config_group *target_fabric_make_mappedlun(
  240. struct config_group *group,
  241. const char *name)
  242. {
  243. struct se_node_acl *se_nacl = container_of(group,
  244. struct se_node_acl, acl_group);
  245. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  246. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  247. struct se_lun_acl *lacl;
  248. struct config_item *acl_ci;
  249. char *buf;
  250. unsigned long mapped_lun;
  251. int ret = 0;
  252. acl_ci = &group->cg_item;
  253. if (!(acl_ci)) {
  254. printk(KERN_ERR "Unable to locatel acl_ci\n");
  255. return NULL;
  256. }
  257. buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
  258. if (!(buf)) {
  259. printk(KERN_ERR "Unable to allocate memory for name buf\n");
  260. return ERR_PTR(-ENOMEM);
  261. }
  262. snprintf(buf, strlen(name) + 1, "%s", name);
  263. /*
  264. * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  265. */
  266. if (strstr(buf, "lun_") != buf) {
  267. printk(KERN_ERR "Unable to locate \"lun_\" from buf: %s"
  268. " name: %s\n", buf, name);
  269. ret = -EINVAL;
  270. goto out;
  271. }
  272. /*
  273. * Determine the Mapped LUN value. This is what the SCSI Initiator
  274. * Port will actually see.
  275. */
  276. if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
  277. ret = -EINVAL;
  278. goto out;
  279. }
  280. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
  281. config_item_name(acl_ci), &ret);
  282. if (!(lacl))
  283. goto out;
  284. config_group_init_type_name(&lacl->se_lun_group, name,
  285. &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
  286. kfree(buf);
  287. return &lacl->se_lun_group;
  288. out:
  289. kfree(buf);
  290. return ERR_PTR(ret);
  291. }
  292. static void target_fabric_drop_mappedlun(
  293. struct config_group *group,
  294. struct config_item *item)
  295. {
  296. config_item_put(item);
  297. }
  298. static void target_fabric_nacl_base_release(struct config_item *item)
  299. {
  300. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  301. struct se_node_acl, acl_group);
  302. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  303. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  304. tf->tf_ops.fabric_drop_nodeacl(se_nacl);
  305. }
  306. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  307. .release = target_fabric_nacl_base_release,
  308. .show_attribute = target_fabric_nacl_base_attr_show,
  309. .store_attribute = target_fabric_nacl_base_attr_store,
  310. };
  311. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  312. .make_group = target_fabric_make_mappedlun,
  313. .drop_item = target_fabric_drop_mappedlun,
  314. };
  315. TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  316. &target_fabric_nacl_base_group_ops, NULL);
  317. /* End of tfc_tpg_nacl_base_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. struct config_group *nacl_cg;
  328. if (!(tf->tf_ops.fabric_make_nodeacl)) {
  329. printk(KERN_ERR "tf->tf_ops.fabric_make_nodeacl is NULL\n");
  330. return ERR_PTR(-ENOSYS);
  331. }
  332. se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
  333. if (IS_ERR(se_nacl))
  334. return ERR_PTR(PTR_ERR(se_nacl));
  335. nacl_cg = &se_nacl->acl_group;
  336. nacl_cg->default_groups = se_nacl->acl_default_groups;
  337. nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
  338. nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
  339. nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
  340. nacl_cg->default_groups[3] = NULL;
  341. config_group_init_type_name(&se_nacl->acl_group, name,
  342. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
  343. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  344. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
  345. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  346. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
  347. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  348. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
  349. return &se_nacl->acl_group;
  350. }
  351. static void target_fabric_drop_nodeacl(
  352. struct config_group *group,
  353. struct config_item *item)
  354. {
  355. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  356. struct se_node_acl, acl_group);
  357. struct config_item *df_item;
  358. struct config_group *nacl_cg;
  359. int i;
  360. nacl_cg = &se_nacl->acl_group;
  361. for (i = 0; nacl_cg->default_groups[i]; i++) {
  362. df_item = &nacl_cg->default_groups[i]->cg_item;
  363. nacl_cg->default_groups[i] = NULL;
  364. config_item_put(df_item);
  365. }
  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. CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
  379. static void target_fabric_np_base_release(struct config_item *item)
  380. {
  381. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  382. struct se_tpg_np, tpg_np_group);
  383. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  384. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  385. tf->tf_ops.fabric_drop_np(se_tpg_np);
  386. }
  387. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  388. .release = target_fabric_np_base_release,
  389. .show_attribute = target_fabric_np_base_attr_show,
  390. .store_attribute = target_fabric_np_base_attr_store,
  391. };
  392. TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
  393. /* End of tfc_tpg_np_base_cit */
  394. /* Start of tfc_tpg_np_cit */
  395. static struct config_group *target_fabric_make_np(
  396. struct config_group *group,
  397. const char *name)
  398. {
  399. struct se_portal_group *se_tpg = container_of(group,
  400. struct se_portal_group, tpg_np_group);
  401. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  402. struct se_tpg_np *se_tpg_np;
  403. if (!(tf->tf_ops.fabric_make_np)) {
  404. printk(KERN_ERR "tf->tf_ops.fabric_make_np is NULL\n");
  405. return ERR_PTR(-ENOSYS);
  406. }
  407. se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
  408. if (!(se_tpg_np) || IS_ERR(se_tpg_np))
  409. return ERR_PTR(-EINVAL);
  410. se_tpg_np->tpg_np_parent = se_tpg;
  411. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  412. &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
  413. return &se_tpg_np->tpg_np_group;
  414. }
  415. static void target_fabric_drop_np(
  416. struct config_group *group,
  417. struct config_item *item)
  418. {
  419. /*
  420. * struct se_tpg_np is released via target_fabric_np_base_release()
  421. */
  422. config_item_put(item);
  423. }
  424. static struct configfs_group_operations target_fabric_np_group_ops = {
  425. .make_group = &target_fabric_make_np,
  426. .drop_item = &target_fabric_drop_np,
  427. };
  428. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  429. /* End of tfc_tpg_np_cit */
  430. /* Start of tfc_tpg_port_cit */
  431. CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
  432. #define TCM_PORT_ATTR(_name, _mode) \
  433. static struct target_fabric_port_attribute target_fabric_port_##_name = \
  434. __CONFIGFS_EATTR(_name, _mode, \
  435. target_fabric_port_show_attr_##_name, \
  436. target_fabric_port_store_attr_##_name);
  437. #define TCM_PORT_ATTOR_RO(_name) \
  438. __CONFIGFS_EATTR_RO(_name, \
  439. target_fabric_port_show_attr_##_name);
  440. /*
  441. * alua_tg_pt_gp
  442. */
  443. static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
  444. struct se_lun *lun,
  445. char *page)
  446. {
  447. if (!(lun))
  448. return -ENODEV;
  449. if (!(lun->lun_sep))
  450. return -ENODEV;
  451. return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
  452. }
  453. static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
  454. struct se_lun *lun,
  455. const char *page,
  456. size_t count)
  457. {
  458. if (!(lun))
  459. return -ENODEV;
  460. if (!(lun->lun_sep))
  461. return -ENODEV;
  462. return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
  463. }
  464. TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
  465. /*
  466. * alua_tg_pt_offline
  467. */
  468. static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
  469. struct se_lun *lun,
  470. char *page)
  471. {
  472. if (!(lun))
  473. return -ENODEV;
  474. if (!(lun->lun_sep))
  475. return -ENODEV;
  476. return core_alua_show_offline_bit(lun, page);
  477. }
  478. static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
  479. struct se_lun *lun,
  480. const char *page,
  481. size_t count)
  482. {
  483. if (!(lun))
  484. return -ENODEV;
  485. if (!(lun->lun_sep))
  486. return -ENODEV;
  487. return core_alua_store_offline_bit(lun, page, count);
  488. }
  489. TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
  490. /*
  491. * alua_tg_pt_status
  492. */
  493. static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
  494. struct se_lun *lun,
  495. char *page)
  496. {
  497. if (!(lun))
  498. return -ENODEV;
  499. if (!(lun->lun_sep))
  500. return -ENODEV;
  501. return core_alua_show_secondary_status(lun, page);
  502. }
  503. static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
  504. struct se_lun *lun,
  505. const char *page,
  506. size_t count)
  507. {
  508. if (!(lun))
  509. return -ENODEV;
  510. if (!(lun->lun_sep))
  511. return -ENODEV;
  512. return core_alua_store_secondary_status(lun, page, count);
  513. }
  514. TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
  515. /*
  516. * alua_tg_pt_write_md
  517. */
  518. static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
  519. struct se_lun *lun,
  520. char *page)
  521. {
  522. if (!(lun))
  523. return -ENODEV;
  524. if (!(lun->lun_sep))
  525. return -ENODEV;
  526. return core_alua_show_secondary_write_metadata(lun, page);
  527. }
  528. static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
  529. struct se_lun *lun,
  530. const char *page,
  531. size_t count)
  532. {
  533. if (!(lun))
  534. return -ENODEV;
  535. if (!(lun->lun_sep))
  536. return -ENODEV;
  537. return core_alua_store_secondary_write_metadata(lun, page, count);
  538. }
  539. TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
  540. static struct configfs_attribute *target_fabric_port_attrs[] = {
  541. &target_fabric_port_alua_tg_pt_gp.attr,
  542. &target_fabric_port_alua_tg_pt_offline.attr,
  543. &target_fabric_port_alua_tg_pt_status.attr,
  544. &target_fabric_port_alua_tg_pt_write_md.attr,
  545. NULL,
  546. };
  547. CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
  548. static int target_fabric_port_link(
  549. struct config_item *lun_ci,
  550. struct config_item *se_dev_ci)
  551. {
  552. struct config_item *tpg_ci;
  553. struct se_device *dev;
  554. struct se_lun *lun = container_of(to_config_group(lun_ci),
  555. struct se_lun, lun_group);
  556. struct se_lun *lun_p;
  557. struct se_portal_group *se_tpg;
  558. struct se_subsystem_dev *se_dev = container_of(
  559. to_config_group(se_dev_ci), struct se_subsystem_dev,
  560. se_dev_group);
  561. struct target_fabric_configfs *tf;
  562. int ret;
  563. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  564. se_tpg = container_of(to_config_group(tpg_ci),
  565. struct se_portal_group, tpg_group);
  566. tf = se_tpg->se_tpg_wwn->wwn_tf;
  567. if (lun->lun_se_dev != NULL) {
  568. printk(KERN_ERR "Port Symlink already exists\n");
  569. return -EEXIST;
  570. }
  571. dev = se_dev->se_dev_ptr;
  572. if (!(dev)) {
  573. printk(KERN_ERR "Unable to locate struct se_device pointer from"
  574. " %s\n", config_item_name(se_dev_ci));
  575. ret = -ENODEV;
  576. goto out;
  577. }
  578. lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
  579. lun->unpacked_lun);
  580. if ((IS_ERR(lun_p)) || !(lun_p)) {
  581. printk(KERN_ERR "core_dev_add_lun() failed\n");
  582. ret = -EINVAL;
  583. goto out;
  584. }
  585. if (tf->tf_ops.fabric_post_link) {
  586. /*
  587. * Call the optional fabric_post_link() to allow a
  588. * fabric module to setup any additional state once
  589. * core_dev_add_lun() has been called..
  590. */
  591. tf->tf_ops.fabric_post_link(se_tpg, lun);
  592. }
  593. return 0;
  594. out:
  595. return ret;
  596. }
  597. static int target_fabric_port_unlink(
  598. struct config_item *lun_ci,
  599. struct config_item *se_dev_ci)
  600. {
  601. struct se_lun *lun = container_of(to_config_group(lun_ci),
  602. struct se_lun, lun_group);
  603. struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
  604. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  605. if (tf->tf_ops.fabric_pre_unlink) {
  606. /*
  607. * Call the optional fabric_pre_unlink() to allow a
  608. * fabric module to release any additional stat before
  609. * core_dev_del_lun() is called.
  610. */
  611. tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
  612. }
  613. core_dev_del_lun(se_tpg, lun->unpacked_lun);
  614. return 0;
  615. }
  616. static struct configfs_item_operations target_fabric_port_item_ops = {
  617. .show_attribute = target_fabric_port_attr_show,
  618. .store_attribute = target_fabric_port_attr_store,
  619. .allow_link = target_fabric_port_link,
  620. .drop_link = target_fabric_port_unlink,
  621. };
  622. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  623. /* End of tfc_tpg_port_cit */
  624. /* Start of tfc_tpg_lun_cit */
  625. static struct config_group *target_fabric_make_lun(
  626. struct config_group *group,
  627. const char *name)
  628. {
  629. struct se_lun *lun;
  630. struct se_portal_group *se_tpg = container_of(group,
  631. struct se_portal_group, tpg_lun_group);
  632. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  633. unsigned long unpacked_lun;
  634. if (strstr(name, "lun_") != name) {
  635. printk(KERN_ERR "Unable to locate \'_\" in"
  636. " \"lun_$LUN_NUMBER\"\n");
  637. return ERR_PTR(-EINVAL);
  638. }
  639. if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
  640. return ERR_PTR(-EINVAL);
  641. lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
  642. if (!(lun))
  643. return ERR_PTR(-EINVAL);
  644. config_group_init_type_name(&lun->lun_group, name,
  645. &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
  646. return &lun->lun_group;
  647. }
  648. static void target_fabric_drop_lun(
  649. struct config_group *group,
  650. struct config_item *item)
  651. {
  652. config_item_put(item);
  653. }
  654. static struct configfs_group_operations target_fabric_lun_group_ops = {
  655. .make_group = &target_fabric_make_lun,
  656. .drop_item = &target_fabric_drop_lun,
  657. };
  658. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  659. /* End of tfc_tpg_lun_cit */
  660. /* Start of tfc_tpg_attrib_cit */
  661. CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
  662. static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
  663. .show_attribute = target_fabric_tpg_attrib_attr_show,
  664. .store_attribute = target_fabric_tpg_attrib_attr_store,
  665. };
  666. TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
  667. /* End of tfc_tpg_attrib_cit */
  668. /* Start of tfc_tpg_param_cit */
  669. CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
  670. static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
  671. .show_attribute = target_fabric_tpg_param_attr_show,
  672. .store_attribute = target_fabric_tpg_param_attr_store,
  673. };
  674. TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
  675. /* End of tfc_tpg_param_cit */
  676. /* Start of tfc_tpg_base_cit */
  677. /*
  678. * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
  679. */
  680. CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
  681. static void target_fabric_tpg_release(struct config_item *item)
  682. {
  683. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  684. struct se_portal_group, tpg_group);
  685. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  686. struct target_fabric_configfs *tf = wwn->wwn_tf;
  687. tf->tf_ops.fabric_drop_tpg(se_tpg);
  688. }
  689. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  690. .release = target_fabric_tpg_release,
  691. .show_attribute = target_fabric_tpg_attr_show,
  692. .store_attribute = target_fabric_tpg_attr_store,
  693. };
  694. TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
  695. /* End of tfc_tpg_base_cit */
  696. /* Start of tfc_tpg_cit */
  697. static struct config_group *target_fabric_make_tpg(
  698. struct config_group *group,
  699. const char *name)
  700. {
  701. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  702. struct target_fabric_configfs *tf = wwn->wwn_tf;
  703. struct se_portal_group *se_tpg;
  704. if (!(tf->tf_ops.fabric_make_tpg)) {
  705. printk(KERN_ERR "tf->tf_ops.fabric_make_tpg is NULL\n");
  706. return ERR_PTR(-ENOSYS);
  707. }
  708. se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
  709. if (!(se_tpg) || IS_ERR(se_tpg))
  710. return ERR_PTR(-EINVAL);
  711. /*
  712. * Setup default groups from pre-allocated se_tpg->tpg_default_groups
  713. */
  714. se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
  715. se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
  716. se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
  717. se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
  718. se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
  719. se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
  720. se_tpg->tpg_group.default_groups[5] = NULL;
  721. config_group_init_type_name(&se_tpg->tpg_group, name,
  722. &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
  723. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  724. &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
  725. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  726. &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
  727. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  728. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
  729. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  730. &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
  731. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  732. &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
  733. return &se_tpg->tpg_group;
  734. }
  735. static void target_fabric_drop_tpg(
  736. struct config_group *group,
  737. struct config_item *item)
  738. {
  739. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  740. struct se_portal_group, tpg_group);
  741. struct config_group *tpg_cg = &se_tpg->tpg_group;
  742. struct config_item *df_item;
  743. int i;
  744. /*
  745. * Release default groups, but do not release tpg_cg->default_groups
  746. * memory as it is statically allocated at se_tpg->tpg_default_groups.
  747. */
  748. for (i = 0; tpg_cg->default_groups[i]; i++) {
  749. df_item = &tpg_cg->default_groups[i]->cg_item;
  750. tpg_cg->default_groups[i] = NULL;
  751. config_item_put(df_item);
  752. }
  753. config_item_put(item);
  754. }
  755. static void target_fabric_release_wwn(struct config_item *item)
  756. {
  757. struct se_wwn *wwn = container_of(to_config_group(item),
  758. struct se_wwn, wwn_group);
  759. struct target_fabric_configfs *tf = wwn->wwn_tf;
  760. tf->tf_ops.fabric_drop_wwn(wwn);
  761. }
  762. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  763. .release = target_fabric_release_wwn,
  764. };
  765. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  766. .make_group = target_fabric_make_tpg,
  767. .drop_item = target_fabric_drop_tpg,
  768. };
  769. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  770. NULL);
  771. /* End of tfc_tpg_cit */
  772. /* Start of tfc_wwn_cit */
  773. static struct config_group *target_fabric_make_wwn(
  774. struct config_group *group,
  775. const char *name)
  776. {
  777. struct target_fabric_configfs *tf = container_of(group,
  778. struct target_fabric_configfs, tf_group);
  779. struct se_wwn *wwn;
  780. if (!(tf->tf_ops.fabric_make_wwn)) {
  781. printk(KERN_ERR "tf->tf_ops.fabric_make_wwn is NULL\n");
  782. return ERR_PTR(-ENOSYS);
  783. }
  784. wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
  785. if (!(wwn) || IS_ERR(wwn))
  786. return ERR_PTR(-EINVAL);
  787. wwn->wwn_tf = tf;
  788. config_group_init_type_name(&wwn->wwn_group, name,
  789. &TF_CIT_TMPL(tf)->tfc_tpg_cit);
  790. return &wwn->wwn_group;
  791. }
  792. static void target_fabric_drop_wwn(
  793. struct config_group *group,
  794. struct config_item *item)
  795. {
  796. config_item_put(item);
  797. }
  798. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  799. .make_group = target_fabric_make_wwn,
  800. .drop_item = target_fabric_drop_wwn,
  801. };
  802. /*
  803. * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
  804. */
  805. CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
  806. static struct configfs_item_operations target_fabric_wwn_item_ops = {
  807. .show_attribute = target_fabric_wwn_attr_show,
  808. .store_attribute = target_fabric_wwn_attr_store,
  809. };
  810. TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
  811. /* End of tfc_wwn_cit */
  812. /* Start of tfc_discovery_cit */
  813. CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
  814. tf_disc_group);
  815. static struct configfs_item_operations target_fabric_discovery_item_ops = {
  816. .show_attribute = target_fabric_discovery_attr_show,
  817. .store_attribute = target_fabric_discovery_attr_store,
  818. };
  819. TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
  820. /* End of tfc_discovery_cit */
  821. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  822. {
  823. target_fabric_setup_discovery_cit(tf);
  824. target_fabric_setup_wwn_cit(tf);
  825. target_fabric_setup_tpg_cit(tf);
  826. target_fabric_setup_tpg_base_cit(tf);
  827. target_fabric_setup_tpg_port_cit(tf);
  828. target_fabric_setup_tpg_lun_cit(tf);
  829. target_fabric_setup_tpg_np_cit(tf);
  830. target_fabric_setup_tpg_np_base_cit(tf);
  831. target_fabric_setup_tpg_attrib_cit(tf);
  832. target_fabric_setup_tpg_param_cit(tf);
  833. target_fabric_setup_tpg_nacl_cit(tf);
  834. target_fabric_setup_tpg_nacl_base_cit(tf);
  835. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  836. target_fabric_setup_tpg_nacl_auth_cit(tf);
  837. target_fabric_setup_tpg_nacl_param_cit(tf);
  838. target_fabric_setup_tpg_mappedlun_cit(tf);
  839. return 0;
  840. }