target_core_configfs.c 80 KB

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