target_core_configfs.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605
  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-2013 Datera, Inc.
  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/configfs_macros.h>
  43. #include "target_core_internal.h"
  44. #include "target_core_alua.h"
  45. #include "target_core_pr.h"
  46. #include "target_core_rd.h"
  47. #include "target_core_xcopy.h"
  48. #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
  49. static void target_core_setup_##_name##_cit(struct target_backend *tb) \
  50. { \
  51. struct config_item_type *cit = &tb->tb_##_name##_cit; \
  52. \
  53. cit->ct_item_ops = _item_ops; \
  54. cit->ct_group_ops = _group_ops; \
  55. cit->ct_attrs = _attrs; \
  56. cit->ct_owner = tb->ops->owner; \
  57. pr_debug("Setup generic %s\n", __stringify(_name)); \
  58. }
  59. #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
  60. static void target_core_setup_##_name##_cit(struct target_backend *tb) \
  61. { \
  62. struct config_item_type *cit = &tb->tb_##_name##_cit; \
  63. \
  64. cit->ct_item_ops = _item_ops; \
  65. cit->ct_group_ops = _group_ops; \
  66. cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
  67. cit->ct_owner = tb->ops->owner; \
  68. pr_debug("Setup generic %s\n", __stringify(_name)); \
  69. }
  70. extern struct t10_alua_lu_gp *default_lu_gp;
  71. static LIST_HEAD(g_tf_list);
  72. static DEFINE_MUTEX(g_tf_lock);
  73. struct target_core_configfs_attribute {
  74. struct configfs_attribute attr;
  75. ssize_t (*show)(void *, char *);
  76. ssize_t (*store)(void *, const char *, size_t);
  77. };
  78. static struct config_group target_core_hbagroup;
  79. static struct config_group alua_group;
  80. static struct config_group alua_lu_gps_group;
  81. static inline struct se_hba *
  82. item_to_hba(struct config_item *item)
  83. {
  84. return container_of(to_config_group(item), struct se_hba, hba_group);
  85. }
  86. /*
  87. * Attributes for /sys/kernel/config/target/
  88. */
  89. static ssize_t target_core_attr_show(struct config_item *item,
  90. struct configfs_attribute *attr,
  91. char *page)
  92. {
  93. return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
  94. " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
  95. utsname()->sysname, utsname()->machine);
  96. }
  97. static struct configfs_item_operations target_core_fabric_item_ops = {
  98. .show_attribute = target_core_attr_show,
  99. };
  100. static struct configfs_attribute target_core_item_attr_version = {
  101. .ca_owner = THIS_MODULE,
  102. .ca_name = "version",
  103. .ca_mode = S_IRUGO,
  104. };
  105. static struct target_fabric_configfs *target_core_get_fabric(
  106. const char *name)
  107. {
  108. struct target_fabric_configfs *tf;
  109. if (!name)
  110. return NULL;
  111. mutex_lock(&g_tf_lock);
  112. list_for_each_entry(tf, &g_tf_list, tf_list) {
  113. if (!strcmp(tf->tf_ops->name, name)) {
  114. atomic_inc(&tf->tf_access_cnt);
  115. mutex_unlock(&g_tf_lock);
  116. return tf;
  117. }
  118. }
  119. mutex_unlock(&g_tf_lock);
  120. return NULL;
  121. }
  122. /*
  123. * Called from struct target_core_group_ops->make_group()
  124. */
  125. static struct config_group *target_core_register_fabric(
  126. struct config_group *group,
  127. const char *name)
  128. {
  129. struct target_fabric_configfs *tf;
  130. int ret;
  131. pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
  132. " %s\n", group, name);
  133. tf = target_core_get_fabric(name);
  134. if (!tf) {
  135. pr_debug("target_core_register_fabric() trying autoload for %s\n",
  136. name);
  137. /*
  138. * Below are some hardcoded request_module() calls to automatically
  139. * local fabric modules when the following is called:
  140. *
  141. * mkdir -p /sys/kernel/config/target/$MODULE_NAME
  142. *
  143. * Note that this does not limit which TCM fabric module can be
  144. * registered, but simply provids auto loading logic for modules with
  145. * mkdir(2) system calls with known TCM fabric modules.
  146. */
  147. if (!strncmp(name, "iscsi", 5)) {
  148. /*
  149. * Automatically load the LIO Target fabric module when the
  150. * following is called:
  151. *
  152. * mkdir -p $CONFIGFS/target/iscsi
  153. */
  154. ret = request_module("iscsi_target_mod");
  155. if (ret < 0) {
  156. pr_debug("request_module() failed for"
  157. " iscsi_target_mod.ko: %d\n", ret);
  158. return ERR_PTR(-EINVAL);
  159. }
  160. } else if (!strncmp(name, "loopback", 8)) {
  161. /*
  162. * Automatically load the tcm_loop fabric module when the
  163. * following is called:
  164. *
  165. * mkdir -p $CONFIGFS/target/loopback
  166. */
  167. ret = request_module("tcm_loop");
  168. if (ret < 0) {
  169. pr_debug("request_module() failed for"
  170. " tcm_loop.ko: %d\n", ret);
  171. return ERR_PTR(-EINVAL);
  172. }
  173. }
  174. tf = target_core_get_fabric(name);
  175. }
  176. if (!tf) {
  177. pr_debug("target_core_get_fabric() failed for %s\n",
  178. name);
  179. return ERR_PTR(-EINVAL);
  180. }
  181. pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
  182. " %s\n", tf->tf_ops->name);
  183. /*
  184. * On a successful target_core_get_fabric() look, the returned
  185. * struct target_fabric_configfs *tf will contain a usage reference.
  186. */
  187. pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
  188. &tf->tf_wwn_cit);
  189. tf->tf_group.default_groups = tf->tf_default_groups;
  190. tf->tf_group.default_groups[0] = &tf->tf_disc_group;
  191. tf->tf_group.default_groups[1] = NULL;
  192. config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
  193. config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
  194. &tf->tf_discovery_cit);
  195. pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
  196. " %s\n", tf->tf_group.cg_item.ci_name);
  197. return &tf->tf_group;
  198. }
  199. /*
  200. * Called from struct target_core_group_ops->drop_item()
  201. */
  202. static void target_core_deregister_fabric(
  203. struct config_group *group,
  204. struct config_item *item)
  205. {
  206. struct target_fabric_configfs *tf = container_of(
  207. to_config_group(item), struct target_fabric_configfs, tf_group);
  208. struct config_group *tf_group;
  209. struct config_item *df_item;
  210. int i;
  211. pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
  212. " tf list\n", config_item_name(item));
  213. pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
  214. " %s\n", tf->tf_ops->name);
  215. atomic_dec(&tf->tf_access_cnt);
  216. pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
  217. " %s\n", config_item_name(item));
  218. tf_group = &tf->tf_group;
  219. for (i = 0; tf_group->default_groups[i]; i++) {
  220. df_item = &tf_group->default_groups[i]->cg_item;
  221. tf_group->default_groups[i] = NULL;
  222. config_item_put(df_item);
  223. }
  224. config_item_put(item);
  225. }
  226. static struct configfs_group_operations target_core_fabric_group_ops = {
  227. .make_group = &target_core_register_fabric,
  228. .drop_item = &target_core_deregister_fabric,
  229. };
  230. /*
  231. * All item attributes appearing in /sys/kernel/target/ appear here.
  232. */
  233. static struct configfs_attribute *target_core_fabric_item_attrs[] = {
  234. &target_core_item_attr_version,
  235. NULL,
  236. };
  237. /*
  238. * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
  239. */
  240. static struct config_item_type target_core_fabrics_item = {
  241. .ct_item_ops = &target_core_fabric_item_ops,
  242. .ct_group_ops = &target_core_fabric_group_ops,
  243. .ct_attrs = target_core_fabric_item_attrs,
  244. .ct_owner = THIS_MODULE,
  245. };
  246. static struct configfs_subsystem target_core_fabrics = {
  247. .su_group = {
  248. .cg_item = {
  249. .ci_namebuf = "target",
  250. .ci_type = &target_core_fabrics_item,
  251. },
  252. },
  253. };
  254. int target_depend_item(struct config_item *item)
  255. {
  256. return configfs_depend_item(&target_core_fabrics, item);
  257. }
  258. EXPORT_SYMBOL(target_depend_item);
  259. void target_undepend_item(struct config_item *item)
  260. {
  261. return configfs_undepend_item(&target_core_fabrics, item);
  262. }
  263. EXPORT_SYMBOL(target_undepend_item);
  264. /*##############################################################################
  265. // Start functions called by external Target Fabrics Modules
  266. //############################################################################*/
  267. static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
  268. {
  269. if (!tfo->name) {
  270. pr_err("Missing tfo->name\n");
  271. return -EINVAL;
  272. }
  273. if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
  274. pr_err("Passed name: %s exceeds TARGET_FABRIC"
  275. "_NAME_SIZE\n", tfo->name);
  276. return -EINVAL;
  277. }
  278. if (!tfo->get_fabric_name) {
  279. pr_err("Missing tfo->get_fabric_name()\n");
  280. return -EINVAL;
  281. }
  282. if (!tfo->tpg_get_wwn) {
  283. pr_err("Missing tfo->tpg_get_wwn()\n");
  284. return -EINVAL;
  285. }
  286. if (!tfo->tpg_get_tag) {
  287. pr_err("Missing tfo->tpg_get_tag()\n");
  288. return -EINVAL;
  289. }
  290. if (!tfo->tpg_check_demo_mode) {
  291. pr_err("Missing tfo->tpg_check_demo_mode()\n");
  292. return -EINVAL;
  293. }
  294. if (!tfo->tpg_check_demo_mode_cache) {
  295. pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
  296. return -EINVAL;
  297. }
  298. if (!tfo->tpg_check_demo_mode_write_protect) {
  299. pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
  300. return -EINVAL;
  301. }
  302. if (!tfo->tpg_check_prod_mode_write_protect) {
  303. pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
  304. return -EINVAL;
  305. }
  306. if (!tfo->tpg_get_inst_index) {
  307. pr_err("Missing tfo->tpg_get_inst_index()\n");
  308. return -EINVAL;
  309. }
  310. if (!tfo->release_cmd) {
  311. pr_err("Missing tfo->release_cmd()\n");
  312. return -EINVAL;
  313. }
  314. if (!tfo->shutdown_session) {
  315. pr_err("Missing tfo->shutdown_session()\n");
  316. return -EINVAL;
  317. }
  318. if (!tfo->close_session) {
  319. pr_err("Missing tfo->close_session()\n");
  320. return -EINVAL;
  321. }
  322. if (!tfo->sess_get_index) {
  323. pr_err("Missing tfo->sess_get_index()\n");
  324. return -EINVAL;
  325. }
  326. if (!tfo->write_pending) {
  327. pr_err("Missing tfo->write_pending()\n");
  328. return -EINVAL;
  329. }
  330. if (!tfo->write_pending_status) {
  331. pr_err("Missing tfo->write_pending_status()\n");
  332. return -EINVAL;
  333. }
  334. if (!tfo->set_default_node_attributes) {
  335. pr_err("Missing tfo->set_default_node_attributes()\n");
  336. return -EINVAL;
  337. }
  338. if (!tfo->get_cmd_state) {
  339. pr_err("Missing tfo->get_cmd_state()\n");
  340. return -EINVAL;
  341. }
  342. if (!tfo->queue_data_in) {
  343. pr_err("Missing tfo->queue_data_in()\n");
  344. return -EINVAL;
  345. }
  346. if (!tfo->queue_status) {
  347. pr_err("Missing tfo->queue_status()\n");
  348. return -EINVAL;
  349. }
  350. if (!tfo->queue_tm_rsp) {
  351. pr_err("Missing tfo->queue_tm_rsp()\n");
  352. return -EINVAL;
  353. }
  354. if (!tfo->aborted_task) {
  355. pr_err("Missing tfo->aborted_task()\n");
  356. return -EINVAL;
  357. }
  358. /*
  359. * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
  360. * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
  361. * target_core_fabric_configfs.c WWN+TPG group context code.
  362. */
  363. if (!tfo->fabric_make_wwn) {
  364. pr_err("Missing tfo->fabric_make_wwn()\n");
  365. return -EINVAL;
  366. }
  367. if (!tfo->fabric_drop_wwn) {
  368. pr_err("Missing tfo->fabric_drop_wwn()\n");
  369. return -EINVAL;
  370. }
  371. if (!tfo->fabric_make_tpg) {
  372. pr_err("Missing tfo->fabric_make_tpg()\n");
  373. return -EINVAL;
  374. }
  375. if (!tfo->fabric_drop_tpg) {
  376. pr_err("Missing tfo->fabric_drop_tpg()\n");
  377. return -EINVAL;
  378. }
  379. return 0;
  380. }
  381. int target_register_template(const struct target_core_fabric_ops *fo)
  382. {
  383. struct target_fabric_configfs *tf;
  384. int ret;
  385. ret = target_fabric_tf_ops_check(fo);
  386. if (ret)
  387. return ret;
  388. tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
  389. if (!tf) {
  390. pr_err("%s: could not allocate memory!\n", __func__);
  391. return -ENOMEM;
  392. }
  393. INIT_LIST_HEAD(&tf->tf_list);
  394. atomic_set(&tf->tf_access_cnt, 0);
  395. tf->tf_ops = fo;
  396. target_fabric_setup_cits(tf);
  397. mutex_lock(&g_tf_lock);
  398. list_add_tail(&tf->tf_list, &g_tf_list);
  399. mutex_unlock(&g_tf_lock);
  400. return 0;
  401. }
  402. EXPORT_SYMBOL(target_register_template);
  403. void target_unregister_template(const struct target_core_fabric_ops *fo)
  404. {
  405. struct target_fabric_configfs *t;
  406. mutex_lock(&g_tf_lock);
  407. list_for_each_entry(t, &g_tf_list, tf_list) {
  408. if (!strcmp(t->tf_ops->name, fo->name)) {
  409. BUG_ON(atomic_read(&t->tf_access_cnt));
  410. list_del(&t->tf_list);
  411. kfree(t);
  412. break;
  413. }
  414. }
  415. mutex_unlock(&g_tf_lock);
  416. }
  417. EXPORT_SYMBOL(target_unregister_template);
  418. /*##############################################################################
  419. // Stop functions called by external Target Fabrics Modules
  420. //############################################################################*/
  421. /* Start functions for struct config_item_type tb_dev_attrib_cit */
  422. #define DEF_TB_DEV_ATTRIB_SHOW(_name) \
  423. static ssize_t show_##_name(struct se_dev_attrib *da, char *page) \
  424. { \
  425. return snprintf(page, PAGE_SIZE, "%u\n", da->_name); \
  426. }
  427. DEF_TB_DEV_ATTRIB_SHOW(emulate_model_alias);
  428. DEF_TB_DEV_ATTRIB_SHOW(emulate_dpo);
  429. DEF_TB_DEV_ATTRIB_SHOW(emulate_fua_write);
  430. DEF_TB_DEV_ATTRIB_SHOW(emulate_fua_read);
  431. DEF_TB_DEV_ATTRIB_SHOW(emulate_write_cache);
  432. DEF_TB_DEV_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
  433. DEF_TB_DEV_ATTRIB_SHOW(emulate_tas);
  434. DEF_TB_DEV_ATTRIB_SHOW(emulate_tpu);
  435. DEF_TB_DEV_ATTRIB_SHOW(emulate_tpws);
  436. DEF_TB_DEV_ATTRIB_SHOW(emulate_caw);
  437. DEF_TB_DEV_ATTRIB_SHOW(emulate_3pc);
  438. DEF_TB_DEV_ATTRIB_SHOW(pi_prot_type);
  439. DEF_TB_DEV_ATTRIB_SHOW(hw_pi_prot_type);
  440. DEF_TB_DEV_ATTRIB_SHOW(pi_prot_format);
  441. DEF_TB_DEV_ATTRIB_SHOW(enforce_pr_isids);
  442. DEF_TB_DEV_ATTRIB_SHOW(is_nonrot);
  443. DEF_TB_DEV_ATTRIB_SHOW(emulate_rest_reord);
  444. DEF_TB_DEV_ATTRIB_SHOW(force_pr_aptpl);
  445. DEF_TB_DEV_ATTRIB_SHOW(hw_block_size);
  446. DEF_TB_DEV_ATTRIB_SHOW(block_size);
  447. DEF_TB_DEV_ATTRIB_SHOW(hw_max_sectors);
  448. DEF_TB_DEV_ATTRIB_SHOW(optimal_sectors);
  449. DEF_TB_DEV_ATTRIB_SHOW(hw_queue_depth);
  450. DEF_TB_DEV_ATTRIB_SHOW(queue_depth);
  451. DEF_TB_DEV_ATTRIB_SHOW(max_unmap_lba_count);
  452. DEF_TB_DEV_ATTRIB_SHOW(max_unmap_block_desc_count);
  453. DEF_TB_DEV_ATTRIB_SHOW(unmap_granularity);
  454. DEF_TB_DEV_ATTRIB_SHOW(unmap_granularity_alignment);
  455. DEF_TB_DEV_ATTRIB_SHOW(max_write_same_len);
  456. #define DEF_TB_DEV_ATTRIB_STORE_U32(_name) \
  457. static ssize_t store_##_name(struct se_dev_attrib *da, const char *page,\
  458. size_t count) \
  459. { \
  460. u32 val; \
  461. int ret; \
  462. \
  463. ret = kstrtou32(page, 0, &val); \
  464. if (ret < 0) \
  465. return ret; \
  466. da->_name = val; \
  467. return count; \
  468. }
  469. DEF_TB_DEV_ATTRIB_STORE_U32(max_unmap_lba_count);
  470. DEF_TB_DEV_ATTRIB_STORE_U32(max_unmap_block_desc_count);
  471. DEF_TB_DEV_ATTRIB_STORE_U32(unmap_granularity);
  472. DEF_TB_DEV_ATTRIB_STORE_U32(unmap_granularity_alignment);
  473. DEF_TB_DEV_ATTRIB_STORE_U32(max_write_same_len);
  474. #define DEF_TB_DEV_ATTRIB_STORE_BOOL(_name) \
  475. static ssize_t store_##_name(struct se_dev_attrib *da, const char *page,\
  476. size_t count) \
  477. { \
  478. bool flag; \
  479. int ret; \
  480. \
  481. ret = strtobool(page, &flag); \
  482. if (ret < 0) \
  483. return ret; \
  484. da->_name = flag; \
  485. return count; \
  486. }
  487. DEF_TB_DEV_ATTRIB_STORE_BOOL(emulate_fua_write);
  488. DEF_TB_DEV_ATTRIB_STORE_BOOL(emulate_caw);
  489. DEF_TB_DEV_ATTRIB_STORE_BOOL(emulate_3pc);
  490. DEF_TB_DEV_ATTRIB_STORE_BOOL(enforce_pr_isids);
  491. DEF_TB_DEV_ATTRIB_STORE_BOOL(is_nonrot);
  492. #define DEF_TB_DEV_ATTRIB_STORE_STUB(_name) \
  493. static ssize_t store_##_name(struct se_dev_attrib *da, const char *page,\
  494. size_t count) \
  495. { \
  496. printk_once(KERN_WARNING \
  497. "ignoring deprecated ##_name## attribute\n"); \
  498. return count; \
  499. }
  500. DEF_TB_DEV_ATTRIB_STORE_STUB(emulate_dpo);
  501. DEF_TB_DEV_ATTRIB_STORE_STUB(emulate_fua_read);
  502. static void dev_set_t10_wwn_model_alias(struct se_device *dev)
  503. {
  504. const char *configname;
  505. configname = config_item_name(&dev->dev_group.cg_item);
  506. if (strlen(configname) >= 16) {
  507. pr_warn("dev[%p]: Backstore name '%s' is too long for "
  508. "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
  509. configname);
  510. }
  511. snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
  512. }
  513. static ssize_t store_emulate_model_alias(struct se_dev_attrib *da,
  514. const char *page, size_t count)
  515. {
  516. struct se_device *dev = da->da_dev;
  517. bool flag;
  518. int ret;
  519. if (dev->export_count) {
  520. pr_err("dev[%p]: Unable to change model alias"
  521. " while export_count is %d\n",
  522. dev, dev->export_count);
  523. return -EINVAL;
  524. }
  525. ret = strtobool(page, &flag);
  526. if (ret < 0)
  527. return ret;
  528. if (flag) {
  529. dev_set_t10_wwn_model_alias(dev);
  530. } else {
  531. strncpy(&dev->t10_wwn.model[0],
  532. dev->transport->inquiry_prod, 16);
  533. }
  534. da->emulate_model_alias = flag;
  535. return count;
  536. }
  537. static ssize_t store_emulate_write_cache(struct se_dev_attrib *da,
  538. const char *page, size_t count)
  539. {
  540. bool flag;
  541. int ret;
  542. ret = strtobool(page, &flag);
  543. if (ret < 0)
  544. return ret;
  545. if (flag && da->da_dev->transport->get_write_cache) {
  546. pr_err("emulate_write_cache not supported for this device\n");
  547. return -EINVAL;
  548. }
  549. da->emulate_write_cache = flag;
  550. pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
  551. da->da_dev, flag);
  552. return count;
  553. }
  554. static ssize_t store_emulate_ua_intlck_ctrl(struct se_dev_attrib *da,
  555. const char *page, size_t count)
  556. {
  557. u32 val;
  558. int ret;
  559. ret = kstrtou32(page, 0, &val);
  560. if (ret < 0)
  561. return ret;
  562. if (val != 0 && val != 1 && val != 2) {
  563. pr_err("Illegal value %d\n", val);
  564. return -EINVAL;
  565. }
  566. if (da->da_dev->export_count) {
  567. pr_err("dev[%p]: Unable to change SE Device"
  568. " UA_INTRLCK_CTRL while export_count is %d\n",
  569. da->da_dev, da->da_dev->export_count);
  570. return -EINVAL;
  571. }
  572. da->emulate_ua_intlck_ctrl = val;
  573. pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
  574. da->da_dev, val);
  575. return count;
  576. }
  577. static ssize_t store_emulate_tas(struct se_dev_attrib *da,
  578. const char *page, size_t count)
  579. {
  580. bool flag;
  581. int ret;
  582. ret = strtobool(page, &flag);
  583. if (ret < 0)
  584. return ret;
  585. if (da->da_dev->export_count) {
  586. pr_err("dev[%p]: Unable to change SE Device TAS while"
  587. " export_count is %d\n",
  588. da->da_dev, da->da_dev->export_count);
  589. return -EINVAL;
  590. }
  591. da->emulate_tas = flag;
  592. pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
  593. da->da_dev, flag ? "Enabled" : "Disabled");
  594. return count;
  595. }
  596. static ssize_t store_emulate_tpu(struct se_dev_attrib *da,
  597. const char *page, size_t count)
  598. {
  599. bool flag;
  600. int ret;
  601. ret = strtobool(page, &flag);
  602. if (ret < 0)
  603. return ret;
  604. /*
  605. * We expect this value to be non-zero when generic Block Layer
  606. * Discard supported is detected iblock_create_virtdevice().
  607. */
  608. if (flag && !da->max_unmap_block_desc_count) {
  609. pr_err("Generic Block Discard not supported\n");
  610. return -ENOSYS;
  611. }
  612. da->emulate_tpu = flag;
  613. pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
  614. da->da_dev, flag);
  615. return count;
  616. }
  617. static ssize_t store_emulate_tpws(struct se_dev_attrib *da,
  618. const char *page, size_t count)
  619. {
  620. bool flag;
  621. int ret;
  622. ret = strtobool(page, &flag);
  623. if (ret < 0)
  624. return ret;
  625. /*
  626. * We expect this value to be non-zero when generic Block Layer
  627. * Discard supported is detected iblock_create_virtdevice().
  628. */
  629. if (flag && !da->max_unmap_block_desc_count) {
  630. pr_err("Generic Block Discard not supported\n");
  631. return -ENOSYS;
  632. }
  633. da->emulate_tpws = flag;
  634. pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
  635. da->da_dev, flag);
  636. return count;
  637. }
  638. static ssize_t store_pi_prot_type(struct se_dev_attrib *da,
  639. const char *page, size_t count)
  640. {
  641. int old_prot = da->pi_prot_type, ret;
  642. struct se_device *dev = da->da_dev;
  643. u32 flag;
  644. ret = kstrtou32(page, 0, &flag);
  645. if (ret < 0)
  646. return ret;
  647. if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
  648. pr_err("Illegal value %d for pi_prot_type\n", flag);
  649. return -EINVAL;
  650. }
  651. if (flag == 2) {
  652. pr_err("DIF TYPE2 protection currently not supported\n");
  653. return -ENOSYS;
  654. }
  655. if (da->hw_pi_prot_type) {
  656. pr_warn("DIF protection enabled on underlying hardware,"
  657. " ignoring\n");
  658. return count;
  659. }
  660. if (!dev->transport->init_prot || !dev->transport->free_prot) {
  661. /* 0 is only allowed value for non-supporting backends */
  662. if (flag == 0)
  663. return 0;
  664. pr_err("DIF protection not supported by backend: %s\n",
  665. dev->transport->name);
  666. return -ENOSYS;
  667. }
  668. if (!(dev->dev_flags & DF_CONFIGURED)) {
  669. pr_err("DIF protection requires device to be configured\n");
  670. return -ENODEV;
  671. }
  672. if (dev->export_count) {
  673. pr_err("dev[%p]: Unable to change SE Device PROT type while"
  674. " export_count is %d\n", dev, dev->export_count);
  675. return -EINVAL;
  676. }
  677. da->pi_prot_type = flag;
  678. if (flag && !old_prot) {
  679. ret = dev->transport->init_prot(dev);
  680. if (ret) {
  681. da->pi_prot_type = old_prot;
  682. return ret;
  683. }
  684. } else if (!flag && old_prot) {
  685. dev->transport->free_prot(dev);
  686. }
  687. pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
  688. return count;
  689. }
  690. static ssize_t store_pi_prot_format(struct se_dev_attrib *da,
  691. const char *page, size_t count)
  692. {
  693. struct se_device *dev = da->da_dev;
  694. bool flag;
  695. int ret;
  696. ret = strtobool(page, &flag);
  697. if (ret < 0)
  698. return ret;
  699. if (!flag)
  700. return count;
  701. if (!dev->transport->format_prot) {
  702. pr_err("DIF protection format not supported by backend %s\n",
  703. dev->transport->name);
  704. return -ENOSYS;
  705. }
  706. if (!(dev->dev_flags & DF_CONFIGURED)) {
  707. pr_err("DIF protection format requires device to be configured\n");
  708. return -ENODEV;
  709. }
  710. if (dev->export_count) {
  711. pr_err("dev[%p]: Unable to format SE Device PROT type while"
  712. " export_count is %d\n", dev, dev->export_count);
  713. return -EINVAL;
  714. }
  715. ret = dev->transport->format_prot(dev);
  716. if (ret)
  717. return ret;
  718. pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
  719. return count;
  720. }
  721. static ssize_t store_force_pr_aptpl(struct se_dev_attrib *da,
  722. const char *page, size_t count)
  723. {
  724. bool flag;
  725. int ret;
  726. ret = strtobool(page, &flag);
  727. if (ret < 0)
  728. return ret;
  729. if (da->da_dev->export_count) {
  730. pr_err("dev[%p]: Unable to set force_pr_aptpl while"
  731. " export_count is %d\n",
  732. da->da_dev, da->da_dev->export_count);
  733. return -EINVAL;
  734. }
  735. da->force_pr_aptpl = flag;
  736. pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
  737. return count;
  738. }
  739. static ssize_t store_emulate_rest_reord(struct se_dev_attrib *da,
  740. const char *page, size_t count)
  741. {
  742. bool flag;
  743. int ret;
  744. ret = strtobool(page, &flag);
  745. if (ret < 0)
  746. return ret;
  747. if (flag != 0) {
  748. printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
  749. " reordering not implemented\n", da->da_dev);
  750. return -ENOSYS;
  751. }
  752. da->emulate_rest_reord = flag;
  753. pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
  754. da->da_dev, flag);
  755. return count;
  756. }
  757. /*
  758. * Note, this can only be called on unexported SE Device Object.
  759. */
  760. static ssize_t store_queue_depth(struct se_dev_attrib *da,
  761. const char *page, size_t count)
  762. {
  763. struct se_device *dev = da->da_dev;
  764. u32 val;
  765. int ret;
  766. ret = kstrtou32(page, 0, &val);
  767. if (ret < 0)
  768. return ret;
  769. if (dev->export_count) {
  770. pr_err("dev[%p]: Unable to change SE Device TCQ while"
  771. " export_count is %d\n",
  772. dev, dev->export_count);
  773. return -EINVAL;
  774. }
  775. if (!val) {
  776. pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
  777. return -EINVAL;
  778. }
  779. if (val > dev->dev_attrib.queue_depth) {
  780. if (val > dev->dev_attrib.hw_queue_depth) {
  781. pr_err("dev[%p]: Passed queue_depth:"
  782. " %u exceeds TCM/SE_Device MAX"
  783. " TCQ: %u\n", dev, val,
  784. dev->dev_attrib.hw_queue_depth);
  785. return -EINVAL;
  786. }
  787. }
  788. da->queue_depth = dev->queue_depth = val;
  789. pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
  790. return count;
  791. }
  792. static ssize_t store_optimal_sectors(struct se_dev_attrib *da,
  793. const char *page, size_t count)
  794. {
  795. u32 val;
  796. int ret;
  797. ret = kstrtou32(page, 0, &val);
  798. if (ret < 0)
  799. return ret;
  800. if (da->da_dev->export_count) {
  801. pr_err("dev[%p]: Unable to change SE Device"
  802. " optimal_sectors while export_count is %d\n",
  803. da->da_dev, da->da_dev->export_count);
  804. return -EINVAL;
  805. }
  806. if (val > da->hw_max_sectors) {
  807. pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
  808. " greater than hw_max_sectors: %u\n",
  809. da->da_dev, val, da->hw_max_sectors);
  810. return -EINVAL;
  811. }
  812. da->optimal_sectors = val;
  813. pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
  814. da->da_dev, val);
  815. return count;
  816. }
  817. static ssize_t store_block_size(struct se_dev_attrib *da,
  818. const char *page, size_t count)
  819. {
  820. u32 val;
  821. int ret;
  822. ret = kstrtou32(page, 0, &val);
  823. if (ret < 0)
  824. return ret;
  825. if (da->da_dev->export_count) {
  826. pr_err("dev[%p]: Unable to change SE Device block_size"
  827. " while export_count is %d\n",
  828. da->da_dev, da->da_dev->export_count);
  829. return -EINVAL;
  830. }
  831. if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
  832. pr_err("dev[%p]: Illegal value for block_device: %u"
  833. " for SE device, must be 512, 1024, 2048 or 4096\n",
  834. da->da_dev, val);
  835. return -EINVAL;
  836. }
  837. da->block_size = val;
  838. if (da->max_bytes_per_io)
  839. da->hw_max_sectors = da->max_bytes_per_io / val;
  840. pr_debug("dev[%p]: SE Device block_size changed to %u\n",
  841. da->da_dev, val);
  842. return count;
  843. }
  844. CONFIGFS_EATTR_STRUCT(target_backend_dev_attrib, se_dev_attrib);
  845. #define TB_DEV_ATTR(_backend, _name, _mode) \
  846. static struct target_backend_dev_attrib_attribute _backend##_dev_attrib_##_name = \
  847. __CONFIGFS_EATTR(_name, _mode, \
  848. show_##_name, \
  849. store_##_name);
  850. #define TB_DEV_ATTR_RO(_backend, _name) \
  851. static struct target_backend_dev_attrib_attribute _backend##_dev_attrib_##_name = \
  852. __CONFIGFS_EATTR_RO(_name, \
  853. show_##_name);
  854. TB_DEV_ATTR(target_core, emulate_model_alias, S_IRUGO | S_IWUSR);
  855. TB_DEV_ATTR(target_core, emulate_dpo, S_IRUGO | S_IWUSR);
  856. TB_DEV_ATTR(target_core, emulate_fua_write, S_IRUGO | S_IWUSR);
  857. TB_DEV_ATTR(target_core, emulate_fua_read, S_IRUGO | S_IWUSR);
  858. TB_DEV_ATTR(target_core, emulate_write_cache, S_IRUGO | S_IWUSR);
  859. TB_DEV_ATTR(target_core, emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
  860. TB_DEV_ATTR(target_core, emulate_tas, S_IRUGO | S_IWUSR);
  861. TB_DEV_ATTR(target_core, emulate_tpu, S_IRUGO | S_IWUSR);
  862. TB_DEV_ATTR(target_core, emulate_tpws, S_IRUGO | S_IWUSR);
  863. TB_DEV_ATTR(target_core, emulate_caw, S_IRUGO | S_IWUSR);
  864. TB_DEV_ATTR(target_core, emulate_3pc, S_IRUGO | S_IWUSR);
  865. TB_DEV_ATTR(target_core, pi_prot_type, S_IRUGO | S_IWUSR);
  866. TB_DEV_ATTR_RO(target_core, hw_pi_prot_type);
  867. TB_DEV_ATTR(target_core, pi_prot_format, S_IRUGO | S_IWUSR);
  868. TB_DEV_ATTR(target_core, enforce_pr_isids, S_IRUGO | S_IWUSR);
  869. TB_DEV_ATTR(target_core, is_nonrot, S_IRUGO | S_IWUSR);
  870. TB_DEV_ATTR(target_core, emulate_rest_reord, S_IRUGO | S_IWUSR);
  871. TB_DEV_ATTR(target_core, force_pr_aptpl, S_IRUGO | S_IWUSR)
  872. TB_DEV_ATTR_RO(target_core, hw_block_size);
  873. TB_DEV_ATTR(target_core, block_size, S_IRUGO | S_IWUSR)
  874. TB_DEV_ATTR_RO(target_core, hw_max_sectors);
  875. TB_DEV_ATTR(target_core, optimal_sectors, S_IRUGO | S_IWUSR);
  876. TB_DEV_ATTR_RO(target_core, hw_queue_depth);
  877. TB_DEV_ATTR(target_core, queue_depth, S_IRUGO | S_IWUSR);
  878. TB_DEV_ATTR(target_core, max_unmap_lba_count, S_IRUGO | S_IWUSR);
  879. TB_DEV_ATTR(target_core, max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
  880. TB_DEV_ATTR(target_core, unmap_granularity, S_IRUGO | S_IWUSR);
  881. TB_DEV_ATTR(target_core, unmap_granularity_alignment, S_IRUGO | S_IWUSR);
  882. TB_DEV_ATTR(target_core, max_write_same_len, S_IRUGO | S_IWUSR);
  883. CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
  884. CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
  885. /*
  886. * dev_attrib attributes for devices using the target core SBC/SPC
  887. * interpreter. Any backend using spc_parse_cdb should be using
  888. * these.
  889. */
  890. struct configfs_attribute *sbc_attrib_attrs[] = {
  891. &target_core_dev_attrib_emulate_model_alias.attr,
  892. &target_core_dev_attrib_emulate_dpo.attr,
  893. &target_core_dev_attrib_emulate_fua_write.attr,
  894. &target_core_dev_attrib_emulate_fua_read.attr,
  895. &target_core_dev_attrib_emulate_write_cache.attr,
  896. &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
  897. &target_core_dev_attrib_emulate_tas.attr,
  898. &target_core_dev_attrib_emulate_tpu.attr,
  899. &target_core_dev_attrib_emulate_tpws.attr,
  900. &target_core_dev_attrib_emulate_caw.attr,
  901. &target_core_dev_attrib_emulate_3pc.attr,
  902. &target_core_dev_attrib_pi_prot_type.attr,
  903. &target_core_dev_attrib_hw_pi_prot_type.attr,
  904. &target_core_dev_attrib_pi_prot_format.attr,
  905. &target_core_dev_attrib_enforce_pr_isids.attr,
  906. &target_core_dev_attrib_is_nonrot.attr,
  907. &target_core_dev_attrib_emulate_rest_reord.attr,
  908. &target_core_dev_attrib_force_pr_aptpl.attr,
  909. &target_core_dev_attrib_hw_block_size.attr,
  910. &target_core_dev_attrib_block_size.attr,
  911. &target_core_dev_attrib_hw_max_sectors.attr,
  912. &target_core_dev_attrib_optimal_sectors.attr,
  913. &target_core_dev_attrib_hw_queue_depth.attr,
  914. &target_core_dev_attrib_queue_depth.attr,
  915. &target_core_dev_attrib_max_unmap_lba_count.attr,
  916. &target_core_dev_attrib_max_unmap_block_desc_count.attr,
  917. &target_core_dev_attrib_unmap_granularity.attr,
  918. &target_core_dev_attrib_unmap_granularity_alignment.attr,
  919. &target_core_dev_attrib_max_write_same_len.attr,
  920. NULL,
  921. };
  922. EXPORT_SYMBOL(sbc_attrib_attrs);
  923. TB_DEV_ATTR_RO(target_pt, hw_pi_prot_type);
  924. TB_DEV_ATTR_RO(target_pt, hw_block_size);
  925. TB_DEV_ATTR_RO(target_pt, hw_max_sectors);
  926. TB_DEV_ATTR_RO(target_pt, hw_queue_depth);
  927. /*
  928. * Minimal dev_attrib attributes for devices passing through CDBs.
  929. * In this case we only provide a few read-only attributes for
  930. * backwards compatibility.
  931. */
  932. struct configfs_attribute *passthrough_attrib_attrs[] = {
  933. &target_pt_dev_attrib_hw_pi_prot_type.attr,
  934. &target_pt_dev_attrib_hw_block_size.attr,
  935. &target_pt_dev_attrib_hw_max_sectors.attr,
  936. &target_pt_dev_attrib_hw_queue_depth.attr,
  937. NULL,
  938. };
  939. EXPORT_SYMBOL(passthrough_attrib_attrs);
  940. static struct configfs_item_operations target_core_dev_attrib_ops = {
  941. .show_attribute = target_core_dev_attrib_attr_show,
  942. .store_attribute = target_core_dev_attrib_attr_store,
  943. };
  944. TB_CIT_SETUP_DRV(dev_attrib, &target_core_dev_attrib_ops, NULL);
  945. /* End functions for struct config_item_type tb_dev_attrib_cit */
  946. /* Start functions for struct config_item_type tb_dev_wwn_cit */
  947. CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
  948. #define SE_DEV_WWN_ATTR(_name, _mode) \
  949. static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
  950. __CONFIGFS_EATTR(_name, _mode, \
  951. target_core_dev_wwn_show_attr_##_name, \
  952. target_core_dev_wwn_store_attr_##_name);
  953. #define SE_DEV_WWN_ATTR_RO(_name); \
  954. do { \
  955. static struct target_core_dev_wwn_attribute \
  956. target_core_dev_wwn_##_name = \
  957. __CONFIGFS_EATTR_RO(_name, \
  958. target_core_dev_wwn_show_attr_##_name); \
  959. } while (0);
  960. /*
  961. * VPD page 0x80 Unit serial
  962. */
  963. static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
  964. struct t10_wwn *t10_wwn,
  965. char *page)
  966. {
  967. return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
  968. &t10_wwn->unit_serial[0]);
  969. }
  970. static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
  971. struct t10_wwn *t10_wwn,
  972. const char *page,
  973. size_t count)
  974. {
  975. struct se_device *dev = t10_wwn->t10_dev;
  976. unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
  977. /*
  978. * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
  979. * from the struct scsi_device level firmware, do not allow
  980. * VPD Unit Serial to be emulated.
  981. *
  982. * Note this struct scsi_device could also be emulating VPD
  983. * information from its drivers/scsi LLD. But for now we assume
  984. * it is doing 'the right thing' wrt a world wide unique
  985. * VPD Unit Serial Number that OS dependent multipath can depend on.
  986. */
  987. if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
  988. pr_err("Underlying SCSI device firmware provided VPD"
  989. " Unit Serial, ignoring request\n");
  990. return -EOPNOTSUPP;
  991. }
  992. if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
  993. pr_err("Emulated VPD Unit Serial exceeds"
  994. " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
  995. return -EOVERFLOW;
  996. }
  997. /*
  998. * Check to see if any active $FABRIC_MOD exports exist. If they
  999. * do exist, fail here as changing this information on the fly
  1000. * (underneath the initiator side OS dependent multipath code)
  1001. * could cause negative effects.
  1002. */
  1003. if (dev->export_count) {
  1004. pr_err("Unable to set VPD Unit Serial while"
  1005. " active %d $FABRIC_MOD exports exist\n",
  1006. dev->export_count);
  1007. return -EINVAL;
  1008. }
  1009. /*
  1010. * This currently assumes ASCII encoding for emulated VPD Unit Serial.
  1011. *
  1012. * Also, strip any newline added from the userspace
  1013. * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
  1014. */
  1015. memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
  1016. snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
  1017. snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
  1018. "%s", strstrip(buf));
  1019. dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
  1020. pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
  1021. " %s\n", dev->t10_wwn.unit_serial);
  1022. return count;
  1023. }
  1024. SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
  1025. /*
  1026. * VPD page 0x83 Protocol Identifier
  1027. */
  1028. static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
  1029. struct t10_wwn *t10_wwn,
  1030. char *page)
  1031. {
  1032. struct t10_vpd *vpd;
  1033. unsigned char buf[VPD_TMP_BUF_SIZE];
  1034. ssize_t len = 0;
  1035. memset(buf, 0, VPD_TMP_BUF_SIZE);
  1036. spin_lock(&t10_wwn->t10_vpd_lock);
  1037. list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
  1038. if (!vpd->protocol_identifier_set)
  1039. continue;
  1040. transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
  1041. if (len + strlen(buf) >= PAGE_SIZE)
  1042. break;
  1043. len += sprintf(page+len, "%s", buf);
  1044. }
  1045. spin_unlock(&t10_wwn->t10_vpd_lock);
  1046. return len;
  1047. }
  1048. static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
  1049. struct t10_wwn *t10_wwn,
  1050. const char *page,
  1051. size_t count)
  1052. {
  1053. return -ENOSYS;
  1054. }
  1055. SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
  1056. /*
  1057. * Generic wrapper for dumping VPD identifiers by association.
  1058. */
  1059. #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
  1060. static ssize_t target_core_dev_wwn_show_attr_##_name( \
  1061. struct t10_wwn *t10_wwn, \
  1062. char *page) \
  1063. { \
  1064. struct t10_vpd *vpd; \
  1065. unsigned char buf[VPD_TMP_BUF_SIZE]; \
  1066. ssize_t len = 0; \
  1067. \
  1068. spin_lock(&t10_wwn->t10_vpd_lock); \
  1069. list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
  1070. if (vpd->association != _assoc) \
  1071. continue; \
  1072. \
  1073. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  1074. transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
  1075. if (len + strlen(buf) >= PAGE_SIZE) \
  1076. break; \
  1077. len += sprintf(page+len, "%s", buf); \
  1078. \
  1079. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  1080. transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
  1081. if (len + strlen(buf) >= PAGE_SIZE) \
  1082. break; \
  1083. len += sprintf(page+len, "%s", buf); \
  1084. \
  1085. memset(buf, 0, VPD_TMP_BUF_SIZE); \
  1086. transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
  1087. if (len + strlen(buf) >= PAGE_SIZE) \
  1088. break; \
  1089. len += sprintf(page+len, "%s", buf); \
  1090. } \
  1091. spin_unlock(&t10_wwn->t10_vpd_lock); \
  1092. \
  1093. return len; \
  1094. }
  1095. /*
  1096. * VPD page 0x83 Association: Logical Unit
  1097. */
  1098. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
  1099. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
  1100. struct t10_wwn *t10_wwn,
  1101. const char *page,
  1102. size_t count)
  1103. {
  1104. return -ENOSYS;
  1105. }
  1106. SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
  1107. /*
  1108. * VPD page 0x83 Association: Target Port
  1109. */
  1110. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
  1111. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
  1112. struct t10_wwn *t10_wwn,
  1113. const char *page,
  1114. size_t count)
  1115. {
  1116. return -ENOSYS;
  1117. }
  1118. SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
  1119. /*
  1120. * VPD page 0x83 Association: SCSI Target Device
  1121. */
  1122. DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
  1123. static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
  1124. struct t10_wwn *t10_wwn,
  1125. const char *page,
  1126. size_t count)
  1127. {
  1128. return -ENOSYS;
  1129. }
  1130. SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
  1131. CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
  1132. static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
  1133. &target_core_dev_wwn_vpd_unit_serial.attr,
  1134. &target_core_dev_wwn_vpd_protocol_identifier.attr,
  1135. &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
  1136. &target_core_dev_wwn_vpd_assoc_target_port.attr,
  1137. &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
  1138. NULL,
  1139. };
  1140. static struct configfs_item_operations target_core_dev_wwn_ops = {
  1141. .show_attribute = target_core_dev_wwn_attr_show,
  1142. .store_attribute = target_core_dev_wwn_attr_store,
  1143. };
  1144. TB_CIT_SETUP(dev_wwn, &target_core_dev_wwn_ops, NULL, target_core_dev_wwn_attrs);
  1145. /* End functions for struct config_item_type tb_dev_wwn_cit */
  1146. /* Start functions for struct config_item_type tb_dev_pr_cit */
  1147. CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
  1148. #define SE_DEV_PR_ATTR(_name, _mode) \
  1149. static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
  1150. __CONFIGFS_EATTR(_name, _mode, \
  1151. target_core_dev_pr_show_attr_##_name, \
  1152. target_core_dev_pr_store_attr_##_name);
  1153. #define SE_DEV_PR_ATTR_RO(_name); \
  1154. static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
  1155. __CONFIGFS_EATTR_RO(_name, \
  1156. target_core_dev_pr_show_attr_##_name);
  1157. static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
  1158. char *page)
  1159. {
  1160. struct se_node_acl *se_nacl;
  1161. struct t10_pr_registration *pr_reg;
  1162. char i_buf[PR_REG_ISID_ID_LEN];
  1163. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  1164. pr_reg = dev->dev_pr_res_holder;
  1165. if (!pr_reg)
  1166. return sprintf(page, "No SPC-3 Reservation holder\n");
  1167. se_nacl = pr_reg->pr_reg_nacl;
  1168. core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
  1169. return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
  1170. se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  1171. se_nacl->initiatorname, i_buf);
  1172. }
  1173. static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
  1174. char *page)
  1175. {
  1176. struct se_node_acl *se_nacl;
  1177. ssize_t len;
  1178. se_nacl = dev->dev_reserved_node_acl;
  1179. if (se_nacl) {
  1180. len = sprintf(page,
  1181. "SPC-2 Reservation: %s Initiator: %s\n",
  1182. se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  1183. se_nacl->initiatorname);
  1184. } else {
  1185. len = sprintf(page, "No SPC-2 Reservation holder\n");
  1186. }
  1187. return len;
  1188. }
  1189. static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
  1190. char *page)
  1191. {
  1192. int ret;
  1193. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1194. return sprintf(page, "Passthrough\n");
  1195. spin_lock(&dev->dev_reservation_lock);
  1196. if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  1197. ret = target_core_dev_pr_show_spc2_res(dev, page);
  1198. else
  1199. ret = target_core_dev_pr_show_spc3_res(dev, page);
  1200. spin_unlock(&dev->dev_reservation_lock);
  1201. return ret;
  1202. }
  1203. SE_DEV_PR_ATTR_RO(res_holder);
  1204. static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
  1205. struct se_device *dev, char *page)
  1206. {
  1207. ssize_t len = 0;
  1208. spin_lock(&dev->dev_reservation_lock);
  1209. if (!dev->dev_pr_res_holder) {
  1210. len = sprintf(page, "No SPC-3 Reservation holder\n");
  1211. } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
  1212. len = sprintf(page, "SPC-3 Reservation: All Target"
  1213. " Ports registration\n");
  1214. } else {
  1215. len = sprintf(page, "SPC-3 Reservation: Single"
  1216. " Target Port registration\n");
  1217. }
  1218. spin_unlock(&dev->dev_reservation_lock);
  1219. return len;
  1220. }
  1221. SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
  1222. static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
  1223. struct se_device *dev, char *page)
  1224. {
  1225. return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
  1226. }
  1227. SE_DEV_PR_ATTR_RO(res_pr_generation);
  1228. /*
  1229. * res_pr_holder_tg_port
  1230. */
  1231. static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
  1232. struct se_device *dev, char *page)
  1233. {
  1234. struct se_node_acl *se_nacl;
  1235. struct se_portal_group *se_tpg;
  1236. struct t10_pr_registration *pr_reg;
  1237. const struct target_core_fabric_ops *tfo;
  1238. ssize_t len = 0;
  1239. spin_lock(&dev->dev_reservation_lock);
  1240. pr_reg = dev->dev_pr_res_holder;
  1241. if (!pr_reg) {
  1242. len = sprintf(page, "No SPC-3 Reservation holder\n");
  1243. goto out_unlock;
  1244. }
  1245. se_nacl = pr_reg->pr_reg_nacl;
  1246. se_tpg = se_nacl->se_tpg;
  1247. tfo = se_tpg->se_tpg_tfo;
  1248. len += sprintf(page+len, "SPC-3 Reservation: %s"
  1249. " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
  1250. tfo->tpg_get_wwn(se_tpg));
  1251. len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
  1252. " Identifier Tag: %hu %s Portal Group Tag: %hu"
  1253. " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
  1254. tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
  1255. tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
  1256. out_unlock:
  1257. spin_unlock(&dev->dev_reservation_lock);
  1258. return len;
  1259. }
  1260. SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
  1261. static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
  1262. struct se_device *dev, char *page)
  1263. {
  1264. const struct target_core_fabric_ops *tfo;
  1265. struct t10_pr_registration *pr_reg;
  1266. unsigned char buf[384];
  1267. char i_buf[PR_REG_ISID_ID_LEN];
  1268. ssize_t len = 0;
  1269. int reg_count = 0;
  1270. len += sprintf(page+len, "SPC-3 PR Registrations:\n");
  1271. spin_lock(&dev->t10_pr.registration_lock);
  1272. list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
  1273. pr_reg_list) {
  1274. memset(buf, 0, 384);
  1275. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  1276. tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
  1277. core_pr_dump_initiator_port(pr_reg, i_buf,
  1278. PR_REG_ISID_ID_LEN);
  1279. sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
  1280. tfo->get_fabric_name(),
  1281. pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
  1282. pr_reg->pr_res_generation);
  1283. if (len + strlen(buf) >= PAGE_SIZE)
  1284. break;
  1285. len += sprintf(page+len, "%s", buf);
  1286. reg_count++;
  1287. }
  1288. spin_unlock(&dev->t10_pr.registration_lock);
  1289. if (!reg_count)
  1290. len += sprintf(page+len, "None\n");
  1291. return len;
  1292. }
  1293. SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
  1294. static ssize_t target_core_dev_pr_show_attr_res_pr_type(
  1295. struct se_device *dev, char *page)
  1296. {
  1297. struct t10_pr_registration *pr_reg;
  1298. ssize_t len = 0;
  1299. spin_lock(&dev->dev_reservation_lock);
  1300. pr_reg = dev->dev_pr_res_holder;
  1301. if (pr_reg) {
  1302. len = sprintf(page, "SPC-3 Reservation Type: %s\n",
  1303. core_scsi3_pr_dump_type(pr_reg->pr_res_type));
  1304. } else {
  1305. len = sprintf(page, "No SPC-3 Reservation holder\n");
  1306. }
  1307. spin_unlock(&dev->dev_reservation_lock);
  1308. return len;
  1309. }
  1310. SE_DEV_PR_ATTR_RO(res_pr_type);
  1311. static ssize_t target_core_dev_pr_show_attr_res_type(
  1312. struct se_device *dev, char *page)
  1313. {
  1314. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1315. return sprintf(page, "SPC_PASSTHROUGH\n");
  1316. else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  1317. return sprintf(page, "SPC2_RESERVATIONS\n");
  1318. else
  1319. return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
  1320. }
  1321. SE_DEV_PR_ATTR_RO(res_type);
  1322. static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
  1323. struct se_device *dev, char *page)
  1324. {
  1325. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1326. return 0;
  1327. return sprintf(page, "APTPL Bit Status: %s\n",
  1328. (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
  1329. }
  1330. SE_DEV_PR_ATTR_RO(res_aptpl_active);
  1331. /*
  1332. * res_aptpl_metadata
  1333. */
  1334. static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
  1335. struct se_device *dev, char *page)
  1336. {
  1337. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1338. return 0;
  1339. return sprintf(page, "Ready to process PR APTPL metadata..\n");
  1340. }
  1341. enum {
  1342. Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
  1343. Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
  1344. Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
  1345. Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
  1346. };
  1347. static match_table_t tokens = {
  1348. {Opt_initiator_fabric, "initiator_fabric=%s"},
  1349. {Opt_initiator_node, "initiator_node=%s"},
  1350. {Opt_initiator_sid, "initiator_sid=%s"},
  1351. {Opt_sa_res_key, "sa_res_key=%s"},
  1352. {Opt_res_holder, "res_holder=%d"},
  1353. {Opt_res_type, "res_type=%d"},
  1354. {Opt_res_scope, "res_scope=%d"},
  1355. {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
  1356. {Opt_mapped_lun, "mapped_lun=%lld"},
  1357. {Opt_target_fabric, "target_fabric=%s"},
  1358. {Opt_target_node, "target_node=%s"},
  1359. {Opt_tpgt, "tpgt=%d"},
  1360. {Opt_port_rtpi, "port_rtpi=%d"},
  1361. {Opt_target_lun, "target_lun=%lld"},
  1362. {Opt_err, NULL}
  1363. };
  1364. static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
  1365. struct se_device *dev,
  1366. const char *page,
  1367. size_t count)
  1368. {
  1369. unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
  1370. unsigned char *t_fabric = NULL, *t_port = NULL;
  1371. char *orig, *ptr, *opts;
  1372. substring_t args[MAX_OPT_ARGS];
  1373. unsigned long long tmp_ll;
  1374. u64 sa_res_key = 0;
  1375. u64 mapped_lun = 0, target_lun = 0;
  1376. int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
  1377. u16 tpgt = 0;
  1378. u8 type = 0;
  1379. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  1380. return 0;
  1381. if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  1382. return 0;
  1383. if (dev->export_count) {
  1384. pr_debug("Unable to process APTPL metadata while"
  1385. " active fabric exports exist\n");
  1386. return -EINVAL;
  1387. }
  1388. opts = kstrdup(page, GFP_KERNEL);
  1389. if (!opts)
  1390. return -ENOMEM;
  1391. orig = opts;
  1392. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  1393. if (!*ptr)
  1394. continue;
  1395. token = match_token(ptr, tokens, args);
  1396. switch (token) {
  1397. case Opt_initiator_fabric:
  1398. i_fabric = match_strdup(args);
  1399. if (!i_fabric) {
  1400. ret = -ENOMEM;
  1401. goto out;
  1402. }
  1403. break;
  1404. case Opt_initiator_node:
  1405. i_port = match_strdup(args);
  1406. if (!i_port) {
  1407. ret = -ENOMEM;
  1408. goto out;
  1409. }
  1410. if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
  1411. pr_err("APTPL metadata initiator_node="
  1412. " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
  1413. PR_APTPL_MAX_IPORT_LEN);
  1414. ret = -EINVAL;
  1415. break;
  1416. }
  1417. break;
  1418. case Opt_initiator_sid:
  1419. isid = match_strdup(args);
  1420. if (!isid) {
  1421. ret = -ENOMEM;
  1422. goto out;
  1423. }
  1424. if (strlen(isid) >= PR_REG_ISID_LEN) {
  1425. pr_err("APTPL metadata initiator_isid"
  1426. "= exceeds PR_REG_ISID_LEN: %d\n",
  1427. PR_REG_ISID_LEN);
  1428. ret = -EINVAL;
  1429. break;
  1430. }
  1431. break;
  1432. case Opt_sa_res_key:
  1433. ret = kstrtoull(args->from, 0, &tmp_ll);
  1434. if (ret < 0) {
  1435. pr_err("kstrtoull() failed for sa_res_key=\n");
  1436. goto out;
  1437. }
  1438. sa_res_key = (u64)tmp_ll;
  1439. break;
  1440. /*
  1441. * PR APTPL Metadata for Reservation
  1442. */
  1443. case Opt_res_holder:
  1444. match_int(args, &arg);
  1445. res_holder = arg;
  1446. break;
  1447. case Opt_res_type:
  1448. match_int(args, &arg);
  1449. type = (u8)arg;
  1450. break;
  1451. case Opt_res_scope:
  1452. match_int(args, &arg);
  1453. break;
  1454. case Opt_res_all_tg_pt:
  1455. match_int(args, &arg);
  1456. all_tg_pt = (int)arg;
  1457. break;
  1458. case Opt_mapped_lun:
  1459. match_int(args, &arg);
  1460. mapped_lun = (u64)arg;
  1461. break;
  1462. /*
  1463. * PR APTPL Metadata for Target Port
  1464. */
  1465. case Opt_target_fabric:
  1466. t_fabric = match_strdup(args);
  1467. if (!t_fabric) {
  1468. ret = -ENOMEM;
  1469. goto out;
  1470. }
  1471. break;
  1472. case Opt_target_node:
  1473. t_port = match_strdup(args);
  1474. if (!t_port) {
  1475. ret = -ENOMEM;
  1476. goto out;
  1477. }
  1478. if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
  1479. pr_err("APTPL metadata target_node="
  1480. " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
  1481. PR_APTPL_MAX_TPORT_LEN);
  1482. ret = -EINVAL;
  1483. break;
  1484. }
  1485. break;
  1486. case Opt_tpgt:
  1487. match_int(args, &arg);
  1488. tpgt = (u16)arg;
  1489. break;
  1490. case Opt_port_rtpi:
  1491. match_int(args, &arg);
  1492. break;
  1493. case Opt_target_lun:
  1494. match_int(args, &arg);
  1495. target_lun = (u64)arg;
  1496. break;
  1497. default:
  1498. break;
  1499. }
  1500. }
  1501. if (!i_port || !t_port || !sa_res_key) {
  1502. pr_err("Illegal parameters for APTPL registration\n");
  1503. ret = -EINVAL;
  1504. goto out;
  1505. }
  1506. if (res_holder && !(type)) {
  1507. pr_err("Illegal PR type: 0x%02x for reservation"
  1508. " holder\n", type);
  1509. ret = -EINVAL;
  1510. goto out;
  1511. }
  1512. ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
  1513. i_port, isid, mapped_lun, t_port, tpgt, target_lun,
  1514. res_holder, all_tg_pt, type);
  1515. out:
  1516. kfree(i_fabric);
  1517. kfree(i_port);
  1518. kfree(isid);
  1519. kfree(t_fabric);
  1520. kfree(t_port);
  1521. kfree(orig);
  1522. return (ret == 0) ? count : ret;
  1523. }
  1524. SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
  1525. CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
  1526. static struct configfs_attribute *target_core_dev_pr_attrs[] = {
  1527. &target_core_dev_pr_res_holder.attr,
  1528. &target_core_dev_pr_res_pr_all_tgt_pts.attr,
  1529. &target_core_dev_pr_res_pr_generation.attr,
  1530. &target_core_dev_pr_res_pr_holder_tg_port.attr,
  1531. &target_core_dev_pr_res_pr_registered_i_pts.attr,
  1532. &target_core_dev_pr_res_pr_type.attr,
  1533. &target_core_dev_pr_res_type.attr,
  1534. &target_core_dev_pr_res_aptpl_active.attr,
  1535. &target_core_dev_pr_res_aptpl_metadata.attr,
  1536. NULL,
  1537. };
  1538. static struct configfs_item_operations target_core_dev_pr_ops = {
  1539. .show_attribute = target_core_dev_pr_attr_show,
  1540. .store_attribute = target_core_dev_pr_attr_store,
  1541. };
  1542. TB_CIT_SETUP(dev_pr, &target_core_dev_pr_ops, NULL, target_core_dev_pr_attrs);
  1543. /* End functions for struct config_item_type tb_dev_pr_cit */
  1544. /* Start functions for struct config_item_type tb_dev_cit */
  1545. static ssize_t target_core_show_dev_info(void *p, char *page)
  1546. {
  1547. struct se_device *dev = p;
  1548. int bl = 0;
  1549. ssize_t read_bytes = 0;
  1550. transport_dump_dev_state(dev, page, &bl);
  1551. read_bytes += bl;
  1552. read_bytes += dev->transport->show_configfs_dev_params(dev,
  1553. page+read_bytes);
  1554. return read_bytes;
  1555. }
  1556. static struct target_core_configfs_attribute target_core_attr_dev_info = {
  1557. .attr = { .ca_owner = THIS_MODULE,
  1558. .ca_name = "info",
  1559. .ca_mode = S_IRUGO },
  1560. .show = target_core_show_dev_info,
  1561. .store = NULL,
  1562. };
  1563. static ssize_t target_core_store_dev_control(
  1564. void *p,
  1565. const char *page,
  1566. size_t count)
  1567. {
  1568. struct se_device *dev = p;
  1569. return dev->transport->set_configfs_dev_params(dev, page, count);
  1570. }
  1571. static struct target_core_configfs_attribute target_core_attr_dev_control = {
  1572. .attr = { .ca_owner = THIS_MODULE,
  1573. .ca_name = "control",
  1574. .ca_mode = S_IWUSR },
  1575. .show = NULL,
  1576. .store = target_core_store_dev_control,
  1577. };
  1578. static ssize_t target_core_show_dev_alias(void *p, char *page)
  1579. {
  1580. struct se_device *dev = p;
  1581. if (!(dev->dev_flags & DF_USING_ALIAS))
  1582. return 0;
  1583. return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
  1584. }
  1585. static ssize_t target_core_store_dev_alias(
  1586. void *p,
  1587. const char *page,
  1588. size_t count)
  1589. {
  1590. struct se_device *dev = p;
  1591. struct se_hba *hba = dev->se_hba;
  1592. ssize_t read_bytes;
  1593. if (count > (SE_DEV_ALIAS_LEN-1)) {
  1594. pr_err("alias count: %d exceeds"
  1595. " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
  1596. SE_DEV_ALIAS_LEN-1);
  1597. return -EINVAL;
  1598. }
  1599. read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
  1600. if (!read_bytes)
  1601. return -EINVAL;
  1602. if (dev->dev_alias[read_bytes - 1] == '\n')
  1603. dev->dev_alias[read_bytes - 1] = '\0';
  1604. dev->dev_flags |= DF_USING_ALIAS;
  1605. pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
  1606. config_item_name(&hba->hba_group.cg_item),
  1607. config_item_name(&dev->dev_group.cg_item),
  1608. dev->dev_alias);
  1609. return read_bytes;
  1610. }
  1611. static struct target_core_configfs_attribute target_core_attr_dev_alias = {
  1612. .attr = { .ca_owner = THIS_MODULE,
  1613. .ca_name = "alias",
  1614. .ca_mode = S_IRUGO | S_IWUSR },
  1615. .show = target_core_show_dev_alias,
  1616. .store = target_core_store_dev_alias,
  1617. };
  1618. static ssize_t target_core_show_dev_udev_path(void *p, char *page)
  1619. {
  1620. struct se_device *dev = p;
  1621. if (!(dev->dev_flags & DF_USING_UDEV_PATH))
  1622. return 0;
  1623. return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
  1624. }
  1625. static ssize_t target_core_store_dev_udev_path(
  1626. void *p,
  1627. const char *page,
  1628. size_t count)
  1629. {
  1630. struct se_device *dev = p;
  1631. struct se_hba *hba = dev->se_hba;
  1632. ssize_t read_bytes;
  1633. if (count > (SE_UDEV_PATH_LEN-1)) {
  1634. pr_err("udev_path count: %d exceeds"
  1635. " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
  1636. SE_UDEV_PATH_LEN-1);
  1637. return -EINVAL;
  1638. }
  1639. read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
  1640. "%s", page);
  1641. if (!read_bytes)
  1642. return -EINVAL;
  1643. if (dev->udev_path[read_bytes - 1] == '\n')
  1644. dev->udev_path[read_bytes - 1] = '\0';
  1645. dev->dev_flags |= DF_USING_UDEV_PATH;
  1646. pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
  1647. config_item_name(&hba->hba_group.cg_item),
  1648. config_item_name(&dev->dev_group.cg_item),
  1649. dev->udev_path);
  1650. return read_bytes;
  1651. }
  1652. static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
  1653. .attr = { .ca_owner = THIS_MODULE,
  1654. .ca_name = "udev_path",
  1655. .ca_mode = S_IRUGO | S_IWUSR },
  1656. .show = target_core_show_dev_udev_path,
  1657. .store = target_core_store_dev_udev_path,
  1658. };
  1659. static ssize_t target_core_show_dev_enable(void *p, char *page)
  1660. {
  1661. struct se_device *dev = p;
  1662. return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
  1663. }
  1664. static ssize_t target_core_store_dev_enable(
  1665. void *p,
  1666. const char *page,
  1667. size_t count)
  1668. {
  1669. struct se_device *dev = p;
  1670. char *ptr;
  1671. int ret;
  1672. ptr = strstr(page, "1");
  1673. if (!ptr) {
  1674. pr_err("For dev_enable ops, only valid value"
  1675. " is \"1\"\n");
  1676. return -EINVAL;
  1677. }
  1678. ret = target_configure_device(dev);
  1679. if (ret)
  1680. return ret;
  1681. return count;
  1682. }
  1683. static struct target_core_configfs_attribute target_core_attr_dev_enable = {
  1684. .attr = { .ca_owner = THIS_MODULE,
  1685. .ca_name = "enable",
  1686. .ca_mode = S_IRUGO | S_IWUSR },
  1687. .show = target_core_show_dev_enable,
  1688. .store = target_core_store_dev_enable,
  1689. };
  1690. static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
  1691. {
  1692. struct se_device *dev = p;
  1693. struct config_item *lu_ci;
  1694. struct t10_alua_lu_gp *lu_gp;
  1695. struct t10_alua_lu_gp_member *lu_gp_mem;
  1696. ssize_t len = 0;
  1697. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1698. if (!lu_gp_mem)
  1699. return 0;
  1700. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1701. lu_gp = lu_gp_mem->lu_gp;
  1702. if (lu_gp) {
  1703. lu_ci = &lu_gp->lu_gp_group.cg_item;
  1704. len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
  1705. config_item_name(lu_ci), lu_gp->lu_gp_id);
  1706. }
  1707. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1708. return len;
  1709. }
  1710. static ssize_t target_core_store_alua_lu_gp(
  1711. void *p,
  1712. const char *page,
  1713. size_t count)
  1714. {
  1715. struct se_device *dev = p;
  1716. struct se_hba *hba = dev->se_hba;
  1717. struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
  1718. struct t10_alua_lu_gp_member *lu_gp_mem;
  1719. unsigned char buf[LU_GROUP_NAME_BUF];
  1720. int move = 0;
  1721. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1722. if (!lu_gp_mem)
  1723. return 0;
  1724. if (count > LU_GROUP_NAME_BUF) {
  1725. pr_err("ALUA LU Group Alias too large!\n");
  1726. return -EINVAL;
  1727. }
  1728. memset(buf, 0, LU_GROUP_NAME_BUF);
  1729. memcpy(buf, page, count);
  1730. /*
  1731. * Any ALUA logical unit alias besides "NULL" means we will be
  1732. * making a new group association.
  1733. */
  1734. if (strcmp(strstrip(buf), "NULL")) {
  1735. /*
  1736. * core_alua_get_lu_gp_by_name() will increment reference to
  1737. * struct t10_alua_lu_gp. This reference is released with
  1738. * core_alua_get_lu_gp_by_name below().
  1739. */
  1740. lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
  1741. if (!lu_gp_new)
  1742. return -ENODEV;
  1743. }
  1744. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1745. lu_gp = lu_gp_mem->lu_gp;
  1746. if (lu_gp) {
  1747. /*
  1748. * Clearing an existing lu_gp association, and replacing
  1749. * with NULL
  1750. */
  1751. if (!lu_gp_new) {
  1752. pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
  1753. " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
  1754. " %hu\n",
  1755. config_item_name(&hba->hba_group.cg_item),
  1756. config_item_name(&dev->dev_group.cg_item),
  1757. config_item_name(&lu_gp->lu_gp_group.cg_item),
  1758. lu_gp->lu_gp_id);
  1759. __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
  1760. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1761. return count;
  1762. }
  1763. /*
  1764. * Removing existing association of lu_gp_mem with lu_gp
  1765. */
  1766. __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
  1767. move = 1;
  1768. }
  1769. /*
  1770. * Associate lu_gp_mem with lu_gp_new.
  1771. */
  1772. __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
  1773. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1774. pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
  1775. " core/alua/lu_gps/%s, ID: %hu\n",
  1776. (move) ? "Moving" : "Adding",
  1777. config_item_name(&hba->hba_group.cg_item),
  1778. config_item_name(&dev->dev_group.cg_item),
  1779. config_item_name(&lu_gp_new->lu_gp_group.cg_item),
  1780. lu_gp_new->lu_gp_id);
  1781. core_alua_put_lu_gp_from_name(lu_gp_new);
  1782. return count;
  1783. }
  1784. static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
  1785. .attr = { .ca_owner = THIS_MODULE,
  1786. .ca_name = "alua_lu_gp",
  1787. .ca_mode = S_IRUGO | S_IWUSR },
  1788. .show = target_core_show_alua_lu_gp,
  1789. .store = target_core_store_alua_lu_gp,
  1790. };
  1791. static ssize_t target_core_show_dev_lba_map(void *p, char *page)
  1792. {
  1793. struct se_device *dev = p;
  1794. struct t10_alua_lba_map *map;
  1795. struct t10_alua_lba_map_member *mem;
  1796. char *b = page;
  1797. int bl = 0;
  1798. char state;
  1799. spin_lock(&dev->t10_alua.lba_map_lock);
  1800. if (!list_empty(&dev->t10_alua.lba_map_list))
  1801. bl += sprintf(b + bl, "%u %u\n",
  1802. dev->t10_alua.lba_map_segment_size,
  1803. dev->t10_alua.lba_map_segment_multiplier);
  1804. list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
  1805. bl += sprintf(b + bl, "%llu %llu",
  1806. map->lba_map_first_lba, map->lba_map_last_lba);
  1807. list_for_each_entry(mem, &map->lba_map_mem_list,
  1808. lba_map_mem_list) {
  1809. switch (mem->lba_map_mem_alua_state) {
  1810. case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
  1811. state = 'O';
  1812. break;
  1813. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  1814. state = 'A';
  1815. break;
  1816. case ALUA_ACCESS_STATE_STANDBY:
  1817. state = 'S';
  1818. break;
  1819. case ALUA_ACCESS_STATE_UNAVAILABLE:
  1820. state = 'U';
  1821. break;
  1822. default:
  1823. state = '.';
  1824. break;
  1825. }
  1826. bl += sprintf(b + bl, " %d:%c",
  1827. mem->lba_map_mem_alua_pg_id, state);
  1828. }
  1829. bl += sprintf(b + bl, "\n");
  1830. }
  1831. spin_unlock(&dev->t10_alua.lba_map_lock);
  1832. return bl;
  1833. }
  1834. static ssize_t target_core_store_dev_lba_map(
  1835. void *p,
  1836. const char *page,
  1837. size_t count)
  1838. {
  1839. struct se_device *dev = p;
  1840. struct t10_alua_lba_map *lba_map = NULL;
  1841. struct list_head lba_list;
  1842. char *map_entries, *ptr;
  1843. char state;
  1844. int pg_num = -1, pg;
  1845. int ret = 0, num = 0, pg_id, alua_state;
  1846. unsigned long start_lba = -1, end_lba = -1;
  1847. unsigned long segment_size = -1, segment_mult = -1;
  1848. map_entries = kstrdup(page, GFP_KERNEL);
  1849. if (!map_entries)
  1850. return -ENOMEM;
  1851. INIT_LIST_HEAD(&lba_list);
  1852. while ((ptr = strsep(&map_entries, "\n")) != NULL) {
  1853. if (!*ptr)
  1854. continue;
  1855. if (num == 0) {
  1856. if (sscanf(ptr, "%lu %lu\n",
  1857. &segment_size, &segment_mult) != 2) {
  1858. pr_err("Invalid line %d\n", num);
  1859. ret = -EINVAL;
  1860. break;
  1861. }
  1862. num++;
  1863. continue;
  1864. }
  1865. if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
  1866. pr_err("Invalid line %d\n", num);
  1867. ret = -EINVAL;
  1868. break;
  1869. }
  1870. ptr = strchr(ptr, ' ');
  1871. if (!ptr) {
  1872. pr_err("Invalid line %d, missing end lba\n", num);
  1873. ret = -EINVAL;
  1874. break;
  1875. }
  1876. ptr++;
  1877. ptr = strchr(ptr, ' ');
  1878. if (!ptr) {
  1879. pr_err("Invalid line %d, missing state definitions\n",
  1880. num);
  1881. ret = -EINVAL;
  1882. break;
  1883. }
  1884. ptr++;
  1885. lba_map = core_alua_allocate_lba_map(&lba_list,
  1886. start_lba, end_lba);
  1887. if (IS_ERR(lba_map)) {
  1888. ret = PTR_ERR(lba_map);
  1889. break;
  1890. }
  1891. pg = 0;
  1892. while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
  1893. switch (state) {
  1894. case 'O':
  1895. alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
  1896. break;
  1897. case 'A':
  1898. alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
  1899. break;
  1900. case 'S':
  1901. alua_state = ALUA_ACCESS_STATE_STANDBY;
  1902. break;
  1903. case 'U':
  1904. alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
  1905. break;
  1906. default:
  1907. pr_err("Invalid ALUA state '%c'\n", state);
  1908. ret = -EINVAL;
  1909. goto out;
  1910. }
  1911. ret = core_alua_allocate_lba_map_mem(lba_map,
  1912. pg_id, alua_state);
  1913. if (ret) {
  1914. pr_err("Invalid target descriptor %d:%c "
  1915. "at line %d\n",
  1916. pg_id, state, num);
  1917. break;
  1918. }
  1919. pg++;
  1920. ptr = strchr(ptr, ' ');
  1921. if (ptr)
  1922. ptr++;
  1923. else
  1924. break;
  1925. }
  1926. if (pg_num == -1)
  1927. pg_num = pg;
  1928. else if (pg != pg_num) {
  1929. pr_err("Only %d from %d port groups definitions "
  1930. "at line %d\n", pg, pg_num, num);
  1931. ret = -EINVAL;
  1932. break;
  1933. }
  1934. num++;
  1935. }
  1936. out:
  1937. if (ret) {
  1938. core_alua_free_lba_map(&lba_list);
  1939. count = ret;
  1940. } else
  1941. core_alua_set_lba_map(dev, &lba_list,
  1942. segment_size, segment_mult);
  1943. kfree(map_entries);
  1944. return count;
  1945. }
  1946. static struct target_core_configfs_attribute target_core_attr_dev_lba_map = {
  1947. .attr = { .ca_owner = THIS_MODULE,
  1948. .ca_name = "lba_map",
  1949. .ca_mode = S_IRUGO | S_IWUSR },
  1950. .show = target_core_show_dev_lba_map,
  1951. .store = target_core_store_dev_lba_map,
  1952. };
  1953. static struct configfs_attribute *target_core_dev_attrs[] = {
  1954. &target_core_attr_dev_info.attr,
  1955. &target_core_attr_dev_control.attr,
  1956. &target_core_attr_dev_alias.attr,
  1957. &target_core_attr_dev_udev_path.attr,
  1958. &target_core_attr_dev_enable.attr,
  1959. &target_core_attr_dev_alua_lu_gp.attr,
  1960. &target_core_attr_dev_lba_map.attr,
  1961. NULL,
  1962. };
  1963. static void target_core_dev_release(struct config_item *item)
  1964. {
  1965. struct config_group *dev_cg = to_config_group(item);
  1966. struct se_device *dev =
  1967. container_of(dev_cg, struct se_device, dev_group);
  1968. kfree(dev_cg->default_groups);
  1969. target_free_device(dev);
  1970. }
  1971. static ssize_t target_core_dev_show(struct config_item *item,
  1972. struct configfs_attribute *attr,
  1973. char *page)
  1974. {
  1975. struct config_group *dev_cg = to_config_group(item);
  1976. struct se_device *dev =
  1977. container_of(dev_cg, struct se_device, dev_group);
  1978. struct target_core_configfs_attribute *tc_attr = container_of(
  1979. attr, struct target_core_configfs_attribute, attr);
  1980. if (!tc_attr->show)
  1981. return -EINVAL;
  1982. return tc_attr->show(dev, page);
  1983. }
  1984. static ssize_t target_core_dev_store(struct config_item *item,
  1985. struct configfs_attribute *attr,
  1986. const char *page, size_t count)
  1987. {
  1988. struct config_group *dev_cg = to_config_group(item);
  1989. struct se_device *dev =
  1990. container_of(dev_cg, struct se_device, dev_group);
  1991. struct target_core_configfs_attribute *tc_attr = container_of(
  1992. attr, struct target_core_configfs_attribute, attr);
  1993. if (!tc_attr->store)
  1994. return -EINVAL;
  1995. return tc_attr->store(dev, page, count);
  1996. }
  1997. static struct configfs_item_operations target_core_dev_item_ops = {
  1998. .release = target_core_dev_release,
  1999. .show_attribute = target_core_dev_show,
  2000. .store_attribute = target_core_dev_store,
  2001. };
  2002. TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
  2003. /* End functions for struct config_item_type tb_dev_cit */
  2004. /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
  2005. CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
  2006. #define SE_DEV_ALUA_LU_ATTR(_name, _mode) \
  2007. static struct target_core_alua_lu_gp_attribute \
  2008. target_core_alua_lu_gp_##_name = \
  2009. __CONFIGFS_EATTR(_name, _mode, \
  2010. target_core_alua_lu_gp_show_attr_##_name, \
  2011. target_core_alua_lu_gp_store_attr_##_name);
  2012. #define SE_DEV_ALUA_LU_ATTR_RO(_name) \
  2013. static struct target_core_alua_lu_gp_attribute \
  2014. target_core_alua_lu_gp_##_name = \
  2015. __CONFIGFS_EATTR_RO(_name, \
  2016. target_core_alua_lu_gp_show_attr_##_name);
  2017. /*
  2018. * lu_gp_id
  2019. */
  2020. static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
  2021. struct t10_alua_lu_gp *lu_gp,
  2022. char *page)
  2023. {
  2024. if (!lu_gp->lu_gp_valid_id)
  2025. return 0;
  2026. return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
  2027. }
  2028. static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
  2029. struct t10_alua_lu_gp *lu_gp,
  2030. const char *page,
  2031. size_t count)
  2032. {
  2033. struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
  2034. unsigned long lu_gp_id;
  2035. int ret;
  2036. ret = kstrtoul(page, 0, &lu_gp_id);
  2037. if (ret < 0) {
  2038. pr_err("kstrtoul() returned %d for"
  2039. " lu_gp_id\n", ret);
  2040. return ret;
  2041. }
  2042. if (lu_gp_id > 0x0000ffff) {
  2043. pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
  2044. " 0x0000ffff\n", lu_gp_id);
  2045. return -EINVAL;
  2046. }
  2047. ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
  2048. if (ret < 0)
  2049. return -EINVAL;
  2050. pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
  2051. " Group: core/alua/lu_gps/%s to ID: %hu\n",
  2052. config_item_name(&alua_lu_gp_cg->cg_item),
  2053. lu_gp->lu_gp_id);
  2054. return count;
  2055. }
  2056. SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
  2057. /*
  2058. * members
  2059. */
  2060. static ssize_t target_core_alua_lu_gp_show_attr_members(
  2061. struct t10_alua_lu_gp *lu_gp,
  2062. char *page)
  2063. {
  2064. struct se_device *dev;
  2065. struct se_hba *hba;
  2066. struct t10_alua_lu_gp_member *lu_gp_mem;
  2067. ssize_t len = 0, cur_len;
  2068. unsigned char buf[LU_GROUP_NAME_BUF];
  2069. memset(buf, 0, LU_GROUP_NAME_BUF);
  2070. spin_lock(&lu_gp->lu_gp_lock);
  2071. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  2072. dev = lu_gp_mem->lu_gp_mem_dev;
  2073. hba = dev->se_hba;
  2074. cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
  2075. config_item_name(&hba->hba_group.cg_item),
  2076. config_item_name(&dev->dev_group.cg_item));
  2077. cur_len++; /* Extra byte for NULL terminator */
  2078. if ((cur_len + len) > PAGE_SIZE) {
  2079. pr_warn("Ran out of lu_gp_show_attr"
  2080. "_members buffer\n");
  2081. break;
  2082. }
  2083. memcpy(page+len, buf, cur_len);
  2084. len += cur_len;
  2085. }
  2086. spin_unlock(&lu_gp->lu_gp_lock);
  2087. return len;
  2088. }
  2089. SE_DEV_ALUA_LU_ATTR_RO(members);
  2090. CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
  2091. static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
  2092. &target_core_alua_lu_gp_lu_gp_id.attr,
  2093. &target_core_alua_lu_gp_members.attr,
  2094. NULL,
  2095. };
  2096. static void target_core_alua_lu_gp_release(struct config_item *item)
  2097. {
  2098. struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
  2099. struct t10_alua_lu_gp, lu_gp_group);
  2100. core_alua_free_lu_gp(lu_gp);
  2101. }
  2102. static struct configfs_item_operations target_core_alua_lu_gp_ops = {
  2103. .release = target_core_alua_lu_gp_release,
  2104. .show_attribute = target_core_alua_lu_gp_attr_show,
  2105. .store_attribute = target_core_alua_lu_gp_attr_store,
  2106. };
  2107. static struct config_item_type target_core_alua_lu_gp_cit = {
  2108. .ct_item_ops = &target_core_alua_lu_gp_ops,
  2109. .ct_attrs = target_core_alua_lu_gp_attrs,
  2110. .ct_owner = THIS_MODULE,
  2111. };
  2112. /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
  2113. /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
  2114. static struct config_group *target_core_alua_create_lu_gp(
  2115. struct config_group *group,
  2116. const char *name)
  2117. {
  2118. struct t10_alua_lu_gp *lu_gp;
  2119. struct config_group *alua_lu_gp_cg = NULL;
  2120. struct config_item *alua_lu_gp_ci = NULL;
  2121. lu_gp = core_alua_allocate_lu_gp(name, 0);
  2122. if (IS_ERR(lu_gp))
  2123. return NULL;
  2124. alua_lu_gp_cg = &lu_gp->lu_gp_group;
  2125. alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
  2126. config_group_init_type_name(alua_lu_gp_cg, name,
  2127. &target_core_alua_lu_gp_cit);
  2128. pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
  2129. " Group: core/alua/lu_gps/%s\n",
  2130. config_item_name(alua_lu_gp_ci));
  2131. return alua_lu_gp_cg;
  2132. }
  2133. static void target_core_alua_drop_lu_gp(
  2134. struct config_group *group,
  2135. struct config_item *item)
  2136. {
  2137. struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
  2138. struct t10_alua_lu_gp, lu_gp_group);
  2139. pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
  2140. " Group: core/alua/lu_gps/%s, ID: %hu\n",
  2141. config_item_name(item), lu_gp->lu_gp_id);
  2142. /*
  2143. * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
  2144. * -> target_core_alua_lu_gp_release()
  2145. */
  2146. config_item_put(item);
  2147. }
  2148. static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
  2149. .make_group = &target_core_alua_create_lu_gp,
  2150. .drop_item = &target_core_alua_drop_lu_gp,
  2151. };
  2152. static struct config_item_type target_core_alua_lu_gps_cit = {
  2153. .ct_item_ops = NULL,
  2154. .ct_group_ops = &target_core_alua_lu_gps_group_ops,
  2155. .ct_owner = THIS_MODULE,
  2156. };
  2157. /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
  2158. /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
  2159. CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
  2160. #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode) \
  2161. static struct target_core_alua_tg_pt_gp_attribute \
  2162. target_core_alua_tg_pt_gp_##_name = \
  2163. __CONFIGFS_EATTR(_name, _mode, \
  2164. target_core_alua_tg_pt_gp_show_attr_##_name, \
  2165. target_core_alua_tg_pt_gp_store_attr_##_name);
  2166. #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name) \
  2167. static struct target_core_alua_tg_pt_gp_attribute \
  2168. target_core_alua_tg_pt_gp_##_name = \
  2169. __CONFIGFS_EATTR_RO(_name, \
  2170. target_core_alua_tg_pt_gp_show_attr_##_name);
  2171. /*
  2172. * alua_access_state
  2173. */
  2174. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
  2175. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2176. char *page)
  2177. {
  2178. return sprintf(page, "%d\n",
  2179. atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
  2180. }
  2181. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
  2182. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2183. const char *page,
  2184. size_t count)
  2185. {
  2186. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  2187. unsigned long tmp;
  2188. int new_state, ret;
  2189. if (!tg_pt_gp->tg_pt_gp_valid_id) {
  2190. pr_err("Unable to do implicit ALUA on non valid"
  2191. " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
  2192. return -EINVAL;
  2193. }
  2194. if (!(dev->dev_flags & DF_CONFIGURED)) {
  2195. pr_err("Unable to set alua_access_state while device is"
  2196. " not configured\n");
  2197. return -ENODEV;
  2198. }
  2199. ret = kstrtoul(page, 0, &tmp);
  2200. if (ret < 0) {
  2201. pr_err("Unable to extract new ALUA access state from"
  2202. " %s\n", page);
  2203. return ret;
  2204. }
  2205. new_state = (int)tmp;
  2206. if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
  2207. pr_err("Unable to process implicit configfs ALUA"
  2208. " transition while TPGS_IMPLICIT_ALUA is disabled\n");
  2209. return -EINVAL;
  2210. }
  2211. if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
  2212. new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
  2213. /* LBA DEPENDENT is only allowed with implicit ALUA */
  2214. pr_err("Unable to process implicit configfs ALUA transition"
  2215. " while explicit ALUA management is enabled\n");
  2216. return -EINVAL;
  2217. }
  2218. ret = core_alua_do_port_transition(tg_pt_gp, dev,
  2219. NULL, NULL, new_state, 0);
  2220. return (!ret) ? count : -EINVAL;
  2221. }
  2222. SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
  2223. /*
  2224. * alua_access_status
  2225. */
  2226. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
  2227. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2228. char *page)
  2229. {
  2230. return sprintf(page, "%s\n",
  2231. core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
  2232. }
  2233. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
  2234. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2235. const char *page,
  2236. size_t count)
  2237. {
  2238. unsigned long tmp;
  2239. int new_status, ret;
  2240. if (!tg_pt_gp->tg_pt_gp_valid_id) {
  2241. pr_err("Unable to do set ALUA access status on non"
  2242. " valid tg_pt_gp ID: %hu\n",
  2243. tg_pt_gp->tg_pt_gp_valid_id);
  2244. return -EINVAL;
  2245. }
  2246. ret = kstrtoul(page, 0, &tmp);
  2247. if (ret < 0) {
  2248. pr_err("Unable to extract new ALUA access status"
  2249. " from %s\n", page);
  2250. return ret;
  2251. }
  2252. new_status = (int)tmp;
  2253. if ((new_status != ALUA_STATUS_NONE) &&
  2254. (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
  2255. (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
  2256. pr_err("Illegal ALUA access status: 0x%02x\n",
  2257. new_status);
  2258. return -EINVAL;
  2259. }
  2260. tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
  2261. return count;
  2262. }
  2263. SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
  2264. /*
  2265. * alua_access_type
  2266. */
  2267. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
  2268. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2269. char *page)
  2270. {
  2271. return core_alua_show_access_type(tg_pt_gp, page);
  2272. }
  2273. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
  2274. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2275. const char *page,
  2276. size_t count)
  2277. {
  2278. return core_alua_store_access_type(tg_pt_gp, page, count);
  2279. }
  2280. SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
  2281. /*
  2282. * alua_supported_states
  2283. */
  2284. #define SE_DEV_ALUA_SUPPORT_STATE_SHOW(_name, _var, _bit) \
  2285. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_support_##_name( \
  2286. struct t10_alua_tg_pt_gp *t, char *p) \
  2287. { \
  2288. return sprintf(p, "%d\n", !!(t->_var & _bit)); \
  2289. }
  2290. #define SE_DEV_ALUA_SUPPORT_STATE_STORE(_name, _var, _bit) \
  2291. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_support_##_name(\
  2292. struct t10_alua_tg_pt_gp *t, const char *p, size_t c) \
  2293. { \
  2294. unsigned long tmp; \
  2295. int ret; \
  2296. \
  2297. if (!t->tg_pt_gp_valid_id) { \
  2298. pr_err("Unable to do set ##_name ALUA state on non" \
  2299. " valid tg_pt_gp ID: %hu\n", \
  2300. t->tg_pt_gp_valid_id); \
  2301. return -EINVAL; \
  2302. } \
  2303. \
  2304. ret = kstrtoul(p, 0, &tmp); \
  2305. if (ret < 0) { \
  2306. pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
  2307. return -EINVAL; \
  2308. } \
  2309. if (tmp > 1) { \
  2310. pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
  2311. return -EINVAL; \
  2312. } \
  2313. if (tmp) \
  2314. t->_var |= _bit; \
  2315. else \
  2316. t->_var &= ~_bit; \
  2317. \
  2318. return c; \
  2319. }
  2320. SE_DEV_ALUA_SUPPORT_STATE_SHOW(transitioning,
  2321. tg_pt_gp_alua_supported_states, ALUA_T_SUP);
  2322. SE_DEV_ALUA_SUPPORT_STATE_STORE(transitioning,
  2323. tg_pt_gp_alua_supported_states, ALUA_T_SUP);
  2324. SE_DEV_ALUA_TG_PT_ATTR(alua_support_transitioning, S_IRUGO | S_IWUSR);
  2325. SE_DEV_ALUA_SUPPORT_STATE_SHOW(offline,
  2326. tg_pt_gp_alua_supported_states, ALUA_O_SUP);
  2327. SE_DEV_ALUA_SUPPORT_STATE_STORE(offline,
  2328. tg_pt_gp_alua_supported_states, ALUA_O_SUP);
  2329. SE_DEV_ALUA_TG_PT_ATTR(alua_support_offline, S_IRUGO | S_IWUSR);
  2330. SE_DEV_ALUA_SUPPORT_STATE_SHOW(lba_dependent,
  2331. tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
  2332. SE_DEV_ALUA_SUPPORT_STATE_STORE(lba_dependent,
  2333. tg_pt_gp_alua_supported_states, ALUA_LBD_SUP);
  2334. SE_DEV_ALUA_TG_PT_ATTR(alua_support_lba_dependent, S_IRUGO);
  2335. SE_DEV_ALUA_SUPPORT_STATE_SHOW(unavailable,
  2336. tg_pt_gp_alua_supported_states, ALUA_U_SUP);
  2337. SE_DEV_ALUA_SUPPORT_STATE_STORE(unavailable,
  2338. tg_pt_gp_alua_supported_states, ALUA_U_SUP);
  2339. SE_DEV_ALUA_TG_PT_ATTR(alua_support_unavailable, S_IRUGO | S_IWUSR);
  2340. SE_DEV_ALUA_SUPPORT_STATE_SHOW(standby,
  2341. tg_pt_gp_alua_supported_states, ALUA_S_SUP);
  2342. SE_DEV_ALUA_SUPPORT_STATE_STORE(standby,
  2343. tg_pt_gp_alua_supported_states, ALUA_S_SUP);
  2344. SE_DEV_ALUA_TG_PT_ATTR(alua_support_standby, S_IRUGO | S_IWUSR);
  2345. SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_optimized,
  2346. tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
  2347. SE_DEV_ALUA_SUPPORT_STATE_STORE(active_optimized,
  2348. tg_pt_gp_alua_supported_states, ALUA_AO_SUP);
  2349. SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_optimized, S_IRUGO | S_IWUSR);
  2350. SE_DEV_ALUA_SUPPORT_STATE_SHOW(active_nonoptimized,
  2351. tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
  2352. SE_DEV_ALUA_SUPPORT_STATE_STORE(active_nonoptimized,
  2353. tg_pt_gp_alua_supported_states, ALUA_AN_SUP);
  2354. SE_DEV_ALUA_TG_PT_ATTR(alua_support_active_nonoptimized, S_IRUGO | S_IWUSR);
  2355. /*
  2356. * alua_write_metadata
  2357. */
  2358. static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
  2359. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2360. char *page)
  2361. {
  2362. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
  2363. }
  2364. static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
  2365. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2366. const char *page,
  2367. size_t count)
  2368. {
  2369. unsigned long tmp;
  2370. int ret;
  2371. ret = kstrtoul(page, 0, &tmp);
  2372. if (ret < 0) {
  2373. pr_err("Unable to extract alua_write_metadata\n");
  2374. return ret;
  2375. }
  2376. if ((tmp != 0) && (tmp != 1)) {
  2377. pr_err("Illegal value for alua_write_metadata:"
  2378. " %lu\n", tmp);
  2379. return -EINVAL;
  2380. }
  2381. tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
  2382. return count;
  2383. }
  2384. SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
  2385. /*
  2386. * nonop_delay_msecs
  2387. */
  2388. static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
  2389. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2390. char *page)
  2391. {
  2392. return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
  2393. }
  2394. static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
  2395. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2396. const char *page,
  2397. size_t count)
  2398. {
  2399. return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
  2400. }
  2401. SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
  2402. /*
  2403. * trans_delay_msecs
  2404. */
  2405. static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
  2406. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2407. char *page)
  2408. {
  2409. return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
  2410. }
  2411. static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
  2412. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2413. const char *page,
  2414. size_t count)
  2415. {
  2416. return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
  2417. }
  2418. SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
  2419. /*
  2420. * implicit_trans_secs
  2421. */
  2422. static ssize_t target_core_alua_tg_pt_gp_show_attr_implicit_trans_secs(
  2423. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2424. char *page)
  2425. {
  2426. return core_alua_show_implicit_trans_secs(tg_pt_gp, page);
  2427. }
  2428. static ssize_t target_core_alua_tg_pt_gp_store_attr_implicit_trans_secs(
  2429. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2430. const char *page,
  2431. size_t count)
  2432. {
  2433. return core_alua_store_implicit_trans_secs(tg_pt_gp, page, count);
  2434. }
  2435. SE_DEV_ALUA_TG_PT_ATTR(implicit_trans_secs, S_IRUGO | S_IWUSR);
  2436. /*
  2437. * preferred
  2438. */
  2439. static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
  2440. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2441. char *page)
  2442. {
  2443. return core_alua_show_preferred_bit(tg_pt_gp, page);
  2444. }
  2445. static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
  2446. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2447. const char *page,
  2448. size_t count)
  2449. {
  2450. return core_alua_store_preferred_bit(tg_pt_gp, page, count);
  2451. }
  2452. SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
  2453. /*
  2454. * tg_pt_gp_id
  2455. */
  2456. static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
  2457. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2458. char *page)
  2459. {
  2460. if (!tg_pt_gp->tg_pt_gp_valid_id)
  2461. return 0;
  2462. return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
  2463. }
  2464. static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
  2465. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2466. const char *page,
  2467. size_t count)
  2468. {
  2469. struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
  2470. unsigned long tg_pt_gp_id;
  2471. int ret;
  2472. ret = kstrtoul(page, 0, &tg_pt_gp_id);
  2473. if (ret < 0) {
  2474. pr_err("kstrtoul() returned %d for"
  2475. " tg_pt_gp_id\n", ret);
  2476. return ret;
  2477. }
  2478. if (tg_pt_gp_id > 0x0000ffff) {
  2479. pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
  2480. " 0x0000ffff\n", tg_pt_gp_id);
  2481. return -EINVAL;
  2482. }
  2483. ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
  2484. if (ret < 0)
  2485. return -EINVAL;
  2486. pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
  2487. "core/alua/tg_pt_gps/%s to ID: %hu\n",
  2488. config_item_name(&alua_tg_pt_gp_cg->cg_item),
  2489. tg_pt_gp->tg_pt_gp_id);
  2490. return count;
  2491. }
  2492. SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
  2493. /*
  2494. * members
  2495. */
  2496. static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
  2497. struct t10_alua_tg_pt_gp *tg_pt_gp,
  2498. char *page)
  2499. {
  2500. struct se_lun *lun;
  2501. ssize_t len = 0, cur_len;
  2502. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  2503. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  2504. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  2505. list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
  2506. lun_tg_pt_gp_link) {
  2507. struct se_portal_group *tpg = lun->lun_tpg;
  2508. cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
  2509. "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
  2510. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  2511. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  2512. config_item_name(&lun->lun_group.cg_item));
  2513. cur_len++; /* Extra byte for NULL terminator */
  2514. if ((cur_len + len) > PAGE_SIZE) {
  2515. pr_warn("Ran out of lu_gp_show_attr"
  2516. "_members buffer\n");
  2517. break;
  2518. }
  2519. memcpy(page+len, buf, cur_len);
  2520. len += cur_len;
  2521. }
  2522. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  2523. return len;
  2524. }
  2525. SE_DEV_ALUA_TG_PT_ATTR_RO(members);
  2526. CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
  2527. tg_pt_gp_group);
  2528. static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
  2529. &target_core_alua_tg_pt_gp_alua_access_state.attr,
  2530. &target_core_alua_tg_pt_gp_alua_access_status.attr,
  2531. &target_core_alua_tg_pt_gp_alua_access_type.attr,
  2532. &target_core_alua_tg_pt_gp_alua_support_transitioning.attr,
  2533. &target_core_alua_tg_pt_gp_alua_support_offline.attr,
  2534. &target_core_alua_tg_pt_gp_alua_support_lba_dependent.attr,
  2535. &target_core_alua_tg_pt_gp_alua_support_unavailable.attr,
  2536. &target_core_alua_tg_pt_gp_alua_support_standby.attr,
  2537. &target_core_alua_tg_pt_gp_alua_support_active_nonoptimized.attr,
  2538. &target_core_alua_tg_pt_gp_alua_support_active_optimized.attr,
  2539. &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
  2540. &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
  2541. &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
  2542. &target_core_alua_tg_pt_gp_implicit_trans_secs.attr,
  2543. &target_core_alua_tg_pt_gp_preferred.attr,
  2544. &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
  2545. &target_core_alua_tg_pt_gp_members.attr,
  2546. NULL,
  2547. };
  2548. static void target_core_alua_tg_pt_gp_release(struct config_item *item)
  2549. {
  2550. struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
  2551. struct t10_alua_tg_pt_gp, tg_pt_gp_group);
  2552. core_alua_free_tg_pt_gp(tg_pt_gp);
  2553. }
  2554. static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
  2555. .release = target_core_alua_tg_pt_gp_release,
  2556. .show_attribute = target_core_alua_tg_pt_gp_attr_show,
  2557. .store_attribute = target_core_alua_tg_pt_gp_attr_store,
  2558. };
  2559. static struct config_item_type target_core_alua_tg_pt_gp_cit = {
  2560. .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
  2561. .ct_attrs = target_core_alua_tg_pt_gp_attrs,
  2562. .ct_owner = THIS_MODULE,
  2563. };
  2564. /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
  2565. /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
  2566. static struct config_group *target_core_alua_create_tg_pt_gp(
  2567. struct config_group *group,
  2568. const char *name)
  2569. {
  2570. struct t10_alua *alua = container_of(group, struct t10_alua,
  2571. alua_tg_pt_gps_group);
  2572. struct t10_alua_tg_pt_gp *tg_pt_gp;
  2573. struct config_group *alua_tg_pt_gp_cg = NULL;
  2574. struct config_item *alua_tg_pt_gp_ci = NULL;
  2575. tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
  2576. if (!tg_pt_gp)
  2577. return NULL;
  2578. alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
  2579. alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
  2580. config_group_init_type_name(alua_tg_pt_gp_cg, name,
  2581. &target_core_alua_tg_pt_gp_cit);
  2582. pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
  2583. " Group: alua/tg_pt_gps/%s\n",
  2584. config_item_name(alua_tg_pt_gp_ci));
  2585. return alua_tg_pt_gp_cg;
  2586. }
  2587. static void target_core_alua_drop_tg_pt_gp(
  2588. struct config_group *group,
  2589. struct config_item *item)
  2590. {
  2591. struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
  2592. struct t10_alua_tg_pt_gp, tg_pt_gp_group);
  2593. pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
  2594. " Group: alua/tg_pt_gps/%s, ID: %hu\n",
  2595. config_item_name(item), tg_pt_gp->tg_pt_gp_id);
  2596. /*
  2597. * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
  2598. * -> target_core_alua_tg_pt_gp_release().
  2599. */
  2600. config_item_put(item);
  2601. }
  2602. static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
  2603. .make_group = &target_core_alua_create_tg_pt_gp,
  2604. .drop_item = &target_core_alua_drop_tg_pt_gp,
  2605. };
  2606. TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
  2607. /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
  2608. /* Start functions for struct config_item_type target_core_alua_cit */
  2609. /*
  2610. * target_core_alua_cit is a ConfigFS group that lives under
  2611. * /sys/kernel/config/target/core/alua. There are default groups
  2612. * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
  2613. * target_core_alua_cit in target_core_init_configfs() below.
  2614. */
  2615. static struct config_item_type target_core_alua_cit = {
  2616. .ct_item_ops = NULL,
  2617. .ct_attrs = NULL,
  2618. .ct_owner = THIS_MODULE,
  2619. };
  2620. /* End functions for struct config_item_type target_core_alua_cit */
  2621. /* Start functions for struct config_item_type tb_dev_stat_cit */
  2622. static struct config_group *target_core_stat_mkdir(
  2623. struct config_group *group,
  2624. const char *name)
  2625. {
  2626. return ERR_PTR(-ENOSYS);
  2627. }
  2628. static void target_core_stat_rmdir(
  2629. struct config_group *group,
  2630. struct config_item *item)
  2631. {
  2632. return;
  2633. }
  2634. static struct configfs_group_operations target_core_stat_group_ops = {
  2635. .make_group = &target_core_stat_mkdir,
  2636. .drop_item = &target_core_stat_rmdir,
  2637. };
  2638. TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
  2639. /* End functions for struct config_item_type tb_dev_stat_cit */
  2640. /* Start functions for struct config_item_type target_core_hba_cit */
  2641. static struct config_group *target_core_make_subdev(
  2642. struct config_group *group,
  2643. const char *name)
  2644. {
  2645. struct t10_alua_tg_pt_gp *tg_pt_gp;
  2646. struct config_item *hba_ci = &group->cg_item;
  2647. struct se_hba *hba = item_to_hba(hba_ci);
  2648. struct target_backend *tb = hba->backend;
  2649. struct se_device *dev;
  2650. struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
  2651. struct config_group *dev_stat_grp = NULL;
  2652. int errno = -ENOMEM, ret;
  2653. ret = mutex_lock_interruptible(&hba->hba_access_mutex);
  2654. if (ret)
  2655. return ERR_PTR(ret);
  2656. dev = target_alloc_device(hba, name);
  2657. if (!dev)
  2658. goto out_unlock;
  2659. dev_cg = &dev->dev_group;
  2660. dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
  2661. GFP_KERNEL);
  2662. if (!dev_cg->default_groups)
  2663. goto out_free_device;
  2664. config_group_init_type_name(dev_cg, name, &tb->tb_dev_cit);
  2665. config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
  2666. &tb->tb_dev_attrib_cit);
  2667. config_group_init_type_name(&dev->dev_pr_group, "pr",
  2668. &tb->tb_dev_pr_cit);
  2669. config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
  2670. &tb->tb_dev_wwn_cit);
  2671. config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
  2672. "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
  2673. config_group_init_type_name(&dev->dev_stat_grps.stat_group,
  2674. "statistics", &tb->tb_dev_stat_cit);
  2675. dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
  2676. dev_cg->default_groups[1] = &dev->dev_pr_group;
  2677. dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
  2678. dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
  2679. dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
  2680. dev_cg->default_groups[5] = NULL;
  2681. /*
  2682. * Add core/$HBA/$DEV/alua/default_tg_pt_gp
  2683. */
  2684. tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
  2685. if (!tg_pt_gp)
  2686. goto out_free_dev_cg_default_groups;
  2687. dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
  2688. tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
  2689. tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2690. GFP_KERNEL);
  2691. if (!tg_pt_gp_cg->default_groups) {
  2692. pr_err("Unable to allocate tg_pt_gp_cg->"
  2693. "default_groups\n");
  2694. goto out_free_tg_pt_gp;
  2695. }
  2696. config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
  2697. "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
  2698. tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
  2699. tg_pt_gp_cg->default_groups[1] = NULL;
  2700. /*
  2701. * Add core/$HBA/$DEV/statistics/ default groups
  2702. */
  2703. dev_stat_grp = &dev->dev_stat_grps.stat_group;
  2704. dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
  2705. GFP_KERNEL);
  2706. if (!dev_stat_grp->default_groups) {
  2707. pr_err("Unable to allocate dev_stat_grp->default_groups\n");
  2708. goto out_free_tg_pt_gp_cg_default_groups;
  2709. }
  2710. target_stat_setup_dev_default_groups(dev);
  2711. mutex_unlock(&hba->hba_access_mutex);
  2712. return dev_cg;
  2713. out_free_tg_pt_gp_cg_default_groups:
  2714. kfree(tg_pt_gp_cg->default_groups);
  2715. out_free_tg_pt_gp:
  2716. core_alua_free_tg_pt_gp(tg_pt_gp);
  2717. out_free_dev_cg_default_groups:
  2718. kfree(dev_cg->default_groups);
  2719. out_free_device:
  2720. target_free_device(dev);
  2721. out_unlock:
  2722. mutex_unlock(&hba->hba_access_mutex);
  2723. return ERR_PTR(errno);
  2724. }
  2725. static void target_core_drop_subdev(
  2726. struct config_group *group,
  2727. struct config_item *item)
  2728. {
  2729. struct config_group *dev_cg = to_config_group(item);
  2730. struct se_device *dev =
  2731. container_of(dev_cg, struct se_device, dev_group);
  2732. struct se_hba *hba;
  2733. struct config_item *df_item;
  2734. struct config_group *tg_pt_gp_cg, *dev_stat_grp;
  2735. int i;
  2736. hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
  2737. mutex_lock(&hba->hba_access_mutex);
  2738. dev_stat_grp = &dev->dev_stat_grps.stat_group;
  2739. for (i = 0; dev_stat_grp->default_groups[i]; i++) {
  2740. df_item = &dev_stat_grp->default_groups[i]->cg_item;
  2741. dev_stat_grp->default_groups[i] = NULL;
  2742. config_item_put(df_item);
  2743. }
  2744. kfree(dev_stat_grp->default_groups);
  2745. tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
  2746. for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
  2747. df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
  2748. tg_pt_gp_cg->default_groups[i] = NULL;
  2749. config_item_put(df_item);
  2750. }
  2751. kfree(tg_pt_gp_cg->default_groups);
  2752. /*
  2753. * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
  2754. * directly from target_core_alua_tg_pt_gp_release().
  2755. */
  2756. dev->t10_alua.default_tg_pt_gp = NULL;
  2757. for (i = 0; dev_cg->default_groups[i]; i++) {
  2758. df_item = &dev_cg->default_groups[i]->cg_item;
  2759. dev_cg->default_groups[i] = NULL;
  2760. config_item_put(df_item);
  2761. }
  2762. /*
  2763. * se_dev is released from target_core_dev_item_ops->release()
  2764. */
  2765. config_item_put(item);
  2766. mutex_unlock(&hba->hba_access_mutex);
  2767. }
  2768. static struct configfs_group_operations target_core_hba_group_ops = {
  2769. .make_group = target_core_make_subdev,
  2770. .drop_item = target_core_drop_subdev,
  2771. };
  2772. CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
  2773. #define SE_HBA_ATTR(_name, _mode) \
  2774. static struct target_core_hba_attribute \
  2775. target_core_hba_##_name = \
  2776. __CONFIGFS_EATTR(_name, _mode, \
  2777. target_core_hba_show_attr_##_name, \
  2778. target_core_hba_store_attr_##_name);
  2779. #define SE_HBA_ATTR_RO(_name) \
  2780. static struct target_core_hba_attribute \
  2781. target_core_hba_##_name = \
  2782. __CONFIGFS_EATTR_RO(_name, \
  2783. target_core_hba_show_attr_##_name);
  2784. static ssize_t target_core_hba_show_attr_hba_info(
  2785. struct se_hba *hba,
  2786. char *page)
  2787. {
  2788. return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
  2789. hba->hba_id, hba->backend->ops->name,
  2790. TARGET_CORE_VERSION);
  2791. }
  2792. SE_HBA_ATTR_RO(hba_info);
  2793. static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
  2794. char *page)
  2795. {
  2796. int hba_mode = 0;
  2797. if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
  2798. hba_mode = 1;
  2799. return sprintf(page, "%d\n", hba_mode);
  2800. }
  2801. static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
  2802. const char *page, size_t count)
  2803. {
  2804. unsigned long mode_flag;
  2805. int ret;
  2806. if (hba->backend->ops->pmode_enable_hba == NULL)
  2807. return -EINVAL;
  2808. ret = kstrtoul(page, 0, &mode_flag);
  2809. if (ret < 0) {
  2810. pr_err("Unable to extract hba mode flag: %d\n", ret);
  2811. return ret;
  2812. }
  2813. if (hba->dev_count) {
  2814. pr_err("Unable to set hba_mode with active devices\n");
  2815. return -EINVAL;
  2816. }
  2817. ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
  2818. if (ret < 0)
  2819. return -EINVAL;
  2820. if (ret > 0)
  2821. hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
  2822. else if (ret == 0)
  2823. hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
  2824. return count;
  2825. }
  2826. SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
  2827. CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
  2828. static void target_core_hba_release(struct config_item *item)
  2829. {
  2830. struct se_hba *hba = container_of(to_config_group(item),
  2831. struct se_hba, hba_group);
  2832. core_delete_hba(hba);
  2833. }
  2834. static struct configfs_attribute *target_core_hba_attrs[] = {
  2835. &target_core_hba_hba_info.attr,
  2836. &target_core_hba_hba_mode.attr,
  2837. NULL,
  2838. };
  2839. static struct configfs_item_operations target_core_hba_item_ops = {
  2840. .release = target_core_hba_release,
  2841. .show_attribute = target_core_hba_attr_show,
  2842. .store_attribute = target_core_hba_attr_store,
  2843. };
  2844. static struct config_item_type target_core_hba_cit = {
  2845. .ct_item_ops = &target_core_hba_item_ops,
  2846. .ct_group_ops = &target_core_hba_group_ops,
  2847. .ct_attrs = target_core_hba_attrs,
  2848. .ct_owner = THIS_MODULE,
  2849. };
  2850. static struct config_group *target_core_call_addhbatotarget(
  2851. struct config_group *group,
  2852. const char *name)
  2853. {
  2854. char *se_plugin_str, *str, *str2;
  2855. struct se_hba *hba;
  2856. char buf[TARGET_CORE_NAME_MAX_LEN];
  2857. unsigned long plugin_dep_id = 0;
  2858. int ret;
  2859. memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
  2860. if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
  2861. pr_err("Passed *name strlen(): %d exceeds"
  2862. " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
  2863. TARGET_CORE_NAME_MAX_LEN);
  2864. return ERR_PTR(-ENAMETOOLONG);
  2865. }
  2866. snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
  2867. str = strstr(buf, "_");
  2868. if (!str) {
  2869. pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
  2870. return ERR_PTR(-EINVAL);
  2871. }
  2872. se_plugin_str = buf;
  2873. /*
  2874. * Special case for subsystem plugins that have "_" in their names.
  2875. * Namely rd_direct and rd_mcp..
  2876. */
  2877. str2 = strstr(str+1, "_");
  2878. if (str2) {
  2879. *str2 = '\0'; /* Terminate for *se_plugin_str */
  2880. str2++; /* Skip to start of plugin dependent ID */
  2881. str = str2;
  2882. } else {
  2883. *str = '\0'; /* Terminate for *se_plugin_str */
  2884. str++; /* Skip to start of plugin dependent ID */
  2885. }
  2886. ret = kstrtoul(str, 0, &plugin_dep_id);
  2887. if (ret < 0) {
  2888. pr_err("kstrtoul() returned %d for"
  2889. " plugin_dep_id\n", ret);
  2890. return ERR_PTR(ret);
  2891. }
  2892. /*
  2893. * Load up TCM subsystem plugins if they have not already been loaded.
  2894. */
  2895. transport_subsystem_check_init();
  2896. hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
  2897. if (IS_ERR(hba))
  2898. return ERR_CAST(hba);
  2899. config_group_init_type_name(&hba->hba_group, name,
  2900. &target_core_hba_cit);
  2901. return &hba->hba_group;
  2902. }
  2903. static void target_core_call_delhbafromtarget(
  2904. struct config_group *group,
  2905. struct config_item *item)
  2906. {
  2907. /*
  2908. * core_delete_hba() is called from target_core_hba_item_ops->release()
  2909. * -> target_core_hba_release()
  2910. */
  2911. config_item_put(item);
  2912. }
  2913. static struct configfs_group_operations target_core_group_ops = {
  2914. .make_group = target_core_call_addhbatotarget,
  2915. .drop_item = target_core_call_delhbafromtarget,
  2916. };
  2917. static struct config_item_type target_core_cit = {
  2918. .ct_item_ops = NULL,
  2919. .ct_group_ops = &target_core_group_ops,
  2920. .ct_attrs = NULL,
  2921. .ct_owner = THIS_MODULE,
  2922. };
  2923. /* Stop functions for struct config_item_type target_core_hba_cit */
  2924. void target_setup_backend_cits(struct target_backend *tb)
  2925. {
  2926. target_core_setup_dev_cit(tb);
  2927. target_core_setup_dev_attrib_cit(tb);
  2928. target_core_setup_dev_pr_cit(tb);
  2929. target_core_setup_dev_wwn_cit(tb);
  2930. target_core_setup_dev_alua_tg_pt_gps_cit(tb);
  2931. target_core_setup_dev_stat_cit(tb);
  2932. }
  2933. static int __init target_core_init_configfs(void)
  2934. {
  2935. struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
  2936. struct config_group *lu_gp_cg = NULL;
  2937. struct configfs_subsystem *subsys = &target_core_fabrics;
  2938. struct t10_alua_lu_gp *lu_gp;
  2939. int ret;
  2940. pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
  2941. " Engine: %s on %s/%s on "UTS_RELEASE"\n",
  2942. TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
  2943. config_group_init(&subsys->su_group);
  2944. mutex_init(&subsys->su_mutex);
  2945. ret = init_se_kmem_caches();
  2946. if (ret < 0)
  2947. return ret;
  2948. /*
  2949. * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
  2950. * and ALUA Logical Unit Group and Target Port Group infrastructure.
  2951. */
  2952. target_cg = &subsys->su_group;
  2953. target_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2954. GFP_KERNEL);
  2955. if (!target_cg->default_groups) {
  2956. pr_err("Unable to allocate target_cg->default_groups\n");
  2957. ret = -ENOMEM;
  2958. goto out_global;
  2959. }
  2960. config_group_init_type_name(&target_core_hbagroup,
  2961. "core", &target_core_cit);
  2962. target_cg->default_groups[0] = &target_core_hbagroup;
  2963. target_cg->default_groups[1] = NULL;
  2964. /*
  2965. * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
  2966. */
  2967. hba_cg = &target_core_hbagroup;
  2968. hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2969. GFP_KERNEL);
  2970. if (!hba_cg->default_groups) {
  2971. pr_err("Unable to allocate hba_cg->default_groups\n");
  2972. ret = -ENOMEM;
  2973. goto out_global;
  2974. }
  2975. config_group_init_type_name(&alua_group,
  2976. "alua", &target_core_alua_cit);
  2977. hba_cg->default_groups[0] = &alua_group;
  2978. hba_cg->default_groups[1] = NULL;
  2979. /*
  2980. * Add ALUA Logical Unit Group and Target Port Group ConfigFS
  2981. * groups under /sys/kernel/config/target/core/alua/
  2982. */
  2983. alua_cg = &alua_group;
  2984. alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  2985. GFP_KERNEL);
  2986. if (!alua_cg->default_groups) {
  2987. pr_err("Unable to allocate alua_cg->default_groups\n");
  2988. ret = -ENOMEM;
  2989. goto out_global;
  2990. }
  2991. config_group_init_type_name(&alua_lu_gps_group,
  2992. "lu_gps", &target_core_alua_lu_gps_cit);
  2993. alua_cg->default_groups[0] = &alua_lu_gps_group;
  2994. alua_cg->default_groups[1] = NULL;
  2995. /*
  2996. * Add core/alua/lu_gps/default_lu_gp
  2997. */
  2998. lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
  2999. if (IS_ERR(lu_gp)) {
  3000. ret = -ENOMEM;
  3001. goto out_global;
  3002. }
  3003. lu_gp_cg = &alua_lu_gps_group;
  3004. lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
  3005. GFP_KERNEL);
  3006. if (!lu_gp_cg->default_groups) {
  3007. pr_err("Unable to allocate lu_gp_cg->default_groups\n");
  3008. ret = -ENOMEM;
  3009. goto out_global;
  3010. }
  3011. config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
  3012. &target_core_alua_lu_gp_cit);
  3013. lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
  3014. lu_gp_cg->default_groups[1] = NULL;
  3015. default_lu_gp = lu_gp;
  3016. /*
  3017. * Register the target_core_mod subsystem with configfs.
  3018. */
  3019. ret = configfs_register_subsystem(subsys);
  3020. if (ret < 0) {
  3021. pr_err("Error %d while registering subsystem %s\n",
  3022. ret, subsys->su_group.cg_item.ci_namebuf);
  3023. goto out_global;
  3024. }
  3025. pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
  3026. " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
  3027. " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
  3028. /*
  3029. * Register built-in RAMDISK subsystem logic for virtual LUN 0
  3030. */
  3031. ret = rd_module_init();
  3032. if (ret < 0)
  3033. goto out;
  3034. ret = core_dev_setup_virtual_lun0();
  3035. if (ret < 0)
  3036. goto out;
  3037. ret = target_xcopy_setup_pt();
  3038. if (ret < 0)
  3039. goto out;
  3040. return 0;
  3041. out:
  3042. configfs_unregister_subsystem(subsys);
  3043. core_dev_release_virtual_lun0();
  3044. rd_module_exit();
  3045. out_global:
  3046. if (default_lu_gp) {
  3047. core_alua_free_lu_gp(default_lu_gp);
  3048. default_lu_gp = NULL;
  3049. }
  3050. if (lu_gp_cg)
  3051. kfree(lu_gp_cg->default_groups);
  3052. if (alua_cg)
  3053. kfree(alua_cg->default_groups);
  3054. if (hba_cg)
  3055. kfree(hba_cg->default_groups);
  3056. kfree(target_cg->default_groups);
  3057. release_se_kmem_caches();
  3058. return ret;
  3059. }
  3060. static void __exit target_core_exit_configfs(void)
  3061. {
  3062. struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
  3063. struct config_item *item;
  3064. int i;
  3065. lu_gp_cg = &alua_lu_gps_group;
  3066. for (i = 0; lu_gp_cg->default_groups[i]; i++) {
  3067. item = &lu_gp_cg->default_groups[i]->cg_item;
  3068. lu_gp_cg->default_groups[i] = NULL;
  3069. config_item_put(item);
  3070. }
  3071. kfree(lu_gp_cg->default_groups);
  3072. lu_gp_cg->default_groups = NULL;
  3073. alua_cg = &alua_group;
  3074. for (i = 0; alua_cg->default_groups[i]; i++) {
  3075. item = &alua_cg->default_groups[i]->cg_item;
  3076. alua_cg->default_groups[i] = NULL;
  3077. config_item_put(item);
  3078. }
  3079. kfree(alua_cg->default_groups);
  3080. alua_cg->default_groups = NULL;
  3081. hba_cg = &target_core_hbagroup;
  3082. for (i = 0; hba_cg->default_groups[i]; i++) {
  3083. item = &hba_cg->default_groups[i]->cg_item;
  3084. hba_cg->default_groups[i] = NULL;
  3085. config_item_put(item);
  3086. }
  3087. kfree(hba_cg->default_groups);
  3088. hba_cg->default_groups = NULL;
  3089. /*
  3090. * We expect subsys->su_group.default_groups to be released
  3091. * by configfs subsystem provider logic..
  3092. */
  3093. configfs_unregister_subsystem(&target_core_fabrics);
  3094. kfree(target_core_fabrics.su_group.default_groups);
  3095. core_alua_free_lu_gp(default_lu_gp);
  3096. default_lu_gp = NULL;
  3097. pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
  3098. " Infrastructure\n");
  3099. core_dev_release_virtual_lun0();
  3100. rd_module_exit();
  3101. target_xcopy_release_pt();
  3102. release_se_kmem_caches();
  3103. }
  3104. MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
  3105. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  3106. MODULE_LICENSE("GPL");
  3107. module_init(target_core_init_configfs);
  3108. module_exit(target_core_exit_configfs);