target_core_pr.c 123 KB

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