target_core_pr.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413
  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. struct 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. INIT_LIST_HEAD(&tid_dest_list);
  1383. local_se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  1384. /*
  1385. * Allocate a struct pr_transport_id_holder and setup the
  1386. * local_node_acl and local_se_deve pointers and add to
  1387. * struct list_head tid_dest_list for add registration
  1388. * processing in the loop of tid_dest_list below.
  1389. */
  1390. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
  1391. if (!tidh_new) {
  1392. pr_err("Unable to allocate tidh_new\n");
  1393. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1394. return -EINVAL;
  1395. }
  1396. INIT_LIST_HEAD(&tidh_new->dest_list);
  1397. tidh_new->dest_tpg = tpg;
  1398. tidh_new->dest_node_acl = se_sess->se_node_acl;
  1399. tidh_new->dest_se_deve = local_se_deve;
  1400. local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1401. se_sess->se_node_acl, local_se_deve, l_isid,
  1402. sa_res_key, all_tg_pt, aptpl);
  1403. if (!local_pr_reg) {
  1404. kfree(tidh_new);
  1405. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1406. return -ENOMEM;
  1407. }
  1408. tidh_new->dest_pr_reg = local_pr_reg;
  1409. /*
  1410. * The local I_T nexus does not hold any configfs dependances,
  1411. * so we set tid_h->dest_local_nexus=1 to prevent the
  1412. * configfs_undepend_item() calls in the tid_dest_list loops below.
  1413. */
  1414. tidh_new->dest_local_nexus = 1;
  1415. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1416. buf = transport_kmap_data_sg(cmd);
  1417. /*
  1418. * For a PERSISTENT RESERVE OUT specify initiator ports payload,
  1419. * first extract TransportID Parameter Data Length, and make sure
  1420. * the value matches up to the SCSI expected data transfer length.
  1421. */
  1422. tpdl = (buf[24] & 0xff) << 24;
  1423. tpdl |= (buf[25] & 0xff) << 16;
  1424. tpdl |= (buf[26] & 0xff) << 8;
  1425. tpdl |= buf[27] & 0xff;
  1426. if ((tpdl + 28) != cmd->data_length) {
  1427. pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
  1428. " does not equal CDB data_length: %u\n", tpdl,
  1429. cmd->data_length);
  1430. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1431. ret = -EINVAL;
  1432. goto out;
  1433. }
  1434. /*
  1435. * Start processing the received transport IDs using the
  1436. * receiving I_T Nexus portal's fabric dependent methods to
  1437. * obtain the SCSI Initiator Port/Device Identifiers.
  1438. */
  1439. ptr = &buf[28];
  1440. while (tpdl > 0) {
  1441. proto_ident = (ptr[0] & 0x0f);
  1442. dest_tpg = NULL;
  1443. spin_lock(&dev->se_port_lock);
  1444. list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
  1445. tmp_tpg = tmp_port->sep_tpg;
  1446. if (!tmp_tpg)
  1447. continue;
  1448. tmp_tf_ops = tmp_tpg->se_tpg_tfo;
  1449. if (!tmp_tf_ops)
  1450. continue;
  1451. if (!tmp_tf_ops->get_fabric_proto_ident ||
  1452. !tmp_tf_ops->tpg_parse_pr_out_transport_id)
  1453. continue;
  1454. /*
  1455. * Look for the matching proto_ident provided by
  1456. * the received TransportID
  1457. */
  1458. tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
  1459. if (tmp_proto_ident != proto_ident)
  1460. continue;
  1461. dest_rtpi = tmp_port->sep_rtpi;
  1462. i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
  1463. tmp_tpg, (const char *)ptr, &tid_len,
  1464. &iport_ptr);
  1465. if (!i_str)
  1466. continue;
  1467. atomic_inc(&tmp_tpg->tpg_pr_ref_count);
  1468. smp_mb__after_atomic_inc();
  1469. spin_unlock(&dev->se_port_lock);
  1470. ret = core_scsi3_tpg_depend_item(tmp_tpg);
  1471. if (ret != 0) {
  1472. pr_err(" core_scsi3_tpg_depend_item()"
  1473. " for tmp_tpg\n");
  1474. atomic_dec(&tmp_tpg->tpg_pr_ref_count);
  1475. smp_mb__after_atomic_dec();
  1476. cmd->scsi_sense_reason =
  1477. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1478. ret = -EINVAL;
  1479. goto out;
  1480. }
  1481. /*
  1482. * Locate the desination initiator ACL to be registered
  1483. * from the decoded fabric module specific TransportID
  1484. * at *i_str.
  1485. */
  1486. spin_lock_irq(&tmp_tpg->acl_node_lock);
  1487. dest_node_acl = __core_tpg_get_initiator_node_acl(
  1488. tmp_tpg, i_str);
  1489. if (dest_node_acl) {
  1490. atomic_inc(&dest_node_acl->acl_pr_ref_count);
  1491. smp_mb__after_atomic_inc();
  1492. }
  1493. spin_unlock_irq(&tmp_tpg->acl_node_lock);
  1494. if (!dest_node_acl) {
  1495. core_scsi3_tpg_undepend_item(tmp_tpg);
  1496. spin_lock(&dev->se_port_lock);
  1497. continue;
  1498. }
  1499. ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
  1500. if (ret != 0) {
  1501. pr_err("configfs_depend_item() failed"
  1502. " for dest_node_acl->acl_group\n");
  1503. atomic_dec(&dest_node_acl->acl_pr_ref_count);
  1504. smp_mb__after_atomic_dec();
  1505. core_scsi3_tpg_undepend_item(tmp_tpg);
  1506. cmd->scsi_sense_reason =
  1507. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1508. ret = -EINVAL;
  1509. goto out;
  1510. }
  1511. dest_tpg = tmp_tpg;
  1512. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
  1513. " %s Port RTPI: %hu\n",
  1514. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1515. dest_node_acl->initiatorname, dest_rtpi);
  1516. spin_lock(&dev->se_port_lock);
  1517. break;
  1518. }
  1519. spin_unlock(&dev->se_port_lock);
  1520. if (!dest_tpg) {
  1521. pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
  1522. " dest_tpg\n");
  1523. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1524. ret = -EINVAL;
  1525. goto out;
  1526. }
  1527. #if 0
  1528. pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
  1529. " tid_len: %d for %s + %s\n",
  1530. dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
  1531. tpdl, tid_len, i_str, iport_ptr);
  1532. #endif
  1533. if (tid_len > tpdl) {
  1534. pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
  1535. " %u for Transport ID: %s\n", tid_len, ptr);
  1536. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1537. core_scsi3_tpg_undepend_item(dest_tpg);
  1538. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1539. ret = -EINVAL;
  1540. goto out;
  1541. }
  1542. /*
  1543. * Locate the desintation struct se_dev_entry pointer for matching
  1544. * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
  1545. * Target Port.
  1546. */
  1547. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
  1548. dest_rtpi);
  1549. if (!dest_se_deve) {
  1550. pr_err("Unable to locate %s dest_se_deve"
  1551. " from destination RTPI: %hu\n",
  1552. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1553. dest_rtpi);
  1554. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1555. core_scsi3_tpg_undepend_item(dest_tpg);
  1556. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1557. ret = -EINVAL;
  1558. goto out;
  1559. }
  1560. ret = core_scsi3_lunacl_depend_item(dest_se_deve);
  1561. if (ret < 0) {
  1562. pr_err("core_scsi3_lunacl_depend_item()"
  1563. " failed\n");
  1564. atomic_dec(&dest_se_deve->pr_ref_count);
  1565. smp_mb__after_atomic_dec();
  1566. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1567. core_scsi3_tpg_undepend_item(dest_tpg);
  1568. cmd->scsi_sense_reason =
  1569. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1570. ret = -EINVAL;
  1571. goto out;
  1572. }
  1573. #if 0
  1574. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
  1575. " dest_se_deve mapped_lun: %u\n",
  1576. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1577. dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
  1578. #endif
  1579. /*
  1580. * Skip any TransportIDs that already have a registration for
  1581. * this target port.
  1582. */
  1583. pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  1584. iport_ptr);
  1585. if (pr_reg_e) {
  1586. core_scsi3_put_pr_reg(pr_reg_e);
  1587. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1588. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1589. core_scsi3_tpg_undepend_item(dest_tpg);
  1590. ptr += tid_len;
  1591. tpdl -= tid_len;
  1592. tid_len = 0;
  1593. continue;
  1594. }
  1595. /*
  1596. * Allocate a struct pr_transport_id_holder and setup
  1597. * the dest_node_acl and dest_se_deve pointers for the
  1598. * loop below.
  1599. */
  1600. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
  1601. GFP_KERNEL);
  1602. if (!tidh_new) {
  1603. pr_err("Unable to allocate tidh_new\n");
  1604. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1605. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1606. core_scsi3_tpg_undepend_item(dest_tpg);
  1607. cmd->scsi_sense_reason =
  1608. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1609. ret = -ENOMEM;
  1610. goto out;
  1611. }
  1612. INIT_LIST_HEAD(&tidh_new->dest_list);
  1613. tidh_new->dest_tpg = dest_tpg;
  1614. tidh_new->dest_node_acl = dest_node_acl;
  1615. tidh_new->dest_se_deve = dest_se_deve;
  1616. /*
  1617. * Allocate, but do NOT add the registration for the
  1618. * TransportID referenced SCSI Initiator port. This
  1619. * done because of the following from spc4r17 in section
  1620. * 6.14.3 wrt SPEC_I_PT:
  1621. *
  1622. * "If a registration fails for any initiator port (e.g., if th
  1623. * logical unit does not have enough resources available to
  1624. * hold the registration information), no registrations shall be
  1625. * made, and the command shall be terminated with
  1626. * CHECK CONDITION status."
  1627. *
  1628. * That means we call __core_scsi3_alloc_registration() here,
  1629. * and then call __core_scsi3_add_registration() in the
  1630. * 2nd loop which will never fail.
  1631. */
  1632. dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1633. dest_node_acl, dest_se_deve, iport_ptr,
  1634. sa_res_key, all_tg_pt, aptpl);
  1635. if (!dest_pr_reg) {
  1636. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1637. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1638. core_scsi3_tpg_undepend_item(dest_tpg);
  1639. kfree(tidh_new);
  1640. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1641. ret = -EINVAL;
  1642. goto out;
  1643. }
  1644. tidh_new->dest_pr_reg = dest_pr_reg;
  1645. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1646. ptr += tid_len;
  1647. tpdl -= tid_len;
  1648. tid_len = 0;
  1649. }
  1650. transport_kunmap_data_sg(cmd);
  1651. /*
  1652. * Go ahead and create a registrations from tid_dest_list for the
  1653. * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
  1654. * and dest_se_deve.
  1655. *
  1656. * The SA Reservation Key from the PROUT is set for the
  1657. * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
  1658. * means that the TransportID Initiator port will be
  1659. * registered on all of the target ports in the SCSI target device
  1660. * ALL_TG_PT=0 means the registration will only be for the
  1661. * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
  1662. * was received.
  1663. */
  1664. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1665. dest_tpg = tidh->dest_tpg;
  1666. dest_node_acl = tidh->dest_node_acl;
  1667. dest_se_deve = tidh->dest_se_deve;
  1668. dest_pr_reg = tidh->dest_pr_reg;
  1669. dest_local_nexus = tidh->dest_local_nexus;
  1670. list_del(&tidh->dest_list);
  1671. kfree(tidh);
  1672. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  1673. prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
  1674. PR_REG_ISID_ID_LEN);
  1675. __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
  1676. dest_pr_reg, 0, 0);
  1677. pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
  1678. " registered Transport ID for Node: %s%s Mapped LUN:"
  1679. " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
  1680. dest_node_acl->initiatorname, (prf_isid) ?
  1681. &i_buf[0] : "", dest_se_deve->mapped_lun);
  1682. if (dest_local_nexus)
  1683. continue;
  1684. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1685. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1686. core_scsi3_tpg_undepend_item(dest_tpg);
  1687. }
  1688. return 0;
  1689. out:
  1690. transport_kunmap_data_sg(cmd);
  1691. /*
  1692. * For the failure case, release everything from tid_dest_list
  1693. * including *dest_pr_reg and the configfs dependances..
  1694. */
  1695. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1696. dest_tpg = tidh->dest_tpg;
  1697. dest_node_acl = tidh->dest_node_acl;
  1698. dest_se_deve = tidh->dest_se_deve;
  1699. dest_pr_reg = tidh->dest_pr_reg;
  1700. dest_local_nexus = tidh->dest_local_nexus;
  1701. list_del(&tidh->dest_list);
  1702. kfree(tidh);
  1703. /*
  1704. * Release any extra ALL_TG_PT=1 registrations for
  1705. * the SPEC_I_PT=1 case.
  1706. */
  1707. list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
  1708. &dest_pr_reg->pr_reg_atp_list,
  1709. pr_reg_atp_mem_list) {
  1710. list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
  1711. core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
  1712. kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
  1713. }
  1714. kfree(dest_pr_reg->pr_aptpl_buf);
  1715. kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
  1716. if (dest_local_nexus)
  1717. continue;
  1718. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1719. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1720. core_scsi3_tpg_undepend_item(dest_tpg);
  1721. }
  1722. return ret;
  1723. }
  1724. /*
  1725. * Called with struct se_device->dev_reservation_lock held
  1726. */
  1727. static int __core_scsi3_update_aptpl_buf(
  1728. struct se_device *dev,
  1729. unsigned char *buf,
  1730. u32 pr_aptpl_buf_len,
  1731. int clear_aptpl_metadata)
  1732. {
  1733. struct se_lun *lun;
  1734. struct se_portal_group *tpg;
  1735. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1736. struct t10_pr_registration *pr_reg;
  1737. unsigned char tmp[512], isid_buf[32];
  1738. ssize_t len = 0;
  1739. int reg_count = 0;
  1740. memset(buf, 0, pr_aptpl_buf_len);
  1741. /*
  1742. * Called to clear metadata once APTPL has been deactivated.
  1743. */
  1744. if (clear_aptpl_metadata) {
  1745. snprintf(buf, pr_aptpl_buf_len,
  1746. "No Registrations or Reservations\n");
  1747. return 0;
  1748. }
  1749. /*
  1750. * Walk the registration list..
  1751. */
  1752. spin_lock(&su_dev->t10_pr.registration_lock);
  1753. list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
  1754. pr_reg_list) {
  1755. tmp[0] = '\0';
  1756. isid_buf[0] = '\0';
  1757. tpg = pr_reg->pr_reg_nacl->se_tpg;
  1758. lun = pr_reg->pr_reg_tg_pt_lun;
  1759. /*
  1760. * Write out any ISID value to APTPL metadata that was included
  1761. * in the original registration.
  1762. */
  1763. if (pr_reg->isid_present_at_reg)
  1764. snprintf(isid_buf, 32, "initiator_sid=%s\n",
  1765. pr_reg->pr_reg_isid);
  1766. /*
  1767. * Include special metadata if the pr_reg matches the
  1768. * reservation holder.
  1769. */
  1770. if (dev->dev_pr_res_holder == pr_reg) {
  1771. snprintf(tmp, 512, "PR_REG_START: %d"
  1772. "\ninitiator_fabric=%s\n"
  1773. "initiator_node=%s\n%s"
  1774. "sa_res_key=%llu\n"
  1775. "res_holder=1\nres_type=%02x\n"
  1776. "res_scope=%02x\nres_all_tg_pt=%d\n"
  1777. "mapped_lun=%u\n", reg_count,
  1778. tpg->se_tpg_tfo->get_fabric_name(),
  1779. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1780. pr_reg->pr_res_key, pr_reg->pr_res_type,
  1781. pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
  1782. pr_reg->pr_res_mapped_lun);
  1783. } else {
  1784. snprintf(tmp, 512, "PR_REG_START: %d\n"
  1785. "initiator_fabric=%s\ninitiator_node=%s\n%s"
  1786. "sa_res_key=%llu\nres_holder=0\n"
  1787. "res_all_tg_pt=%d\nmapped_lun=%u\n",
  1788. reg_count, tpg->se_tpg_tfo->get_fabric_name(),
  1789. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1790. pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
  1791. pr_reg->pr_res_mapped_lun);
  1792. }
  1793. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1794. pr_err("Unable to update renaming"
  1795. " APTPL metadata\n");
  1796. spin_unlock(&su_dev->t10_pr.registration_lock);
  1797. return -EMSGSIZE;
  1798. }
  1799. len += sprintf(buf+len, "%s", tmp);
  1800. /*
  1801. * Include information about the associated SCSI target port.
  1802. */
  1803. snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
  1804. "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
  1805. " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
  1806. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1807. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1808. lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
  1809. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1810. pr_err("Unable to update renaming"
  1811. " APTPL metadata\n");
  1812. spin_unlock(&su_dev->t10_pr.registration_lock);
  1813. return -EMSGSIZE;
  1814. }
  1815. len += sprintf(buf+len, "%s", tmp);
  1816. reg_count++;
  1817. }
  1818. spin_unlock(&su_dev->t10_pr.registration_lock);
  1819. if (!reg_count)
  1820. len += sprintf(buf+len, "No Registrations or Reservations");
  1821. return 0;
  1822. }
  1823. static int core_scsi3_update_aptpl_buf(
  1824. struct se_device *dev,
  1825. unsigned char *buf,
  1826. u32 pr_aptpl_buf_len,
  1827. int clear_aptpl_metadata)
  1828. {
  1829. int ret;
  1830. spin_lock(&dev->dev_reservation_lock);
  1831. ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
  1832. clear_aptpl_metadata);
  1833. spin_unlock(&dev->dev_reservation_lock);
  1834. return ret;
  1835. }
  1836. /*
  1837. * Called with struct se_device->aptpl_file_mutex held
  1838. */
  1839. static int __core_scsi3_write_aptpl_to_file(
  1840. struct se_device *dev,
  1841. unsigned char *buf,
  1842. u32 pr_aptpl_buf_len)
  1843. {
  1844. struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
  1845. struct file *file;
  1846. struct iovec iov[1];
  1847. mm_segment_t old_fs;
  1848. int flags = O_RDWR | O_CREAT | O_TRUNC;
  1849. char path[512];
  1850. int ret;
  1851. memset(iov, 0, sizeof(struct iovec));
  1852. memset(path, 0, 512);
  1853. if (strlen(&wwn->unit_serial[0]) >= 512) {
  1854. pr_err("WWN value for struct se_device does not fit"
  1855. " into path buffer\n");
  1856. return -EMSGSIZE;
  1857. }
  1858. snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
  1859. file = filp_open(path, flags, 0600);
  1860. if (IS_ERR(file) || !file || !file->f_dentry) {
  1861. pr_err("filp_open(%s) for APTPL metadata"
  1862. " failed\n", path);
  1863. return (PTR_ERR(file) < 0 ? PTR_ERR(file) : -ENOENT);
  1864. }
  1865. iov[0].iov_base = &buf[0];
  1866. if (!pr_aptpl_buf_len)
  1867. iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
  1868. else
  1869. iov[0].iov_len = pr_aptpl_buf_len;
  1870. old_fs = get_fs();
  1871. set_fs(get_ds());
  1872. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  1873. set_fs(old_fs);
  1874. if (ret < 0) {
  1875. pr_debug("Error writing APTPL metadata file: %s\n", path);
  1876. filp_close(file, NULL);
  1877. return -EIO;
  1878. }
  1879. filp_close(file, NULL);
  1880. return 0;
  1881. }
  1882. static int core_scsi3_update_and_write_aptpl(
  1883. struct se_device *dev,
  1884. unsigned char *in_buf,
  1885. u32 in_pr_aptpl_buf_len)
  1886. {
  1887. unsigned char null_buf[64], *buf;
  1888. u32 pr_aptpl_buf_len;
  1889. int ret, clear_aptpl_metadata = 0;
  1890. /*
  1891. * Can be called with a NULL pointer from PROUT service action CLEAR
  1892. */
  1893. if (!in_buf) {
  1894. memset(null_buf, 0, 64);
  1895. buf = &null_buf[0];
  1896. /*
  1897. * This will clear the APTPL metadata to:
  1898. * "No Registrations or Reservations" status
  1899. */
  1900. pr_aptpl_buf_len = 64;
  1901. clear_aptpl_metadata = 1;
  1902. } else {
  1903. buf = in_buf;
  1904. pr_aptpl_buf_len = in_pr_aptpl_buf_len;
  1905. }
  1906. ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
  1907. clear_aptpl_metadata);
  1908. if (ret != 0)
  1909. return ret;
  1910. /*
  1911. * __core_scsi3_write_aptpl_to_file() will call strlen()
  1912. * on the passed buf to determine pr_aptpl_buf_len.
  1913. */
  1914. ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
  1915. if (ret != 0)
  1916. return ret;
  1917. return ret;
  1918. }
  1919. static int core_scsi3_emulate_pro_register(
  1920. struct se_cmd *cmd,
  1921. u64 res_key,
  1922. u64 sa_res_key,
  1923. int aptpl,
  1924. int all_tg_pt,
  1925. int spec_i_pt,
  1926. int ignore_key)
  1927. {
  1928. struct se_session *se_sess = cmd->se_sess;
  1929. struct se_device *dev = cmd->se_dev;
  1930. struct se_dev_entry *se_deve;
  1931. struct se_lun *se_lun = cmd->se_lun;
  1932. struct se_portal_group *se_tpg;
  1933. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
  1934. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  1935. /* Used for APTPL metadata w/ UNREGISTER */
  1936. unsigned char *pr_aptpl_buf = NULL;
  1937. unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
  1938. int pr_holder = 0, ret = 0, type;
  1939. if (!se_sess || !se_lun) {
  1940. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  1941. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1942. return -EINVAL;
  1943. }
  1944. se_tpg = se_sess->se_tpg;
  1945. se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  1946. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
  1947. memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
  1948. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
  1949. PR_REG_ISID_LEN);
  1950. isid_ptr = &isid_buf[0];
  1951. }
  1952. /*
  1953. * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
  1954. */
  1955. pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  1956. if (!pr_reg_e) {
  1957. if (res_key) {
  1958. pr_warn("SPC-3 PR: Reservation Key non-zero"
  1959. " for SA REGISTER, returning CONFLICT\n");
  1960. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  1961. return -EINVAL;
  1962. }
  1963. /*
  1964. * Do nothing but return GOOD status.
  1965. */
  1966. if (!sa_res_key)
  1967. return 0;
  1968. if (!spec_i_pt) {
  1969. /*
  1970. * Perform the Service Action REGISTER on the Initiator
  1971. * Port Endpoint that the PRO was received from on the
  1972. * Logical Unit of the SCSI device server.
  1973. */
  1974. ret = core_scsi3_alloc_registration(cmd->se_dev,
  1975. se_sess->se_node_acl, se_deve, isid_ptr,
  1976. sa_res_key, all_tg_pt, aptpl,
  1977. ignore_key, 0);
  1978. if (ret != 0) {
  1979. pr_err("Unable to allocate"
  1980. " struct t10_pr_registration\n");
  1981. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1982. return -EINVAL;
  1983. }
  1984. } else {
  1985. /*
  1986. * Register both the Initiator port that received
  1987. * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
  1988. * TransportID from Parameter list and loop through
  1989. * fabric dependent parameter list while calling
  1990. * logic from of core_scsi3_alloc_registration() for
  1991. * each TransportID provided SCSI Initiator Port/Device
  1992. */
  1993. ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
  1994. isid_ptr, sa_res_key, all_tg_pt, aptpl);
  1995. if (ret != 0)
  1996. return ret;
  1997. }
  1998. /*
  1999. * Nothing left to do for the APTPL=0 case.
  2000. */
  2001. if (!aptpl) {
  2002. pr_tmpl->pr_aptpl_active = 0;
  2003. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  2004. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
  2005. " REGISTER\n");
  2006. return 0;
  2007. }
  2008. /*
  2009. * Locate the newly allocated local I_T Nexus *pr_reg, and
  2010. * update the APTPL metadata information using its
  2011. * preallocated *pr_reg->pr_aptpl_buf.
  2012. */
  2013. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
  2014. se_sess->se_node_acl, se_sess);
  2015. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2016. &pr_reg->pr_aptpl_buf[0],
  2017. pr_tmpl->pr_aptpl_buf_len);
  2018. if (!ret) {
  2019. pr_tmpl->pr_aptpl_active = 1;
  2020. pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
  2021. }
  2022. core_scsi3_put_pr_reg(pr_reg);
  2023. return ret;
  2024. } else {
  2025. /*
  2026. * Locate the existing *pr_reg via struct se_node_acl pointers
  2027. */
  2028. pr_reg = pr_reg_e;
  2029. type = pr_reg->pr_res_type;
  2030. if (!ignore_key) {
  2031. if (res_key != pr_reg->pr_res_key) {
  2032. pr_err("SPC-3 PR REGISTER: Received"
  2033. " res_key: 0x%016Lx does not match"
  2034. " existing SA REGISTER res_key:"
  2035. " 0x%016Lx\n", res_key,
  2036. pr_reg->pr_res_key);
  2037. core_scsi3_put_pr_reg(pr_reg);
  2038. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2039. return -EINVAL;
  2040. }
  2041. }
  2042. if (spec_i_pt) {
  2043. pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
  2044. " set while sa_res_key=0\n");
  2045. core_scsi3_put_pr_reg(pr_reg);
  2046. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2047. return -EINVAL;
  2048. }
  2049. /*
  2050. * An existing ALL_TG_PT=1 registration being released
  2051. * must also set ALL_TG_PT=1 in the incoming PROUT.
  2052. */
  2053. if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
  2054. pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
  2055. " registration exists, but ALL_TG_PT=1 bit not"
  2056. " present in received PROUT\n");
  2057. core_scsi3_put_pr_reg(pr_reg);
  2058. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2059. return -EINVAL;
  2060. }
  2061. /*
  2062. * Allocate APTPL metadata buffer used for UNREGISTER ops
  2063. */
  2064. if (aptpl) {
  2065. pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
  2066. GFP_KERNEL);
  2067. if (!pr_aptpl_buf) {
  2068. pr_err("Unable to allocate"
  2069. " pr_aptpl_buf\n");
  2070. core_scsi3_put_pr_reg(pr_reg);
  2071. cmd->scsi_sense_reason =
  2072. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2073. return -EINVAL;
  2074. }
  2075. }
  2076. /*
  2077. * sa_res_key=0 Unregister Reservation Key for registered I_T
  2078. * Nexus sa_res_key=1 Change Reservation Key for registered I_T
  2079. * Nexus.
  2080. */
  2081. if (!sa_res_key) {
  2082. pr_holder = core_scsi3_check_implict_release(
  2083. cmd->se_dev, pr_reg);
  2084. if (pr_holder < 0) {
  2085. kfree(pr_aptpl_buf);
  2086. core_scsi3_put_pr_reg(pr_reg);
  2087. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2088. return -EINVAL;
  2089. }
  2090. spin_lock(&pr_tmpl->registration_lock);
  2091. /*
  2092. * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
  2093. * and matching pr_res_key.
  2094. */
  2095. if (pr_reg->pr_reg_all_tg_pt) {
  2096. list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
  2097. &pr_tmpl->registration_list,
  2098. pr_reg_list) {
  2099. if (!pr_reg_p->pr_reg_all_tg_pt)
  2100. continue;
  2101. if (pr_reg_p->pr_res_key != res_key)
  2102. continue;
  2103. if (pr_reg == pr_reg_p)
  2104. continue;
  2105. if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
  2106. pr_reg_p->pr_reg_nacl->initiatorname))
  2107. continue;
  2108. __core_scsi3_free_registration(dev,
  2109. pr_reg_p, NULL, 0);
  2110. }
  2111. }
  2112. /*
  2113. * Release the calling I_T Nexus registration now..
  2114. */
  2115. __core_scsi3_free_registration(cmd->se_dev, pr_reg,
  2116. NULL, 1);
  2117. /*
  2118. * From spc4r17, section 5.7.11.3 Unregistering
  2119. *
  2120. * If the persistent reservation is a registrants only
  2121. * type, the device server shall establish a unit
  2122. * attention condition for the initiator port associated
  2123. * with every registered I_T nexus except for the I_T
  2124. * nexus on which the PERSISTENT RESERVE OUT command was
  2125. * received, with the additional sense code set to
  2126. * RESERVATIONS RELEASED.
  2127. */
  2128. if (pr_holder &&
  2129. ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
  2130. (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
  2131. list_for_each_entry(pr_reg_p,
  2132. &pr_tmpl->registration_list,
  2133. pr_reg_list) {
  2134. core_scsi3_ua_allocate(
  2135. pr_reg_p->pr_reg_nacl,
  2136. pr_reg_p->pr_res_mapped_lun,
  2137. 0x2A,
  2138. ASCQ_2AH_RESERVATIONS_RELEASED);
  2139. }
  2140. }
  2141. spin_unlock(&pr_tmpl->registration_lock);
  2142. if (!aptpl) {
  2143. pr_tmpl->pr_aptpl_active = 0;
  2144. core_scsi3_update_and_write_aptpl(dev, NULL, 0);
  2145. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
  2146. " for UNREGISTER\n");
  2147. return 0;
  2148. }
  2149. ret = core_scsi3_update_and_write_aptpl(dev,
  2150. &pr_aptpl_buf[0],
  2151. pr_tmpl->pr_aptpl_buf_len);
  2152. if (!ret) {
  2153. pr_tmpl->pr_aptpl_active = 1;
  2154. pr_debug("SPC-3 PR: Set APTPL Bit Activated"
  2155. " for UNREGISTER\n");
  2156. }
  2157. kfree(pr_aptpl_buf);
  2158. return ret;
  2159. } else {
  2160. /*
  2161. * Increment PRgeneration counter for struct se_device"
  2162. * upon a successful REGISTER, see spc4r17 section 6.3.2
  2163. * READ_KEYS service action.
  2164. */
  2165. pr_reg->pr_res_generation = core_scsi3_pr_generation(
  2166. cmd->se_dev);
  2167. pr_reg->pr_res_key = sa_res_key;
  2168. pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
  2169. " Key for %s to: 0x%016Lx PRgeneration:"
  2170. " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
  2171. (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
  2172. pr_reg->pr_reg_nacl->initiatorname,
  2173. pr_reg->pr_res_key, pr_reg->pr_res_generation);
  2174. if (!aptpl) {
  2175. pr_tmpl->pr_aptpl_active = 0;
  2176. core_scsi3_update_and_write_aptpl(dev, NULL, 0);
  2177. core_scsi3_put_pr_reg(pr_reg);
  2178. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
  2179. " for REGISTER\n");
  2180. return 0;
  2181. }
  2182. ret = core_scsi3_update_and_write_aptpl(dev,
  2183. &pr_aptpl_buf[0],
  2184. pr_tmpl->pr_aptpl_buf_len);
  2185. if (!ret) {
  2186. pr_tmpl->pr_aptpl_active = 1;
  2187. pr_debug("SPC-3 PR: Set APTPL Bit Activated"
  2188. " for REGISTER\n");
  2189. }
  2190. kfree(pr_aptpl_buf);
  2191. core_scsi3_put_pr_reg(pr_reg);
  2192. }
  2193. }
  2194. return 0;
  2195. }
  2196. unsigned char *core_scsi3_pr_dump_type(int type)
  2197. {
  2198. switch (type) {
  2199. case PR_TYPE_WRITE_EXCLUSIVE:
  2200. return "Write Exclusive Access";
  2201. case PR_TYPE_EXCLUSIVE_ACCESS:
  2202. return "Exclusive Access";
  2203. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2204. return "Write Exclusive Access, Registrants Only";
  2205. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2206. return "Exclusive Access, Registrants Only";
  2207. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2208. return "Write Exclusive Access, All Registrants";
  2209. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2210. return "Exclusive Access, All Registrants";
  2211. default:
  2212. break;
  2213. }
  2214. return "Unknown SPC-3 PR Type";
  2215. }
  2216. static int core_scsi3_pro_reserve(
  2217. struct se_cmd *cmd,
  2218. struct se_device *dev,
  2219. int type,
  2220. int scope,
  2221. u64 res_key)
  2222. {
  2223. struct se_session *se_sess = cmd->se_sess;
  2224. struct se_dev_entry *se_deve;
  2225. struct se_lun *se_lun = cmd->se_lun;
  2226. struct se_portal_group *se_tpg;
  2227. struct t10_pr_registration *pr_reg, *pr_res_holder;
  2228. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2229. char i_buf[PR_REG_ISID_ID_LEN];
  2230. int ret, prf_isid;
  2231. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2232. if (!se_sess || !se_lun) {
  2233. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2234. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2235. return -EINVAL;
  2236. }
  2237. se_tpg = se_sess->se_tpg;
  2238. se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  2239. /*
  2240. * Locate the existing *pr_reg via struct se_node_acl pointers
  2241. */
  2242. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2243. se_sess);
  2244. if (!pr_reg) {
  2245. pr_err("SPC-3 PR: Unable to locate"
  2246. " PR_REGISTERED *pr_reg for RESERVE\n");
  2247. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2248. return -EINVAL;
  2249. }
  2250. /*
  2251. * From spc4r17 Section 5.7.9: Reserving:
  2252. *
  2253. * An application client creates a persistent reservation by issuing
  2254. * a PERSISTENT RESERVE OUT command with RESERVE service action through
  2255. * a registered I_T nexus with the following parameters:
  2256. * a) RESERVATION KEY set to the value of the reservation key that is
  2257. * registered with the logical unit for the I_T nexus; and
  2258. */
  2259. if (res_key != pr_reg->pr_res_key) {
  2260. pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
  2261. " does not match existing SA REGISTER res_key:"
  2262. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2263. core_scsi3_put_pr_reg(pr_reg);
  2264. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2265. return -EINVAL;
  2266. }
  2267. /*
  2268. * From spc4r17 Section 5.7.9: Reserving:
  2269. *
  2270. * From above:
  2271. * b) TYPE field and SCOPE field set to the persistent reservation
  2272. * being created.
  2273. *
  2274. * Only one persistent reservation is allowed at a time per logical unit
  2275. * and that persistent reservation has a scope of LU_SCOPE.
  2276. */
  2277. if (scope != PR_SCOPE_LU_SCOPE) {
  2278. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2279. core_scsi3_put_pr_reg(pr_reg);
  2280. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2281. return -EINVAL;
  2282. }
  2283. /*
  2284. * See if we have an existing PR reservation holder pointer at
  2285. * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
  2286. * *pr_res_holder.
  2287. */
  2288. spin_lock(&dev->dev_reservation_lock);
  2289. pr_res_holder = dev->dev_pr_res_holder;
  2290. if ((pr_res_holder)) {
  2291. /*
  2292. * From spc4r17 Section 5.7.9: Reserving:
  2293. *
  2294. * If the device server receives a PERSISTENT RESERVE OUT
  2295. * command from an I_T nexus other than a persistent reservation
  2296. * holder (see 5.7.10) that attempts to create a persistent
  2297. * reservation when a persistent reservation already exists for
  2298. * the logical unit, then the command shall be completed with
  2299. * RESERVATION CONFLICT status.
  2300. */
  2301. if (pr_res_holder != pr_reg) {
  2302. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2303. pr_err("SPC-3 PR: Attempted RESERVE from"
  2304. " [%s]: %s while reservation already held by"
  2305. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2306. cmd->se_tfo->get_fabric_name(),
  2307. se_sess->se_node_acl->initiatorname,
  2308. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2309. pr_res_holder->pr_reg_nacl->initiatorname);
  2310. spin_unlock(&dev->dev_reservation_lock);
  2311. core_scsi3_put_pr_reg(pr_reg);
  2312. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2313. return -EINVAL;
  2314. }
  2315. /*
  2316. * From spc4r17 Section 5.7.9: Reserving:
  2317. *
  2318. * If a persistent reservation holder attempts to modify the
  2319. * type or scope of an existing persistent reservation, the
  2320. * command shall be completed with RESERVATION CONFLICT status.
  2321. */
  2322. if ((pr_res_holder->pr_res_type != type) ||
  2323. (pr_res_holder->pr_res_scope != scope)) {
  2324. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2325. pr_err("SPC-3 PR: Attempted RESERVE from"
  2326. " [%s]: %s trying to change TYPE and/or SCOPE,"
  2327. " while reservation already held by [%s]: %s,"
  2328. " returning RESERVATION_CONFLICT\n",
  2329. cmd->se_tfo->get_fabric_name(),
  2330. se_sess->se_node_acl->initiatorname,
  2331. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2332. pr_res_holder->pr_reg_nacl->initiatorname);
  2333. spin_unlock(&dev->dev_reservation_lock);
  2334. core_scsi3_put_pr_reg(pr_reg);
  2335. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2336. return -EINVAL;
  2337. }
  2338. /*
  2339. * From spc4r17 Section 5.7.9: Reserving:
  2340. *
  2341. * If the device server receives a PERSISTENT RESERVE OUT
  2342. * command with RESERVE service action where the TYPE field and
  2343. * the SCOPE field contain the same values as the existing type
  2344. * and scope from a persistent reservation holder, it shall not
  2345. * make any change to the existing persistent reservation and
  2346. * shall completethe command with GOOD status.
  2347. */
  2348. spin_unlock(&dev->dev_reservation_lock);
  2349. core_scsi3_put_pr_reg(pr_reg);
  2350. return 0;
  2351. }
  2352. /*
  2353. * Otherwise, our *pr_reg becomes the PR reservation holder for said
  2354. * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
  2355. */
  2356. pr_reg->pr_res_scope = scope;
  2357. pr_reg->pr_res_type = type;
  2358. pr_reg->pr_res_holder = 1;
  2359. dev->dev_pr_res_holder = pr_reg;
  2360. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2361. PR_REG_ISID_ID_LEN);
  2362. pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
  2363. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2364. cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
  2365. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2366. pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
  2367. cmd->se_tfo->get_fabric_name(),
  2368. se_sess->se_node_acl->initiatorname,
  2369. (prf_isid) ? &i_buf[0] : "");
  2370. spin_unlock(&dev->dev_reservation_lock);
  2371. if (pr_tmpl->pr_aptpl_active) {
  2372. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2373. &pr_reg->pr_aptpl_buf[0],
  2374. pr_tmpl->pr_aptpl_buf_len);
  2375. if (!ret)
  2376. pr_debug("SPC-3 PR: Updated APTPL metadata"
  2377. " for RESERVE\n");
  2378. }
  2379. core_scsi3_put_pr_reg(pr_reg);
  2380. return 0;
  2381. }
  2382. static int core_scsi3_emulate_pro_reserve(
  2383. struct se_cmd *cmd,
  2384. int type,
  2385. int scope,
  2386. u64 res_key)
  2387. {
  2388. struct se_device *dev = cmd->se_dev;
  2389. int ret = 0;
  2390. switch (type) {
  2391. case PR_TYPE_WRITE_EXCLUSIVE:
  2392. case PR_TYPE_EXCLUSIVE_ACCESS:
  2393. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2394. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2395. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2396. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2397. ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
  2398. break;
  2399. default:
  2400. pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
  2401. " 0x%02x\n", type);
  2402. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2403. return -EINVAL;
  2404. }
  2405. return ret;
  2406. }
  2407. /*
  2408. * Called with struct se_device->dev_reservation_lock held.
  2409. */
  2410. static void __core_scsi3_complete_pro_release(
  2411. struct se_device *dev,
  2412. struct se_node_acl *se_nacl,
  2413. struct t10_pr_registration *pr_reg,
  2414. int explict)
  2415. {
  2416. struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
  2417. char i_buf[PR_REG_ISID_ID_LEN];
  2418. int prf_isid;
  2419. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2420. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2421. PR_REG_ISID_ID_LEN);
  2422. /*
  2423. * Go ahead and release the current PR reservation holder.
  2424. */
  2425. dev->dev_pr_res_holder = NULL;
  2426. pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
  2427. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2428. tfo->get_fabric_name(), (explict) ? "explict" : "implict",
  2429. core_scsi3_pr_dump_type(pr_reg->pr_res_type),
  2430. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2431. pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
  2432. tfo->get_fabric_name(), se_nacl->initiatorname,
  2433. (prf_isid) ? &i_buf[0] : "");
  2434. /*
  2435. * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
  2436. */
  2437. pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
  2438. }
  2439. static int core_scsi3_emulate_pro_release(
  2440. struct se_cmd *cmd,
  2441. int type,
  2442. int scope,
  2443. u64 res_key)
  2444. {
  2445. struct se_device *dev = cmd->se_dev;
  2446. struct se_session *se_sess = cmd->se_sess;
  2447. struct se_lun *se_lun = cmd->se_lun;
  2448. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
  2449. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2450. int ret, all_reg = 0;
  2451. if (!se_sess || !se_lun) {
  2452. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2453. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2454. return -EINVAL;
  2455. }
  2456. /*
  2457. * Locate the existing *pr_reg via struct se_node_acl pointers
  2458. */
  2459. pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  2460. if (!pr_reg) {
  2461. pr_err("SPC-3 PR: Unable to locate"
  2462. " PR_REGISTERED *pr_reg for RELEASE\n");
  2463. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2464. return -EINVAL;
  2465. }
  2466. /*
  2467. * From spc4r17 Section 5.7.11.2 Releasing:
  2468. *
  2469. * If there is no persistent reservation or in response to a persistent
  2470. * reservation release request from a registered I_T nexus that is not a
  2471. * persistent reservation holder (see 5.7.10), the device server shall
  2472. * do the following:
  2473. *
  2474. * a) Not release the persistent reservation, if any;
  2475. * b) Not remove any registrations; and
  2476. * c) Complete the command with GOOD status.
  2477. */
  2478. spin_lock(&dev->dev_reservation_lock);
  2479. pr_res_holder = dev->dev_pr_res_holder;
  2480. if (!pr_res_holder) {
  2481. /*
  2482. * No persistent reservation, return GOOD status.
  2483. */
  2484. spin_unlock(&dev->dev_reservation_lock);
  2485. core_scsi3_put_pr_reg(pr_reg);
  2486. return 0;
  2487. }
  2488. if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2489. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
  2490. all_reg = 1;
  2491. if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
  2492. /*
  2493. * Non 'All Registrants' PR Type cases..
  2494. * Release request from a registered I_T nexus that is not a
  2495. * persistent reservation holder. return GOOD status.
  2496. */
  2497. spin_unlock(&dev->dev_reservation_lock);
  2498. core_scsi3_put_pr_reg(pr_reg);
  2499. return 0;
  2500. }
  2501. /*
  2502. * From spc4r17 Section 5.7.11.2 Releasing:
  2503. *
  2504. * Only the persistent reservation holder (see 5.7.10) is allowed to
  2505. * release a persistent reservation.
  2506. *
  2507. * An application client releases the persistent reservation by issuing
  2508. * a PERSISTENT RESERVE OUT command with RELEASE service action through
  2509. * an I_T nexus that is a persistent reservation holder with the
  2510. * following parameters:
  2511. *
  2512. * a) RESERVATION KEY field set to the value of the reservation key
  2513. * that is registered with the logical unit for the I_T nexus;
  2514. */
  2515. if (res_key != pr_reg->pr_res_key) {
  2516. pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
  2517. " does not match existing SA REGISTER res_key:"
  2518. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2519. spin_unlock(&dev->dev_reservation_lock);
  2520. core_scsi3_put_pr_reg(pr_reg);
  2521. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2522. return -EINVAL;
  2523. }
  2524. /*
  2525. * From spc4r17 Section 5.7.11.2 Releasing and above:
  2526. *
  2527. * b) TYPE field and SCOPE field set to match the persistent
  2528. * reservation being released.
  2529. */
  2530. if ((pr_res_holder->pr_res_type != type) ||
  2531. (pr_res_holder->pr_res_scope != scope)) {
  2532. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2533. pr_err("SPC-3 PR RELEASE: Attempted to release"
  2534. " reservation from [%s]: %s with different TYPE "
  2535. "and/or SCOPE while reservation already held by"
  2536. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2537. cmd->se_tfo->get_fabric_name(),
  2538. se_sess->se_node_acl->initiatorname,
  2539. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2540. pr_res_holder->pr_reg_nacl->initiatorname);
  2541. spin_unlock(&dev->dev_reservation_lock);
  2542. core_scsi3_put_pr_reg(pr_reg);
  2543. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2544. return -EINVAL;
  2545. }
  2546. /*
  2547. * In response to a persistent reservation release request from the
  2548. * persistent reservation holder the device server shall perform a
  2549. * release by doing the following as an uninterrupted series of actions:
  2550. * a) Release the persistent reservation;
  2551. * b) Not remove any registration(s);
  2552. * c) If the released persistent reservation is a registrants only type
  2553. * or all registrants type persistent reservation,
  2554. * the device server shall establish a unit attention condition for
  2555. * the initiator port associated with every regis-
  2556. * tered I_T nexus other than I_T nexus on which the PERSISTENT
  2557. * RESERVE OUT command with RELEASE service action was received,
  2558. * with the additional sense code set to RESERVATIONS RELEASED; and
  2559. * d) If the persistent reservation is of any other type, the device
  2560. * server shall not establish a unit attention condition.
  2561. */
  2562. __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
  2563. pr_reg, 1);
  2564. spin_unlock(&dev->dev_reservation_lock);
  2565. if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
  2566. (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
  2567. (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
  2568. (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  2569. /*
  2570. * If no UNIT ATTENTION conditions will be established for
  2571. * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
  2572. * go ahead and check for APTPL=1 update+write below
  2573. */
  2574. goto write_aptpl;
  2575. }
  2576. spin_lock(&pr_tmpl->registration_lock);
  2577. list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
  2578. pr_reg_list) {
  2579. /*
  2580. * Do not establish a UNIT ATTENTION condition
  2581. * for the calling I_T Nexus
  2582. */
  2583. if (pr_reg_p == pr_reg)
  2584. continue;
  2585. core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
  2586. pr_reg_p->pr_res_mapped_lun,
  2587. 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
  2588. }
  2589. spin_unlock(&pr_tmpl->registration_lock);
  2590. write_aptpl:
  2591. if (pr_tmpl->pr_aptpl_active) {
  2592. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2593. &pr_reg->pr_aptpl_buf[0],
  2594. pr_tmpl->pr_aptpl_buf_len);
  2595. if (!ret)
  2596. pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
  2597. }
  2598. core_scsi3_put_pr_reg(pr_reg);
  2599. return 0;
  2600. }
  2601. static int core_scsi3_emulate_pro_clear(
  2602. struct se_cmd *cmd,
  2603. u64 res_key)
  2604. {
  2605. struct se_device *dev = cmd->se_dev;
  2606. struct se_node_acl *pr_reg_nacl;
  2607. struct se_session *se_sess = cmd->se_sess;
  2608. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2609. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2610. u32 pr_res_mapped_lun = 0;
  2611. int calling_it_nexus = 0;
  2612. /*
  2613. * Locate the existing *pr_reg via struct se_node_acl pointers
  2614. */
  2615. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
  2616. se_sess->se_node_acl, se_sess);
  2617. if (!pr_reg_n) {
  2618. pr_err("SPC-3 PR: Unable to locate"
  2619. " PR_REGISTERED *pr_reg for CLEAR\n");
  2620. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2621. return -EINVAL;
  2622. }
  2623. /*
  2624. * From spc4r17 section 5.7.11.6, Clearing:
  2625. *
  2626. * Any application client may release the persistent reservation and
  2627. * remove all registrations from a device server by issuing a
  2628. * PERSISTENT RESERVE OUT command with CLEAR service action through a
  2629. * registered I_T nexus with the following parameter:
  2630. *
  2631. * a) RESERVATION KEY field set to the value of the reservation key
  2632. * that is registered with the logical unit for the I_T nexus.
  2633. */
  2634. if (res_key != pr_reg_n->pr_res_key) {
  2635. pr_err("SPC-3 PR REGISTER: Received"
  2636. " res_key: 0x%016Lx does not match"
  2637. " existing SA REGISTER res_key:"
  2638. " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
  2639. core_scsi3_put_pr_reg(pr_reg_n);
  2640. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2641. return -EINVAL;
  2642. }
  2643. /*
  2644. * a) Release the persistent reservation, if any;
  2645. */
  2646. spin_lock(&dev->dev_reservation_lock);
  2647. pr_res_holder = dev->dev_pr_res_holder;
  2648. if (pr_res_holder) {
  2649. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2650. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  2651. pr_res_holder, 0);
  2652. }
  2653. spin_unlock(&dev->dev_reservation_lock);
  2654. /*
  2655. * b) Remove all registration(s) (see spc4r17 5.7.7);
  2656. */
  2657. spin_lock(&pr_tmpl->registration_lock);
  2658. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2659. &pr_tmpl->registration_list, pr_reg_list) {
  2660. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2661. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2662. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2663. __core_scsi3_free_registration(dev, pr_reg, NULL,
  2664. calling_it_nexus);
  2665. /*
  2666. * e) Establish a unit attention condition for the initiator
  2667. * port associated with every registered I_T nexus other
  2668. * than the I_T nexus on which the PERSISTENT RESERVE OUT
  2669. * command with CLEAR service action was received, with the
  2670. * additional sense code set to RESERVATIONS PREEMPTED.
  2671. */
  2672. if (!calling_it_nexus)
  2673. core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
  2674. 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
  2675. }
  2676. spin_unlock(&pr_tmpl->registration_lock);
  2677. pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
  2678. cmd->se_tfo->get_fabric_name());
  2679. if (pr_tmpl->pr_aptpl_active) {
  2680. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  2681. pr_debug("SPC-3 PR: Updated APTPL metadata"
  2682. " for CLEAR\n");
  2683. }
  2684. core_scsi3_pr_generation(dev);
  2685. return 0;
  2686. }
  2687. /*
  2688. * Called with struct se_device->dev_reservation_lock held.
  2689. */
  2690. static void __core_scsi3_complete_pro_preempt(
  2691. struct se_device *dev,
  2692. struct t10_pr_registration *pr_reg,
  2693. struct list_head *preempt_and_abort_list,
  2694. int type,
  2695. int scope,
  2696. int abort)
  2697. {
  2698. struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
  2699. struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
  2700. char i_buf[PR_REG_ISID_ID_LEN];
  2701. int prf_isid;
  2702. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2703. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2704. PR_REG_ISID_ID_LEN);
  2705. /*
  2706. * Do an implict RELEASE of the existing reservation.
  2707. */
  2708. if (dev->dev_pr_res_holder)
  2709. __core_scsi3_complete_pro_release(dev, nacl,
  2710. dev->dev_pr_res_holder, 0);
  2711. dev->dev_pr_res_holder = pr_reg;
  2712. pr_reg->pr_res_holder = 1;
  2713. pr_reg->pr_res_type = type;
  2714. pr_reg->pr_res_scope = scope;
  2715. pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
  2716. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2717. tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
  2718. core_scsi3_pr_dump_type(type),
  2719. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2720. pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
  2721. tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
  2722. nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
  2723. /*
  2724. * For PREEMPT_AND_ABORT, add the preempting reservation's
  2725. * struct t10_pr_registration to the list that will be compared
  2726. * against received CDBs..
  2727. */
  2728. if (preempt_and_abort_list)
  2729. list_add_tail(&pr_reg->pr_reg_abort_list,
  2730. preempt_and_abort_list);
  2731. }
  2732. static void core_scsi3_release_preempt_and_abort(
  2733. struct list_head *preempt_and_abort_list,
  2734. struct t10_pr_registration *pr_reg_holder)
  2735. {
  2736. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  2737. list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
  2738. pr_reg_abort_list) {
  2739. list_del(&pr_reg->pr_reg_abort_list);
  2740. if (pr_reg_holder == pr_reg)
  2741. continue;
  2742. if (pr_reg->pr_res_holder) {
  2743. pr_warn("pr_reg->pr_res_holder still set\n");
  2744. continue;
  2745. }
  2746. pr_reg->pr_reg_deve = NULL;
  2747. pr_reg->pr_reg_nacl = NULL;
  2748. kfree(pr_reg->pr_aptpl_buf);
  2749. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  2750. }
  2751. }
  2752. static int core_scsi3_pro_preempt(
  2753. struct se_cmd *cmd,
  2754. int type,
  2755. int scope,
  2756. u64 res_key,
  2757. u64 sa_res_key,
  2758. int abort)
  2759. {
  2760. struct se_device *dev = cmd->se_dev;
  2761. struct se_dev_entry *se_deve;
  2762. struct se_node_acl *pr_reg_nacl;
  2763. struct se_session *se_sess = cmd->se_sess;
  2764. struct list_head preempt_and_abort_list;
  2765. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2766. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2767. u32 pr_res_mapped_lun = 0;
  2768. int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
  2769. int prh_type = 0, prh_scope = 0, ret;
  2770. if (!se_sess) {
  2771. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2772. return -EINVAL;
  2773. }
  2774. se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  2775. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2776. se_sess);
  2777. if (!pr_reg_n) {
  2778. pr_err("SPC-3 PR: Unable to locate"
  2779. " PR_REGISTERED *pr_reg for PREEMPT%s\n",
  2780. (abort) ? "_AND_ABORT" : "");
  2781. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2782. return -EINVAL;
  2783. }
  2784. if (pr_reg_n->pr_res_key != res_key) {
  2785. core_scsi3_put_pr_reg(pr_reg_n);
  2786. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2787. return -EINVAL;
  2788. }
  2789. if (scope != PR_SCOPE_LU_SCOPE) {
  2790. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2791. core_scsi3_put_pr_reg(pr_reg_n);
  2792. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2793. return -EINVAL;
  2794. }
  2795. INIT_LIST_HEAD(&preempt_and_abort_list);
  2796. spin_lock(&dev->dev_reservation_lock);
  2797. pr_res_holder = dev->dev_pr_res_holder;
  2798. if (pr_res_holder &&
  2799. ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2800. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
  2801. all_reg = 1;
  2802. if (!all_reg && !sa_res_key) {
  2803. spin_unlock(&dev->dev_reservation_lock);
  2804. core_scsi3_put_pr_reg(pr_reg_n);
  2805. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2806. return -EINVAL;
  2807. }
  2808. /*
  2809. * From spc4r17, section 5.7.11.4.4 Removing Registrations:
  2810. *
  2811. * If the SERVICE ACTION RESERVATION KEY field does not identify a
  2812. * persistent reservation holder or there is no persistent reservation
  2813. * holder (i.e., there is no persistent reservation), then the device
  2814. * server shall perform a preempt by doing the following in an
  2815. * uninterrupted series of actions. (See below..)
  2816. */
  2817. if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
  2818. /*
  2819. * No existing or SA Reservation Key matching reservations..
  2820. *
  2821. * PROUT SA PREEMPT with All Registrant type reservations are
  2822. * allowed to be processed without a matching SA Reservation Key
  2823. */
  2824. spin_lock(&pr_tmpl->registration_lock);
  2825. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2826. &pr_tmpl->registration_list, pr_reg_list) {
  2827. /*
  2828. * Removing of registrations in non all registrants
  2829. * type reservations without a matching SA reservation
  2830. * key.
  2831. *
  2832. * a) Remove the registrations for all I_T nexuses
  2833. * specified by the SERVICE ACTION RESERVATION KEY
  2834. * field;
  2835. * b) Ignore the contents of the SCOPE and TYPE fields;
  2836. * c) Process tasks as defined in 5.7.1; and
  2837. * d) Establish a unit attention condition for the
  2838. * initiator port associated with every I_T nexus
  2839. * that lost its registration other than the I_T
  2840. * nexus on which the PERSISTENT RESERVE OUT command
  2841. * was received, with the additional sense code set
  2842. * to REGISTRATIONS PREEMPTED.
  2843. */
  2844. if (!all_reg) {
  2845. if (pr_reg->pr_res_key != sa_res_key)
  2846. continue;
  2847. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2848. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2849. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2850. __core_scsi3_free_registration(dev, pr_reg,
  2851. (abort) ? &preempt_and_abort_list :
  2852. NULL, calling_it_nexus);
  2853. released_regs++;
  2854. } else {
  2855. /*
  2856. * Case for any existing all registrants type
  2857. * reservation, follow logic in spc4r17 section
  2858. * 5.7.11.4 Preempting, Table 52 and Figure 7.
  2859. *
  2860. * For a ZERO SA Reservation key, release
  2861. * all other registrations and do an implict
  2862. * release of active persistent reservation.
  2863. *
  2864. * For a non-ZERO SA Reservation key, only
  2865. * release the matching reservation key from
  2866. * registrations.
  2867. */
  2868. if ((sa_res_key) &&
  2869. (pr_reg->pr_res_key != sa_res_key))
  2870. continue;
  2871. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2872. if (calling_it_nexus)
  2873. continue;
  2874. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2875. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2876. __core_scsi3_free_registration(dev, pr_reg,
  2877. (abort) ? &preempt_and_abort_list :
  2878. NULL, 0);
  2879. released_regs++;
  2880. }
  2881. if (!calling_it_nexus)
  2882. core_scsi3_ua_allocate(pr_reg_nacl,
  2883. pr_res_mapped_lun, 0x2A,
  2884. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2885. }
  2886. spin_unlock(&pr_tmpl->registration_lock);
  2887. /*
  2888. * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
  2889. * a PREEMPT AND ABORT service action sets the SERVICE ACTION
  2890. * RESERVATION KEY field to a value that does not match any
  2891. * registered reservation key, then the device server shall
  2892. * complete the command with RESERVATION CONFLICT status.
  2893. */
  2894. if (!released_regs) {
  2895. spin_unlock(&dev->dev_reservation_lock);
  2896. core_scsi3_put_pr_reg(pr_reg_n);
  2897. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2898. return -EINVAL;
  2899. }
  2900. /*
  2901. * For an existing all registrants type reservation
  2902. * with a zero SA rservation key, preempt the existing
  2903. * reservation with the new PR type and scope.
  2904. */
  2905. if (pr_res_holder && all_reg && !(sa_res_key)) {
  2906. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2907. (abort) ? &preempt_and_abort_list : NULL,
  2908. type, scope, abort);
  2909. if (abort)
  2910. core_scsi3_release_preempt_and_abort(
  2911. &preempt_and_abort_list, pr_reg_n);
  2912. }
  2913. spin_unlock(&dev->dev_reservation_lock);
  2914. if (pr_tmpl->pr_aptpl_active) {
  2915. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2916. &pr_reg_n->pr_aptpl_buf[0],
  2917. pr_tmpl->pr_aptpl_buf_len);
  2918. if (!ret)
  2919. pr_debug("SPC-3 PR: Updated APTPL"
  2920. " metadata for PREEMPT%s\n", (abort) ?
  2921. "_AND_ABORT" : "");
  2922. }
  2923. core_scsi3_put_pr_reg(pr_reg_n);
  2924. core_scsi3_pr_generation(cmd->se_dev);
  2925. return 0;
  2926. }
  2927. /*
  2928. * The PREEMPTing SA reservation key matches that of the
  2929. * existing persistent reservation, first, we check if
  2930. * we are preempting our own reservation.
  2931. * From spc4r17, section 5.7.11.4.3 Preempting
  2932. * persistent reservations and registration handling
  2933. *
  2934. * If an all registrants persistent reservation is not
  2935. * present, it is not an error for the persistent
  2936. * reservation holder to preempt itself (i.e., a
  2937. * PERSISTENT RESERVE OUT with a PREEMPT service action
  2938. * or a PREEMPT AND ABORT service action with the
  2939. * SERVICE ACTION RESERVATION KEY value equal to the
  2940. * persistent reservation holder's reservation key that
  2941. * is received from the persistent reservation holder).
  2942. * In that case, the device server shall establish the
  2943. * new persistent reservation and maintain the
  2944. * registration.
  2945. */
  2946. prh_type = pr_res_holder->pr_res_type;
  2947. prh_scope = pr_res_holder->pr_res_scope;
  2948. /*
  2949. * If the SERVICE ACTION RESERVATION KEY field identifies a
  2950. * persistent reservation holder (see 5.7.10), the device
  2951. * server shall perform a preempt by doing the following as
  2952. * an uninterrupted series of actions:
  2953. *
  2954. * a) Release the persistent reservation for the holder
  2955. * identified by the SERVICE ACTION RESERVATION KEY field;
  2956. */
  2957. if (pr_reg_n != pr_res_holder)
  2958. __core_scsi3_complete_pro_release(dev,
  2959. pr_res_holder->pr_reg_nacl,
  2960. dev->dev_pr_res_holder, 0);
  2961. /*
  2962. * b) Remove the registrations for all I_T nexuses identified
  2963. * by the SERVICE ACTION RESERVATION KEY field, except the
  2964. * I_T nexus that is being used for the PERSISTENT RESERVE
  2965. * OUT command. If an all registrants persistent reservation
  2966. * is present and the SERVICE ACTION RESERVATION KEY field
  2967. * is set to zero, then all registrations shall be removed
  2968. * except for that of the I_T nexus that is being used for
  2969. * the PERSISTENT RESERVE OUT command;
  2970. */
  2971. spin_lock(&pr_tmpl->registration_lock);
  2972. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2973. &pr_tmpl->registration_list, pr_reg_list) {
  2974. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2975. if (calling_it_nexus)
  2976. continue;
  2977. if (pr_reg->pr_res_key != sa_res_key)
  2978. continue;
  2979. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2980. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2981. __core_scsi3_free_registration(dev, pr_reg,
  2982. (abort) ? &preempt_and_abort_list : NULL,
  2983. calling_it_nexus);
  2984. /*
  2985. * e) Establish a unit attention condition for the initiator
  2986. * port associated with every I_T nexus that lost its
  2987. * persistent reservation and/or registration, with the
  2988. * additional sense code set to REGISTRATIONS PREEMPTED;
  2989. */
  2990. core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
  2991. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2992. }
  2993. spin_unlock(&pr_tmpl->registration_lock);
  2994. /*
  2995. * c) Establish a persistent reservation for the preempting
  2996. * I_T nexus using the contents of the SCOPE and TYPE fields;
  2997. */
  2998. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2999. (abort) ? &preempt_and_abort_list : NULL,
  3000. type, scope, abort);
  3001. /*
  3002. * d) Process tasks as defined in 5.7.1;
  3003. * e) See above..
  3004. * f) If the type or scope has changed, then for every I_T nexus
  3005. * whose reservation key was not removed, except for the I_T
  3006. * nexus on which the PERSISTENT RESERVE OUT command was
  3007. * received, the device server shall establish a unit
  3008. * attention condition for the initiator port associated with
  3009. * that I_T nexus, with the additional sense code set to
  3010. * RESERVATIONS RELEASED. If the type or scope have not
  3011. * changed, then no unit attention condition(s) shall be
  3012. * established for this reason.
  3013. */
  3014. if ((prh_type != type) || (prh_scope != scope)) {
  3015. spin_lock(&pr_tmpl->registration_lock);
  3016. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  3017. &pr_tmpl->registration_list, pr_reg_list) {
  3018. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  3019. if (calling_it_nexus)
  3020. continue;
  3021. core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
  3022. pr_reg->pr_res_mapped_lun, 0x2A,
  3023. ASCQ_2AH_RESERVATIONS_RELEASED);
  3024. }
  3025. spin_unlock(&pr_tmpl->registration_lock);
  3026. }
  3027. spin_unlock(&dev->dev_reservation_lock);
  3028. /*
  3029. * Call LUN_RESET logic upon list of struct t10_pr_registration,
  3030. * All received CDBs for the matching existing reservation and
  3031. * registrations undergo ABORT_TASK logic.
  3032. *
  3033. * From there, core_scsi3_release_preempt_and_abort() will
  3034. * release every registration in the list (which have already
  3035. * been removed from the primary pr_reg list), except the
  3036. * new persistent reservation holder, the calling Initiator Port.
  3037. */
  3038. if (abort) {
  3039. core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
  3040. core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
  3041. pr_reg_n);
  3042. }
  3043. if (pr_tmpl->pr_aptpl_active) {
  3044. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  3045. &pr_reg_n->pr_aptpl_buf[0],
  3046. pr_tmpl->pr_aptpl_buf_len);
  3047. if (!ret)
  3048. pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
  3049. "%s\n", (abort) ? "_AND_ABORT" : "");
  3050. }
  3051. core_scsi3_put_pr_reg(pr_reg_n);
  3052. core_scsi3_pr_generation(cmd->se_dev);
  3053. return 0;
  3054. }
  3055. static int core_scsi3_emulate_pro_preempt(
  3056. struct se_cmd *cmd,
  3057. int type,
  3058. int scope,
  3059. u64 res_key,
  3060. u64 sa_res_key,
  3061. int abort)
  3062. {
  3063. int ret = 0;
  3064. switch (type) {
  3065. case PR_TYPE_WRITE_EXCLUSIVE:
  3066. case PR_TYPE_EXCLUSIVE_ACCESS:
  3067. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  3068. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  3069. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  3070. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  3071. ret = core_scsi3_pro_preempt(cmd, type, scope,
  3072. res_key, sa_res_key, abort);
  3073. break;
  3074. default:
  3075. pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
  3076. " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
  3077. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3078. return -EINVAL;
  3079. }
  3080. return ret;
  3081. }
  3082. static int core_scsi3_emulate_pro_register_and_move(
  3083. struct se_cmd *cmd,
  3084. u64 res_key,
  3085. u64 sa_res_key,
  3086. int aptpl,
  3087. int unreg)
  3088. {
  3089. struct se_session *se_sess = cmd->se_sess;
  3090. struct se_device *dev = cmd->se_dev;
  3091. struct se_dev_entry *se_deve, *dest_se_deve = NULL;
  3092. struct se_lun *se_lun = cmd->se_lun;
  3093. struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
  3094. struct se_port *se_port;
  3095. struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
  3096. struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
  3097. struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
  3098. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  3099. unsigned char *buf;
  3100. unsigned char *initiator_str;
  3101. char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
  3102. u32 tid_len, tmp_tid_len;
  3103. int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
  3104. unsigned short rtpi;
  3105. unsigned char proto_ident;
  3106. if (!se_sess || !se_lun) {
  3107. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  3108. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3109. return -EINVAL;
  3110. }
  3111. memset(dest_iport, 0, 64);
  3112. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  3113. se_tpg = se_sess->se_tpg;
  3114. tf_ops = se_tpg->se_tpg_tfo;
  3115. se_deve = &se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  3116. /*
  3117. * Follow logic from spc4r17 Section 5.7.8, Table 50 --
  3118. * Register behaviors for a REGISTER AND MOVE service action
  3119. *
  3120. * Locate the existing *pr_reg via struct se_node_acl pointers
  3121. */
  3122. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  3123. se_sess);
  3124. if (!pr_reg) {
  3125. pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
  3126. " *pr_reg for REGISTER_AND_MOVE\n");
  3127. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3128. return -EINVAL;
  3129. }
  3130. /*
  3131. * The provided reservation key much match the existing reservation key
  3132. * provided during this initiator's I_T nexus registration.
  3133. */
  3134. if (res_key != pr_reg->pr_res_key) {
  3135. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
  3136. " res_key: 0x%016Lx does not match existing SA REGISTER"
  3137. " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  3138. core_scsi3_put_pr_reg(pr_reg);
  3139. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3140. return -EINVAL;
  3141. }
  3142. /*
  3143. * The service active reservation key needs to be non zero
  3144. */
  3145. if (!sa_res_key) {
  3146. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
  3147. " sa_res_key\n");
  3148. core_scsi3_put_pr_reg(pr_reg);
  3149. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3150. return -EINVAL;
  3151. }
  3152. /*
  3153. * Determine the Relative Target Port Identifier where the reservation
  3154. * will be moved to for the TransportID containing SCSI initiator WWN
  3155. * information.
  3156. */
  3157. buf = transport_kmap_data_sg(cmd);
  3158. rtpi = (buf[18] & 0xff) << 8;
  3159. rtpi |= buf[19] & 0xff;
  3160. tid_len = (buf[20] & 0xff) << 24;
  3161. tid_len |= (buf[21] & 0xff) << 16;
  3162. tid_len |= (buf[22] & 0xff) << 8;
  3163. tid_len |= buf[23] & 0xff;
  3164. transport_kunmap_data_sg(cmd);
  3165. buf = NULL;
  3166. if ((tid_len + 24) != cmd->data_length) {
  3167. pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
  3168. " does not equal CDB data_length: %u\n", tid_len,
  3169. cmd->data_length);
  3170. core_scsi3_put_pr_reg(pr_reg);
  3171. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3172. return -EINVAL;
  3173. }
  3174. spin_lock(&dev->se_port_lock);
  3175. list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
  3176. if (se_port->sep_rtpi != rtpi)
  3177. continue;
  3178. dest_se_tpg = se_port->sep_tpg;
  3179. if (!dest_se_tpg)
  3180. continue;
  3181. dest_tf_ops = dest_se_tpg->se_tpg_tfo;
  3182. if (!dest_tf_ops)
  3183. continue;
  3184. atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
  3185. smp_mb__after_atomic_inc();
  3186. spin_unlock(&dev->se_port_lock);
  3187. ret = core_scsi3_tpg_depend_item(dest_se_tpg);
  3188. if (ret != 0) {
  3189. pr_err("core_scsi3_tpg_depend_item() failed"
  3190. " for dest_se_tpg\n");
  3191. atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
  3192. smp_mb__after_atomic_dec();
  3193. core_scsi3_put_pr_reg(pr_reg);
  3194. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3195. return -EINVAL;
  3196. }
  3197. spin_lock(&dev->se_port_lock);
  3198. break;
  3199. }
  3200. spin_unlock(&dev->se_port_lock);
  3201. if (!dest_se_tpg || !dest_tf_ops) {
  3202. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  3203. " fabric ops from Relative Target Port Identifier:"
  3204. " %hu\n", rtpi);
  3205. core_scsi3_put_pr_reg(pr_reg);
  3206. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3207. return -EINVAL;
  3208. }
  3209. buf = transport_kmap_data_sg(cmd);
  3210. proto_ident = (buf[24] & 0x0f);
  3211. #if 0
  3212. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
  3213. " 0x%02x\n", proto_ident);
  3214. #endif
  3215. if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
  3216. pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
  3217. " proto_ident: 0x%02x does not match ident: 0x%02x"
  3218. " from fabric: %s\n", proto_ident,
  3219. dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
  3220. dest_tf_ops->get_fabric_name());
  3221. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3222. ret = -EINVAL;
  3223. goto out;
  3224. }
  3225. if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
  3226. pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
  3227. " containg a valid tpg_parse_pr_out_transport_id"
  3228. " function pointer\n");
  3229. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3230. ret = -EINVAL;
  3231. goto out;
  3232. }
  3233. initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
  3234. (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
  3235. if (!initiator_str) {
  3236. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  3237. " initiator_str from Transport ID\n");
  3238. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3239. ret = -EINVAL;
  3240. goto out;
  3241. }
  3242. transport_kunmap_data_sg(cmd);
  3243. buf = NULL;
  3244. pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
  3245. " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
  3246. "port" : "device", initiator_str, (iport_ptr != NULL) ?
  3247. iport_ptr : "");
  3248. /*
  3249. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3250. * action specifies a TransportID that is the same as the initiator port
  3251. * of the I_T nexus for the command received, then the command shall
  3252. * be terminated with CHECK CONDITION status, with the sense key set to
  3253. * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
  3254. * IN PARAMETER LIST.
  3255. */
  3256. pr_reg_nacl = pr_reg->pr_reg_nacl;
  3257. matching_iname = (!strcmp(initiator_str,
  3258. pr_reg_nacl->initiatorname)) ? 1 : 0;
  3259. if (!matching_iname)
  3260. goto after_iport_check;
  3261. if (!iport_ptr || !pr_reg->isid_present_at_reg) {
  3262. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
  3263. " matches: %s on received I_T Nexus\n", initiator_str,
  3264. pr_reg_nacl->initiatorname);
  3265. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3266. ret = -EINVAL;
  3267. goto out;
  3268. }
  3269. if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
  3270. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
  3271. " matches: %s %s on received I_T Nexus\n",
  3272. initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
  3273. pr_reg->pr_reg_isid);
  3274. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3275. ret = -EINVAL;
  3276. goto out;
  3277. }
  3278. after_iport_check:
  3279. /*
  3280. * Locate the destination struct se_node_acl from the received Transport ID
  3281. */
  3282. spin_lock_irq(&dest_se_tpg->acl_node_lock);
  3283. dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
  3284. initiator_str);
  3285. if (dest_node_acl) {
  3286. atomic_inc(&dest_node_acl->acl_pr_ref_count);
  3287. smp_mb__after_atomic_inc();
  3288. }
  3289. spin_unlock_irq(&dest_se_tpg->acl_node_lock);
  3290. if (!dest_node_acl) {
  3291. pr_err("Unable to locate %s dest_node_acl for"
  3292. " TransportID%s\n", dest_tf_ops->get_fabric_name(),
  3293. initiator_str);
  3294. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3295. ret = -EINVAL;
  3296. goto out;
  3297. }
  3298. ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
  3299. if (ret != 0) {
  3300. pr_err("core_scsi3_nodeacl_depend_item() for"
  3301. " dest_node_acl\n");
  3302. atomic_dec(&dest_node_acl->acl_pr_ref_count);
  3303. smp_mb__after_atomic_dec();
  3304. dest_node_acl = NULL;
  3305. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3306. ret = -EINVAL;
  3307. goto out;
  3308. }
  3309. #if 0
  3310. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
  3311. " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
  3312. dest_node_acl->initiatorname);
  3313. #endif
  3314. /*
  3315. * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
  3316. * PORT IDENTIFIER.
  3317. */
  3318. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
  3319. if (!dest_se_deve) {
  3320. pr_err("Unable to locate %s dest_se_deve from RTPI:"
  3321. " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
  3322. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3323. ret = -EINVAL;
  3324. goto out;
  3325. }
  3326. ret = core_scsi3_lunacl_depend_item(dest_se_deve);
  3327. if (ret < 0) {
  3328. pr_err("core_scsi3_lunacl_depend_item() failed\n");
  3329. atomic_dec(&dest_se_deve->pr_ref_count);
  3330. smp_mb__after_atomic_dec();
  3331. dest_se_deve = NULL;
  3332. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3333. ret = -EINVAL;
  3334. goto out;
  3335. }
  3336. #if 0
  3337. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
  3338. " ACL for dest_se_deve->mapped_lun: %u\n",
  3339. dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
  3340. dest_se_deve->mapped_lun);
  3341. #endif
  3342. /*
  3343. * A persistent reservation needs to already existing in order to
  3344. * successfully complete the REGISTER_AND_MOVE service action..
  3345. */
  3346. spin_lock(&dev->dev_reservation_lock);
  3347. pr_res_holder = dev->dev_pr_res_holder;
  3348. if (!pr_res_holder) {
  3349. pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
  3350. " currently held\n");
  3351. spin_unlock(&dev->dev_reservation_lock);
  3352. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3353. ret = -EINVAL;
  3354. goto out;
  3355. }
  3356. /*
  3357. * The received on I_T Nexus must be the reservation holder.
  3358. *
  3359. * From spc4r17 section 5.7.8 Table 50 --
  3360. * Register behaviors for a REGISTER AND MOVE service action
  3361. */
  3362. if (pr_res_holder != pr_reg) {
  3363. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
  3364. " Nexus is not reservation holder\n");
  3365. spin_unlock(&dev->dev_reservation_lock);
  3366. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3367. ret = -EINVAL;
  3368. goto out;
  3369. }
  3370. /*
  3371. * From spc4r17 section 5.7.8: registering and moving reservation
  3372. *
  3373. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3374. * action is received and the established persistent reservation is a
  3375. * Write Exclusive - All Registrants type or Exclusive Access -
  3376. * All Registrants type reservation, then the command shall be completed
  3377. * with RESERVATION CONFLICT status.
  3378. */
  3379. if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3380. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  3381. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
  3382. " reservation for type: %s\n",
  3383. core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
  3384. spin_unlock(&dev->dev_reservation_lock);
  3385. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3386. ret = -EINVAL;
  3387. goto out;
  3388. }
  3389. pr_res_nacl = pr_res_holder->pr_reg_nacl;
  3390. /*
  3391. * b) Ignore the contents of the (received) SCOPE and TYPE fields;
  3392. */
  3393. type = pr_res_holder->pr_res_type;
  3394. scope = pr_res_holder->pr_res_type;
  3395. /*
  3396. * c) Associate the reservation key specified in the SERVICE ACTION
  3397. * RESERVATION KEY field with the I_T nexus specified as the
  3398. * destination of the register and move, where:
  3399. * A) The I_T nexus is specified by the TransportID and the
  3400. * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
  3401. * B) Regardless of the TransportID format used, the association for
  3402. * the initiator port is based on either the initiator port name
  3403. * (see 3.1.71) on SCSI transport protocols where port names are
  3404. * required or the initiator port identifier (see 3.1.70) on SCSI
  3405. * transport protocols where port names are not required;
  3406. * d) Register the reservation key specified in the SERVICE ACTION
  3407. * RESERVATION KEY field;
  3408. * e) Retain the reservation key specified in the SERVICE ACTION
  3409. * RESERVATION KEY field and associated information;
  3410. *
  3411. * Also, It is not an error for a REGISTER AND MOVE service action to
  3412. * register an I_T nexus that is already registered with the same
  3413. * reservation key or a different reservation key.
  3414. */
  3415. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3416. iport_ptr);
  3417. if (!dest_pr_reg) {
  3418. ret = core_scsi3_alloc_registration(cmd->se_dev,
  3419. dest_node_acl, dest_se_deve, iport_ptr,
  3420. sa_res_key, 0, aptpl, 2, 1);
  3421. if (ret != 0) {
  3422. spin_unlock(&dev->dev_reservation_lock);
  3423. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3424. ret = -EINVAL;
  3425. goto out;
  3426. }
  3427. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3428. iport_ptr);
  3429. new_reg = 1;
  3430. }
  3431. /*
  3432. * f) Release the persistent reservation for the persistent reservation
  3433. * holder (i.e., the I_T nexus on which the
  3434. */
  3435. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  3436. dev->dev_pr_res_holder, 0);
  3437. /*
  3438. * g) Move the persistent reservation to the specified I_T nexus using
  3439. * the same scope and type as the persistent reservation released in
  3440. * item f); and
  3441. */
  3442. dev->dev_pr_res_holder = dest_pr_reg;
  3443. dest_pr_reg->pr_res_holder = 1;
  3444. dest_pr_reg->pr_res_type = type;
  3445. pr_reg->pr_res_scope = scope;
  3446. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  3447. PR_REG_ISID_ID_LEN);
  3448. /*
  3449. * Increment PRGeneration for existing registrations..
  3450. */
  3451. if (!new_reg)
  3452. dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
  3453. spin_unlock(&dev->dev_reservation_lock);
  3454. pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
  3455. " created new reservation holder TYPE: %s on object RTPI:"
  3456. " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
  3457. core_scsi3_pr_dump_type(type), rtpi,
  3458. dest_pr_reg->pr_res_generation);
  3459. pr_debug("SPC-3 PR Successfully moved reservation from"
  3460. " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
  3461. tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
  3462. (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
  3463. dest_node_acl->initiatorname, (iport_ptr != NULL) ?
  3464. iport_ptr : "");
  3465. /*
  3466. * It is now safe to release configfs group dependencies for destination
  3467. * of Transport ID Initiator Device/Port Identifier
  3468. */
  3469. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3470. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3471. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3472. /*
  3473. * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
  3474. * nexus on which PERSISTENT RESERVE OUT command was received.
  3475. */
  3476. if (unreg) {
  3477. spin_lock(&pr_tmpl->registration_lock);
  3478. __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
  3479. spin_unlock(&pr_tmpl->registration_lock);
  3480. } else
  3481. core_scsi3_put_pr_reg(pr_reg);
  3482. /*
  3483. * Clear the APTPL metadata if APTPL has been disabled, otherwise
  3484. * write out the updated metadata to struct file for this SCSI device.
  3485. */
  3486. if (!aptpl) {
  3487. pr_tmpl->pr_aptpl_active = 0;
  3488. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  3489. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
  3490. " REGISTER_AND_MOVE\n");
  3491. } else {
  3492. pr_tmpl->pr_aptpl_active = 1;
  3493. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  3494. &dest_pr_reg->pr_aptpl_buf[0],
  3495. pr_tmpl->pr_aptpl_buf_len);
  3496. if (!ret)
  3497. pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
  3498. " REGISTER_AND_MOVE\n");
  3499. }
  3500. transport_kunmap_data_sg(cmd);
  3501. core_scsi3_put_pr_reg(dest_pr_reg);
  3502. return 0;
  3503. out:
  3504. if (buf)
  3505. transport_kunmap_data_sg(cmd);
  3506. if (dest_se_deve)
  3507. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3508. if (dest_node_acl)
  3509. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3510. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3511. core_scsi3_put_pr_reg(pr_reg);
  3512. return ret;
  3513. }
  3514. static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
  3515. {
  3516. unsigned int __v1, __v2;
  3517. __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
  3518. __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
  3519. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  3520. }
  3521. /*
  3522. * See spc4r17 section 6.14 Table 170
  3523. */
  3524. int target_scsi3_emulate_pr_out(struct se_task *task)
  3525. {
  3526. struct se_cmd *cmd = task->task_se_cmd;
  3527. unsigned char *cdb = &cmd->t_task_cdb[0];
  3528. unsigned char *buf;
  3529. u64 res_key, sa_res_key;
  3530. int sa, scope, type, aptpl;
  3531. int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
  3532. int ret;
  3533. /*
  3534. * Following spc2r20 5.5.1 Reservations overview:
  3535. *
  3536. * If a logical unit has been reserved by any RESERVE command and is
  3537. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3538. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3539. * initiator or service action and shall terminate with a RESERVATION
  3540. * CONFLICT status.
  3541. */
  3542. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
  3543. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3544. " SPC-2 reservation is held, returning"
  3545. " RESERVATION_CONFLICT\n");
  3546. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3547. ret = EINVAL;
  3548. goto out;
  3549. }
  3550. /*
  3551. * FIXME: A NULL struct se_session pointer means an this is not coming from
  3552. * a $FABRIC_MOD's nexus, but from internal passthrough ops.
  3553. */
  3554. if (!cmd->se_sess) {
  3555. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3556. return -EINVAL;
  3557. }
  3558. if (cmd->data_length < 24) {
  3559. pr_warn("SPC-PR: Received PR OUT parameter list"
  3560. " length too small: %u\n", cmd->data_length);
  3561. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3562. ret = -EINVAL;
  3563. goto out;
  3564. }
  3565. /*
  3566. * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
  3567. */
  3568. sa = (cdb[1] & 0x1f);
  3569. scope = (cdb[2] & 0xf0);
  3570. type = (cdb[2] & 0x0f);
  3571. buf = transport_kmap_data_sg(cmd);
  3572. /*
  3573. * From PERSISTENT_RESERVE_OUT parameter list (payload)
  3574. */
  3575. res_key = core_scsi3_extract_reservation_key(&buf[0]);
  3576. sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
  3577. /*
  3578. * REGISTER_AND_MOVE uses a different SA parameter list containing
  3579. * SCSI TransportIDs.
  3580. */
  3581. if (sa != PRO_REGISTER_AND_MOVE) {
  3582. spec_i_pt = (buf[20] & 0x08);
  3583. all_tg_pt = (buf[20] & 0x04);
  3584. aptpl = (buf[20] & 0x01);
  3585. } else {
  3586. aptpl = (buf[17] & 0x01);
  3587. unreg = (buf[17] & 0x02);
  3588. }
  3589. transport_kunmap_data_sg(cmd);
  3590. buf = NULL;
  3591. /*
  3592. * SPEC_I_PT=1 is only valid for Service action: REGISTER
  3593. */
  3594. if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
  3595. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3596. ret = -EINVAL;
  3597. goto out;
  3598. }
  3599. /*
  3600. * From spc4r17 section 6.14:
  3601. *
  3602. * If the SPEC_I_PT bit is set to zero, the service action is not
  3603. * REGISTER AND MOVE, and the parameter list length is not 24, then
  3604. * the command shall be terminated with CHECK CONDITION status, with
  3605. * the sense key set to ILLEGAL REQUEST, and the additional sense
  3606. * code set to PARAMETER LIST LENGTH ERROR.
  3607. */
  3608. if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
  3609. (cmd->data_length != 24)) {
  3610. pr_warn("SPC-PR: Received PR OUT illegal parameter"
  3611. " list length: %u\n", cmd->data_length);
  3612. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3613. ret = -EINVAL;
  3614. goto out;
  3615. }
  3616. /*
  3617. * (core_scsi3_emulate_pro_* function parameters
  3618. * are defined by spc4r17 Table 174:
  3619. * PERSISTENT_RESERVE_OUT service actions and valid parameters.
  3620. */
  3621. switch (sa) {
  3622. case PRO_REGISTER:
  3623. ret = core_scsi3_emulate_pro_register(cmd,
  3624. res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
  3625. break;
  3626. case PRO_RESERVE:
  3627. ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
  3628. break;
  3629. case PRO_RELEASE:
  3630. ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
  3631. break;
  3632. case PRO_CLEAR:
  3633. ret = core_scsi3_emulate_pro_clear(cmd, res_key);
  3634. break;
  3635. case PRO_PREEMPT:
  3636. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3637. res_key, sa_res_key, 0);
  3638. break;
  3639. case PRO_PREEMPT_AND_ABORT:
  3640. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3641. res_key, sa_res_key, 1);
  3642. break;
  3643. case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
  3644. ret = core_scsi3_emulate_pro_register(cmd,
  3645. 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
  3646. break;
  3647. case PRO_REGISTER_AND_MOVE:
  3648. ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
  3649. sa_res_key, aptpl, unreg);
  3650. break;
  3651. default:
  3652. pr_err("Unknown PERSISTENT_RESERVE_OUT service"
  3653. " action: 0x%02x\n", cdb[1] & 0x1f);
  3654. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3655. ret = -EINVAL;
  3656. break;
  3657. }
  3658. out:
  3659. if (!ret) {
  3660. task->task_scsi_status = GOOD;
  3661. transport_complete_task(task, 1);
  3662. }
  3663. return ret;
  3664. }
  3665. /*
  3666. * PERSISTENT_RESERVE_IN Service Action READ_KEYS
  3667. *
  3668. * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
  3669. */
  3670. static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
  3671. {
  3672. struct se_device *se_dev = cmd->se_dev;
  3673. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3674. struct t10_pr_registration *pr_reg;
  3675. unsigned char *buf;
  3676. u32 add_len = 0, off = 8;
  3677. if (cmd->data_length < 8) {
  3678. pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
  3679. " too small\n", cmd->data_length);
  3680. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3681. return -EINVAL;
  3682. }
  3683. buf = transport_kmap_data_sg(cmd);
  3684. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3685. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3686. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3687. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3688. spin_lock(&su_dev->t10_pr.registration_lock);
  3689. list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
  3690. pr_reg_list) {
  3691. /*
  3692. * Check for overflow of 8byte PRI READ_KEYS payload and
  3693. * next reservation key list descriptor.
  3694. */
  3695. if ((add_len + 8) > (cmd->data_length - 8))
  3696. break;
  3697. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3698. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3699. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3700. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3701. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3702. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3703. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3704. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3705. add_len += 8;
  3706. }
  3707. spin_unlock(&su_dev->t10_pr.registration_lock);
  3708. buf[4] = ((add_len >> 24) & 0xff);
  3709. buf[5] = ((add_len >> 16) & 0xff);
  3710. buf[6] = ((add_len >> 8) & 0xff);
  3711. buf[7] = (add_len & 0xff);
  3712. transport_kunmap_data_sg(cmd);
  3713. return 0;
  3714. }
  3715. /*
  3716. * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
  3717. *
  3718. * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
  3719. */
  3720. static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
  3721. {
  3722. struct se_device *se_dev = cmd->se_dev;
  3723. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3724. struct t10_pr_registration *pr_reg;
  3725. unsigned char *buf;
  3726. u64 pr_res_key;
  3727. u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
  3728. if (cmd->data_length < 8) {
  3729. pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
  3730. " too small\n", cmd->data_length);
  3731. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3732. return -EINVAL;
  3733. }
  3734. buf = transport_kmap_data_sg(cmd);
  3735. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3736. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3737. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3738. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3739. spin_lock(&se_dev->dev_reservation_lock);
  3740. pr_reg = se_dev->dev_pr_res_holder;
  3741. if ((pr_reg)) {
  3742. /*
  3743. * Set the hardcoded Additional Length
  3744. */
  3745. buf[4] = ((add_len >> 24) & 0xff);
  3746. buf[5] = ((add_len >> 16) & 0xff);
  3747. buf[6] = ((add_len >> 8) & 0xff);
  3748. buf[7] = (add_len & 0xff);
  3749. if (cmd->data_length < 22)
  3750. goto err;
  3751. /*
  3752. * Set the Reservation key.
  3753. *
  3754. * From spc4r17, section 5.7.10:
  3755. * A persistent reservation holder has its reservation key
  3756. * returned in the parameter data from a PERSISTENT
  3757. * RESERVE IN command with READ RESERVATION service action as
  3758. * follows:
  3759. * a) For a persistent reservation of the type Write Exclusive
  3760. * - All Registrants or Exclusive Access ­ All Regitrants,
  3761. * the reservation key shall be set to zero; or
  3762. * b) For all other persistent reservation types, the
  3763. * reservation key shall be set to the registered
  3764. * reservation key for the I_T nexus that holds the
  3765. * persistent reservation.
  3766. */
  3767. if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3768. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
  3769. pr_res_key = 0;
  3770. else
  3771. pr_res_key = pr_reg->pr_res_key;
  3772. buf[8] = ((pr_res_key >> 56) & 0xff);
  3773. buf[9] = ((pr_res_key >> 48) & 0xff);
  3774. buf[10] = ((pr_res_key >> 40) & 0xff);
  3775. buf[11] = ((pr_res_key >> 32) & 0xff);
  3776. buf[12] = ((pr_res_key >> 24) & 0xff);
  3777. buf[13] = ((pr_res_key >> 16) & 0xff);
  3778. buf[14] = ((pr_res_key >> 8) & 0xff);
  3779. buf[15] = (pr_res_key & 0xff);
  3780. /*
  3781. * Set the SCOPE and TYPE
  3782. */
  3783. buf[21] = (pr_reg->pr_res_scope & 0xf0) |
  3784. (pr_reg->pr_res_type & 0x0f);
  3785. }
  3786. err:
  3787. spin_unlock(&se_dev->dev_reservation_lock);
  3788. transport_kunmap_data_sg(cmd);
  3789. return 0;
  3790. }
  3791. /*
  3792. * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
  3793. *
  3794. * See spc4r17 section 6.13.4 Table 165
  3795. */
  3796. static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
  3797. {
  3798. struct se_device *dev = cmd->se_dev;
  3799. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  3800. unsigned char *buf;
  3801. u16 add_len = 8; /* Hardcoded to 8. */
  3802. if (cmd->data_length < 6) {
  3803. pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
  3804. " %u too small\n", cmd->data_length);
  3805. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3806. return -EINVAL;
  3807. }
  3808. buf = transport_kmap_data_sg(cmd);
  3809. buf[0] = ((add_len << 8) & 0xff);
  3810. buf[1] = (add_len & 0xff);
  3811. buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
  3812. buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
  3813. buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
  3814. buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
  3815. /*
  3816. * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
  3817. * set the TMV: Task Mask Valid bit.
  3818. */
  3819. buf[3] |= 0x80;
  3820. /*
  3821. * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
  3822. */
  3823. buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
  3824. /*
  3825. * PTPL_A: Persistence across Target Power Loss Active bit
  3826. */
  3827. if (pr_tmpl->pr_aptpl_active)
  3828. buf[3] |= 0x01;
  3829. /*
  3830. * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
  3831. */
  3832. buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3833. buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
  3834. buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
  3835. buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
  3836. buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
  3837. buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3838. transport_kunmap_data_sg(cmd);
  3839. return 0;
  3840. }
  3841. /*
  3842. * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
  3843. *
  3844. * See spc4r17 section 6.13.5 Table 168 and 169
  3845. */
  3846. static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
  3847. {
  3848. struct se_device *se_dev = cmd->se_dev;
  3849. struct se_node_acl *se_nacl;
  3850. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3851. struct se_portal_group *se_tpg;
  3852. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  3853. struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
  3854. unsigned char *buf;
  3855. u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
  3856. u32 off = 8; /* off into first Full Status descriptor */
  3857. int format_code = 0;
  3858. if (cmd->data_length < 8) {
  3859. pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
  3860. " too small\n", cmd->data_length);
  3861. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3862. return -EINVAL;
  3863. }
  3864. buf = transport_kmap_data_sg(cmd);
  3865. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3866. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3867. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3868. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3869. spin_lock(&pr_tmpl->registration_lock);
  3870. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  3871. &pr_tmpl->registration_list, pr_reg_list) {
  3872. se_nacl = pr_reg->pr_reg_nacl;
  3873. se_tpg = pr_reg->pr_reg_nacl->se_tpg;
  3874. add_desc_len = 0;
  3875. atomic_inc(&pr_reg->pr_res_holders);
  3876. smp_mb__after_atomic_inc();
  3877. spin_unlock(&pr_tmpl->registration_lock);
  3878. /*
  3879. * Determine expected length of $FABRIC_MOD specific
  3880. * TransportID full status descriptor..
  3881. */
  3882. exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
  3883. se_tpg, se_nacl, pr_reg, &format_code);
  3884. if ((exp_desc_len + add_len) > cmd->data_length) {
  3885. pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
  3886. " out of buffer: %d\n", cmd->data_length);
  3887. spin_lock(&pr_tmpl->registration_lock);
  3888. atomic_dec(&pr_reg->pr_res_holders);
  3889. smp_mb__after_atomic_dec();
  3890. break;
  3891. }
  3892. /*
  3893. * Set RESERVATION KEY
  3894. */
  3895. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3896. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3897. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3898. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3899. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3900. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3901. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3902. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3903. off += 4; /* Skip Over Reserved area */
  3904. /*
  3905. * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
  3906. */
  3907. if (pr_reg->pr_reg_all_tg_pt)
  3908. buf[off] = 0x02;
  3909. /*
  3910. * The struct se_lun pointer will be present for the
  3911. * reservation holder for PR_HOLDER bit.
  3912. *
  3913. * Also, if this registration is the reservation
  3914. * holder, fill in SCOPE and TYPE in the next byte.
  3915. */
  3916. if (pr_reg->pr_res_holder) {
  3917. buf[off++] |= 0x01;
  3918. buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
  3919. (pr_reg->pr_res_type & 0x0f);
  3920. } else
  3921. off += 2;
  3922. off += 4; /* Skip over reserved area */
  3923. /*
  3924. * From spc4r17 6.3.15:
  3925. *
  3926. * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
  3927. * IDENTIFIER field contains the relative port identifier (see
  3928. * 3.1.120) of the target port that is part of the I_T nexus
  3929. * described by this full status descriptor. If the ALL_TG_PT
  3930. * bit is set to one, the contents of the RELATIVE TARGET PORT
  3931. * IDENTIFIER field are not defined by this standard.
  3932. */
  3933. if (!pr_reg->pr_reg_all_tg_pt) {
  3934. struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
  3935. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  3936. buf[off++] = (port->sep_rtpi & 0xff);
  3937. } else
  3938. off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFER */
  3939. /*
  3940. * Now, have the $FABRIC_MOD fill in the protocol identifier
  3941. */
  3942. desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
  3943. se_nacl, pr_reg, &format_code, &buf[off+4]);
  3944. spin_lock(&pr_tmpl->registration_lock);
  3945. atomic_dec(&pr_reg->pr_res_holders);
  3946. smp_mb__after_atomic_dec();
  3947. /*
  3948. * Set the ADDITIONAL DESCRIPTOR LENGTH
  3949. */
  3950. buf[off++] = ((desc_len >> 24) & 0xff);
  3951. buf[off++] = ((desc_len >> 16) & 0xff);
  3952. buf[off++] = ((desc_len >> 8) & 0xff);
  3953. buf[off++] = (desc_len & 0xff);
  3954. /*
  3955. * Size of full desctipor header minus TransportID
  3956. * containing $FABRIC_MOD specific) initiator device/port
  3957. * WWN information.
  3958. *
  3959. * See spc4r17 Section 6.13.5 Table 169
  3960. */
  3961. add_desc_len = (24 + desc_len);
  3962. off += desc_len;
  3963. add_len += add_desc_len;
  3964. }
  3965. spin_unlock(&pr_tmpl->registration_lock);
  3966. /*
  3967. * Set ADDITIONAL_LENGTH
  3968. */
  3969. buf[4] = ((add_len >> 24) & 0xff);
  3970. buf[5] = ((add_len >> 16) & 0xff);
  3971. buf[6] = ((add_len >> 8) & 0xff);
  3972. buf[7] = (add_len & 0xff);
  3973. transport_kunmap_data_sg(cmd);
  3974. return 0;
  3975. }
  3976. int target_scsi3_emulate_pr_in(struct se_task *task)
  3977. {
  3978. struct se_cmd *cmd = task->task_se_cmd;
  3979. int ret;
  3980. /*
  3981. * Following spc2r20 5.5.1 Reservations overview:
  3982. *
  3983. * If a logical unit has been reserved by any RESERVE command and is
  3984. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3985. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3986. * initiator or service action and shall terminate with a RESERVATION
  3987. * CONFLICT status.
  3988. */
  3989. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
  3990. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3991. " SPC-2 reservation is held, returning"
  3992. " RESERVATION_CONFLICT\n");
  3993. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3994. return -EINVAL;
  3995. }
  3996. switch (cmd->t_task_cdb[1] & 0x1f) {
  3997. case PRI_READ_KEYS:
  3998. ret = core_scsi3_pri_read_keys(cmd);
  3999. break;
  4000. case PRI_READ_RESERVATION:
  4001. ret = core_scsi3_pri_read_reservation(cmd);
  4002. break;
  4003. case PRI_REPORT_CAPABILITIES:
  4004. ret = core_scsi3_pri_report_capabilities(cmd);
  4005. break;
  4006. case PRI_READ_FULL_STATUS:
  4007. ret = core_scsi3_pri_read_full_status(cmd);
  4008. break;
  4009. default:
  4010. pr_err("Unknown PERSISTENT_RESERVE_IN service"
  4011. " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
  4012. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  4013. ret = -EINVAL;
  4014. break;
  4015. }
  4016. if (!ret) {
  4017. task->task_scsi_status = GOOD;
  4018. transport_complete_task(task, 1);
  4019. }
  4020. return ret;
  4021. }
  4022. static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
  4023. {
  4024. return 0;
  4025. }
  4026. static int core_pt_seq_non_holder(
  4027. struct se_cmd *cmd,
  4028. unsigned char *cdb,
  4029. u32 pr_reg_type)
  4030. {
  4031. return 0;
  4032. }
  4033. int core_setup_reservations(struct se_device *dev, int force_pt)
  4034. {
  4035. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  4036. struct t10_reservation *rest = &su_dev->t10_pr;
  4037. /*
  4038. * If this device is from Target_Core_Mod/pSCSI, use the reservations
  4039. * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
  4040. * cause a problem because libata and some SATA RAID HBAs appear
  4041. * under Linux/SCSI, but to emulate reservations themselves.
  4042. */
  4043. if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  4044. !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
  4045. rest->res_type = SPC_PASSTHROUGH;
  4046. rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
  4047. rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
  4048. pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
  4049. " emulation\n", dev->transport->name);
  4050. return 0;
  4051. }
  4052. /*
  4053. * If SPC-3 or above is reported by real or emulated struct se_device,
  4054. * use emulated Persistent Reservations.
  4055. */
  4056. if (dev->transport->get_device_rev(dev) >= SCSI_3) {
  4057. rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
  4058. rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
  4059. rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
  4060. pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
  4061. " emulation\n", dev->transport->name);
  4062. } else {
  4063. rest->res_type = SPC2_RESERVATIONS;
  4064. rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
  4065. rest->pr_ops.t10_seq_non_holder =
  4066. &core_scsi2_reservation_seq_non_holder;
  4067. pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
  4068. dev->transport->name);
  4069. }
  4070. return 0;
  4071. }