target_core_pr.c 125 KB

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