target_core_pr.c 125 KB

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