target_core_pr.c 132 KB

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