target_core_pr.c 122 KB

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