target_core_pr.c 125 KB

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