target_core_fabric_configfs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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 void 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. 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. configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
  294. core_tpg_del_initiator_node_acl(se_nacl);
  295. }
  296. static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
  297. .release = target_fabric_nacl_base_release,
  298. };
  299. static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
  300. .make_group = target_fabric_make_mappedlun,
  301. .drop_item = target_fabric_drop_mappedlun,
  302. };
  303. TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
  304. &target_fabric_nacl_base_group_ops);
  305. /* End of tfc_tpg_nacl_base_cit */
  306. /* Start of tfc_node_fabric_stats_cit */
  307. /*
  308. * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
  309. * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
  310. */
  311. TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
  312. /* End of tfc_wwn_fabric_stats_cit */
  313. /* Start of tfc_tpg_nacl_cit */
  314. static struct config_group *target_fabric_make_nodeacl(
  315. struct config_group *group,
  316. const char *name)
  317. {
  318. struct se_portal_group *se_tpg = container_of(group,
  319. struct se_portal_group, tpg_acl_group);
  320. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  321. struct se_node_acl *se_nacl;
  322. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
  323. if (IS_ERR(se_nacl))
  324. return ERR_CAST(se_nacl);
  325. config_group_init_type_name(&se_nacl->acl_group, name,
  326. &tf->tf_tpg_nacl_base_cit);
  327. config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
  328. &tf->tf_tpg_nacl_attrib_cit);
  329. configfs_add_default_group(&se_nacl->acl_attrib_group,
  330. &se_nacl->acl_group);
  331. config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
  332. &tf->tf_tpg_nacl_auth_cit);
  333. configfs_add_default_group(&se_nacl->acl_auth_group,
  334. &se_nacl->acl_group);
  335. config_group_init_type_name(&se_nacl->acl_param_group, "param",
  336. &tf->tf_tpg_nacl_param_cit);
  337. configfs_add_default_group(&se_nacl->acl_param_group,
  338. &se_nacl->acl_group);
  339. config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
  340. "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
  341. configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
  342. &se_nacl->acl_group);
  343. if (tf->tf_ops->fabric_init_nodeacl) {
  344. int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
  345. if (ret) {
  346. configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
  347. core_tpg_del_initiator_node_acl(se_nacl);
  348. return ERR_PTR(ret);
  349. }
  350. }
  351. return &se_nacl->acl_group;
  352. }
  353. static void target_fabric_drop_nodeacl(
  354. struct config_group *group,
  355. struct config_item *item)
  356. {
  357. struct se_node_acl *se_nacl = container_of(to_config_group(item),
  358. struct se_node_acl, acl_group);
  359. configfs_remove_default_groups(&se_nacl->acl_group);
  360. /*
  361. * struct se_node_acl free is done in target_fabric_nacl_base_release()
  362. */
  363. config_item_put(item);
  364. }
  365. static struct configfs_group_operations target_fabric_nacl_group_ops = {
  366. .make_group = target_fabric_make_nodeacl,
  367. .drop_item = target_fabric_drop_nodeacl,
  368. };
  369. TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
  370. /* End of tfc_tpg_nacl_cit */
  371. /* Start of tfc_tpg_np_base_cit */
  372. static void target_fabric_np_base_release(struct config_item *item)
  373. {
  374. struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
  375. struct se_tpg_np, tpg_np_group);
  376. struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
  377. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  378. tf->tf_ops->fabric_drop_np(se_tpg_np);
  379. }
  380. static struct configfs_item_operations target_fabric_np_base_item_ops = {
  381. .release = target_fabric_np_base_release,
  382. };
  383. TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
  384. /* End of tfc_tpg_np_base_cit */
  385. /* Start of tfc_tpg_np_cit */
  386. static struct config_group *target_fabric_make_np(
  387. struct config_group *group,
  388. const char *name)
  389. {
  390. struct se_portal_group *se_tpg = container_of(group,
  391. struct se_portal_group, tpg_np_group);
  392. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  393. struct se_tpg_np *se_tpg_np;
  394. if (!tf->tf_ops->fabric_make_np) {
  395. pr_err("tf->tf_ops.fabric_make_np is NULL\n");
  396. return ERR_PTR(-ENOSYS);
  397. }
  398. se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
  399. if (!se_tpg_np || IS_ERR(se_tpg_np))
  400. return ERR_PTR(-EINVAL);
  401. se_tpg_np->tpg_np_parent = se_tpg;
  402. config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
  403. &tf->tf_tpg_np_base_cit);
  404. return &se_tpg_np->tpg_np_group;
  405. }
  406. static void target_fabric_drop_np(
  407. struct config_group *group,
  408. struct config_item *item)
  409. {
  410. /*
  411. * struct se_tpg_np is released via target_fabric_np_base_release()
  412. */
  413. config_item_put(item);
  414. }
  415. static struct configfs_group_operations target_fabric_np_group_ops = {
  416. .make_group = &target_fabric_make_np,
  417. .drop_item = &target_fabric_drop_np,
  418. };
  419. TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
  420. /* End of tfc_tpg_np_cit */
  421. /* Start of tfc_tpg_port_cit */
  422. static struct se_lun *item_to_lun(struct config_item *item)
  423. {
  424. return container_of(to_config_group(item), struct se_lun,
  425. lun_group);
  426. }
  427. static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
  428. char *page)
  429. {
  430. struct se_lun *lun = item_to_lun(item);
  431. if (!lun || !lun->lun_se_dev)
  432. return -ENODEV;
  433. return core_alua_show_tg_pt_gp_info(lun, page);
  434. }
  435. static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
  436. const char *page, size_t count)
  437. {
  438. struct se_lun *lun = item_to_lun(item);
  439. if (!lun || !lun->lun_se_dev)
  440. return -ENODEV;
  441. return core_alua_store_tg_pt_gp_info(lun, page, count);
  442. }
  443. static ssize_t target_fabric_port_alua_tg_pt_offline_show(
  444. struct config_item *item, char *page)
  445. {
  446. struct se_lun *lun = item_to_lun(item);
  447. if (!lun || !lun->lun_se_dev)
  448. return -ENODEV;
  449. return core_alua_show_offline_bit(lun, page);
  450. }
  451. static ssize_t target_fabric_port_alua_tg_pt_offline_store(
  452. struct config_item *item, const char *page, size_t count)
  453. {
  454. struct se_lun *lun = item_to_lun(item);
  455. if (!lun || !lun->lun_se_dev)
  456. return -ENODEV;
  457. return core_alua_store_offline_bit(lun, page, count);
  458. }
  459. static ssize_t target_fabric_port_alua_tg_pt_status_show(
  460. struct config_item *item, char *page)
  461. {
  462. struct se_lun *lun = item_to_lun(item);
  463. if (!lun || !lun->lun_se_dev)
  464. return -ENODEV;
  465. return core_alua_show_secondary_status(lun, page);
  466. }
  467. static ssize_t target_fabric_port_alua_tg_pt_status_store(
  468. struct config_item *item, const char *page, size_t count)
  469. {
  470. struct se_lun *lun = item_to_lun(item);
  471. if (!lun || !lun->lun_se_dev)
  472. return -ENODEV;
  473. return core_alua_store_secondary_status(lun, page, count);
  474. }
  475. static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
  476. struct config_item *item, char *page)
  477. {
  478. struct se_lun *lun = item_to_lun(item);
  479. if (!lun || !lun->lun_se_dev)
  480. return -ENODEV;
  481. return core_alua_show_secondary_write_metadata(lun, page);
  482. }
  483. static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
  484. struct config_item *item, const char *page, size_t count)
  485. {
  486. struct se_lun *lun = item_to_lun(item);
  487. if (!lun || !lun->lun_se_dev)
  488. return -ENODEV;
  489. return core_alua_store_secondary_write_metadata(lun, page, count);
  490. }
  491. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
  492. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
  493. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
  494. CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
  495. static struct configfs_attribute *target_fabric_port_attrs[] = {
  496. &target_fabric_port_attr_alua_tg_pt_gp,
  497. &target_fabric_port_attr_alua_tg_pt_offline,
  498. &target_fabric_port_attr_alua_tg_pt_status,
  499. &target_fabric_port_attr_alua_tg_pt_write_md,
  500. NULL,
  501. };
  502. static int target_fabric_port_link(
  503. struct config_item *lun_ci,
  504. struct config_item *se_dev_ci)
  505. {
  506. struct config_item *tpg_ci;
  507. struct se_lun *lun = container_of(to_config_group(lun_ci),
  508. struct se_lun, lun_group);
  509. struct se_portal_group *se_tpg;
  510. struct se_device *dev =
  511. container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
  512. struct target_fabric_configfs *tf;
  513. int ret;
  514. if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
  515. pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
  516. " %p to struct se_device: %p\n", se_dev_ci, dev);
  517. return -EFAULT;
  518. }
  519. if (!(dev->dev_flags & DF_CONFIGURED)) {
  520. pr_err("se_device not configured yet, cannot port link\n");
  521. return -ENODEV;
  522. }
  523. tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
  524. se_tpg = container_of(to_config_group(tpg_ci),
  525. struct se_portal_group, tpg_group);
  526. tf = se_tpg->se_tpg_wwn->wwn_tf;
  527. if (lun->lun_se_dev != NULL) {
  528. pr_err("Port Symlink already exists\n");
  529. return -EEXIST;
  530. }
  531. ret = core_dev_add_lun(se_tpg, dev, lun);
  532. if (ret) {
  533. pr_err("core_dev_add_lun() failed: %d\n", ret);
  534. goto out;
  535. }
  536. if (tf->tf_ops->fabric_post_link) {
  537. /*
  538. * Call the optional fabric_post_link() to allow a
  539. * fabric module to setup any additional state once
  540. * core_dev_add_lun() has been called..
  541. */
  542. tf->tf_ops->fabric_post_link(se_tpg, lun);
  543. }
  544. return 0;
  545. out:
  546. return ret;
  547. }
  548. static void target_fabric_port_unlink(
  549. struct config_item *lun_ci,
  550. struct config_item *se_dev_ci)
  551. {
  552. struct se_lun *lun = container_of(to_config_group(lun_ci),
  553. struct se_lun, lun_group);
  554. struct se_portal_group *se_tpg = lun->lun_tpg;
  555. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  556. if (tf->tf_ops->fabric_pre_unlink) {
  557. /*
  558. * Call the optional fabric_pre_unlink() to allow a
  559. * fabric module to release any additional stat before
  560. * core_dev_del_lun() is called.
  561. */
  562. tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
  563. }
  564. core_dev_del_lun(se_tpg, lun);
  565. }
  566. static void target_fabric_port_release(struct config_item *item)
  567. {
  568. struct se_lun *lun = container_of(to_config_group(item),
  569. struct se_lun, lun_group);
  570. kfree_rcu(lun, rcu_head);
  571. }
  572. static struct configfs_item_operations target_fabric_port_item_ops = {
  573. .release = target_fabric_port_release,
  574. .allow_link = target_fabric_port_link,
  575. .drop_link = target_fabric_port_unlink,
  576. };
  577. TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
  578. /* End of tfc_tpg_port_cit */
  579. /* Start of tfc_tpg_port_stat_cit */
  580. static struct config_group *target_core_port_stat_mkdir(
  581. struct config_group *group,
  582. const char *name)
  583. {
  584. return ERR_PTR(-ENOSYS);
  585. }
  586. static void target_core_port_stat_rmdir(
  587. struct config_group *group,
  588. struct config_item *item)
  589. {
  590. return;
  591. }
  592. static struct configfs_group_operations target_fabric_port_stat_group_ops = {
  593. .make_group = target_core_port_stat_mkdir,
  594. .drop_item = target_core_port_stat_rmdir,
  595. };
  596. TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
  597. /* End of tfc_tpg_port_stat_cit */
  598. /* Start of tfc_tpg_lun_cit */
  599. static struct config_group *target_fabric_make_lun(
  600. struct config_group *group,
  601. const char *name)
  602. {
  603. struct se_lun *lun;
  604. struct se_portal_group *se_tpg = container_of(group,
  605. struct se_portal_group, tpg_lun_group);
  606. struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
  607. unsigned long long unpacked_lun;
  608. int errno;
  609. if (strstr(name, "lun_") != name) {
  610. pr_err("Unable to locate \'_\" in"
  611. " \"lun_$LUN_NUMBER\"\n");
  612. return ERR_PTR(-EINVAL);
  613. }
  614. errno = kstrtoull(name + 4, 0, &unpacked_lun);
  615. if (errno)
  616. return ERR_PTR(errno);
  617. lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
  618. if (IS_ERR(lun))
  619. return ERR_CAST(lun);
  620. config_group_init_type_name(&lun->lun_group, name,
  621. &tf->tf_tpg_port_cit);
  622. config_group_init_type_name(&lun->port_stat_grps.stat_group,
  623. "statistics", &tf->tf_tpg_port_stat_cit);
  624. configfs_add_default_group(&lun->port_stat_grps.stat_group,
  625. &lun->lun_group);
  626. target_stat_setup_port_default_groups(lun);
  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. struct se_lun *lun = container_of(to_config_group(item),
  634. struct se_lun, lun_group);
  635. configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
  636. configfs_remove_default_groups(&lun->lun_group);
  637. config_item_put(item);
  638. }
  639. static struct configfs_group_operations target_fabric_lun_group_ops = {
  640. .make_group = &target_fabric_make_lun,
  641. .drop_item = &target_fabric_drop_lun,
  642. };
  643. TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
  644. /* End of tfc_tpg_lun_cit */
  645. TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
  646. TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
  647. TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
  648. /* Start of tfc_tpg_base_cit */
  649. static void target_fabric_tpg_release(struct config_item *item)
  650. {
  651. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  652. struct se_portal_group, tpg_group);
  653. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  654. struct target_fabric_configfs *tf = wwn->wwn_tf;
  655. tf->tf_ops->fabric_drop_tpg(se_tpg);
  656. }
  657. static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
  658. .release = target_fabric_tpg_release,
  659. };
  660. TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
  661. /* End of tfc_tpg_base_cit */
  662. /* Start of tfc_tpg_cit */
  663. static struct config_group *target_fabric_make_tpg(
  664. struct config_group *group,
  665. const char *name)
  666. {
  667. struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
  668. struct target_fabric_configfs *tf = wwn->wwn_tf;
  669. struct se_portal_group *se_tpg;
  670. if (!tf->tf_ops->fabric_make_tpg) {
  671. pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
  672. return ERR_PTR(-ENOSYS);
  673. }
  674. se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
  675. if (!se_tpg || IS_ERR(se_tpg))
  676. return ERR_PTR(-EINVAL);
  677. config_group_init_type_name(&se_tpg->tpg_group, name,
  678. &tf->tf_tpg_base_cit);
  679. config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
  680. &tf->tf_tpg_lun_cit);
  681. configfs_add_default_group(&se_tpg->tpg_lun_group,
  682. &se_tpg->tpg_group);
  683. config_group_init_type_name(&se_tpg->tpg_np_group, "np",
  684. &tf->tf_tpg_np_cit);
  685. configfs_add_default_group(&se_tpg->tpg_np_group,
  686. &se_tpg->tpg_group);
  687. config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
  688. &tf->tf_tpg_nacl_cit);
  689. configfs_add_default_group(&se_tpg->tpg_acl_group,
  690. &se_tpg->tpg_group);
  691. config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
  692. &tf->tf_tpg_attrib_cit);
  693. configfs_add_default_group(&se_tpg->tpg_attrib_group,
  694. &se_tpg->tpg_group);
  695. config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
  696. &tf->tf_tpg_auth_cit);
  697. configfs_add_default_group(&se_tpg->tpg_auth_group,
  698. &se_tpg->tpg_group);
  699. config_group_init_type_name(&se_tpg->tpg_param_group, "param",
  700. &tf->tf_tpg_param_cit);
  701. configfs_add_default_group(&se_tpg->tpg_param_group,
  702. &se_tpg->tpg_group);
  703. return &se_tpg->tpg_group;
  704. }
  705. static void target_fabric_drop_tpg(
  706. struct config_group *group,
  707. struct config_item *item)
  708. {
  709. struct se_portal_group *se_tpg = container_of(to_config_group(item),
  710. struct se_portal_group, tpg_group);
  711. configfs_remove_default_groups(&se_tpg->tpg_group);
  712. config_item_put(item);
  713. }
  714. static void target_fabric_release_wwn(struct config_item *item)
  715. {
  716. struct se_wwn *wwn = container_of(to_config_group(item),
  717. struct se_wwn, wwn_group);
  718. struct target_fabric_configfs *tf = wwn->wwn_tf;
  719. configfs_remove_default_groups(&wwn->fabric_stat_group);
  720. tf->tf_ops->fabric_drop_wwn(wwn);
  721. }
  722. static struct configfs_item_operations target_fabric_tpg_item_ops = {
  723. .release = target_fabric_release_wwn,
  724. };
  725. static struct configfs_group_operations target_fabric_tpg_group_ops = {
  726. .make_group = target_fabric_make_tpg,
  727. .drop_item = target_fabric_drop_tpg,
  728. };
  729. TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
  730. NULL);
  731. /* End of tfc_tpg_cit */
  732. /* Start of tfc_wwn_fabric_stats_cit */
  733. /*
  734. * This is used as a placeholder for struct se_wwn->fabric_stat_group
  735. * to allow fabrics access to ->fabric_stat_group->default_groups[]
  736. */
  737. TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
  738. /* End of tfc_wwn_fabric_stats_cit */
  739. /* Start of tfc_wwn_cit */
  740. static struct config_group *target_fabric_make_wwn(
  741. struct config_group *group,
  742. const char *name)
  743. {
  744. struct target_fabric_configfs *tf = container_of(group,
  745. struct target_fabric_configfs, tf_group);
  746. struct se_wwn *wwn;
  747. if (!tf->tf_ops->fabric_make_wwn) {
  748. pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
  749. return ERR_PTR(-ENOSYS);
  750. }
  751. wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
  752. if (!wwn || IS_ERR(wwn))
  753. return ERR_PTR(-EINVAL);
  754. wwn->wwn_tf = tf;
  755. config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
  756. config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
  757. &tf->tf_wwn_fabric_stats_cit);
  758. configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
  759. if (tf->tf_ops->add_wwn_groups)
  760. tf->tf_ops->add_wwn_groups(wwn);
  761. return &wwn->wwn_group;
  762. }
  763. static void target_fabric_drop_wwn(
  764. struct config_group *group,
  765. struct config_item *item)
  766. {
  767. struct se_wwn *wwn = container_of(to_config_group(item),
  768. struct se_wwn, wwn_group);
  769. configfs_remove_default_groups(&wwn->wwn_group);
  770. config_item_put(item);
  771. }
  772. static struct configfs_group_operations target_fabric_wwn_group_ops = {
  773. .make_group = target_fabric_make_wwn,
  774. .drop_item = target_fabric_drop_wwn,
  775. };
  776. TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
  777. TF_CIT_SETUP_DRV(discovery, NULL, NULL);
  778. int target_fabric_setup_cits(struct target_fabric_configfs *tf)
  779. {
  780. target_fabric_setup_discovery_cit(tf);
  781. target_fabric_setup_wwn_cit(tf);
  782. target_fabric_setup_wwn_fabric_stats_cit(tf);
  783. target_fabric_setup_tpg_cit(tf);
  784. target_fabric_setup_tpg_base_cit(tf);
  785. target_fabric_setup_tpg_port_cit(tf);
  786. target_fabric_setup_tpg_port_stat_cit(tf);
  787. target_fabric_setup_tpg_lun_cit(tf);
  788. target_fabric_setup_tpg_np_cit(tf);
  789. target_fabric_setup_tpg_np_base_cit(tf);
  790. target_fabric_setup_tpg_attrib_cit(tf);
  791. target_fabric_setup_tpg_auth_cit(tf);
  792. target_fabric_setup_tpg_param_cit(tf);
  793. target_fabric_setup_tpg_nacl_cit(tf);
  794. target_fabric_setup_tpg_nacl_base_cit(tf);
  795. target_fabric_setup_tpg_nacl_attrib_cit(tf);
  796. target_fabric_setup_tpg_nacl_auth_cit(tf);
  797. target_fabric_setup_tpg_nacl_param_cit(tf);
  798. target_fabric_setup_tpg_nacl_stat_cit(tf);
  799. target_fabric_setup_tpg_mappedlun_cit(tf);
  800. target_fabric_setup_tpg_mappedlun_stat_cit(tf);
  801. return 0;
  802. }