target_core_configfs.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010
  1. /*******************************************************************************
  2. * Filename: target_core_configfs.c
  3. *
  4. * This file contains ConfigFS logic for the Generic Target Engine project.
  5. *
  6. * (c) Copyright 2008-2012 RisingTide Systems LLC.
  7. *
  8. * Nicholas A. Bellinger <nab@kernel.org>
  9. *
  10. * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. ****************************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <generated/utsrelease.h>
  25. #include <linux/utsname.h>
  26. #include <linux/init.h>
  27. #include <linux/fs.h>
  28. #include <linux/namei.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/delay.h>
  32. #include <linux/unistd.h>
  33. #include <linux/string.h>
  34. #include <linux/parser.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/configfs.h>
  37. #include <linux/spinlock.h>
  38. #include <target/target_core_base.h>
  39. #include <target/target_core_backend.h>
  40. #include <target/target_core_fabric.h>
  41. #include <target/target_core_fabric_configfs.h>
  42. #include <target/target_core_configfs.h>
  43. #include <target/configfs_macros.h>
  44. #include "target_core_internal.h"
  45. #include "target_core_alua.h"
  46. #include "target_core_pr.h"
  47. #include "target_core_rd.h"
  48. extern struct t10_alua_lu_gp *default_lu_gp;
  49. static LIST_HEAD(g_tf_list);
  50. static DEFINE_MUTEX(g_tf_lock);
  51. struct target_core_configfs_attribute {
  52. struct configfs_attribute attr;
  53. ssize_t (*show)(void *, char *);
  54. ssize_t (*store)(void *, const char *, size_t);
  55. };
  56. static struct config_group target_core_hbagroup;
  57. static struct config_group alua_group;
  58. static struct config_group alua_lu_gps_group;
  59. static inline struct se_hba *
  60. item_to_hba(struct config_item *item)
  61. {
  62. return container_of(to_config_group(item), struct se_hba, hba_group);
  63. }
  64. /*
  65. * Attributes for /sys/kernel/config/target/
  66. */
  67. static ssize_t target_core_attr_show(struct config_item *item,
  68. struct configfs_attribute *attr,
  69. char *page)
  70. {
  71. return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
  72. " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
  73. utsname()->sysname, utsname()->machine);
  74. }
  75. static struct configfs_item_operations target_core_fabric_item_ops = {
  76. .show_attribute = target_core_attr_show,
  77. };
  78. static struct configfs_attribute target_core_item_attr_version = {
  79. .ca_owner = THIS_MODULE,
  80. .ca_name = "version",
  81. .ca_mode = S_IRUGO,
  82. };
  83. static struct target_fabric_configfs *target_core_get_fabric(
  84. const char *name)
  85. {
  86. struct target_fabric_configfs *tf;
  87. if (!name)
  88. return NULL;
  89. mutex_lock(&g_tf_lock);
  90. list_for_each_entry(tf, &g_tf_list, tf_list) {
  91. if (!strcmp(tf->tf_name, name)) {
  92. atomic_inc(&tf->tf_access_cnt);
  93. mutex_unlock(&g_tf_lock);
  94. return tf;
  95. }
  96. }
  97. mutex_unlock(&g_tf_lock);
  98. return NULL;
  99. }
  100. /*
  101. * Called from struct target_core_group_ops->make_group()
  102. */
  103. static struct config_group *target_core_register_fabric(
  104. struct config_group *group,
  105. const char *name)
  106. {
  107. struct target_fabric_configfs *tf;
  108. int ret;
  109. pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
  110. " %s\n", group, name);
  111. /*
  112. * Below are some hardcoded request_module() calls to automatically
  113. * local fabric modules when the following is called:
  114. *
  115. * mkdir -p /sys/kernel/config/target/$MODULE_NAME
  116. *
  117. * Note that this does not limit which TCM fabric module can be
  118. * registered, but simply provids auto loading logic for modules with
  119. * mkdir(2) system calls with known TCM fabric modules.
  120. */
  121. if (!strncmp(name, "iscsi", 5)) {
  122. /*
  123. * Automatically load the LIO Target fabric module when the
  124. * following is called:
  125. *
  126. * mkdir -p $CONFIGFS/target/iscsi
  127. */
  128. ret = request_module("iscsi_target_mod");
  129. if (ret < 0) {
  130. pr_err("request_module() failed for"
  131. " iscsi_target_mod.ko: %d\n", ret);
  132. return ERR_PTR(-EINVAL);
  133. }
  134. } else if (!strncmp(name, "loopback", 8)) {
  135. /*
  136. * Automatically load the tcm_loop fabric module when the
  137. * following is called:
  138. *
  139. * mkdir -p $CONFIGFS/target/loopback
  140. */
  141. ret = request_module("tcm_loop");
  142. if (ret < 0) {
  143. pr_err("request_module() failed for"
  144. " tcm_loop.ko: %d\n", ret);
  145. return ERR_PTR(-EINVAL);
  146. }
  147. }
  148. tf = target_core_get_fabric(name);
  149. if (!tf) {
  150. pr_err("target_core_get_fabric() failed for %s\n",
  151. name);
  152. return ERR_PTR(-EINVAL);
  153. }
  154. pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
  155. " %s\n", tf->tf_name);
  156. /*
  157. * On a successful target_core_get_fabric() look, the returned
  158. * struct target_fabric_configfs *tf will contain a usage reference.
  159. */
  160. pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
  161. &TF_CIT_TMPL(tf)->tfc_wwn_cit);
  162. tf->tf_group.default_groups = tf->tf_default_groups;
  163. tf->tf_group.default_groups[0] = &tf->tf_disc_group;
  164. tf->tf_group.default_groups[1] = NULL;
  165. config_group_init_type_name(&tf->tf_group, name,
  166. &TF_CIT_TMPL(tf)->tfc_wwn_cit);
  167. config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
  168. &TF_CIT_TMPL(tf)->tfc_discovery_cit);
  169. pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
  170. " %s\n", tf->tf_group.cg_item.ci_name);
  171. /*
  172. * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
  173. */
  174. tf->tf_ops.tf_subsys = tf->tf_subsys;
  175. tf->tf_fabric = &tf->tf_group.cg_item;
  176. pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
  177. " for %s\n", name);
  178. return &tf->tf_group;
  179. }
  180. /*
  181. * Called from struct target_core_group_ops->drop_item()
  182. */
  183. static void target_core_deregister_fabric(
  184. struct config_group *group,
  185. struct config_item *item)
  186. {
  187. struct target_fabric_configfs *tf = container_of(
  188. to_config_group(item), struct target_fabric_configfs, tf_group);
  189. struct config_group *tf_group;
  190. struct config_item *df_item;
  191. int i;
  192. pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
  193. " tf list\n", config_item_name(item));
  194. pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
  195. " %s\n", tf->tf_name);
  196. atomic_dec(&tf->tf_access_cnt);
  197. pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
  198. " tf->tf_fabric for %s\n", tf->tf_name);
  199. tf->tf_fabric = NULL;
  200. pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
  201. " %s\n", config_item_name(item));
  202. tf_group = &tf->tf_group;
  203. for (i = 0; tf_group->default_groups[i]; i++) {
  204. df_item = &tf_group->default_groups[i]->cg_item;
  205. tf_group->default_groups[i] = NULL;
  206. config_item_put(df_item);
  207. }
  208. config_item_put(item);
  209. }
  210. static struct configfs_group_operations target_core_fabric_group_ops = {
  211. .make_group = &target_core_register_fabric,
  212. .drop_item = &target_core_deregister_fabric,
  213. };
  214. /*
  215. * All item attributes appearing in /sys/kernel/target/ appear here.
  216. */
  217. static struct configfs_attribute *target_core_fabric_item_attrs[] = {
  218. &target_core_item_attr_version,
  219. NULL,
  220. };
  221. /*
  222. * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
  223. */
  224. static struct config_item_type target_core_fabrics_item = {
  225. .ct_item_ops = &target_core_fabric_item_ops,
  226. .ct_group_ops = &target_core_fabric_group_ops,
  227. .ct_attrs = target_core_fabric_item_attrs,
  228. .ct_owner = THIS_MODULE,
  229. };
  230. static struct configfs_subsystem target_core_fabrics = {
  231. .su_group = {
  232. .cg_item = {
  233. .ci_namebuf = "target",
  234. .ci_type = &target_core_fabrics_item,
  235. },
  236. },
  237. };
  238. static struct configfs_subsystem *target_core_subsystem[] = {
  239. &target_core_fabrics,
  240. NULL,
  241. };
  242. /*##############################################################################
  243. // Start functions called by external Target Fabrics Modules
  244. //############################################################################*/
  245. /*
  246. * First function called by fabric modules to:
  247. *
  248. * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
  249. * 2) Add struct target_fabric_configfs to g_tf_list
  250. * 3) Return struct target_fabric_configfs to fabric module to be passed
  251. * into target_fabric_configfs_register().
  252. */
  253. struct target_fabric_configfs *target_fabric_configfs_init(
  254. struct module *fabric_mod,
  255. const char *name)
  256. {
  257. struct target_fabric_configfs *tf;
  258. if (!(name)) {
  259. pr_err("Unable to locate passed fabric name\n");
  260. return ERR_PTR(-EINVAL);
  261. }
  262. if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
  263. pr_err("Passed name: %s exceeds TARGET_FABRIC"
  264. "_NAME_SIZE\n", name);
  265. return ERR_PTR(-EINVAL);
  266. }
  267. tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
  268. if (!tf)
  269. return ERR_PTR(-ENOMEM);
  270. INIT_LIST_HEAD(&tf->tf_list);
  271. atomic_set(&tf->tf_access_cnt, 0);
  272. /*
  273. * Setup the default generic struct config_item_type's (cits) in
  274. * struct target_fabric_configfs->tf_cit_tmpl
  275. */
  276. tf->tf_module = fabric_mod;
  277. target_fabric_setup_cits(tf);
  278. tf->tf_subsys = target_core_subsystem[0];
  279. snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
  280. mutex_lock(&g_tf_lock);
  281. list_add_tail(&tf->tf_list, &g_tf_list);
  282. mutex_unlock(&g_tf_lock);
  283. pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
  284. ">>>>>>>>>>>>>>\n");
  285. pr_debug("Initialized struct target_fabric_configfs: %p for"
  286. " %s\n", tf, tf->tf_name);
  287. return tf;
  288. }
  289. EXPORT_SYMBOL(target_fabric_configfs_init);
  290. /*
  291. * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
  292. */
  293. void target_fabric_configfs_free(
  294. struct target_fabric_configfs *tf)
  295. {
  296. mutex_lock(&g_tf_lock);
  297. list_del(&tf->tf_list);
  298. mutex_unlock(&g_tf_lock);
  299. kfree(tf);
  300. }
  301. EXPORT_SYMBOL(target_fabric_configfs_free);
  302. /*
  303. * Perform a sanity check of the passed tf->tf_ops before completing
  304. * TCM fabric module registration.
  305. */
  306. static int target_fabric_tf_ops_check(
  307. struct target_fabric_configfs *tf)
  308. {
  309. struct target_core_fabric_ops *tfo = &tf->tf_ops;
  310. if (!tfo->get_fabric_name) {
  311. pr_err("Missing tfo->get_fabric_name()\n");
  312. return -EINVAL;
  313. }
  314. if (!tfo->get_fabric_proto_ident) {
  315. pr_err("Missing tfo->get_fabric_proto_ident()\n");
  316. return -EINVAL;
  317. }
  318. if (!tfo->tpg_get_wwn) {
  319. pr_err("Missing tfo->tpg_get_wwn()\n");
  320. return -EINVAL;
  321. }
  322. if (!tfo->tpg_get_tag) {
  323. pr_err("Missing tfo->tpg_get_tag()\n");
  324. return -EINVAL;
  325. }
  326. if (!tfo->tpg_get_default_depth) {
  327. pr_err("Missing tfo->tpg_get_default_depth()\n");
  328. return -EINVAL;
  329. }
  330. if (!tfo->tpg_get_pr_transport_id) {
  331. pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
  332. return -EINVAL;
  333. }
  334. if (!tfo->tpg_get_pr_transport_id_len) {
  335. pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
  336. return -EINVAL;
  337. }
  338. if (!tfo->tpg_check_demo_mode) {
  339. pr_err("Missing tfo->tpg_check_demo_mode()\n");
  340. return -EINVAL;
  341. }
  342. if (!tfo->tpg_check_demo_mode_cache) {
  343. pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
  344. return -EINVAL;
  345. }
  346. if (!tfo->tpg_check_demo_mode_write_protect) {
  347. pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
  348. return -EINVAL;
  349. }
  350. if (!tfo->tpg_check_prod_mode_write_protect) {
  351. pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
  352. return -EINVAL;
  353. }
  354. if (!tfo->tpg_alloc_fabric_acl) {
  355. pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
  356. return -EINVAL;
  357. }
  358. if (!tfo->tpg_release_fabric_acl) {
  359. pr_err("Missing tfo->tpg_release_fabric_acl()\n");
  360. return -EINVAL;
  361. }
  362. if (!tfo->tpg_get_inst_index) {
  363. pr_err("Missing tfo->tpg_get_inst_index()\n");
  364. return -EINVAL;
  365. }
  366. if (!tfo->release_cmd) {
  367. pr_err("Missing tfo->release_cmd()\n");
  368. return -EINVAL;
  369. }
  370. if (!tfo->shutdown_session) {
  371. pr_err("Missing tfo->shutdown_session()\n");
  372. return -EINVAL;
  373. }
  374. if (!tfo->close_session) {
  375. pr_err("Missing tfo->close_session()\n");
  376. return -EINVAL;
  377. }
  378. if (!tfo->sess_get_index) {
  379. pr_err("Missing tfo->sess_get_index()\n");
  380. return -EINVAL;
  381. }
  382. if (!tfo->write_pending) {
  383. pr_err("Missing tfo->write_pending()\n");
  384. return -EINVAL;
  385. }
  386. if (!tfo->write_pending_status) {
  387. pr_err("Missing tfo->write_pending_status()\n");
  388. return -EINVAL;
  389. }
  390. if (!tfo->set_default_node_attributes) {
  391. pr_err("Missing tfo->set_default_node_attributes()\n");
  392. return -EINVAL;
  393. }
  394. if (!tfo->get_task_tag) {
  395. pr_err("Missing tfo->get_task_tag()\n");
  396. return -EINVAL;
  397. }
  398. if (!tfo->get_cmd_state) {
  399. pr_err("Missing tfo->get_cmd_state()\n");
  400. return -EINVAL;
  401. }
  402. if (!tfo->queue_data_in) {
  403. pr_err("Missing tfo->queue_data_in()\n");
  404. return -EINVAL;
  405. }
  406. if (!tfo->queue_status) {
  407. pr_err("Missing tfo->queue_status()\n");
  408. return -EINVAL;
  409. }
  410. if (!tfo->queue_tm_rsp) {
  411. pr_err("Missing tfo->queue_tm_rsp()\n");
  412. return -EINVAL;
  413. }
  414. /*
  415. * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
  416. * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
  417. * target_core_fabric_configfs.c WWN+TPG group context code.
  418. */
  419. if (!tfo->fabric_make_wwn) {
  420. pr_err("Missing tfo->fabric_make_wwn()\n");
  421. return -EINVAL;
  422. }
  423. if (!tfo->fabric_drop_wwn) {
  424. pr_err("Missing tfo->fabric_drop_wwn()\n");
  425. return -EINVAL;
  426. }
  427. if (!tfo->fabric_make_tpg) {
  428. pr_err("Missing tfo->fabric_make_tpg()\n");
  429. return -EINVAL;
  430. }
  431. if (!tfo->fabric_drop_tpg) {
  432. pr_err("Missing tfo->fabric_drop_tpg()\n");
  433. return -EINVAL;
  434. }
  435. return 0;
  436. }
  437. /*
  438. * Called 2nd from fabric module with returned parameter of
  439. * struct target_fabric_configfs * from target_fabric_configfs_init().
  440. *
  441. * Upon a successful registration, the new fabric's struct config_item is
  442. * return. Also, a pointer to this struct is set in the passed
  443. * struct target_fabric_configfs.
  444. */
  445. int target_fabric_configfs_register(
  446. struct target_fabric_configfs *tf)
  447. {
  448. int ret;
  449. if (!tf) {
  450. pr_err("Unable to locate target_fabric_configfs"
  451. " pointer\n");
  452. return -EINVAL;
  453. }
  454. if (!tf->tf_subsys) {
  455. pr_err("Unable to target struct config_subsystem"
  456. " pointer\n");
  457. return -EINVAL;
  458. }
  459. ret = target_fabric_tf_ops_check(tf);
  460. if (ret < 0)
  461. return ret;
  462. pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
  463. ">>>>>>>>>>\n");
  464. return 0;
  465. }
  466. EXPORT_SYMBOL(target_fabric_configfs_register);
  467. void target_fabric_configfs_deregister(
  468. struct target_fabric_configfs *tf)
  469. {
  470. struct configfs_subsystem *su;
  471. if (!tf) {
  472. pr_err("Unable to locate passed target_fabric_"
  473. "configfs\n");
  474. return;
  475. }
  476. su = tf->tf_subsys;
  477. if (!su) {
  478. pr_err("Unable to locate passed tf->tf_subsys"
  479. " pointer\n");
  480. return;
  481. }
  482. pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
  483. ">>>>>>>>>>>>\n");
  484. mutex_lock(&g_tf_lock);
  485. if (atomic_read(&tf->tf_access_cnt)) {
  486. mutex_unlock(&g_tf_lock);
  487. pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
  488. tf->tf_name);
  489. BUG();
  490. }
  491. list_del(&tf->tf_list);
  492. mutex_unlock(&g_tf_lock);
  493. pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
  494. " %s\n", tf->tf_name);
  495. tf->tf_module = NULL;
  496. tf->tf_subsys = NULL;
  497. kfree(tf);
  498. pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
  499. ">>>>>\n");
  500. }
  501. EXPORT_SYMBOL(target_fabric_configfs_deregister);
  502. /*##############################################################################
  503. // Stop functions called by external Target Fabrics Modules
  504. //############################################################################*/
  505. /* Start functions for struct config_item_type target_core_dev_attrib_cit */
  506. #define DEF_DEV_ATTRIB_SHOW(_name) \
  507. static ssize_t target_core_dev_show_attr_##_name( \
  508. struct se_dev_attrib *da, \
  509. char *page) \
  510. { \
  511. return snprintf(page, PAGE_SIZE, "%u\n", \
  512. (u32)da->da_dev->dev_attrib._name); \
  513. }
  514. #define DEF_DEV_ATTRIB_STORE(_name) \
  515. static ssize_t target_core_dev_store_attr_##_name( \
  516. struct se_dev_attrib *da, \
  517. const char *page, \
  518. size_t count) \
  519. { \
  520. unsigned long val; \
  521. int ret; \
  522. \
  523. ret = strict_strtoul(page, 0, &val); \
  524. if (ret < 0) { \
  525. pr_err("strict_strtoul() failed with" \
  526. " ret: %d\n", ret); \
  527. return -EINVAL; \
  528. } \
  529. ret = se_dev_set_##_name(da->da_dev, (u32)val); \
  530. \
  531. return (!ret) ? count : -EINVAL; \
  532. }
  533. #define DEF_DEV_ATTRIB(_name) \
  534. DEF_DEV_ATTRIB_SHOW(_name); \
  535. DEF_DEV_ATTRIB_STORE(_name);
  536. #define DEF_DEV_ATTRIB_RO(_name) \
  537. DEF_DEV_ATTRIB_SHOW(_name);
  538. CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
  539. #define SE_DEV_ATTR(_name, _mode) \
  540. static struct target_core_dev_attrib_attribute \
  541. target_core_dev_attrib_##_name = \
  542. __CONFIGFS_EATTR(_name, _mode, \
  543. target_core_dev_show_attr_##_name, \
  544. target_core_dev_store_attr_##_name);
  545. #define SE_DEV_ATTR_RO(_name); \
  546. static struct target_core_dev_attrib_attribute \
  547. target_core_dev_attrib_##_name = \
  548. __CONFIGFS_EATTR_RO(_name, \
  549. target_core_dev_show_attr_##_name);
  550. DEF_DEV_ATTRIB(emulate_model_alias);
  551. SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
  552. DEF_DEV_ATTRIB(emulate_dpo);
  553. SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
  554. DEF_DEV_ATTRIB(emulate_fua_write);
  555. SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
  556. DEF_DEV_ATTRIB(emulate_fua_read);
  557. SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
  558. DEF_DEV_ATTRIB(emulate_write_cache);
  559. SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
  560. DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
  561. SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
  562. DEF_DEV_ATTRIB(emulate_tas);
  563. SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
  564. DEF_DEV_ATTRIB(emulate_tpu);
  565. SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
  566. DEF_DEV_ATTRIB(emulate_tpws);
  567. SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
  568. DEF_DEV_ATTRIB(enforce_pr_isids);
  569. SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
  570. DEF_DEV_ATTRIB(is_nonrot);
  571. SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
  572. DEF_DEV_ATTRIB(emulate_rest_reord);
  573. SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
  574. DEF_DEV_ATTRIB_RO(hw_block_size);
  575. SE_DEV_ATTR_RO(hw_block_size);
  576. DEF_DEV_ATTRIB(block_size);
  577. SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
  578. DEF_DEV_ATTRIB_RO(hw_max_sectors);
  579. SE_DEV_ATTR_RO(hw_max_sectors);
  580. DEF_DEV_ATTRIB(fabric_max_sectors);
  581. SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
  582. DEF_DEV_ATTRIB(optimal_sectors);
  583. SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
  584. DEF_DEV_ATTRIB_RO(hw_queue_depth);
  585. SE_DEV_ATTR_RO(hw_queue_depth);
  586. DEF_DEV_ATTRIB(queue_depth);
  587. SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
  588. DEF_DEV_ATTRIB(max_unmap_lba_count);
  589. SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
  590. DEF_DEV_ATTRIB(max_unmap_block_desc_count);
  591. SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
  592. DEF_DEV_ATTRIB(unmap_granularity);
  593. SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
  594. DEF_DEV_ATTRIB(unmap_granularity_alignment);
  595. SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
  596. DEF_DEV_ATTRIB(max_write_same_len);
  597. SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
  598. CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
  599. static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
  600. &target_core_dev_attrib_emulate_model_alias.attr,
  601. &target_core_dev_attrib_emulate_dpo.attr,
  602. &target_core_dev_attrib_emulate_fua_write.attr,
  603. &target_core_dev_attrib_emulate_fua_read.attr,
  604. &target_core_dev_attrib_emulate_write_cache.attr,
  605. &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
  606. &target_core_dev_attrib_emulate_tas.attr,
  607. &target_core_dev_attrib_emulate_tpu.attr,
  608. &target_core_dev_attrib_emulate_tpws.attr,
  609. &target_core_dev_attrib_enforce_pr_isids.attr,
  610. &target_core_dev_attrib_is_nonrot.attr,
  611. &target_core_dev_attrib_emulate_rest_reord.attr,
  612. &target_core_dev_attrib_hw_block_size.attr,
  613. &target_core_dev_attrib_block_size.attr,
  614. &target_core_dev_attrib_hw_max_sectors.attr,
  615. &target_core_dev_attrib_fabric_max_sectors.attr,
  616. &target_core_dev_attrib_optimal_sectors.attr,
  617. &target_core_dev_attrib_hw_queue_depth.attr,
  618. &target_core_dev_attrib_queue_depth.attr,
  619. &target_core_dev_attrib_max_unmap_lba_count.attr,
  620. &target_core_dev_attrib_max_unmap_block_desc_count.attr,
  621. &target_core_dev_attrib_unmap_granularity.attr,
  622. &target_core_dev_attrib_unmap_granularity_alignment.attr,
  623. &target_core_dev_attrib_max_write_same_len.attr,
  624. NULL,
  625. };
  626. static struct configfs_item_operations target_core_dev_attrib_ops = {
  627. .show_attribute = target_core_dev_attrib_attr_show,
  628. .store_attribute = target_core_dev_attrib_attr_store,
  629. };
  630. static struct config_item_type target_core_dev_attrib_cit = {
  631. .ct_item_ops = &target_core_dev_attrib_ops,
  632. .ct_attrs = target_core_dev_attrib_attrs,
  633. .ct_owner = THIS_MODULE,
  634. };
  635. /* End functions for struct config_item_type target_core_dev_attrib_cit */
  636. /* Start functions for struct config_item_type target_core_dev_wwn_cit */
  637. CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
  638. #define SE_DEV_WWN_ATTR(_name, _mode) \
  639. static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
  640. __CONFIGFS_EATTR(_name, _mode, \
  641. target_core_dev_wwn_show_attr_##_name, \
  642. target_core_dev_wwn_store_attr_##_name);
  643. #define SE_DEV_WWN_ATTR_RO(_name); \
  644. do { \
  645. static struct target_core_dev_wwn_attribute \
  646. target_core_dev_wwn_##_name = \
  647. __CONFIGFS_EATTR_RO(_name, \
  648. target_core_dev_wwn_show_attr_##_name); \
  649. } while (0);
  650. /*
  651. * VPD page 0x80 Unit serial
  652. */
  653. static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
  654. struct t10_wwn *t10_wwn,
  655. char *page)
  656. {
  657. return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
  658. &t10_wwn->unit_serial[0]);
  659. }
  660. static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
  661. struct t10_wwn *t10_wwn,
  662. const char *page,
  663. size_t count)
  664. {
  665. struct se_device *dev = t10_wwn->t10_dev;
  666. unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
  667. /*
  668. * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
  669. * from the struct scsi_device level firmware, do not allow
  670. * VPD Unit Serial to be emulated.
  671. *
  672. * Note this struct scsi_device could also be emulating VPD
  673. * information from its drivers/scsi LLD. But for now we assume
  674. * it is doing 'the right thing' wrt a world wide unique
  675. * VPD Unit Serial Number that OS dependent multipath can depend on.
  676. */
  677. if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
  678. pr_err("Underlying SCSI device firmware provided VPD"
  679. " Unit Serial, ignoring request\n");
  680. return -EOPNOTSUPP;
  681. }
  682. if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
  683. pr_err("Emulated VPD Unit Serial exceeds"
  684. " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
  685. return -EOVERFLOW;
  686. }
  687. /*
  688. * Check to see if any active $FABRIC_MOD exports exist. If they
  689. * do exist, fail here as changing this information on the fly
  690. * (underneath the initiator side OS dependent multipath code)
  691. * could cause negative effects.
  692. */
  693. if (dev->export_count) {
  694. pr_err("Unable to set VPD Unit Serial while"
  695. " active %d $FABRIC_MOD exports exist\n",
  696. dev->export_count);
  697. return -EINVAL;
  698. }
  699. /*
  700. * This currently assumes ASCII encoding for emulated VPD Unit Serial.
  701. *
  702. * Also, strip any newline added from the userspace
  703. * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
  704. */
  705. memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
  706. snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
  707. snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
  708. "%s", strstrip(buf));
  709. dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
  710. pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
  711. " %s\n", dev->t10_wwn.unit_serial);
  712. return count;
  713. }
  714. SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
  715. /*
  716. * VPD page 0x83 Protocol Identifier
  717. */
  718. static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
  719. struct t10_wwn *t10_wwn,
  720. char *page)
  721. {
  722. struct t10_vpd *vpd;
  723. unsigned char buf[VPD_TMP_BUF_SIZE];
  724. ssize_t len = 0;
  725. memset(buf, 0, VPD_TMP_BUF_SIZE);
  726. spin_lock(&t10_wwn->t10_vpd_lock);
  727. list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
  728. if (!vpd->protocol_identifier_set)
  729. continue;
  730. transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
  731. if (len + strlen(buf) >= PAGE_SIZE)
  732. break;
  733. len += sprintf(page+len, "%s", buf);
  734. }
  735. spin_unlock(&t10_wwn->t10_vpd_lock);
  736. return len;
  737. }
  738. static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
  739. struct t10_wwn *t10_wwn,
  740. const char *page,
  741. size_t count)
  742. {
  743. return -ENOSYS;
  744. }
  745. SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
  746. /*
  747. * Generic wrapper for dumping VPD identifiers by association.
  748. */
  749. #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
  750. static ssize_t target_core_dev_wwn_show_attr_##_name( \
  751. struct t10_wwn *t10_wwn, \
  752. char *page) \
  753. { \
  754. struct t10_vpd *vpd; \
  755. unsigned char buf[VPD_TMP_BUF_SIZE]; \
  756. ssize_t len = 0; \
  757. \
  758. spin_lock(&t10_wwn->t10_vpd_lock); \
  759. list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
  760. if (vpd->association != _assoc) \
  761. continue; \
  762. \
  763. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  764. transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
  765. if (len + strlen(buf) >= PAGE_SIZE) \
  766. break; \
  767. len += sprintf(page+len, "%s", buf); \
  768. \
  769. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  770. transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
  771. if (len + strlen(buf) >= PAGE_SIZE) \
  772. break; \
  773. len += sprintf(page+len, "%s", buf); \
  774. \
  775. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  776. transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
  777. if (len + strlen(buf) >= PAGE_SIZE) \
  778. break; \
  779. len += sprintf(page+len, "%s", buf); \
  780. } \
  781. spin_unlock(&t10_wwn->t10_vpd_lock); \
  782. \
  783. return len; \
  784. }
  785. /*
  786. * VPD page 0x83 Association: Logical Unit
  787. */
  788. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
  789. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
  790. struct t10_wwn *t10_wwn,
  791. const char *page,
  792. size_t count)
  793. {
  794. return -ENOSYS;
  795. }
  796. SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
  797. /*
  798. * VPD page 0x83 Association: Target Port
  799. */
  800. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
  801. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
  802. struct t10_wwn *t10_wwn,
  803. const char *page,
  804. size_t count)
  805. {
  806. return -ENOSYS;
  807. }
  808. SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
  809. /*
  810. * VPD page 0x83 Association: SCSI Target Device
  811. */
  812. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
  813. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
  814. struct t10_wwn *t10_wwn,
  815. const char *page,
  816. size_t count)
  817. {
  818. return -ENOSYS;
  819. }
  820. SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
  821. CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
  822. static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
  823. &target_core_dev_wwn_vpd_unit_serial.attr,
  824. &target_core_dev_wwn_vpd_protocol_identifier.attr,
  825. &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
  826. &target_core_dev_wwn_vpd_assoc_target_port.attr,
  827. &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
  828. NULL,
  829. };
  830. static struct configfs_item_operations target_core_dev_wwn_ops = {
  831. .show_attribute = target_core_dev_wwn_attr_show,
  832. .store_attribute = target_core_dev_wwn_attr_store,
  833. };
  834. static struct config_item_type target_core_dev_wwn_cit = {
  835. .ct_item_ops = &target_core_dev_wwn_ops,
  836. .ct_attrs = target_core_dev_wwn_attrs,
  837. .ct_owner = THIS_MODULE,
  838. };
  839. /* End functions for struct config_item_type target_core_dev_wwn_cit */
  840. /* Start functions for struct config_item_type target_core_dev_pr_cit */
  841. CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
  842. #define SE_DEV_PR_ATTR(_name, _mode) \
  843. static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
  844. __CONFIGFS_EATTR(_name, _mode, \
  845. target_core_dev_pr_show_attr_##_name, \
  846. target_core_dev_pr_store_attr_##_name);
  847. #define SE_DEV_PR_ATTR_RO(_name); \
  848. static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
  849. __CONFIGFS_EATTR_RO(_name, \
  850. target_core_dev_pr_show_attr_##_name);
  851. static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
  852. char *page)
  853. {
  854. struct se_node_acl *se_nacl;
  855. struct t10_pr_registration *pr_reg;
  856. char i_buf[PR_REG_ISID_ID_LEN];
  857. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  858. pr_reg = dev->dev_pr_res_holder;
  859. if (!pr_reg)
  860. return sprintf(page, "No SPC-3 Reservation holder\n");
  861. se_nacl = pr_reg->pr_reg_nacl;
  862. core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
  863. return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
  864. se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  865. se_nacl->initiatorname, i_buf);
  866. }
  867. static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
  868. char *page)
  869. {
  870. struct se_node_acl *se_nacl;
  871. ssize_t len;
  872. se_nacl = dev->dev_reserved_node_acl;
  873. if (se_nacl) {
  874. len = sprintf(page,
  875. "SPC-2 Reservation: %s Initiator: %s\n",
  876. se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  877. se_nacl->initiatorname);
  878. } else {
  879. len = sprintf(page, "No SPC-2 Reservation holder\n");
  880. }
  881. return len;
  882. }
  883. static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
  884. char *page)
  885. {
  886. int ret;
  887. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  888. return sprintf(page, "Passthrough\n");
  889. spin_lock(&dev->dev_reservation_lock);
  890. if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  891. ret = target_core_dev_pr_show_spc2_res(dev, page);
  892. else
  893. ret = target_core_dev_pr_show_spc3_res(dev, page);
  894. spin_unlock(&dev->dev_reservation_lock);
  895. return ret;
  896. }
  897. SE_DEV_PR_ATTR_RO(res_holder);
  898. static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
  899. struct se_device *dev, char *page)
  900. {
  901. ssize_t len = 0;
  902. spin_lock(&dev->dev_reservation_lock);
  903. if (!dev->dev_pr_res_holder) {
  904. len = sprintf(page, "No SPC-3 Reservation holder\n");
  905. } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
  906. len = sprintf(page, "SPC-3 Reservation: All Target"
  907. " Ports registration\n");
  908. } else {
  909. len = sprintf(page, "SPC-3 Reservation: Single"
  910. " Target Port registration\n");
  911. }
  912. spin_unlock(&dev->dev_reservation_lock);
  913. return len;
  914. }
  915. SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
  916. static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
  917. struct se_device *dev, char *page)
  918. {
  919. return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
  920. }
  921. SE_DEV_PR_ATTR_RO(res_pr_generation);
  922. /*
  923. * res_pr_holder_tg_port
  924. */
  925. static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
  926. struct se_device *dev, char *page)
  927. {
  928. struct se_node_acl *se_nacl;
  929. struct se_lun *lun;
  930. struct se_portal_group *se_tpg;
  931. struct t10_pr_registration *pr_reg;
  932. struct target_core_fabric_ops *tfo;
  933. ssize_t len = 0;
  934. spin_lock(&dev->dev_reservation_lock);
  935. pr_reg = dev->dev_pr_res_holder;
  936. if (!pr_reg) {
  937. len = sprintf(page, "No SPC-3 Reservation holder\n");
  938. goto out_unlock;
  939. }
  940. se_nacl = pr_reg->pr_reg_nacl;
  941. se_tpg = se_nacl->se_tpg;
  942. lun = pr_reg->pr_reg_tg_pt_lun;
  943. tfo = se_tpg->se_tpg_tfo;
  944. len += sprintf(page+len, "SPC-3 Reservation: %s"
  945. " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
  946. tfo->tpg_get_wwn(se_tpg));
  947. len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
  948. " Identifier Tag: %hu %s Portal Group Tag: %hu"
  949. " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
  950. tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
  951. tfo->get_fabric_name(), lun->unpacked_lun);
  952. out_unlock:
  953. spin_unlock(&dev->dev_reservation_lock);
  954. return len;
  955. }
  956. SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
  957. static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
  958. struct se_device *dev, char *page)
  959. {
  960. struct target_core_fabric_ops *tfo;
  961. struct t10_pr_registration *pr_reg;
  962. unsigned char buf[384];
  963. char i_buf[PR_REG_ISID_ID_LEN];
  964. ssize_t len = 0;
  965. int reg_count = 0;
  966. len += sprintf(page+len, "SPC-3 PR Registrations:\n");
  967. spin_lock(&dev->t10_pr.registration_lock);
  968. list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
  969. pr_reg_list) {
  970. memset(buf, 0, 384);
  971. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  972. tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
  973. core_pr_dump_initiator_port(pr_reg, i_buf,
  974. PR_REG_ISID_ID_LEN);
  975. sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
  976. tfo->get_fabric_name(),
  977. pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
  978. pr_reg->pr_res_generation);
  979. if (len + strlen(buf) >= PAGE_SIZE)
  980. break;
  981. len += sprintf(page+len, "%s", buf);
  982. reg_count++;
  983. }
  984. spin_unlock(&dev->t10_pr.registration_lock);
  985. if (!reg_count)
  986. len += sprintf(page+len, "None\n");
  987. return len;
  988. }
  989. SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
  990. static ssize_t target_core_dev_pr_show_attr_res_pr_type(
  991. struct se_device *dev, char *page)
  992. {
  993. struct t10_pr_registration *pr_reg;
  994. ssize_t len = 0;
  995. spin_lock(&dev->dev_reservation_lock);
  996. pr_reg = dev->dev_pr_res_holder;
  997. if (pr_reg) {
  998. len = sprintf(page, "SPC-3 Reservation Type: %s\n",
  999. core_scsi3_pr_dump_type(pr_reg->pr_res_type));
  1000. } else {
  1001. len = sprintf(page, "No SPC-3 Reservation holder\n");
  1002. }
  1003. spin_unlock(&dev->dev_reservation_lock);
  1004. return len;
  1005. }
  1006. SE_DEV_PR_ATTR_RO(res_pr_type);
  1007. static ssize_t target_core_dev_pr_show_attr_res_type(
  1008. struct se_device *dev, char *page)
  1009. {
  1010. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  1011. return sprintf(page, "SPC_PASSTHROUGH\n");
  1012. else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  1013. return sprintf(page, "SPC2_RESERVATIONS\n");
  1014. else
  1015. return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
  1016. }
  1017. SE_DEV_PR_ATTR_RO(res_type);
  1018. static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
  1019. struct se_device *dev, char *page)
  1020. {
  1021. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  1022. return 0;
  1023. return sprintf(page, "APTPL Bit Status: %s\n",
  1024. (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
  1025. }
  1026. SE_DEV_PR_ATTR_RO(res_aptpl_active);
  1027. /*
  1028. * res_aptpl_metadata
  1029. */
  1030. static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
  1031. struct se_device *dev, char *page)
  1032. {
  1033. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  1034. return 0;
  1035. return sprintf(page, "Ready to process PR APTPL metadata..\n");
  1036. }
  1037. enum {
  1038. Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
  1039. Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
  1040. Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
  1041. Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
  1042. };
  1043. static match_table_t tokens = {
  1044. {Opt_initiator_fabric, "initiator_fabric=%s"},
  1045. {Opt_initiator_node, "initiator_node=%s"},
  1046. {Opt_initiator_sid, "initiator_sid=%s"},
  1047. {Opt_sa_res_key, "sa_res_key=%s"},
  1048. {Opt_res_holder, "res_holder=%d"},
  1049. {Opt_res_type, "res_type=%d"},
  1050. {Opt_res_scope, "res_scope=%d"},
  1051. {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
  1052. {Opt_mapped_lun, "mapped_lun=%d"},
  1053. {Opt_target_fabric, "target_fabric=%s"},
  1054. {Opt_target_node, "target_node=%s"},
  1055. {Opt_tpgt, "tpgt=%d"},
  1056. {Opt_port_rtpi, "port_rtpi=%d"},
  1057. {Opt_target_lun, "target_lun=%d"},
  1058. {Opt_err, NULL}
  1059. };
  1060. static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
  1061. struct se_device *dev,
  1062. const char *page,
  1063. size_t count)
  1064. {
  1065. unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
  1066. unsigned char *t_fabric = NULL, *t_port = NULL;
  1067. char *orig, *ptr, *arg_p, *opts;
  1068. substring_t args[MAX_OPT_ARGS];
  1069. unsigned long long tmp_ll;
  1070. u64 sa_res_key = 0;
  1071. u32 mapped_lun = 0, target_lun = 0;
  1072. int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
  1073. u16 port_rpti = 0, tpgt = 0;
  1074. u8 type = 0, scope;
  1075. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  1076. return 0;
  1077. if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  1078. return 0;
  1079. if (dev->export_count) {
  1080. pr_debug("Unable to process APTPL metadata while"
  1081. " active fabric exports exist\n");
  1082. return -EINVAL;
  1083. }
  1084. opts = kstrdup(page, GFP_KERNEL);
  1085. if (!opts)
  1086. return -ENOMEM;
  1087. orig = opts;
  1088. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  1089. if (!*ptr)
  1090. continue;
  1091. token = match_token(ptr, tokens, args);
  1092. switch (token) {
  1093. case Opt_initiator_fabric:
  1094. i_fabric = match_strdup(&args[0]);
  1095. if (!i_fabric) {
  1096. ret = -ENOMEM;
  1097. goto out;
  1098. }
  1099. break;
  1100. case Opt_initiator_node:
  1101. i_port = match_strdup(&args[0]);
  1102. if (!i_port) {
  1103. ret = -ENOMEM;
  1104. goto out;
  1105. }
  1106. if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
  1107. pr_err("APTPL metadata initiator_node="
  1108. " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
  1109. PR_APTPL_MAX_IPORT_LEN);
  1110. ret = -EINVAL;
  1111. break;
  1112. }
  1113. break;
  1114. case Opt_initiator_sid:
  1115. isid = match_strdup(&args[0]);
  1116. if (!isid) {
  1117. ret = -ENOMEM;
  1118. goto out;
  1119. }
  1120. if (strlen(isid) >= PR_REG_ISID_LEN) {
  1121. pr_err("APTPL metadata initiator_isid"
  1122. "= exceeds PR_REG_ISID_LEN: %d\n",
  1123. PR_REG_ISID_LEN);
  1124. ret = -EINVAL;
  1125. break;
  1126. }
  1127. break;
  1128. case Opt_sa_res_key:
  1129. arg_p = match_strdup(&args[0]);
  1130. if (!arg_p) {
  1131. ret = -ENOMEM;
  1132. goto out;
  1133. }
  1134. ret = strict_strtoull(arg_p, 0, &tmp_ll);
  1135. if (ret < 0) {
  1136. pr_err("strict_strtoull() failed for"
  1137. " sa_res_key=\n");
  1138. goto out;
  1139. }
  1140. sa_res_key = (u64)tmp_ll;
  1141. break;
  1142. /*
  1143. * PR APTPL Metadata for Reservation
  1144. */
  1145. case Opt_res_holder:
  1146. match_int(args, &arg);
  1147. res_holder = arg;
  1148. break;
  1149. case Opt_res_type:
  1150. match_int(args, &arg);
  1151. type = (u8)arg;
  1152. break;
  1153. case Opt_res_scope:
  1154. match_int(args, &arg);
  1155. scope = (u8)arg;
  1156. break;
  1157. case Opt_res_all_tg_pt:
  1158. match_int(args, &arg);
  1159. all_tg_pt = (int)arg;
  1160. break;
  1161. case Opt_mapped_lun:
  1162. match_int(args, &arg);
  1163. mapped_lun = (u32)arg;
  1164. break;
  1165. /*
  1166. * PR APTPL Metadata for Target Port
  1167. */
  1168. case Opt_target_fabric:
  1169. t_fabric = match_strdup(&args[0]);
  1170. if (!t_fabric) {
  1171. ret = -ENOMEM;
  1172. goto out;
  1173. }
  1174. break;
  1175. case Opt_target_node:
  1176. t_port = match_strdup(&args[0]);
  1177. if (!t_port) {
  1178. ret = -ENOMEM;
  1179. goto out;
  1180. }
  1181. if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
  1182. pr_err("APTPL metadata target_node="
  1183. " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
  1184. PR_APTPL_MAX_TPORT_LEN);
  1185. ret = -EINVAL;
  1186. break;
  1187. }
  1188. break;
  1189. case Opt_tpgt:
  1190. match_int(args, &arg);
  1191. tpgt = (u16)arg;
  1192. break;
  1193. case Opt_port_rtpi:
  1194. match_int(args, &arg);
  1195. port_rpti = (u16)arg;
  1196. break;
  1197. case Opt_target_lun:
  1198. match_int(args, &arg);
  1199. target_lun = (u32)arg;
  1200. break;
  1201. default:
  1202. break;
  1203. }
  1204. }
  1205. if (!i_port || !t_port || !sa_res_key) {
  1206. pr_err("Illegal parameters for APTPL registration\n");
  1207. ret = -EINVAL;
  1208. goto out;
  1209. }
  1210. if (res_holder && !(type)) {
  1211. pr_err("Illegal PR type: 0x%02x for reservation"
  1212. " holder\n", type);
  1213. ret = -EINVAL;
  1214. goto out;
  1215. }
  1216. ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
  1217. i_port, isid, mapped_lun, t_port, tpgt, target_lun,
  1218. res_holder, all_tg_pt, type);
  1219. out:
  1220. kfree(i_fabric);
  1221. kfree(i_port);
  1222. kfree(isid);
  1223. kfree(t_fabric);
  1224. kfree(t_port);
  1225. kfree(orig);
  1226. return (ret == 0) ? count : ret;
  1227. }
  1228. SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
  1229. CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
  1230. static struct configfs_attribute *target_core_dev_pr_attrs[] = {
  1231. &target_core_dev_pr_res_holder.attr,
  1232. &target_core_dev_pr_res_pr_all_tgt_pts.attr,
  1233. &target_core_dev_pr_res_pr_generation.attr,
  1234. &target_core_dev_pr_res_pr_holder_tg_port.attr,
  1235. &target_core_dev_pr_res_pr_registered_i_pts.attr,
  1236. &target_core_dev_pr_res_pr_type.attr,
  1237. &target_core_dev_pr_res_type.attr,
  1238. &target_core_dev_pr_res_aptpl_active.attr,
  1239. &target_core_dev_pr_res_aptpl_metadata.attr,
  1240. NULL,
  1241. };
  1242. static struct configfs_item_operations target_core_dev_pr_ops = {
  1243. .show_attribute = target_core_dev_pr_attr_show,
  1244. .store_attribute = target_core_dev_pr_attr_store,
  1245. };
  1246. static struct config_item_type target_core_dev_pr_cit = {
  1247. .ct_item_ops = &target_core_dev_pr_ops,
  1248. .ct_attrs = target_core_dev_pr_attrs,
  1249. .ct_owner = THIS_MODULE,
  1250. };
  1251. /* End functions for struct config_item_type target_core_dev_pr_cit */
  1252. /* Start functions for struct config_item_type target_core_dev_cit */
  1253. static ssize_t target_core_show_dev_info(void *p, char *page)
  1254. {
  1255. struct se_device *dev = p;
  1256. struct se_subsystem_api *t = dev->transport;
  1257. int bl = 0;
  1258. ssize_t read_bytes = 0;
  1259. transport_dump_dev_state(dev, page, &bl);
  1260. read_bytes += bl;
  1261. read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
  1262. return read_bytes;
  1263. }
  1264. static struct target_core_configfs_attribute target_core_attr_dev_info = {
  1265. .attr = { .ca_owner = THIS_MODULE,
  1266. .ca_name = "info",
  1267. .ca_mode = S_IRUGO },
  1268. .show = target_core_show_dev_info,
  1269. .store = NULL,
  1270. };
  1271. static ssize_t target_core_store_dev_control(
  1272. void *p,
  1273. const char *page,
  1274. size_t count)
  1275. {
  1276. struct se_device *dev = p;
  1277. struct se_subsystem_api *t = dev->transport;
  1278. return t->set_configfs_dev_params(dev, page, count);
  1279. }
  1280. static struct target_core_configfs_attribute target_core_attr_dev_control = {
  1281. .attr = { .ca_owner = THIS_MODULE,
  1282. .ca_name = "control",
  1283. .ca_mode = S_IWUSR },
  1284. .show = NULL,
  1285. .store = target_core_store_dev_control,
  1286. };
  1287. static ssize_t target_core_show_dev_alias(void *p, char *page)
  1288. {
  1289. struct se_device *dev = p;
  1290. if (!(dev->dev_flags & DF_USING_ALIAS))
  1291. return 0;
  1292. return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
  1293. }
  1294. static ssize_t target_core_store_dev_alias(
  1295. void *p,
  1296. const char *page,
  1297. size_t count)
  1298. {
  1299. struct se_device *dev = p;
  1300. struct se_hba *hba = dev->se_hba;
  1301. ssize_t read_bytes;
  1302. if (count > (SE_DEV_ALIAS_LEN-1)) {
  1303. pr_err("alias count: %d exceeds"
  1304. " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
  1305. SE_DEV_ALIAS_LEN-1);
  1306. return -EINVAL;
  1307. }
  1308. read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
  1309. if (!read_bytes)
  1310. return -EINVAL;
  1311. if (dev->dev_alias[read_bytes - 1] == '\n')
  1312. dev->dev_alias[read_bytes - 1] = '\0';
  1313. dev->dev_flags |= DF_USING_ALIAS;
  1314. pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
  1315. config_item_name(&hba->hba_group.cg_item),
  1316. config_item_name(&dev->dev_group.cg_item),
  1317. dev->dev_alias);
  1318. return read_bytes;
  1319. }
  1320. static struct target_core_configfs_attribute target_core_attr_dev_alias = {
  1321. .attr = { .ca_owner = THIS_MODULE,
  1322. .ca_name = "alias",
  1323. .ca_mode = S_IRUGO | S_IWUSR },
  1324. .show = target_core_show_dev_alias,
  1325. .store = target_core_store_dev_alias,
  1326. };
  1327. static ssize_t target_core_show_dev_udev_path(void *p, char *page)
  1328. {
  1329. struct se_device *dev = p;
  1330. if (!(dev->dev_flags & DF_USING_UDEV_PATH))
  1331. return 0;
  1332. return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
  1333. }
  1334. static ssize_t target_core_store_dev_udev_path(
  1335. void *p,
  1336. const char *page,
  1337. size_t count)
  1338. {
  1339. struct se_device *dev = p;
  1340. struct se_hba *hba = dev->se_hba;
  1341. ssize_t read_bytes;
  1342. if (count > (SE_UDEV_PATH_LEN-1)) {
  1343. pr_err("udev_path count: %d exceeds"
  1344. " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
  1345. SE_UDEV_PATH_LEN-1);
  1346. return -EINVAL;
  1347. }
  1348. read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
  1349. "%s", page);
  1350. if (!read_bytes)
  1351. return -EINVAL;
  1352. if (dev->udev_path[read_bytes - 1] == '\n')
  1353. dev->udev_path[read_bytes - 1] = '\0';
  1354. dev->dev_flags |= DF_USING_UDEV_PATH;
  1355. pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
  1356. config_item_name(&hba->hba_group.cg_item),
  1357. config_item_name(&dev->dev_group.cg_item),
  1358. dev->udev_path);
  1359. return read_bytes;
  1360. }
  1361. static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
  1362. .attr = { .ca_owner = THIS_MODULE,
  1363. .ca_name = "udev_path",
  1364. .ca_mode = S_IRUGO | S_IWUSR },
  1365. .show = target_core_show_dev_udev_path,
  1366. .store = target_core_store_dev_udev_path,
  1367. };
  1368. static ssize_t target_core_show_dev_enable(void *p, char *page)
  1369. {
  1370. struct se_device *dev = p;
  1371. return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
  1372. }
  1373. static ssize_t target_core_store_dev_enable(
  1374. void *p,
  1375. const char *page,
  1376. size_t count)
  1377. {
  1378. struct se_device *dev = p;
  1379. char *ptr;
  1380. int ret;
  1381. ptr = strstr(page, "1");
  1382. if (!ptr) {
  1383. pr_err("For dev_enable ops, only valid value"
  1384. " is \"1\"\n");
  1385. return -EINVAL;
  1386. }
  1387. ret = target_configure_device(dev);
  1388. if (ret)
  1389. return ret;
  1390. return count;
  1391. }
  1392. static struct target_core_configfs_attribute target_core_attr_dev_enable = {
  1393. .attr = { .ca_owner = THIS_MODULE,
  1394. .ca_name = "enable",
  1395. .ca_mode = S_IRUGO | S_IWUSR },
  1396. .show = target_core_show_dev_enable,
  1397. .store = target_core_store_dev_enable,
  1398. };
  1399. static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
  1400. {
  1401. struct se_device *dev = p;
  1402. struct config_item *lu_ci;
  1403. struct t10_alua_lu_gp *lu_gp;
  1404. struct t10_alua_lu_gp_member *lu_gp_mem;
  1405. ssize_t len = 0;
  1406. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1407. if (!lu_gp_mem)
  1408. return 0;
  1409. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1410. lu_gp = lu_gp_mem->lu_gp;
  1411. if (lu_gp) {
  1412. lu_ci = &lu_gp->lu_gp_group.cg_item;
  1413. len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
  1414. config_item_name(lu_ci), lu_gp->lu_gp_id);
  1415. }
  1416. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1417. return len;
  1418. }
  1419. static ssize_t target_core_store_alua_lu_gp(
  1420. void *p,
  1421. const char *page,
  1422. size_t count)
  1423. {
  1424. struct se_device *dev = p;
  1425. struct se_hba *hba = dev->se_hba;
  1426. struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
  1427. struct t10_alua_lu_gp_member *lu_gp_mem;
  1428. unsigned char buf[LU_GROUP_NAME_BUF];
  1429. int move = 0;
  1430. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1431. if (!lu_gp_mem)
  1432. return 0;
  1433. if (count > LU_GROUP_NAME_BUF) {
  1434. pr_err("ALUA LU Group Alias too large!\n");
  1435. return -EINVAL;
  1436. }
  1437. memset(buf, 0, LU_GROUP_NAME_BUF);
  1438. memcpy(buf, page, count);
  1439. /*
  1440. * Any ALUA logical unit alias besides "NULL" means we will be
  1441. * making a new group association.
  1442. */
  1443. if (strcmp(strstrip(buf), "NULL")) {
  1444. /*
  1445. * core_alua_get_lu_gp_by_name() will increment reference to
  1446. * struct t10_alua_lu_gp. This reference is released with
  1447. * core_alua_get_lu_gp_by_name below().
  1448. */
  1449. lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
  1450. if (!lu_gp_new)
  1451. return -ENODEV;
  1452. }
  1453. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1454. lu_gp = lu_gp_mem->lu_gp;
  1455. if (lu_gp) {
  1456. /*
  1457. * Clearing an existing lu_gp association, and replacing
  1458. * with NULL
  1459. */
  1460. if (!lu_gp_new) {
  1461. pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
  1462. " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
  1463. " %hu\n",
  1464. config_item_name(&hba->hba_group.cg_item),
  1465. config_item_name(&dev->dev_group.cg_item),
  1466. config_item_name(&lu_gp->lu_gp_group.cg_item),
  1467. lu_gp->lu_gp_id);
  1468. __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
  1469. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1470. return count;
  1471. }
  1472. /*
  1473. * Removing existing association of lu_gp_mem with lu_gp
  1474. */
  1475. __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
  1476. move = 1;
  1477. }
  1478. /*
  1479. * Associate lu_gp_mem with lu_gp_new.
  1480. */
  1481. __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
  1482. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1483. pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
  1484. " core/alua/lu_gps/%s, ID: %hu\n",
  1485. (move) ? "Moving" : "Adding",
  1486. config_item_name(&hba->hba_group.cg_item),
  1487. config_item_name(&dev->dev_group.cg_item),
  1488. config_item_name(&lu_gp_new->lu_gp_group.cg_item),
  1489. lu_gp_new->lu_gp_id);
  1490. core_alua_put_lu_gp_from_name(lu_gp_new);
  1491. return count;
  1492. }
  1493. static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
  1494. .attr = { .ca_owner = THIS_MODULE,
  1495. .ca_name = "alua_lu_gp",
  1496. .ca_mode = S_IRUGO | S_IWUSR },
  1497. .show = target_core_show_alua_lu_gp,
  1498. .store = target_core_store_alua_lu_gp,
  1499. };
  1500. static struct configfs_attribute *lio_core_dev_attrs[] = {
  1501. &target_core_attr_dev_info.attr,
  1502. &target_core_attr_dev_control.attr,
  1503. &target_core_attr_dev_alias.attr,
  1504. &target_core_attr_dev_udev_path.attr,
  1505. &target_core_attr_dev_enable.attr,
  1506. &target_core_attr_dev_alua_lu_gp.attr,
  1507. NULL,
  1508. };
  1509. static void target_core_dev_release(struct config_item *item)
  1510. {
  1511. struct config_group *dev_cg = to_config_group(item);
  1512. struct se_device *dev =
  1513. container_of(dev_cg, struct se_device, dev_group);
  1514. kfree(dev_cg->default_groups);
  1515. target_free_device(dev);
  1516. }
  1517. static ssize_t target_core_dev_show(struct config_item *item,
  1518. struct configfs_attribute *attr,
  1519. char *page)
  1520. {
  1521. struct config_group *dev_cg = to_config_group(item);
  1522. struct se_device *dev =
  1523. container_of(dev_cg, struct se_device, dev_group);
  1524. struct target_core_configfs_attribute *tc_attr = container_of(
  1525. attr, struct target_core_configfs_attribute, attr);
  1526. if (!tc_attr->show)
  1527. return -EINVAL;
  1528. return tc_attr->show(dev, page);
  1529. }
  1530. static ssize_t target_core_dev_store(struct config_item *item,
  1531. struct configfs_attribute *attr,
  1532. const char *page, size_t count)
  1533. {
  1534. struct config_group *dev_cg = to_config_group(item);
  1535. struct se_device *dev =
  1536. container_of(dev_cg, struct se_device, dev_group);
  1537. struct target_core_configfs_attribute *tc_attr = container_of(
  1538. attr, struct target_core_configfs_attribute, attr);
  1539. if (!tc_attr->store)
  1540. return -EINVAL;
  1541. return tc_attr->store(dev, page, count);
  1542. }
  1543. static struct configfs_item_operations target_core_dev_item_ops = {
  1544. .release = target_core_dev_release,
  1545. .show_attribute = target_core_dev_show,
  1546. .store_attribute = target_core_dev_store,
  1547. };
  1548. static struct config_item_type target_core_dev_cit = {
  1549. .ct_item_ops = &target_core_dev_item_ops,
  1550. .ct_attrs = lio_core_dev_attrs,
  1551. .ct_owner = THIS_MODULE,
  1552. };
  1553. /* End functions for struct config_item_type target_core_dev_cit */
  1554. /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
  1555. CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
  1556. #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
  1557. static struct target_core_alua_lu_gp_attribute \
  1558. target_core_alua_lu_gp_##_name = \
  1559. __CONFIGFS_EATTR(_name, _mode, \
  1560. target_core_alua_lu_gp_show_attr_##_name, \
  1561. target_core_alua_lu_gp_store_attr_##_name);
  1562. #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
  1563. static struct target_core_alua_lu_gp_attribute \
  1564. target_core_alua_lu_gp_##_name = \
  1565. __CONFIGFS_EATTR_RO(_name, \
  1566. target_core_alua_lu_gp_show_attr_##_name);
  1567. /*
  1568. * lu_gp_id
  1569. */
  1570. static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
  1571. struct t10_alua_lu_gp *lu_gp,
  1572. char *page)
  1573. {
  1574. if (!lu_gp->lu_gp_valid_id)
  1575. return 0;
  1576. return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
  1577. }
  1578. static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
  1579. struct t10_alua_lu_gp *lu_gp,
  1580. const char *page,
  1581. size_t count)
  1582. {
  1583. struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
  1584. unsigned long lu_gp_id;
  1585. int ret;
  1586. ret = strict_strtoul(page, 0, &lu_gp_id);
  1587. if (ret < 0) {
  1588. pr_err("strict_strtoul() returned %d for"
  1589. " lu_gp_id\n", ret);
  1590. return -EINVAL;
  1591. }
  1592. if (lu_gp_id > 0x0000ffff) {
  1593. pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
  1594. " 0x0000ffff\n", lu_gp_id);
  1595. return -EINVAL;
  1596. }
  1597. ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
  1598. if (ret < 0)
  1599. return -EINVAL;
  1600. pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
  1601. " Group: core/alua/lu_gps/%s to ID: %hu\n",
  1602. config_item_name(&alua_lu_gp_cg->cg_item),
  1603. lu_gp->lu_gp_id);
  1604. return count;
  1605. }
  1606. SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
  1607. /*
  1608. * members
  1609. */
  1610. static ssize_t target_core_alua_lu_gp_show_attr_members(
  1611. struct t10_alua_lu_gp *lu_gp,
  1612. char *page)
  1613. {
  1614. struct se_device *dev;
  1615. struct se_hba *hba;
  1616. struct t10_alua_lu_gp_member *lu_gp_mem;
  1617. ssize_t len = 0, cur_len;
  1618. unsigned char buf[LU_GROUP_NAME_BUF];
  1619. memset(buf, 0, LU_GROUP_NAME_BUF);
  1620. spin_lock(&lu_gp->lu_gp_lock);
  1621. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1622. dev = lu_gp_mem->lu_gp_mem_dev;
  1623. hba = dev->se_hba;
  1624. cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
  1625. config_item_name(&hba->hba_group.cg_item),
  1626. config_item_name(&dev->dev_group.cg_item));
  1627. cur_len++; /* Extra byte for NULL terminator */
  1628. if ((cur_len + len) > PAGE_SIZE) {
  1629. pr_warn("Ran out of lu_gp_show_attr"
  1630. "_members buffer\n");
  1631. break;
  1632. }
  1633. memcpy(page+len, buf, cur_len);
  1634. len += cur_len;
  1635. }
  1636. spin_unlock(&lu_gp->lu_gp_lock);
  1637. return len;
  1638. }
  1639. SE_DEV_ALUA_LU_ATTR_RO(members);
  1640. CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
  1641. static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
  1642. &target_core_alua_lu_gp_lu_gp_id.attr,
  1643. &target_core_alua_lu_gp_members.attr,
  1644. NULL,
  1645. };
  1646. static void target_core_alua_lu_gp_release(struct config_item *item)
  1647. {
  1648. struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
  1649. struct t10_alua_lu_gp, lu_gp_group);
  1650. core_alua_free_lu_gp(lu_gp);
  1651. }
  1652. static struct configfs_item_operations target_core_alua_lu_gp_ops = {
  1653. .release = target_core_alua_lu_gp_release,
  1654. .show_attribute = target_core_alua_lu_gp_attr_show,
  1655. .store_attribute = target_core_alua_lu_gp_attr_store,
  1656. };
  1657. static struct config_item_type target_core_alua_lu_gp_cit = {
  1658. .ct_item_ops = &target_core_alua_lu_gp_ops,
  1659. .ct_attrs = target_core_alua_lu_gp_attrs,
  1660. .ct_owner = THIS_MODULE,
  1661. };
  1662. /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
  1663. /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
  1664. static struct config_group *target_core_alua_create_lu_gp(
  1665. struct config_group *group,
  1666. const char *name)
  1667. {
  1668. struct t10_alua_lu_gp *lu_gp;
  1669. struct config_group *alua_lu_gp_cg = NULL;
  1670. struct config_item *alua_lu_gp_ci = NULL;
  1671. lu_gp = core_alua_allocate_lu_gp(name, 0);
  1672. if (IS_ERR(lu_gp))
  1673. return NULL;
  1674. alua_lu_gp_cg = &lu_gp->lu_gp_group;
  1675. alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
  1676. config_group_init_type_name(alua_lu_gp_cg, name,
  1677. &target_core_alua_lu_gp_cit);
  1678. pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
  1679. " Group: core/alua/lu_gps/%s\n",
  1680. config_item_name(alua_lu_gp_ci));
  1681. return alua_lu_gp_cg;
  1682. }
  1683. static void target_core_alua_drop_lu_gp(
  1684. struct config_group *group,
  1685. struct config_item *item)
  1686. {
  1687. struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
  1688. struct t10_alua_lu_gp, lu_gp_group);
  1689. pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
  1690. " Group: core/alua/lu_gps/%s, ID: %hu\n",
  1691. config_item_name(item), lu_gp->lu_gp_id);
  1692. /*
  1693. * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
  1694. * -> target_core_alua_lu_gp_release()
  1695. */
  1696. config_item_put(item);
  1697. }
  1698. static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
  1699. .make_group = &target_core_alua_create_lu_gp,
  1700. .drop_item = &target_core_alua_drop_lu_gp,
  1701. };
  1702. static struct config_item_type target_core_alua_lu_gps_cit = {
  1703. .ct_item_ops = NULL,
  1704. .ct_group_ops = &target_core_alua_lu_gps_group_ops,
  1705. .ct_owner = THIS_MODULE,
  1706. };
  1707. /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
  1708. /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
  1709. CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
  1710. #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
  1711. static struct target_core_alua_tg_pt_gp_attribute \
  1712. target_core_alua_tg_pt_gp_##_name = \
  1713. __CONFIGFS_EATTR(_name, _mode, \
  1714. target_core_alua_tg_pt_gp_show_attr_##_name, \
  1715. target_core_alua_tg_pt_gp_store_attr_##_name);
  1716. #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
  1717. static struct target_core_alua_tg_pt_gp_attribute \
  1718. target_core_alua_tg_pt_gp_##_name = \
  1719. __CONFIGFS_EATTR_RO(_name, \
  1720. target_core_alua_tg_pt_gp_show_attr_##_name);
  1721. /*
  1722. * alua_access_state
  1723. */
  1724. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
  1725. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1726. char *page)
  1727. {
  1728. return sprintf(page, "%d\n",
  1729. atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
  1730. }
  1731. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
  1732. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1733. const char *page,
  1734. size_t count)
  1735. {
  1736. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1737. unsigned long tmp;
  1738. int new_state, ret;
  1739. if (!tg_pt_gp->tg_pt_gp_valid_id) {
  1740. pr_err("Unable to do implict ALUA on non valid"
  1741. " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
  1742. return -EINVAL;
  1743. }
  1744. ret = strict_strtoul(page, 0, &tmp);
  1745. if (ret < 0) {
  1746. pr_err("Unable to extract new ALUA access state from"
  1747. " %s\n", page);
  1748. return -EINVAL;
  1749. }
  1750. new_state = (int)tmp;
  1751. if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
  1752. pr_err("Unable to process implict configfs ALUA"
  1753. " transition while TPGS_IMPLICT_ALUA is disabled\n");
  1754. return -EINVAL;
  1755. }
  1756. ret = core_alua_do_port_transition(tg_pt_gp, dev,
  1757. NULL, NULL, new_state, 0);
  1758. return (!ret) ? count : -EINVAL;
  1759. }
  1760. SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
  1761. /*
  1762. * alua_access_status
  1763. */
  1764. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
  1765. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1766. char *page)
  1767. {
  1768. return sprintf(page, "%s\n",
  1769. core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
  1770. }
  1771. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
  1772. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1773. const char *page,
  1774. size_t count)
  1775. {
  1776. unsigned long tmp;
  1777. int new_status, ret;
  1778. if (!tg_pt_gp->tg_pt_gp_valid_id) {
  1779. pr_err("Unable to do set ALUA access status on non"
  1780. " valid tg_pt_gp ID: %hu\n",
  1781. tg_pt_gp->tg_pt_gp_valid_id);
  1782. return -EINVAL;
  1783. }
  1784. ret = strict_strtoul(page, 0, &tmp);
  1785. if (ret < 0) {
  1786. pr_err("Unable to extract new ALUA access status"
  1787. " from %s\n", page);
  1788. return -EINVAL;
  1789. }
  1790. new_status = (int)tmp;
  1791. if ((new_status != ALUA_STATUS_NONE) &&
  1792. (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  1793. (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
  1794. pr_err("Illegal ALUA access status: 0x%02x\n",
  1795. new_status);
  1796. return -EINVAL;
  1797. }
  1798. tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
  1799. return count;
  1800. }
  1801. SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
  1802. /*
  1803. * alua_access_type
  1804. */
  1805. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
  1806. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1807. char *page)
  1808. {
  1809. return core_alua_show_access_type(tg_pt_gp, page);
  1810. }
  1811. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
  1812. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1813. const char *page,
  1814. size_t count)
  1815. {
  1816. return core_alua_store_access_type(tg_pt_gp, page, count);
  1817. }
  1818. SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
  1819. /*
  1820. * alua_write_metadata
  1821. */
  1822. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
  1823. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1824. char *page)
  1825. {
  1826. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
  1827. }
  1828. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
  1829. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1830. const char *page,
  1831. size_t count)
  1832. {
  1833. unsigned long tmp;
  1834. int ret;
  1835. ret = strict_strtoul(page, 0, &tmp);
  1836. if (ret < 0) {
  1837. pr_err("Unable to extract alua_write_metadata\n");
  1838. return -EINVAL;
  1839. }
  1840. if ((tmp != 0) && (tmp != 1)) {
  1841. pr_err("Illegal value for alua_write_metadata:"
  1842. " %lu\n", tmp);
  1843. return -EINVAL;
  1844. }
  1845. tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
  1846. return count;
  1847. }
  1848. SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
  1849. /*
  1850. * nonop_delay_msecs
  1851. */
  1852. static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
  1853. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1854. char *page)
  1855. {
  1856. return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
  1857. }
  1858. static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
  1859. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1860. const char *page,
  1861. size_t count)
  1862. {
  1863. return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
  1864. }
  1865. SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
  1866. /*
  1867. * trans_delay_msecs
  1868. */
  1869. static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
  1870. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1871. char *page)
  1872. {
  1873. return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
  1874. }
  1875. static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
  1876. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1877. const char *page,
  1878. size_t count)
  1879. {
  1880. return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
  1881. }
  1882. SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
  1883. /*
  1884. * implict_trans_secs
  1885. */
  1886. static ssize_t target_core_alua_tg_pt_gp_show_attr_implict_trans_secs(
  1887. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1888. char *page)
  1889. {
  1890. return core_alua_show_implict_trans_secs(tg_pt_gp, page);
  1891. }
  1892. static ssize_t target_core_alua_tg_pt_gp_store_attr_implict_trans_secs(
  1893. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1894. const char *page,
  1895. size_t count)
  1896. {
  1897. return core_alua_store_implict_trans_secs(tg_pt_gp, page, count);
  1898. }
  1899. SE_DEV_ALUA_TG_PT_ATTR(implict_trans_secs, S_IRUGO | S_IWUSR);
  1900. /*
  1901. * preferred
  1902. */
  1903. static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
  1904. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1905. char *page)
  1906. {
  1907. return core_alua_show_preferred_bit(tg_pt_gp, page);
  1908. }
  1909. static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
  1910. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1911. const char *page,
  1912. size_t count)
  1913. {
  1914. return core_alua_store_preferred_bit(tg_pt_gp, page, count);
  1915. }
  1916. SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
  1917. /*
  1918. * tg_pt_gp_id
  1919. */
  1920. static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
  1921. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1922. char *page)
  1923. {
  1924. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1925. return 0;
  1926. return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
  1927. }
  1928. static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
  1929. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1930. const char *page,
  1931. size_t count)
  1932. {
  1933. struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
  1934. unsigned long tg_pt_gp_id;
  1935. int ret;
  1936. ret = strict_strtoul(page, 0, &tg_pt_gp_id);
  1937. if (ret < 0) {
  1938. pr_err("strict_strtoul() returned %d for"
  1939. " tg_pt_gp_id\n", ret);
  1940. return -EINVAL;
  1941. }
  1942. if (tg_pt_gp_id > 0x0000ffff) {
  1943. pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
  1944. " 0x0000ffff\n", tg_pt_gp_id);
  1945. return -EINVAL;
  1946. }
  1947. ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
  1948. if (ret < 0)
  1949. return -EINVAL;
  1950. pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
  1951. "core/alua/tg_pt_gps/%s to ID: %hu\n",
  1952. config_item_name(&alua_tg_pt_gp_cg->cg_item),
  1953. tg_pt_gp->tg_pt_gp_id);
  1954. return count;
  1955. }
  1956. SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
  1957. /*
  1958. * members
  1959. */
  1960. static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
  1961. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1962. char *page)
  1963. {
  1964. struct se_port *port;
  1965. struct se_portal_group *tpg;
  1966. struct se_lun *lun;
  1967. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1968. ssize_t len = 0, cur_len;
  1969. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1970. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1971. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1972. list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  1973. tg_pt_gp_mem_list) {
  1974. port = tg_pt_gp_mem->tg_pt;
  1975. tpg = port->sep_tpg;
  1976. lun = port->sep_lun;
  1977. cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
  1978. "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1979. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1980. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1981. config_item_name(&lun->lun_group.cg_item));
  1982. cur_len++; /* Extra byte for NULL terminator */
  1983. if ((cur_len + len) > PAGE_SIZE) {
  1984. pr_warn("Ran out of lu_gp_show_attr"
  1985. "_members buffer\n");
  1986. break;
  1987. }
  1988. memcpy(page+len, buf, cur_len);
  1989. len += cur_len;
  1990. }
  1991. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1992. return len;
  1993. }
  1994. SE_DEV_ALUA_TG_PT_ATTR_RO(members);
  1995. CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
  1996. tg_pt_gp_group);
  1997. static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
  1998. &target_core_alua_tg_pt_gp_alua_access_state.attr,
  1999. &target_core_alua_tg_pt_gp_alua_access_status.attr,
  2000. &target_core_alua_tg_pt_gp_alua_access_type.attr,
  2001. &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
  2002. &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
  2003. &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
  2004. &target_core_alua_tg_pt_gp_implict_trans_secs.attr,
  2005. &target_core_alua_tg_pt_gp_preferred.attr,
  2006. &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
  2007. &target_core_alua_tg_pt_gp_members.attr,
  2008. NULL,
  2009. };
  2010. static void target_core_alua_tg_pt_gp_release(struct config_item *item)
  2011. {
  2012. struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
  2013. struct t10_alua_tg_pt_gp, tg_pt_gp_group);
  2014. core_alua_free_tg_pt_gp(tg_pt_gp);
  2015. }
  2016. static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
  2017. .release = target_core_alua_tg_pt_gp_release,
  2018. .show_attribute = target_core_alua_tg_pt_gp_attr_show,
  2019. .store_attribute = target_core_alua_tg_pt_gp_attr_store,
  2020. };
  2021. static struct config_item_type target_core_alua_tg_pt_gp_cit = {
  2022. .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
  2023. .ct_attrs = target_core_alua_tg_pt_gp_attrs,
  2024. .ct_owner = THIS_MODULE,
  2025. };
  2026. /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
  2027. /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
  2028. static struct config_group *target_core_alua_create_tg_pt_gp(
  2029. struct config_group *group,
  2030. const char *name)
  2031. {
  2032. struct t10_alua *alua = container_of(group, struct t10_alua,
  2033. alua_tg_pt_gps_group);
  2034. struct t10_alua_tg_pt_gp *tg_pt_gp;
  2035. struct config_group *alua_tg_pt_gp_cg = NULL;
  2036. struct config_item *alua_tg_pt_gp_ci = NULL;
  2037. tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
  2038. if (!tg_pt_gp)
  2039. return NULL;
  2040. alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
  2041. alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
  2042. config_group_init_type_name(alua_tg_pt_gp_cg, name,
  2043. &target_core_alua_tg_pt_gp_cit);
  2044. pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
  2045. " Group: alua/tg_pt_gps/%s\n",
  2046. config_item_name(alua_tg_pt_gp_ci));
  2047. return alua_tg_pt_gp_cg;
  2048. }
  2049. static void target_core_alua_drop_tg_pt_gp(
  2050. struct config_group *group,
  2051. struct config_item *item)
  2052. {
  2053. struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
  2054. struct t10_alua_tg_pt_gp, tg_pt_gp_group);
  2055. pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
  2056. " Group: alua/tg_pt_gps/%s, ID: %hu\n",
  2057. config_item_name(item), tg_pt_gp->tg_pt_gp_id);
  2058. /*
  2059. * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
  2060. * -> target_core_alua_tg_pt_gp_release().
  2061. */
  2062. config_item_put(item);
  2063. }
  2064. static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
  2065. .make_group = &target_core_alua_create_tg_pt_gp,
  2066. .drop_item = &target_core_alua_drop_tg_pt_gp,
  2067. };
  2068. static struct config_item_type target_core_alua_tg_pt_gps_cit = {
  2069. .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
  2070. .ct_owner = THIS_MODULE,
  2071. };
  2072. /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
  2073. /* Start functions for struct config_item_type target_core_alua_cit */
  2074. /*
  2075. * target_core_alua_cit is a ConfigFS group that lives under
  2076. * /sys/kernel/config/target/core/alua. There are default groups
  2077. * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
  2078. * target_core_alua_cit in target_core_init_configfs() below.
  2079. */
  2080. static struct config_item_type target_core_alua_cit = {
  2081. .ct_item_ops = NULL,
  2082. .ct_attrs = NULL,
  2083. .ct_owner = THIS_MODULE,
  2084. };
  2085. /* End functions for struct config_item_type target_core_alua_cit */
  2086. /* Start functions for struct config_item_type target_core_stat_cit */
  2087. static struct config_group *target_core_stat_mkdir(
  2088. struct config_group *group,
  2089. const char *name)
  2090. {
  2091. return ERR_PTR(-ENOSYS);
  2092. }
  2093. static void target_core_stat_rmdir(
  2094. struct config_group *group,
  2095. struct config_item *item)
  2096. {
  2097. return;
  2098. }
  2099. static struct configfs_group_operations target_core_stat_group_ops = {
  2100. .make_group = &target_core_stat_mkdir,
  2101. .drop_item = &target_core_stat_rmdir,
  2102. };
  2103. static struct config_item_type target_core_stat_cit = {
  2104. .ct_group_ops = &target_core_stat_group_ops,
  2105. .ct_owner = THIS_MODULE,
  2106. };
  2107. /* End functions for struct config_item_type target_core_stat_cit */
  2108. /* Start functions for struct config_item_type target_core_hba_cit */
  2109. static struct config_group *target_core_make_subdev(
  2110. struct config_group *group,
  2111. const char *name)
  2112. {
  2113. struct t10_alua_tg_pt_gp *tg_pt_gp;
  2114. struct se_subsystem_api *t;
  2115. struct config_item *hba_ci = &group->cg_item;
  2116. struct se_hba *hba = item_to_hba(hba_ci);
  2117. struct se_device *dev;
  2118. struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
  2119. struct config_group *dev_stat_grp = NULL;
  2120. int errno = -ENOMEM, ret;
  2121. ret = mutex_lock_interruptible(&hba->hba_access_mutex);
  2122. if (ret)
  2123. return ERR_PTR(ret);
  2124. /*
  2125. * Locate the struct se_subsystem_api from parent's struct se_hba.
  2126. */
  2127. t = hba->transport;
  2128. dev = target_alloc_device(hba, name);
  2129. if (!dev)
  2130. goto out_unlock;
  2131. dev_cg = &dev->dev_group;
  2132. dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
  2133. GFP_KERNEL);
  2134. if (!dev_cg->default_groups)
  2135. goto out_free_device;
  2136. config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
  2137. config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
  2138. &target_core_dev_attrib_cit);
  2139. config_group_init_type_name(&dev->dev_pr_group, "pr",
  2140. &target_core_dev_pr_cit);
  2141. config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
  2142. &target_core_dev_wwn_cit);
  2143. config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
  2144. "alua", &target_core_alua_tg_pt_gps_cit);
  2145. config_group_init_type_name(&dev->dev_stat_grps.stat_group,
  2146. "statistics", &target_core_stat_cit);
  2147. dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
  2148. dev_cg->default_groups[1] = &dev->dev_pr_group;
  2149. dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
  2150. dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
  2151. dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
  2152. dev_cg->default_groups[5] = NULL;
  2153. /*
  2154. * Add core/$HBA/$DEV/alua/default_tg_pt_gp
  2155. */
  2156. tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
  2157. if (!tg_pt_gp)
  2158. goto out_free_dev_cg_default_groups;
  2159. dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
  2160. tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
  2161. tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2162. GFP_KERNEL);
  2163. if (!tg_pt_gp_cg->default_groups) {
  2164. pr_err("Unable to allocate tg_pt_gp_cg->"
  2165. "default_groups\n");
  2166. goto out_free_tg_pt_gp;
  2167. }
  2168. config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
  2169. "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
  2170. tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
  2171. tg_pt_gp_cg->default_groups[1] = NULL;
  2172. /*
  2173. * Add core/$HBA/$DEV/statistics/ default groups
  2174. */
  2175. dev_stat_grp = &dev->dev_stat_grps.stat_group;
  2176. dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
  2177. GFP_KERNEL);
  2178. if (!dev_stat_grp->default_groups) {
  2179. pr_err("Unable to allocate dev_stat_grp->default_groups\n");
  2180. goto out_free_tg_pt_gp_cg_default_groups;
  2181. }
  2182. target_stat_setup_dev_default_groups(dev);
  2183. mutex_unlock(&hba->hba_access_mutex);
  2184. return dev_cg;
  2185. out_free_tg_pt_gp_cg_default_groups:
  2186. kfree(tg_pt_gp_cg->default_groups);
  2187. out_free_tg_pt_gp:
  2188. core_alua_free_tg_pt_gp(tg_pt_gp);
  2189. out_free_dev_cg_default_groups:
  2190. kfree(dev_cg->default_groups);
  2191. out_free_device:
  2192. target_free_device(dev);
  2193. out_unlock:
  2194. mutex_unlock(&hba->hba_access_mutex);
  2195. return ERR_PTR(errno);
  2196. }
  2197. static void target_core_drop_subdev(
  2198. struct config_group *group,
  2199. struct config_item *item)
  2200. {
  2201. struct config_group *dev_cg = to_config_group(item);
  2202. struct se_device *dev =
  2203. container_of(dev_cg, struct se_device, dev_group);
  2204. struct se_hba *hba;
  2205. struct config_item *df_item;
  2206. struct config_group *tg_pt_gp_cg, *dev_stat_grp;
  2207. int i;
  2208. hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
  2209. mutex_lock(&hba->hba_access_mutex);
  2210. dev_stat_grp = &dev->dev_stat_grps.stat_group;
  2211. for (i = 0; dev_stat_grp->default_groups[i]; i++) {
  2212. df_item = &dev_stat_grp->default_groups[i]->cg_item;
  2213. dev_stat_grp->default_groups[i] = NULL;
  2214. config_item_put(df_item);
  2215. }
  2216. kfree(dev_stat_grp->default_groups);
  2217. tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
  2218. for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
  2219. df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
  2220. tg_pt_gp_cg->default_groups[i] = NULL;
  2221. config_item_put(df_item);
  2222. }
  2223. kfree(tg_pt_gp_cg->default_groups);
  2224. /*
  2225. * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
  2226. * directly from target_core_alua_tg_pt_gp_release().
  2227. */
  2228. dev->t10_alua.default_tg_pt_gp = NULL;
  2229. for (i = 0; dev_cg->default_groups[i]; i++) {
  2230. df_item = &dev_cg->default_groups[i]->cg_item;
  2231. dev_cg->default_groups[i] = NULL;
  2232. config_item_put(df_item);
  2233. }
  2234. /*
  2235. * se_dev is released from target_core_dev_item_ops->release()
  2236. */
  2237. config_item_put(item);
  2238. mutex_unlock(&hba->hba_access_mutex);
  2239. }
  2240. static struct configfs_group_operations target_core_hba_group_ops = {
  2241. .make_group = target_core_make_subdev,
  2242. .drop_item = target_core_drop_subdev,
  2243. };
  2244. CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
  2245. #define SE_HBA_ATTR(_name, _mode) \
  2246. static struct target_core_hba_attribute \
  2247. target_core_hba_##_name = \
  2248. __CONFIGFS_EATTR(_name, _mode, \
  2249. target_core_hba_show_attr_##_name, \
  2250. target_core_hba_store_attr_##_name);
  2251. #define SE_HBA_ATTR_RO(_name) \
  2252. static struct target_core_hba_attribute \
  2253. target_core_hba_##_name = \
  2254. __CONFIGFS_EATTR_RO(_name, \
  2255. target_core_hba_show_attr_##_name);
  2256. static ssize_t target_core_hba_show_attr_hba_info(
  2257. struct se_hba *hba,
  2258. char *page)
  2259. {
  2260. return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
  2261. hba->hba_id, hba->transport->name,
  2262. TARGET_CORE_CONFIGFS_VERSION);
  2263. }
  2264. SE_HBA_ATTR_RO(hba_info);
  2265. static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
  2266. char *page)
  2267. {
  2268. int hba_mode = 0;
  2269. if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
  2270. hba_mode = 1;
  2271. return sprintf(page, "%d\n", hba_mode);
  2272. }
  2273. static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
  2274. const char *page, size_t count)
  2275. {
  2276. struct se_subsystem_api *transport = hba->transport;
  2277. unsigned long mode_flag;
  2278. int ret;
  2279. if (transport->pmode_enable_hba == NULL)
  2280. return -EINVAL;
  2281. ret = strict_strtoul(page, 0, &mode_flag);
  2282. if (ret < 0) {
  2283. pr_err("Unable to extract hba mode flag: %d\n", ret);
  2284. return -EINVAL;
  2285. }
  2286. if (hba->dev_count) {
  2287. pr_err("Unable to set hba_mode with active devices\n");
  2288. return -EINVAL;
  2289. }
  2290. ret = transport->pmode_enable_hba(hba, mode_flag);
  2291. if (ret < 0)
  2292. return -EINVAL;
  2293. if (ret > 0)
  2294. hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
  2295. else if (ret == 0)
  2296. hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
  2297. return count;
  2298. }
  2299. SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
  2300. CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
  2301. static void target_core_hba_release(struct config_item *item)
  2302. {
  2303. struct se_hba *hba = container_of(to_config_group(item),
  2304. struct se_hba, hba_group);
  2305. core_delete_hba(hba);
  2306. }
  2307. static struct configfs_attribute *target_core_hba_attrs[] = {
  2308. &target_core_hba_hba_info.attr,
  2309. &target_core_hba_hba_mode.attr,
  2310. NULL,
  2311. };
  2312. static struct configfs_item_operations target_core_hba_item_ops = {
  2313. .release = target_core_hba_release,
  2314. .show_attribute = target_core_hba_attr_show,
  2315. .store_attribute = target_core_hba_attr_store,
  2316. };
  2317. static struct config_item_type target_core_hba_cit = {
  2318. .ct_item_ops = &target_core_hba_item_ops,
  2319. .ct_group_ops = &target_core_hba_group_ops,
  2320. .ct_attrs = target_core_hba_attrs,
  2321. .ct_owner = THIS_MODULE,
  2322. };
  2323. static struct config_group *target_core_call_addhbatotarget(
  2324. struct config_group *group,
  2325. const char *name)
  2326. {
  2327. char *se_plugin_str, *str, *str2;
  2328. struct se_hba *hba;
  2329. char buf[TARGET_CORE_NAME_MAX_LEN];
  2330. unsigned long plugin_dep_id = 0;
  2331. int ret;
  2332. memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
  2333. if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
  2334. pr_err("Passed *name strlen(): %d exceeds"
  2335. " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
  2336. TARGET_CORE_NAME_MAX_LEN);
  2337. return ERR_PTR(-ENAMETOOLONG);
  2338. }
  2339. snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
  2340. str = strstr(buf, "_");
  2341. if (!str) {
  2342. pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
  2343. return ERR_PTR(-EINVAL);
  2344. }
  2345. se_plugin_str = buf;
  2346. /*
  2347. * Special case for subsystem plugins that have "_" in their names.
  2348. * Namely rd_direct and rd_mcp..
  2349. */
  2350. str2 = strstr(str+1, "_");
  2351. if (str2) {
  2352. *str2 = '\0'; /* Terminate for *se_plugin_str */
  2353. str2++; /* Skip to start of plugin dependent ID */
  2354. str = str2;
  2355. } else {
  2356. *str = '\0'; /* Terminate for *se_plugin_str */
  2357. str++; /* Skip to start of plugin dependent ID */
  2358. }
  2359. ret = strict_strtoul(str, 0, &plugin_dep_id);
  2360. if (ret < 0) {
  2361. pr_err("strict_strtoul() returned %d for"
  2362. " plugin_dep_id\n", ret);
  2363. return ERR_PTR(-EINVAL);
  2364. }
  2365. /*
  2366. * Load up TCM subsystem plugins if they have not already been loaded.
  2367. */
  2368. transport_subsystem_check_init();
  2369. hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
  2370. if (IS_ERR(hba))
  2371. return ERR_CAST(hba);
  2372. config_group_init_type_name(&hba->hba_group, name,
  2373. &target_core_hba_cit);
  2374. return &hba->hba_group;
  2375. }
  2376. static void target_core_call_delhbafromtarget(
  2377. struct config_group *group,
  2378. struct config_item *item)
  2379. {
  2380. /*
  2381. * core_delete_hba() is called from target_core_hba_item_ops->release()
  2382. * -> target_core_hba_release()
  2383. */
  2384. config_item_put(item);
  2385. }
  2386. static struct configfs_group_operations target_core_group_ops = {
  2387. .make_group = target_core_call_addhbatotarget,
  2388. .drop_item = target_core_call_delhbafromtarget,
  2389. };
  2390. static struct config_item_type target_core_cit = {
  2391. .ct_item_ops = NULL,
  2392. .ct_group_ops = &target_core_group_ops,
  2393. .ct_attrs = NULL,
  2394. .ct_owner = THIS_MODULE,
  2395. };
  2396. /* Stop functions for struct config_item_type target_core_hba_cit */
  2397. static int __init target_core_init_configfs(void)
  2398. {
  2399. struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
  2400. struct config_group *lu_gp_cg = NULL;
  2401. struct configfs_subsystem *subsys;
  2402. struct t10_alua_lu_gp *lu_gp;
  2403. int ret;
  2404. pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
  2405. " Engine: %s on %s/%s on "UTS_RELEASE"\n",
  2406. TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
  2407. subsys = target_core_subsystem[0];
  2408. config_group_init(&subsys->su_group);
  2409. mutex_init(&subsys->su_mutex);
  2410. ret = init_se_kmem_caches();
  2411. if (ret < 0)
  2412. return ret;
  2413. /*
  2414. * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
  2415. * and ALUA Logical Unit Group and Target Port Group infrastructure.
  2416. */
  2417. target_cg = &subsys->su_group;
  2418. target_cg->default_groups = kmalloc(sizeof(struct config_group) * 2,
  2419. GFP_KERNEL);
  2420. if (!target_cg->default_groups) {
  2421. pr_err("Unable to allocate target_cg->default_groups\n");
  2422. ret = -ENOMEM;
  2423. goto out_global;
  2424. }
  2425. config_group_init_type_name(&target_core_hbagroup,
  2426. "core", &target_core_cit);
  2427. target_cg->default_groups[0] = &target_core_hbagroup;
  2428. target_cg->default_groups[1] = NULL;
  2429. /*
  2430. * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
  2431. */
  2432. hba_cg = &target_core_hbagroup;
  2433. hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2434. GFP_KERNEL);
  2435. if (!hba_cg->default_groups) {
  2436. pr_err("Unable to allocate hba_cg->default_groups\n");
  2437. ret = -ENOMEM;
  2438. goto out_global;
  2439. }
  2440. config_group_init_type_name(&alua_group,
  2441. "alua", &target_core_alua_cit);
  2442. hba_cg->default_groups[0] = &alua_group;
  2443. hba_cg->default_groups[1] = NULL;
  2444. /*
  2445. * Add ALUA Logical Unit Group and Target Port Group ConfigFS
  2446. * groups under /sys/kernel/config/target/core/alua/
  2447. */
  2448. alua_cg = &alua_group;
  2449. alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2450. GFP_KERNEL);
  2451. if (!alua_cg->default_groups) {
  2452. pr_err("Unable to allocate alua_cg->default_groups\n");
  2453. ret = -ENOMEM;
  2454. goto out_global;
  2455. }
  2456. config_group_init_type_name(&alua_lu_gps_group,
  2457. "lu_gps", &target_core_alua_lu_gps_cit);
  2458. alua_cg->default_groups[0] = &alua_lu_gps_group;
  2459. alua_cg->default_groups[1] = NULL;
  2460. /*
  2461. * Add core/alua/lu_gps/default_lu_gp
  2462. */
  2463. lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
  2464. if (IS_ERR(lu_gp)) {
  2465. ret = -ENOMEM;
  2466. goto out_global;
  2467. }
  2468. lu_gp_cg = &alua_lu_gps_group;
  2469. lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2470. GFP_KERNEL);
  2471. if (!lu_gp_cg->default_groups) {
  2472. pr_err("Unable to allocate lu_gp_cg->default_groups\n");
  2473. ret = -ENOMEM;
  2474. goto out_global;
  2475. }
  2476. config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
  2477. &target_core_alua_lu_gp_cit);
  2478. lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
  2479. lu_gp_cg->default_groups[1] = NULL;
  2480. default_lu_gp = lu_gp;
  2481. /*
  2482. * Register the target_core_mod subsystem with configfs.
  2483. */
  2484. ret = configfs_register_subsystem(subsys);
  2485. if (ret < 0) {
  2486. pr_err("Error %d while registering subsystem %s\n",
  2487. ret, subsys->su_group.cg_item.ci_namebuf);
  2488. goto out_global;
  2489. }
  2490. pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
  2491. " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
  2492. " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
  2493. /*
  2494. * Register built-in RAMDISK subsystem logic for virtual LUN 0
  2495. */
  2496. ret = rd_module_init();
  2497. if (ret < 0)
  2498. goto out;
  2499. ret = core_dev_setup_virtual_lun0();
  2500. if (ret < 0)
  2501. goto out;
  2502. return 0;
  2503. out:
  2504. configfs_unregister_subsystem(subsys);
  2505. core_dev_release_virtual_lun0();
  2506. rd_module_exit();
  2507. out_global:
  2508. if (default_lu_gp) {
  2509. core_alua_free_lu_gp(default_lu_gp);
  2510. default_lu_gp = NULL;
  2511. }
  2512. if (lu_gp_cg)
  2513. kfree(lu_gp_cg->default_groups);
  2514. if (alua_cg)
  2515. kfree(alua_cg->default_groups);
  2516. if (hba_cg)
  2517. kfree(hba_cg->default_groups);
  2518. kfree(target_cg->default_groups);
  2519. release_se_kmem_caches();
  2520. return ret;
  2521. }
  2522. static void __exit target_core_exit_configfs(void)
  2523. {
  2524. struct configfs_subsystem *subsys;
  2525. struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
  2526. struct config_item *item;
  2527. int i;
  2528. subsys = target_core_subsystem[0];
  2529. lu_gp_cg = &alua_lu_gps_group;
  2530. for (i = 0; lu_gp_cg->default_groups[i]; i++) {
  2531. item = &lu_gp_cg->default_groups[i]->cg_item;
  2532. lu_gp_cg->default_groups[i] = NULL;
  2533. config_item_put(item);
  2534. }
  2535. kfree(lu_gp_cg->default_groups);
  2536. lu_gp_cg->default_groups = NULL;
  2537. alua_cg = &alua_group;
  2538. for (i = 0; alua_cg->default_groups[i]; i++) {
  2539. item = &alua_cg->default_groups[i]->cg_item;
  2540. alua_cg->default_groups[i] = NULL;
  2541. config_item_put(item);
  2542. }
  2543. kfree(alua_cg->default_groups);
  2544. alua_cg->default_groups = NULL;
  2545. hba_cg = &target_core_hbagroup;
  2546. for (i = 0; hba_cg->default_groups[i]; i++) {
  2547. item = &hba_cg->default_groups[i]->cg_item;
  2548. hba_cg->default_groups[i] = NULL;
  2549. config_item_put(item);
  2550. }
  2551. kfree(hba_cg->default_groups);
  2552. hba_cg->default_groups = NULL;
  2553. /*
  2554. * We expect subsys->su_group.default_groups to be released
  2555. * by configfs subsystem provider logic..
  2556. */
  2557. configfs_unregister_subsystem(subsys);
  2558. kfree(subsys->su_group.default_groups);
  2559. core_alua_free_lu_gp(default_lu_gp);
  2560. default_lu_gp = NULL;
  2561. pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
  2562. " Infrastructure\n");
  2563. core_dev_release_virtual_lun0();
  2564. rd_module_exit();
  2565. release_se_kmem_caches();
  2566. }
  2567. MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
  2568. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  2569. MODULE_LICENSE("GPL");
  2570. module_init(target_core_init_configfs);
  2571. module_exit(target_core_exit_configfs);