target_core_pr.c 124 KB

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