target_core_configfs.c 87 KB

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