target_core_pr.c 129 KB

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