target_core_configfs.c 80 KB

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