target_core_pr.c 124 KB

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