target_core_pr.c 123 KB

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