target_core_pr.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391
  1. /*******************************************************************************
  2. * Filename: target_core_pr.c
  3. *
  4. * This file contains SPC-3 compliant persistent reservations and
  5. * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
  6. *
  7. * Copyright (c) 2009, 2010 Rising Tide Systems
  8. * Copyright (c) 2009, 2010 Linux-iSCSI.org
  9. *
  10. * Nicholas A. Bellinger <nab@kernel.org>
  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. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. *
  26. ******************************************************************************/
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/list.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <asm/unaligned.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_backend.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_configfs.h>
  37. #include "target_core_internal.h"
  38. #include "target_core_pr.h"
  39. #include "target_core_ua.h"
  40. /*
  41. * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
  42. */
  43. struct pr_transport_id_holder {
  44. int dest_local_nexus;
  45. struct t10_pr_registration *dest_pr_reg;
  46. struct se_portal_group *dest_tpg;
  47. struct se_node_acl *dest_node_acl;
  48. struct se_dev_entry *dest_se_deve;
  49. struct list_head dest_list;
  50. };
  51. int core_pr_dump_initiator_port(
  52. struct t10_pr_registration *pr_reg,
  53. char *buf,
  54. u32 size)
  55. {
  56. if (!pr_reg->isid_present_at_reg)
  57. return 0;
  58. snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
  59. return 1;
  60. }
  61. static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
  62. struct t10_pr_registration *, int);
  63. static int core_scsi2_reservation_seq_non_holder(
  64. struct se_cmd *cmd,
  65. unsigned char *cdb,
  66. u32 pr_reg_type)
  67. {
  68. switch (cdb[0]) {
  69. case INQUIRY:
  70. case RELEASE:
  71. case RELEASE_10:
  72. return 0;
  73. default:
  74. return 1;
  75. }
  76. return 1;
  77. }
  78. static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
  79. {
  80. struct se_device *dev = cmd->se_dev;
  81. struct se_session *sess = cmd->se_sess;
  82. int ret;
  83. if (!sess)
  84. return 0;
  85. spin_lock(&dev->dev_reservation_lock);
  86. if (!dev->dev_reserved_node_acl || !sess) {
  87. spin_unlock(&dev->dev_reservation_lock);
  88. return 0;
  89. }
  90. if (dev->dev_reserved_node_acl != sess->se_node_acl) {
  91. spin_unlock(&dev->dev_reservation_lock);
  92. return -EINVAL;
  93. }
  94. if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
  95. spin_unlock(&dev->dev_reservation_lock);
  96. return 0;
  97. }
  98. ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
  99. spin_unlock(&dev->dev_reservation_lock);
  100. return ret;
  101. }
  102. static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
  103. struct se_node_acl *, struct se_session *);
  104. static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
  105. static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
  106. {
  107. struct se_session *se_sess = cmd->se_sess;
  108. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  109. struct t10_pr_registration *pr_reg;
  110. struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
  111. int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
  112. int conflict = 0;
  113. if (!crh)
  114. return -EINVAL;
  115. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  116. se_sess);
  117. if (pr_reg) {
  118. /*
  119. * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
  120. * behavior
  121. *
  122. * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
  123. * status, but no reservation shall be established and the
  124. * persistent reservation shall not be changed, if the command
  125. * is received from a) and b) below.
  126. *
  127. * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
  128. * status, but the persistent reservation shall not be released,
  129. * if the command is received from a) and b)
  130. *
  131. * a) An I_T nexus that is a persistent reservation holder; or
  132. * b) An I_T nexus that is registered if a registrants only or
  133. * all registrants type persistent reservation is present.
  134. *
  135. * In all other cases, a RESERVE(6) command, RESERVE(10) command,
  136. * RELEASE(6) command, or RELEASE(10) command shall be processed
  137. * as defined in SPC-2.
  138. */
  139. if (pr_reg->pr_res_holder) {
  140. core_scsi3_put_pr_reg(pr_reg);
  141. return 1;
  142. }
  143. if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
  144. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
  145. (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  146. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  147. core_scsi3_put_pr_reg(pr_reg);
  148. return 1;
  149. }
  150. core_scsi3_put_pr_reg(pr_reg);
  151. conflict = 1;
  152. } else {
  153. /*
  154. * Following spc2r20 5.5.1 Reservations overview:
  155. *
  156. * If a logical unit has executed a PERSISTENT RESERVE OUT
  157. * command with the REGISTER or the REGISTER AND IGNORE
  158. * EXISTING KEY service action and is still registered by any
  159. * initiator, all RESERVE commands and all RELEASE commands
  160. * regardless of initiator shall conflict and shall terminate
  161. * with a RESERVATION CONFLICT status.
  162. */
  163. spin_lock(&pr_tmpl->registration_lock);
  164. conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
  165. spin_unlock(&pr_tmpl->registration_lock);
  166. }
  167. if (conflict) {
  168. pr_err("Received legacy SPC-2 RESERVE/RELEASE"
  169. " while active SPC-3 registrations exist,"
  170. " returning RESERVATION_CONFLICT\n");
  171. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  172. return -EBUSY;
  173. }
  174. return 0;
  175. }
  176. int target_scsi2_reservation_release(struct se_cmd *cmd)
  177. {
  178. struct se_device *dev = cmd->se_dev;
  179. struct se_session *sess = cmd->se_sess;
  180. struct se_portal_group *tpg = sess->se_tpg;
  181. int ret = 0, rc;
  182. if (!sess || !tpg)
  183. goto out;
  184. rc = target_check_scsi2_reservation_conflict(cmd);
  185. if (rc == 1)
  186. goto out;
  187. else if (rc < 0) {
  188. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  189. ret = -EINVAL;
  190. goto out;
  191. }
  192. ret = 0;
  193. spin_lock(&dev->dev_reservation_lock);
  194. if (!dev->dev_reserved_node_acl || !sess)
  195. goto out_unlock;
  196. if (dev->dev_reserved_node_acl != sess->se_node_acl)
  197. goto out_unlock;
  198. dev->dev_reserved_node_acl = NULL;
  199. dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
  200. if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
  201. dev->dev_res_bin_isid = 0;
  202. dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
  203. }
  204. pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
  205. " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  206. cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
  207. sess->se_node_acl->initiatorname);
  208. out_unlock:
  209. spin_unlock(&dev->dev_reservation_lock);
  210. out:
  211. if (!ret)
  212. target_complete_cmd(cmd, GOOD);
  213. return ret;
  214. }
  215. int target_scsi2_reservation_reserve(struct se_cmd *cmd)
  216. {
  217. struct se_device *dev = cmd->se_dev;
  218. struct se_session *sess = cmd->se_sess;
  219. struct se_portal_group *tpg = sess->se_tpg;
  220. int ret = 0, rc;
  221. if ((cmd->t_task_cdb[1] & 0x01) &&
  222. (cmd->t_task_cdb[1] & 0x02)) {
  223. pr_err("LongIO and Obselete Bits set, returning"
  224. " ILLEGAL_REQUEST\n");
  225. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  226. ret = -EINVAL;
  227. goto out;
  228. }
  229. /*
  230. * This is currently the case for target_core_mod passthrough struct se_cmd
  231. * ops
  232. */
  233. if (!sess || !tpg)
  234. goto out;
  235. rc = target_check_scsi2_reservation_conflict(cmd);
  236. if (rc == 1)
  237. goto out;
  238. else if (rc < 0) {
  239. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  240. ret = -EINVAL;
  241. goto out;
  242. }
  243. ret = 0;
  244. spin_lock(&dev->dev_reservation_lock);
  245. if (dev->dev_reserved_node_acl &&
  246. (dev->dev_reserved_node_acl != sess->se_node_acl)) {
  247. pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
  248. tpg->se_tpg_tfo->get_fabric_name());
  249. pr_err("Original reserver LUN: %u %s\n",
  250. cmd->se_lun->unpacked_lun,
  251. dev->dev_reserved_node_acl->initiatorname);
  252. pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
  253. " from %s \n", cmd->se_lun->unpacked_lun,
  254. cmd->se_deve->mapped_lun,
  255. sess->se_node_acl->initiatorname);
  256. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  257. ret = -EINVAL;
  258. goto out_unlock;
  259. }
  260. dev->dev_reserved_node_acl = sess->se_node_acl;
  261. dev->dev_flags |= DF_SPC2_RESERVATIONS;
  262. if (sess->sess_bin_isid != 0) {
  263. dev->dev_res_bin_isid = sess->sess_bin_isid;
  264. dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
  265. }
  266. pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
  267. " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  268. cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
  269. sess->se_node_acl->initiatorname);
  270. out_unlock:
  271. spin_unlock(&dev->dev_reservation_lock);
  272. out:
  273. if (!ret)
  274. target_complete_cmd(cmd, GOOD);
  275. return ret;
  276. }
  277. /*
  278. * Begin SPC-3/SPC-4 Persistent Reservations emulation support
  279. *
  280. * This function is called by those initiator ports who are *NOT*
  281. * the active PR reservation holder when a reservation is present.
  282. */
  283. static int core_scsi3_pr_seq_non_holder(
  284. struct se_cmd *cmd,
  285. unsigned char *cdb,
  286. u32 pr_reg_type)
  287. {
  288. struct se_dev_entry *se_deve;
  289. struct se_session *se_sess = cmd->se_sess;
  290. int other_cdb = 0, ignore_reg;
  291. int registered_nexus = 0, ret = 1; /* Conflict by default */
  292. int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
  293. int we = 0; /* Write Exclusive */
  294. int legacy = 0; /* Act like a legacy device and return
  295. * RESERVATION CONFLICT on some CDBs */
  296. /*
  297. * A legacy SPC-2 reservation is being held.
  298. */
  299. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
  300. return core_scsi2_reservation_seq_non_holder(cmd,
  301. cdb, pr_reg_type);
  302. se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  303. /*
  304. * Determine if the registration should be ignored due to
  305. * non-matching ISIDs in core_scsi3_pr_reservation_check().
  306. */
  307. ignore_reg = (pr_reg_type & 0x80000000);
  308. if (ignore_reg)
  309. pr_reg_type &= ~0x80000000;
  310. switch (pr_reg_type) {
  311. case PR_TYPE_WRITE_EXCLUSIVE:
  312. we = 1;
  313. case PR_TYPE_EXCLUSIVE_ACCESS:
  314. /*
  315. * Some commands are only allowed for the persistent reservation
  316. * holder.
  317. */
  318. if ((se_deve->def_pr_registered) && !(ignore_reg))
  319. registered_nexus = 1;
  320. break;
  321. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  322. we = 1;
  323. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  324. /*
  325. * Some commands are only allowed for registered I_T Nexuses.
  326. */
  327. reg_only = 1;
  328. if ((se_deve->def_pr_registered) && !(ignore_reg))
  329. registered_nexus = 1;
  330. break;
  331. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  332. we = 1;
  333. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  334. /*
  335. * Each registered I_T Nexus is a reservation holder.
  336. */
  337. all_reg = 1;
  338. if ((se_deve->def_pr_registered) && !(ignore_reg))
  339. registered_nexus = 1;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. /*
  345. * Referenced from spc4r17 table 45 for *NON* PR holder access
  346. */
  347. switch (cdb[0]) {
  348. case SECURITY_PROTOCOL_IN:
  349. if (registered_nexus)
  350. return 0;
  351. ret = (we) ? 0 : 1;
  352. break;
  353. case MODE_SENSE:
  354. case MODE_SENSE_10:
  355. case READ_ATTRIBUTE:
  356. case READ_BUFFER:
  357. case RECEIVE_DIAGNOSTIC:
  358. if (legacy) {
  359. ret = 1;
  360. break;
  361. }
  362. if (registered_nexus) {
  363. ret = 0;
  364. break;
  365. }
  366. ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
  367. break;
  368. case PERSISTENT_RESERVE_OUT:
  369. /*
  370. * This follows PERSISTENT_RESERVE_OUT service actions that
  371. * are allowed in the presence of various reservations.
  372. * See spc4r17, table 46
  373. */
  374. switch (cdb[1] & 0x1f) {
  375. case PRO_CLEAR:
  376. case PRO_PREEMPT:
  377. case PRO_PREEMPT_AND_ABORT:
  378. ret = (registered_nexus) ? 0 : 1;
  379. break;
  380. case PRO_REGISTER:
  381. case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
  382. ret = 0;
  383. break;
  384. case PRO_REGISTER_AND_MOVE:
  385. case PRO_RESERVE:
  386. ret = 1;
  387. break;
  388. case PRO_RELEASE:
  389. ret = (registered_nexus) ? 0 : 1;
  390. break;
  391. default:
  392. pr_err("Unknown PERSISTENT_RESERVE_OUT service"
  393. " action: 0x%02x\n", cdb[1] & 0x1f);
  394. return -EINVAL;
  395. }
  396. break;
  397. case RELEASE:
  398. case RELEASE_10:
  399. /* Handled by CRH=1 in target_scsi2_reservation_release() */
  400. ret = 0;
  401. break;
  402. case RESERVE:
  403. case RESERVE_10:
  404. /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
  405. ret = 0;
  406. break;
  407. case TEST_UNIT_READY:
  408. ret = (legacy) ? 1 : 0; /* Conflict for legacy */
  409. break;
  410. case MAINTENANCE_IN:
  411. switch (cdb[1] & 0x1f) {
  412. case MI_MANAGEMENT_PROTOCOL_IN:
  413. if (registered_nexus) {
  414. ret = 0;
  415. break;
  416. }
  417. ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
  418. break;
  419. case MI_REPORT_SUPPORTED_OPERATION_CODES:
  420. case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
  421. if (legacy) {
  422. ret = 1;
  423. break;
  424. }
  425. if (registered_nexus) {
  426. ret = 0;
  427. break;
  428. }
  429. ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
  430. break;
  431. case MI_REPORT_ALIASES:
  432. case MI_REPORT_IDENTIFYING_INFORMATION:
  433. case MI_REPORT_PRIORITY:
  434. case MI_REPORT_TARGET_PGS:
  435. case MI_REPORT_TIMESTAMP:
  436. ret = 0; /* Allowed */
  437. break;
  438. default:
  439. pr_err("Unknown MI Service Action: 0x%02x\n",
  440. (cdb[1] & 0x1f));
  441. return -EINVAL;
  442. }
  443. break;
  444. case ACCESS_CONTROL_IN:
  445. case ACCESS_CONTROL_OUT:
  446. case INQUIRY:
  447. case LOG_SENSE:
  448. case READ_MEDIA_SERIAL_NUMBER:
  449. case REPORT_LUNS:
  450. case REQUEST_SENSE:
  451. case PERSISTENT_RESERVE_IN:
  452. ret = 0; /*/ Allowed CDBs */
  453. break;
  454. default:
  455. other_cdb = 1;
  456. break;
  457. }
  458. /*
  459. * Case where the CDB is explicitly allowed in the above switch
  460. * statement.
  461. */
  462. if (!ret && !other_cdb) {
  463. pr_debug("Allowing explict CDB: 0x%02x for %s"
  464. " reservation holder\n", cdb[0],
  465. core_scsi3_pr_dump_type(pr_reg_type));
  466. return ret;
  467. }
  468. /*
  469. * Check if write exclusive initiator ports *NOT* holding the
  470. * WRITE_EXCLUSIVE_* reservation.
  471. */
  472. if ((we) && !(registered_nexus)) {
  473. if (cmd->data_direction == DMA_TO_DEVICE) {
  474. /*
  475. * Conflict for write exclusive
  476. */
  477. pr_debug("%s Conflict for unregistered nexus"
  478. " %s CDB: 0x%02x to %s reservation\n",
  479. transport_dump_cmd_direction(cmd),
  480. se_sess->se_node_acl->initiatorname, cdb[0],
  481. core_scsi3_pr_dump_type(pr_reg_type));
  482. return 1;
  483. } else {
  484. /*
  485. * Allow non WRITE CDBs for all Write Exclusive
  486. * PR TYPEs to pass for registered and
  487. * non-registered_nexuxes NOT holding the reservation.
  488. *
  489. * We only make noise for the unregisterd nexuses,
  490. * as we expect registered non-reservation holding
  491. * nexuses to issue CDBs.
  492. */
  493. if (!registered_nexus) {
  494. pr_debug("Allowing implict CDB: 0x%02x"
  495. " for %s reservation on unregistered"
  496. " nexus\n", cdb[0],
  497. core_scsi3_pr_dump_type(pr_reg_type));
  498. }
  499. return 0;
  500. }
  501. } else if ((reg_only) || (all_reg)) {
  502. if (registered_nexus) {
  503. /*
  504. * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
  505. * allow commands from registered nexuses.
  506. */
  507. pr_debug("Allowing implict CDB: 0x%02x for %s"
  508. " reservation\n", cdb[0],
  509. core_scsi3_pr_dump_type(pr_reg_type));
  510. return 0;
  511. }
  512. }
  513. pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
  514. " for %s reservation\n", transport_dump_cmd_direction(cmd),
  515. (registered_nexus) ? "" : "un",
  516. se_sess->se_node_acl->initiatorname, cdb[0],
  517. core_scsi3_pr_dump_type(pr_reg_type));
  518. return 1; /* Conflict by default */
  519. }
  520. static u32 core_scsi3_pr_generation(struct se_device *dev)
  521. {
  522. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  523. u32 prg;
  524. /*
  525. * PRGeneration field shall contain the value of a 32-bit wrapping
  526. * counter mainted by the device server.
  527. *
  528. * Note that this is done regardless of Active Persist across
  529. * Target PowerLoss (APTPL)
  530. *
  531. * See spc4r17 section 6.3.12 READ_KEYS service action
  532. */
  533. spin_lock(&dev->dev_reservation_lock);
  534. prg = su_dev->t10_pr.pr_generation++;
  535. spin_unlock(&dev->dev_reservation_lock);
  536. return prg;
  537. }
  538. static int core_scsi3_pr_reservation_check(
  539. struct se_cmd *cmd,
  540. u32 *pr_reg_type)
  541. {
  542. struct se_device *dev = cmd->se_dev;
  543. struct se_session *sess = cmd->se_sess;
  544. int ret;
  545. if (!sess)
  546. return 0;
  547. /*
  548. * A legacy SPC-2 reservation is being held.
  549. */
  550. if (dev->dev_flags & DF_SPC2_RESERVATIONS)
  551. return core_scsi2_reservation_check(cmd, pr_reg_type);
  552. spin_lock(&dev->dev_reservation_lock);
  553. if (!dev->dev_pr_res_holder) {
  554. spin_unlock(&dev->dev_reservation_lock);
  555. return 0;
  556. }
  557. *pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
  558. cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
  559. if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl) {
  560. spin_unlock(&dev->dev_reservation_lock);
  561. return -EINVAL;
  562. }
  563. if (!dev->dev_pr_res_holder->isid_present_at_reg) {
  564. spin_unlock(&dev->dev_reservation_lock);
  565. return 0;
  566. }
  567. ret = (dev->dev_pr_res_holder->pr_reg_bin_isid ==
  568. sess->sess_bin_isid) ? 0 : -EINVAL;
  569. /*
  570. * Use bit in *pr_reg_type to notify ISID mismatch in
  571. * core_scsi3_pr_seq_non_holder().
  572. */
  573. if (ret != 0)
  574. *pr_reg_type |= 0x80000000;
  575. spin_unlock(&dev->dev_reservation_lock);
  576. return ret;
  577. }
  578. static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
  579. struct se_device *dev,
  580. struct se_node_acl *nacl,
  581. struct se_dev_entry *deve,
  582. unsigned char *isid,
  583. u64 sa_res_key,
  584. int all_tg_pt,
  585. int aptpl)
  586. {
  587. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  588. struct t10_pr_registration *pr_reg;
  589. pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
  590. if (!pr_reg) {
  591. pr_err("Unable to allocate struct t10_pr_registration\n");
  592. return NULL;
  593. }
  594. pr_reg->pr_aptpl_buf = kzalloc(su_dev->t10_pr.pr_aptpl_buf_len,
  595. GFP_ATOMIC);
  596. if (!pr_reg->pr_aptpl_buf) {
  597. pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
  598. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  599. return NULL;
  600. }
  601. INIT_LIST_HEAD(&pr_reg->pr_reg_list);
  602. INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
  603. INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
  604. INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
  605. INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
  606. atomic_set(&pr_reg->pr_res_holders, 0);
  607. pr_reg->pr_reg_nacl = nacl;
  608. pr_reg->pr_reg_deve = deve;
  609. pr_reg->pr_res_mapped_lun = deve->mapped_lun;
  610. pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
  611. pr_reg->pr_res_key = sa_res_key;
  612. pr_reg->pr_reg_all_tg_pt = all_tg_pt;
  613. pr_reg->pr_reg_aptpl = aptpl;
  614. pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
  615. /*
  616. * If an ISID value for this SCSI Initiator Port exists,
  617. * save it to the registration now.
  618. */
  619. if (isid != NULL) {
  620. pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
  621. snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
  622. pr_reg->isid_present_at_reg = 1;
  623. }
  624. return pr_reg;
  625. }
  626. static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
  627. static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
  628. /*
  629. * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
  630. * modes.
  631. */
  632. static struct t10_pr_registration *__core_scsi3_alloc_registration(
  633. struct se_device *dev,
  634. struct se_node_acl *nacl,
  635. struct se_dev_entry *deve,
  636. unsigned char *isid,
  637. u64 sa_res_key,
  638. int all_tg_pt,
  639. int aptpl)
  640. {
  641. struct se_dev_entry *deve_tmp;
  642. struct se_node_acl *nacl_tmp;
  643. struct se_port *port, *port_tmp;
  644. struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
  645. struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
  646. int ret;
  647. /*
  648. * Create a registration for the I_T Nexus upon which the
  649. * PROUT REGISTER was received.
  650. */
  651. pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
  652. sa_res_key, all_tg_pt, aptpl);
  653. if (!pr_reg)
  654. return NULL;
  655. /*
  656. * Return pointer to pr_reg for ALL_TG_PT=0
  657. */
  658. if (!all_tg_pt)
  659. return pr_reg;
  660. /*
  661. * Create list of matching SCSI Initiator Port registrations
  662. * for ALL_TG_PT=1
  663. */
  664. spin_lock(&dev->se_port_lock);
  665. list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
  666. atomic_inc(&port->sep_tg_pt_ref_cnt);
  667. smp_mb__after_atomic_inc();
  668. spin_unlock(&dev->se_port_lock);
  669. spin_lock_bh(&port->sep_alua_lock);
  670. list_for_each_entry(deve_tmp, &port->sep_alua_list,
  671. alua_port_list) {
  672. /*
  673. * This pointer will be NULL for demo mode MappedLUNs
  674. * that have not been make explict via a ConfigFS
  675. * MappedLUN group for the SCSI Initiator Node ACL.
  676. */
  677. if (!deve_tmp->se_lun_acl)
  678. continue;
  679. nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
  680. /*
  681. * Skip the matching struct se_node_acl that is allocated
  682. * above..
  683. */
  684. if (nacl == nacl_tmp)
  685. continue;
  686. /*
  687. * Only perform PR registrations for target ports on
  688. * the same fabric module as the REGISTER w/ ALL_TG_PT=1
  689. * arrived.
  690. */
  691. if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
  692. continue;
  693. /*
  694. * Look for a matching Initiator Node ACL in ASCII format
  695. */
  696. if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
  697. continue;
  698. atomic_inc(&deve_tmp->pr_ref_count);
  699. smp_mb__after_atomic_inc();
  700. spin_unlock_bh(&port->sep_alua_lock);
  701. /*
  702. * Grab a configfs group dependency that is released
  703. * for the exception path at label out: below, or upon
  704. * completion of adding ALL_TG_PT=1 registrations in
  705. * __core_scsi3_add_registration()
  706. */
  707. ret = core_scsi3_lunacl_depend_item(deve_tmp);
  708. if (ret < 0) {
  709. pr_err("core_scsi3_lunacl_depend"
  710. "_item() failed\n");
  711. atomic_dec(&port->sep_tg_pt_ref_cnt);
  712. smp_mb__after_atomic_dec();
  713. atomic_dec(&deve_tmp->pr_ref_count);
  714. smp_mb__after_atomic_dec();
  715. goto out;
  716. }
  717. /*
  718. * Located a matching SCSI Initiator Port on a different
  719. * port, allocate the pr_reg_atp and attach it to the
  720. * pr_reg->pr_reg_atp_list that will be processed once
  721. * the original *pr_reg is processed in
  722. * __core_scsi3_add_registration()
  723. */
  724. pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
  725. nacl_tmp, deve_tmp, NULL,
  726. sa_res_key, all_tg_pt, aptpl);
  727. if (!pr_reg_atp) {
  728. atomic_dec(&port->sep_tg_pt_ref_cnt);
  729. smp_mb__after_atomic_dec();
  730. atomic_dec(&deve_tmp->pr_ref_count);
  731. smp_mb__after_atomic_dec();
  732. core_scsi3_lunacl_undepend_item(deve_tmp);
  733. goto out;
  734. }
  735. list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
  736. &pr_reg->pr_reg_atp_list);
  737. spin_lock_bh(&port->sep_alua_lock);
  738. }
  739. spin_unlock_bh(&port->sep_alua_lock);
  740. spin_lock(&dev->se_port_lock);
  741. atomic_dec(&port->sep_tg_pt_ref_cnt);
  742. smp_mb__after_atomic_dec();
  743. }
  744. spin_unlock(&dev->se_port_lock);
  745. return pr_reg;
  746. out:
  747. list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
  748. &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
  749. list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
  750. core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
  751. kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
  752. }
  753. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  754. return NULL;
  755. }
  756. int core_scsi3_alloc_aptpl_registration(
  757. struct t10_reservation *pr_tmpl,
  758. u64 sa_res_key,
  759. unsigned char *i_port,
  760. unsigned char *isid,
  761. u32 mapped_lun,
  762. unsigned char *t_port,
  763. u16 tpgt,
  764. u32 target_lun,
  765. int res_holder,
  766. int all_tg_pt,
  767. u8 type)
  768. {
  769. struct t10_pr_registration *pr_reg;
  770. if (!i_port || !t_port || !sa_res_key) {
  771. pr_err("Illegal parameters for APTPL registration\n");
  772. return -EINVAL;
  773. }
  774. pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
  775. if (!pr_reg) {
  776. pr_err("Unable to allocate struct t10_pr_registration\n");
  777. return -ENOMEM;
  778. }
  779. pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
  780. INIT_LIST_HEAD(&pr_reg->pr_reg_list);
  781. INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
  782. INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
  783. INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
  784. INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
  785. atomic_set(&pr_reg->pr_res_holders, 0);
  786. pr_reg->pr_reg_nacl = NULL;
  787. pr_reg->pr_reg_deve = NULL;
  788. pr_reg->pr_res_mapped_lun = mapped_lun;
  789. pr_reg->pr_aptpl_target_lun = target_lun;
  790. pr_reg->pr_res_key = sa_res_key;
  791. pr_reg->pr_reg_all_tg_pt = all_tg_pt;
  792. pr_reg->pr_reg_aptpl = 1;
  793. pr_reg->pr_reg_tg_pt_lun = NULL;
  794. pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
  795. pr_reg->pr_res_type = type;
  796. /*
  797. * If an ISID value had been saved in APTPL metadata for this
  798. * SCSI Initiator Port, restore it now.
  799. */
  800. if (isid != NULL) {
  801. pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
  802. snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
  803. pr_reg->isid_present_at_reg = 1;
  804. }
  805. /*
  806. * Copy the i_port and t_port information from caller.
  807. */
  808. snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
  809. snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
  810. pr_reg->pr_reg_tpgt = tpgt;
  811. /*
  812. * Set pr_res_holder from caller, the pr_reg who is the reservation
  813. * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
  814. * the Initiator Node LUN ACL from the fabric module is created for
  815. * this registration.
  816. */
  817. pr_reg->pr_res_holder = res_holder;
  818. list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
  819. pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
  820. " metadata\n", (res_holder) ? "+reservation" : "");
  821. return 0;
  822. }
  823. static void core_scsi3_aptpl_reserve(
  824. struct se_device *dev,
  825. struct se_portal_group *tpg,
  826. struct se_node_acl *node_acl,
  827. struct t10_pr_registration *pr_reg)
  828. {
  829. char i_buf[PR_REG_ISID_ID_LEN];
  830. int prf_isid;
  831. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  832. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  833. PR_REG_ISID_ID_LEN);
  834. spin_lock(&dev->dev_reservation_lock);
  835. dev->dev_pr_res_holder = pr_reg;
  836. spin_unlock(&dev->dev_reservation_lock);
  837. pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
  838. " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
  839. tpg->se_tpg_tfo->get_fabric_name(),
  840. core_scsi3_pr_dump_type(pr_reg->pr_res_type),
  841. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  842. pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
  843. tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
  844. (prf_isid) ? &i_buf[0] : "");
  845. }
  846. static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
  847. struct t10_pr_registration *, int, int);
  848. static int __core_scsi3_check_aptpl_registration(
  849. struct se_device *dev,
  850. struct se_portal_group *tpg,
  851. struct se_lun *lun,
  852. u32 target_lun,
  853. struct se_node_acl *nacl,
  854. struct se_dev_entry *deve)
  855. {
  856. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  857. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  858. unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
  859. unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
  860. u16 tpgt;
  861. memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
  862. memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
  863. /*
  864. * Copy Initiator Port information from struct se_node_acl
  865. */
  866. snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
  867. snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
  868. tpg->se_tpg_tfo->tpg_get_wwn(tpg));
  869. tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
  870. /*
  871. * Look for the matching registrations+reservation from those
  872. * created from APTPL metadata. Note that multiple registrations
  873. * may exist for fabrics that use ISIDs in their SCSI Initiator Port
  874. * TransportIDs.
  875. */
  876. spin_lock(&pr_tmpl->aptpl_reg_lock);
  877. list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
  878. pr_reg_aptpl_list) {
  879. if (!strcmp(pr_reg->pr_iport, i_port) &&
  880. (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
  881. !(strcmp(pr_reg->pr_tport, t_port)) &&
  882. (pr_reg->pr_reg_tpgt == tpgt) &&
  883. (pr_reg->pr_aptpl_target_lun == target_lun)) {
  884. pr_reg->pr_reg_nacl = nacl;
  885. pr_reg->pr_reg_deve = deve;
  886. pr_reg->pr_reg_tg_pt_lun = lun;
  887. list_del(&pr_reg->pr_reg_aptpl_list);
  888. spin_unlock(&pr_tmpl->aptpl_reg_lock);
  889. /*
  890. * At this point all of the pointers in *pr_reg will
  891. * be setup, so go ahead and add the registration.
  892. */
  893. __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
  894. /*
  895. * If this registration is the reservation holder,
  896. * make that happen now..
  897. */
  898. if (pr_reg->pr_res_holder)
  899. core_scsi3_aptpl_reserve(dev, tpg,
  900. nacl, pr_reg);
  901. /*
  902. * Reenable pr_aptpl_active to accept new metadata
  903. * updates once the SCSI device is active again..
  904. */
  905. spin_lock(&pr_tmpl->aptpl_reg_lock);
  906. pr_tmpl->pr_aptpl_active = 1;
  907. }
  908. }
  909. spin_unlock(&pr_tmpl->aptpl_reg_lock);
  910. return 0;
  911. }
  912. int core_scsi3_check_aptpl_registration(
  913. struct se_device *dev,
  914. struct se_portal_group *tpg,
  915. struct se_lun *lun,
  916. struct se_lun_acl *lun_acl)
  917. {
  918. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  919. struct se_node_acl *nacl = lun_acl->se_lun_nacl;
  920. struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
  921. if (su_dev->t10_pr.res_type != SPC3_PERSISTENT_RESERVATIONS)
  922. return 0;
  923. return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
  924. lun->unpacked_lun, nacl, deve);
  925. }
  926. static void __core_scsi3_dump_registration(
  927. struct target_core_fabric_ops *tfo,
  928. struct se_device *dev,
  929. struct se_node_acl *nacl,
  930. struct t10_pr_registration *pr_reg,
  931. int register_type)
  932. {
  933. struct se_portal_group *se_tpg = nacl->se_tpg;
  934. char i_buf[PR_REG_ISID_ID_LEN];
  935. int prf_isid;
  936. memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
  937. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  938. PR_REG_ISID_ID_LEN);
  939. pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
  940. " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
  941. "_AND_MOVE" : (register_type == 1) ?
  942. "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
  943. (prf_isid) ? i_buf : "");
  944. pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
  945. tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
  946. tfo->tpg_get_tag(se_tpg));
  947. pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
  948. " Port(s)\n", tfo->get_fabric_name(),
  949. (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
  950. dev->transport->name);
  951. pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
  952. " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
  953. pr_reg->pr_res_key, pr_reg->pr_res_generation,
  954. pr_reg->pr_reg_aptpl);
  955. }
  956. /*
  957. * this function can be called with struct se_device->dev_reservation_lock
  958. * when register_move = 1
  959. */
  960. static void __core_scsi3_add_registration(
  961. struct se_device *dev,
  962. struct se_node_acl *nacl,
  963. struct t10_pr_registration *pr_reg,
  964. int register_type,
  965. int register_move)
  966. {
  967. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  968. struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
  969. struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
  970. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  971. /*
  972. * Increment PRgeneration counter for struct se_device upon a successful
  973. * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
  974. *
  975. * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
  976. * action, the struct se_device->dev_reservation_lock will already be held,
  977. * so we do not call core_scsi3_pr_generation() which grabs the lock
  978. * for the REGISTER.
  979. */
  980. pr_reg->pr_res_generation = (register_move) ?
  981. su_dev->t10_pr.pr_generation++ :
  982. core_scsi3_pr_generation(dev);
  983. spin_lock(&pr_tmpl->registration_lock);
  984. list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
  985. pr_reg->pr_reg_deve->def_pr_registered = 1;
  986. __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
  987. spin_unlock(&pr_tmpl->registration_lock);
  988. /*
  989. * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
  990. */
  991. if (!pr_reg->pr_reg_all_tg_pt || register_move)
  992. return;
  993. /*
  994. * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
  995. * allocated in __core_scsi3_alloc_registration()
  996. */
  997. list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
  998. &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
  999. list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
  1000. pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
  1001. spin_lock(&pr_tmpl->registration_lock);
  1002. list_add_tail(&pr_reg_tmp->pr_reg_list,
  1003. &pr_tmpl->registration_list);
  1004. pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
  1005. __core_scsi3_dump_registration(tfo, dev,
  1006. pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
  1007. register_type);
  1008. spin_unlock(&pr_tmpl->registration_lock);
  1009. /*
  1010. * Drop configfs group dependency reference from
  1011. * __core_scsi3_alloc_registration()
  1012. */
  1013. core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
  1014. }
  1015. }
  1016. static int core_scsi3_alloc_registration(
  1017. struct se_device *dev,
  1018. struct se_node_acl *nacl,
  1019. struct se_dev_entry *deve,
  1020. unsigned char *isid,
  1021. u64 sa_res_key,
  1022. int all_tg_pt,
  1023. int aptpl,
  1024. int register_type,
  1025. int register_move)
  1026. {
  1027. struct t10_pr_registration *pr_reg;
  1028. pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
  1029. sa_res_key, all_tg_pt, aptpl);
  1030. if (!pr_reg)
  1031. return -EPERM;
  1032. __core_scsi3_add_registration(dev, nacl, pr_reg,
  1033. register_type, register_move);
  1034. return 0;
  1035. }
  1036. static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
  1037. struct se_device *dev,
  1038. struct se_node_acl *nacl,
  1039. unsigned char *isid)
  1040. {
  1041. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  1042. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  1043. struct se_portal_group *tpg;
  1044. spin_lock(&pr_tmpl->registration_lock);
  1045. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  1046. &pr_tmpl->registration_list, pr_reg_list) {
  1047. /*
  1048. * First look for a matching struct se_node_acl
  1049. */
  1050. if (pr_reg->pr_reg_nacl != nacl)
  1051. continue;
  1052. tpg = pr_reg->pr_reg_nacl->se_tpg;
  1053. /*
  1054. * If this registration does NOT contain a fabric provided
  1055. * ISID, then we have found a match.
  1056. */
  1057. if (!pr_reg->isid_present_at_reg) {
  1058. /*
  1059. * Determine if this SCSI device server requires that
  1060. * SCSI Intiatior TransportID w/ ISIDs is enforced
  1061. * for fabric modules (iSCSI) requiring them.
  1062. */
  1063. if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
  1064. if (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids)
  1065. continue;
  1066. }
  1067. atomic_inc(&pr_reg->pr_res_holders);
  1068. smp_mb__after_atomic_inc();
  1069. spin_unlock(&pr_tmpl->registration_lock);
  1070. return pr_reg;
  1071. }
  1072. /*
  1073. * If the *pr_reg contains a fabric defined ISID for multi-value
  1074. * SCSI Initiator Port TransportIDs, then we expect a valid
  1075. * matching ISID to be provided by the local SCSI Initiator Port.
  1076. */
  1077. if (!isid)
  1078. continue;
  1079. if (strcmp(isid, pr_reg->pr_reg_isid))
  1080. continue;
  1081. atomic_inc(&pr_reg->pr_res_holders);
  1082. smp_mb__after_atomic_inc();
  1083. spin_unlock(&pr_tmpl->registration_lock);
  1084. return pr_reg;
  1085. }
  1086. spin_unlock(&pr_tmpl->registration_lock);
  1087. return NULL;
  1088. }
  1089. static struct t10_pr_registration *core_scsi3_locate_pr_reg(
  1090. struct se_device *dev,
  1091. struct se_node_acl *nacl,
  1092. struct se_session *sess)
  1093. {
  1094. struct se_portal_group *tpg = nacl->se_tpg;
  1095. unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
  1096. if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
  1097. memset(&buf[0], 0, PR_REG_ISID_LEN);
  1098. tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
  1099. PR_REG_ISID_LEN);
  1100. isid_ptr = &buf[0];
  1101. }
  1102. return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
  1103. }
  1104. static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
  1105. {
  1106. atomic_dec(&pr_reg->pr_res_holders);
  1107. smp_mb__after_atomic_dec();
  1108. }
  1109. static int core_scsi3_check_implict_release(
  1110. struct se_device *dev,
  1111. struct t10_pr_registration *pr_reg)
  1112. {
  1113. struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
  1114. struct t10_pr_registration *pr_res_holder;
  1115. int ret = 0;
  1116. spin_lock(&dev->dev_reservation_lock);
  1117. pr_res_holder = dev->dev_pr_res_holder;
  1118. if (!pr_res_holder) {
  1119. spin_unlock(&dev->dev_reservation_lock);
  1120. return ret;
  1121. }
  1122. if (pr_res_holder == pr_reg) {
  1123. /*
  1124. * Perform an implict RELEASE if the registration that
  1125. * is being released is holding the reservation.
  1126. *
  1127. * From spc4r17, section 5.7.11.1:
  1128. *
  1129. * e) If the I_T nexus is the persistent reservation holder
  1130. * and the persistent reservation is not an all registrants
  1131. * type, then a PERSISTENT RESERVE OUT command with REGISTER
  1132. * service action or REGISTER AND IGNORE EXISTING KEY
  1133. * service action with the SERVICE ACTION RESERVATION KEY
  1134. * field set to zero (see 5.7.11.3).
  1135. */
  1136. __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
  1137. ret = 1;
  1138. /*
  1139. * For 'All Registrants' reservation types, all existing
  1140. * registrations are still processed as reservation holders
  1141. * in core_scsi3_pr_seq_non_holder() after the initial
  1142. * reservation holder is implictly released here.
  1143. */
  1144. } else if (pr_reg->pr_reg_all_tg_pt &&
  1145. (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
  1146. pr_reg->pr_reg_nacl->initiatorname)) &&
  1147. (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
  1148. pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
  1149. " UNREGISTER while existing reservation with matching"
  1150. " key 0x%016Lx is present from another SCSI Initiator"
  1151. " Port\n", pr_reg->pr_res_key);
  1152. ret = -EPERM;
  1153. }
  1154. spin_unlock(&dev->dev_reservation_lock);
  1155. return ret;
  1156. }
  1157. /*
  1158. * Called with struct t10_reservation->registration_lock held.
  1159. */
  1160. static void __core_scsi3_free_registration(
  1161. struct se_device *dev,
  1162. struct t10_pr_registration *pr_reg,
  1163. struct list_head *preempt_and_abort_list,
  1164. int dec_holders)
  1165. {
  1166. struct target_core_fabric_ops *tfo =
  1167. pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
  1168. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  1169. char i_buf[PR_REG_ISID_ID_LEN];
  1170. int prf_isid;
  1171. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  1172. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  1173. PR_REG_ISID_ID_LEN);
  1174. pr_reg->pr_reg_deve->def_pr_registered = 0;
  1175. pr_reg->pr_reg_deve->pr_res_key = 0;
  1176. list_del(&pr_reg->pr_reg_list);
  1177. /*
  1178. * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
  1179. * so call core_scsi3_put_pr_reg() to decrement our reference.
  1180. */
  1181. if (dec_holders)
  1182. core_scsi3_put_pr_reg(pr_reg);
  1183. /*
  1184. * Wait until all reference from any other I_T nexuses for this
  1185. * *pr_reg have been released. Because list_del() is called above,
  1186. * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
  1187. * count back to zero, and we release *pr_reg.
  1188. */
  1189. while (atomic_read(&pr_reg->pr_res_holders) != 0) {
  1190. spin_unlock(&pr_tmpl->registration_lock);
  1191. pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
  1192. tfo->get_fabric_name());
  1193. cpu_relax();
  1194. spin_lock(&pr_tmpl->registration_lock);
  1195. }
  1196. pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
  1197. " Node: %s%s\n", tfo->get_fabric_name(),
  1198. pr_reg->pr_reg_nacl->initiatorname,
  1199. (prf_isid) ? &i_buf[0] : "");
  1200. pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
  1201. " Port(s)\n", tfo->get_fabric_name(),
  1202. (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
  1203. dev->transport->name);
  1204. pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
  1205. " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
  1206. pr_reg->pr_res_generation);
  1207. if (!preempt_and_abort_list) {
  1208. pr_reg->pr_reg_deve = NULL;
  1209. pr_reg->pr_reg_nacl = NULL;
  1210. kfree(pr_reg->pr_aptpl_buf);
  1211. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  1212. return;
  1213. }
  1214. /*
  1215. * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
  1216. * are released once the ABORT_TASK_SET has completed..
  1217. */
  1218. list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
  1219. }
  1220. void core_scsi3_free_pr_reg_from_nacl(
  1221. struct se_device *dev,
  1222. struct se_node_acl *nacl)
  1223. {
  1224. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  1225. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
  1226. /*
  1227. * If the passed se_node_acl matches the reservation holder,
  1228. * release the reservation.
  1229. */
  1230. spin_lock(&dev->dev_reservation_lock);
  1231. pr_res_holder = dev->dev_pr_res_holder;
  1232. if ((pr_res_holder != NULL) &&
  1233. (pr_res_holder->pr_reg_nacl == nacl))
  1234. __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
  1235. spin_unlock(&dev->dev_reservation_lock);
  1236. /*
  1237. * Release any registration associated with the struct se_node_acl.
  1238. */
  1239. spin_lock(&pr_tmpl->registration_lock);
  1240. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  1241. &pr_tmpl->registration_list, pr_reg_list) {
  1242. if (pr_reg->pr_reg_nacl != nacl)
  1243. continue;
  1244. __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
  1245. }
  1246. spin_unlock(&pr_tmpl->registration_lock);
  1247. }
  1248. void core_scsi3_free_all_registrations(
  1249. struct se_device *dev)
  1250. {
  1251. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  1252. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
  1253. spin_lock(&dev->dev_reservation_lock);
  1254. pr_res_holder = dev->dev_pr_res_holder;
  1255. if (pr_res_holder != NULL) {
  1256. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  1257. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  1258. pr_res_holder, 0);
  1259. }
  1260. spin_unlock(&dev->dev_reservation_lock);
  1261. spin_lock(&pr_tmpl->registration_lock);
  1262. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  1263. &pr_tmpl->registration_list, pr_reg_list) {
  1264. __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
  1265. }
  1266. spin_unlock(&pr_tmpl->registration_lock);
  1267. spin_lock(&pr_tmpl->aptpl_reg_lock);
  1268. list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
  1269. pr_reg_aptpl_list) {
  1270. list_del(&pr_reg->pr_reg_aptpl_list);
  1271. kfree(pr_reg->pr_aptpl_buf);
  1272. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  1273. }
  1274. spin_unlock(&pr_tmpl->aptpl_reg_lock);
  1275. }
  1276. static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
  1277. {
  1278. return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
  1279. &tpg->tpg_group.cg_item);
  1280. }
  1281. static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
  1282. {
  1283. configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
  1284. &tpg->tpg_group.cg_item);
  1285. atomic_dec(&tpg->tpg_pr_ref_count);
  1286. smp_mb__after_atomic_dec();
  1287. }
  1288. static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
  1289. {
  1290. struct se_portal_group *tpg = nacl->se_tpg;
  1291. if (nacl->dynamic_node_acl)
  1292. return 0;
  1293. return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
  1294. &nacl->acl_group.cg_item);
  1295. }
  1296. static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
  1297. {
  1298. struct se_portal_group *tpg = nacl->se_tpg;
  1299. if (nacl->dynamic_node_acl) {
  1300. atomic_dec(&nacl->acl_pr_ref_count);
  1301. smp_mb__after_atomic_dec();
  1302. return;
  1303. }
  1304. configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
  1305. &nacl->acl_group.cg_item);
  1306. atomic_dec(&nacl->acl_pr_ref_count);
  1307. smp_mb__after_atomic_dec();
  1308. }
  1309. static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
  1310. {
  1311. struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
  1312. struct se_node_acl *nacl;
  1313. struct se_portal_group *tpg;
  1314. /*
  1315. * For nacl->dynamic_node_acl=1
  1316. */
  1317. if (!lun_acl)
  1318. return 0;
  1319. nacl = lun_acl->se_lun_nacl;
  1320. tpg = nacl->se_tpg;
  1321. return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
  1322. &lun_acl->se_lun_group.cg_item);
  1323. }
  1324. static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
  1325. {
  1326. struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
  1327. struct se_node_acl *nacl;
  1328. struct se_portal_group *tpg;
  1329. /*
  1330. * For nacl->dynamic_node_acl=1
  1331. */
  1332. if (!lun_acl) {
  1333. atomic_dec(&se_deve->pr_ref_count);
  1334. smp_mb__after_atomic_dec();
  1335. return;
  1336. }
  1337. nacl = lun_acl->se_lun_nacl;
  1338. tpg = nacl->se_tpg;
  1339. configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
  1340. &lun_acl->se_lun_group.cg_item);
  1341. atomic_dec(&se_deve->pr_ref_count);
  1342. smp_mb__after_atomic_dec();
  1343. }
  1344. static int core_scsi3_decode_spec_i_port(
  1345. struct se_cmd *cmd,
  1346. struct se_portal_group *tpg,
  1347. unsigned char *l_isid,
  1348. u64 sa_res_key,
  1349. int all_tg_pt,
  1350. int aptpl)
  1351. {
  1352. struct se_device *dev = cmd->se_dev;
  1353. struct se_port *tmp_port;
  1354. struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
  1355. struct se_session *se_sess = cmd->se_sess;
  1356. struct se_node_acl *dest_node_acl = NULL;
  1357. struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
  1358. struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
  1359. struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
  1360. LIST_HEAD(tid_dest_list);
  1361. struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
  1362. struct target_core_fabric_ops *tmp_tf_ops;
  1363. unsigned char *buf;
  1364. unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
  1365. char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
  1366. u32 tpdl, tid_len = 0;
  1367. int ret, dest_local_nexus, prf_isid;
  1368. u32 dest_rtpi = 0;
  1369. memset(dest_iport, 0, 64);
  1370. local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  1371. /*
  1372. * Allocate a struct pr_transport_id_holder and setup the
  1373. * local_node_acl and local_se_deve pointers and add to
  1374. * struct list_head tid_dest_list for add registration
  1375. * processing in the loop of tid_dest_list below.
  1376. */
  1377. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
  1378. if (!tidh_new) {
  1379. pr_err("Unable to allocate tidh_new\n");
  1380. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1381. return -EINVAL;
  1382. }
  1383. INIT_LIST_HEAD(&tidh_new->dest_list);
  1384. tidh_new->dest_tpg = tpg;
  1385. tidh_new->dest_node_acl = se_sess->se_node_acl;
  1386. tidh_new->dest_se_deve = local_se_deve;
  1387. local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1388. se_sess->se_node_acl, local_se_deve, l_isid,
  1389. sa_res_key, all_tg_pt, aptpl);
  1390. if (!local_pr_reg) {
  1391. kfree(tidh_new);
  1392. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1393. return -ENOMEM;
  1394. }
  1395. tidh_new->dest_pr_reg = local_pr_reg;
  1396. /*
  1397. * The local I_T nexus does not hold any configfs dependances,
  1398. * so we set tid_h->dest_local_nexus=1 to prevent the
  1399. * configfs_undepend_item() calls in the tid_dest_list loops below.
  1400. */
  1401. tidh_new->dest_local_nexus = 1;
  1402. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1403. buf = transport_kmap_data_sg(cmd);
  1404. /*
  1405. * For a PERSISTENT RESERVE OUT specify initiator ports payload,
  1406. * first extract TransportID Parameter Data Length, and make sure
  1407. * the value matches up to the SCSI expected data transfer length.
  1408. */
  1409. tpdl = (buf[24] & 0xff) << 24;
  1410. tpdl |= (buf[25] & 0xff) << 16;
  1411. tpdl |= (buf[26] & 0xff) << 8;
  1412. tpdl |= buf[27] & 0xff;
  1413. if ((tpdl + 28) != cmd->data_length) {
  1414. pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
  1415. " does not equal CDB data_length: %u\n", tpdl,
  1416. cmd->data_length);
  1417. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1418. ret = -EINVAL;
  1419. goto out;
  1420. }
  1421. /*
  1422. * Start processing the received transport IDs using the
  1423. * receiving I_T Nexus portal's fabric dependent methods to
  1424. * obtain the SCSI Initiator Port/Device Identifiers.
  1425. */
  1426. ptr = &buf[28];
  1427. while (tpdl > 0) {
  1428. proto_ident = (ptr[0] & 0x0f);
  1429. dest_tpg = NULL;
  1430. spin_lock(&dev->se_port_lock);
  1431. list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
  1432. tmp_tpg = tmp_port->sep_tpg;
  1433. if (!tmp_tpg)
  1434. continue;
  1435. tmp_tf_ops = tmp_tpg->se_tpg_tfo;
  1436. if (!tmp_tf_ops)
  1437. continue;
  1438. if (!tmp_tf_ops->get_fabric_proto_ident ||
  1439. !tmp_tf_ops->tpg_parse_pr_out_transport_id)
  1440. continue;
  1441. /*
  1442. * Look for the matching proto_ident provided by
  1443. * the received TransportID
  1444. */
  1445. tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
  1446. if (tmp_proto_ident != proto_ident)
  1447. continue;
  1448. dest_rtpi = tmp_port->sep_rtpi;
  1449. i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
  1450. tmp_tpg, (const char *)ptr, &tid_len,
  1451. &iport_ptr);
  1452. if (!i_str)
  1453. continue;
  1454. atomic_inc(&tmp_tpg->tpg_pr_ref_count);
  1455. smp_mb__after_atomic_inc();
  1456. spin_unlock(&dev->se_port_lock);
  1457. ret = core_scsi3_tpg_depend_item(tmp_tpg);
  1458. if (ret != 0) {
  1459. pr_err(" core_scsi3_tpg_depend_item()"
  1460. " for tmp_tpg\n");
  1461. atomic_dec(&tmp_tpg->tpg_pr_ref_count);
  1462. smp_mb__after_atomic_dec();
  1463. cmd->scsi_sense_reason =
  1464. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1465. ret = -EINVAL;
  1466. goto out;
  1467. }
  1468. /*
  1469. * Locate the desination initiator ACL to be registered
  1470. * from the decoded fabric module specific TransportID
  1471. * at *i_str.
  1472. */
  1473. spin_lock_irq(&tmp_tpg->acl_node_lock);
  1474. dest_node_acl = __core_tpg_get_initiator_node_acl(
  1475. tmp_tpg, i_str);
  1476. if (dest_node_acl) {
  1477. atomic_inc(&dest_node_acl->acl_pr_ref_count);
  1478. smp_mb__after_atomic_inc();
  1479. }
  1480. spin_unlock_irq(&tmp_tpg->acl_node_lock);
  1481. if (!dest_node_acl) {
  1482. core_scsi3_tpg_undepend_item(tmp_tpg);
  1483. spin_lock(&dev->se_port_lock);
  1484. continue;
  1485. }
  1486. ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
  1487. if (ret != 0) {
  1488. pr_err("configfs_depend_item() failed"
  1489. " for dest_node_acl->acl_group\n");
  1490. atomic_dec(&dest_node_acl->acl_pr_ref_count);
  1491. smp_mb__after_atomic_dec();
  1492. core_scsi3_tpg_undepend_item(tmp_tpg);
  1493. cmd->scsi_sense_reason =
  1494. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1495. ret = -EINVAL;
  1496. goto out;
  1497. }
  1498. dest_tpg = tmp_tpg;
  1499. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
  1500. " %s Port RTPI: %hu\n",
  1501. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1502. dest_node_acl->initiatorname, dest_rtpi);
  1503. spin_lock(&dev->se_port_lock);
  1504. break;
  1505. }
  1506. spin_unlock(&dev->se_port_lock);
  1507. if (!dest_tpg) {
  1508. pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
  1509. " dest_tpg\n");
  1510. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1511. ret = -EINVAL;
  1512. goto out;
  1513. }
  1514. pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
  1515. " tid_len: %d for %s + %s\n",
  1516. dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
  1517. tpdl, tid_len, i_str, iport_ptr);
  1518. if (tid_len > tpdl) {
  1519. pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
  1520. " %u for Transport ID: %s\n", tid_len, ptr);
  1521. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1522. core_scsi3_tpg_undepend_item(dest_tpg);
  1523. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1524. ret = -EINVAL;
  1525. goto out;
  1526. }
  1527. /*
  1528. * Locate the desintation struct se_dev_entry pointer for matching
  1529. * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
  1530. * Target Port.
  1531. */
  1532. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
  1533. dest_rtpi);
  1534. if (!dest_se_deve) {
  1535. pr_err("Unable to locate %s dest_se_deve"
  1536. " from destination RTPI: %hu\n",
  1537. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1538. dest_rtpi);
  1539. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1540. core_scsi3_tpg_undepend_item(dest_tpg);
  1541. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1542. ret = -EINVAL;
  1543. goto out;
  1544. }
  1545. ret = core_scsi3_lunacl_depend_item(dest_se_deve);
  1546. if (ret < 0) {
  1547. pr_err("core_scsi3_lunacl_depend_item()"
  1548. " failed\n");
  1549. atomic_dec(&dest_se_deve->pr_ref_count);
  1550. smp_mb__after_atomic_dec();
  1551. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1552. core_scsi3_tpg_undepend_item(dest_tpg);
  1553. cmd->scsi_sense_reason =
  1554. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1555. ret = -EINVAL;
  1556. goto out;
  1557. }
  1558. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
  1559. " dest_se_deve mapped_lun: %u\n",
  1560. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1561. dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
  1562. /*
  1563. * Skip any TransportIDs that already have a registration for
  1564. * this target port.
  1565. */
  1566. pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  1567. iport_ptr);
  1568. if (pr_reg_e) {
  1569. core_scsi3_put_pr_reg(pr_reg_e);
  1570. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1571. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1572. core_scsi3_tpg_undepend_item(dest_tpg);
  1573. ptr += tid_len;
  1574. tpdl -= tid_len;
  1575. tid_len = 0;
  1576. continue;
  1577. }
  1578. /*
  1579. * Allocate a struct pr_transport_id_holder and setup
  1580. * the dest_node_acl and dest_se_deve pointers for the
  1581. * loop below.
  1582. */
  1583. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
  1584. GFP_KERNEL);
  1585. if (!tidh_new) {
  1586. pr_err("Unable to allocate tidh_new\n");
  1587. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1588. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1589. core_scsi3_tpg_undepend_item(dest_tpg);
  1590. cmd->scsi_sense_reason =
  1591. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1592. ret = -ENOMEM;
  1593. goto out;
  1594. }
  1595. INIT_LIST_HEAD(&tidh_new->dest_list);
  1596. tidh_new->dest_tpg = dest_tpg;
  1597. tidh_new->dest_node_acl = dest_node_acl;
  1598. tidh_new->dest_se_deve = dest_se_deve;
  1599. /*
  1600. * Allocate, but do NOT add the registration for the
  1601. * TransportID referenced SCSI Initiator port. This
  1602. * done because of the following from spc4r17 in section
  1603. * 6.14.3 wrt SPEC_I_PT:
  1604. *
  1605. * "If a registration fails for any initiator port (e.g., if th
  1606. * logical unit does not have enough resources available to
  1607. * hold the registration information), no registrations shall be
  1608. * made, and the command shall be terminated with
  1609. * CHECK CONDITION status."
  1610. *
  1611. * That means we call __core_scsi3_alloc_registration() here,
  1612. * and then call __core_scsi3_add_registration() in the
  1613. * 2nd loop which will never fail.
  1614. */
  1615. dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1616. dest_node_acl, dest_se_deve, iport_ptr,
  1617. sa_res_key, all_tg_pt, aptpl);
  1618. if (!dest_pr_reg) {
  1619. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1620. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1621. core_scsi3_tpg_undepend_item(dest_tpg);
  1622. kfree(tidh_new);
  1623. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1624. ret = -EINVAL;
  1625. goto out;
  1626. }
  1627. tidh_new->dest_pr_reg = dest_pr_reg;
  1628. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1629. ptr += tid_len;
  1630. tpdl -= tid_len;
  1631. tid_len = 0;
  1632. }
  1633. transport_kunmap_data_sg(cmd);
  1634. /*
  1635. * Go ahead and create a registrations from tid_dest_list for the
  1636. * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
  1637. * and dest_se_deve.
  1638. *
  1639. * The SA Reservation Key from the PROUT is set for the
  1640. * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
  1641. * means that the TransportID Initiator port will be
  1642. * registered on all of the target ports in the SCSI target device
  1643. * ALL_TG_PT=0 means the registration will only be for the
  1644. * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
  1645. * was received.
  1646. */
  1647. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1648. dest_tpg = tidh->dest_tpg;
  1649. dest_node_acl = tidh->dest_node_acl;
  1650. dest_se_deve = tidh->dest_se_deve;
  1651. dest_pr_reg = tidh->dest_pr_reg;
  1652. dest_local_nexus = tidh->dest_local_nexus;
  1653. list_del(&tidh->dest_list);
  1654. kfree(tidh);
  1655. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  1656. prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
  1657. PR_REG_ISID_ID_LEN);
  1658. __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
  1659. dest_pr_reg, 0, 0);
  1660. pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
  1661. " registered Transport ID for Node: %s%s Mapped LUN:"
  1662. " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
  1663. dest_node_acl->initiatorname, (prf_isid) ?
  1664. &i_buf[0] : "", dest_se_deve->mapped_lun);
  1665. if (dest_local_nexus)
  1666. continue;
  1667. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1668. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1669. core_scsi3_tpg_undepend_item(dest_tpg);
  1670. }
  1671. return 0;
  1672. out:
  1673. transport_kunmap_data_sg(cmd);
  1674. /*
  1675. * For the failure case, release everything from tid_dest_list
  1676. * including *dest_pr_reg and the configfs dependances..
  1677. */
  1678. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1679. dest_tpg = tidh->dest_tpg;
  1680. dest_node_acl = tidh->dest_node_acl;
  1681. dest_se_deve = tidh->dest_se_deve;
  1682. dest_pr_reg = tidh->dest_pr_reg;
  1683. dest_local_nexus = tidh->dest_local_nexus;
  1684. list_del(&tidh->dest_list);
  1685. kfree(tidh);
  1686. /*
  1687. * Release any extra ALL_TG_PT=1 registrations for
  1688. * the SPEC_I_PT=1 case.
  1689. */
  1690. list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
  1691. &dest_pr_reg->pr_reg_atp_list,
  1692. pr_reg_atp_mem_list) {
  1693. list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
  1694. core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
  1695. kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
  1696. }
  1697. kfree(dest_pr_reg->pr_aptpl_buf);
  1698. kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
  1699. if (dest_local_nexus)
  1700. continue;
  1701. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1702. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1703. core_scsi3_tpg_undepend_item(dest_tpg);
  1704. }
  1705. return ret;
  1706. }
  1707. /*
  1708. * Called with struct se_device->dev_reservation_lock held
  1709. */
  1710. static int __core_scsi3_update_aptpl_buf(
  1711. struct se_device *dev,
  1712. unsigned char *buf,
  1713. u32 pr_aptpl_buf_len,
  1714. int clear_aptpl_metadata)
  1715. {
  1716. struct se_lun *lun;
  1717. struct se_portal_group *tpg;
  1718. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1719. struct t10_pr_registration *pr_reg;
  1720. unsigned char tmp[512], isid_buf[32];
  1721. ssize_t len = 0;
  1722. int reg_count = 0;
  1723. memset(buf, 0, pr_aptpl_buf_len);
  1724. /*
  1725. * Called to clear metadata once APTPL has been deactivated.
  1726. */
  1727. if (clear_aptpl_metadata) {
  1728. snprintf(buf, pr_aptpl_buf_len,
  1729. "No Registrations or Reservations\n");
  1730. return 0;
  1731. }
  1732. /*
  1733. * Walk the registration list..
  1734. */
  1735. spin_lock(&su_dev->t10_pr.registration_lock);
  1736. list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
  1737. pr_reg_list) {
  1738. tmp[0] = '\0';
  1739. isid_buf[0] = '\0';
  1740. tpg = pr_reg->pr_reg_nacl->se_tpg;
  1741. lun = pr_reg->pr_reg_tg_pt_lun;
  1742. /*
  1743. * Write out any ISID value to APTPL metadata that was included
  1744. * in the original registration.
  1745. */
  1746. if (pr_reg->isid_present_at_reg)
  1747. snprintf(isid_buf, 32, "initiator_sid=%s\n",
  1748. pr_reg->pr_reg_isid);
  1749. /*
  1750. * Include special metadata if the pr_reg matches the
  1751. * reservation holder.
  1752. */
  1753. if (dev->dev_pr_res_holder == pr_reg) {
  1754. snprintf(tmp, 512, "PR_REG_START: %d"
  1755. "\ninitiator_fabric=%s\n"
  1756. "initiator_node=%s\n%s"
  1757. "sa_res_key=%llu\n"
  1758. "res_holder=1\nres_type=%02x\n"
  1759. "res_scope=%02x\nres_all_tg_pt=%d\n"
  1760. "mapped_lun=%u\n", reg_count,
  1761. tpg->se_tpg_tfo->get_fabric_name(),
  1762. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1763. pr_reg->pr_res_key, pr_reg->pr_res_type,
  1764. pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
  1765. pr_reg->pr_res_mapped_lun);
  1766. } else {
  1767. snprintf(tmp, 512, "PR_REG_START: %d\n"
  1768. "initiator_fabric=%s\ninitiator_node=%s\n%s"
  1769. "sa_res_key=%llu\nres_holder=0\n"
  1770. "res_all_tg_pt=%d\nmapped_lun=%u\n",
  1771. reg_count, tpg->se_tpg_tfo->get_fabric_name(),
  1772. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1773. pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
  1774. pr_reg->pr_res_mapped_lun);
  1775. }
  1776. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1777. pr_err("Unable to update renaming"
  1778. " APTPL metadata\n");
  1779. spin_unlock(&su_dev->t10_pr.registration_lock);
  1780. return -EMSGSIZE;
  1781. }
  1782. len += sprintf(buf+len, "%s", tmp);
  1783. /*
  1784. * Include information about the associated SCSI target port.
  1785. */
  1786. snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
  1787. "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
  1788. " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
  1789. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1790. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1791. lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
  1792. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1793. pr_err("Unable to update renaming"
  1794. " APTPL metadata\n");
  1795. spin_unlock(&su_dev->t10_pr.registration_lock);
  1796. return -EMSGSIZE;
  1797. }
  1798. len += sprintf(buf+len, "%s", tmp);
  1799. reg_count++;
  1800. }
  1801. spin_unlock(&su_dev->t10_pr.registration_lock);
  1802. if (!reg_count)
  1803. len += sprintf(buf+len, "No Registrations or Reservations");
  1804. return 0;
  1805. }
  1806. static int core_scsi3_update_aptpl_buf(
  1807. struct se_device *dev,
  1808. unsigned char *buf,
  1809. u32 pr_aptpl_buf_len,
  1810. int clear_aptpl_metadata)
  1811. {
  1812. int ret;
  1813. spin_lock(&dev->dev_reservation_lock);
  1814. ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
  1815. clear_aptpl_metadata);
  1816. spin_unlock(&dev->dev_reservation_lock);
  1817. return ret;
  1818. }
  1819. /*
  1820. * Called with struct se_device->aptpl_file_mutex held
  1821. */
  1822. static int __core_scsi3_write_aptpl_to_file(
  1823. struct se_device *dev,
  1824. unsigned char *buf,
  1825. u32 pr_aptpl_buf_len)
  1826. {
  1827. struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
  1828. struct file *file;
  1829. struct iovec iov[1];
  1830. mm_segment_t old_fs;
  1831. int flags = O_RDWR | O_CREAT | O_TRUNC;
  1832. char path[512];
  1833. int ret;
  1834. memset(iov, 0, sizeof(struct iovec));
  1835. memset(path, 0, 512);
  1836. if (strlen(&wwn->unit_serial[0]) >= 512) {
  1837. pr_err("WWN value for struct se_device does not fit"
  1838. " into path buffer\n");
  1839. return -EMSGSIZE;
  1840. }
  1841. snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
  1842. file = filp_open(path, flags, 0600);
  1843. if (IS_ERR(file) || !file || !file->f_dentry) {
  1844. pr_err("filp_open(%s) for APTPL metadata"
  1845. " failed\n", path);
  1846. return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
  1847. }
  1848. iov[0].iov_base = &buf[0];
  1849. if (!pr_aptpl_buf_len)
  1850. iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
  1851. else
  1852. iov[0].iov_len = pr_aptpl_buf_len;
  1853. old_fs = get_fs();
  1854. set_fs(get_ds());
  1855. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  1856. set_fs(old_fs);
  1857. if (ret < 0) {
  1858. pr_debug("Error writing APTPL metadata file: %s\n", path);
  1859. filp_close(file, NULL);
  1860. return -EIO;
  1861. }
  1862. filp_close(file, NULL);
  1863. return 0;
  1864. }
  1865. static int core_scsi3_update_and_write_aptpl(
  1866. struct se_device *dev,
  1867. unsigned char *in_buf,
  1868. u32 in_pr_aptpl_buf_len)
  1869. {
  1870. unsigned char null_buf[64], *buf;
  1871. u32 pr_aptpl_buf_len;
  1872. int ret, clear_aptpl_metadata = 0;
  1873. /*
  1874. * Can be called with a NULL pointer from PROUT service action CLEAR
  1875. */
  1876. if (!in_buf) {
  1877. memset(null_buf, 0, 64);
  1878. buf = &null_buf[0];
  1879. /*
  1880. * This will clear the APTPL metadata to:
  1881. * "No Registrations or Reservations" status
  1882. */
  1883. pr_aptpl_buf_len = 64;
  1884. clear_aptpl_metadata = 1;
  1885. } else {
  1886. buf = in_buf;
  1887. pr_aptpl_buf_len = in_pr_aptpl_buf_len;
  1888. }
  1889. ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
  1890. clear_aptpl_metadata);
  1891. if (ret != 0)
  1892. return ret;
  1893. /*
  1894. * __core_scsi3_write_aptpl_to_file() will call strlen()
  1895. * on the passed buf to determine pr_aptpl_buf_len.
  1896. */
  1897. ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
  1898. if (ret != 0)
  1899. return ret;
  1900. return ret;
  1901. }
  1902. static int core_scsi3_emulate_pro_register(
  1903. struct se_cmd *cmd,
  1904. u64 res_key,
  1905. u64 sa_res_key,
  1906. int aptpl,
  1907. int all_tg_pt,
  1908. int spec_i_pt,
  1909. int ignore_key)
  1910. {
  1911. struct se_session *se_sess = cmd->se_sess;
  1912. struct se_device *dev = cmd->se_dev;
  1913. struct se_dev_entry *se_deve;
  1914. struct se_lun *se_lun = cmd->se_lun;
  1915. struct se_portal_group *se_tpg;
  1916. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
  1917. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  1918. /* Used for APTPL metadata w/ UNREGISTER */
  1919. unsigned char *pr_aptpl_buf = NULL;
  1920. unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
  1921. int pr_holder = 0, ret = 0, type;
  1922. if (!se_sess || !se_lun) {
  1923. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  1924. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1925. return -EINVAL;
  1926. }
  1927. se_tpg = se_sess->se_tpg;
  1928. se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  1929. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
  1930. memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
  1931. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
  1932. PR_REG_ISID_LEN);
  1933. isid_ptr = &isid_buf[0];
  1934. }
  1935. /*
  1936. * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
  1937. */
  1938. pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  1939. if (!pr_reg_e) {
  1940. if (res_key) {
  1941. pr_warn("SPC-3 PR: Reservation Key non-zero"
  1942. " for SA REGISTER, returning CONFLICT\n");
  1943. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  1944. return -EINVAL;
  1945. }
  1946. /*
  1947. * Do nothing but return GOOD status.
  1948. */
  1949. if (!sa_res_key)
  1950. return 0;
  1951. if (!spec_i_pt) {
  1952. /*
  1953. * Perform the Service Action REGISTER on the Initiator
  1954. * Port Endpoint that the PRO was received from on the
  1955. * Logical Unit of the SCSI device server.
  1956. */
  1957. ret = core_scsi3_alloc_registration(cmd->se_dev,
  1958. se_sess->se_node_acl, se_deve, isid_ptr,
  1959. sa_res_key, all_tg_pt, aptpl,
  1960. ignore_key, 0);
  1961. if (ret != 0) {
  1962. pr_err("Unable to allocate"
  1963. " struct t10_pr_registration\n");
  1964. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1965. return -EINVAL;
  1966. }
  1967. } else {
  1968. /*
  1969. * Register both the Initiator port that received
  1970. * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
  1971. * TransportID from Parameter list and loop through
  1972. * fabric dependent parameter list while calling
  1973. * logic from of core_scsi3_alloc_registration() for
  1974. * each TransportID provided SCSI Initiator Port/Device
  1975. */
  1976. ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
  1977. isid_ptr, sa_res_key, all_tg_pt, aptpl);
  1978. if (ret != 0)
  1979. return ret;
  1980. }
  1981. /*
  1982. * Nothing left to do for the APTPL=0 case.
  1983. */
  1984. if (!aptpl) {
  1985. pr_tmpl->pr_aptpl_active = 0;
  1986. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  1987. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
  1988. " REGISTER\n");
  1989. return 0;
  1990. }
  1991. /*
  1992. * Locate the newly allocated local I_T Nexus *pr_reg, and
  1993. * update the APTPL metadata information using its
  1994. * preallocated *pr_reg->pr_aptpl_buf.
  1995. */
  1996. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
  1997. se_sess->se_node_acl, se_sess);
  1998. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  1999. &pr_reg->pr_aptpl_buf[0],
  2000. pr_tmpl->pr_aptpl_buf_len);
  2001. if (!ret) {
  2002. pr_tmpl->pr_aptpl_active = 1;
  2003. pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
  2004. }
  2005. core_scsi3_put_pr_reg(pr_reg);
  2006. return ret;
  2007. } else {
  2008. /*
  2009. * Locate the existing *pr_reg via struct se_node_acl pointers
  2010. */
  2011. pr_reg = pr_reg_e;
  2012. type = pr_reg->pr_res_type;
  2013. if (!ignore_key) {
  2014. if (res_key != pr_reg->pr_res_key) {
  2015. pr_err("SPC-3 PR REGISTER: Received"
  2016. " res_key: 0x%016Lx does not match"
  2017. " existing SA REGISTER res_key:"
  2018. " 0x%016Lx\n", res_key,
  2019. pr_reg->pr_res_key);
  2020. core_scsi3_put_pr_reg(pr_reg);
  2021. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2022. return -EINVAL;
  2023. }
  2024. }
  2025. if (spec_i_pt) {
  2026. pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
  2027. " set while sa_res_key=0\n");
  2028. core_scsi3_put_pr_reg(pr_reg);
  2029. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2030. return -EINVAL;
  2031. }
  2032. /*
  2033. * An existing ALL_TG_PT=1 registration being released
  2034. * must also set ALL_TG_PT=1 in the incoming PROUT.
  2035. */
  2036. if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
  2037. pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
  2038. " registration exists, but ALL_TG_PT=1 bit not"
  2039. " present in received PROUT\n");
  2040. core_scsi3_put_pr_reg(pr_reg);
  2041. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2042. return -EINVAL;
  2043. }
  2044. /*
  2045. * Allocate APTPL metadata buffer used for UNREGISTER ops
  2046. */
  2047. if (aptpl) {
  2048. pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
  2049. GFP_KERNEL);
  2050. if (!pr_aptpl_buf) {
  2051. pr_err("Unable to allocate"
  2052. " pr_aptpl_buf\n");
  2053. core_scsi3_put_pr_reg(pr_reg);
  2054. cmd->scsi_sense_reason =
  2055. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2056. return -EINVAL;
  2057. }
  2058. }
  2059. /*
  2060. * sa_res_key=0 Unregister Reservation Key for registered I_T
  2061. * Nexus sa_res_key=1 Change Reservation Key for registered I_T
  2062. * Nexus.
  2063. */
  2064. if (!sa_res_key) {
  2065. pr_holder = core_scsi3_check_implict_release(
  2066. cmd->se_dev, pr_reg);
  2067. if (pr_holder < 0) {
  2068. kfree(pr_aptpl_buf);
  2069. core_scsi3_put_pr_reg(pr_reg);
  2070. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2071. return -EINVAL;
  2072. }
  2073. spin_lock(&pr_tmpl->registration_lock);
  2074. /*
  2075. * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
  2076. * and matching pr_res_key.
  2077. */
  2078. if (pr_reg->pr_reg_all_tg_pt) {
  2079. list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
  2080. &pr_tmpl->registration_list,
  2081. pr_reg_list) {
  2082. if (!pr_reg_p->pr_reg_all_tg_pt)
  2083. continue;
  2084. if (pr_reg_p->pr_res_key != res_key)
  2085. continue;
  2086. if (pr_reg == pr_reg_p)
  2087. continue;
  2088. if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
  2089. pr_reg_p->pr_reg_nacl->initiatorname))
  2090. continue;
  2091. __core_scsi3_free_registration(dev,
  2092. pr_reg_p, NULL, 0);
  2093. }
  2094. }
  2095. /*
  2096. * Release the calling I_T Nexus registration now..
  2097. */
  2098. __core_scsi3_free_registration(cmd->se_dev, pr_reg,
  2099. NULL, 1);
  2100. /*
  2101. * From spc4r17, section 5.7.11.3 Unregistering
  2102. *
  2103. * If the persistent reservation is a registrants only
  2104. * type, the device server shall establish a unit
  2105. * attention condition for the initiator port associated
  2106. * with every registered I_T nexus except for the I_T
  2107. * nexus on which the PERSISTENT RESERVE OUT command was
  2108. * received, with the additional sense code set to
  2109. * RESERVATIONS RELEASED.
  2110. */
  2111. if (pr_holder &&
  2112. ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
  2113. (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
  2114. list_for_each_entry(pr_reg_p,
  2115. &pr_tmpl->registration_list,
  2116. pr_reg_list) {
  2117. core_scsi3_ua_allocate(
  2118. pr_reg_p->pr_reg_nacl,
  2119. pr_reg_p->pr_res_mapped_lun,
  2120. 0x2A,
  2121. ASCQ_2AH_RESERVATIONS_RELEASED);
  2122. }
  2123. }
  2124. spin_unlock(&pr_tmpl->registration_lock);
  2125. if (!aptpl) {
  2126. pr_tmpl->pr_aptpl_active = 0;
  2127. core_scsi3_update_and_write_aptpl(dev, NULL, 0);
  2128. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
  2129. " for UNREGISTER\n");
  2130. return 0;
  2131. }
  2132. ret = core_scsi3_update_and_write_aptpl(dev,
  2133. &pr_aptpl_buf[0],
  2134. pr_tmpl->pr_aptpl_buf_len);
  2135. if (!ret) {
  2136. pr_tmpl->pr_aptpl_active = 1;
  2137. pr_debug("SPC-3 PR: Set APTPL Bit Activated"
  2138. " for UNREGISTER\n");
  2139. }
  2140. kfree(pr_aptpl_buf);
  2141. return ret;
  2142. } else {
  2143. /*
  2144. * Increment PRgeneration counter for struct se_device"
  2145. * upon a successful REGISTER, see spc4r17 section 6.3.2
  2146. * READ_KEYS service action.
  2147. */
  2148. pr_reg->pr_res_generation = core_scsi3_pr_generation(
  2149. cmd->se_dev);
  2150. pr_reg->pr_res_key = sa_res_key;
  2151. pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
  2152. " Key for %s to: 0x%016Lx PRgeneration:"
  2153. " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
  2154. (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
  2155. pr_reg->pr_reg_nacl->initiatorname,
  2156. pr_reg->pr_res_key, pr_reg->pr_res_generation);
  2157. if (!aptpl) {
  2158. pr_tmpl->pr_aptpl_active = 0;
  2159. core_scsi3_update_and_write_aptpl(dev, NULL, 0);
  2160. core_scsi3_put_pr_reg(pr_reg);
  2161. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
  2162. " for REGISTER\n");
  2163. return 0;
  2164. }
  2165. ret = core_scsi3_update_and_write_aptpl(dev,
  2166. &pr_aptpl_buf[0],
  2167. pr_tmpl->pr_aptpl_buf_len);
  2168. if (!ret) {
  2169. pr_tmpl->pr_aptpl_active = 1;
  2170. pr_debug("SPC-3 PR: Set APTPL Bit Activated"
  2171. " for REGISTER\n");
  2172. }
  2173. kfree(pr_aptpl_buf);
  2174. core_scsi3_put_pr_reg(pr_reg);
  2175. }
  2176. }
  2177. return 0;
  2178. }
  2179. unsigned char *core_scsi3_pr_dump_type(int type)
  2180. {
  2181. switch (type) {
  2182. case PR_TYPE_WRITE_EXCLUSIVE:
  2183. return "Write Exclusive Access";
  2184. case PR_TYPE_EXCLUSIVE_ACCESS:
  2185. return "Exclusive Access";
  2186. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2187. return "Write Exclusive Access, Registrants Only";
  2188. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2189. return "Exclusive Access, Registrants Only";
  2190. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2191. return "Write Exclusive Access, All Registrants";
  2192. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2193. return "Exclusive Access, All Registrants";
  2194. default:
  2195. break;
  2196. }
  2197. return "Unknown SPC-3 PR Type";
  2198. }
  2199. static int core_scsi3_pro_reserve(
  2200. struct se_cmd *cmd,
  2201. struct se_device *dev,
  2202. int type,
  2203. int scope,
  2204. u64 res_key)
  2205. {
  2206. struct se_session *se_sess = cmd->se_sess;
  2207. struct se_lun *se_lun = cmd->se_lun;
  2208. struct t10_pr_registration *pr_reg, *pr_res_holder;
  2209. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2210. char i_buf[PR_REG_ISID_ID_LEN];
  2211. int ret, prf_isid;
  2212. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2213. if (!se_sess || !se_lun) {
  2214. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2215. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2216. return -EINVAL;
  2217. }
  2218. /*
  2219. * Locate the existing *pr_reg via struct se_node_acl pointers
  2220. */
  2221. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2222. se_sess);
  2223. if (!pr_reg) {
  2224. pr_err("SPC-3 PR: Unable to locate"
  2225. " PR_REGISTERED *pr_reg for RESERVE\n");
  2226. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2227. return -EINVAL;
  2228. }
  2229. /*
  2230. * From spc4r17 Section 5.7.9: Reserving:
  2231. *
  2232. * An application client creates a persistent reservation by issuing
  2233. * a PERSISTENT RESERVE OUT command with RESERVE service action through
  2234. * a registered I_T nexus with the following parameters:
  2235. * a) RESERVATION KEY set to the value of the reservation key that is
  2236. * registered with the logical unit for the I_T nexus; and
  2237. */
  2238. if (res_key != pr_reg->pr_res_key) {
  2239. pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
  2240. " does not match existing SA REGISTER res_key:"
  2241. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2242. core_scsi3_put_pr_reg(pr_reg);
  2243. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2244. return -EINVAL;
  2245. }
  2246. /*
  2247. * From spc4r17 Section 5.7.9: Reserving:
  2248. *
  2249. * From above:
  2250. * b) TYPE field and SCOPE field set to the persistent reservation
  2251. * being created.
  2252. *
  2253. * Only one persistent reservation is allowed at a time per logical unit
  2254. * and that persistent reservation has a scope of LU_SCOPE.
  2255. */
  2256. if (scope != PR_SCOPE_LU_SCOPE) {
  2257. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2258. core_scsi3_put_pr_reg(pr_reg);
  2259. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2260. return -EINVAL;
  2261. }
  2262. /*
  2263. * See if we have an existing PR reservation holder pointer at
  2264. * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
  2265. * *pr_res_holder.
  2266. */
  2267. spin_lock(&dev->dev_reservation_lock);
  2268. pr_res_holder = dev->dev_pr_res_holder;
  2269. if ((pr_res_holder)) {
  2270. /*
  2271. * From spc4r17 Section 5.7.9: Reserving:
  2272. *
  2273. * If the device server receives a PERSISTENT RESERVE OUT
  2274. * command from an I_T nexus other than a persistent reservation
  2275. * holder (see 5.7.10) that attempts to create a persistent
  2276. * reservation when a persistent reservation already exists for
  2277. * the logical unit, then the command shall be completed with
  2278. * RESERVATION CONFLICT status.
  2279. */
  2280. if (pr_res_holder != pr_reg) {
  2281. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2282. pr_err("SPC-3 PR: Attempted RESERVE from"
  2283. " [%s]: %s while reservation already held by"
  2284. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2285. cmd->se_tfo->get_fabric_name(),
  2286. se_sess->se_node_acl->initiatorname,
  2287. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2288. pr_res_holder->pr_reg_nacl->initiatorname);
  2289. spin_unlock(&dev->dev_reservation_lock);
  2290. core_scsi3_put_pr_reg(pr_reg);
  2291. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2292. return -EINVAL;
  2293. }
  2294. /*
  2295. * From spc4r17 Section 5.7.9: Reserving:
  2296. *
  2297. * If a persistent reservation holder attempts to modify the
  2298. * type or scope of an existing persistent reservation, the
  2299. * command shall be completed with RESERVATION CONFLICT status.
  2300. */
  2301. if ((pr_res_holder->pr_res_type != type) ||
  2302. (pr_res_holder->pr_res_scope != scope)) {
  2303. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2304. pr_err("SPC-3 PR: Attempted RESERVE from"
  2305. " [%s]: %s trying to change TYPE and/or SCOPE,"
  2306. " while reservation already held by [%s]: %s,"
  2307. " returning RESERVATION_CONFLICT\n",
  2308. cmd->se_tfo->get_fabric_name(),
  2309. se_sess->se_node_acl->initiatorname,
  2310. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2311. pr_res_holder->pr_reg_nacl->initiatorname);
  2312. spin_unlock(&dev->dev_reservation_lock);
  2313. core_scsi3_put_pr_reg(pr_reg);
  2314. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2315. return -EINVAL;
  2316. }
  2317. /*
  2318. * From spc4r17 Section 5.7.9: Reserving:
  2319. *
  2320. * If the device server receives a PERSISTENT RESERVE OUT
  2321. * command with RESERVE service action where the TYPE field and
  2322. * the SCOPE field contain the same values as the existing type
  2323. * and scope from a persistent reservation holder, it shall not
  2324. * make any change to the existing persistent reservation and
  2325. * shall completethe command with GOOD status.
  2326. */
  2327. spin_unlock(&dev->dev_reservation_lock);
  2328. core_scsi3_put_pr_reg(pr_reg);
  2329. return 0;
  2330. }
  2331. /*
  2332. * Otherwise, our *pr_reg becomes the PR reservation holder for said
  2333. * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
  2334. */
  2335. pr_reg->pr_res_scope = scope;
  2336. pr_reg->pr_res_type = type;
  2337. pr_reg->pr_res_holder = 1;
  2338. dev->dev_pr_res_holder = pr_reg;
  2339. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2340. PR_REG_ISID_ID_LEN);
  2341. pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
  2342. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2343. cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
  2344. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2345. pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
  2346. cmd->se_tfo->get_fabric_name(),
  2347. se_sess->se_node_acl->initiatorname,
  2348. (prf_isid) ? &i_buf[0] : "");
  2349. spin_unlock(&dev->dev_reservation_lock);
  2350. if (pr_tmpl->pr_aptpl_active) {
  2351. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2352. &pr_reg->pr_aptpl_buf[0],
  2353. pr_tmpl->pr_aptpl_buf_len);
  2354. if (!ret)
  2355. pr_debug("SPC-3 PR: Updated APTPL metadata"
  2356. " for RESERVE\n");
  2357. }
  2358. core_scsi3_put_pr_reg(pr_reg);
  2359. return 0;
  2360. }
  2361. static int core_scsi3_emulate_pro_reserve(
  2362. struct se_cmd *cmd,
  2363. int type,
  2364. int scope,
  2365. u64 res_key)
  2366. {
  2367. struct se_device *dev = cmd->se_dev;
  2368. int ret = 0;
  2369. switch (type) {
  2370. case PR_TYPE_WRITE_EXCLUSIVE:
  2371. case PR_TYPE_EXCLUSIVE_ACCESS:
  2372. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2373. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2374. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2375. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2376. ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
  2377. break;
  2378. default:
  2379. pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
  2380. " 0x%02x\n", type);
  2381. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2382. return -EINVAL;
  2383. }
  2384. return ret;
  2385. }
  2386. /*
  2387. * Called with struct se_device->dev_reservation_lock held.
  2388. */
  2389. static void __core_scsi3_complete_pro_release(
  2390. struct se_device *dev,
  2391. struct se_node_acl *se_nacl,
  2392. struct t10_pr_registration *pr_reg,
  2393. int explict)
  2394. {
  2395. struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
  2396. char i_buf[PR_REG_ISID_ID_LEN];
  2397. int prf_isid;
  2398. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2399. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2400. PR_REG_ISID_ID_LEN);
  2401. /*
  2402. * Go ahead and release the current PR reservation holder.
  2403. */
  2404. dev->dev_pr_res_holder = NULL;
  2405. pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
  2406. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2407. tfo->get_fabric_name(), (explict) ? "explict" : "implict",
  2408. core_scsi3_pr_dump_type(pr_reg->pr_res_type),
  2409. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2410. pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
  2411. tfo->get_fabric_name(), se_nacl->initiatorname,
  2412. (prf_isid) ? &i_buf[0] : "");
  2413. /*
  2414. * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
  2415. */
  2416. pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
  2417. }
  2418. static int core_scsi3_emulate_pro_release(
  2419. struct se_cmd *cmd,
  2420. int type,
  2421. int scope,
  2422. u64 res_key)
  2423. {
  2424. struct se_device *dev = cmd->se_dev;
  2425. struct se_session *se_sess = cmd->se_sess;
  2426. struct se_lun *se_lun = cmd->se_lun;
  2427. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
  2428. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2429. int ret, all_reg = 0;
  2430. if (!se_sess || !se_lun) {
  2431. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2432. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2433. return -EINVAL;
  2434. }
  2435. /*
  2436. * Locate the existing *pr_reg via struct se_node_acl pointers
  2437. */
  2438. pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  2439. if (!pr_reg) {
  2440. pr_err("SPC-3 PR: Unable to locate"
  2441. " PR_REGISTERED *pr_reg for RELEASE\n");
  2442. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2443. return -EINVAL;
  2444. }
  2445. /*
  2446. * From spc4r17 Section 5.7.11.2 Releasing:
  2447. *
  2448. * If there is no persistent reservation or in response to a persistent
  2449. * reservation release request from a registered I_T nexus that is not a
  2450. * persistent reservation holder (see 5.7.10), the device server shall
  2451. * do the following:
  2452. *
  2453. * a) Not release the persistent reservation, if any;
  2454. * b) Not remove any registrations; and
  2455. * c) Complete the command with GOOD status.
  2456. */
  2457. spin_lock(&dev->dev_reservation_lock);
  2458. pr_res_holder = dev->dev_pr_res_holder;
  2459. if (!pr_res_holder) {
  2460. /*
  2461. * No persistent reservation, return GOOD status.
  2462. */
  2463. spin_unlock(&dev->dev_reservation_lock);
  2464. core_scsi3_put_pr_reg(pr_reg);
  2465. return 0;
  2466. }
  2467. if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2468. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
  2469. all_reg = 1;
  2470. if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
  2471. /*
  2472. * Non 'All Registrants' PR Type cases..
  2473. * Release request from a registered I_T nexus that is not a
  2474. * persistent reservation holder. return GOOD status.
  2475. */
  2476. spin_unlock(&dev->dev_reservation_lock);
  2477. core_scsi3_put_pr_reg(pr_reg);
  2478. return 0;
  2479. }
  2480. /*
  2481. * From spc4r17 Section 5.7.11.2 Releasing:
  2482. *
  2483. * Only the persistent reservation holder (see 5.7.10) is allowed to
  2484. * release a persistent reservation.
  2485. *
  2486. * An application client releases the persistent reservation by issuing
  2487. * a PERSISTENT RESERVE OUT command with RELEASE service action through
  2488. * an I_T nexus that is a persistent reservation holder with the
  2489. * following parameters:
  2490. *
  2491. * a) RESERVATION KEY field set to the value of the reservation key
  2492. * that is registered with the logical unit for the I_T nexus;
  2493. */
  2494. if (res_key != pr_reg->pr_res_key) {
  2495. pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
  2496. " does not match existing SA REGISTER res_key:"
  2497. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2498. spin_unlock(&dev->dev_reservation_lock);
  2499. core_scsi3_put_pr_reg(pr_reg);
  2500. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2501. return -EINVAL;
  2502. }
  2503. /*
  2504. * From spc4r17 Section 5.7.11.2 Releasing and above:
  2505. *
  2506. * b) TYPE field and SCOPE field set to match the persistent
  2507. * reservation being released.
  2508. */
  2509. if ((pr_res_holder->pr_res_type != type) ||
  2510. (pr_res_holder->pr_res_scope != scope)) {
  2511. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2512. pr_err("SPC-3 PR RELEASE: Attempted to release"
  2513. " reservation from [%s]: %s with different TYPE "
  2514. "and/or SCOPE while reservation already held by"
  2515. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2516. cmd->se_tfo->get_fabric_name(),
  2517. se_sess->se_node_acl->initiatorname,
  2518. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2519. pr_res_holder->pr_reg_nacl->initiatorname);
  2520. spin_unlock(&dev->dev_reservation_lock);
  2521. core_scsi3_put_pr_reg(pr_reg);
  2522. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2523. return -EINVAL;
  2524. }
  2525. /*
  2526. * In response to a persistent reservation release request from the
  2527. * persistent reservation holder the device server shall perform a
  2528. * release by doing the following as an uninterrupted series of actions:
  2529. * a) Release the persistent reservation;
  2530. * b) Not remove any registration(s);
  2531. * c) If the released persistent reservation is a registrants only type
  2532. * or all registrants type persistent reservation,
  2533. * the device server shall establish a unit attention condition for
  2534. * the initiator port associated with every regis-
  2535. * tered I_T nexus other than I_T nexus on which the PERSISTENT
  2536. * RESERVE OUT command with RELEASE service action was received,
  2537. * with the additional sense code set to RESERVATIONS RELEASED; and
  2538. * d) If the persistent reservation is of any other type, the device
  2539. * server shall not establish a unit attention condition.
  2540. */
  2541. __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
  2542. pr_reg, 1);
  2543. spin_unlock(&dev->dev_reservation_lock);
  2544. if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
  2545. (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
  2546. (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
  2547. (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  2548. /*
  2549. * If no UNIT ATTENTION conditions will be established for
  2550. * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
  2551. * go ahead and check for APTPL=1 update+write below
  2552. */
  2553. goto write_aptpl;
  2554. }
  2555. spin_lock(&pr_tmpl->registration_lock);
  2556. list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
  2557. pr_reg_list) {
  2558. /*
  2559. * Do not establish a UNIT ATTENTION condition
  2560. * for the calling I_T Nexus
  2561. */
  2562. if (pr_reg_p == pr_reg)
  2563. continue;
  2564. core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
  2565. pr_reg_p->pr_res_mapped_lun,
  2566. 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
  2567. }
  2568. spin_unlock(&pr_tmpl->registration_lock);
  2569. write_aptpl:
  2570. if (pr_tmpl->pr_aptpl_active) {
  2571. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2572. &pr_reg->pr_aptpl_buf[0],
  2573. pr_tmpl->pr_aptpl_buf_len);
  2574. if (!ret)
  2575. pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
  2576. }
  2577. core_scsi3_put_pr_reg(pr_reg);
  2578. return 0;
  2579. }
  2580. static int core_scsi3_emulate_pro_clear(
  2581. struct se_cmd *cmd,
  2582. u64 res_key)
  2583. {
  2584. struct se_device *dev = cmd->se_dev;
  2585. struct se_node_acl *pr_reg_nacl;
  2586. struct se_session *se_sess = cmd->se_sess;
  2587. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2588. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2589. u32 pr_res_mapped_lun = 0;
  2590. int calling_it_nexus = 0;
  2591. /*
  2592. * Locate the existing *pr_reg via struct se_node_acl pointers
  2593. */
  2594. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
  2595. se_sess->se_node_acl, se_sess);
  2596. if (!pr_reg_n) {
  2597. pr_err("SPC-3 PR: Unable to locate"
  2598. " PR_REGISTERED *pr_reg for CLEAR\n");
  2599. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2600. return -EINVAL;
  2601. }
  2602. /*
  2603. * From spc4r17 section 5.7.11.6, Clearing:
  2604. *
  2605. * Any application client may release the persistent reservation and
  2606. * remove all registrations from a device server by issuing a
  2607. * PERSISTENT RESERVE OUT command with CLEAR service action through a
  2608. * registered I_T nexus with the following parameter:
  2609. *
  2610. * a) RESERVATION KEY field set to the value of the reservation key
  2611. * that is registered with the logical unit for the I_T nexus.
  2612. */
  2613. if (res_key != pr_reg_n->pr_res_key) {
  2614. pr_err("SPC-3 PR REGISTER: Received"
  2615. " res_key: 0x%016Lx does not match"
  2616. " existing SA REGISTER res_key:"
  2617. " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
  2618. core_scsi3_put_pr_reg(pr_reg_n);
  2619. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2620. return -EINVAL;
  2621. }
  2622. /*
  2623. * a) Release the persistent reservation, if any;
  2624. */
  2625. spin_lock(&dev->dev_reservation_lock);
  2626. pr_res_holder = dev->dev_pr_res_holder;
  2627. if (pr_res_holder) {
  2628. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2629. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  2630. pr_res_holder, 0);
  2631. }
  2632. spin_unlock(&dev->dev_reservation_lock);
  2633. /*
  2634. * b) Remove all registration(s) (see spc4r17 5.7.7);
  2635. */
  2636. spin_lock(&pr_tmpl->registration_lock);
  2637. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2638. &pr_tmpl->registration_list, pr_reg_list) {
  2639. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2640. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2641. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2642. __core_scsi3_free_registration(dev, pr_reg, NULL,
  2643. calling_it_nexus);
  2644. /*
  2645. * e) Establish a unit attention condition for the initiator
  2646. * port associated with every registered I_T nexus other
  2647. * than the I_T nexus on which the PERSISTENT RESERVE OUT
  2648. * command with CLEAR service action was received, with the
  2649. * additional sense code set to RESERVATIONS PREEMPTED.
  2650. */
  2651. if (!calling_it_nexus)
  2652. core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
  2653. 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
  2654. }
  2655. spin_unlock(&pr_tmpl->registration_lock);
  2656. pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
  2657. cmd->se_tfo->get_fabric_name());
  2658. if (pr_tmpl->pr_aptpl_active) {
  2659. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  2660. pr_debug("SPC-3 PR: Updated APTPL metadata"
  2661. " for CLEAR\n");
  2662. }
  2663. core_scsi3_pr_generation(dev);
  2664. return 0;
  2665. }
  2666. /*
  2667. * Called with struct se_device->dev_reservation_lock held.
  2668. */
  2669. static void __core_scsi3_complete_pro_preempt(
  2670. struct se_device *dev,
  2671. struct t10_pr_registration *pr_reg,
  2672. struct list_head *preempt_and_abort_list,
  2673. int type,
  2674. int scope,
  2675. int abort)
  2676. {
  2677. struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
  2678. struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
  2679. char i_buf[PR_REG_ISID_ID_LEN];
  2680. int prf_isid;
  2681. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2682. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2683. PR_REG_ISID_ID_LEN);
  2684. /*
  2685. * Do an implict RELEASE of the existing reservation.
  2686. */
  2687. if (dev->dev_pr_res_holder)
  2688. __core_scsi3_complete_pro_release(dev, nacl,
  2689. dev->dev_pr_res_holder, 0);
  2690. dev->dev_pr_res_holder = pr_reg;
  2691. pr_reg->pr_res_holder = 1;
  2692. pr_reg->pr_res_type = type;
  2693. pr_reg->pr_res_scope = scope;
  2694. pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
  2695. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2696. tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
  2697. core_scsi3_pr_dump_type(type),
  2698. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2699. pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
  2700. tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
  2701. nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
  2702. /*
  2703. * For PREEMPT_AND_ABORT, add the preempting reservation's
  2704. * struct t10_pr_registration to the list that will be compared
  2705. * against received CDBs..
  2706. */
  2707. if (preempt_and_abort_list)
  2708. list_add_tail(&pr_reg->pr_reg_abort_list,
  2709. preempt_and_abort_list);
  2710. }
  2711. static void core_scsi3_release_preempt_and_abort(
  2712. struct list_head *preempt_and_abort_list,
  2713. struct t10_pr_registration *pr_reg_holder)
  2714. {
  2715. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  2716. list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
  2717. pr_reg_abort_list) {
  2718. list_del(&pr_reg->pr_reg_abort_list);
  2719. if (pr_reg_holder == pr_reg)
  2720. continue;
  2721. if (pr_reg->pr_res_holder) {
  2722. pr_warn("pr_reg->pr_res_holder still set\n");
  2723. continue;
  2724. }
  2725. pr_reg->pr_reg_deve = NULL;
  2726. pr_reg->pr_reg_nacl = NULL;
  2727. kfree(pr_reg->pr_aptpl_buf);
  2728. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  2729. }
  2730. }
  2731. static int core_scsi3_pro_preempt(
  2732. struct se_cmd *cmd,
  2733. int type,
  2734. int scope,
  2735. u64 res_key,
  2736. u64 sa_res_key,
  2737. int abort)
  2738. {
  2739. struct se_device *dev = cmd->se_dev;
  2740. struct se_node_acl *pr_reg_nacl;
  2741. struct se_session *se_sess = cmd->se_sess;
  2742. LIST_HEAD(preempt_and_abort_list);
  2743. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2744. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2745. u32 pr_res_mapped_lun = 0;
  2746. int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
  2747. int prh_type = 0, prh_scope = 0, ret;
  2748. if (!se_sess) {
  2749. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2750. return -EINVAL;
  2751. }
  2752. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2753. se_sess);
  2754. if (!pr_reg_n) {
  2755. pr_err("SPC-3 PR: Unable to locate"
  2756. " PR_REGISTERED *pr_reg for PREEMPT%s\n",
  2757. (abort) ? "_AND_ABORT" : "");
  2758. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2759. return -EINVAL;
  2760. }
  2761. if (pr_reg_n->pr_res_key != res_key) {
  2762. core_scsi3_put_pr_reg(pr_reg_n);
  2763. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2764. return -EINVAL;
  2765. }
  2766. if (scope != PR_SCOPE_LU_SCOPE) {
  2767. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2768. core_scsi3_put_pr_reg(pr_reg_n);
  2769. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2770. return -EINVAL;
  2771. }
  2772. spin_lock(&dev->dev_reservation_lock);
  2773. pr_res_holder = dev->dev_pr_res_holder;
  2774. if (pr_res_holder &&
  2775. ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2776. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
  2777. all_reg = 1;
  2778. if (!all_reg && !sa_res_key) {
  2779. spin_unlock(&dev->dev_reservation_lock);
  2780. core_scsi3_put_pr_reg(pr_reg_n);
  2781. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2782. return -EINVAL;
  2783. }
  2784. /*
  2785. * From spc4r17, section 5.7.11.4.4 Removing Registrations:
  2786. *
  2787. * If the SERVICE ACTION RESERVATION KEY field does not identify a
  2788. * persistent reservation holder or there is no persistent reservation
  2789. * holder (i.e., there is no persistent reservation), then the device
  2790. * server shall perform a preempt by doing the following in an
  2791. * uninterrupted series of actions. (See below..)
  2792. */
  2793. if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
  2794. /*
  2795. * No existing or SA Reservation Key matching reservations..
  2796. *
  2797. * PROUT SA PREEMPT with All Registrant type reservations are
  2798. * allowed to be processed without a matching SA Reservation Key
  2799. */
  2800. spin_lock(&pr_tmpl->registration_lock);
  2801. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2802. &pr_tmpl->registration_list, pr_reg_list) {
  2803. /*
  2804. * Removing of registrations in non all registrants
  2805. * type reservations without a matching SA reservation
  2806. * key.
  2807. *
  2808. * a) Remove the registrations for all I_T nexuses
  2809. * specified by the SERVICE ACTION RESERVATION KEY
  2810. * field;
  2811. * b) Ignore the contents of the SCOPE and TYPE fields;
  2812. * c) Process tasks as defined in 5.7.1; and
  2813. * d) Establish a unit attention condition for the
  2814. * initiator port associated with every I_T nexus
  2815. * that lost its registration other than the I_T
  2816. * nexus on which the PERSISTENT RESERVE OUT command
  2817. * was received, with the additional sense code set
  2818. * to REGISTRATIONS PREEMPTED.
  2819. */
  2820. if (!all_reg) {
  2821. if (pr_reg->pr_res_key != sa_res_key)
  2822. continue;
  2823. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2824. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2825. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2826. __core_scsi3_free_registration(dev, pr_reg,
  2827. (abort) ? &preempt_and_abort_list :
  2828. NULL, calling_it_nexus);
  2829. released_regs++;
  2830. } else {
  2831. /*
  2832. * Case for any existing all registrants type
  2833. * reservation, follow logic in spc4r17 section
  2834. * 5.7.11.4 Preempting, Table 52 and Figure 7.
  2835. *
  2836. * For a ZERO SA Reservation key, release
  2837. * all other registrations and do an implict
  2838. * release of active persistent reservation.
  2839. *
  2840. * For a non-ZERO SA Reservation key, only
  2841. * release the matching reservation key from
  2842. * registrations.
  2843. */
  2844. if ((sa_res_key) &&
  2845. (pr_reg->pr_res_key != sa_res_key))
  2846. continue;
  2847. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2848. if (calling_it_nexus)
  2849. continue;
  2850. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2851. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2852. __core_scsi3_free_registration(dev, pr_reg,
  2853. (abort) ? &preempt_and_abort_list :
  2854. NULL, 0);
  2855. released_regs++;
  2856. }
  2857. if (!calling_it_nexus)
  2858. core_scsi3_ua_allocate(pr_reg_nacl,
  2859. pr_res_mapped_lun, 0x2A,
  2860. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2861. }
  2862. spin_unlock(&pr_tmpl->registration_lock);
  2863. /*
  2864. * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
  2865. * a PREEMPT AND ABORT service action sets the SERVICE ACTION
  2866. * RESERVATION KEY field to a value that does not match any
  2867. * registered reservation key, then the device server shall
  2868. * complete the command with RESERVATION CONFLICT status.
  2869. */
  2870. if (!released_regs) {
  2871. spin_unlock(&dev->dev_reservation_lock);
  2872. core_scsi3_put_pr_reg(pr_reg_n);
  2873. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2874. return -EINVAL;
  2875. }
  2876. /*
  2877. * For an existing all registrants type reservation
  2878. * with a zero SA rservation key, preempt the existing
  2879. * reservation with the new PR type and scope.
  2880. */
  2881. if (pr_res_holder && all_reg && !(sa_res_key)) {
  2882. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2883. (abort) ? &preempt_and_abort_list : NULL,
  2884. type, scope, abort);
  2885. if (abort)
  2886. core_scsi3_release_preempt_and_abort(
  2887. &preempt_and_abort_list, pr_reg_n);
  2888. }
  2889. spin_unlock(&dev->dev_reservation_lock);
  2890. if (pr_tmpl->pr_aptpl_active) {
  2891. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2892. &pr_reg_n->pr_aptpl_buf[0],
  2893. pr_tmpl->pr_aptpl_buf_len);
  2894. if (!ret)
  2895. pr_debug("SPC-3 PR: Updated APTPL"
  2896. " metadata for PREEMPT%s\n", (abort) ?
  2897. "_AND_ABORT" : "");
  2898. }
  2899. core_scsi3_put_pr_reg(pr_reg_n);
  2900. core_scsi3_pr_generation(cmd->se_dev);
  2901. return 0;
  2902. }
  2903. /*
  2904. * The PREEMPTing SA reservation key matches that of the
  2905. * existing persistent reservation, first, we check if
  2906. * we are preempting our own reservation.
  2907. * From spc4r17, section 5.7.11.4.3 Preempting
  2908. * persistent reservations and registration handling
  2909. *
  2910. * If an all registrants persistent reservation is not
  2911. * present, it is not an error for the persistent
  2912. * reservation holder to preempt itself (i.e., a
  2913. * PERSISTENT RESERVE OUT with a PREEMPT service action
  2914. * or a PREEMPT AND ABORT service action with the
  2915. * SERVICE ACTION RESERVATION KEY value equal to the
  2916. * persistent reservation holder's reservation key that
  2917. * is received from the persistent reservation holder).
  2918. * In that case, the device server shall establish the
  2919. * new persistent reservation and maintain the
  2920. * registration.
  2921. */
  2922. prh_type = pr_res_holder->pr_res_type;
  2923. prh_scope = pr_res_holder->pr_res_scope;
  2924. /*
  2925. * If the SERVICE ACTION RESERVATION KEY field identifies a
  2926. * persistent reservation holder (see 5.7.10), the device
  2927. * server shall perform a preempt by doing the following as
  2928. * an uninterrupted series of actions:
  2929. *
  2930. * a) Release the persistent reservation for the holder
  2931. * identified by the SERVICE ACTION RESERVATION KEY field;
  2932. */
  2933. if (pr_reg_n != pr_res_holder)
  2934. __core_scsi3_complete_pro_release(dev,
  2935. pr_res_holder->pr_reg_nacl,
  2936. dev->dev_pr_res_holder, 0);
  2937. /*
  2938. * b) Remove the registrations for all I_T nexuses identified
  2939. * by the SERVICE ACTION RESERVATION KEY field, except the
  2940. * I_T nexus that is being used for the PERSISTENT RESERVE
  2941. * OUT command. If an all registrants persistent reservation
  2942. * is present and the SERVICE ACTION RESERVATION KEY field
  2943. * is set to zero, then all registrations shall be removed
  2944. * except for that of the I_T nexus that is being used for
  2945. * the PERSISTENT RESERVE OUT command;
  2946. */
  2947. spin_lock(&pr_tmpl->registration_lock);
  2948. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2949. &pr_tmpl->registration_list, pr_reg_list) {
  2950. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2951. if (calling_it_nexus)
  2952. continue;
  2953. if (pr_reg->pr_res_key != sa_res_key)
  2954. continue;
  2955. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2956. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2957. __core_scsi3_free_registration(dev, pr_reg,
  2958. (abort) ? &preempt_and_abort_list : NULL,
  2959. calling_it_nexus);
  2960. /*
  2961. * e) Establish a unit attention condition for the initiator
  2962. * port associated with every I_T nexus that lost its
  2963. * persistent reservation and/or registration, with the
  2964. * additional sense code set to REGISTRATIONS PREEMPTED;
  2965. */
  2966. core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
  2967. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2968. }
  2969. spin_unlock(&pr_tmpl->registration_lock);
  2970. /*
  2971. * c) Establish a persistent reservation for the preempting
  2972. * I_T nexus using the contents of the SCOPE and TYPE fields;
  2973. */
  2974. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2975. (abort) ? &preempt_and_abort_list : NULL,
  2976. type, scope, abort);
  2977. /*
  2978. * d) Process tasks as defined in 5.7.1;
  2979. * e) See above..
  2980. * f) If the type or scope has changed, then for every I_T nexus
  2981. * whose reservation key was not removed, except for the I_T
  2982. * nexus on which the PERSISTENT RESERVE OUT command was
  2983. * received, the device server shall establish a unit
  2984. * attention condition for the initiator port associated with
  2985. * that I_T nexus, with the additional sense code set to
  2986. * RESERVATIONS RELEASED. If the type or scope have not
  2987. * changed, then no unit attention condition(s) shall be
  2988. * established for this reason.
  2989. */
  2990. if ((prh_type != type) || (prh_scope != scope)) {
  2991. spin_lock(&pr_tmpl->registration_lock);
  2992. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2993. &pr_tmpl->registration_list, pr_reg_list) {
  2994. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2995. if (calling_it_nexus)
  2996. continue;
  2997. core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
  2998. pr_reg->pr_res_mapped_lun, 0x2A,
  2999. ASCQ_2AH_RESERVATIONS_RELEASED);
  3000. }
  3001. spin_unlock(&pr_tmpl->registration_lock);
  3002. }
  3003. spin_unlock(&dev->dev_reservation_lock);
  3004. /*
  3005. * Call LUN_RESET logic upon list of struct t10_pr_registration,
  3006. * All received CDBs for the matching existing reservation and
  3007. * registrations undergo ABORT_TASK logic.
  3008. *
  3009. * From there, core_scsi3_release_preempt_and_abort() will
  3010. * release every registration in the list (which have already
  3011. * been removed from the primary pr_reg list), except the
  3012. * new persistent reservation holder, the calling Initiator Port.
  3013. */
  3014. if (abort) {
  3015. core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
  3016. core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
  3017. pr_reg_n);
  3018. }
  3019. if (pr_tmpl->pr_aptpl_active) {
  3020. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  3021. &pr_reg_n->pr_aptpl_buf[0],
  3022. pr_tmpl->pr_aptpl_buf_len);
  3023. if (!ret)
  3024. pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
  3025. "%s\n", (abort) ? "_AND_ABORT" : "");
  3026. }
  3027. core_scsi3_put_pr_reg(pr_reg_n);
  3028. core_scsi3_pr_generation(cmd->se_dev);
  3029. return 0;
  3030. }
  3031. static int core_scsi3_emulate_pro_preempt(
  3032. struct se_cmd *cmd,
  3033. int type,
  3034. int scope,
  3035. u64 res_key,
  3036. u64 sa_res_key,
  3037. int abort)
  3038. {
  3039. int ret = 0;
  3040. switch (type) {
  3041. case PR_TYPE_WRITE_EXCLUSIVE:
  3042. case PR_TYPE_EXCLUSIVE_ACCESS:
  3043. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  3044. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  3045. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  3046. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  3047. ret = core_scsi3_pro_preempt(cmd, type, scope,
  3048. res_key, sa_res_key, abort);
  3049. break;
  3050. default:
  3051. pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
  3052. " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
  3053. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3054. return -EINVAL;
  3055. }
  3056. return ret;
  3057. }
  3058. static int core_scsi3_emulate_pro_register_and_move(
  3059. struct se_cmd *cmd,
  3060. u64 res_key,
  3061. u64 sa_res_key,
  3062. int aptpl,
  3063. int unreg)
  3064. {
  3065. struct se_session *se_sess = cmd->se_sess;
  3066. struct se_device *dev = cmd->se_dev;
  3067. struct se_dev_entry *dest_se_deve = NULL;
  3068. struct se_lun *se_lun = cmd->se_lun;
  3069. struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
  3070. struct se_port *se_port;
  3071. struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
  3072. struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
  3073. struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
  3074. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  3075. unsigned char *buf;
  3076. unsigned char *initiator_str;
  3077. char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
  3078. u32 tid_len, tmp_tid_len;
  3079. int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
  3080. unsigned short rtpi;
  3081. unsigned char proto_ident;
  3082. if (!se_sess || !se_lun) {
  3083. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  3084. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3085. return -EINVAL;
  3086. }
  3087. memset(dest_iport, 0, 64);
  3088. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  3089. se_tpg = se_sess->se_tpg;
  3090. tf_ops = se_tpg->se_tpg_tfo;
  3091. /*
  3092. * Follow logic from spc4r17 Section 5.7.8, Table 50 --
  3093. * Register behaviors for a REGISTER AND MOVE service action
  3094. *
  3095. * Locate the existing *pr_reg via struct se_node_acl pointers
  3096. */
  3097. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  3098. se_sess);
  3099. if (!pr_reg) {
  3100. pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
  3101. " *pr_reg for REGISTER_AND_MOVE\n");
  3102. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3103. return -EINVAL;
  3104. }
  3105. /*
  3106. * The provided reservation key much match the existing reservation key
  3107. * provided during this initiator's I_T nexus registration.
  3108. */
  3109. if (res_key != pr_reg->pr_res_key) {
  3110. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
  3111. " res_key: 0x%016Lx does not match existing SA REGISTER"
  3112. " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  3113. core_scsi3_put_pr_reg(pr_reg);
  3114. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3115. return -EINVAL;
  3116. }
  3117. /*
  3118. * The service active reservation key needs to be non zero
  3119. */
  3120. if (!sa_res_key) {
  3121. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
  3122. " sa_res_key\n");
  3123. core_scsi3_put_pr_reg(pr_reg);
  3124. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3125. return -EINVAL;
  3126. }
  3127. /*
  3128. * Determine the Relative Target Port Identifier where the reservation
  3129. * will be moved to for the TransportID containing SCSI initiator WWN
  3130. * information.
  3131. */
  3132. buf = transport_kmap_data_sg(cmd);
  3133. rtpi = (buf[18] & 0xff) << 8;
  3134. rtpi |= buf[19] & 0xff;
  3135. tid_len = (buf[20] & 0xff) << 24;
  3136. tid_len |= (buf[21] & 0xff) << 16;
  3137. tid_len |= (buf[22] & 0xff) << 8;
  3138. tid_len |= buf[23] & 0xff;
  3139. transport_kunmap_data_sg(cmd);
  3140. buf = NULL;
  3141. if ((tid_len + 24) != cmd->data_length) {
  3142. pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
  3143. " does not equal CDB data_length: %u\n", tid_len,
  3144. cmd->data_length);
  3145. core_scsi3_put_pr_reg(pr_reg);
  3146. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3147. return -EINVAL;
  3148. }
  3149. spin_lock(&dev->se_port_lock);
  3150. list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
  3151. if (se_port->sep_rtpi != rtpi)
  3152. continue;
  3153. dest_se_tpg = se_port->sep_tpg;
  3154. if (!dest_se_tpg)
  3155. continue;
  3156. dest_tf_ops = dest_se_tpg->se_tpg_tfo;
  3157. if (!dest_tf_ops)
  3158. continue;
  3159. atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
  3160. smp_mb__after_atomic_inc();
  3161. spin_unlock(&dev->se_port_lock);
  3162. ret = core_scsi3_tpg_depend_item(dest_se_tpg);
  3163. if (ret != 0) {
  3164. pr_err("core_scsi3_tpg_depend_item() failed"
  3165. " for dest_se_tpg\n");
  3166. atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
  3167. smp_mb__after_atomic_dec();
  3168. core_scsi3_put_pr_reg(pr_reg);
  3169. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3170. return -EINVAL;
  3171. }
  3172. spin_lock(&dev->se_port_lock);
  3173. break;
  3174. }
  3175. spin_unlock(&dev->se_port_lock);
  3176. if (!dest_se_tpg || !dest_tf_ops) {
  3177. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  3178. " fabric ops from Relative Target Port Identifier:"
  3179. " %hu\n", rtpi);
  3180. core_scsi3_put_pr_reg(pr_reg);
  3181. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3182. return -EINVAL;
  3183. }
  3184. buf = transport_kmap_data_sg(cmd);
  3185. proto_ident = (buf[24] & 0x0f);
  3186. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
  3187. " 0x%02x\n", proto_ident);
  3188. if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
  3189. pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
  3190. " proto_ident: 0x%02x does not match ident: 0x%02x"
  3191. " from fabric: %s\n", proto_ident,
  3192. dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
  3193. dest_tf_ops->get_fabric_name());
  3194. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3195. ret = -EINVAL;
  3196. goto out;
  3197. }
  3198. if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
  3199. pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
  3200. " containg a valid tpg_parse_pr_out_transport_id"
  3201. " function pointer\n");
  3202. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3203. ret = -EINVAL;
  3204. goto out;
  3205. }
  3206. initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
  3207. (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
  3208. if (!initiator_str) {
  3209. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  3210. " initiator_str from Transport ID\n");
  3211. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3212. ret = -EINVAL;
  3213. goto out;
  3214. }
  3215. transport_kunmap_data_sg(cmd);
  3216. buf = NULL;
  3217. pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
  3218. " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
  3219. "port" : "device", initiator_str, (iport_ptr != NULL) ?
  3220. iport_ptr : "");
  3221. /*
  3222. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3223. * action specifies a TransportID that is the same as the initiator port
  3224. * of the I_T nexus for the command received, then the command shall
  3225. * be terminated with CHECK CONDITION status, with the sense key set to
  3226. * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
  3227. * IN PARAMETER LIST.
  3228. */
  3229. pr_reg_nacl = pr_reg->pr_reg_nacl;
  3230. matching_iname = (!strcmp(initiator_str,
  3231. pr_reg_nacl->initiatorname)) ? 1 : 0;
  3232. if (!matching_iname)
  3233. goto after_iport_check;
  3234. if (!iport_ptr || !pr_reg->isid_present_at_reg) {
  3235. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
  3236. " matches: %s on received I_T Nexus\n", initiator_str,
  3237. pr_reg_nacl->initiatorname);
  3238. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3239. ret = -EINVAL;
  3240. goto out;
  3241. }
  3242. if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
  3243. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
  3244. " matches: %s %s on received I_T Nexus\n",
  3245. initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
  3246. pr_reg->pr_reg_isid);
  3247. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3248. ret = -EINVAL;
  3249. goto out;
  3250. }
  3251. after_iport_check:
  3252. /*
  3253. * Locate the destination struct se_node_acl from the received Transport ID
  3254. */
  3255. spin_lock_irq(&dest_se_tpg->acl_node_lock);
  3256. dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
  3257. initiator_str);
  3258. if (dest_node_acl) {
  3259. atomic_inc(&dest_node_acl->acl_pr_ref_count);
  3260. smp_mb__after_atomic_inc();
  3261. }
  3262. spin_unlock_irq(&dest_se_tpg->acl_node_lock);
  3263. if (!dest_node_acl) {
  3264. pr_err("Unable to locate %s dest_node_acl for"
  3265. " TransportID%s\n", dest_tf_ops->get_fabric_name(),
  3266. initiator_str);
  3267. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3268. ret = -EINVAL;
  3269. goto out;
  3270. }
  3271. ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
  3272. if (ret != 0) {
  3273. pr_err("core_scsi3_nodeacl_depend_item() for"
  3274. " dest_node_acl\n");
  3275. atomic_dec(&dest_node_acl->acl_pr_ref_count);
  3276. smp_mb__after_atomic_dec();
  3277. dest_node_acl = NULL;
  3278. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3279. ret = -EINVAL;
  3280. goto out;
  3281. }
  3282. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
  3283. " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
  3284. dest_node_acl->initiatorname);
  3285. /*
  3286. * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
  3287. * PORT IDENTIFIER.
  3288. */
  3289. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
  3290. if (!dest_se_deve) {
  3291. pr_err("Unable to locate %s dest_se_deve from RTPI:"
  3292. " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
  3293. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3294. ret = -EINVAL;
  3295. goto out;
  3296. }
  3297. ret = core_scsi3_lunacl_depend_item(dest_se_deve);
  3298. if (ret < 0) {
  3299. pr_err("core_scsi3_lunacl_depend_item() failed\n");
  3300. atomic_dec(&dest_se_deve->pr_ref_count);
  3301. smp_mb__after_atomic_dec();
  3302. dest_se_deve = NULL;
  3303. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3304. ret = -EINVAL;
  3305. goto out;
  3306. }
  3307. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
  3308. " ACL for dest_se_deve->mapped_lun: %u\n",
  3309. dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
  3310. dest_se_deve->mapped_lun);
  3311. /*
  3312. * A persistent reservation needs to already existing in order to
  3313. * successfully complete the REGISTER_AND_MOVE service action..
  3314. */
  3315. spin_lock(&dev->dev_reservation_lock);
  3316. pr_res_holder = dev->dev_pr_res_holder;
  3317. if (!pr_res_holder) {
  3318. pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
  3319. " currently held\n");
  3320. spin_unlock(&dev->dev_reservation_lock);
  3321. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3322. ret = -EINVAL;
  3323. goto out;
  3324. }
  3325. /*
  3326. * The received on I_T Nexus must be the reservation holder.
  3327. *
  3328. * From spc4r17 section 5.7.8 Table 50 --
  3329. * Register behaviors for a REGISTER AND MOVE service action
  3330. */
  3331. if (pr_res_holder != pr_reg) {
  3332. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
  3333. " Nexus is not reservation holder\n");
  3334. spin_unlock(&dev->dev_reservation_lock);
  3335. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3336. ret = -EINVAL;
  3337. goto out;
  3338. }
  3339. /*
  3340. * From spc4r17 section 5.7.8: registering and moving reservation
  3341. *
  3342. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3343. * action is received and the established persistent reservation is a
  3344. * Write Exclusive - All Registrants type or Exclusive Access -
  3345. * All Registrants type reservation, then the command shall be completed
  3346. * with RESERVATION CONFLICT status.
  3347. */
  3348. if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3349. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  3350. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
  3351. " reservation for type: %s\n",
  3352. core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
  3353. spin_unlock(&dev->dev_reservation_lock);
  3354. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3355. ret = -EINVAL;
  3356. goto out;
  3357. }
  3358. pr_res_nacl = pr_res_holder->pr_reg_nacl;
  3359. /*
  3360. * b) Ignore the contents of the (received) SCOPE and TYPE fields;
  3361. */
  3362. type = pr_res_holder->pr_res_type;
  3363. scope = pr_res_holder->pr_res_type;
  3364. /*
  3365. * c) Associate the reservation key specified in the SERVICE ACTION
  3366. * RESERVATION KEY field with the I_T nexus specified as the
  3367. * destination of the register and move, where:
  3368. * A) The I_T nexus is specified by the TransportID and the
  3369. * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
  3370. * B) Regardless of the TransportID format used, the association for
  3371. * the initiator port is based on either the initiator port name
  3372. * (see 3.1.71) on SCSI transport protocols where port names are
  3373. * required or the initiator port identifier (see 3.1.70) on SCSI
  3374. * transport protocols where port names are not required;
  3375. * d) Register the reservation key specified in the SERVICE ACTION
  3376. * RESERVATION KEY field;
  3377. * e) Retain the reservation key specified in the SERVICE ACTION
  3378. * RESERVATION KEY field and associated information;
  3379. *
  3380. * Also, It is not an error for a REGISTER AND MOVE service action to
  3381. * register an I_T nexus that is already registered with the same
  3382. * reservation key or a different reservation key.
  3383. */
  3384. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3385. iport_ptr);
  3386. if (!dest_pr_reg) {
  3387. ret = core_scsi3_alloc_registration(cmd->se_dev,
  3388. dest_node_acl, dest_se_deve, iport_ptr,
  3389. sa_res_key, 0, aptpl, 2, 1);
  3390. if (ret != 0) {
  3391. spin_unlock(&dev->dev_reservation_lock);
  3392. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3393. ret = -EINVAL;
  3394. goto out;
  3395. }
  3396. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3397. iport_ptr);
  3398. new_reg = 1;
  3399. }
  3400. /*
  3401. * f) Release the persistent reservation for the persistent reservation
  3402. * holder (i.e., the I_T nexus on which the
  3403. */
  3404. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  3405. dev->dev_pr_res_holder, 0);
  3406. /*
  3407. * g) Move the persistent reservation to the specified I_T nexus using
  3408. * the same scope and type as the persistent reservation released in
  3409. * item f); and
  3410. */
  3411. dev->dev_pr_res_holder = dest_pr_reg;
  3412. dest_pr_reg->pr_res_holder = 1;
  3413. dest_pr_reg->pr_res_type = type;
  3414. pr_reg->pr_res_scope = scope;
  3415. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  3416. PR_REG_ISID_ID_LEN);
  3417. /*
  3418. * Increment PRGeneration for existing registrations..
  3419. */
  3420. if (!new_reg)
  3421. dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
  3422. spin_unlock(&dev->dev_reservation_lock);
  3423. pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
  3424. " created new reservation holder TYPE: %s on object RTPI:"
  3425. " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
  3426. core_scsi3_pr_dump_type(type), rtpi,
  3427. dest_pr_reg->pr_res_generation);
  3428. pr_debug("SPC-3 PR Successfully moved reservation from"
  3429. " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
  3430. tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
  3431. (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
  3432. dest_node_acl->initiatorname, (iport_ptr != NULL) ?
  3433. iport_ptr : "");
  3434. /*
  3435. * It is now safe to release configfs group dependencies for destination
  3436. * of Transport ID Initiator Device/Port Identifier
  3437. */
  3438. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3439. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3440. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3441. /*
  3442. * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
  3443. * nexus on which PERSISTENT RESERVE OUT command was received.
  3444. */
  3445. if (unreg) {
  3446. spin_lock(&pr_tmpl->registration_lock);
  3447. __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
  3448. spin_unlock(&pr_tmpl->registration_lock);
  3449. } else
  3450. core_scsi3_put_pr_reg(pr_reg);
  3451. /*
  3452. * Clear the APTPL metadata if APTPL has been disabled, otherwise
  3453. * write out the updated metadata to struct file for this SCSI device.
  3454. */
  3455. if (!aptpl) {
  3456. pr_tmpl->pr_aptpl_active = 0;
  3457. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  3458. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
  3459. " REGISTER_AND_MOVE\n");
  3460. } else {
  3461. pr_tmpl->pr_aptpl_active = 1;
  3462. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  3463. &dest_pr_reg->pr_aptpl_buf[0],
  3464. pr_tmpl->pr_aptpl_buf_len);
  3465. if (!ret)
  3466. pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
  3467. " REGISTER_AND_MOVE\n");
  3468. }
  3469. transport_kunmap_data_sg(cmd);
  3470. core_scsi3_put_pr_reg(dest_pr_reg);
  3471. return 0;
  3472. out:
  3473. if (buf)
  3474. transport_kunmap_data_sg(cmd);
  3475. if (dest_se_deve)
  3476. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3477. if (dest_node_acl)
  3478. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3479. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3480. core_scsi3_put_pr_reg(pr_reg);
  3481. return ret;
  3482. }
  3483. static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
  3484. {
  3485. unsigned int __v1, __v2;
  3486. __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
  3487. __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
  3488. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  3489. }
  3490. /*
  3491. * See spc4r17 section 6.14 Table 170
  3492. */
  3493. int target_scsi3_emulate_pr_out(struct se_cmd *cmd)
  3494. {
  3495. unsigned char *cdb = &cmd->t_task_cdb[0];
  3496. unsigned char *buf;
  3497. u64 res_key, sa_res_key;
  3498. int sa, scope, type, aptpl;
  3499. int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
  3500. int ret;
  3501. /*
  3502. * Following spc2r20 5.5.1 Reservations overview:
  3503. *
  3504. * If a logical unit has been reserved by any RESERVE command and is
  3505. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3506. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3507. * initiator or service action and shall terminate with a RESERVATION
  3508. * CONFLICT status.
  3509. */
  3510. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
  3511. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3512. " SPC-2 reservation is held, returning"
  3513. " RESERVATION_CONFLICT\n");
  3514. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3515. ret = EINVAL;
  3516. goto out;
  3517. }
  3518. /*
  3519. * FIXME: A NULL struct se_session pointer means an this is not coming from
  3520. * a $FABRIC_MOD's nexus, but from internal passthrough ops.
  3521. */
  3522. if (!cmd->se_sess) {
  3523. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3524. return -EINVAL;
  3525. }
  3526. if (cmd->data_length < 24) {
  3527. pr_warn("SPC-PR: Received PR OUT parameter list"
  3528. " length too small: %u\n", cmd->data_length);
  3529. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3530. ret = -EINVAL;
  3531. goto out;
  3532. }
  3533. /*
  3534. * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
  3535. */
  3536. sa = (cdb[1] & 0x1f);
  3537. scope = (cdb[2] & 0xf0);
  3538. type = (cdb[2] & 0x0f);
  3539. buf = transport_kmap_data_sg(cmd);
  3540. /*
  3541. * From PERSISTENT_RESERVE_OUT parameter list (payload)
  3542. */
  3543. res_key = core_scsi3_extract_reservation_key(&buf[0]);
  3544. sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
  3545. /*
  3546. * REGISTER_AND_MOVE uses a different SA parameter list containing
  3547. * SCSI TransportIDs.
  3548. */
  3549. if (sa != PRO_REGISTER_AND_MOVE) {
  3550. spec_i_pt = (buf[20] & 0x08);
  3551. all_tg_pt = (buf[20] & 0x04);
  3552. aptpl = (buf[20] & 0x01);
  3553. } else {
  3554. aptpl = (buf[17] & 0x01);
  3555. unreg = (buf[17] & 0x02);
  3556. }
  3557. transport_kunmap_data_sg(cmd);
  3558. buf = NULL;
  3559. /*
  3560. * SPEC_I_PT=1 is only valid for Service action: REGISTER
  3561. */
  3562. if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
  3563. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3564. ret = -EINVAL;
  3565. goto out;
  3566. }
  3567. /*
  3568. * From spc4r17 section 6.14:
  3569. *
  3570. * If the SPEC_I_PT bit is set to zero, the service action is not
  3571. * REGISTER AND MOVE, and the parameter list length is not 24, then
  3572. * the command shall be terminated with CHECK CONDITION status, with
  3573. * the sense key set to ILLEGAL REQUEST, and the additional sense
  3574. * code set to PARAMETER LIST LENGTH ERROR.
  3575. */
  3576. if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
  3577. (cmd->data_length != 24)) {
  3578. pr_warn("SPC-PR: Received PR OUT illegal parameter"
  3579. " list length: %u\n", cmd->data_length);
  3580. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3581. ret = -EINVAL;
  3582. goto out;
  3583. }
  3584. /*
  3585. * (core_scsi3_emulate_pro_* function parameters
  3586. * are defined by spc4r17 Table 174:
  3587. * PERSISTENT_RESERVE_OUT service actions and valid parameters.
  3588. */
  3589. switch (sa) {
  3590. case PRO_REGISTER:
  3591. ret = core_scsi3_emulate_pro_register(cmd,
  3592. res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
  3593. break;
  3594. case PRO_RESERVE:
  3595. ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
  3596. break;
  3597. case PRO_RELEASE:
  3598. ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
  3599. break;
  3600. case PRO_CLEAR:
  3601. ret = core_scsi3_emulate_pro_clear(cmd, res_key);
  3602. break;
  3603. case PRO_PREEMPT:
  3604. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3605. res_key, sa_res_key, 0);
  3606. break;
  3607. case PRO_PREEMPT_AND_ABORT:
  3608. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3609. res_key, sa_res_key, 1);
  3610. break;
  3611. case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
  3612. ret = core_scsi3_emulate_pro_register(cmd,
  3613. 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
  3614. break;
  3615. case PRO_REGISTER_AND_MOVE:
  3616. ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
  3617. sa_res_key, aptpl, unreg);
  3618. break;
  3619. default:
  3620. pr_err("Unknown PERSISTENT_RESERVE_OUT service"
  3621. " action: 0x%02x\n", cdb[1] & 0x1f);
  3622. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3623. ret = -EINVAL;
  3624. break;
  3625. }
  3626. out:
  3627. if (!ret)
  3628. target_complete_cmd(cmd, GOOD);
  3629. return ret;
  3630. }
  3631. /*
  3632. * PERSISTENT_RESERVE_IN Service Action READ_KEYS
  3633. *
  3634. * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
  3635. */
  3636. static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
  3637. {
  3638. struct se_device *se_dev = cmd->se_dev;
  3639. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3640. struct t10_pr_registration *pr_reg;
  3641. unsigned char *buf;
  3642. u32 add_len = 0, off = 8;
  3643. if (cmd->data_length < 8) {
  3644. pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
  3645. " too small\n", cmd->data_length);
  3646. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3647. return -EINVAL;
  3648. }
  3649. buf = transport_kmap_data_sg(cmd);
  3650. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3651. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3652. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3653. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3654. spin_lock(&su_dev->t10_pr.registration_lock);
  3655. list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
  3656. pr_reg_list) {
  3657. /*
  3658. * Check for overflow of 8byte PRI READ_KEYS payload and
  3659. * next reservation key list descriptor.
  3660. */
  3661. if ((add_len + 8) > (cmd->data_length - 8))
  3662. break;
  3663. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3664. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3665. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3666. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3667. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3668. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3669. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3670. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3671. add_len += 8;
  3672. }
  3673. spin_unlock(&su_dev->t10_pr.registration_lock);
  3674. buf[4] = ((add_len >> 24) & 0xff);
  3675. buf[5] = ((add_len >> 16) & 0xff);
  3676. buf[6] = ((add_len >> 8) & 0xff);
  3677. buf[7] = (add_len & 0xff);
  3678. transport_kunmap_data_sg(cmd);
  3679. return 0;
  3680. }
  3681. /*
  3682. * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
  3683. *
  3684. * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
  3685. */
  3686. static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
  3687. {
  3688. struct se_device *se_dev = cmd->se_dev;
  3689. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3690. struct t10_pr_registration *pr_reg;
  3691. unsigned char *buf;
  3692. u64 pr_res_key;
  3693. u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
  3694. if (cmd->data_length < 8) {
  3695. pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
  3696. " too small\n", cmd->data_length);
  3697. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3698. return -EINVAL;
  3699. }
  3700. buf = transport_kmap_data_sg(cmd);
  3701. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3702. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3703. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3704. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3705. spin_lock(&se_dev->dev_reservation_lock);
  3706. pr_reg = se_dev->dev_pr_res_holder;
  3707. if ((pr_reg)) {
  3708. /*
  3709. * Set the hardcoded Additional Length
  3710. */
  3711. buf[4] = ((add_len >> 24) & 0xff);
  3712. buf[5] = ((add_len >> 16) & 0xff);
  3713. buf[6] = ((add_len >> 8) & 0xff);
  3714. buf[7] = (add_len & 0xff);
  3715. if (cmd->data_length < 22)
  3716. goto err;
  3717. /*
  3718. * Set the Reservation key.
  3719. *
  3720. * From spc4r17, section 5.7.10:
  3721. * A persistent reservation holder has its reservation key
  3722. * returned in the parameter data from a PERSISTENT
  3723. * RESERVE IN command with READ RESERVATION service action as
  3724. * follows:
  3725. * a) For a persistent reservation of the type Write Exclusive
  3726. * - All Registrants or Exclusive Access ­ All Regitrants,
  3727. * the reservation key shall be set to zero; or
  3728. * b) For all other persistent reservation types, the
  3729. * reservation key shall be set to the registered
  3730. * reservation key for the I_T nexus that holds the
  3731. * persistent reservation.
  3732. */
  3733. if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3734. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
  3735. pr_res_key = 0;
  3736. else
  3737. pr_res_key = pr_reg->pr_res_key;
  3738. buf[8] = ((pr_res_key >> 56) & 0xff);
  3739. buf[9] = ((pr_res_key >> 48) & 0xff);
  3740. buf[10] = ((pr_res_key >> 40) & 0xff);
  3741. buf[11] = ((pr_res_key >> 32) & 0xff);
  3742. buf[12] = ((pr_res_key >> 24) & 0xff);
  3743. buf[13] = ((pr_res_key >> 16) & 0xff);
  3744. buf[14] = ((pr_res_key >> 8) & 0xff);
  3745. buf[15] = (pr_res_key & 0xff);
  3746. /*
  3747. * Set the SCOPE and TYPE
  3748. */
  3749. buf[21] = (pr_reg->pr_res_scope & 0xf0) |
  3750. (pr_reg->pr_res_type & 0x0f);
  3751. }
  3752. err:
  3753. spin_unlock(&se_dev->dev_reservation_lock);
  3754. transport_kunmap_data_sg(cmd);
  3755. return 0;
  3756. }
  3757. /*
  3758. * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
  3759. *
  3760. * See spc4r17 section 6.13.4 Table 165
  3761. */
  3762. static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
  3763. {
  3764. struct se_device *dev = cmd->se_dev;
  3765. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  3766. unsigned char *buf;
  3767. u16 add_len = 8; /* Hardcoded to 8. */
  3768. if (cmd->data_length < 6) {
  3769. pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
  3770. " %u too small\n", cmd->data_length);
  3771. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3772. return -EINVAL;
  3773. }
  3774. buf = transport_kmap_data_sg(cmd);
  3775. buf[0] = ((add_len << 8) & 0xff);
  3776. buf[1] = (add_len & 0xff);
  3777. buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
  3778. buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
  3779. buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
  3780. buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
  3781. /*
  3782. * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
  3783. * set the TMV: Task Mask Valid bit.
  3784. */
  3785. buf[3] |= 0x80;
  3786. /*
  3787. * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
  3788. */
  3789. buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
  3790. /*
  3791. * PTPL_A: Persistence across Target Power Loss Active bit
  3792. */
  3793. if (pr_tmpl->pr_aptpl_active)
  3794. buf[3] |= 0x01;
  3795. /*
  3796. * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
  3797. */
  3798. buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3799. buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
  3800. buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
  3801. buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
  3802. buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
  3803. buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3804. transport_kunmap_data_sg(cmd);
  3805. return 0;
  3806. }
  3807. /*
  3808. * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
  3809. *
  3810. * See spc4r17 section 6.13.5 Table 168 and 169
  3811. */
  3812. static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
  3813. {
  3814. struct se_device *se_dev = cmd->se_dev;
  3815. struct se_node_acl *se_nacl;
  3816. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3817. struct se_portal_group *se_tpg;
  3818. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  3819. struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
  3820. unsigned char *buf;
  3821. u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
  3822. u32 off = 8; /* off into first Full Status descriptor */
  3823. int format_code = 0;
  3824. if (cmd->data_length < 8) {
  3825. pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
  3826. " too small\n", cmd->data_length);
  3827. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3828. return -EINVAL;
  3829. }
  3830. buf = transport_kmap_data_sg(cmd);
  3831. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3832. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3833. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3834. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3835. spin_lock(&pr_tmpl->registration_lock);
  3836. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  3837. &pr_tmpl->registration_list, pr_reg_list) {
  3838. se_nacl = pr_reg->pr_reg_nacl;
  3839. se_tpg = pr_reg->pr_reg_nacl->se_tpg;
  3840. add_desc_len = 0;
  3841. atomic_inc(&pr_reg->pr_res_holders);
  3842. smp_mb__after_atomic_inc();
  3843. spin_unlock(&pr_tmpl->registration_lock);
  3844. /*
  3845. * Determine expected length of $FABRIC_MOD specific
  3846. * TransportID full status descriptor..
  3847. */
  3848. exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
  3849. se_tpg, se_nacl, pr_reg, &format_code);
  3850. if ((exp_desc_len + add_len) > cmd->data_length) {
  3851. pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
  3852. " out of buffer: %d\n", cmd->data_length);
  3853. spin_lock(&pr_tmpl->registration_lock);
  3854. atomic_dec(&pr_reg->pr_res_holders);
  3855. smp_mb__after_atomic_dec();
  3856. break;
  3857. }
  3858. /*
  3859. * Set RESERVATION KEY
  3860. */
  3861. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3862. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3863. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3864. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3865. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3866. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3867. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3868. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3869. off += 4; /* Skip Over Reserved area */
  3870. /*
  3871. * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
  3872. */
  3873. if (pr_reg->pr_reg_all_tg_pt)
  3874. buf[off] = 0x02;
  3875. /*
  3876. * The struct se_lun pointer will be present for the
  3877. * reservation holder for PR_HOLDER bit.
  3878. *
  3879. * Also, if this registration is the reservation
  3880. * holder, fill in SCOPE and TYPE in the next byte.
  3881. */
  3882. if (pr_reg->pr_res_holder) {
  3883. buf[off++] |= 0x01;
  3884. buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
  3885. (pr_reg->pr_res_type & 0x0f);
  3886. } else
  3887. off += 2;
  3888. off += 4; /* Skip over reserved area */
  3889. /*
  3890. * From spc4r17 6.3.15:
  3891. *
  3892. * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
  3893. * IDENTIFIER field contains the relative port identifier (see
  3894. * 3.1.120) of the target port that is part of the I_T nexus
  3895. * described by this full status descriptor. If the ALL_TG_PT
  3896. * bit is set to one, the contents of the RELATIVE TARGET PORT
  3897. * IDENTIFIER field are not defined by this standard.
  3898. */
  3899. if (!pr_reg->pr_reg_all_tg_pt) {
  3900. struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
  3901. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  3902. buf[off++] = (port->sep_rtpi & 0xff);
  3903. } else
  3904. off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
  3905. /*
  3906. * Now, have the $FABRIC_MOD fill in the protocol identifier
  3907. */
  3908. desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
  3909. se_nacl, pr_reg, &format_code, &buf[off+4]);
  3910. spin_lock(&pr_tmpl->registration_lock);
  3911. atomic_dec(&pr_reg->pr_res_holders);
  3912. smp_mb__after_atomic_dec();
  3913. /*
  3914. * Set the ADDITIONAL DESCRIPTOR LENGTH
  3915. */
  3916. buf[off++] = ((desc_len >> 24) & 0xff);
  3917. buf[off++] = ((desc_len >> 16) & 0xff);
  3918. buf[off++] = ((desc_len >> 8) & 0xff);
  3919. buf[off++] = (desc_len & 0xff);
  3920. /*
  3921. * Size of full desctipor header minus TransportID
  3922. * containing $FABRIC_MOD specific) initiator device/port
  3923. * WWN information.
  3924. *
  3925. * See spc4r17 Section 6.13.5 Table 169
  3926. */
  3927. add_desc_len = (24 + desc_len);
  3928. off += desc_len;
  3929. add_len += add_desc_len;
  3930. }
  3931. spin_unlock(&pr_tmpl->registration_lock);
  3932. /*
  3933. * Set ADDITIONAL_LENGTH
  3934. */
  3935. buf[4] = ((add_len >> 24) & 0xff);
  3936. buf[5] = ((add_len >> 16) & 0xff);
  3937. buf[6] = ((add_len >> 8) & 0xff);
  3938. buf[7] = (add_len & 0xff);
  3939. transport_kunmap_data_sg(cmd);
  3940. return 0;
  3941. }
  3942. int target_scsi3_emulate_pr_in(struct se_cmd *cmd)
  3943. {
  3944. int ret;
  3945. /*
  3946. * Following spc2r20 5.5.1 Reservations overview:
  3947. *
  3948. * If a logical unit has been reserved by any RESERVE command and is
  3949. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3950. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3951. * initiator or service action and shall terminate with a RESERVATION
  3952. * CONFLICT status.
  3953. */
  3954. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
  3955. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3956. " SPC-2 reservation is held, returning"
  3957. " RESERVATION_CONFLICT\n");
  3958. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3959. return -EINVAL;
  3960. }
  3961. switch (cmd->t_task_cdb[1] & 0x1f) {
  3962. case PRI_READ_KEYS:
  3963. ret = core_scsi3_pri_read_keys(cmd);
  3964. break;
  3965. case PRI_READ_RESERVATION:
  3966. ret = core_scsi3_pri_read_reservation(cmd);
  3967. break;
  3968. case PRI_REPORT_CAPABILITIES:
  3969. ret = core_scsi3_pri_report_capabilities(cmd);
  3970. break;
  3971. case PRI_READ_FULL_STATUS:
  3972. ret = core_scsi3_pri_read_full_status(cmd);
  3973. break;
  3974. default:
  3975. pr_err("Unknown PERSISTENT_RESERVE_IN service"
  3976. " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
  3977. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3978. ret = -EINVAL;
  3979. break;
  3980. }
  3981. if (!ret)
  3982. target_complete_cmd(cmd, GOOD);
  3983. return ret;
  3984. }
  3985. static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
  3986. {
  3987. return 0;
  3988. }
  3989. static int core_pt_seq_non_holder(
  3990. struct se_cmd *cmd,
  3991. unsigned char *cdb,
  3992. u32 pr_reg_type)
  3993. {
  3994. return 0;
  3995. }
  3996. int core_setup_reservations(struct se_device *dev, int force_pt)
  3997. {
  3998. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  3999. struct t10_reservation *rest = &su_dev->t10_pr;
  4000. /*
  4001. * If this device is from Target_Core_Mod/pSCSI, use the reservations
  4002. * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
  4003. * cause a problem because libata and some SATA RAID HBAs appear
  4004. * under Linux/SCSI, but to emulate reservations themselves.
  4005. */
  4006. if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  4007. !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
  4008. rest->res_type = SPC_PASSTHROUGH;
  4009. rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
  4010. rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
  4011. pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
  4012. " emulation\n", dev->transport->name);
  4013. return 0;
  4014. }
  4015. /*
  4016. * If SPC-3 or above is reported by real or emulated struct se_device,
  4017. * use emulated Persistent Reservations.
  4018. */
  4019. if (dev->transport->get_device_rev(dev) >= SCSI_3) {
  4020. rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
  4021. rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
  4022. rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
  4023. pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
  4024. " emulation\n", dev->transport->name);
  4025. } else {
  4026. rest->res_type = SPC2_RESERVATIONS;
  4027. rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
  4028. rest->pr_ops.t10_seq_non_holder =
  4029. &core_scsi2_reservation_seq_non_holder;
  4030. pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
  4031. dev->transport->name);
  4032. }
  4033. return 0;
  4034. }