target_core_pr.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163
  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. /*
  1329. * For nacl->dynamic_node_acl=1
  1330. */
  1331. lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
  1332. atomic_read(&se_deve->pr_kref.refcount) != 0);
  1333. if (!lun_acl)
  1334. return 0;
  1335. return target_depend_item(&lun_acl->se_lun_group.cg_item);
  1336. }
  1337. static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
  1338. {
  1339. struct se_lun_acl *lun_acl;
  1340. /*
  1341. * For nacl->dynamic_node_acl=1
  1342. */
  1343. lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
  1344. atomic_read(&se_deve->pr_kref.refcount) != 0);
  1345. if (!lun_acl) {
  1346. kref_put(&se_deve->pr_kref, target_pr_kref_release);
  1347. return;
  1348. }
  1349. target_undepend_item(&lun_acl->se_lun_group.cg_item);
  1350. kref_put(&se_deve->pr_kref, target_pr_kref_release);
  1351. }
  1352. static sense_reason_t
  1353. core_scsi3_decode_spec_i_port(
  1354. struct se_cmd *cmd,
  1355. struct se_portal_group *tpg,
  1356. unsigned char *l_isid,
  1357. u64 sa_res_key,
  1358. int all_tg_pt,
  1359. int aptpl)
  1360. {
  1361. struct se_device *dev = cmd->se_dev;
  1362. struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
  1363. struct se_session *se_sess = cmd->se_sess;
  1364. struct se_node_acl *dest_node_acl = NULL;
  1365. struct se_dev_entry *dest_se_deve = NULL;
  1366. struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
  1367. struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
  1368. LIST_HEAD(tid_dest_list);
  1369. struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
  1370. unsigned char *buf, *ptr, proto_ident;
  1371. const unsigned char *i_str = NULL;
  1372. char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
  1373. sense_reason_t ret;
  1374. u32 tpdl, tid_len = 0;
  1375. u32 dest_rtpi = 0;
  1376. /*
  1377. * Allocate a struct pr_transport_id_holder and setup the
  1378. * local_node_acl pointer and add to struct list_head tid_dest_list
  1379. * for add registration processing in the loop of tid_dest_list below.
  1380. */
  1381. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
  1382. if (!tidh_new) {
  1383. pr_err("Unable to allocate tidh_new\n");
  1384. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1385. }
  1386. INIT_LIST_HEAD(&tidh_new->dest_list);
  1387. tidh_new->dest_tpg = tpg;
  1388. tidh_new->dest_node_acl = se_sess->se_node_acl;
  1389. local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1390. se_sess->se_node_acl, cmd->se_lun,
  1391. NULL, cmd->orig_fe_lun, l_isid,
  1392. sa_res_key, all_tg_pt, aptpl);
  1393. if (!local_pr_reg) {
  1394. kfree(tidh_new);
  1395. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1396. }
  1397. tidh_new->dest_pr_reg = local_pr_reg;
  1398. /*
  1399. * The local I_T nexus does not hold any configfs dependances,
  1400. * so we set tidh_new->dest_se_deve to NULL to prevent the
  1401. * configfs_undepend_item() calls in the tid_dest_list loops below.
  1402. */
  1403. tidh_new->dest_se_deve = NULL;
  1404. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1405. if (cmd->data_length < 28) {
  1406. pr_warn("SPC-PR: Received PR OUT parameter list"
  1407. " length too small: %u\n", cmd->data_length);
  1408. ret = TCM_INVALID_PARAMETER_LIST;
  1409. goto out;
  1410. }
  1411. buf = transport_kmap_data_sg(cmd);
  1412. if (!buf) {
  1413. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1414. goto out;
  1415. }
  1416. /*
  1417. * For a PERSISTENT RESERVE OUT specify initiator ports payload,
  1418. * first extract TransportID Parameter Data Length, and make sure
  1419. * the value matches up to the SCSI expected data transfer length.
  1420. */
  1421. tpdl = (buf[24] & 0xff) << 24;
  1422. tpdl |= (buf[25] & 0xff) << 16;
  1423. tpdl |= (buf[26] & 0xff) << 8;
  1424. tpdl |= buf[27] & 0xff;
  1425. if ((tpdl + 28) != cmd->data_length) {
  1426. pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
  1427. " does not equal CDB data_length: %u\n", tpdl,
  1428. cmd->data_length);
  1429. ret = TCM_INVALID_PARAMETER_LIST;
  1430. goto out_unmap;
  1431. }
  1432. /*
  1433. * Start processing the received transport IDs using the
  1434. * receiving I_T Nexus portal's fabric dependent methods to
  1435. * obtain the SCSI Initiator Port/Device Identifiers.
  1436. */
  1437. ptr = &buf[28];
  1438. while (tpdl > 0) {
  1439. struct se_lun *dest_lun, *tmp_lun;
  1440. proto_ident = (ptr[0] & 0x0f);
  1441. dest_tpg = NULL;
  1442. spin_lock(&dev->se_port_lock);
  1443. list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
  1444. tmp_tpg = tmp_lun->lun_tpg;
  1445. /*
  1446. * Look for the matching proto_ident provided by
  1447. * the received TransportID
  1448. */
  1449. if (tmp_tpg->proto_id != proto_ident)
  1450. continue;
  1451. dest_rtpi = tmp_lun->lun_rtpi;
  1452. i_str = target_parse_pr_out_transport_id(tmp_tpg,
  1453. (const char *)ptr, &tid_len, &iport_ptr);
  1454. if (!i_str)
  1455. continue;
  1456. atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
  1457. spin_unlock(&dev->se_port_lock);
  1458. if (core_scsi3_tpg_depend_item(tmp_tpg)) {
  1459. pr_err(" core_scsi3_tpg_depend_item()"
  1460. " for tmp_tpg\n");
  1461. atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
  1462. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1463. goto out_unmap;
  1464. }
  1465. /*
  1466. * Locate the destination initiator ACL to be registered
  1467. * from the decoded fabric module specific TransportID
  1468. * at *i_str.
  1469. */
  1470. mutex_lock(&tmp_tpg->acl_node_mutex);
  1471. dest_node_acl = __core_tpg_get_initiator_node_acl(
  1472. tmp_tpg, i_str);
  1473. if (dest_node_acl)
  1474. atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
  1475. mutex_unlock(&tmp_tpg->acl_node_mutex);
  1476. if (!dest_node_acl) {
  1477. core_scsi3_tpg_undepend_item(tmp_tpg);
  1478. spin_lock(&dev->se_port_lock);
  1479. continue;
  1480. }
  1481. if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
  1482. pr_err("configfs_depend_item() failed"
  1483. " for dest_node_acl->acl_group\n");
  1484. atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
  1485. core_scsi3_tpg_undepend_item(tmp_tpg);
  1486. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1487. goto out_unmap;
  1488. }
  1489. dest_tpg = tmp_tpg;
  1490. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
  1491. " %s Port RTPI: %hu\n",
  1492. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1493. dest_node_acl->initiatorname, dest_rtpi);
  1494. spin_lock(&dev->se_port_lock);
  1495. break;
  1496. }
  1497. spin_unlock(&dev->se_port_lock);
  1498. if (!dest_tpg) {
  1499. pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
  1500. " dest_tpg\n");
  1501. ret = TCM_INVALID_PARAMETER_LIST;
  1502. goto out_unmap;
  1503. }
  1504. pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
  1505. " tid_len: %d for %s + %s\n",
  1506. dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
  1507. tpdl, tid_len, i_str, iport_ptr);
  1508. if (tid_len > tpdl) {
  1509. pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
  1510. " %u for Transport ID: %s\n", tid_len, ptr);
  1511. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1512. core_scsi3_tpg_undepend_item(dest_tpg);
  1513. ret = TCM_INVALID_PARAMETER_LIST;
  1514. goto out_unmap;
  1515. }
  1516. /*
  1517. * Locate the desintation struct se_dev_entry pointer for matching
  1518. * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
  1519. * Target Port.
  1520. */
  1521. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
  1522. dest_rtpi);
  1523. if (!dest_se_deve) {
  1524. pr_err("Unable to locate %s dest_se_deve"
  1525. " from destination RTPI: %hu\n",
  1526. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1527. dest_rtpi);
  1528. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1529. core_scsi3_tpg_undepend_item(dest_tpg);
  1530. ret = TCM_INVALID_PARAMETER_LIST;
  1531. goto out_unmap;
  1532. }
  1533. if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
  1534. pr_err("core_scsi3_lunacl_depend_item()"
  1535. " failed\n");
  1536. kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
  1537. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1538. core_scsi3_tpg_undepend_item(dest_tpg);
  1539. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1540. goto out_unmap;
  1541. }
  1542. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
  1543. " dest_se_deve mapped_lun: %llu\n",
  1544. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1545. dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
  1546. /*
  1547. * Skip any TransportIDs that already have a registration for
  1548. * this target port.
  1549. */
  1550. pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  1551. iport_ptr);
  1552. if (pr_reg_e) {
  1553. core_scsi3_put_pr_reg(pr_reg_e);
  1554. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1555. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1556. core_scsi3_tpg_undepend_item(dest_tpg);
  1557. ptr += tid_len;
  1558. tpdl -= tid_len;
  1559. tid_len = 0;
  1560. continue;
  1561. }
  1562. /*
  1563. * Allocate a struct pr_transport_id_holder and setup
  1564. * the dest_node_acl and dest_se_deve pointers for the
  1565. * loop below.
  1566. */
  1567. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
  1568. GFP_KERNEL);
  1569. if (!tidh_new) {
  1570. pr_err("Unable to allocate tidh_new\n");
  1571. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1572. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1573. core_scsi3_tpg_undepend_item(dest_tpg);
  1574. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1575. goto out_unmap;
  1576. }
  1577. INIT_LIST_HEAD(&tidh_new->dest_list);
  1578. tidh_new->dest_tpg = dest_tpg;
  1579. tidh_new->dest_node_acl = dest_node_acl;
  1580. tidh_new->dest_se_deve = dest_se_deve;
  1581. /*
  1582. * Allocate, but do NOT add the registration for the
  1583. * TransportID referenced SCSI Initiator port. This
  1584. * done because of the following from spc4r17 in section
  1585. * 6.14.3 wrt SPEC_I_PT:
  1586. *
  1587. * "If a registration fails for any initiator port (e.g., if th
  1588. * logical unit does not have enough resources available to
  1589. * hold the registration information), no registrations shall be
  1590. * made, and the command shall be terminated with
  1591. * CHECK CONDITION status."
  1592. *
  1593. * That means we call __core_scsi3_alloc_registration() here,
  1594. * and then call __core_scsi3_add_registration() in the
  1595. * 2nd loop which will never fail.
  1596. */
  1597. dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
  1598. atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
  1599. dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1600. dest_node_acl, dest_lun, dest_se_deve,
  1601. dest_se_deve->mapped_lun, iport_ptr,
  1602. sa_res_key, all_tg_pt, aptpl);
  1603. if (!dest_pr_reg) {
  1604. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1605. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1606. core_scsi3_tpg_undepend_item(dest_tpg);
  1607. kfree(tidh_new);
  1608. ret = TCM_INVALID_PARAMETER_LIST;
  1609. goto out_unmap;
  1610. }
  1611. tidh_new->dest_pr_reg = dest_pr_reg;
  1612. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1613. ptr += tid_len;
  1614. tpdl -= tid_len;
  1615. tid_len = 0;
  1616. }
  1617. transport_kunmap_data_sg(cmd);
  1618. /*
  1619. * Go ahead and create a registrations from tid_dest_list for the
  1620. * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
  1621. * and dest_se_deve.
  1622. *
  1623. * The SA Reservation Key from the PROUT is set for the
  1624. * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
  1625. * means that the TransportID Initiator port will be
  1626. * registered on all of the target ports in the SCSI target device
  1627. * ALL_TG_PT=0 means the registration will only be for the
  1628. * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
  1629. * was received.
  1630. */
  1631. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1632. dest_tpg = tidh->dest_tpg;
  1633. dest_node_acl = tidh->dest_node_acl;
  1634. dest_se_deve = tidh->dest_se_deve;
  1635. dest_pr_reg = tidh->dest_pr_reg;
  1636. list_del(&tidh->dest_list);
  1637. kfree(tidh);
  1638. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  1639. core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
  1640. __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
  1641. dest_pr_reg, 0, 0);
  1642. pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
  1643. " registered Transport ID for Node: %s%s Mapped LUN:"
  1644. " %llu\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
  1645. dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
  1646. dest_se_deve->mapped_lun : 0);
  1647. if (!dest_se_deve) {
  1648. kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
  1649. target_pr_kref_release);
  1650. continue;
  1651. }
  1652. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1653. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1654. core_scsi3_tpg_undepend_item(dest_tpg);
  1655. }
  1656. return 0;
  1657. out_unmap:
  1658. transport_kunmap_data_sg(cmd);
  1659. out:
  1660. /*
  1661. * For the failure case, release everything from tid_dest_list
  1662. * including *dest_pr_reg and the configfs dependances..
  1663. */
  1664. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1665. dest_tpg = tidh->dest_tpg;
  1666. dest_node_acl = tidh->dest_node_acl;
  1667. dest_se_deve = tidh->dest_se_deve;
  1668. dest_pr_reg = tidh->dest_pr_reg;
  1669. list_del(&tidh->dest_list);
  1670. kfree(tidh);
  1671. /*
  1672. * Release any extra ALL_TG_PT=1 registrations for
  1673. * the SPEC_I_PT=1 case.
  1674. */
  1675. list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
  1676. &dest_pr_reg->pr_reg_atp_list,
  1677. pr_reg_atp_mem_list) {
  1678. list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
  1679. core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
  1680. kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
  1681. }
  1682. kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
  1683. if (!dest_se_deve) {
  1684. kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
  1685. target_pr_kref_release);
  1686. continue;
  1687. }
  1688. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1689. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1690. core_scsi3_tpg_undepend_item(dest_tpg);
  1691. }
  1692. return ret;
  1693. }
  1694. static int core_scsi3_update_aptpl_buf(
  1695. struct se_device *dev,
  1696. unsigned char *buf,
  1697. u32 pr_aptpl_buf_len)
  1698. {
  1699. struct se_portal_group *tpg;
  1700. struct t10_pr_registration *pr_reg;
  1701. unsigned char tmp[512], isid_buf[32];
  1702. ssize_t len = 0;
  1703. int reg_count = 0;
  1704. int ret = 0;
  1705. spin_lock(&dev->dev_reservation_lock);
  1706. spin_lock(&dev->t10_pr.registration_lock);
  1707. /*
  1708. * Walk the registration list..
  1709. */
  1710. list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
  1711. pr_reg_list) {
  1712. tmp[0] = '\0';
  1713. isid_buf[0] = '\0';
  1714. tpg = pr_reg->pr_reg_nacl->se_tpg;
  1715. /*
  1716. * Write out any ISID value to APTPL metadata that was included
  1717. * in the original registration.
  1718. */
  1719. if (pr_reg->isid_present_at_reg)
  1720. snprintf(isid_buf, 32, "initiator_sid=%s\n",
  1721. pr_reg->pr_reg_isid);
  1722. /*
  1723. * Include special metadata if the pr_reg matches the
  1724. * reservation holder.
  1725. */
  1726. if (dev->dev_pr_res_holder == pr_reg) {
  1727. snprintf(tmp, 512, "PR_REG_START: %d"
  1728. "\ninitiator_fabric=%s\n"
  1729. "initiator_node=%s\n%s"
  1730. "sa_res_key=%llu\n"
  1731. "res_holder=1\nres_type=%02x\n"
  1732. "res_scope=%02x\nres_all_tg_pt=%d\n"
  1733. "mapped_lun=%llu\n", reg_count,
  1734. tpg->se_tpg_tfo->get_fabric_name(),
  1735. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1736. pr_reg->pr_res_key, pr_reg->pr_res_type,
  1737. pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
  1738. pr_reg->pr_res_mapped_lun);
  1739. } else {
  1740. snprintf(tmp, 512, "PR_REG_START: %d\n"
  1741. "initiator_fabric=%s\ninitiator_node=%s\n%s"
  1742. "sa_res_key=%llu\nres_holder=0\n"
  1743. "res_all_tg_pt=%d\nmapped_lun=%llu\n",
  1744. reg_count, tpg->se_tpg_tfo->get_fabric_name(),
  1745. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1746. pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
  1747. pr_reg->pr_res_mapped_lun);
  1748. }
  1749. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1750. pr_err("Unable to update renaming APTPL metadata,"
  1751. " reallocating larger buffer\n");
  1752. ret = -EMSGSIZE;
  1753. goto out;
  1754. }
  1755. len += sprintf(buf+len, "%s", tmp);
  1756. /*
  1757. * Include information about the associated SCSI target port.
  1758. */
  1759. snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
  1760. "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
  1761. " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
  1762. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1763. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1764. pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
  1765. reg_count);
  1766. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1767. pr_err("Unable to update renaming APTPL metadata,"
  1768. " reallocating larger buffer\n");
  1769. ret = -EMSGSIZE;
  1770. goto out;
  1771. }
  1772. len += sprintf(buf+len, "%s", tmp);
  1773. reg_count++;
  1774. }
  1775. if (!reg_count)
  1776. len += sprintf(buf+len, "No Registrations or Reservations");
  1777. out:
  1778. spin_unlock(&dev->t10_pr.registration_lock);
  1779. spin_unlock(&dev->dev_reservation_lock);
  1780. return ret;
  1781. }
  1782. static int __core_scsi3_write_aptpl_to_file(
  1783. struct se_device *dev,
  1784. unsigned char *buf)
  1785. {
  1786. struct t10_wwn *wwn = &dev->t10_wwn;
  1787. struct file *file;
  1788. int flags = O_RDWR | O_CREAT | O_TRUNC;
  1789. char path[512];
  1790. u32 pr_aptpl_buf_len;
  1791. int ret;
  1792. memset(path, 0, 512);
  1793. if (strlen(&wwn->unit_serial[0]) >= 512) {
  1794. pr_err("WWN value for struct se_device does not fit"
  1795. " into path buffer\n");
  1796. return -EMSGSIZE;
  1797. }
  1798. snprintf(path, 512, "%s/pr/aptpl_%s", db_root, &wwn->unit_serial[0]);
  1799. file = filp_open(path, flags, 0600);
  1800. if (IS_ERR(file)) {
  1801. pr_err("filp_open(%s) for APTPL metadata"
  1802. " failed\n", path);
  1803. return PTR_ERR(file);
  1804. }
  1805. pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
  1806. ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
  1807. if (ret < 0)
  1808. pr_debug("Error writing APTPL metadata file: %s\n", path);
  1809. fput(file);
  1810. return (ret < 0) ? -EIO : 0;
  1811. }
  1812. /*
  1813. * Clear the APTPL metadata if APTPL has been disabled, otherwise
  1814. * write out the updated metadata to struct file for this SCSI device.
  1815. */
  1816. static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
  1817. {
  1818. unsigned char *buf;
  1819. int rc, len = PR_APTPL_BUF_LEN;
  1820. if (!aptpl) {
  1821. char *null_buf = "No Registrations or Reservations\n";
  1822. rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
  1823. dev->t10_pr.pr_aptpl_active = 0;
  1824. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
  1825. if (rc)
  1826. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1827. return 0;
  1828. }
  1829. retry:
  1830. buf = vzalloc(len);
  1831. if (!buf)
  1832. return TCM_OUT_OF_RESOURCES;
  1833. rc = core_scsi3_update_aptpl_buf(dev, buf, len);
  1834. if (rc < 0) {
  1835. vfree(buf);
  1836. len *= 2;
  1837. goto retry;
  1838. }
  1839. rc = __core_scsi3_write_aptpl_to_file(dev, buf);
  1840. if (rc != 0) {
  1841. pr_err("SPC-3 PR: Could not update APTPL\n");
  1842. vfree(buf);
  1843. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1844. }
  1845. dev->t10_pr.pr_aptpl_active = 1;
  1846. vfree(buf);
  1847. pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
  1848. return 0;
  1849. }
  1850. static sense_reason_t
  1851. core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
  1852. bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
  1853. {
  1854. struct se_session *se_sess = cmd->se_sess;
  1855. struct se_device *dev = cmd->se_dev;
  1856. struct se_lun *se_lun = cmd->se_lun;
  1857. struct se_portal_group *se_tpg;
  1858. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
  1859. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  1860. unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
  1861. sense_reason_t ret = TCM_NO_SENSE;
  1862. int pr_holder = 0, type;
  1863. if (!se_sess || !se_lun) {
  1864. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  1865. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1866. }
  1867. se_tpg = se_sess->se_tpg;
  1868. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
  1869. memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
  1870. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
  1871. PR_REG_ISID_LEN);
  1872. isid_ptr = &isid_buf[0];
  1873. }
  1874. /*
  1875. * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
  1876. */
  1877. pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  1878. if (!pr_reg) {
  1879. if (res_key) {
  1880. pr_warn("SPC-3 PR: Reservation Key non-zero"
  1881. " for SA REGISTER, returning CONFLICT\n");
  1882. return TCM_RESERVATION_CONFLICT;
  1883. }
  1884. /*
  1885. * Do nothing but return GOOD status.
  1886. */
  1887. if (!sa_res_key)
  1888. return 0;
  1889. if (!spec_i_pt) {
  1890. /*
  1891. * Perform the Service Action REGISTER on the Initiator
  1892. * Port Endpoint that the PRO was received from on the
  1893. * Logical Unit of the SCSI device server.
  1894. */
  1895. if (core_scsi3_alloc_registration(cmd->se_dev,
  1896. se_sess->se_node_acl, cmd->se_lun,
  1897. NULL, cmd->orig_fe_lun, isid_ptr,
  1898. sa_res_key, all_tg_pt, aptpl,
  1899. register_type, 0)) {
  1900. pr_err("Unable to allocate"
  1901. " struct t10_pr_registration\n");
  1902. return TCM_INVALID_PARAMETER_LIST;
  1903. }
  1904. } else {
  1905. /*
  1906. * Register both the Initiator port that received
  1907. * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
  1908. * TransportID from Parameter list and loop through
  1909. * fabric dependent parameter list while calling
  1910. * logic from of core_scsi3_alloc_registration() for
  1911. * each TransportID provided SCSI Initiator Port/Device
  1912. */
  1913. ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
  1914. isid_ptr, sa_res_key, all_tg_pt, aptpl);
  1915. if (ret != 0)
  1916. return ret;
  1917. }
  1918. return core_scsi3_update_and_write_aptpl(dev, aptpl);
  1919. }
  1920. /* ok, existing registration */
  1921. if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
  1922. pr_err("SPC-3 PR REGISTER: Received"
  1923. " res_key: 0x%016Lx does not match"
  1924. " existing SA REGISTER res_key:"
  1925. " 0x%016Lx\n", res_key,
  1926. pr_reg->pr_res_key);
  1927. ret = TCM_RESERVATION_CONFLICT;
  1928. goto out;
  1929. }
  1930. if (spec_i_pt) {
  1931. pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
  1932. " set on a registered nexus\n");
  1933. ret = TCM_INVALID_PARAMETER_LIST;
  1934. goto out;
  1935. }
  1936. /*
  1937. * An existing ALL_TG_PT=1 registration being released
  1938. * must also set ALL_TG_PT=1 in the incoming PROUT.
  1939. */
  1940. if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
  1941. pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
  1942. " registration exists, but ALL_TG_PT=1 bit not"
  1943. " present in received PROUT\n");
  1944. ret = TCM_INVALID_CDB_FIELD;
  1945. goto out;
  1946. }
  1947. /*
  1948. * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
  1949. */
  1950. if (sa_res_key) {
  1951. /*
  1952. * Increment PRgeneration counter for struct se_device"
  1953. * upon a successful REGISTER, see spc4r17 section 6.3.2
  1954. * READ_KEYS service action.
  1955. */
  1956. pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
  1957. pr_reg->pr_res_key = sa_res_key;
  1958. pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
  1959. " Key for %s to: 0x%016Lx PRgeneration:"
  1960. " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
  1961. (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
  1962. pr_reg->pr_reg_nacl->initiatorname,
  1963. pr_reg->pr_res_key, pr_reg->pr_res_generation);
  1964. } else {
  1965. /*
  1966. * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
  1967. */
  1968. type = pr_reg->pr_res_type;
  1969. pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
  1970. pr_reg);
  1971. if (pr_holder < 0) {
  1972. ret = TCM_RESERVATION_CONFLICT;
  1973. goto out;
  1974. }
  1975. spin_lock(&pr_tmpl->registration_lock);
  1976. /*
  1977. * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
  1978. * and matching pr_res_key.
  1979. */
  1980. if (pr_reg->pr_reg_all_tg_pt) {
  1981. list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
  1982. &pr_tmpl->registration_list,
  1983. pr_reg_list) {
  1984. if (!pr_reg_p->pr_reg_all_tg_pt)
  1985. continue;
  1986. if (pr_reg_p->pr_res_key != res_key)
  1987. continue;
  1988. if (pr_reg == pr_reg_p)
  1989. continue;
  1990. if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
  1991. pr_reg_p->pr_reg_nacl->initiatorname))
  1992. continue;
  1993. __core_scsi3_free_registration(dev,
  1994. pr_reg_p, NULL, 0);
  1995. }
  1996. }
  1997. /*
  1998. * Release the calling I_T Nexus registration now..
  1999. */
  2000. __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
  2001. pr_reg = NULL;
  2002. /*
  2003. * From spc4r17, section 5.7.11.3 Unregistering
  2004. *
  2005. * If the persistent reservation is a registrants only
  2006. * type, the device server shall establish a unit
  2007. * attention condition for the initiator port associated
  2008. * with every registered I_T nexus except for the I_T
  2009. * nexus on which the PERSISTENT RESERVE OUT command was
  2010. * received, with the additional sense code set to
  2011. * RESERVATIONS RELEASED.
  2012. */
  2013. if (pr_holder &&
  2014. (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
  2015. type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
  2016. list_for_each_entry(pr_reg_p,
  2017. &pr_tmpl->registration_list,
  2018. pr_reg_list) {
  2019. target_ua_allocate_lun(
  2020. pr_reg_p->pr_reg_nacl,
  2021. pr_reg_p->pr_res_mapped_lun,
  2022. 0x2A,
  2023. ASCQ_2AH_RESERVATIONS_RELEASED);
  2024. }
  2025. }
  2026. spin_unlock(&pr_tmpl->registration_lock);
  2027. }
  2028. ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
  2029. out:
  2030. if (pr_reg)
  2031. core_scsi3_put_pr_reg(pr_reg);
  2032. return ret;
  2033. }
  2034. unsigned char *core_scsi3_pr_dump_type(int type)
  2035. {
  2036. switch (type) {
  2037. case PR_TYPE_WRITE_EXCLUSIVE:
  2038. return "Write Exclusive Access";
  2039. case PR_TYPE_EXCLUSIVE_ACCESS:
  2040. return "Exclusive Access";
  2041. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2042. return "Write Exclusive Access, Registrants Only";
  2043. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2044. return "Exclusive Access, Registrants Only";
  2045. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2046. return "Write Exclusive Access, All Registrants";
  2047. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2048. return "Exclusive Access, All Registrants";
  2049. default:
  2050. break;
  2051. }
  2052. return "Unknown SPC-3 PR Type";
  2053. }
  2054. static sense_reason_t
  2055. core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
  2056. {
  2057. struct se_device *dev = cmd->se_dev;
  2058. struct se_session *se_sess = cmd->se_sess;
  2059. struct se_lun *se_lun = cmd->se_lun;
  2060. struct t10_pr_registration *pr_reg, *pr_res_holder;
  2061. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  2062. char i_buf[PR_REG_ISID_ID_LEN];
  2063. sense_reason_t ret;
  2064. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2065. if (!se_sess || !se_lun) {
  2066. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2067. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2068. }
  2069. /*
  2070. * Locate the existing *pr_reg via struct se_node_acl pointers
  2071. */
  2072. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2073. se_sess);
  2074. if (!pr_reg) {
  2075. pr_err("SPC-3 PR: Unable to locate"
  2076. " PR_REGISTERED *pr_reg for RESERVE\n");
  2077. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2078. }
  2079. /*
  2080. * From spc4r17 Section 5.7.9: Reserving:
  2081. *
  2082. * An application client creates a persistent reservation by issuing
  2083. * a PERSISTENT RESERVE OUT command with RESERVE service action through
  2084. * a registered I_T nexus with the following parameters:
  2085. * a) RESERVATION KEY set to the value of the reservation key that is
  2086. * registered with the logical unit for the I_T nexus; and
  2087. */
  2088. if (res_key != pr_reg->pr_res_key) {
  2089. pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
  2090. " does not match existing SA REGISTER res_key:"
  2091. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2092. ret = TCM_RESERVATION_CONFLICT;
  2093. goto out_put_pr_reg;
  2094. }
  2095. /*
  2096. * From spc4r17 Section 5.7.9: Reserving:
  2097. *
  2098. * From above:
  2099. * b) TYPE field and SCOPE field set to the persistent reservation
  2100. * being created.
  2101. *
  2102. * Only one persistent reservation is allowed at a time per logical unit
  2103. * and that persistent reservation has a scope of LU_SCOPE.
  2104. */
  2105. if (scope != PR_SCOPE_LU_SCOPE) {
  2106. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2107. ret = TCM_INVALID_PARAMETER_LIST;
  2108. goto out_put_pr_reg;
  2109. }
  2110. /*
  2111. * See if we have an existing PR reservation holder pointer at
  2112. * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
  2113. * *pr_res_holder.
  2114. */
  2115. spin_lock(&dev->dev_reservation_lock);
  2116. pr_res_holder = dev->dev_pr_res_holder;
  2117. if (pr_res_holder) {
  2118. /*
  2119. * From spc4r17 Section 5.7.9: Reserving:
  2120. *
  2121. * If the device server receives a PERSISTENT RESERVE OUT
  2122. * command from an I_T nexus other than a persistent reservation
  2123. * holder (see 5.7.10) that attempts to create a persistent
  2124. * reservation when a persistent reservation already exists for
  2125. * the logical unit, then the command shall be completed with
  2126. * RESERVATION CONFLICT status.
  2127. */
  2128. if (!is_reservation_holder(pr_res_holder, pr_reg)) {
  2129. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2130. pr_err("SPC-3 PR: Attempted RESERVE from"
  2131. " [%s]: %s while reservation already held by"
  2132. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2133. cmd->se_tfo->get_fabric_name(),
  2134. se_sess->se_node_acl->initiatorname,
  2135. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2136. pr_res_holder->pr_reg_nacl->initiatorname);
  2137. spin_unlock(&dev->dev_reservation_lock);
  2138. ret = TCM_RESERVATION_CONFLICT;
  2139. goto out_put_pr_reg;
  2140. }
  2141. /*
  2142. * From spc4r17 Section 5.7.9: Reserving:
  2143. *
  2144. * If a persistent reservation holder attempts to modify the
  2145. * type or scope of an existing persistent reservation, the
  2146. * command shall be completed with RESERVATION CONFLICT status.
  2147. */
  2148. if ((pr_res_holder->pr_res_type != type) ||
  2149. (pr_res_holder->pr_res_scope != scope)) {
  2150. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2151. pr_err("SPC-3 PR: Attempted RESERVE from"
  2152. " [%s]: %s trying to change TYPE and/or SCOPE,"
  2153. " while reservation already held by [%s]: %s,"
  2154. " returning RESERVATION_CONFLICT\n",
  2155. cmd->se_tfo->get_fabric_name(),
  2156. se_sess->se_node_acl->initiatorname,
  2157. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2158. pr_res_holder->pr_reg_nacl->initiatorname);
  2159. spin_unlock(&dev->dev_reservation_lock);
  2160. ret = TCM_RESERVATION_CONFLICT;
  2161. goto out_put_pr_reg;
  2162. }
  2163. /*
  2164. * From spc4r17 Section 5.7.9: Reserving:
  2165. *
  2166. * If the device server receives a PERSISTENT RESERVE OUT
  2167. * command with RESERVE service action where the TYPE field and
  2168. * the SCOPE field contain the same values as the existing type
  2169. * and scope from a persistent reservation holder, it shall not
  2170. * make any change to the existing persistent reservation and
  2171. * shall completethe command with GOOD status.
  2172. */
  2173. spin_unlock(&dev->dev_reservation_lock);
  2174. ret = 0;
  2175. goto out_put_pr_reg;
  2176. }
  2177. /*
  2178. * Otherwise, our *pr_reg becomes the PR reservation holder for said
  2179. * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
  2180. */
  2181. pr_reg->pr_res_scope = scope;
  2182. pr_reg->pr_res_type = type;
  2183. pr_reg->pr_res_holder = 1;
  2184. dev->dev_pr_res_holder = pr_reg;
  2185. core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
  2186. pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
  2187. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2188. cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
  2189. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2190. pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
  2191. cmd->se_tfo->get_fabric_name(),
  2192. se_sess->se_node_acl->initiatorname,
  2193. i_buf);
  2194. spin_unlock(&dev->dev_reservation_lock);
  2195. if (pr_tmpl->pr_aptpl_active)
  2196. core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
  2197. ret = 0;
  2198. out_put_pr_reg:
  2199. core_scsi3_put_pr_reg(pr_reg);
  2200. return ret;
  2201. }
  2202. static sense_reason_t
  2203. core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
  2204. u64 res_key)
  2205. {
  2206. switch (type) {
  2207. case PR_TYPE_WRITE_EXCLUSIVE:
  2208. case PR_TYPE_EXCLUSIVE_ACCESS:
  2209. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2210. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2211. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2212. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2213. return core_scsi3_pro_reserve(cmd, type, scope, res_key);
  2214. default:
  2215. pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
  2216. " 0x%02x\n", type);
  2217. return TCM_INVALID_CDB_FIELD;
  2218. }
  2219. }
  2220. /*
  2221. * Called with struct se_device->dev_reservation_lock held.
  2222. */
  2223. static void __core_scsi3_complete_pro_release(
  2224. struct se_device *dev,
  2225. struct se_node_acl *se_nacl,
  2226. struct t10_pr_registration *pr_reg,
  2227. int explicit,
  2228. int unreg)
  2229. {
  2230. const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
  2231. char i_buf[PR_REG_ISID_ID_LEN];
  2232. int pr_res_type = 0, pr_res_scope = 0;
  2233. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2234. core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
  2235. /*
  2236. * Go ahead and release the current PR reservation holder.
  2237. * If an All Registrants reservation is currently active and
  2238. * a unregister operation is requested, replace the current
  2239. * dev_pr_res_holder with another active registration.
  2240. */
  2241. if (dev->dev_pr_res_holder) {
  2242. pr_res_type = dev->dev_pr_res_holder->pr_res_type;
  2243. pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
  2244. dev->dev_pr_res_holder->pr_res_type = 0;
  2245. dev->dev_pr_res_holder->pr_res_scope = 0;
  2246. dev->dev_pr_res_holder->pr_res_holder = 0;
  2247. dev->dev_pr_res_holder = NULL;
  2248. }
  2249. if (!unreg)
  2250. goto out;
  2251. spin_lock(&dev->t10_pr.registration_lock);
  2252. list_del_init(&pr_reg->pr_reg_list);
  2253. /*
  2254. * If the I_T nexus is a reservation holder, the persistent reservation
  2255. * is of an all registrants type, and the I_T nexus is the last remaining
  2256. * registered I_T nexus, then the device server shall also release the
  2257. * persistent reservation.
  2258. */
  2259. if (!list_empty(&dev->t10_pr.registration_list) &&
  2260. ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2261. (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
  2262. dev->dev_pr_res_holder =
  2263. list_entry(dev->t10_pr.registration_list.next,
  2264. struct t10_pr_registration, pr_reg_list);
  2265. dev->dev_pr_res_holder->pr_res_type = pr_res_type;
  2266. dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
  2267. dev->dev_pr_res_holder->pr_res_holder = 1;
  2268. }
  2269. spin_unlock(&dev->t10_pr.registration_lock);
  2270. out:
  2271. if (!dev->dev_pr_res_holder) {
  2272. pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
  2273. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2274. tfo->get_fabric_name(), (explicit) ? "explicit" :
  2275. "implicit", core_scsi3_pr_dump_type(pr_res_type),
  2276. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2277. }
  2278. pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
  2279. tfo->get_fabric_name(), se_nacl->initiatorname,
  2280. i_buf);
  2281. /*
  2282. * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
  2283. */
  2284. pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
  2285. }
  2286. static sense_reason_t
  2287. core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
  2288. u64 res_key)
  2289. {
  2290. struct se_device *dev = cmd->se_dev;
  2291. struct se_session *se_sess = cmd->se_sess;
  2292. struct se_lun *se_lun = cmd->se_lun;
  2293. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
  2294. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  2295. sense_reason_t ret = 0;
  2296. if (!se_sess || !se_lun) {
  2297. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2298. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2299. }
  2300. /*
  2301. * Locate the existing *pr_reg via struct se_node_acl pointers
  2302. */
  2303. pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  2304. if (!pr_reg) {
  2305. pr_err("SPC-3 PR: Unable to locate"
  2306. " PR_REGISTERED *pr_reg for RELEASE\n");
  2307. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2308. }
  2309. /*
  2310. * From spc4r17 Section 5.7.11.2 Releasing:
  2311. *
  2312. * If there is no persistent reservation or in response to a persistent
  2313. * reservation release request from a registered I_T nexus that is not a
  2314. * persistent reservation holder (see 5.7.10), the device server shall
  2315. * do the following:
  2316. *
  2317. * a) Not release the persistent reservation, if any;
  2318. * b) Not remove any registrations; and
  2319. * c) Complete the command with GOOD status.
  2320. */
  2321. spin_lock(&dev->dev_reservation_lock);
  2322. pr_res_holder = dev->dev_pr_res_holder;
  2323. if (!pr_res_holder) {
  2324. /*
  2325. * No persistent reservation, return GOOD status.
  2326. */
  2327. spin_unlock(&dev->dev_reservation_lock);
  2328. goto out_put_pr_reg;
  2329. }
  2330. if (!is_reservation_holder(pr_res_holder, pr_reg)) {
  2331. /*
  2332. * Release request from a registered I_T nexus that is not a
  2333. * persistent reservation holder. return GOOD status.
  2334. */
  2335. spin_unlock(&dev->dev_reservation_lock);
  2336. goto out_put_pr_reg;
  2337. }
  2338. /*
  2339. * From spc4r17 Section 5.7.11.2 Releasing:
  2340. *
  2341. * Only the persistent reservation holder (see 5.7.10) is allowed to
  2342. * release a persistent reservation.
  2343. *
  2344. * An application client releases the persistent reservation by issuing
  2345. * a PERSISTENT RESERVE OUT command with RELEASE service action through
  2346. * an I_T nexus that is a persistent reservation holder with the
  2347. * following parameters:
  2348. *
  2349. * a) RESERVATION KEY field set to the value of the reservation key
  2350. * that is registered with the logical unit for the I_T nexus;
  2351. */
  2352. if (res_key != pr_reg->pr_res_key) {
  2353. pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
  2354. " does not match existing SA REGISTER res_key:"
  2355. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2356. spin_unlock(&dev->dev_reservation_lock);
  2357. ret = TCM_RESERVATION_CONFLICT;
  2358. goto out_put_pr_reg;
  2359. }
  2360. /*
  2361. * From spc4r17 Section 5.7.11.2 Releasing and above:
  2362. *
  2363. * b) TYPE field and SCOPE field set to match the persistent
  2364. * reservation being released.
  2365. */
  2366. if ((pr_res_holder->pr_res_type != type) ||
  2367. (pr_res_holder->pr_res_scope != scope)) {
  2368. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2369. pr_err("SPC-3 PR RELEASE: Attempted to release"
  2370. " reservation from [%s]: %s with different TYPE "
  2371. "and/or SCOPE while reservation already held by"
  2372. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2373. cmd->se_tfo->get_fabric_name(),
  2374. se_sess->se_node_acl->initiatorname,
  2375. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2376. pr_res_holder->pr_reg_nacl->initiatorname);
  2377. spin_unlock(&dev->dev_reservation_lock);
  2378. ret = TCM_RESERVATION_CONFLICT;
  2379. goto out_put_pr_reg;
  2380. }
  2381. /*
  2382. * In response to a persistent reservation release request from the
  2383. * persistent reservation holder the device server shall perform a
  2384. * release by doing the following as an uninterrupted series of actions:
  2385. * a) Release the persistent reservation;
  2386. * b) Not remove any registration(s);
  2387. * c) If the released persistent reservation is a registrants only type
  2388. * or all registrants type persistent reservation,
  2389. * the device server shall establish a unit attention condition for
  2390. * the initiator port associated with every regis-
  2391. * tered I_T nexus other than I_T nexus on which the PERSISTENT
  2392. * RESERVE OUT command with RELEASE service action was received,
  2393. * with the additional sense code set to RESERVATIONS RELEASED; and
  2394. * d) If the persistent reservation is of any other type, the device
  2395. * server shall not establish a unit attention condition.
  2396. */
  2397. __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
  2398. pr_reg, 1, 0);
  2399. spin_unlock(&dev->dev_reservation_lock);
  2400. if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
  2401. (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
  2402. (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
  2403. (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  2404. /*
  2405. * If no UNIT ATTENTION conditions will be established for
  2406. * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
  2407. * go ahead and check for APTPL=1 update+write below
  2408. */
  2409. goto write_aptpl;
  2410. }
  2411. spin_lock(&pr_tmpl->registration_lock);
  2412. list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
  2413. pr_reg_list) {
  2414. /*
  2415. * Do not establish a UNIT ATTENTION condition
  2416. * for the calling I_T Nexus
  2417. */
  2418. if (pr_reg_p == pr_reg)
  2419. continue;
  2420. target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
  2421. pr_reg_p->pr_res_mapped_lun,
  2422. 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
  2423. }
  2424. spin_unlock(&pr_tmpl->registration_lock);
  2425. write_aptpl:
  2426. if (pr_tmpl->pr_aptpl_active)
  2427. core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
  2428. out_put_pr_reg:
  2429. core_scsi3_put_pr_reg(pr_reg);
  2430. return ret;
  2431. }
  2432. static sense_reason_t
  2433. core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
  2434. {
  2435. struct se_device *dev = cmd->se_dev;
  2436. struct se_node_acl *pr_reg_nacl;
  2437. struct se_session *se_sess = cmd->se_sess;
  2438. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  2439. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2440. u64 pr_res_mapped_lun = 0;
  2441. int calling_it_nexus = 0;
  2442. /*
  2443. * Locate the existing *pr_reg via struct se_node_acl pointers
  2444. */
  2445. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
  2446. se_sess->se_node_acl, se_sess);
  2447. if (!pr_reg_n) {
  2448. pr_err("SPC-3 PR: Unable to locate"
  2449. " PR_REGISTERED *pr_reg for CLEAR\n");
  2450. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2451. }
  2452. /*
  2453. * From spc4r17 section 5.7.11.6, Clearing:
  2454. *
  2455. * Any application client may release the persistent reservation and
  2456. * remove all registrations from a device server by issuing a
  2457. * PERSISTENT RESERVE OUT command with CLEAR service action through a
  2458. * registered I_T nexus with the following parameter:
  2459. *
  2460. * a) RESERVATION KEY field set to the value of the reservation key
  2461. * that is registered with the logical unit for the I_T nexus.
  2462. */
  2463. if (res_key != pr_reg_n->pr_res_key) {
  2464. pr_err("SPC-3 PR REGISTER: Received"
  2465. " res_key: 0x%016Lx does not match"
  2466. " existing SA REGISTER res_key:"
  2467. " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
  2468. core_scsi3_put_pr_reg(pr_reg_n);
  2469. return TCM_RESERVATION_CONFLICT;
  2470. }
  2471. /*
  2472. * a) Release the persistent reservation, if any;
  2473. */
  2474. spin_lock(&dev->dev_reservation_lock);
  2475. pr_res_holder = dev->dev_pr_res_holder;
  2476. if (pr_res_holder) {
  2477. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2478. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  2479. pr_res_holder, 0, 0);
  2480. }
  2481. spin_unlock(&dev->dev_reservation_lock);
  2482. /*
  2483. * b) Remove all registration(s) (see spc4r17 5.7.7);
  2484. */
  2485. spin_lock(&pr_tmpl->registration_lock);
  2486. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2487. &pr_tmpl->registration_list, pr_reg_list) {
  2488. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2489. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2490. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2491. __core_scsi3_free_registration(dev, pr_reg, NULL,
  2492. calling_it_nexus);
  2493. /*
  2494. * e) Establish a unit attention condition for the initiator
  2495. * port associated with every registered I_T nexus other
  2496. * than the I_T nexus on which the PERSISTENT RESERVE OUT
  2497. * command with CLEAR service action was received, with the
  2498. * additional sense code set to RESERVATIONS PREEMPTED.
  2499. */
  2500. if (!calling_it_nexus)
  2501. target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
  2502. 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
  2503. }
  2504. spin_unlock(&pr_tmpl->registration_lock);
  2505. pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
  2506. cmd->se_tfo->get_fabric_name());
  2507. core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
  2508. core_scsi3_pr_generation(dev);
  2509. return 0;
  2510. }
  2511. /*
  2512. * Called with struct se_device->dev_reservation_lock held.
  2513. */
  2514. static void __core_scsi3_complete_pro_preempt(
  2515. struct se_device *dev,
  2516. struct t10_pr_registration *pr_reg,
  2517. struct list_head *preempt_and_abort_list,
  2518. int type,
  2519. int scope,
  2520. enum preempt_type preempt_type)
  2521. {
  2522. struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
  2523. const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
  2524. char i_buf[PR_REG_ISID_ID_LEN];
  2525. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2526. core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
  2527. /*
  2528. * Do an implicit RELEASE of the existing reservation.
  2529. */
  2530. if (dev->dev_pr_res_holder)
  2531. __core_scsi3_complete_pro_release(dev, nacl,
  2532. dev->dev_pr_res_holder, 0, 0);
  2533. dev->dev_pr_res_holder = pr_reg;
  2534. pr_reg->pr_res_holder = 1;
  2535. pr_reg->pr_res_type = type;
  2536. pr_reg->pr_res_scope = scope;
  2537. pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
  2538. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2539. tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
  2540. core_scsi3_pr_dump_type(type),
  2541. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2542. pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
  2543. tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
  2544. nacl->initiatorname, i_buf);
  2545. /*
  2546. * For PREEMPT_AND_ABORT, add the preempting reservation's
  2547. * struct t10_pr_registration to the list that will be compared
  2548. * against received CDBs..
  2549. */
  2550. if (preempt_and_abort_list)
  2551. list_add_tail(&pr_reg->pr_reg_abort_list,
  2552. preempt_and_abort_list);
  2553. }
  2554. static void core_scsi3_release_preempt_and_abort(
  2555. struct list_head *preempt_and_abort_list,
  2556. struct t10_pr_registration *pr_reg_holder)
  2557. {
  2558. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  2559. list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
  2560. pr_reg_abort_list) {
  2561. list_del(&pr_reg->pr_reg_abort_list);
  2562. if (pr_reg_holder == pr_reg)
  2563. continue;
  2564. if (pr_reg->pr_res_holder) {
  2565. pr_warn("pr_reg->pr_res_holder still set\n");
  2566. continue;
  2567. }
  2568. pr_reg->pr_reg_deve = NULL;
  2569. pr_reg->pr_reg_nacl = NULL;
  2570. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  2571. }
  2572. }
  2573. static sense_reason_t
  2574. core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
  2575. u64 sa_res_key, enum preempt_type preempt_type)
  2576. {
  2577. struct se_device *dev = cmd->se_dev;
  2578. struct se_node_acl *pr_reg_nacl;
  2579. struct se_session *se_sess = cmd->se_sess;
  2580. LIST_HEAD(preempt_and_abort_list);
  2581. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2582. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  2583. u64 pr_res_mapped_lun = 0;
  2584. int all_reg = 0, calling_it_nexus = 0;
  2585. bool sa_res_key_unmatched = sa_res_key != 0;
  2586. int prh_type = 0, prh_scope = 0;
  2587. if (!se_sess)
  2588. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2589. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2590. se_sess);
  2591. if (!pr_reg_n) {
  2592. pr_err("SPC-3 PR: Unable to locate"
  2593. " PR_REGISTERED *pr_reg for PREEMPT%s\n",
  2594. (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
  2595. return TCM_RESERVATION_CONFLICT;
  2596. }
  2597. if (pr_reg_n->pr_res_key != res_key) {
  2598. core_scsi3_put_pr_reg(pr_reg_n);
  2599. return TCM_RESERVATION_CONFLICT;
  2600. }
  2601. if (scope != PR_SCOPE_LU_SCOPE) {
  2602. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2603. core_scsi3_put_pr_reg(pr_reg_n);
  2604. return TCM_INVALID_PARAMETER_LIST;
  2605. }
  2606. spin_lock(&dev->dev_reservation_lock);
  2607. pr_res_holder = dev->dev_pr_res_holder;
  2608. if (pr_res_holder &&
  2609. ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2610. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
  2611. all_reg = 1;
  2612. if (!all_reg && !sa_res_key) {
  2613. spin_unlock(&dev->dev_reservation_lock);
  2614. core_scsi3_put_pr_reg(pr_reg_n);
  2615. return TCM_INVALID_PARAMETER_LIST;
  2616. }
  2617. /*
  2618. * From spc4r17, section 5.7.11.4.4 Removing Registrations:
  2619. *
  2620. * If the SERVICE ACTION RESERVATION KEY field does not identify a
  2621. * persistent reservation holder or there is no persistent reservation
  2622. * holder (i.e., there is no persistent reservation), then the device
  2623. * server shall perform a preempt by doing the following in an
  2624. * uninterrupted series of actions. (See below..)
  2625. */
  2626. if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
  2627. /*
  2628. * No existing or SA Reservation Key matching reservations..
  2629. *
  2630. * PROUT SA PREEMPT with All Registrant type reservations are
  2631. * allowed to be processed without a matching SA Reservation Key
  2632. */
  2633. spin_lock(&pr_tmpl->registration_lock);
  2634. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2635. &pr_tmpl->registration_list, pr_reg_list) {
  2636. /*
  2637. * Removing of registrations in non all registrants
  2638. * type reservations without a matching SA reservation
  2639. * key.
  2640. *
  2641. * a) Remove the registrations for all I_T nexuses
  2642. * specified by the SERVICE ACTION RESERVATION KEY
  2643. * field;
  2644. * b) Ignore the contents of the SCOPE and TYPE fields;
  2645. * c) Process tasks as defined in 5.7.1; and
  2646. * d) Establish a unit attention condition for the
  2647. * initiator port associated with every I_T nexus
  2648. * that lost its registration other than the I_T
  2649. * nexus on which the PERSISTENT RESERVE OUT command
  2650. * was received, with the additional sense code set
  2651. * to REGISTRATIONS PREEMPTED.
  2652. */
  2653. if (!all_reg) {
  2654. if (pr_reg->pr_res_key != sa_res_key)
  2655. continue;
  2656. sa_res_key_unmatched = false;
  2657. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2658. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2659. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2660. __core_scsi3_free_registration(dev, pr_reg,
  2661. (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
  2662. NULL, calling_it_nexus);
  2663. } else {
  2664. /*
  2665. * Case for any existing all registrants type
  2666. * reservation, follow logic in spc4r17 section
  2667. * 5.7.11.4 Preempting, Table 52 and Figure 7.
  2668. *
  2669. * For a ZERO SA Reservation key, release
  2670. * all other registrations and do an implicit
  2671. * release of active persistent reservation.
  2672. *
  2673. * For a non-ZERO SA Reservation key, only
  2674. * release the matching reservation key from
  2675. * registrations.
  2676. */
  2677. if ((sa_res_key) &&
  2678. (pr_reg->pr_res_key != sa_res_key))
  2679. continue;
  2680. sa_res_key_unmatched = false;
  2681. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2682. if (calling_it_nexus)
  2683. continue;
  2684. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2685. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2686. __core_scsi3_free_registration(dev, pr_reg,
  2687. (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
  2688. NULL, 0);
  2689. }
  2690. if (!calling_it_nexus)
  2691. target_ua_allocate_lun(pr_reg_nacl,
  2692. pr_res_mapped_lun, 0x2A,
  2693. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2694. }
  2695. spin_unlock(&pr_tmpl->registration_lock);
  2696. /*
  2697. * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
  2698. * a PREEMPT AND ABORT service action sets the SERVICE ACTION
  2699. * RESERVATION KEY field to a value that does not match any
  2700. * registered reservation key, then the device server shall
  2701. * complete the command with RESERVATION CONFLICT status.
  2702. */
  2703. if (sa_res_key_unmatched) {
  2704. spin_unlock(&dev->dev_reservation_lock);
  2705. core_scsi3_put_pr_reg(pr_reg_n);
  2706. return TCM_RESERVATION_CONFLICT;
  2707. }
  2708. /*
  2709. * For an existing all registrants type reservation
  2710. * with a zero SA rservation key, preempt the existing
  2711. * reservation with the new PR type and scope.
  2712. */
  2713. if (pr_res_holder && all_reg && !(sa_res_key)) {
  2714. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2715. (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
  2716. type, scope, preempt_type);
  2717. if (preempt_type == PREEMPT_AND_ABORT)
  2718. core_scsi3_release_preempt_and_abort(
  2719. &preempt_and_abort_list, pr_reg_n);
  2720. }
  2721. spin_unlock(&dev->dev_reservation_lock);
  2722. if (pr_tmpl->pr_aptpl_active)
  2723. core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
  2724. core_scsi3_put_pr_reg(pr_reg_n);
  2725. core_scsi3_pr_generation(cmd->se_dev);
  2726. return 0;
  2727. }
  2728. /*
  2729. * The PREEMPTing SA reservation key matches that of the
  2730. * existing persistent reservation, first, we check if
  2731. * we are preempting our own reservation.
  2732. * From spc4r17, section 5.7.11.4.3 Preempting
  2733. * persistent reservations and registration handling
  2734. *
  2735. * If an all registrants persistent reservation is not
  2736. * present, it is not an error for the persistent
  2737. * reservation holder to preempt itself (i.e., a
  2738. * PERSISTENT RESERVE OUT with a PREEMPT service action
  2739. * or a PREEMPT AND ABORT service action with the
  2740. * SERVICE ACTION RESERVATION KEY value equal to the
  2741. * persistent reservation holder's reservation key that
  2742. * is received from the persistent reservation holder).
  2743. * In that case, the device server shall establish the
  2744. * new persistent reservation and maintain the
  2745. * registration.
  2746. */
  2747. prh_type = pr_res_holder->pr_res_type;
  2748. prh_scope = pr_res_holder->pr_res_scope;
  2749. /*
  2750. * If the SERVICE ACTION RESERVATION KEY field identifies a
  2751. * persistent reservation holder (see 5.7.10), the device
  2752. * server shall perform a preempt by doing the following as
  2753. * an uninterrupted series of actions:
  2754. *
  2755. * a) Release the persistent reservation for the holder
  2756. * identified by the SERVICE ACTION RESERVATION KEY field;
  2757. */
  2758. if (pr_reg_n != pr_res_holder)
  2759. __core_scsi3_complete_pro_release(dev,
  2760. pr_res_holder->pr_reg_nacl,
  2761. dev->dev_pr_res_holder, 0, 0);
  2762. /*
  2763. * b) Remove the registrations for all I_T nexuses identified
  2764. * by the SERVICE ACTION RESERVATION KEY field, except the
  2765. * I_T nexus that is being used for the PERSISTENT RESERVE
  2766. * OUT command. If an all registrants persistent reservation
  2767. * is present and the SERVICE ACTION RESERVATION KEY field
  2768. * is set to zero, then all registrations shall be removed
  2769. * except for that of the I_T nexus that is being used for
  2770. * the PERSISTENT RESERVE OUT command;
  2771. */
  2772. spin_lock(&pr_tmpl->registration_lock);
  2773. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2774. &pr_tmpl->registration_list, pr_reg_list) {
  2775. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2776. if (calling_it_nexus)
  2777. continue;
  2778. if (pr_reg->pr_res_key != sa_res_key)
  2779. continue;
  2780. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2781. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2782. __core_scsi3_free_registration(dev, pr_reg,
  2783. (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
  2784. calling_it_nexus);
  2785. /*
  2786. * e) Establish a unit attention condition for the initiator
  2787. * port associated with every I_T nexus that lost its
  2788. * persistent reservation and/or registration, with the
  2789. * additional sense code set to REGISTRATIONS PREEMPTED;
  2790. */
  2791. target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
  2792. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2793. }
  2794. spin_unlock(&pr_tmpl->registration_lock);
  2795. /*
  2796. * c) Establish a persistent reservation for the preempting
  2797. * I_T nexus using the contents of the SCOPE and TYPE fields;
  2798. */
  2799. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2800. (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
  2801. type, scope, preempt_type);
  2802. /*
  2803. * d) Process tasks as defined in 5.7.1;
  2804. * e) See above..
  2805. * f) If the type or scope has changed, then for every I_T nexus
  2806. * whose reservation key was not removed, except for the I_T
  2807. * nexus on which the PERSISTENT RESERVE OUT command was
  2808. * received, the device server shall establish a unit
  2809. * attention condition for the initiator port associated with
  2810. * that I_T nexus, with the additional sense code set to
  2811. * RESERVATIONS RELEASED. If the type or scope have not
  2812. * changed, then no unit attention condition(s) shall be
  2813. * established for this reason.
  2814. */
  2815. if ((prh_type != type) || (prh_scope != scope)) {
  2816. spin_lock(&pr_tmpl->registration_lock);
  2817. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2818. &pr_tmpl->registration_list, pr_reg_list) {
  2819. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2820. if (calling_it_nexus)
  2821. continue;
  2822. target_ua_allocate_lun(pr_reg->pr_reg_nacl,
  2823. pr_reg->pr_res_mapped_lun, 0x2A,
  2824. ASCQ_2AH_RESERVATIONS_RELEASED);
  2825. }
  2826. spin_unlock(&pr_tmpl->registration_lock);
  2827. }
  2828. spin_unlock(&dev->dev_reservation_lock);
  2829. /*
  2830. * Call LUN_RESET logic upon list of struct t10_pr_registration,
  2831. * All received CDBs for the matching existing reservation and
  2832. * registrations undergo ABORT_TASK logic.
  2833. *
  2834. * From there, core_scsi3_release_preempt_and_abort() will
  2835. * release every registration in the list (which have already
  2836. * been removed from the primary pr_reg list), except the
  2837. * new persistent reservation holder, the calling Initiator Port.
  2838. */
  2839. if (preempt_type == PREEMPT_AND_ABORT) {
  2840. core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
  2841. core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
  2842. pr_reg_n);
  2843. }
  2844. if (pr_tmpl->pr_aptpl_active)
  2845. core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
  2846. core_scsi3_put_pr_reg(pr_reg_n);
  2847. core_scsi3_pr_generation(cmd->se_dev);
  2848. return 0;
  2849. }
  2850. static sense_reason_t
  2851. core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
  2852. u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
  2853. {
  2854. switch (type) {
  2855. case PR_TYPE_WRITE_EXCLUSIVE:
  2856. case PR_TYPE_EXCLUSIVE_ACCESS:
  2857. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2858. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2859. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2860. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2861. return core_scsi3_pro_preempt(cmd, type, scope, res_key,
  2862. sa_res_key, preempt_type);
  2863. default:
  2864. pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
  2865. " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
  2866. return TCM_INVALID_CDB_FIELD;
  2867. }
  2868. }
  2869. static sense_reason_t
  2870. core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
  2871. u64 sa_res_key, int aptpl, int unreg)
  2872. {
  2873. struct se_session *se_sess = cmd->se_sess;
  2874. struct se_device *dev = cmd->se_dev;
  2875. struct se_dev_entry *dest_se_deve = NULL;
  2876. struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
  2877. struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
  2878. struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
  2879. const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
  2880. struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
  2881. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  2882. unsigned char *buf;
  2883. const unsigned char *initiator_str;
  2884. char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
  2885. u32 tid_len, tmp_tid_len;
  2886. int new_reg = 0, type, scope, matching_iname;
  2887. sense_reason_t ret;
  2888. unsigned short rtpi;
  2889. unsigned char proto_ident;
  2890. if (!se_sess || !se_lun) {
  2891. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2892. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2893. }
  2894. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2895. se_tpg = se_sess->se_tpg;
  2896. tf_ops = se_tpg->se_tpg_tfo;
  2897. /*
  2898. * Follow logic from spc4r17 Section 5.7.8, Table 50 --
  2899. * Register behaviors for a REGISTER AND MOVE service action
  2900. *
  2901. * Locate the existing *pr_reg via struct se_node_acl pointers
  2902. */
  2903. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2904. se_sess);
  2905. if (!pr_reg) {
  2906. pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
  2907. " *pr_reg for REGISTER_AND_MOVE\n");
  2908. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2909. }
  2910. /*
  2911. * The provided reservation key much match the existing reservation key
  2912. * provided during this initiator's I_T nexus registration.
  2913. */
  2914. if (res_key != pr_reg->pr_res_key) {
  2915. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
  2916. " res_key: 0x%016Lx does not match existing SA REGISTER"
  2917. " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2918. ret = TCM_RESERVATION_CONFLICT;
  2919. goto out_put_pr_reg;
  2920. }
  2921. /*
  2922. * The service active reservation key needs to be non zero
  2923. */
  2924. if (!sa_res_key) {
  2925. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
  2926. " sa_res_key\n");
  2927. ret = TCM_INVALID_PARAMETER_LIST;
  2928. goto out_put_pr_reg;
  2929. }
  2930. /*
  2931. * Determine the Relative Target Port Identifier where the reservation
  2932. * will be moved to for the TransportID containing SCSI initiator WWN
  2933. * information.
  2934. */
  2935. buf = transport_kmap_data_sg(cmd);
  2936. if (!buf) {
  2937. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2938. goto out_put_pr_reg;
  2939. }
  2940. rtpi = (buf[18] & 0xff) << 8;
  2941. rtpi |= buf[19] & 0xff;
  2942. tid_len = (buf[20] & 0xff) << 24;
  2943. tid_len |= (buf[21] & 0xff) << 16;
  2944. tid_len |= (buf[22] & 0xff) << 8;
  2945. tid_len |= buf[23] & 0xff;
  2946. transport_kunmap_data_sg(cmd);
  2947. buf = NULL;
  2948. if ((tid_len + 24) != cmd->data_length) {
  2949. pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
  2950. " does not equal CDB data_length: %u\n", tid_len,
  2951. cmd->data_length);
  2952. ret = TCM_INVALID_PARAMETER_LIST;
  2953. goto out_put_pr_reg;
  2954. }
  2955. spin_lock(&dev->se_port_lock);
  2956. list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
  2957. if (tmp_lun->lun_rtpi != rtpi)
  2958. continue;
  2959. dest_se_tpg = tmp_lun->lun_tpg;
  2960. dest_tf_ops = dest_se_tpg->se_tpg_tfo;
  2961. if (!dest_tf_ops)
  2962. continue;
  2963. atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
  2964. spin_unlock(&dev->se_port_lock);
  2965. if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
  2966. pr_err("core_scsi3_tpg_depend_item() failed"
  2967. " for dest_se_tpg\n");
  2968. atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
  2969. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2970. goto out_put_pr_reg;
  2971. }
  2972. spin_lock(&dev->se_port_lock);
  2973. break;
  2974. }
  2975. spin_unlock(&dev->se_port_lock);
  2976. if (!dest_se_tpg || !dest_tf_ops) {
  2977. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  2978. " fabric ops from Relative Target Port Identifier:"
  2979. " %hu\n", rtpi);
  2980. ret = TCM_INVALID_PARAMETER_LIST;
  2981. goto out_put_pr_reg;
  2982. }
  2983. buf = transport_kmap_data_sg(cmd);
  2984. if (!buf) {
  2985. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2986. goto out_put_pr_reg;
  2987. }
  2988. proto_ident = (buf[24] & 0x0f);
  2989. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
  2990. " 0x%02x\n", proto_ident);
  2991. if (proto_ident != dest_se_tpg->proto_id) {
  2992. pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
  2993. " proto_ident: 0x%02x does not match ident: 0x%02x"
  2994. " from fabric: %s\n", proto_ident,
  2995. dest_se_tpg->proto_id,
  2996. dest_tf_ops->get_fabric_name());
  2997. ret = TCM_INVALID_PARAMETER_LIST;
  2998. goto out;
  2999. }
  3000. initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
  3001. (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
  3002. if (!initiator_str) {
  3003. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  3004. " initiator_str from Transport ID\n");
  3005. ret = TCM_INVALID_PARAMETER_LIST;
  3006. goto out;
  3007. }
  3008. transport_kunmap_data_sg(cmd);
  3009. buf = NULL;
  3010. pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
  3011. " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
  3012. "port" : "device", initiator_str, (iport_ptr != NULL) ?
  3013. iport_ptr : "");
  3014. /*
  3015. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3016. * action specifies a TransportID that is the same as the initiator port
  3017. * of the I_T nexus for the command received, then the command shall
  3018. * be terminated with CHECK CONDITION status, with the sense key set to
  3019. * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
  3020. * IN PARAMETER LIST.
  3021. */
  3022. pr_reg_nacl = pr_reg->pr_reg_nacl;
  3023. matching_iname = (!strcmp(initiator_str,
  3024. pr_reg_nacl->initiatorname)) ? 1 : 0;
  3025. if (!matching_iname)
  3026. goto after_iport_check;
  3027. if (!iport_ptr || !pr_reg->isid_present_at_reg) {
  3028. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
  3029. " matches: %s on received I_T Nexus\n", initiator_str,
  3030. pr_reg_nacl->initiatorname);
  3031. ret = TCM_INVALID_PARAMETER_LIST;
  3032. goto out;
  3033. }
  3034. if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
  3035. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
  3036. " matches: %s %s on received I_T Nexus\n",
  3037. initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
  3038. pr_reg->pr_reg_isid);
  3039. ret = TCM_INVALID_PARAMETER_LIST;
  3040. goto out;
  3041. }
  3042. after_iport_check:
  3043. /*
  3044. * Locate the destination struct se_node_acl from the received Transport ID
  3045. */
  3046. mutex_lock(&dest_se_tpg->acl_node_mutex);
  3047. dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
  3048. initiator_str);
  3049. if (dest_node_acl)
  3050. atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
  3051. mutex_unlock(&dest_se_tpg->acl_node_mutex);
  3052. if (!dest_node_acl) {
  3053. pr_err("Unable to locate %s dest_node_acl for"
  3054. " TransportID%s\n", dest_tf_ops->get_fabric_name(),
  3055. initiator_str);
  3056. ret = TCM_INVALID_PARAMETER_LIST;
  3057. goto out;
  3058. }
  3059. if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
  3060. pr_err("core_scsi3_nodeacl_depend_item() for"
  3061. " dest_node_acl\n");
  3062. atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
  3063. dest_node_acl = NULL;
  3064. ret = TCM_INVALID_PARAMETER_LIST;
  3065. goto out;
  3066. }
  3067. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
  3068. " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
  3069. dest_node_acl->initiatorname);
  3070. /*
  3071. * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
  3072. * PORT IDENTIFIER.
  3073. */
  3074. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
  3075. if (!dest_se_deve) {
  3076. pr_err("Unable to locate %s dest_se_deve from RTPI:"
  3077. " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
  3078. ret = TCM_INVALID_PARAMETER_LIST;
  3079. goto out;
  3080. }
  3081. if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
  3082. pr_err("core_scsi3_lunacl_depend_item() failed\n");
  3083. kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
  3084. dest_se_deve = NULL;
  3085. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3086. goto out;
  3087. }
  3088. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
  3089. " ACL for dest_se_deve->mapped_lun: %llu\n",
  3090. dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
  3091. dest_se_deve->mapped_lun);
  3092. /*
  3093. * A persistent reservation needs to already existing in order to
  3094. * successfully complete the REGISTER_AND_MOVE service action..
  3095. */
  3096. spin_lock(&dev->dev_reservation_lock);
  3097. pr_res_holder = dev->dev_pr_res_holder;
  3098. if (!pr_res_holder) {
  3099. pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
  3100. " currently held\n");
  3101. spin_unlock(&dev->dev_reservation_lock);
  3102. ret = TCM_INVALID_CDB_FIELD;
  3103. goto out;
  3104. }
  3105. /*
  3106. * The received on I_T Nexus must be the reservation holder.
  3107. *
  3108. * From spc4r17 section 5.7.8 Table 50 --
  3109. * Register behaviors for a REGISTER AND MOVE service action
  3110. */
  3111. if (!is_reservation_holder(pr_res_holder, pr_reg)) {
  3112. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
  3113. " Nexus is not reservation holder\n");
  3114. spin_unlock(&dev->dev_reservation_lock);
  3115. ret = TCM_RESERVATION_CONFLICT;
  3116. goto out;
  3117. }
  3118. /*
  3119. * From spc4r17 section 5.7.8: registering and moving reservation
  3120. *
  3121. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3122. * action is received and the established persistent reservation is a
  3123. * Write Exclusive - All Registrants type or Exclusive Access -
  3124. * All Registrants type reservation, then the command shall be completed
  3125. * with RESERVATION CONFLICT status.
  3126. */
  3127. if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3128. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  3129. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
  3130. " reservation for type: %s\n",
  3131. core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
  3132. spin_unlock(&dev->dev_reservation_lock);
  3133. ret = TCM_RESERVATION_CONFLICT;
  3134. goto out;
  3135. }
  3136. pr_res_nacl = pr_res_holder->pr_reg_nacl;
  3137. /*
  3138. * b) Ignore the contents of the (received) SCOPE and TYPE fields;
  3139. */
  3140. type = pr_res_holder->pr_res_type;
  3141. scope = pr_res_holder->pr_res_type;
  3142. /*
  3143. * c) Associate the reservation key specified in the SERVICE ACTION
  3144. * RESERVATION KEY field with the I_T nexus specified as the
  3145. * destination of the register and move, where:
  3146. * A) The I_T nexus is specified by the TransportID and the
  3147. * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
  3148. * B) Regardless of the TransportID format used, the association for
  3149. * the initiator port is based on either the initiator port name
  3150. * (see 3.1.71) on SCSI transport protocols where port names are
  3151. * required or the initiator port identifier (see 3.1.70) on SCSI
  3152. * transport protocols where port names are not required;
  3153. * d) Register the reservation key specified in the SERVICE ACTION
  3154. * RESERVATION KEY field;
  3155. * e) Retain the reservation key specified in the SERVICE ACTION
  3156. * RESERVATION KEY field and associated information;
  3157. *
  3158. * Also, It is not an error for a REGISTER AND MOVE service action to
  3159. * register an I_T nexus that is already registered with the same
  3160. * reservation key or a different reservation key.
  3161. */
  3162. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3163. iport_ptr);
  3164. if (!dest_pr_reg) {
  3165. struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
  3166. atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
  3167. spin_unlock(&dev->dev_reservation_lock);
  3168. if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
  3169. dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
  3170. iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
  3171. ret = TCM_INVALID_PARAMETER_LIST;
  3172. goto out;
  3173. }
  3174. spin_lock(&dev->dev_reservation_lock);
  3175. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3176. iport_ptr);
  3177. new_reg = 1;
  3178. }
  3179. /*
  3180. * f) Release the persistent reservation for the persistent reservation
  3181. * holder (i.e., the I_T nexus on which the
  3182. */
  3183. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  3184. dev->dev_pr_res_holder, 0, 0);
  3185. /*
  3186. * g) Move the persistent reservation to the specified I_T nexus using
  3187. * the same scope and type as the persistent reservation released in
  3188. * item f); and
  3189. */
  3190. dev->dev_pr_res_holder = dest_pr_reg;
  3191. dest_pr_reg->pr_res_holder = 1;
  3192. dest_pr_reg->pr_res_type = type;
  3193. pr_reg->pr_res_scope = scope;
  3194. core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
  3195. /*
  3196. * Increment PRGeneration for existing registrations..
  3197. */
  3198. if (!new_reg)
  3199. dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
  3200. spin_unlock(&dev->dev_reservation_lock);
  3201. pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
  3202. " created new reservation holder TYPE: %s on object RTPI:"
  3203. " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
  3204. core_scsi3_pr_dump_type(type), rtpi,
  3205. dest_pr_reg->pr_res_generation);
  3206. pr_debug("SPC-3 PR Successfully moved reservation from"
  3207. " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
  3208. tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
  3209. i_buf, dest_tf_ops->get_fabric_name(),
  3210. dest_node_acl->initiatorname, (iport_ptr != NULL) ?
  3211. iport_ptr : "");
  3212. /*
  3213. * It is now safe to release configfs group dependencies for destination
  3214. * of Transport ID Initiator Device/Port Identifier
  3215. */
  3216. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3217. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3218. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3219. /*
  3220. * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
  3221. * nexus on which PERSISTENT RESERVE OUT command was received.
  3222. */
  3223. if (unreg) {
  3224. spin_lock(&pr_tmpl->registration_lock);
  3225. __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
  3226. spin_unlock(&pr_tmpl->registration_lock);
  3227. } else
  3228. core_scsi3_put_pr_reg(pr_reg);
  3229. core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
  3230. transport_kunmap_data_sg(cmd);
  3231. core_scsi3_put_pr_reg(dest_pr_reg);
  3232. return 0;
  3233. out:
  3234. if (buf)
  3235. transport_kunmap_data_sg(cmd);
  3236. if (dest_se_deve)
  3237. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3238. if (dest_node_acl)
  3239. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3240. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3241. out_put_pr_reg:
  3242. core_scsi3_put_pr_reg(pr_reg);
  3243. return ret;
  3244. }
  3245. static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
  3246. {
  3247. unsigned int __v1, __v2;
  3248. __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
  3249. __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
  3250. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  3251. }
  3252. /*
  3253. * See spc4r17 section 6.14 Table 170
  3254. */
  3255. sense_reason_t
  3256. target_scsi3_emulate_pr_out(struct se_cmd *cmd)
  3257. {
  3258. struct se_device *dev = cmd->se_dev;
  3259. unsigned char *cdb = &cmd->t_task_cdb[0];
  3260. unsigned char *buf;
  3261. u64 res_key, sa_res_key;
  3262. int sa, scope, type, aptpl;
  3263. int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
  3264. sense_reason_t ret;
  3265. /*
  3266. * Following spc2r20 5.5.1 Reservations overview:
  3267. *
  3268. * If a logical unit has been reserved by any RESERVE command and is
  3269. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3270. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3271. * initiator or service action and shall terminate with a RESERVATION
  3272. * CONFLICT status.
  3273. */
  3274. if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
  3275. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3276. " SPC-2 reservation is held, returning"
  3277. " RESERVATION_CONFLICT\n");
  3278. return TCM_RESERVATION_CONFLICT;
  3279. }
  3280. /*
  3281. * FIXME: A NULL struct se_session pointer means an this is not coming from
  3282. * a $FABRIC_MOD's nexus, but from internal passthrough ops.
  3283. */
  3284. if (!cmd->se_sess)
  3285. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3286. if (cmd->data_length < 24) {
  3287. pr_warn("SPC-PR: Received PR OUT parameter list"
  3288. " length too small: %u\n", cmd->data_length);
  3289. return TCM_INVALID_PARAMETER_LIST;
  3290. }
  3291. /*
  3292. * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
  3293. */
  3294. sa = (cdb[1] & 0x1f);
  3295. scope = (cdb[2] & 0xf0);
  3296. type = (cdb[2] & 0x0f);
  3297. buf = transport_kmap_data_sg(cmd);
  3298. if (!buf)
  3299. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3300. /*
  3301. * From PERSISTENT_RESERVE_OUT parameter list (payload)
  3302. */
  3303. res_key = core_scsi3_extract_reservation_key(&buf[0]);
  3304. sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
  3305. /*
  3306. * REGISTER_AND_MOVE uses a different SA parameter list containing
  3307. * SCSI TransportIDs.
  3308. */
  3309. if (sa != PRO_REGISTER_AND_MOVE) {
  3310. spec_i_pt = (buf[20] & 0x08);
  3311. all_tg_pt = (buf[20] & 0x04);
  3312. aptpl = (buf[20] & 0x01);
  3313. } else {
  3314. aptpl = (buf[17] & 0x01);
  3315. unreg = (buf[17] & 0x02);
  3316. }
  3317. /*
  3318. * If the backend device has been configured to force APTPL metadata
  3319. * write-out, go ahead and propigate aptpl=1 down now.
  3320. */
  3321. if (dev->dev_attrib.force_pr_aptpl)
  3322. aptpl = 1;
  3323. transport_kunmap_data_sg(cmd);
  3324. buf = NULL;
  3325. /*
  3326. * SPEC_I_PT=1 is only valid for Service action: REGISTER
  3327. */
  3328. if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
  3329. return TCM_INVALID_PARAMETER_LIST;
  3330. /*
  3331. * From spc4r17 section 6.14:
  3332. *
  3333. * If the SPEC_I_PT bit is set to zero, the service action is not
  3334. * REGISTER AND MOVE, and the parameter list length is not 24, then
  3335. * the command shall be terminated with CHECK CONDITION status, with
  3336. * the sense key set to ILLEGAL REQUEST, and the additional sense
  3337. * code set to PARAMETER LIST LENGTH ERROR.
  3338. */
  3339. if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
  3340. (cmd->data_length != 24)) {
  3341. pr_warn("SPC-PR: Received PR OUT illegal parameter"
  3342. " list length: %u\n", cmd->data_length);
  3343. return TCM_INVALID_PARAMETER_LIST;
  3344. }
  3345. /*
  3346. * (core_scsi3_emulate_pro_* function parameters
  3347. * are defined by spc4r17 Table 174:
  3348. * PERSISTENT_RESERVE_OUT service actions and valid parameters.
  3349. */
  3350. switch (sa) {
  3351. case PRO_REGISTER:
  3352. ret = core_scsi3_emulate_pro_register(cmd,
  3353. res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
  3354. break;
  3355. case PRO_RESERVE:
  3356. ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
  3357. break;
  3358. case PRO_RELEASE:
  3359. ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
  3360. break;
  3361. case PRO_CLEAR:
  3362. ret = core_scsi3_emulate_pro_clear(cmd, res_key);
  3363. break;
  3364. case PRO_PREEMPT:
  3365. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3366. res_key, sa_res_key, PREEMPT);
  3367. break;
  3368. case PRO_PREEMPT_AND_ABORT:
  3369. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3370. res_key, sa_res_key, PREEMPT_AND_ABORT);
  3371. break;
  3372. case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
  3373. ret = core_scsi3_emulate_pro_register(cmd,
  3374. 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
  3375. break;
  3376. case PRO_REGISTER_AND_MOVE:
  3377. ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
  3378. sa_res_key, aptpl, unreg);
  3379. break;
  3380. default:
  3381. pr_err("Unknown PERSISTENT_RESERVE_OUT service"
  3382. " action: 0x%02x\n", cdb[1] & 0x1f);
  3383. return TCM_INVALID_CDB_FIELD;
  3384. }
  3385. if (!ret)
  3386. target_complete_cmd(cmd, GOOD);
  3387. return ret;
  3388. }
  3389. /*
  3390. * PERSISTENT_RESERVE_IN Service Action READ_KEYS
  3391. *
  3392. * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
  3393. */
  3394. static sense_reason_t
  3395. core_scsi3_pri_read_keys(struct se_cmd *cmd)
  3396. {
  3397. struct se_device *dev = cmd->se_dev;
  3398. struct t10_pr_registration *pr_reg;
  3399. unsigned char *buf;
  3400. u32 add_len = 0, off = 8;
  3401. if (cmd->data_length < 8) {
  3402. pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
  3403. " too small\n", cmd->data_length);
  3404. return TCM_INVALID_CDB_FIELD;
  3405. }
  3406. buf = transport_kmap_data_sg(cmd);
  3407. if (!buf)
  3408. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3409. buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
  3410. buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
  3411. buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
  3412. buf[3] = (dev->t10_pr.pr_generation & 0xff);
  3413. spin_lock(&dev->t10_pr.registration_lock);
  3414. list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
  3415. pr_reg_list) {
  3416. /*
  3417. * Check for overflow of 8byte PRI READ_KEYS payload and
  3418. * next reservation key list descriptor.
  3419. */
  3420. if ((add_len + 8) > (cmd->data_length - 8))
  3421. break;
  3422. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3423. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3424. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3425. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3426. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3427. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3428. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3429. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3430. add_len += 8;
  3431. }
  3432. spin_unlock(&dev->t10_pr.registration_lock);
  3433. buf[4] = ((add_len >> 24) & 0xff);
  3434. buf[5] = ((add_len >> 16) & 0xff);
  3435. buf[6] = ((add_len >> 8) & 0xff);
  3436. buf[7] = (add_len & 0xff);
  3437. transport_kunmap_data_sg(cmd);
  3438. return 0;
  3439. }
  3440. /*
  3441. * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
  3442. *
  3443. * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
  3444. */
  3445. static sense_reason_t
  3446. core_scsi3_pri_read_reservation(struct se_cmd *cmd)
  3447. {
  3448. struct se_device *dev = cmd->se_dev;
  3449. struct t10_pr_registration *pr_reg;
  3450. unsigned char *buf;
  3451. u64 pr_res_key;
  3452. u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
  3453. if (cmd->data_length < 8) {
  3454. pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
  3455. " too small\n", cmd->data_length);
  3456. return TCM_INVALID_CDB_FIELD;
  3457. }
  3458. buf = transport_kmap_data_sg(cmd);
  3459. if (!buf)
  3460. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3461. buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
  3462. buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
  3463. buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
  3464. buf[3] = (dev->t10_pr.pr_generation & 0xff);
  3465. spin_lock(&dev->dev_reservation_lock);
  3466. pr_reg = dev->dev_pr_res_holder;
  3467. if (pr_reg) {
  3468. /*
  3469. * Set the hardcoded Additional Length
  3470. */
  3471. buf[4] = ((add_len >> 24) & 0xff);
  3472. buf[5] = ((add_len >> 16) & 0xff);
  3473. buf[6] = ((add_len >> 8) & 0xff);
  3474. buf[7] = (add_len & 0xff);
  3475. if (cmd->data_length < 22)
  3476. goto err;
  3477. /*
  3478. * Set the Reservation key.
  3479. *
  3480. * From spc4r17, section 5.7.10:
  3481. * A persistent reservation holder has its reservation key
  3482. * returned in the parameter data from a PERSISTENT
  3483. * RESERVE IN command with READ RESERVATION service action as
  3484. * follows:
  3485. * a) For a persistent reservation of the type Write Exclusive
  3486. * - All Registrants or Exclusive Access ­ All Regitrants,
  3487. * the reservation key shall be set to zero; or
  3488. * b) For all other persistent reservation types, the
  3489. * reservation key shall be set to the registered
  3490. * reservation key for the I_T nexus that holds the
  3491. * persistent reservation.
  3492. */
  3493. if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3494. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
  3495. pr_res_key = 0;
  3496. else
  3497. pr_res_key = pr_reg->pr_res_key;
  3498. buf[8] = ((pr_res_key >> 56) & 0xff);
  3499. buf[9] = ((pr_res_key >> 48) & 0xff);
  3500. buf[10] = ((pr_res_key >> 40) & 0xff);
  3501. buf[11] = ((pr_res_key >> 32) & 0xff);
  3502. buf[12] = ((pr_res_key >> 24) & 0xff);
  3503. buf[13] = ((pr_res_key >> 16) & 0xff);
  3504. buf[14] = ((pr_res_key >> 8) & 0xff);
  3505. buf[15] = (pr_res_key & 0xff);
  3506. /*
  3507. * Set the SCOPE and TYPE
  3508. */
  3509. buf[21] = (pr_reg->pr_res_scope & 0xf0) |
  3510. (pr_reg->pr_res_type & 0x0f);
  3511. }
  3512. err:
  3513. spin_unlock(&dev->dev_reservation_lock);
  3514. transport_kunmap_data_sg(cmd);
  3515. return 0;
  3516. }
  3517. /*
  3518. * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
  3519. *
  3520. * See spc4r17 section 6.13.4 Table 165
  3521. */
  3522. static sense_reason_t
  3523. core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
  3524. {
  3525. struct se_device *dev = cmd->se_dev;
  3526. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  3527. unsigned char *buf;
  3528. u16 add_len = 8; /* Hardcoded to 8. */
  3529. if (cmd->data_length < 6) {
  3530. pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
  3531. " %u too small\n", cmd->data_length);
  3532. return TCM_INVALID_CDB_FIELD;
  3533. }
  3534. buf = transport_kmap_data_sg(cmd);
  3535. if (!buf)
  3536. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3537. buf[0] = ((add_len >> 8) & 0xff);
  3538. buf[1] = (add_len & 0xff);
  3539. buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
  3540. buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
  3541. buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
  3542. buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
  3543. /*
  3544. * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
  3545. * set the TMV: Task Mask Valid bit.
  3546. */
  3547. buf[3] |= 0x80;
  3548. /*
  3549. * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
  3550. */
  3551. buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
  3552. /*
  3553. * PTPL_A: Persistence across Target Power Loss Active bit
  3554. */
  3555. if (pr_tmpl->pr_aptpl_active)
  3556. buf[3] |= 0x01;
  3557. /*
  3558. * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
  3559. */
  3560. buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3561. buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
  3562. buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
  3563. buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
  3564. buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
  3565. buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3566. transport_kunmap_data_sg(cmd);
  3567. return 0;
  3568. }
  3569. /*
  3570. * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
  3571. *
  3572. * See spc4r17 section 6.13.5 Table 168 and 169
  3573. */
  3574. static sense_reason_t
  3575. core_scsi3_pri_read_full_status(struct se_cmd *cmd)
  3576. {
  3577. struct se_device *dev = cmd->se_dev;
  3578. struct se_node_acl *se_nacl;
  3579. struct se_portal_group *se_tpg;
  3580. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  3581. struct t10_reservation *pr_tmpl = &dev->t10_pr;
  3582. unsigned char *buf;
  3583. u32 add_desc_len = 0, add_len = 0;
  3584. u32 off = 8; /* off into first Full Status descriptor */
  3585. int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
  3586. int exp_desc_len, desc_len;
  3587. bool all_reg = false;
  3588. if (cmd->data_length < 8) {
  3589. pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
  3590. " too small\n", cmd->data_length);
  3591. return TCM_INVALID_CDB_FIELD;
  3592. }
  3593. buf = transport_kmap_data_sg(cmd);
  3594. if (!buf)
  3595. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3596. buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
  3597. buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
  3598. buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
  3599. buf[3] = (dev->t10_pr.pr_generation & 0xff);
  3600. spin_lock(&dev->dev_reservation_lock);
  3601. if (dev->dev_pr_res_holder) {
  3602. struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
  3603. if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
  3604. pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
  3605. all_reg = true;
  3606. pr_res_type = pr_holder->pr_res_type;
  3607. pr_res_scope = pr_holder->pr_res_scope;
  3608. }
  3609. }
  3610. spin_unlock(&dev->dev_reservation_lock);
  3611. spin_lock(&pr_tmpl->registration_lock);
  3612. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  3613. &pr_tmpl->registration_list, pr_reg_list) {
  3614. se_nacl = pr_reg->pr_reg_nacl;
  3615. se_tpg = pr_reg->pr_reg_nacl->se_tpg;
  3616. add_desc_len = 0;
  3617. atomic_inc_mb(&pr_reg->pr_res_holders);
  3618. spin_unlock(&pr_tmpl->registration_lock);
  3619. /*
  3620. * Determine expected length of $FABRIC_MOD specific
  3621. * TransportID full status descriptor..
  3622. */
  3623. exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
  3624. &format_code);
  3625. if (exp_desc_len < 0 ||
  3626. exp_desc_len + add_len > cmd->data_length) {
  3627. pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
  3628. " out of buffer: %d\n", cmd->data_length);
  3629. spin_lock(&pr_tmpl->registration_lock);
  3630. atomic_dec_mb(&pr_reg->pr_res_holders);
  3631. break;
  3632. }
  3633. /*
  3634. * Set RESERVATION KEY
  3635. */
  3636. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3637. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3638. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3639. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3640. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3641. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3642. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3643. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3644. off += 4; /* Skip Over Reserved area */
  3645. /*
  3646. * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
  3647. */
  3648. if (pr_reg->pr_reg_all_tg_pt)
  3649. buf[off] = 0x02;
  3650. /*
  3651. * The struct se_lun pointer will be present for the
  3652. * reservation holder for PR_HOLDER bit.
  3653. *
  3654. * Also, if this registration is the reservation
  3655. * holder or there is an All Registrants reservation
  3656. * active, fill in SCOPE and TYPE in the next byte.
  3657. */
  3658. if (pr_reg->pr_res_holder) {
  3659. buf[off++] |= 0x01;
  3660. buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
  3661. (pr_reg->pr_res_type & 0x0f);
  3662. } else if (all_reg) {
  3663. buf[off++] |= 0x01;
  3664. buf[off++] = (pr_res_scope & 0xf0) |
  3665. (pr_res_type & 0x0f);
  3666. } else {
  3667. off += 2;
  3668. }
  3669. off += 4; /* Skip over reserved area */
  3670. /*
  3671. * From spc4r17 6.3.15:
  3672. *
  3673. * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
  3674. * IDENTIFIER field contains the relative port identifier (see
  3675. * 3.1.120) of the target port that is part of the I_T nexus
  3676. * described by this full status descriptor. If the ALL_TG_PT
  3677. * bit is set to one, the contents of the RELATIVE TARGET PORT
  3678. * IDENTIFIER field are not defined by this standard.
  3679. */
  3680. if (!pr_reg->pr_reg_all_tg_pt) {
  3681. u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
  3682. buf[off++] = ((sep_rtpi >> 8) & 0xff);
  3683. buf[off++] = (sep_rtpi & 0xff);
  3684. } else
  3685. off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
  3686. buf[off+4] = se_tpg->proto_id;
  3687. /*
  3688. * Now, have the $FABRIC_MOD fill in the transport ID.
  3689. */
  3690. desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
  3691. &format_code, &buf[off+4]);
  3692. spin_lock(&pr_tmpl->registration_lock);
  3693. atomic_dec_mb(&pr_reg->pr_res_holders);
  3694. if (desc_len < 0)
  3695. break;
  3696. /*
  3697. * Set the ADDITIONAL DESCRIPTOR LENGTH
  3698. */
  3699. buf[off++] = ((desc_len >> 24) & 0xff);
  3700. buf[off++] = ((desc_len >> 16) & 0xff);
  3701. buf[off++] = ((desc_len >> 8) & 0xff);
  3702. buf[off++] = (desc_len & 0xff);
  3703. /*
  3704. * Size of full desctipor header minus TransportID
  3705. * containing $FABRIC_MOD specific) initiator device/port
  3706. * WWN information.
  3707. *
  3708. * See spc4r17 Section 6.13.5 Table 169
  3709. */
  3710. add_desc_len = (24 + desc_len);
  3711. off += desc_len;
  3712. add_len += add_desc_len;
  3713. }
  3714. spin_unlock(&pr_tmpl->registration_lock);
  3715. /*
  3716. * Set ADDITIONAL_LENGTH
  3717. */
  3718. buf[4] = ((add_len >> 24) & 0xff);
  3719. buf[5] = ((add_len >> 16) & 0xff);
  3720. buf[6] = ((add_len >> 8) & 0xff);
  3721. buf[7] = (add_len & 0xff);
  3722. transport_kunmap_data_sg(cmd);
  3723. return 0;
  3724. }
  3725. sense_reason_t
  3726. target_scsi3_emulate_pr_in(struct se_cmd *cmd)
  3727. {
  3728. sense_reason_t ret;
  3729. /*
  3730. * Following spc2r20 5.5.1 Reservations overview:
  3731. *
  3732. * If a logical unit has been reserved by any RESERVE command and is
  3733. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3734. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3735. * initiator or service action and shall terminate with a RESERVATION
  3736. * CONFLICT status.
  3737. */
  3738. if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
  3739. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3740. " SPC-2 reservation is held, returning"
  3741. " RESERVATION_CONFLICT\n");
  3742. return TCM_RESERVATION_CONFLICT;
  3743. }
  3744. switch (cmd->t_task_cdb[1] & 0x1f) {
  3745. case PRI_READ_KEYS:
  3746. ret = core_scsi3_pri_read_keys(cmd);
  3747. break;
  3748. case PRI_READ_RESERVATION:
  3749. ret = core_scsi3_pri_read_reservation(cmd);
  3750. break;
  3751. case PRI_REPORT_CAPABILITIES:
  3752. ret = core_scsi3_pri_report_capabilities(cmd);
  3753. break;
  3754. case PRI_READ_FULL_STATUS:
  3755. ret = core_scsi3_pri_read_full_status(cmd);
  3756. break;
  3757. default:
  3758. pr_err("Unknown PERSISTENT_RESERVE_IN service"
  3759. " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
  3760. return TCM_INVALID_CDB_FIELD;
  3761. }
  3762. if (!ret)
  3763. target_complete_cmd(cmd, GOOD);
  3764. return ret;
  3765. }
  3766. sense_reason_t
  3767. target_check_reservation(struct se_cmd *cmd)
  3768. {
  3769. struct se_device *dev = cmd->se_dev;
  3770. sense_reason_t ret;
  3771. if (!cmd->se_sess)
  3772. return 0;
  3773. if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
  3774. return 0;
  3775. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
  3776. return 0;
  3777. spin_lock(&dev->dev_reservation_lock);
  3778. if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
  3779. ret = target_scsi2_reservation_check(cmd);
  3780. else
  3781. ret = target_scsi3_pr_reservation_check(cmd);
  3782. spin_unlock(&dev->dev_reservation_lock);
  3783. return ret;
  3784. }