target_core_pr.c 132 KB

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