target_core_pr.c 125 KB

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