target_core_fabric_configfs.c 27 KB

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