target_core_pr.c 125 KB

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