target_core_configfs.c 80 KB

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