target_core_configfs.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002
  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_dpo);
  551. SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
  552. DEF_DEV_ATTRIB(emulate_fua_write);
  553. SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
  554. DEF_DEV_ATTRIB(emulate_fua_read);
  555. SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
  556. DEF_DEV_ATTRIB(emulate_write_cache);
  557. SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
  558. DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
  559. SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
  560. DEF_DEV_ATTRIB(emulate_tas);
  561. SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
  562. DEF_DEV_ATTRIB(emulate_tpu);
  563. SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
  564. DEF_DEV_ATTRIB(emulate_tpws);
  565. SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
  566. DEF_DEV_ATTRIB(enforce_pr_isids);
  567. SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
  568. DEF_DEV_ATTRIB(is_nonrot);
  569. SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
  570. DEF_DEV_ATTRIB(emulate_rest_reord);
  571. SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
  572. DEF_DEV_ATTRIB_RO(hw_block_size);
  573. SE_DEV_ATTR_RO(hw_block_size);
  574. DEF_DEV_ATTRIB(block_size);
  575. SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
  576. DEF_DEV_ATTRIB_RO(hw_max_sectors);
  577. SE_DEV_ATTR_RO(hw_max_sectors);
  578. DEF_DEV_ATTRIB(fabric_max_sectors);
  579. SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
  580. DEF_DEV_ATTRIB(optimal_sectors);
  581. SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
  582. DEF_DEV_ATTRIB_RO(hw_queue_depth);
  583. SE_DEV_ATTR_RO(hw_queue_depth);
  584. DEF_DEV_ATTRIB(queue_depth);
  585. SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
  586. DEF_DEV_ATTRIB(max_unmap_lba_count);
  587. SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
  588. DEF_DEV_ATTRIB(max_unmap_block_desc_count);
  589. SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
  590. DEF_DEV_ATTRIB(unmap_granularity);
  591. SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
  592. DEF_DEV_ATTRIB(unmap_granularity_alignment);
  593. SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
  594. DEF_DEV_ATTRIB(max_write_same_len);
  595. SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
  596. CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
  597. static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
  598. &target_core_dev_attrib_emulate_dpo.attr,
  599. &target_core_dev_attrib_emulate_fua_write.attr,
  600. &target_core_dev_attrib_emulate_fua_read.attr,
  601. &target_core_dev_attrib_emulate_write_cache.attr,
  602. &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
  603. &target_core_dev_attrib_emulate_tas.attr,
  604. &target_core_dev_attrib_emulate_tpu.attr,
  605. &target_core_dev_attrib_emulate_tpws.attr,
  606. &target_core_dev_attrib_enforce_pr_isids.attr,
  607. &target_core_dev_attrib_is_nonrot.attr,
  608. &target_core_dev_attrib_emulate_rest_reord.attr,
  609. &target_core_dev_attrib_hw_block_size.attr,
  610. &target_core_dev_attrib_block_size.attr,
  611. &target_core_dev_attrib_hw_max_sectors.attr,
  612. &target_core_dev_attrib_fabric_max_sectors.attr,
  613. &target_core_dev_attrib_optimal_sectors.attr,
  614. &target_core_dev_attrib_hw_queue_depth.attr,
  615. &target_core_dev_attrib_queue_depth.attr,
  616. &target_core_dev_attrib_max_unmap_lba_count.attr,
  617. &target_core_dev_attrib_max_unmap_block_desc_count.attr,
  618. &target_core_dev_attrib_unmap_granularity.attr,
  619. &target_core_dev_attrib_unmap_granularity_alignment.attr,
  620. &target_core_dev_attrib_max_write_same_len.attr,
  621. NULL,
  622. };
  623. static struct configfs_item_operations target_core_dev_attrib_ops = {
  624. .show_attribute = target_core_dev_attrib_attr_show,
  625. .store_attribute = target_core_dev_attrib_attr_store,
  626. };
  627. static struct config_item_type target_core_dev_attrib_cit = {
  628. .ct_item_ops = &target_core_dev_attrib_ops,
  629. .ct_attrs = target_core_dev_attrib_attrs,
  630. .ct_owner = THIS_MODULE,
  631. };
  632. /* End functions for struct config_item_type target_core_dev_attrib_cit */
  633. /* Start functions for struct config_item_type target_core_dev_wwn_cit */
  634. CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
  635. #define SE_DEV_WWN_ATTR(_name, _mode) \
  636. static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
  637. __CONFIGFS_EATTR(_name, _mode, \
  638. target_core_dev_wwn_show_attr_##_name, \
  639. target_core_dev_wwn_store_attr_##_name);
  640. #define SE_DEV_WWN_ATTR_RO(_name); \
  641. do { \
  642. static struct target_core_dev_wwn_attribute \
  643. target_core_dev_wwn_##_name = \
  644. __CONFIGFS_EATTR_RO(_name, \
  645. target_core_dev_wwn_show_attr_##_name); \
  646. } while (0);
  647. /*
  648. * VPD page 0x80 Unit serial
  649. */
  650. static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
  651. struct t10_wwn *t10_wwn,
  652. char *page)
  653. {
  654. return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
  655. &t10_wwn->unit_serial[0]);
  656. }
  657. static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
  658. struct t10_wwn *t10_wwn,
  659. const char *page,
  660. size_t count)
  661. {
  662. struct se_device *dev = t10_wwn->t10_dev;
  663. unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
  664. /*
  665. * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
  666. * from the struct scsi_device level firmware, do not allow
  667. * VPD Unit Serial to be emulated.
  668. *
  669. * Note this struct scsi_device could also be emulating VPD
  670. * information from its drivers/scsi LLD. But for now we assume
  671. * it is doing 'the right thing' wrt a world wide unique
  672. * VPD Unit Serial Number that OS dependent multipath can depend on.
  673. */
  674. if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
  675. pr_err("Underlying SCSI device firmware provided VPD"
  676. " Unit Serial, ignoring request\n");
  677. return -EOPNOTSUPP;
  678. }
  679. if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
  680. pr_err("Emulated VPD Unit Serial exceeds"
  681. " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
  682. return -EOVERFLOW;
  683. }
  684. /*
  685. * Check to see if any active $FABRIC_MOD exports exist. If they
  686. * do exist, fail here as changing this information on the fly
  687. * (underneath the initiator side OS dependent multipath code)
  688. * could cause negative effects.
  689. */
  690. if (dev->export_count) {
  691. pr_err("Unable to set VPD Unit Serial while"
  692. " active %d $FABRIC_MOD exports exist\n",
  693. dev->export_count);
  694. return -EINVAL;
  695. }
  696. /*
  697. * This currently assumes ASCII encoding for emulated VPD Unit Serial.
  698. *
  699. * Also, strip any newline added from the userspace
  700. * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
  701. */
  702. memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
  703. snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
  704. snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
  705. "%s", strstrip(buf));
  706. dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
  707. pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
  708. " %s\n", dev->t10_wwn.unit_serial);
  709. return count;
  710. }
  711. SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
  712. /*
  713. * VPD page 0x83 Protocol Identifier
  714. */
  715. static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
  716. struct t10_wwn *t10_wwn,
  717. char *page)
  718. {
  719. struct t10_vpd *vpd;
  720. unsigned char buf[VPD_TMP_BUF_SIZE];
  721. ssize_t len = 0;
  722. memset(buf, 0, VPD_TMP_BUF_SIZE);
  723. spin_lock(&t10_wwn->t10_vpd_lock);
  724. list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
  725. if (!vpd->protocol_identifier_set)
  726. continue;
  727. transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
  728. if (len + strlen(buf) >= PAGE_SIZE)
  729. break;
  730. len += sprintf(page+len, "%s", buf);
  731. }
  732. spin_unlock(&t10_wwn->t10_vpd_lock);
  733. return len;
  734. }
  735. static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
  736. struct t10_wwn *t10_wwn,
  737. const char *page,
  738. size_t count)
  739. {
  740. return -ENOSYS;
  741. }
  742. SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
  743. /*
  744. * Generic wrapper for dumping VPD identifiers by association.
  745. */
  746. #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
  747. static ssize_t target_core_dev_wwn_show_attr_##_name( \
  748. struct t10_wwn *t10_wwn, \
  749. char *page) \
  750. { \
  751. struct t10_vpd *vpd; \
  752. unsigned char buf[VPD_TMP_BUF_SIZE]; \
  753. ssize_t len = 0; \
  754. \
  755. spin_lock(&t10_wwn->t10_vpd_lock); \
  756. list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
  757. if (vpd->association != _assoc) \
  758. continue; \
  759. \
  760. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  761. transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
  762. if (len + strlen(buf) >= PAGE_SIZE) \
  763. break; \
  764. len += sprintf(page+len, "%s", buf); \
  765. \
  766. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  767. transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
  768. if (len + strlen(buf) >= PAGE_SIZE) \
  769. break; \
  770. len += sprintf(page+len, "%s", buf); \
  771. \
  772. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  773. transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
  774. if (len + strlen(buf) >= PAGE_SIZE) \
  775. break; \
  776. len += sprintf(page+len, "%s", buf); \
  777. } \
  778. spin_unlock(&t10_wwn->t10_vpd_lock); \
  779. \
  780. return len; \
  781. }
  782. /*
  783. * VPD page 0x83 Association: Logical Unit
  784. */
  785. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
  786. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
  787. struct t10_wwn *t10_wwn,
  788. const char *page,
  789. size_t count)
  790. {
  791. return -ENOSYS;
  792. }
  793. SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
  794. /*
  795. * VPD page 0x83 Association: Target Port
  796. */
  797. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
  798. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
  799. struct t10_wwn *t10_wwn,
  800. const char *page,
  801. size_t count)
  802. {
  803. return -ENOSYS;
  804. }
  805. SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
  806. /*
  807. * VPD page 0x83 Association: SCSI Target Device
  808. */
  809. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
  810. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
  811. struct t10_wwn *t10_wwn,
  812. const char *page,
  813. size_t count)
  814. {
  815. return -ENOSYS;
  816. }
  817. SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
  818. CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
  819. static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
  820. &target_core_dev_wwn_vpd_unit_serial.attr,
  821. &target_core_dev_wwn_vpd_protocol_identifier.attr,
  822. &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
  823. &target_core_dev_wwn_vpd_assoc_target_port.attr,
  824. &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
  825. NULL,
  826. };
  827. static struct configfs_item_operations target_core_dev_wwn_ops = {
  828. .show_attribute = target_core_dev_wwn_attr_show,
  829. .store_attribute = target_core_dev_wwn_attr_store,
  830. };
  831. static struct config_item_type target_core_dev_wwn_cit = {
  832. .ct_item_ops = &target_core_dev_wwn_ops,
  833. .ct_attrs = target_core_dev_wwn_attrs,
  834. .ct_owner = THIS_MODULE,
  835. };
  836. /* End functions for struct config_item_type target_core_dev_wwn_cit */
  837. /* Start functions for struct config_item_type target_core_dev_pr_cit */
  838. CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
  839. #define SE_DEV_PR_ATTR(_name, _mode) \
  840. static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
  841. __CONFIGFS_EATTR(_name, _mode, \
  842. target_core_dev_pr_show_attr_##_name, \
  843. target_core_dev_pr_store_attr_##_name);
  844. #define SE_DEV_PR_ATTR_RO(_name); \
  845. static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
  846. __CONFIGFS_EATTR_RO(_name, \
  847. target_core_dev_pr_show_attr_##_name);
  848. static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
  849. char *page)
  850. {
  851. struct se_node_acl *se_nacl;
  852. struct t10_pr_registration *pr_reg;
  853. char i_buf[PR_REG_ISID_ID_LEN];
  854. int prf_isid;
  855. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  856. pr_reg = dev->dev_pr_res_holder;
  857. if (!pr_reg)
  858. return sprintf(page, "No SPC-3 Reservation holder\n");
  859. se_nacl = pr_reg->pr_reg_nacl;
  860. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  861. PR_REG_ISID_ID_LEN);
  862. return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
  863. se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  864. se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
  865. }
  866. static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
  867. char *page)
  868. {
  869. struct se_node_acl *se_nacl;
  870. ssize_t len;
  871. se_nacl = dev->dev_reserved_node_acl;
  872. if (se_nacl) {
  873. len = sprintf(page,
  874. "SPC-2 Reservation: %s Initiator: %s\n",
  875. se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  876. se_nacl->initiatorname);
  877. } else {
  878. len = sprintf(page, "No SPC-2 Reservation holder\n");
  879. }
  880. return len;
  881. }
  882. static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
  883. char *page)
  884. {
  885. int ret;
  886. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  887. return sprintf(page, "Passthrough\n");
  888. spin_lock(&dev->dev_reservation_lock);
  889. if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  890. ret = target_core_dev_pr_show_spc2_res(dev, page);
  891. else
  892. ret = target_core_dev_pr_show_spc3_res(dev, page);
  893. spin_unlock(&dev->dev_reservation_lock);
  894. return ret;
  895. }
  896. SE_DEV_PR_ATTR_RO(res_holder);
  897. static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
  898. struct se_device *dev, char *page)
  899. {
  900. ssize_t len = 0;
  901. spin_lock(&dev->dev_reservation_lock);
  902. if (!dev->dev_pr_res_holder) {
  903. len = sprintf(page, "No SPC-3 Reservation holder\n");
  904. } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
  905. len = sprintf(page, "SPC-3 Reservation: All Target"
  906. " Ports registration\n");
  907. } else {
  908. len = sprintf(page, "SPC-3 Reservation: Single"
  909. " Target Port registration\n");
  910. }
  911. spin_unlock(&dev->dev_reservation_lock);
  912. return len;
  913. }
  914. SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
  915. static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
  916. struct se_device *dev, char *page)
  917. {
  918. return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
  919. }
  920. SE_DEV_PR_ATTR_RO(res_pr_generation);
  921. /*
  922. * res_pr_holder_tg_port
  923. */
  924. static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
  925. struct se_device *dev, char *page)
  926. {
  927. struct se_node_acl *se_nacl;
  928. struct se_lun *lun;
  929. struct se_portal_group *se_tpg;
  930. struct t10_pr_registration *pr_reg;
  931. struct target_core_fabric_ops *tfo;
  932. ssize_t len = 0;
  933. spin_lock(&dev->dev_reservation_lock);
  934. pr_reg = dev->dev_pr_res_holder;
  935. if (!pr_reg) {
  936. len = sprintf(page, "No SPC-3 Reservation holder\n");
  937. goto out_unlock;
  938. }
  939. se_nacl = pr_reg->pr_reg_nacl;
  940. se_tpg = se_nacl->se_tpg;
  941. lun = pr_reg->pr_reg_tg_pt_lun;
  942. tfo = se_tpg->se_tpg_tfo;
  943. len += sprintf(page+len, "SPC-3 Reservation: %s"
  944. " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
  945. tfo->tpg_get_wwn(se_tpg));
  946. len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
  947. " Identifier Tag: %hu %s Portal Group Tag: %hu"
  948. " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
  949. tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
  950. tfo->get_fabric_name(), lun->unpacked_lun);
  951. out_unlock:
  952. spin_unlock(&dev->dev_reservation_lock);
  953. return len;
  954. }
  955. SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
  956. static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
  957. struct se_device *dev, char *page)
  958. {
  959. struct target_core_fabric_ops *tfo;
  960. struct t10_pr_registration *pr_reg;
  961. unsigned char buf[384];
  962. char i_buf[PR_REG_ISID_ID_LEN];
  963. ssize_t len = 0;
  964. int reg_count = 0, prf_isid;
  965. len += sprintf(page+len, "SPC-3 PR Registrations:\n");
  966. spin_lock(&dev->t10_pr.registration_lock);
  967. list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
  968. pr_reg_list) {
  969. memset(buf, 0, 384);
  970. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  971. tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
  972. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  973. PR_REG_ISID_ID_LEN);
  974. sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
  975. tfo->get_fabric_name(),
  976. pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
  977. &i_buf[0] : "", 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_store_dev_enable(
  1369. void *p,
  1370. const char *page,
  1371. size_t count)
  1372. {
  1373. struct se_device *dev = p;
  1374. char *ptr;
  1375. int ret;
  1376. ptr = strstr(page, "1");
  1377. if (!ptr) {
  1378. pr_err("For dev_enable ops, only valid value"
  1379. " is \"1\"\n");
  1380. return -EINVAL;
  1381. }
  1382. ret = target_configure_device(dev);
  1383. if (ret)
  1384. return ret;
  1385. return count;
  1386. }
  1387. static struct target_core_configfs_attribute target_core_attr_dev_enable = {
  1388. .attr = { .ca_owner = THIS_MODULE,
  1389. .ca_name = "enable",
  1390. .ca_mode = S_IWUSR },
  1391. .show = NULL,
  1392. .store = target_core_store_dev_enable,
  1393. };
  1394. static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
  1395. {
  1396. struct se_device *dev = p;
  1397. struct config_item *lu_ci;
  1398. struct t10_alua_lu_gp *lu_gp;
  1399. struct t10_alua_lu_gp_member *lu_gp_mem;
  1400. ssize_t len = 0;
  1401. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1402. if (!lu_gp_mem)
  1403. return 0;
  1404. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1405. lu_gp = lu_gp_mem->lu_gp;
  1406. if (lu_gp) {
  1407. lu_ci = &lu_gp->lu_gp_group.cg_item;
  1408. len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
  1409. config_item_name(lu_ci), lu_gp->lu_gp_id);
  1410. }
  1411. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1412. return len;
  1413. }
  1414. static ssize_t target_core_store_alua_lu_gp(
  1415. void *p,
  1416. const char *page,
  1417. size_t count)
  1418. {
  1419. struct se_device *dev = p;
  1420. struct se_hba *hba = dev->se_hba;
  1421. struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
  1422. struct t10_alua_lu_gp_member *lu_gp_mem;
  1423. unsigned char buf[LU_GROUP_NAME_BUF];
  1424. int move = 0;
  1425. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1426. if (!lu_gp_mem)
  1427. return 0;
  1428. if (count > LU_GROUP_NAME_BUF) {
  1429. pr_err("ALUA LU Group Alias too large!\n");
  1430. return -EINVAL;
  1431. }
  1432. memset(buf, 0, LU_GROUP_NAME_BUF);
  1433. memcpy(buf, page, count);
  1434. /*
  1435. * Any ALUA logical unit alias besides "NULL" means we will be
  1436. * making a new group association.
  1437. */
  1438. if (strcmp(strstrip(buf), "NULL")) {
  1439. /*
  1440. * core_alua_get_lu_gp_by_name() will increment reference to
  1441. * struct t10_alua_lu_gp. This reference is released with
  1442. * core_alua_get_lu_gp_by_name below().
  1443. */
  1444. lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
  1445. if (!lu_gp_new)
  1446. return -ENODEV;
  1447. }
  1448. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1449. lu_gp = lu_gp_mem->lu_gp;
  1450. if (lu_gp) {
  1451. /*
  1452. * Clearing an existing lu_gp association, and replacing
  1453. * with NULL
  1454. */
  1455. if (!lu_gp_new) {
  1456. pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
  1457. " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
  1458. " %hu\n",
  1459. config_item_name(&hba->hba_group.cg_item),
  1460. config_item_name(&dev->dev_group.cg_item),
  1461. config_item_name(&lu_gp->lu_gp_group.cg_item),
  1462. lu_gp->lu_gp_id);
  1463. __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
  1464. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1465. return count;
  1466. }
  1467. /*
  1468. * Removing existing association of lu_gp_mem with lu_gp
  1469. */
  1470. __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
  1471. move = 1;
  1472. }
  1473. /*
  1474. * Associate lu_gp_mem with lu_gp_new.
  1475. */
  1476. __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
  1477. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1478. pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
  1479. " core/alua/lu_gps/%s, ID: %hu\n",
  1480. (move) ? "Moving" : "Adding",
  1481. config_item_name(&hba->hba_group.cg_item),
  1482. config_item_name(&dev->dev_group.cg_item),
  1483. config_item_name(&lu_gp_new->lu_gp_group.cg_item),
  1484. lu_gp_new->lu_gp_id);
  1485. core_alua_put_lu_gp_from_name(lu_gp_new);
  1486. return count;
  1487. }
  1488. static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
  1489. .attr = { .ca_owner = THIS_MODULE,
  1490. .ca_name = "alua_lu_gp",
  1491. .ca_mode = S_IRUGO | S_IWUSR },
  1492. .show = target_core_show_alua_lu_gp,
  1493. .store = target_core_store_alua_lu_gp,
  1494. };
  1495. static struct configfs_attribute *lio_core_dev_attrs[] = {
  1496. &target_core_attr_dev_info.attr,
  1497. &target_core_attr_dev_control.attr,
  1498. &target_core_attr_dev_alias.attr,
  1499. &target_core_attr_dev_udev_path.attr,
  1500. &target_core_attr_dev_enable.attr,
  1501. &target_core_attr_dev_alua_lu_gp.attr,
  1502. NULL,
  1503. };
  1504. static void target_core_dev_release(struct config_item *item)
  1505. {
  1506. struct config_group *dev_cg = to_config_group(item);
  1507. struct se_device *dev =
  1508. container_of(dev_cg, struct se_device, dev_group);
  1509. kfree(dev_cg->default_groups);
  1510. target_free_device(dev);
  1511. }
  1512. static ssize_t target_core_dev_show(struct config_item *item,
  1513. struct configfs_attribute *attr,
  1514. char *page)
  1515. {
  1516. struct config_group *dev_cg = to_config_group(item);
  1517. struct se_device *dev =
  1518. container_of(dev_cg, struct se_device, dev_group);
  1519. struct target_core_configfs_attribute *tc_attr = container_of(
  1520. attr, struct target_core_configfs_attribute, attr);
  1521. if (!tc_attr->show)
  1522. return -EINVAL;
  1523. return tc_attr->show(dev, page);
  1524. }
  1525. static ssize_t target_core_dev_store(struct config_item *item,
  1526. struct configfs_attribute *attr,
  1527. const char *page, size_t count)
  1528. {
  1529. struct config_group *dev_cg = to_config_group(item);
  1530. struct se_device *dev =
  1531. container_of(dev_cg, struct se_device, dev_group);
  1532. struct target_core_configfs_attribute *tc_attr = container_of(
  1533. attr, struct target_core_configfs_attribute, attr);
  1534. if (!tc_attr->store)
  1535. return -EINVAL;
  1536. return tc_attr->store(dev, page, count);
  1537. }
  1538. static struct configfs_item_operations target_core_dev_item_ops = {
  1539. .release = target_core_dev_release,
  1540. .show_attribute = target_core_dev_show,
  1541. .store_attribute = target_core_dev_store,
  1542. };
  1543. static struct config_item_type target_core_dev_cit = {
  1544. .ct_item_ops = &target_core_dev_item_ops,
  1545. .ct_attrs = lio_core_dev_attrs,
  1546. .ct_owner = THIS_MODULE,
  1547. };
  1548. /* End functions for struct config_item_type target_core_dev_cit */
  1549. /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
  1550. CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
  1551. #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
  1552. static struct target_core_alua_lu_gp_attribute \
  1553. target_core_alua_lu_gp_##_name = \
  1554. __CONFIGFS_EATTR(_name, _mode, \
  1555. target_core_alua_lu_gp_show_attr_##_name, \
  1556. target_core_alua_lu_gp_store_attr_##_name);
  1557. #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
  1558. static struct target_core_alua_lu_gp_attribute \
  1559. target_core_alua_lu_gp_##_name = \
  1560. __CONFIGFS_EATTR_RO(_name, \
  1561. target_core_alua_lu_gp_show_attr_##_name);
  1562. /*
  1563. * lu_gp_id
  1564. */
  1565. static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
  1566. struct t10_alua_lu_gp *lu_gp,
  1567. char *page)
  1568. {
  1569. if (!lu_gp->lu_gp_valid_id)
  1570. return 0;
  1571. return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
  1572. }
  1573. static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
  1574. struct t10_alua_lu_gp *lu_gp,
  1575. const char *page,
  1576. size_t count)
  1577. {
  1578. struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
  1579. unsigned long lu_gp_id;
  1580. int ret;
  1581. ret = strict_strtoul(page, 0, &lu_gp_id);
  1582. if (ret < 0) {
  1583. pr_err("strict_strtoul() returned %d for"
  1584. " lu_gp_id\n", ret);
  1585. return -EINVAL;
  1586. }
  1587. if (lu_gp_id > 0x0000ffff) {
  1588. pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
  1589. " 0x0000ffff\n", lu_gp_id);
  1590. return -EINVAL;
  1591. }
  1592. ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
  1593. if (ret < 0)
  1594. return -EINVAL;
  1595. pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
  1596. " Group: core/alua/lu_gps/%s to ID: %hu\n",
  1597. config_item_name(&alua_lu_gp_cg->cg_item),
  1598. lu_gp->lu_gp_id);
  1599. return count;
  1600. }
  1601. SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
  1602. /*
  1603. * members
  1604. */
  1605. static ssize_t target_core_alua_lu_gp_show_attr_members(
  1606. struct t10_alua_lu_gp *lu_gp,
  1607. char *page)
  1608. {
  1609. struct se_device *dev;
  1610. struct se_hba *hba;
  1611. struct t10_alua_lu_gp_member *lu_gp_mem;
  1612. ssize_t len = 0, cur_len;
  1613. unsigned char buf[LU_GROUP_NAME_BUF];
  1614. memset(buf, 0, LU_GROUP_NAME_BUF);
  1615. spin_lock(&lu_gp->lu_gp_lock);
  1616. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1617. dev = lu_gp_mem->lu_gp_mem_dev;
  1618. hba = dev->se_hba;
  1619. cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
  1620. config_item_name(&hba->hba_group.cg_item),
  1621. config_item_name(&dev->dev_group.cg_item));
  1622. cur_len++; /* Extra byte for NULL terminator */
  1623. if ((cur_len + len) > PAGE_SIZE) {
  1624. pr_warn("Ran out of lu_gp_show_attr"
  1625. "_members buffer\n");
  1626. break;
  1627. }
  1628. memcpy(page+len, buf, cur_len);
  1629. len += cur_len;
  1630. }
  1631. spin_unlock(&lu_gp->lu_gp_lock);
  1632. return len;
  1633. }
  1634. SE_DEV_ALUA_LU_ATTR_RO(members);
  1635. CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
  1636. static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
  1637. &target_core_alua_lu_gp_lu_gp_id.attr,
  1638. &target_core_alua_lu_gp_members.attr,
  1639. NULL,
  1640. };
  1641. static void target_core_alua_lu_gp_release(struct config_item *item)
  1642. {
  1643. struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
  1644. struct t10_alua_lu_gp, lu_gp_group);
  1645. core_alua_free_lu_gp(lu_gp);
  1646. }
  1647. static struct configfs_item_operations target_core_alua_lu_gp_ops = {
  1648. .release = target_core_alua_lu_gp_release,
  1649. .show_attribute = target_core_alua_lu_gp_attr_show,
  1650. .store_attribute = target_core_alua_lu_gp_attr_store,
  1651. };
  1652. static struct config_item_type target_core_alua_lu_gp_cit = {
  1653. .ct_item_ops = &target_core_alua_lu_gp_ops,
  1654. .ct_attrs = target_core_alua_lu_gp_attrs,
  1655. .ct_owner = THIS_MODULE,
  1656. };
  1657. /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
  1658. /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
  1659. static struct config_group *target_core_alua_create_lu_gp(
  1660. struct config_group *group,
  1661. const char *name)
  1662. {
  1663. struct t10_alua_lu_gp *lu_gp;
  1664. struct config_group *alua_lu_gp_cg = NULL;
  1665. struct config_item *alua_lu_gp_ci = NULL;
  1666. lu_gp = core_alua_allocate_lu_gp(name, 0);
  1667. if (IS_ERR(lu_gp))
  1668. return NULL;
  1669. alua_lu_gp_cg = &lu_gp->lu_gp_group;
  1670. alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
  1671. config_group_init_type_name(alua_lu_gp_cg, name,
  1672. &target_core_alua_lu_gp_cit);
  1673. pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
  1674. " Group: core/alua/lu_gps/%s\n",
  1675. config_item_name(alua_lu_gp_ci));
  1676. return alua_lu_gp_cg;
  1677. }
  1678. static void target_core_alua_drop_lu_gp(
  1679. struct config_group *group,
  1680. struct config_item *item)
  1681. {
  1682. struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
  1683. struct t10_alua_lu_gp, lu_gp_group);
  1684. pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
  1685. " Group: core/alua/lu_gps/%s, ID: %hu\n",
  1686. config_item_name(item), lu_gp->lu_gp_id);
  1687. /*
  1688. * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
  1689. * -> target_core_alua_lu_gp_release()
  1690. */
  1691. config_item_put(item);
  1692. }
  1693. static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
  1694. .make_group = &target_core_alua_create_lu_gp,
  1695. .drop_item = &target_core_alua_drop_lu_gp,
  1696. };
  1697. static struct config_item_type target_core_alua_lu_gps_cit = {
  1698. .ct_item_ops = NULL,
  1699. .ct_group_ops = &target_core_alua_lu_gps_group_ops,
  1700. .ct_owner = THIS_MODULE,
  1701. };
  1702. /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
  1703. /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
  1704. CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
  1705. #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
  1706. static struct target_core_alua_tg_pt_gp_attribute \
  1707. target_core_alua_tg_pt_gp_##_name = \
  1708. __CONFIGFS_EATTR(_name, _mode, \
  1709. target_core_alua_tg_pt_gp_show_attr_##_name, \
  1710. target_core_alua_tg_pt_gp_store_attr_##_name);
  1711. #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
  1712. static struct target_core_alua_tg_pt_gp_attribute \
  1713. target_core_alua_tg_pt_gp_##_name = \
  1714. __CONFIGFS_EATTR_RO(_name, \
  1715. target_core_alua_tg_pt_gp_show_attr_##_name);
  1716. /*
  1717. * alua_access_state
  1718. */
  1719. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
  1720. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1721. char *page)
  1722. {
  1723. return sprintf(page, "%d\n",
  1724. atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
  1725. }
  1726. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
  1727. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1728. const char *page,
  1729. size_t count)
  1730. {
  1731. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1732. unsigned long tmp;
  1733. int new_state, ret;
  1734. if (!tg_pt_gp->tg_pt_gp_valid_id) {
  1735. pr_err("Unable to do implict ALUA on non valid"
  1736. " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
  1737. return -EINVAL;
  1738. }
  1739. ret = strict_strtoul(page, 0, &tmp);
  1740. if (ret < 0) {
  1741. pr_err("Unable to extract new ALUA access state from"
  1742. " %s\n", page);
  1743. return -EINVAL;
  1744. }
  1745. new_state = (int)tmp;
  1746. if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
  1747. pr_err("Unable to process implict configfs ALUA"
  1748. " transition while TPGS_IMPLICT_ALUA is disabled\n");
  1749. return -EINVAL;
  1750. }
  1751. ret = core_alua_do_port_transition(tg_pt_gp, dev,
  1752. NULL, NULL, new_state, 0);
  1753. return (!ret) ? count : -EINVAL;
  1754. }
  1755. SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
  1756. /*
  1757. * alua_access_status
  1758. */
  1759. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
  1760. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1761. char *page)
  1762. {
  1763. return sprintf(page, "%s\n",
  1764. core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
  1765. }
  1766. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
  1767. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1768. const char *page,
  1769. size_t count)
  1770. {
  1771. unsigned long tmp;
  1772. int new_status, ret;
  1773. if (!tg_pt_gp->tg_pt_gp_valid_id) {
  1774. pr_err("Unable to do set ALUA access status on non"
  1775. " valid tg_pt_gp ID: %hu\n",
  1776. tg_pt_gp->tg_pt_gp_valid_id);
  1777. return -EINVAL;
  1778. }
  1779. ret = strict_strtoul(page, 0, &tmp);
  1780. if (ret < 0) {
  1781. pr_err("Unable to extract new ALUA access status"
  1782. " from %s\n", page);
  1783. return -EINVAL;
  1784. }
  1785. new_status = (int)tmp;
  1786. if ((new_status != ALUA_STATUS_NONE) &&
  1787. (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  1788. (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
  1789. pr_err("Illegal ALUA access status: 0x%02x\n",
  1790. new_status);
  1791. return -EINVAL;
  1792. }
  1793. tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
  1794. return count;
  1795. }
  1796. SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
  1797. /*
  1798. * alua_access_type
  1799. */
  1800. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
  1801. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1802. char *page)
  1803. {
  1804. return core_alua_show_access_type(tg_pt_gp, page);
  1805. }
  1806. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
  1807. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1808. const char *page,
  1809. size_t count)
  1810. {
  1811. return core_alua_store_access_type(tg_pt_gp, page, count);
  1812. }
  1813. SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
  1814. /*
  1815. * alua_write_metadata
  1816. */
  1817. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
  1818. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1819. char *page)
  1820. {
  1821. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
  1822. }
  1823. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
  1824. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1825. const char *page,
  1826. size_t count)
  1827. {
  1828. unsigned long tmp;
  1829. int ret;
  1830. ret = strict_strtoul(page, 0, &tmp);
  1831. if (ret < 0) {
  1832. pr_err("Unable to extract alua_write_metadata\n");
  1833. return -EINVAL;
  1834. }
  1835. if ((tmp != 0) && (tmp != 1)) {
  1836. pr_err("Illegal value for alua_write_metadata:"
  1837. " %lu\n", tmp);
  1838. return -EINVAL;
  1839. }
  1840. tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
  1841. return count;
  1842. }
  1843. SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
  1844. /*
  1845. * nonop_delay_msecs
  1846. */
  1847. static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
  1848. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1849. char *page)
  1850. {
  1851. return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
  1852. }
  1853. static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
  1854. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1855. const char *page,
  1856. size_t count)
  1857. {
  1858. return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
  1859. }
  1860. SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
  1861. /*
  1862. * trans_delay_msecs
  1863. */
  1864. static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
  1865. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1866. char *page)
  1867. {
  1868. return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
  1869. }
  1870. static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
  1871. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1872. const char *page,
  1873. size_t count)
  1874. {
  1875. return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
  1876. }
  1877. SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
  1878. /*
  1879. * implict_trans_secs
  1880. */
  1881. static ssize_t target_core_alua_tg_pt_gp_show_attr_implict_trans_secs(
  1882. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1883. char *page)
  1884. {
  1885. return core_alua_show_implict_trans_secs(tg_pt_gp, page);
  1886. }
  1887. static ssize_t target_core_alua_tg_pt_gp_store_attr_implict_trans_secs(
  1888. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1889. const char *page,
  1890. size_t count)
  1891. {
  1892. return core_alua_store_implict_trans_secs(tg_pt_gp, page, count);
  1893. }
  1894. SE_DEV_ALUA_TG_PT_ATTR(implict_trans_secs, S_IRUGO | S_IWUSR);
  1895. /*
  1896. * preferred
  1897. */
  1898. static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
  1899. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1900. char *page)
  1901. {
  1902. return core_alua_show_preferred_bit(tg_pt_gp, page);
  1903. }
  1904. static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
  1905. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1906. const char *page,
  1907. size_t count)
  1908. {
  1909. return core_alua_store_preferred_bit(tg_pt_gp, page, count);
  1910. }
  1911. SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
  1912. /*
  1913. * tg_pt_gp_id
  1914. */
  1915. static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
  1916. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1917. char *page)
  1918. {
  1919. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1920. return 0;
  1921. return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
  1922. }
  1923. static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
  1924. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1925. const char *page,
  1926. size_t count)
  1927. {
  1928. struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
  1929. unsigned long tg_pt_gp_id;
  1930. int ret;
  1931. ret = strict_strtoul(page, 0, &tg_pt_gp_id);
  1932. if (ret < 0) {
  1933. pr_err("strict_strtoul() returned %d for"
  1934. " tg_pt_gp_id\n", ret);
  1935. return -EINVAL;
  1936. }
  1937. if (tg_pt_gp_id > 0x0000ffff) {
  1938. pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
  1939. " 0x0000ffff\n", tg_pt_gp_id);
  1940. return -EINVAL;
  1941. }
  1942. ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
  1943. if (ret < 0)
  1944. return -EINVAL;
  1945. pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
  1946. "core/alua/tg_pt_gps/%s to ID: %hu\n",
  1947. config_item_name(&alua_tg_pt_gp_cg->cg_item),
  1948. tg_pt_gp->tg_pt_gp_id);
  1949. return count;
  1950. }
  1951. SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
  1952. /*
  1953. * members
  1954. */
  1955. static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
  1956. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1957. char *page)
  1958. {
  1959. struct se_port *port;
  1960. struct se_portal_group *tpg;
  1961. struct se_lun *lun;
  1962. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1963. ssize_t len = 0, cur_len;
  1964. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1965. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1966. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1967. list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  1968. tg_pt_gp_mem_list) {
  1969. port = tg_pt_gp_mem->tg_pt;
  1970. tpg = port->sep_tpg;
  1971. lun = port->sep_lun;
  1972. cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
  1973. "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1974. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1975. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1976. config_item_name(&lun->lun_group.cg_item));
  1977. cur_len++; /* Extra byte for NULL terminator */
  1978. if ((cur_len + len) > PAGE_SIZE) {
  1979. pr_warn("Ran out of lu_gp_show_attr"
  1980. "_members buffer\n");
  1981. break;
  1982. }
  1983. memcpy(page+len, buf, cur_len);
  1984. len += cur_len;
  1985. }
  1986. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1987. return len;
  1988. }
  1989. SE_DEV_ALUA_TG_PT_ATTR_RO(members);
  1990. CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
  1991. tg_pt_gp_group);
  1992. static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
  1993. &target_core_alua_tg_pt_gp_alua_access_state.attr,
  1994. &target_core_alua_tg_pt_gp_alua_access_status.attr,
  1995. &target_core_alua_tg_pt_gp_alua_access_type.attr,
  1996. &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
  1997. &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
  1998. &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
  1999. &target_core_alua_tg_pt_gp_implict_trans_secs.attr,
  2000. &target_core_alua_tg_pt_gp_preferred.attr,
  2001. &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
  2002. &target_core_alua_tg_pt_gp_members.attr,
  2003. NULL,
  2004. };
  2005. static void target_core_alua_tg_pt_gp_release(struct config_item *item)
  2006. {
  2007. struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
  2008. struct t10_alua_tg_pt_gp, tg_pt_gp_group);
  2009. core_alua_free_tg_pt_gp(tg_pt_gp);
  2010. }
  2011. static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
  2012. .release = target_core_alua_tg_pt_gp_release,
  2013. .show_attribute = target_core_alua_tg_pt_gp_attr_show,
  2014. .store_attribute = target_core_alua_tg_pt_gp_attr_store,
  2015. };
  2016. static struct config_item_type target_core_alua_tg_pt_gp_cit = {
  2017. .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
  2018. .ct_attrs = target_core_alua_tg_pt_gp_attrs,
  2019. .ct_owner = THIS_MODULE,
  2020. };
  2021. /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
  2022. /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
  2023. static struct config_group *target_core_alua_create_tg_pt_gp(
  2024. struct config_group *group,
  2025. const char *name)
  2026. {
  2027. struct t10_alua *alua = container_of(group, struct t10_alua,
  2028. alua_tg_pt_gps_group);
  2029. struct t10_alua_tg_pt_gp *tg_pt_gp;
  2030. struct config_group *alua_tg_pt_gp_cg = NULL;
  2031. struct config_item *alua_tg_pt_gp_ci = NULL;
  2032. tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
  2033. if (!tg_pt_gp)
  2034. return NULL;
  2035. alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
  2036. alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
  2037. config_group_init_type_name(alua_tg_pt_gp_cg, name,
  2038. &target_core_alua_tg_pt_gp_cit);
  2039. pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
  2040. " Group: alua/tg_pt_gps/%s\n",
  2041. config_item_name(alua_tg_pt_gp_ci));
  2042. return alua_tg_pt_gp_cg;
  2043. }
  2044. static void target_core_alua_drop_tg_pt_gp(
  2045. struct config_group *group,
  2046. struct config_item *item)
  2047. {
  2048. struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
  2049. struct t10_alua_tg_pt_gp, tg_pt_gp_group);
  2050. pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
  2051. " Group: alua/tg_pt_gps/%s, ID: %hu\n",
  2052. config_item_name(item), tg_pt_gp->tg_pt_gp_id);
  2053. /*
  2054. * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
  2055. * -> target_core_alua_tg_pt_gp_release().
  2056. */
  2057. config_item_put(item);
  2058. }
  2059. static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
  2060. .make_group = &target_core_alua_create_tg_pt_gp,
  2061. .drop_item = &target_core_alua_drop_tg_pt_gp,
  2062. };
  2063. static struct config_item_type target_core_alua_tg_pt_gps_cit = {
  2064. .ct_group_ops = &target_core_alua_tg_pt_gps_group_ops,
  2065. .ct_owner = THIS_MODULE,
  2066. };
  2067. /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
  2068. /* Start functions for struct config_item_type target_core_alua_cit */
  2069. /*
  2070. * target_core_alua_cit is a ConfigFS group that lives under
  2071. * /sys/kernel/config/target/core/alua. There are default groups
  2072. * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
  2073. * target_core_alua_cit in target_core_init_configfs() below.
  2074. */
  2075. static struct config_item_type target_core_alua_cit = {
  2076. .ct_item_ops = NULL,
  2077. .ct_attrs = NULL,
  2078. .ct_owner = THIS_MODULE,
  2079. };
  2080. /* End functions for struct config_item_type target_core_alua_cit */
  2081. /* Start functions for struct config_item_type target_core_stat_cit */
  2082. static struct config_group *target_core_stat_mkdir(
  2083. struct config_group *group,
  2084. const char *name)
  2085. {
  2086. return ERR_PTR(-ENOSYS);
  2087. }
  2088. static void target_core_stat_rmdir(
  2089. struct config_group *group,
  2090. struct config_item *item)
  2091. {
  2092. return;
  2093. }
  2094. static struct configfs_group_operations target_core_stat_group_ops = {
  2095. .make_group = &target_core_stat_mkdir,
  2096. .drop_item = &target_core_stat_rmdir,
  2097. };
  2098. static struct config_item_type target_core_stat_cit = {
  2099. .ct_group_ops = &target_core_stat_group_ops,
  2100. .ct_owner = THIS_MODULE,
  2101. };
  2102. /* End functions for struct config_item_type target_core_stat_cit */
  2103. /* Start functions for struct config_item_type target_core_hba_cit */
  2104. static struct config_group *target_core_make_subdev(
  2105. struct config_group *group,
  2106. const char *name)
  2107. {
  2108. struct t10_alua_tg_pt_gp *tg_pt_gp;
  2109. struct se_subsystem_api *t;
  2110. struct config_item *hba_ci = &group->cg_item;
  2111. struct se_hba *hba = item_to_hba(hba_ci);
  2112. struct se_device *dev;
  2113. struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
  2114. struct config_group *dev_stat_grp = NULL;
  2115. int errno = -ENOMEM, ret;
  2116. ret = mutex_lock_interruptible(&hba->hba_access_mutex);
  2117. if (ret)
  2118. return ERR_PTR(ret);
  2119. /*
  2120. * Locate the struct se_subsystem_api from parent's struct se_hba.
  2121. */
  2122. t = hba->transport;
  2123. dev = target_alloc_device(hba, name);
  2124. if (!dev)
  2125. goto out_unlock;
  2126. dev_cg = &dev->dev_group;
  2127. dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
  2128. GFP_KERNEL);
  2129. if (!dev_cg->default_groups)
  2130. goto out_free_device;
  2131. config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
  2132. config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
  2133. &target_core_dev_attrib_cit);
  2134. config_group_init_type_name(&dev->dev_pr_group, "pr",
  2135. &target_core_dev_pr_cit);
  2136. config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
  2137. &target_core_dev_wwn_cit);
  2138. config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
  2139. "alua", &target_core_alua_tg_pt_gps_cit);
  2140. config_group_init_type_name(&dev->dev_stat_grps.stat_group,
  2141. "statistics", &target_core_stat_cit);
  2142. dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
  2143. dev_cg->default_groups[1] = &dev->dev_pr_group;
  2144. dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
  2145. dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
  2146. dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
  2147. dev_cg->default_groups[5] = NULL;
  2148. /*
  2149. * Add core/$HBA/$DEV/alua/default_tg_pt_gp
  2150. */
  2151. tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
  2152. if (!tg_pt_gp)
  2153. goto out_free_dev_cg_default_groups;
  2154. dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
  2155. tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
  2156. tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2157. GFP_KERNEL);
  2158. if (!tg_pt_gp_cg->default_groups) {
  2159. pr_err("Unable to allocate tg_pt_gp_cg->"
  2160. "default_groups\n");
  2161. goto out_free_tg_pt_gp;
  2162. }
  2163. config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
  2164. "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
  2165. tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
  2166. tg_pt_gp_cg->default_groups[1] = NULL;
  2167. /*
  2168. * Add core/$HBA/$DEV/statistics/ default groups
  2169. */
  2170. dev_stat_grp = &dev->dev_stat_grps.stat_group;
  2171. dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
  2172. GFP_KERNEL);
  2173. if (!dev_stat_grp->default_groups) {
  2174. pr_err("Unable to allocate dev_stat_grp->default_groups\n");
  2175. goto out_free_tg_pt_gp_cg_default_groups;
  2176. }
  2177. target_stat_setup_dev_default_groups(dev);
  2178. mutex_unlock(&hba->hba_access_mutex);
  2179. return dev_cg;
  2180. out_free_tg_pt_gp_cg_default_groups:
  2181. kfree(tg_pt_gp_cg->default_groups);
  2182. out_free_tg_pt_gp:
  2183. core_alua_free_tg_pt_gp(tg_pt_gp);
  2184. out_free_dev_cg_default_groups:
  2185. kfree(dev_cg->default_groups);
  2186. out_free_device:
  2187. target_free_device(dev);
  2188. out_unlock:
  2189. mutex_unlock(&hba->hba_access_mutex);
  2190. return ERR_PTR(errno);
  2191. }
  2192. static void target_core_drop_subdev(
  2193. struct config_group *group,
  2194. struct config_item *item)
  2195. {
  2196. struct config_group *dev_cg = to_config_group(item);
  2197. struct se_device *dev =
  2198. container_of(dev_cg, struct se_device, dev_group);
  2199. struct se_hba *hba;
  2200. struct config_item *df_item;
  2201. struct config_group *tg_pt_gp_cg, *dev_stat_grp;
  2202. int i;
  2203. hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
  2204. mutex_lock(&hba->hba_access_mutex);
  2205. dev_stat_grp = &dev->dev_stat_grps.stat_group;
  2206. for (i = 0; dev_stat_grp->default_groups[i]; i++) {
  2207. df_item = &dev_stat_grp->default_groups[i]->cg_item;
  2208. dev_stat_grp->default_groups[i] = NULL;
  2209. config_item_put(df_item);
  2210. }
  2211. kfree(dev_stat_grp->default_groups);
  2212. tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
  2213. for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
  2214. df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
  2215. tg_pt_gp_cg->default_groups[i] = NULL;
  2216. config_item_put(df_item);
  2217. }
  2218. kfree(tg_pt_gp_cg->default_groups);
  2219. /*
  2220. * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
  2221. * directly from target_core_alua_tg_pt_gp_release().
  2222. */
  2223. dev->t10_alua.default_tg_pt_gp = NULL;
  2224. for (i = 0; dev_cg->default_groups[i]; i++) {
  2225. df_item = &dev_cg->default_groups[i]->cg_item;
  2226. dev_cg->default_groups[i] = NULL;
  2227. config_item_put(df_item);
  2228. }
  2229. /*
  2230. * se_dev is released from target_core_dev_item_ops->release()
  2231. */
  2232. config_item_put(item);
  2233. mutex_unlock(&hba->hba_access_mutex);
  2234. }
  2235. static struct configfs_group_operations target_core_hba_group_ops = {
  2236. .make_group = target_core_make_subdev,
  2237. .drop_item = target_core_drop_subdev,
  2238. };
  2239. CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
  2240. #define SE_HBA_ATTR(_name, _mode) \
  2241. static struct target_core_hba_attribute \
  2242. target_core_hba_##_name = \
  2243. __CONFIGFS_EATTR(_name, _mode, \
  2244. target_core_hba_show_attr_##_name, \
  2245. target_core_hba_store_attr_##_name);
  2246. #define SE_HBA_ATTR_RO(_name) \
  2247. static struct target_core_hba_attribute \
  2248. target_core_hba_##_name = \
  2249. __CONFIGFS_EATTR_RO(_name, \
  2250. target_core_hba_show_attr_##_name);
  2251. static ssize_t target_core_hba_show_attr_hba_info(
  2252. struct se_hba *hba,
  2253. char *page)
  2254. {
  2255. return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
  2256. hba->hba_id, hba->transport->name,
  2257. TARGET_CORE_CONFIGFS_VERSION);
  2258. }
  2259. SE_HBA_ATTR_RO(hba_info);
  2260. static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
  2261. char *page)
  2262. {
  2263. int hba_mode = 0;
  2264. if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
  2265. hba_mode = 1;
  2266. return sprintf(page, "%d\n", hba_mode);
  2267. }
  2268. static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
  2269. const char *page, size_t count)
  2270. {
  2271. struct se_subsystem_api *transport = hba->transport;
  2272. unsigned long mode_flag;
  2273. int ret;
  2274. if (transport->pmode_enable_hba == NULL)
  2275. return -EINVAL;
  2276. ret = strict_strtoul(page, 0, &mode_flag);
  2277. if (ret < 0) {
  2278. pr_err("Unable to extract hba mode flag: %d\n", ret);
  2279. return -EINVAL;
  2280. }
  2281. if (hba->dev_count) {
  2282. pr_err("Unable to set hba_mode with active devices\n");
  2283. return -EINVAL;
  2284. }
  2285. ret = transport->pmode_enable_hba(hba, mode_flag);
  2286. if (ret < 0)
  2287. return -EINVAL;
  2288. if (ret > 0)
  2289. hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
  2290. else if (ret == 0)
  2291. hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
  2292. return count;
  2293. }
  2294. SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
  2295. CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
  2296. static void target_core_hba_release(struct config_item *item)
  2297. {
  2298. struct se_hba *hba = container_of(to_config_group(item),
  2299. struct se_hba, hba_group);
  2300. core_delete_hba(hba);
  2301. }
  2302. static struct configfs_attribute *target_core_hba_attrs[] = {
  2303. &target_core_hba_hba_info.attr,
  2304. &target_core_hba_hba_mode.attr,
  2305. NULL,
  2306. };
  2307. static struct configfs_item_operations target_core_hba_item_ops = {
  2308. .release = target_core_hba_release,
  2309. .show_attribute = target_core_hba_attr_show,
  2310. .store_attribute = target_core_hba_attr_store,
  2311. };
  2312. static struct config_item_type target_core_hba_cit = {
  2313. .ct_item_ops = &target_core_hba_item_ops,
  2314. .ct_group_ops = &target_core_hba_group_ops,
  2315. .ct_attrs = target_core_hba_attrs,
  2316. .ct_owner = THIS_MODULE,
  2317. };
  2318. static struct config_group *target_core_call_addhbatotarget(
  2319. struct config_group *group,
  2320. const char *name)
  2321. {
  2322. char *se_plugin_str, *str, *str2;
  2323. struct se_hba *hba;
  2324. char buf[TARGET_CORE_NAME_MAX_LEN];
  2325. unsigned long plugin_dep_id = 0;
  2326. int ret;
  2327. memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
  2328. if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
  2329. pr_err("Passed *name strlen(): %d exceeds"
  2330. " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
  2331. TARGET_CORE_NAME_MAX_LEN);
  2332. return ERR_PTR(-ENAMETOOLONG);
  2333. }
  2334. snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
  2335. str = strstr(buf, "_");
  2336. if (!str) {
  2337. pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
  2338. return ERR_PTR(-EINVAL);
  2339. }
  2340. se_plugin_str = buf;
  2341. /*
  2342. * Special case for subsystem plugins that have "_" in their names.
  2343. * Namely rd_direct and rd_mcp..
  2344. */
  2345. str2 = strstr(str+1, "_");
  2346. if (str2) {
  2347. *str2 = '\0'; /* Terminate for *se_plugin_str */
  2348. str2++; /* Skip to start of plugin dependent ID */
  2349. str = str2;
  2350. } else {
  2351. *str = '\0'; /* Terminate for *se_plugin_str */
  2352. str++; /* Skip to start of plugin dependent ID */
  2353. }
  2354. ret = strict_strtoul(str, 0, &plugin_dep_id);
  2355. if (ret < 0) {
  2356. pr_err("strict_strtoul() returned %d for"
  2357. " plugin_dep_id\n", ret);
  2358. return ERR_PTR(-EINVAL);
  2359. }
  2360. /*
  2361. * Load up TCM subsystem plugins if they have not already been loaded.
  2362. */
  2363. transport_subsystem_check_init();
  2364. hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
  2365. if (IS_ERR(hba))
  2366. return ERR_CAST(hba);
  2367. config_group_init_type_name(&hba->hba_group, name,
  2368. &target_core_hba_cit);
  2369. return &hba->hba_group;
  2370. }
  2371. static void target_core_call_delhbafromtarget(
  2372. struct config_group *group,
  2373. struct config_item *item)
  2374. {
  2375. /*
  2376. * core_delete_hba() is called from target_core_hba_item_ops->release()
  2377. * -> target_core_hba_release()
  2378. */
  2379. config_item_put(item);
  2380. }
  2381. static struct configfs_group_operations target_core_group_ops = {
  2382. .make_group = target_core_call_addhbatotarget,
  2383. .drop_item = target_core_call_delhbafromtarget,
  2384. };
  2385. static struct config_item_type target_core_cit = {
  2386. .ct_item_ops = NULL,
  2387. .ct_group_ops = &target_core_group_ops,
  2388. .ct_attrs = NULL,
  2389. .ct_owner = THIS_MODULE,
  2390. };
  2391. /* Stop functions for struct config_item_type target_core_hba_cit */
  2392. static int __init target_core_init_configfs(void)
  2393. {
  2394. struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
  2395. struct config_group *lu_gp_cg = NULL;
  2396. struct configfs_subsystem *subsys;
  2397. struct t10_alua_lu_gp *lu_gp;
  2398. int ret;
  2399. pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
  2400. " Engine: %s on %s/%s on "UTS_RELEASE"\n",
  2401. TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
  2402. subsys = target_core_subsystem[0];
  2403. config_group_init(&subsys->su_group);
  2404. mutex_init(&subsys->su_mutex);
  2405. ret = init_se_kmem_caches();
  2406. if (ret < 0)
  2407. return ret;
  2408. /*
  2409. * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
  2410. * and ALUA Logical Unit Group and Target Port Group infrastructure.
  2411. */
  2412. target_cg = &subsys->su_group;
  2413. target_cg->default_groups = kmalloc(sizeof(struct config_group) * 2,
  2414. GFP_KERNEL);
  2415. if (!target_cg->default_groups) {
  2416. pr_err("Unable to allocate target_cg->default_groups\n");
  2417. ret = -ENOMEM;
  2418. goto out_global;
  2419. }
  2420. config_group_init_type_name(&target_core_hbagroup,
  2421. "core", &target_core_cit);
  2422. target_cg->default_groups[0] = &target_core_hbagroup;
  2423. target_cg->default_groups[1] = NULL;
  2424. /*
  2425. * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
  2426. */
  2427. hba_cg = &target_core_hbagroup;
  2428. hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2429. GFP_KERNEL);
  2430. if (!hba_cg->default_groups) {
  2431. pr_err("Unable to allocate hba_cg->default_groups\n");
  2432. ret = -ENOMEM;
  2433. goto out_global;
  2434. }
  2435. config_group_init_type_name(&alua_group,
  2436. "alua", &target_core_alua_cit);
  2437. hba_cg->default_groups[0] = &alua_group;
  2438. hba_cg->default_groups[1] = NULL;
  2439. /*
  2440. * Add ALUA Logical Unit Group and Target Port Group ConfigFS
  2441. * groups under /sys/kernel/config/target/core/alua/
  2442. */
  2443. alua_cg = &alua_group;
  2444. alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2445. GFP_KERNEL);
  2446. if (!alua_cg->default_groups) {
  2447. pr_err("Unable to allocate alua_cg->default_groups\n");
  2448. ret = -ENOMEM;
  2449. goto out_global;
  2450. }
  2451. config_group_init_type_name(&alua_lu_gps_group,
  2452. "lu_gps", &target_core_alua_lu_gps_cit);
  2453. alua_cg->default_groups[0] = &alua_lu_gps_group;
  2454. alua_cg->default_groups[1] = NULL;
  2455. /*
  2456. * Add core/alua/lu_gps/default_lu_gp
  2457. */
  2458. lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
  2459. if (IS_ERR(lu_gp)) {
  2460. ret = -ENOMEM;
  2461. goto out_global;
  2462. }
  2463. lu_gp_cg = &alua_lu_gps_group;
  2464. lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2465. GFP_KERNEL);
  2466. if (!lu_gp_cg->default_groups) {
  2467. pr_err("Unable to allocate lu_gp_cg->default_groups\n");
  2468. ret = -ENOMEM;
  2469. goto out_global;
  2470. }
  2471. config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
  2472. &target_core_alua_lu_gp_cit);
  2473. lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
  2474. lu_gp_cg->default_groups[1] = NULL;
  2475. default_lu_gp = lu_gp;
  2476. /*
  2477. * Register the target_core_mod subsystem with configfs.
  2478. */
  2479. ret = configfs_register_subsystem(subsys);
  2480. if (ret < 0) {
  2481. pr_err("Error %d while registering subsystem %s\n",
  2482. ret, subsys->su_group.cg_item.ci_namebuf);
  2483. goto out_global;
  2484. }
  2485. pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
  2486. " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
  2487. " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
  2488. /*
  2489. * Register built-in RAMDISK subsystem logic for virtual LUN 0
  2490. */
  2491. ret = rd_module_init();
  2492. if (ret < 0)
  2493. goto out;
  2494. ret = core_dev_setup_virtual_lun0();
  2495. if (ret < 0)
  2496. goto out;
  2497. return 0;
  2498. out:
  2499. configfs_unregister_subsystem(subsys);
  2500. core_dev_release_virtual_lun0();
  2501. rd_module_exit();
  2502. out_global:
  2503. if (default_lu_gp) {
  2504. core_alua_free_lu_gp(default_lu_gp);
  2505. default_lu_gp = NULL;
  2506. }
  2507. if (lu_gp_cg)
  2508. kfree(lu_gp_cg->default_groups);
  2509. if (alua_cg)
  2510. kfree(alua_cg->default_groups);
  2511. if (hba_cg)
  2512. kfree(hba_cg->default_groups);
  2513. kfree(target_cg->default_groups);
  2514. release_se_kmem_caches();
  2515. return ret;
  2516. }
  2517. static void __exit target_core_exit_configfs(void)
  2518. {
  2519. struct configfs_subsystem *subsys;
  2520. struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
  2521. struct config_item *item;
  2522. int i;
  2523. subsys = target_core_subsystem[0];
  2524. lu_gp_cg = &alua_lu_gps_group;
  2525. for (i = 0; lu_gp_cg->default_groups[i]; i++) {
  2526. item = &lu_gp_cg->default_groups[i]->cg_item;
  2527. lu_gp_cg->default_groups[i] = NULL;
  2528. config_item_put(item);
  2529. }
  2530. kfree(lu_gp_cg->default_groups);
  2531. lu_gp_cg->default_groups = NULL;
  2532. alua_cg = &alua_group;
  2533. for (i = 0; alua_cg->default_groups[i]; i++) {
  2534. item = &alua_cg->default_groups[i]->cg_item;
  2535. alua_cg->default_groups[i] = NULL;
  2536. config_item_put(item);
  2537. }
  2538. kfree(alua_cg->default_groups);
  2539. alua_cg->default_groups = NULL;
  2540. hba_cg = &target_core_hbagroup;
  2541. for (i = 0; hba_cg->default_groups[i]; i++) {
  2542. item = &hba_cg->default_groups[i]->cg_item;
  2543. hba_cg->default_groups[i] = NULL;
  2544. config_item_put(item);
  2545. }
  2546. kfree(hba_cg->default_groups);
  2547. hba_cg->default_groups = NULL;
  2548. /*
  2549. * We expect subsys->su_group.default_groups to be released
  2550. * by configfs subsystem provider logic..
  2551. */
  2552. configfs_unregister_subsystem(subsys);
  2553. kfree(subsys->su_group.default_groups);
  2554. core_alua_free_lu_gp(default_lu_gp);
  2555. default_lu_gp = NULL;
  2556. pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
  2557. " Infrastructure\n");
  2558. core_dev_release_virtual_lun0();
  2559. rd_module_exit();
  2560. release_se_kmem_caches();
  2561. }
  2562. MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
  2563. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  2564. MODULE_LICENSE("GPL");
  2565. module_init(target_core_init_configfs);
  2566. module_exit(target_core_exit_configfs);