target_core_fabric_configfs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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 struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
  193. &target_fabric_mappedlun_write_protect.attr,
  194. NULL,
  195. };
  196. static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
  197. .show_attribute = target_fabric_mappedlun_attr_show,
  198. .store_attribute = target_fabric_mappedlun_attr_store,
  199. .allow_link = target_fabric_mappedlun_link,
  200. .drop_link = target_fabric_mappedlun_unlink,
  201. };
  202. TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  203. target_fabric_mappedlun_attrs);
  204. /* End of tfc_tpg_mappedlun_cit */
  205. /* Start of tfc_tpg_nacl_attrib_cit */
  206. CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
  207. static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
  208. .show_attribute = target_fabric_nacl_attrib_attr_show,
  209. .store_attribute = target_fabric_nacl_attrib_attr_store,
  210. };
  211. TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
  212. /* End of tfc_tpg_nacl_attrib_cit */
  213. /* Start of tfc_tpg_nacl_auth_cit */
  214. CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
  215. static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
  216. .show_attribute = target_fabric_nacl_auth_attr_show,
  217. .store_attribute = target_fabric_nacl_auth_attr_store,
  218. };
  219. TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
  220. /* End of tfc_tpg_nacl_auth_cit */
  221. /* Start of tfc_tpg_nacl_param_cit */
  222. CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
  223. static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
  224. .show_attribute = target_fabric_nacl_param_attr_show,
  225. .store_attribute = target_fabric_nacl_param_attr_store,
  226. };
  227. TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
  228. /* End of tfc_tpg_nacl_param_cit */
  229. /* Start of tfc_tpg_nacl_base_cit */
  230. CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
  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;
  240. struct config_item *acl_ci;
  241. char *buf;
  242. unsigned long mapped_lun;
  243. int ret = 0;
  244. acl_ci = &group->cg_item;
  245. if (!(acl_ci)) {
  246. printk(KERN_ERR "Unable to locatel acl_ci\n");
  247. return NULL;
  248. }
  249. buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
  250. if (!(buf)) {
  251. printk(KERN_ERR "Unable to allocate memory for name buf\n");
  252. return ERR_PTR(-ENOMEM);
  253. }
  254. snprintf(buf, strlen(name) + 1, "%s", name);
  255. /*
  256. * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  257. */
  258. if (strstr(buf, "lun_") != buf) {
  259. printk(KERN_ERR "Unable to locate \"lun_\" from buf: %s"
  260. " name: %s\n", buf, name);
  261. ret = -EINVAL;
  262. goto out;
  263. }
  264. /*
  265. * Determine the Mapped LUN value. This is what the SCSI Initiator
  266. * Port will actually see.
  267. */
  268. if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
  269. ret = -EINVAL;
  270. goto out;
  271. }
  272. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
  273. config_item_name(acl_ci), &ret);
  274. if (!(lacl))
  275. goto out;
  276. config_group_init_type_name(&lacl->se_lun_group, name,
  277. &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
  278. kfree(buf);
  279. return &lacl->se_lun_group;
  280. out:
  281. kfree(buf);
  282. return ERR_PTR(ret);
  283. }
  284. static void target_fabric_drop_mappedlun(
  285. struct config_group *group,
  286. struct config_item *item)
  287. {
  288. struct se_lun_acl *lacl = container_of(to_config_group(item),
  289. struct se_lun_acl, se_lun_group);
  290. struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
  291. config_item_put(item);
  292. core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
  293. }
  294. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  295. .show_attribute = target_fabric_nacl_base_attr_show,
  296. .store_attribute = target_fabric_nacl_base_attr_store,
  297. };
  298. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  299. .make_group = target_fabric_make_mappedlun,
  300. .drop_item = target_fabric_drop_mappedlun,
  301. };
  302. TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  303. &target_fabric_nacl_base_group_ops, NULL);
  304. /* End of tfc_tpg_nacl_base_cit */
  305. /* Start of tfc_tpg_nacl_cit */
  306. static struct config_group *target_fabric_make_nodeacl(
  307. struct config_group *group,
  308. const char *name)
  309. {
  310. struct se_portal_group *se_tpg = container_of(group,
  311. struct se_portal_group, tpg_acl_group);
  312. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  313. struct se_node_acl *se_nacl;
  314. struct config_group *nacl_cg;
  315. if (!(tf->tf_ops.fabric_make_nodeacl)) {
  316. printk(KERN_ERR "tf->tf_ops.fabric_make_nodeacl is NULL\n");
  317. return ERR_PTR(-ENOSYS);
  318. }
  319. se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
  320. if (IS_ERR(se_nacl))
  321. return ERR_PTR(PTR_ERR(se_nacl));
  322. nacl_cg = &se_nacl->acl_group;
  323. nacl_cg->default_groups = se_nacl->acl_default_groups;
  324. nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
  325. nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
  326. nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
  327. nacl_cg->default_groups[3] = NULL;
  328. config_group_init_type_name(&se_nacl->acl_group, name,
  329. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
  330. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  331. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
  332. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  333. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
  334. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  335. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
  336. return &se_nacl->acl_group;
  337. }
  338. static void target_fabric_drop_nodeacl(
  339. struct config_group *group,
  340. struct config_item *item)
  341. {
  342. struct se_portal_group *se_tpg = container_of(group,
  343. struct se_portal_group, tpg_acl_group);
  344. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  345. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  346. struct se_node_acl, acl_group);
  347. struct config_item *df_item;
  348. struct config_group *nacl_cg;
  349. int i;
  350. nacl_cg = &se_nacl->acl_group;
  351. for (i = 0; nacl_cg->default_groups[i]; i++) {
  352. df_item = &nacl_cg->default_groups[i]->cg_item;
  353. nacl_cg->default_groups[i] = NULL;
  354. config_item_put(df_item);
  355. }
  356. config_item_put(item);
  357. tf->tf_ops.fabric_drop_nodeacl(se_nacl);
  358. }
  359. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  360. .make_group = target_fabric_make_nodeacl,
  361. .drop_item = target_fabric_drop_nodeacl,
  362. };
  363. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  364. /* End of tfc_tpg_nacl_cit */
  365. /* Start of tfc_tpg_np_base_cit */
  366. CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
  367. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  368. .show_attribute = target_fabric_np_base_attr_show,
  369. .store_attribute = target_fabric_np_base_attr_store,
  370. };
  371. TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
  372. /* End of tfc_tpg_np_base_cit */
  373. /* Start of tfc_tpg_np_cit */
  374. static struct config_group *target_fabric_make_np(
  375. struct config_group *group,
  376. const char *name)
  377. {
  378. struct se_portal_group *se_tpg = container_of(group,
  379. struct se_portal_group, tpg_np_group);
  380. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  381. struct se_tpg_np *se_tpg_np;
  382. if (!(tf->tf_ops.fabric_make_np)) {
  383. printk(KERN_ERR "tf->tf_ops.fabric_make_np is NULL\n");
  384. return ERR_PTR(-ENOSYS);
  385. }
  386. se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
  387. if (!(se_tpg_np) || IS_ERR(se_tpg_np))
  388. return ERR_PTR(-EINVAL);
  389. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  390. &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
  391. return &se_tpg_np->tpg_np_group;
  392. }
  393. static void target_fabric_drop_np(
  394. struct config_group *group,
  395. struct config_item *item)
  396. {
  397. struct se_portal_group *se_tpg = container_of(group,
  398. struct se_portal_group, tpg_np_group);
  399. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  400. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  401. struct se_tpg_np, tpg_np_group);
  402. config_item_put(item);
  403. tf->tf_ops.fabric_drop_np(se_tpg_np);
  404. }
  405. static struct configfs_group_operations target_fabric_np_group_ops = {
  406. .make_group = &target_fabric_make_np,
  407. .drop_item = &target_fabric_drop_np,
  408. };
  409. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  410. /* End of tfc_tpg_np_cit */
  411. /* Start of tfc_tpg_port_cit */
  412. CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
  413. #define TCM_PORT_ATTR(_name, _mode) \
  414. static struct target_fabric_port_attribute target_fabric_port_##_name = \
  415. __CONFIGFS_EATTR(_name, _mode, \
  416. target_fabric_port_show_attr_##_name, \
  417. target_fabric_port_store_attr_##_name);
  418. #define TCM_PORT_ATTOR_RO(_name) \
  419. __CONFIGFS_EATTR_RO(_name, \
  420. target_fabric_port_show_attr_##_name);
  421. /*
  422. * alua_tg_pt_gp
  423. */
  424. static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
  425. struct se_lun *lun,
  426. char *page)
  427. {
  428. if (!(lun))
  429. return -ENODEV;
  430. if (!(lun->lun_sep))
  431. return -ENODEV;
  432. return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
  433. }
  434. static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
  435. struct se_lun *lun,
  436. const char *page,
  437. size_t count)
  438. {
  439. if (!(lun))
  440. return -ENODEV;
  441. if (!(lun->lun_sep))
  442. return -ENODEV;
  443. return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
  444. }
  445. TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
  446. /*
  447. * alua_tg_pt_offline
  448. */
  449. static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
  450. struct se_lun *lun,
  451. char *page)
  452. {
  453. if (!(lun))
  454. return -ENODEV;
  455. if (!(lun->lun_sep))
  456. return -ENODEV;
  457. return core_alua_show_offline_bit(lun, page);
  458. }
  459. static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
  460. struct se_lun *lun,
  461. const char *page,
  462. size_t count)
  463. {
  464. if (!(lun))
  465. return -ENODEV;
  466. if (!(lun->lun_sep))
  467. return -ENODEV;
  468. return core_alua_store_offline_bit(lun, page, count);
  469. }
  470. TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
  471. /*
  472. * alua_tg_pt_status
  473. */
  474. static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
  475. struct se_lun *lun,
  476. char *page)
  477. {
  478. if (!(lun))
  479. return -ENODEV;
  480. if (!(lun->lun_sep))
  481. return -ENODEV;
  482. return core_alua_show_secondary_status(lun, page);
  483. }
  484. static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
  485. struct se_lun *lun,
  486. const char *page,
  487. size_t count)
  488. {
  489. if (!(lun))
  490. return -ENODEV;
  491. if (!(lun->lun_sep))
  492. return -ENODEV;
  493. return core_alua_store_secondary_status(lun, page, count);
  494. }
  495. TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
  496. /*
  497. * alua_tg_pt_write_md
  498. */
  499. static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
  500. struct se_lun *lun,
  501. char *page)
  502. {
  503. if (!(lun))
  504. return -ENODEV;
  505. if (!(lun->lun_sep))
  506. return -ENODEV;
  507. return core_alua_show_secondary_write_metadata(lun, page);
  508. }
  509. static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
  510. struct se_lun *lun,
  511. const char *page,
  512. size_t count)
  513. {
  514. if (!(lun))
  515. return -ENODEV;
  516. if (!(lun->lun_sep))
  517. return -ENODEV;
  518. return core_alua_store_secondary_write_metadata(lun, page, count);
  519. }
  520. TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
  521. static struct configfs_attribute *target_fabric_port_attrs[] = {
  522. &target_fabric_port_alua_tg_pt_gp.attr,
  523. &target_fabric_port_alua_tg_pt_offline.attr,
  524. &target_fabric_port_alua_tg_pt_status.attr,
  525. &target_fabric_port_alua_tg_pt_write_md.attr,
  526. NULL,
  527. };
  528. CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
  529. static int target_fabric_port_link(
  530. struct config_item *lun_ci,
  531. struct config_item *se_dev_ci)
  532. {
  533. struct config_item *tpg_ci;
  534. struct se_device *dev;
  535. struct se_lun *lun = container_of(to_config_group(lun_ci),
  536. struct se_lun, lun_group);
  537. struct se_lun *lun_p;
  538. struct se_portal_group *se_tpg;
  539. struct se_subsystem_dev *se_dev = container_of(
  540. to_config_group(se_dev_ci), struct se_subsystem_dev,
  541. se_dev_group);
  542. struct target_fabric_configfs *tf;
  543. int ret;
  544. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  545. se_tpg = container_of(to_config_group(tpg_ci),
  546. struct se_portal_group, tpg_group);
  547. tf = se_tpg->se_tpg_wwn->wwn_tf;
  548. if (lun->lun_se_dev != NULL) {
  549. printk(KERN_ERR "Port Symlink already exists\n");
  550. return -EEXIST;
  551. }
  552. dev = se_dev->se_dev_ptr;
  553. if (!(dev)) {
  554. printk(KERN_ERR "Unable to locate struct se_device pointer from"
  555. " %s\n", config_item_name(se_dev_ci));
  556. ret = -ENODEV;
  557. goto out;
  558. }
  559. lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
  560. lun->unpacked_lun);
  561. if ((IS_ERR(lun_p)) || !(lun_p)) {
  562. printk(KERN_ERR "core_dev_add_lun() failed\n");
  563. ret = -EINVAL;
  564. goto out;
  565. }
  566. if (tf->tf_ops.fabric_post_link) {
  567. /*
  568. * Call the optional fabric_post_link() to allow a
  569. * fabric module to setup any additional state once
  570. * core_dev_add_lun() has been called..
  571. */
  572. tf->tf_ops.fabric_post_link(se_tpg, lun);
  573. }
  574. return 0;
  575. out:
  576. return ret;
  577. }
  578. static int target_fabric_port_unlink(
  579. struct config_item *lun_ci,
  580. struct config_item *se_dev_ci)
  581. {
  582. struct se_lun *lun = container_of(to_config_group(lun_ci),
  583. struct se_lun, lun_group);
  584. struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
  585. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  586. if (tf->tf_ops.fabric_pre_unlink) {
  587. /*
  588. * Call the optional fabric_pre_unlink() to allow a
  589. * fabric module to release any additional stat before
  590. * core_dev_del_lun() is called.
  591. */
  592. tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
  593. }
  594. core_dev_del_lun(se_tpg, lun->unpacked_lun);
  595. return 0;
  596. }
  597. static struct configfs_item_operations target_fabric_port_item_ops = {
  598. .show_attribute = target_fabric_port_attr_show,
  599. .store_attribute = target_fabric_port_attr_store,
  600. .allow_link = target_fabric_port_link,
  601. .drop_link = target_fabric_port_unlink,
  602. };
  603. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  604. /* End of tfc_tpg_port_cit */
  605. /* Start of tfc_tpg_lun_cit */
  606. static struct config_group *target_fabric_make_lun(
  607. struct config_group *group,
  608. const char *name)
  609. {
  610. struct se_lun *lun;
  611. struct se_portal_group *se_tpg = container_of(group,
  612. struct se_portal_group, tpg_lun_group);
  613. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  614. unsigned long unpacked_lun;
  615. if (strstr(name, "lun_") != name) {
  616. printk(KERN_ERR "Unable to locate \'_\" in"
  617. " \"lun_$LUN_NUMBER\"\n");
  618. return ERR_PTR(-EINVAL);
  619. }
  620. if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
  621. return ERR_PTR(-EINVAL);
  622. lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
  623. if (!(lun))
  624. return ERR_PTR(-EINVAL);
  625. config_group_init_type_name(&lun->lun_group, name,
  626. &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
  627. return &lun->lun_group;
  628. }
  629. static void target_fabric_drop_lun(
  630. struct config_group *group,
  631. struct config_item *item)
  632. {
  633. config_item_put(item);
  634. }
  635. static struct configfs_group_operations target_fabric_lun_group_ops = {
  636. .make_group = &target_fabric_make_lun,
  637. .drop_item = &target_fabric_drop_lun,
  638. };
  639. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  640. /* End of tfc_tpg_lun_cit */
  641. /* Start of tfc_tpg_attrib_cit */
  642. CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
  643. static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
  644. .show_attribute = target_fabric_tpg_attrib_attr_show,
  645. .store_attribute = target_fabric_tpg_attrib_attr_store,
  646. };
  647. TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
  648. /* End of tfc_tpg_attrib_cit */
  649. /* Start of tfc_tpg_param_cit */
  650. CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
  651. static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
  652. .show_attribute = target_fabric_tpg_param_attr_show,
  653. .store_attribute = target_fabric_tpg_param_attr_store,
  654. };
  655. TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
  656. /* End of tfc_tpg_param_cit */
  657. /* Start of tfc_tpg_base_cit */
  658. /*
  659. * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
  660. */
  661. CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
  662. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  663. .show_attribute = target_fabric_tpg_attr_show,
  664. .store_attribute = target_fabric_tpg_attr_store,
  665. };
  666. TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, 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. printk(KERN_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. /*
  684. * Setup default groups from pre-allocated se_tpg->tpg_default_groups
  685. */
  686. se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
  687. se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
  688. se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
  689. se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
  690. se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
  691. se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
  692. se_tpg->tpg_group.default_groups[5] = NULL;
  693. config_group_init_type_name(&se_tpg->tpg_group, name,
  694. &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
  695. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  696. &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
  697. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  698. &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
  699. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  700. &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
  701. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  702. &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
  703. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  704. &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
  705. return &se_tpg->tpg_group;
  706. }
  707. static void target_fabric_drop_tpg(
  708. struct config_group *group,
  709. struct config_item *item)
  710. {
  711. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  712. struct target_fabric_configfs *tf = wwn->wwn_tf;
  713. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  714. struct se_portal_group, tpg_group);
  715. struct config_group *tpg_cg = &se_tpg->tpg_group;
  716. struct config_item *df_item;
  717. int i;
  718. /*
  719. * Release default groups, but do not release tpg_cg->default_groups
  720. * memory as it is statically allocated at se_tpg->tpg_default_groups.
  721. */
  722. for (i = 0; tpg_cg->default_groups[i]; i++) {
  723. df_item = &tpg_cg->default_groups[i]->cg_item;
  724. tpg_cg->default_groups[i] = NULL;
  725. config_item_put(df_item);
  726. }
  727. config_item_put(item);
  728. tf->tf_ops.fabric_drop_tpg(se_tpg);
  729. }
  730. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  731. .make_group = target_fabric_make_tpg,
  732. .drop_item = target_fabric_drop_tpg,
  733. };
  734. TF_CIT_SETUP(tpg, NULL, &target_fabric_tpg_group_ops, NULL);
  735. /* End of tfc_tpg_cit */
  736. /* Start of tfc_wwn_cit */
  737. static struct config_group *target_fabric_make_wwn(
  738. struct config_group *group,
  739. const char *name)
  740. {
  741. struct target_fabric_configfs *tf = container_of(group,
  742. struct target_fabric_configfs, tf_group);
  743. struct se_wwn *wwn;
  744. if (!(tf->tf_ops.fabric_make_wwn)) {
  745. printk(KERN_ERR "tf->tf_ops.fabric_make_wwn is NULL\n");
  746. return ERR_PTR(-ENOSYS);
  747. }
  748. wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
  749. if (!(wwn) || IS_ERR(wwn))
  750. return ERR_PTR(-EINVAL);
  751. wwn->wwn_tf = tf;
  752. config_group_init_type_name(&wwn->wwn_group, name,
  753. &TF_CIT_TMPL(tf)->tfc_tpg_cit);
  754. return &wwn->wwn_group;
  755. }
  756. static void target_fabric_drop_wwn(
  757. struct config_group *group,
  758. struct config_item *item)
  759. {
  760. struct target_fabric_configfs *tf = container_of(group,
  761. struct target_fabric_configfs, tf_group);
  762. struct se_wwn *wwn = container_of(to_config_group(item),
  763. struct se_wwn, wwn_group);
  764. config_item_put(item);
  765. tf->tf_ops.fabric_drop_wwn(wwn);
  766. }
  767. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  768. .make_group = target_fabric_make_wwn,
  769. .drop_item = target_fabric_drop_wwn,
  770. };
  771. /*
  772. * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
  773. */
  774. CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
  775. static struct configfs_item_operations target_fabric_wwn_item_ops = {
  776. .show_attribute = target_fabric_wwn_attr_show,
  777. .store_attribute = target_fabric_wwn_attr_store,
  778. };
  779. TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
  780. /* End of tfc_wwn_cit */
  781. /* Start of tfc_discovery_cit */
  782. CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
  783. tf_disc_group);
  784. static struct configfs_item_operations target_fabric_discovery_item_ops = {
  785. .show_attribute = target_fabric_discovery_attr_show,
  786. .store_attribute = target_fabric_discovery_attr_store,
  787. };
  788. TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
  789. /* End of tfc_discovery_cit */
  790. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  791. {
  792. target_fabric_setup_discovery_cit(tf);
  793. target_fabric_setup_wwn_cit(tf);
  794. target_fabric_setup_tpg_cit(tf);
  795. target_fabric_setup_tpg_base_cit(tf);
  796. target_fabric_setup_tpg_port_cit(tf);
  797. target_fabric_setup_tpg_lun_cit(tf);
  798. target_fabric_setup_tpg_np_cit(tf);
  799. target_fabric_setup_tpg_np_base_cit(tf);
  800. target_fabric_setup_tpg_attrib_cit(tf);
  801. target_fabric_setup_tpg_param_cit(tf);
  802. target_fabric_setup_tpg_nacl_cit(tf);
  803. target_fabric_setup_tpg_nacl_base_cit(tf);
  804. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  805. target_fabric_setup_tpg_nacl_auth_cit(tf);
  806. target_fabric_setup_tpg_nacl_param_cit(tf);
  807. target_fabric_setup_tpg_mappedlun_cit(tf);
  808. return 0;
  809. }