target_core_pr.c 125 KB

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