target_core_pr.c 124 KB

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