target_core_pr.c 125 KB

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