target_core_pr.c 126 KB

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