target_core_pr.c 124 KB

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