target_core_fabric_configfs.c 28 KB

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