target_core_pr.c 129 KB

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