target_core_configfs.c 87 KB

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