target_core_pr.c 131 KB

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