target_core_pr.c 132 KB

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