target_core_pr.c 121 KB

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