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