target_core_pr.c 125 KB

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