target_core_pr.c 132 KB

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