target_core_fabric_configfs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*******************************************************************************
  2. * Filename: target_core_fabric_configfs.c
  3. *
  4. * This file contains generic fabric module configfs infrastructure for
  5. * TCM v4.x code
  6. *
  7. * (c) Copyright 2010-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@linux-iscsi.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. ****************************************************************************/
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/utsname.h>
  24. #include <linux/init.h>
  25. #include <linux/fs.h>
  26. #include <linux/namei.h>
  27. #include <linux/slab.h>
  28. #include <linux/types.h>
  29. #include <linux/delay.h>
  30. #include <linux/unistd.h>
  31. #include <linux/string.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/configfs.h>
  34. #include <target/target_core_base.h>
  35. #include <target/target_core_fabric.h>
  36. #include "target_core_internal.h"
  37. #include "target_core_alua.h"
  38. #include "target_core_pr.h"
  39. #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
  40. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  41. { \
  42. struct config_item_type *cit = &tf->tf_##_name##_cit; \
  43. \
  44. cit->ct_item_ops = _item_ops; \
  45. cit->ct_group_ops = _group_ops; \
  46. cit->ct_attrs = _attrs; \
  47. cit->ct_owner = tf->tf_ops->module; \
  48. pr_debug("Setup generic %s\n", __stringify(_name)); \
  49. }
  50. #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
  51. static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
  52. { \
  53. struct config_item_type *cit = &tf->tf_##_name##_cit; \
  54. struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
  55. \
  56. cit->ct_item_ops = _item_ops; \
  57. cit->ct_group_ops = _group_ops; \
  58. cit->ct_attrs = attrs; \
  59. cit->ct_owner = tf->tf_ops->module; \
  60. pr_debug("Setup generic %s\n", __stringify(_name)); \
  61. }
  62. /* Start of tfc_tpg_mappedlun_cit */
  63. static int target_fabric_mappedlun_link(
  64. struct config_item *lun_acl_ci,
  65. struct config_item *lun_ci)
  66. {
  67. struct se_dev_entry *deve;
  68. struct se_lun *lun = container_of(to_config_group(lun_ci),
  69. struct se_lun, lun_group);
  70. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  71. struct se_lun_acl, se_lun_group);
  72. struct se_portal_group *se_tpg;
  73. struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
  74. bool lun_access_ro;
  75. if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
  76. pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
  77. " %p to struct lun: %p\n", lun_ci, lun);
  78. return -EFAULT;
  79. }
  80. /*
  81. * Ensure that the source port exists
  82. */
  83. if (!lun->lun_se_dev) {
  84. pr_err("Source se_lun->lun_se_dev does not exist\n");
  85. return -EINVAL;
  86. }
  87. se_tpg = lun->lun_tpg;
  88. nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
  89. tpg_ci = &nacl_ci->ci_group->cg_item;
  90. wwn_ci = &tpg_ci->ci_group->cg_item;
  91. tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
  92. wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
  93. /*
  94. * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
  95. */
  96. if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
  97. pr_err("Illegal Initiator ACL SymLink outside of %s\n",
  98. config_item_name(wwn_ci));
  99. return -EINVAL;
  100. }
  101. if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
  102. pr_err("Illegal Initiator ACL Symlink outside of %s"
  103. " TPGT: %s\n", config_item_name(wwn_ci),
  104. config_item_name(tpg_ci));
  105. return -EINVAL;
  106. }
  107. /*
  108. * If this struct se_node_acl was dynamically generated with
  109. * tpg_1/attrib/generate_node_acls=1, use the existing
  110. * deve->lun_access_ro value, which will be true when
  111. * tpg_1/attrib/demo_mode_write_protect=1
  112. */
  113. rcu_read_lock();
  114. deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
  115. if (deve)
  116. lun_access_ro = deve->lun_access_ro;
  117. else
  118. lun_access_ro =
  119. (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
  120. se_tpg)) ? true : false;
  121. rcu_read_unlock();
  122. /*
  123. * Determine the actual mapped LUN value user wants..
  124. *
  125. * This value is what the SCSI Initiator actually sees the
  126. * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
  127. */
  128. return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
  129. }
  130. static int target_fabric_mappedlun_unlink(
  131. struct config_item *lun_acl_ci,
  132. struct config_item *lun_ci)
  133. {
  134. struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
  135. struct se_lun_acl, se_lun_group);
  136. struct se_lun *lun = container_of(to_config_group(lun_ci),
  137. struct se_lun, lun_group);
  138. return core_dev_del_initiator_node_lun_acl(lun, lacl);
  139. }
  140. static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
  141. {
  142. return container_of(to_config_group(item), struct se_lun_acl,
  143. se_lun_group);
  144. }
  145. static ssize_t target_fabric_mappedlun_write_protect_show(
  146. struct config_item *item, char *page)
  147. {
  148. struct se_lun_acl *lacl = item_to_lun_acl(item);
  149. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  150. struct se_dev_entry *deve;
  151. ssize_t len = 0;
  152. rcu_read_lock();
  153. deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
  154. if (deve) {
  155. len = sprintf(page, "%d\n", deve->lun_access_ro);
  156. }
  157. rcu_read_unlock();
  158. return len;
  159. }
  160. static ssize_t target_fabric_mappedlun_write_protect_store(
  161. struct config_item *item, const char *page, size_t count)
  162. {
  163. struct se_lun_acl *lacl = item_to_lun_acl(item);
  164. struct se_node_acl *se_nacl = lacl->se_lun_nacl;
  165. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  166. unsigned long wp;
  167. int ret;
  168. ret = kstrtoul(page, 0, &wp);
  169. if (ret)
  170. return ret;
  171. if ((wp != 1) && (wp != 0))
  172. return -EINVAL;
  173. /* wp=1 means lun_access_ro=true */
  174. core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
  175. pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
  176. " Mapped LUN: %llu Write Protect bit to %s\n",
  177. se_tpg->se_tpg_tfo->get_fabric_name(),
  178. se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
  179. return count;
  180. }
  181. CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
  182. static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
  183. &target_fabric_mappedlun_attr_write_protect,
  184. NULL,
  185. };
  186. static void target_fabric_mappedlun_release(struct config_item *item)
  187. {
  188. struct se_lun_acl *lacl = container_of(to_config_group(item),
  189. struct se_lun_acl, se_lun_group);
  190. struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
  191. core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
  192. }
  193. static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
  194. .release = target_fabric_mappedlun_release,
  195. .allow_link = target_fabric_mappedlun_link,
  196. .drop_link = target_fabric_mappedlun_unlink,
  197. };
  198. TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
  199. target_fabric_mappedlun_attrs);
  200. /* End of tfc_tpg_mappedlun_cit */
  201. /* Start of tfc_tpg_mappedlun_port_cit */
  202. static struct config_group *target_core_mappedlun_stat_mkdir(
  203. struct config_group *group,
  204. const char *name)
  205. {
  206. return ERR_PTR(-ENOSYS);
  207. }
  208. static void target_core_mappedlun_stat_rmdir(
  209. struct config_group *group,
  210. struct config_item *item)
  211. {
  212. return;
  213. }
  214. static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
  215. .make_group = target_core_mappedlun_stat_mkdir,
  216. .drop_item = target_core_mappedlun_stat_rmdir,
  217. };
  218. TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
  219. NULL);
  220. /* End of tfc_tpg_mappedlun_port_cit */
  221. TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
  222. TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
  223. TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
  224. /* Start of tfc_tpg_nacl_base_cit */
  225. static struct config_group *target_fabric_make_mappedlun(
  226. struct config_group *group,
  227. const char *name)
  228. {
  229. struct se_node_acl *se_nacl = container_of(group,
  230. struct se_node_acl, acl_group);
  231. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  232. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  233. struct se_lun_acl *lacl = NULL;
  234. char *buf;
  235. unsigned long long mapped_lun;
  236. int ret = 0;
  237. buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
  238. if (!buf) {
  239. pr_err("Unable to allocate memory for name buf\n");
  240. return ERR_PTR(-ENOMEM);
  241. }
  242. snprintf(buf, strlen(name) + 1, "%s", name);
  243. /*
  244. * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
  245. */
  246. if (strstr(buf, "lun_") != buf) {
  247. pr_err("Unable to locate \"lun_\" from buf: %s"
  248. " name: %s\n", buf, name);
  249. ret = -EINVAL;
  250. goto out;
  251. }
  252. /*
  253. * Determine the Mapped LUN value. This is what the SCSI Initiator
  254. * Port will actually see.
  255. */
  256. ret = kstrtoull(buf + 4, 0, &mapped_lun);
  257. if (ret)
  258. goto out;
  259. lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
  260. mapped_lun, &ret);
  261. if (!lacl) {
  262. ret = -EINVAL;
  263. goto out;
  264. }
  265. config_group_init_type_name(&lacl->se_lun_group, name,
  266. &tf->tf_tpg_mappedlun_cit);
  267. config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
  268. "statistics", &tf->tf_tpg_mappedlun_stat_cit);
  269. configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
  270. &lacl->se_lun_group);
  271. target_stat_setup_mappedlun_default_groups(lacl);
  272. kfree(buf);
  273. return &lacl->se_lun_group;
  274. out:
  275. kfree(lacl);
  276. kfree(buf);
  277. return ERR_PTR(ret);
  278. }
  279. static void target_fabric_drop_mappedlun(
  280. struct config_group *group,
  281. struct config_item *item)
  282. {
  283. struct se_lun_acl *lacl = container_of(to_config_group(item),
  284. struct se_lun_acl, se_lun_group);
  285. configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
  286. configfs_remove_default_groups(&lacl->se_lun_group);
  287. config_item_put(item);
  288. }
  289. static void target_fabric_nacl_base_release(struct config_item *item)
  290. {
  291. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  292. struct se_node_acl, acl_group);
  293. struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
  294. if (tf->tf_ops->fabric_cleanup_nodeacl)
  295. tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
  296. core_tpg_del_initiator_node_acl(se_nacl);
  297. }
  298. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  299. .release = target_fabric_nacl_base_release,
  300. };
  301. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  302. .make_group = target_fabric_make_mappedlun,
  303. .drop_item = target_fabric_drop_mappedlun,
  304. };
  305. TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  306. &target_fabric_nacl_base_group_ops);
  307. /* End of tfc_tpg_nacl_base_cit */
  308. /* Start of tfc_node_fabric_stats_cit */
  309. /*
  310. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  311. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  312. */
  313. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  314. /* End of tfc_wwn_fabric_stats_cit */
  315. /* Start of tfc_tpg_nacl_cit */
  316. static struct config_group *target_fabric_make_nodeacl(
  317. struct config_group *group,
  318. const char *name)
  319. {
  320. struct se_portal_group *se_tpg = container_of(group,
  321. struct se_portal_group, tpg_acl_group);
  322. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  323. struct se_node_acl *se_nacl;
  324. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
  325. if (IS_ERR(se_nacl))
  326. return ERR_CAST(se_nacl);
  327. if (tf->tf_ops->fabric_init_nodeacl) {
  328. int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
  329. if (ret) {
  330. core_tpg_del_initiator_node_acl(se_nacl);
  331. return ERR_PTR(ret);
  332. }
  333. }
  334. config_group_init_type_name(&se_nacl->acl_group, name,
  335. &tf->tf_tpg_nacl_base_cit);
  336. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  337. &tf->tf_tpg_nacl_attrib_cit);
  338. configfs_add_default_group(&se_nacl->acl_attrib_group,
  339. &se_nacl->acl_group);
  340. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  341. &tf->tf_tpg_nacl_auth_cit);
  342. configfs_add_default_group(&se_nacl->acl_auth_group,
  343. &se_nacl->acl_group);
  344. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  345. &tf->tf_tpg_nacl_param_cit);
  346. configfs_add_default_group(&se_nacl->acl_param_group,
  347. &se_nacl->acl_group);
  348. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  349. "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
  350. configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
  351. &se_nacl->acl_group);
  352. return &se_nacl->acl_group;
  353. }
  354. static void target_fabric_drop_nodeacl(
  355. struct config_group *group,
  356. struct config_item *item)
  357. {
  358. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  359. struct se_node_acl, acl_group);
  360. configfs_remove_default_groups(&se_nacl->acl_group);
  361. /*
  362. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  363. */
  364. config_item_put(item);
  365. }
  366. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  367. .make_group = target_fabric_make_nodeacl,
  368. .drop_item = target_fabric_drop_nodeacl,
  369. };
  370. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  371. /* End of tfc_tpg_nacl_cit */
  372. /* Start of tfc_tpg_np_base_cit */
  373. static void target_fabric_np_base_release(struct config_item *item)
  374. {
  375. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  376. struct se_tpg_np, tpg_np_group);
  377. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  378. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  379. tf->tf_ops->fabric_drop_np(se_tpg_np);
  380. }
  381. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  382. .release = target_fabric_np_base_release,
  383. };
  384. TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
  385. /* End of tfc_tpg_np_base_cit */
  386. /* Start of tfc_tpg_np_cit */
  387. static struct config_group *target_fabric_make_np(
  388. struct config_group *group,
  389. const char *name)
  390. {
  391. struct se_portal_group *se_tpg = container_of(group,
  392. struct se_portal_group, tpg_np_group);
  393. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  394. struct se_tpg_np *se_tpg_np;
  395. if (!tf->tf_ops->fabric_make_np) {
  396. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  397. return ERR_PTR(-ENOSYS);
  398. }
  399. se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
  400. if (!se_tpg_np || IS_ERR(se_tpg_np))
  401. return ERR_PTR(-EINVAL);
  402. se_tpg_np->tpg_np_parent = se_tpg;
  403. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  404. &tf->tf_tpg_np_base_cit);
  405. return &se_tpg_np->tpg_np_group;
  406. }
  407. static void target_fabric_drop_np(
  408. struct config_group *group,
  409. struct config_item *item)
  410. {
  411. /*
  412. * struct se_tpg_np is released via target_fabric_np_base_release()
  413. */
  414. config_item_put(item);
  415. }
  416. static struct configfs_group_operations target_fabric_np_group_ops = {
  417. .make_group = &target_fabric_make_np,
  418. .drop_item = &target_fabric_drop_np,
  419. };
  420. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  421. /* End of tfc_tpg_np_cit */
  422. /* Start of tfc_tpg_port_cit */
  423. static struct se_lun *item_to_lun(struct config_item *item)
  424. {
  425. return container_of(to_config_group(item), struct se_lun,
  426. lun_group);
  427. }
  428. static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
  429. char *page)
  430. {
  431. struct se_lun *lun = item_to_lun(item);
  432. if (!lun || !lun->lun_se_dev)
  433. return -ENODEV;
  434. return core_alua_show_tg_pt_gp_info(lun, page);
  435. }
  436. static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
  437. const char *page, size_t count)
  438. {
  439. struct se_lun *lun = item_to_lun(item);
  440. if (!lun || !lun->lun_se_dev)
  441. return -ENODEV;
  442. return core_alua_store_tg_pt_gp_info(lun, page, count);
  443. }
  444. static ssize_t target_fabric_port_alua_tg_pt_offline_show(
  445. struct config_item *item, char *page)
  446. {
  447. struct se_lun *lun = item_to_lun(item);
  448. if (!lun || !lun->lun_se_dev)
  449. return -ENODEV;
  450. return core_alua_show_offline_bit(lun, page);
  451. }
  452. static ssize_t target_fabric_port_alua_tg_pt_offline_store(
  453. struct config_item *item, const char *page, size_t count)
  454. {
  455. struct se_lun *lun = item_to_lun(item);
  456. if (!lun || !lun->lun_se_dev)
  457. return -ENODEV;
  458. return core_alua_store_offline_bit(lun, page, count);
  459. }
  460. static ssize_t target_fabric_port_alua_tg_pt_status_show(
  461. struct config_item *item, char *page)
  462. {
  463. struct se_lun *lun = item_to_lun(item);
  464. if (!lun || !lun->lun_se_dev)
  465. return -ENODEV;
  466. return core_alua_show_secondary_status(lun, page);
  467. }
  468. static ssize_t target_fabric_port_alua_tg_pt_status_store(
  469. struct config_item *item, const char *page, size_t count)
  470. {
  471. struct se_lun *lun = item_to_lun(item);
  472. if (!lun || !lun->lun_se_dev)
  473. return -ENODEV;
  474. return core_alua_store_secondary_status(lun, page, count);
  475. }
  476. static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
  477. struct config_item *item, char *page)
  478. {
  479. struct se_lun *lun = item_to_lun(item);
  480. if (!lun || !lun->lun_se_dev)
  481. return -ENODEV;
  482. return core_alua_show_secondary_write_metadata(lun, page);
  483. }
  484. static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
  485. struct config_item *item, const char *page, size_t count)
  486. {
  487. struct se_lun *lun = item_to_lun(item);
  488. if (!lun || !lun->lun_se_dev)
  489. return -ENODEV;
  490. return core_alua_store_secondary_write_metadata(lun, page, count);
  491. }
  492. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
  493. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
  494. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
  495. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
  496. static struct configfs_attribute *target_fabric_port_attrs[] = {
  497. &target_fabric_port_attr_alua_tg_pt_gp,
  498. &target_fabric_port_attr_alua_tg_pt_offline,
  499. &target_fabric_port_attr_alua_tg_pt_status,
  500. &target_fabric_port_attr_alua_tg_pt_write_md,
  501. NULL,
  502. };
  503. static int target_fabric_port_link(
  504. struct config_item *lun_ci,
  505. struct config_item *se_dev_ci)
  506. {
  507. struct config_item *tpg_ci;
  508. struct se_lun *lun = container_of(to_config_group(lun_ci),
  509. struct se_lun, lun_group);
  510. struct se_portal_group *se_tpg;
  511. struct se_device *dev =
  512. container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  513. struct target_fabric_configfs *tf;
  514. int ret;
  515. if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
  516. pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
  517. " %p to struct se_device: %p\n", se_dev_ci, dev);
  518. return -EFAULT;
  519. }
  520. if (!(dev->dev_flags & DF_CONFIGURED)) {
  521. pr_err("se_device not configured yet, cannot port link\n");
  522. return -ENODEV;
  523. }
  524. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  525. se_tpg = container_of(to_config_group(tpg_ci),
  526. struct se_portal_group, tpg_group);
  527. tf = se_tpg->se_tpg_wwn->wwn_tf;
  528. if (lun->lun_se_dev != NULL) {
  529. pr_err("Port Symlink already exists\n");
  530. return -EEXIST;
  531. }
  532. ret = core_dev_add_lun(se_tpg, dev, lun);
  533. if (ret) {
  534. pr_err("core_dev_add_lun() failed: %d\n", ret);
  535. goto out;
  536. }
  537. if (tf->tf_ops->fabric_post_link) {
  538. /*
  539. * Call the optional fabric_post_link() to allow a
  540. * fabric module to setup any additional state once
  541. * core_dev_add_lun() has been called..
  542. */
  543. tf->tf_ops->fabric_post_link(se_tpg, lun);
  544. }
  545. return 0;
  546. out:
  547. return ret;
  548. }
  549. static int target_fabric_port_unlink(
  550. struct config_item *lun_ci,
  551. struct config_item *se_dev_ci)
  552. {
  553. struct se_lun *lun = container_of(to_config_group(lun_ci),
  554. struct se_lun, lun_group);
  555. struct se_portal_group *se_tpg = lun->lun_tpg;
  556. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  557. if (tf->tf_ops->fabric_pre_unlink) {
  558. /*
  559. * Call the optional fabric_pre_unlink() to allow a
  560. * fabric module to release any additional stat before
  561. * core_dev_del_lun() is called.
  562. */
  563. tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
  564. }
  565. core_dev_del_lun(se_tpg, lun);
  566. return 0;
  567. }
  568. static void target_fabric_port_release(struct config_item *item)
  569. {
  570. struct se_lun *lun = container_of(to_config_group(item),
  571. struct se_lun, lun_group);
  572. kfree_rcu(lun, rcu_head);
  573. }
  574. static struct configfs_item_operations target_fabric_port_item_ops = {
  575. .release = target_fabric_port_release,
  576. .allow_link = target_fabric_port_link,
  577. .drop_link = target_fabric_port_unlink,
  578. };
  579. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  580. /* End of tfc_tpg_port_cit */
  581. /* Start of tfc_tpg_port_stat_cit */
  582. static struct config_group *target_core_port_stat_mkdir(
  583. struct config_group *group,
  584. const char *name)
  585. {
  586. return ERR_PTR(-ENOSYS);
  587. }
  588. static void target_core_port_stat_rmdir(
  589. struct config_group *group,
  590. struct config_item *item)
  591. {
  592. return;
  593. }
  594. static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  595. .make_group = target_core_port_stat_mkdir,
  596. .drop_item = target_core_port_stat_rmdir,
  597. };
  598. TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  599. /* End of tfc_tpg_port_stat_cit */
  600. /* Start of tfc_tpg_lun_cit */
  601. static struct config_group *target_fabric_make_lun(
  602. struct config_group *group,
  603. const char *name)
  604. {
  605. struct se_lun *lun;
  606. struct se_portal_group *se_tpg = container_of(group,
  607. struct se_portal_group, tpg_lun_group);
  608. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  609. unsigned long long unpacked_lun;
  610. int errno;
  611. if (strstr(name, "lun_") != name) {
  612. pr_err("Unable to locate \'_\" in"
  613. " \"lun_$LUN_NUMBER\"\n");
  614. return ERR_PTR(-EINVAL);
  615. }
  616. errno = kstrtoull(name + 4, 0, &unpacked_lun);
  617. if (errno)
  618. return ERR_PTR(errno);
  619. lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
  620. if (IS_ERR(lun))
  621. return ERR_CAST(lun);
  622. config_group_init_type_name(&lun->lun_group, name,
  623. &tf->tf_tpg_port_cit);
  624. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  625. "statistics", &tf->tf_tpg_port_stat_cit);
  626. configfs_add_default_group(&lun->port_stat_grps.stat_group,
  627. &lun->lun_group);
  628. target_stat_setup_port_default_groups(lun);
  629. return &lun->lun_group;
  630. }
  631. static void target_fabric_drop_lun(
  632. struct config_group *group,
  633. struct config_item *item)
  634. {
  635. struct se_lun *lun = container_of(to_config_group(item),
  636. struct se_lun, lun_group);
  637. configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
  638. configfs_remove_default_groups(&lun->lun_group);
  639. config_item_put(item);
  640. }
  641. static struct configfs_group_operations target_fabric_lun_group_ops = {
  642. .make_group = &target_fabric_make_lun,
  643. .drop_item = &target_fabric_drop_lun,
  644. };
  645. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  646. /* End of tfc_tpg_lun_cit */
  647. TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
  648. TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
  649. TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
  650. /* Start of tfc_tpg_base_cit */
  651. static void target_fabric_tpg_release(struct config_item *item)
  652. {
  653. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  654. struct se_portal_group, tpg_group);
  655. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  656. struct target_fabric_configfs *tf = wwn->wwn_tf;
  657. tf->tf_ops->fabric_drop_tpg(se_tpg);
  658. }
  659. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  660. .release = target_fabric_tpg_release,
  661. };
  662. TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
  663. /* End of tfc_tpg_base_cit */
  664. /* Start of tfc_tpg_cit */
  665. static struct config_group *target_fabric_make_tpg(
  666. struct config_group *group,
  667. const char *name)
  668. {
  669. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  670. struct target_fabric_configfs *tf = wwn->wwn_tf;
  671. struct se_portal_group *se_tpg;
  672. if (!tf->tf_ops->fabric_make_tpg) {
  673. pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
  674. return ERR_PTR(-ENOSYS);
  675. }
  676. se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
  677. if (!se_tpg || IS_ERR(se_tpg))
  678. return ERR_PTR(-EINVAL);
  679. config_group_init_type_name(&se_tpg->tpg_group, name,
  680. &tf->tf_tpg_base_cit);
  681. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  682. &tf->tf_tpg_lun_cit);
  683. configfs_add_default_group(&se_tpg->tpg_lun_group,
  684. &se_tpg->tpg_group);
  685. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  686. &tf->tf_tpg_np_cit);
  687. configfs_add_default_group(&se_tpg->tpg_np_group,
  688. &se_tpg->tpg_group);
  689. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  690. &tf->tf_tpg_nacl_cit);
  691. configfs_add_default_group(&se_tpg->tpg_acl_group,
  692. &se_tpg->tpg_group);
  693. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  694. &tf->tf_tpg_attrib_cit);
  695. configfs_add_default_group(&se_tpg->tpg_attrib_group,
  696. &se_tpg->tpg_group);
  697. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  698. &tf->tf_tpg_auth_cit);
  699. configfs_add_default_group(&se_tpg->tpg_auth_group,
  700. &se_tpg->tpg_group);
  701. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  702. &tf->tf_tpg_param_cit);
  703. configfs_add_default_group(&se_tpg->tpg_param_group,
  704. &se_tpg->tpg_group);
  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_portal_group *se_tpg = container_of(to_config_group(item),
  712. struct se_portal_group, tpg_group);
  713. configfs_remove_default_groups(&se_tpg->tpg_group);
  714. config_item_put(item);
  715. }
  716. static void target_fabric_release_wwn(struct config_item *item)
  717. {
  718. struct se_wwn *wwn = container_of(to_config_group(item),
  719. struct se_wwn, wwn_group);
  720. struct target_fabric_configfs *tf = wwn->wwn_tf;
  721. tf->tf_ops->fabric_drop_wwn(wwn);
  722. }
  723. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  724. .release = target_fabric_release_wwn,
  725. };
  726. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  727. .make_group = target_fabric_make_tpg,
  728. .drop_item = target_fabric_drop_tpg,
  729. };
  730. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  731. NULL);
  732. /* End of tfc_tpg_cit */
  733. /* Start of tfc_wwn_fabric_stats_cit */
  734. /*
  735. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  736. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  737. */
  738. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  739. /* End of tfc_wwn_fabric_stats_cit */
  740. /* Start of tfc_wwn_cit */
  741. static struct config_group *target_fabric_make_wwn(
  742. struct config_group *group,
  743. const char *name)
  744. {
  745. struct target_fabric_configfs *tf = container_of(group,
  746. struct target_fabric_configfs, tf_group);
  747. struct se_wwn *wwn;
  748. if (!tf->tf_ops->fabric_make_wwn) {
  749. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  750. return ERR_PTR(-ENOSYS);
  751. }
  752. wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
  753. if (!wwn || IS_ERR(wwn))
  754. return ERR_PTR(-EINVAL);
  755. wwn->wwn_tf = tf;
  756. config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
  757. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  758. &tf->tf_wwn_fabric_stats_cit);
  759. configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
  760. return &wwn->wwn_group;
  761. }
  762. static void target_fabric_drop_wwn(
  763. struct config_group *group,
  764. struct config_item *item)
  765. {
  766. struct se_wwn *wwn = container_of(to_config_group(item),
  767. struct se_wwn, wwn_group);
  768. configfs_remove_default_groups(&wwn->wwn_group);
  769. config_item_put(item);
  770. }
  771. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  772. .make_group = target_fabric_make_wwn,
  773. .drop_item = target_fabric_drop_wwn,
  774. };
  775. TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
  776. TF_CIT_SETUP_DRV(discovery, NULL, NULL);
  777. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  778. {
  779. target_fabric_setup_discovery_cit(tf);
  780. target_fabric_setup_wwn_cit(tf);
  781. target_fabric_setup_wwn_fabric_stats_cit(tf);
  782. target_fabric_setup_tpg_cit(tf);
  783. target_fabric_setup_tpg_base_cit(tf);
  784. target_fabric_setup_tpg_port_cit(tf);
  785. target_fabric_setup_tpg_port_stat_cit(tf);
  786. target_fabric_setup_tpg_lun_cit(tf);
  787. target_fabric_setup_tpg_np_cit(tf);
  788. target_fabric_setup_tpg_np_base_cit(tf);
  789. target_fabric_setup_tpg_attrib_cit(tf);
  790. target_fabric_setup_tpg_auth_cit(tf);
  791. target_fabric_setup_tpg_param_cit(tf);
  792. target_fabric_setup_tpg_nacl_cit(tf);
  793. target_fabric_setup_tpg_nacl_base_cit(tf);
  794. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  795. target_fabric_setup_tpg_nacl_auth_cit(tf);
  796. target_fabric_setup_tpg_nacl_param_cit(tf);
  797. target_fabric_setup_tpg_nacl_stat_cit(tf);
  798. target_fabric_setup_tpg_mappedlun_cit(tf);
  799. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  800. return 0;
  801. }