target_core_fabric_configfs.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. int lun_access;
  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 deve->lun_flags,
  115. * which be will write protected (READ-ONLY) 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 = deve->lun_flags;
  122. else
  123. lun_access =
  124. (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
  125. se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
  126. TRANSPORT_LUNFLAGS_READ_WRITE;
  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);
  135. }
  136. static int 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. return 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",
  162. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
  163. }
  164. rcu_read_unlock();
  165. return len;
  166. }
  167. static ssize_t target_fabric_mappedlun_write_protect_store(
  168. struct config_item *item, const char *page, size_t count)
  169. {
  170. struct se_lun_acl *lacl = item_to_lun_acl(item);
  171. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  172. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  173. unsigned long op;
  174. int ret;
  175. ret = kstrtoul(page, 0, &op);
  176. if (ret)
  177. return ret;
  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. pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
  185. " Mapped LUN: %llu Write Protect bit to %s\n",
  186. se_tpg->se_tpg_tfo->get_fabric_name(),
  187. se_nacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
  188. return count;
  189. }
  190. CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
  191. static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
  192. &target_fabric_mappedlun_attr_write_protect,
  193. NULL,
  194. };
  195. static void target_fabric_mappedlun_release(struct config_item *item)
  196. {
  197. struct se_lun_acl *lacl = container_of(to_config_group(item),
  198. struct se_lun_acl, se_lun_group);
  199. struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
  200. core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
  201. }
  202. static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
  203. .release = target_fabric_mappedlun_release,
  204. .allow_link = target_fabric_mappedlun_link,
  205. .drop_link = target_fabric_mappedlun_unlink,
  206. };
  207. TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  208. target_fabric_mappedlun_attrs);
  209. /* End of tfc_tpg_mappedlun_cit */
  210. /* Start of tfc_tpg_mappedlun_port_cit */
  211. static struct config_group *target_core_mappedlun_stat_mkdir(
  212. struct config_group *group,
  213. const char *name)
  214. {
  215. return ERR_PTR(-ENOSYS);
  216. }
  217. static void target_core_mappedlun_stat_rmdir(
  218. struct config_group *group,
  219. struct config_item *item)
  220. {
  221. return;
  222. }
  223. static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
  224. .make_group = target_core_mappedlun_stat_mkdir,
  225. .drop_item = target_core_mappedlun_stat_rmdir,
  226. };
  227. TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
  228. NULL);
  229. /* End of tfc_tpg_mappedlun_port_cit */
  230. TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
  231. TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
  232. TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
  233. /* Start of tfc_tpg_nacl_base_cit */
  234. static struct config_group *target_fabric_make_mappedlun(
  235. struct config_group *group,
  236. const char *name)
  237. {
  238. struct se_node_acl *se_nacl = container_of(group,
  239. struct se_node_acl, acl_group);
  240. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  241. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  242. struct se_lun_acl *lacl = NULL;
  243. struct config_item *acl_ci;
  244. struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  245. char *buf;
  246. unsigned long long mapped_lun;
  247. int ret = 0;
  248. acl_ci = &group->cg_item;
  249. if (!acl_ci) {
  250. pr_err("Unable to locatel acl_ci\n");
  251. return NULL;
  252. }
  253. buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
  254. if (!buf) {
  255. pr_err("Unable to allocate memory for name buf\n");
  256. return ERR_PTR(-ENOMEM);
  257. }
  258. snprintf(buf, strlen(name) + 1, "%s", name);
  259. /*
  260. * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  261. */
  262. if (strstr(buf, "lun_") != buf) {
  263. pr_err("Unable to locate \"lun_\" from buf: %s"
  264. " name: %s\n", buf, name);
  265. ret = -EINVAL;
  266. goto out;
  267. }
  268. /*
  269. * Determine the Mapped LUN value. This is what the SCSI Initiator
  270. * Port will actually see.
  271. */
  272. ret = kstrtoull(buf + 4, 0, &mapped_lun);
  273. if (ret)
  274. goto out;
  275. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
  276. mapped_lun, &ret);
  277. if (!lacl) {
  278. ret = -EINVAL;
  279. goto out;
  280. }
  281. lacl_cg = &lacl->se_lun_group;
  282. lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  283. GFP_KERNEL);
  284. if (!lacl_cg->default_groups) {
  285. pr_err("Unable to allocate lacl_cg->default_groups\n");
  286. ret = -ENOMEM;
  287. goto out;
  288. }
  289. config_group_init_type_name(&lacl->se_lun_group, name,
  290. &tf->tf_tpg_mappedlun_cit);
  291. config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  292. "statistics", &tf->tf_tpg_mappedlun_stat_cit);
  293. lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
  294. lacl_cg->default_groups[1] = NULL;
  295. ml_stat_grp = &lacl->ml_stat_grps.stat_group;
  296. ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
  297. GFP_KERNEL);
  298. if (!ml_stat_grp->default_groups) {
  299. pr_err("Unable to allocate ml_stat_grp->default_groups\n");
  300. ret = -ENOMEM;
  301. goto out;
  302. }
  303. target_stat_setup_mappedlun_default_groups(lacl);
  304. kfree(buf);
  305. return &lacl->se_lun_group;
  306. out:
  307. if (lacl_cg)
  308. kfree(lacl_cg->default_groups);
  309. kfree(lacl);
  310. kfree(buf);
  311. return ERR_PTR(ret);
  312. }
  313. static void target_fabric_drop_mappedlun(
  314. struct config_group *group,
  315. struct config_item *item)
  316. {
  317. struct se_lun_acl *lacl = container_of(to_config_group(item),
  318. struct se_lun_acl, se_lun_group);
  319. struct config_item *df_item;
  320. struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
  321. int i;
  322. ml_stat_grp = &lacl->ml_stat_grps.stat_group;
  323. for (i = 0; ml_stat_grp->default_groups[i]; i++) {
  324. df_item = &ml_stat_grp->default_groups[i]->cg_item;
  325. ml_stat_grp->default_groups[i] = NULL;
  326. config_item_put(df_item);
  327. }
  328. kfree(ml_stat_grp->default_groups);
  329. lacl_cg = &lacl->se_lun_group;
  330. for (i = 0; lacl_cg->default_groups[i]; i++) {
  331. df_item = &lacl_cg->default_groups[i]->cg_item;
  332. lacl_cg->default_groups[i] = NULL;
  333. config_item_put(df_item);
  334. }
  335. kfree(lacl_cg->default_groups);
  336. config_item_put(item);
  337. }
  338. static void target_fabric_nacl_base_release(struct config_item *item)
  339. {
  340. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  341. struct se_node_acl, acl_group);
  342. struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
  343. if (tf->tf_ops->fabric_cleanup_nodeacl)
  344. tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
  345. core_tpg_del_initiator_node_acl(se_nacl);
  346. }
  347. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  348. .release = target_fabric_nacl_base_release,
  349. };
  350. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  351. .make_group = target_fabric_make_mappedlun,
  352. .drop_item = target_fabric_drop_mappedlun,
  353. };
  354. TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  355. &target_fabric_nacl_base_group_ops);
  356. /* End of tfc_tpg_nacl_base_cit */
  357. /* Start of tfc_node_fabric_stats_cit */
  358. /*
  359. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  360. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  361. */
  362. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  363. /* End of tfc_wwn_fabric_stats_cit */
  364. /* Start of tfc_tpg_nacl_cit */
  365. static struct config_group *target_fabric_make_nodeacl(
  366. struct config_group *group,
  367. const char *name)
  368. {
  369. struct se_portal_group *se_tpg = container_of(group,
  370. struct se_portal_group, tpg_acl_group);
  371. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  372. struct se_node_acl *se_nacl;
  373. struct config_group *nacl_cg;
  374. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
  375. if (IS_ERR(se_nacl))
  376. return ERR_CAST(se_nacl);
  377. if (tf->tf_ops->fabric_init_nodeacl) {
  378. int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
  379. if (ret) {
  380. core_tpg_del_initiator_node_acl(se_nacl);
  381. return ERR_PTR(ret);
  382. }
  383. }
  384. nacl_cg = &se_nacl->acl_group;
  385. nacl_cg->default_groups = se_nacl->acl_default_groups;
  386. nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
  387. nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
  388. nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
  389. nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
  390. nacl_cg->default_groups[4] = NULL;
  391. config_group_init_type_name(&se_nacl->acl_group, name,
  392. &tf->tf_tpg_nacl_base_cit);
  393. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  394. &tf->tf_tpg_nacl_attrib_cit);
  395. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  396. &tf->tf_tpg_nacl_auth_cit);
  397. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  398. &tf->tf_tpg_nacl_param_cit);
  399. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  400. "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
  401. return &se_nacl->acl_group;
  402. }
  403. static void target_fabric_drop_nodeacl(
  404. struct config_group *group,
  405. struct config_item *item)
  406. {
  407. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  408. struct se_node_acl, acl_group);
  409. struct config_item *df_item;
  410. struct config_group *nacl_cg;
  411. int i;
  412. nacl_cg = &se_nacl->acl_group;
  413. for (i = 0; nacl_cg->default_groups[i]; i++) {
  414. df_item = &nacl_cg->default_groups[i]->cg_item;
  415. nacl_cg->default_groups[i] = NULL;
  416. config_item_put(df_item);
  417. }
  418. /*
  419. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  420. */
  421. config_item_put(item);
  422. }
  423. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  424. .make_group = target_fabric_make_nodeacl,
  425. .drop_item = target_fabric_drop_nodeacl,
  426. };
  427. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  428. /* End of tfc_tpg_nacl_cit */
  429. /* Start of tfc_tpg_np_base_cit */
  430. static void target_fabric_np_base_release(struct config_item *item)
  431. {
  432. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  433. struct se_tpg_np, tpg_np_group);
  434. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  435. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  436. tf->tf_ops->fabric_drop_np(se_tpg_np);
  437. }
  438. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  439. .release = target_fabric_np_base_release,
  440. };
  441. TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
  442. /* End of tfc_tpg_np_base_cit */
  443. /* Start of tfc_tpg_np_cit */
  444. static struct config_group *target_fabric_make_np(
  445. struct config_group *group,
  446. const char *name)
  447. {
  448. struct se_portal_group *se_tpg = container_of(group,
  449. struct se_portal_group, tpg_np_group);
  450. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  451. struct se_tpg_np *se_tpg_np;
  452. if (!tf->tf_ops->fabric_make_np) {
  453. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  454. return ERR_PTR(-ENOSYS);
  455. }
  456. se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
  457. if (!se_tpg_np || IS_ERR(se_tpg_np))
  458. return ERR_PTR(-EINVAL);
  459. se_tpg_np->tpg_np_parent = se_tpg;
  460. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  461. &tf->tf_tpg_np_base_cit);
  462. return &se_tpg_np->tpg_np_group;
  463. }
  464. static void target_fabric_drop_np(
  465. struct config_group *group,
  466. struct config_item *item)
  467. {
  468. /*
  469. * struct se_tpg_np is released via target_fabric_np_base_release()
  470. */
  471. config_item_put(item);
  472. }
  473. static struct configfs_group_operations target_fabric_np_group_ops = {
  474. .make_group = &target_fabric_make_np,
  475. .drop_item = &target_fabric_drop_np,
  476. };
  477. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  478. /* End of tfc_tpg_np_cit */
  479. /* Start of tfc_tpg_port_cit */
  480. static struct se_lun *item_to_lun(struct config_item *item)
  481. {
  482. return container_of(to_config_group(item), struct se_lun,
  483. lun_group);
  484. }
  485. static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
  486. char *page)
  487. {
  488. struct se_lun *lun = item_to_lun(item);
  489. if (!lun || !lun->lun_se_dev)
  490. return -ENODEV;
  491. return core_alua_show_tg_pt_gp_info(lun, page);
  492. }
  493. static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
  494. const char *page, size_t count)
  495. {
  496. struct se_lun *lun = item_to_lun(item);
  497. if (!lun || !lun->lun_se_dev)
  498. return -ENODEV;
  499. return core_alua_store_tg_pt_gp_info(lun, page, count);
  500. }
  501. static ssize_t target_fabric_port_alua_tg_pt_offline_show(
  502. struct config_item *item, char *page)
  503. {
  504. struct se_lun *lun = item_to_lun(item);
  505. if (!lun || !lun->lun_se_dev)
  506. return -ENODEV;
  507. return core_alua_show_offline_bit(lun, page);
  508. }
  509. static ssize_t target_fabric_port_alua_tg_pt_offline_store(
  510. struct config_item *item, const char *page, size_t count)
  511. {
  512. struct se_lun *lun = item_to_lun(item);
  513. if (!lun || !lun->lun_se_dev)
  514. return -ENODEV;
  515. return core_alua_store_offline_bit(lun, page, count);
  516. }
  517. static ssize_t target_fabric_port_alua_tg_pt_status_show(
  518. struct config_item *item, char *page)
  519. {
  520. struct se_lun *lun = item_to_lun(item);
  521. if (!lun || !lun->lun_se_dev)
  522. return -ENODEV;
  523. return core_alua_show_secondary_status(lun, page);
  524. }
  525. static ssize_t target_fabric_port_alua_tg_pt_status_store(
  526. struct config_item *item, const char *page, size_t count)
  527. {
  528. struct se_lun *lun = item_to_lun(item);
  529. if (!lun || !lun->lun_se_dev)
  530. return -ENODEV;
  531. return core_alua_store_secondary_status(lun, page, count);
  532. }
  533. static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
  534. struct config_item *item, char *page)
  535. {
  536. struct se_lun *lun = item_to_lun(item);
  537. if (!lun || !lun->lun_se_dev)
  538. return -ENODEV;
  539. return core_alua_show_secondary_write_metadata(lun, page);
  540. }
  541. static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
  542. struct config_item *item, const char *page, size_t count)
  543. {
  544. struct se_lun *lun = item_to_lun(item);
  545. if (!lun || !lun->lun_se_dev)
  546. return -ENODEV;
  547. return core_alua_store_secondary_write_metadata(lun, page, count);
  548. }
  549. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
  550. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
  551. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
  552. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
  553. static struct configfs_attribute *target_fabric_port_attrs[] = {
  554. &target_fabric_port_attr_alua_tg_pt_gp,
  555. &target_fabric_port_attr_alua_tg_pt_offline,
  556. &target_fabric_port_attr_alua_tg_pt_status,
  557. &target_fabric_port_attr_alua_tg_pt_write_md,
  558. NULL,
  559. };
  560. static int target_fabric_port_link(
  561. struct config_item *lun_ci,
  562. struct config_item *se_dev_ci)
  563. {
  564. struct config_item *tpg_ci;
  565. struct se_lun *lun = container_of(to_config_group(lun_ci),
  566. struct se_lun, lun_group);
  567. struct se_portal_group *se_tpg;
  568. struct se_device *dev =
  569. container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  570. struct target_fabric_configfs *tf;
  571. int ret;
  572. if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
  573. pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
  574. " %p to struct se_device: %p\n", se_dev_ci, dev);
  575. return -EFAULT;
  576. }
  577. if (!(dev->dev_flags & DF_CONFIGURED)) {
  578. pr_err("se_device not configured yet, cannot port link\n");
  579. return -ENODEV;
  580. }
  581. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  582. se_tpg = container_of(to_config_group(tpg_ci),
  583. struct se_portal_group, tpg_group);
  584. tf = se_tpg->se_tpg_wwn->wwn_tf;
  585. if (lun->lun_se_dev != NULL) {
  586. pr_err("Port Symlink already exists\n");
  587. return -EEXIST;
  588. }
  589. ret = core_dev_add_lun(se_tpg, dev, lun);
  590. if (ret) {
  591. pr_err("core_dev_add_lun() failed: %d\n", ret);
  592. goto out;
  593. }
  594. if (tf->tf_ops->fabric_post_link) {
  595. /*
  596. * Call the optional fabric_post_link() to allow a
  597. * fabric module to setup any additional state once
  598. * core_dev_add_lun() has been called..
  599. */
  600. tf->tf_ops->fabric_post_link(se_tpg, lun);
  601. }
  602. return 0;
  603. out:
  604. return ret;
  605. }
  606. static int target_fabric_port_unlink(
  607. struct config_item *lun_ci,
  608. struct config_item *se_dev_ci)
  609. {
  610. struct se_lun *lun = container_of(to_config_group(lun_ci),
  611. struct se_lun, lun_group);
  612. struct se_portal_group *se_tpg = lun->lun_tpg;
  613. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  614. if (tf->tf_ops->fabric_pre_unlink) {
  615. /*
  616. * Call the optional fabric_pre_unlink() to allow a
  617. * fabric module to release any additional stat before
  618. * core_dev_del_lun() is called.
  619. */
  620. tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
  621. }
  622. core_dev_del_lun(se_tpg, lun);
  623. return 0;
  624. }
  625. static void target_fabric_port_release(struct config_item *item)
  626. {
  627. struct se_lun *lun = container_of(to_config_group(item),
  628. struct se_lun, lun_group);
  629. kfree_rcu(lun, rcu_head);
  630. }
  631. static struct configfs_item_operations target_fabric_port_item_ops = {
  632. .release = target_fabric_port_release,
  633. .allow_link = target_fabric_port_link,
  634. .drop_link = target_fabric_port_unlink,
  635. };
  636. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  637. /* End of tfc_tpg_port_cit */
  638. /* Start of tfc_tpg_port_stat_cit */
  639. static struct config_group *target_core_port_stat_mkdir(
  640. struct config_group *group,
  641. const char *name)
  642. {
  643. return ERR_PTR(-ENOSYS);
  644. }
  645. static void target_core_port_stat_rmdir(
  646. struct config_group *group,
  647. struct config_item *item)
  648. {
  649. return;
  650. }
  651. static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  652. .make_group = target_core_port_stat_mkdir,
  653. .drop_item = target_core_port_stat_rmdir,
  654. };
  655. TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  656. /* End of tfc_tpg_port_stat_cit */
  657. /* Start of tfc_tpg_lun_cit */
  658. static struct config_group *target_fabric_make_lun(
  659. struct config_group *group,
  660. const char *name)
  661. {
  662. struct se_lun *lun;
  663. struct se_portal_group *se_tpg = container_of(group,
  664. struct se_portal_group, tpg_lun_group);
  665. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  666. struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
  667. unsigned long long unpacked_lun;
  668. int errno;
  669. if (strstr(name, "lun_") != name) {
  670. pr_err("Unable to locate \'_\" in"
  671. " \"lun_$LUN_NUMBER\"\n");
  672. return ERR_PTR(-EINVAL);
  673. }
  674. errno = kstrtoull(name + 4, 0, &unpacked_lun);
  675. if (errno)
  676. return ERR_PTR(errno);
  677. lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
  678. if (IS_ERR(lun))
  679. return ERR_CAST(lun);
  680. lun_cg = &lun->lun_group;
  681. lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  682. GFP_KERNEL);
  683. if (!lun_cg->default_groups) {
  684. pr_err("Unable to allocate lun_cg->default_groups\n");
  685. kfree(lun);
  686. return ERR_PTR(-ENOMEM);
  687. }
  688. config_group_init_type_name(&lun->lun_group, name,
  689. &tf->tf_tpg_port_cit);
  690. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  691. "statistics", &tf->tf_tpg_port_stat_cit);
  692. lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
  693. lun_cg->default_groups[1] = NULL;
  694. port_stat_grp = &lun->port_stat_grps.stat_group;
  695. port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
  696. GFP_KERNEL);
  697. if (!port_stat_grp->default_groups) {
  698. pr_err("Unable to allocate port_stat_grp->default_groups\n");
  699. kfree(lun_cg->default_groups);
  700. kfree(lun);
  701. return ERR_PTR(-ENOMEM);
  702. }
  703. target_stat_setup_port_default_groups(lun);
  704. return &lun->lun_group;
  705. }
  706. static void target_fabric_drop_lun(
  707. struct config_group *group,
  708. struct config_item *item)
  709. {
  710. struct se_lun *lun = container_of(to_config_group(item),
  711. struct se_lun, lun_group);
  712. struct config_item *df_item;
  713. struct config_group *lun_cg, *port_stat_grp;
  714. int i;
  715. port_stat_grp = &lun->port_stat_grps.stat_group;
  716. for (i = 0; port_stat_grp->default_groups[i]; i++) {
  717. df_item = &port_stat_grp->default_groups[i]->cg_item;
  718. port_stat_grp->default_groups[i] = NULL;
  719. config_item_put(df_item);
  720. }
  721. kfree(port_stat_grp->default_groups);
  722. lun_cg = &lun->lun_group;
  723. for (i = 0; lun_cg->default_groups[i]; i++) {
  724. df_item = &lun_cg->default_groups[i]->cg_item;
  725. lun_cg->default_groups[i] = NULL;
  726. config_item_put(df_item);
  727. }
  728. kfree(lun_cg->default_groups);
  729. config_item_put(item);
  730. }
  731. static struct configfs_group_operations target_fabric_lun_group_ops = {
  732. .make_group = &target_fabric_make_lun,
  733. .drop_item = &target_fabric_drop_lun,
  734. };
  735. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  736. /* End of tfc_tpg_lun_cit */
  737. TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
  738. TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
  739. TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
  740. /* Start of tfc_tpg_base_cit */
  741. static void target_fabric_tpg_release(struct config_item *item)
  742. {
  743. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  744. struct se_portal_group, tpg_group);
  745. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  746. struct target_fabric_configfs *tf = wwn->wwn_tf;
  747. tf->tf_ops->fabric_drop_tpg(se_tpg);
  748. }
  749. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  750. .release = target_fabric_tpg_release,
  751. };
  752. TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
  753. /* End of tfc_tpg_base_cit */
  754. /* Start of tfc_tpg_cit */
  755. static struct config_group *target_fabric_make_tpg(
  756. struct config_group *group,
  757. const char *name)
  758. {
  759. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  760. struct target_fabric_configfs *tf = wwn->wwn_tf;
  761. struct se_portal_group *se_tpg;
  762. if (!tf->tf_ops->fabric_make_tpg) {
  763. pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
  764. return ERR_PTR(-ENOSYS);
  765. }
  766. se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
  767. if (!se_tpg || IS_ERR(se_tpg))
  768. return ERR_PTR(-EINVAL);
  769. /*
  770. * Setup default groups from pre-allocated se_tpg->tpg_default_groups
  771. */
  772. se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
  773. se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
  774. se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
  775. se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
  776. se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
  777. se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
  778. se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
  779. se_tpg->tpg_group.default_groups[6] = NULL;
  780. config_group_init_type_name(&se_tpg->tpg_group, name,
  781. &tf->tf_tpg_base_cit);
  782. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  783. &tf->tf_tpg_lun_cit);
  784. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  785. &tf->tf_tpg_np_cit);
  786. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  787. &tf->tf_tpg_nacl_cit);
  788. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  789. &tf->tf_tpg_attrib_cit);
  790. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  791. &tf->tf_tpg_auth_cit);
  792. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  793. &tf->tf_tpg_param_cit);
  794. return &se_tpg->tpg_group;
  795. }
  796. static void target_fabric_drop_tpg(
  797. struct config_group *group,
  798. struct config_item *item)
  799. {
  800. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  801. struct se_portal_group, tpg_group);
  802. struct config_group *tpg_cg = &se_tpg->tpg_group;
  803. struct config_item *df_item;
  804. int i;
  805. /*
  806. * Release default groups, but do not release tpg_cg->default_groups
  807. * memory as it is statically allocated at se_tpg->tpg_default_groups.
  808. */
  809. for (i = 0; tpg_cg->default_groups[i]; i++) {
  810. df_item = &tpg_cg->default_groups[i]->cg_item;
  811. tpg_cg->default_groups[i] = NULL;
  812. config_item_put(df_item);
  813. }
  814. config_item_put(item);
  815. }
  816. static void target_fabric_release_wwn(struct config_item *item)
  817. {
  818. struct se_wwn *wwn = container_of(to_config_group(item),
  819. struct se_wwn, wwn_group);
  820. struct target_fabric_configfs *tf = wwn->wwn_tf;
  821. tf->tf_ops->fabric_drop_wwn(wwn);
  822. }
  823. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  824. .release = target_fabric_release_wwn,
  825. };
  826. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  827. .make_group = target_fabric_make_tpg,
  828. .drop_item = target_fabric_drop_tpg,
  829. };
  830. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  831. NULL);
  832. /* End of tfc_tpg_cit */
  833. /* Start of tfc_wwn_fabric_stats_cit */
  834. /*
  835. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  836. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  837. */
  838. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  839. /* End of tfc_wwn_fabric_stats_cit */
  840. /* Start of tfc_wwn_cit */
  841. static struct config_group *target_fabric_make_wwn(
  842. struct config_group *group,
  843. const char *name)
  844. {
  845. struct target_fabric_configfs *tf = container_of(group,
  846. struct target_fabric_configfs, tf_group);
  847. struct se_wwn *wwn;
  848. if (!tf->tf_ops->fabric_make_wwn) {
  849. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  850. return ERR_PTR(-ENOSYS);
  851. }
  852. wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
  853. if (!wwn || IS_ERR(wwn))
  854. return ERR_PTR(-EINVAL);
  855. wwn->wwn_tf = tf;
  856. /*
  857. * Setup default groups from pre-allocated wwn->wwn_default_groups
  858. */
  859. wwn->wwn_group.default_groups = wwn->wwn_default_groups;
  860. wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
  861. wwn->wwn_group.default_groups[1] = NULL;
  862. config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
  863. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  864. &tf->tf_wwn_fabric_stats_cit);
  865. return &wwn->wwn_group;
  866. }
  867. static void target_fabric_drop_wwn(
  868. struct config_group *group,
  869. struct config_item *item)
  870. {
  871. struct se_wwn *wwn = container_of(to_config_group(item),
  872. struct se_wwn, wwn_group);
  873. struct config_item *df_item;
  874. struct config_group *cg = &wwn->wwn_group;
  875. int i;
  876. for (i = 0; cg->default_groups[i]; i++) {
  877. df_item = &cg->default_groups[i]->cg_item;
  878. cg->default_groups[i] = NULL;
  879. config_item_put(df_item);
  880. }
  881. config_item_put(item);
  882. }
  883. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  884. .make_group = target_fabric_make_wwn,
  885. .drop_item = target_fabric_drop_wwn,
  886. };
  887. TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
  888. TF_CIT_SETUP_DRV(discovery, NULL, NULL);
  889. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  890. {
  891. target_fabric_setup_discovery_cit(tf);
  892. target_fabric_setup_wwn_cit(tf);
  893. target_fabric_setup_wwn_fabric_stats_cit(tf);
  894. target_fabric_setup_tpg_cit(tf);
  895. target_fabric_setup_tpg_base_cit(tf);
  896. target_fabric_setup_tpg_port_cit(tf);
  897. target_fabric_setup_tpg_port_stat_cit(tf);
  898. target_fabric_setup_tpg_lun_cit(tf);
  899. target_fabric_setup_tpg_np_cit(tf);
  900. target_fabric_setup_tpg_np_base_cit(tf);
  901. target_fabric_setup_tpg_attrib_cit(tf);
  902. target_fabric_setup_tpg_auth_cit(tf);
  903. target_fabric_setup_tpg_param_cit(tf);
  904. target_fabric_setup_tpg_nacl_cit(tf);
  905. target_fabric_setup_tpg_nacl_base_cit(tf);
  906. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  907. target_fabric_setup_tpg_nacl_auth_cit(tf);
  908. target_fabric_setup_tpg_nacl_param_cit(tf);
  909. target_fabric_setup_tpg_nacl_stat_cit(tf);
  910. target_fabric_setup_tpg_mappedlun_cit(tf);
  911. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  912. return 0;
  913. }