target_core_pr.c 126 KB

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