target_core_pr.c 124 KB

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