target_core_pr.c 129 KB

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