target_core_pr.c 124 KB

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