target_core_pr.c 130 KB

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