target_core_pr.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405
  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_backend.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_configfs.h>
  37. #include "target_core_internal.h"
  38. #include "target_core_pr.h"
  39. #include "target_core_ua.h"
  40. /*
  41. * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
  42. */
  43. struct pr_transport_id_holder {
  44. int dest_local_nexus;
  45. struct t10_pr_registration *dest_pr_reg;
  46. struct se_portal_group *dest_tpg;
  47. struct se_node_acl *dest_node_acl;
  48. struct se_dev_entry *dest_se_deve;
  49. struct list_head dest_list;
  50. };
  51. int core_pr_dump_initiator_port(
  52. struct t10_pr_registration *pr_reg,
  53. char *buf,
  54. u32 size)
  55. {
  56. if (!pr_reg->isid_present_at_reg)
  57. return 0;
  58. snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
  59. return 1;
  60. }
  61. static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
  62. struct t10_pr_registration *, int);
  63. static int core_scsi2_reservation_seq_non_holder(
  64. struct se_cmd *cmd,
  65. unsigned char *cdb,
  66. u32 pr_reg_type)
  67. {
  68. switch (cdb[0]) {
  69. case INQUIRY:
  70. case RELEASE:
  71. case RELEASE_10:
  72. return 0;
  73. default:
  74. return 1;
  75. }
  76. return 1;
  77. }
  78. static int core_scsi2_reservation_check(struct se_cmd *cmd, u32 *pr_reg_type)
  79. {
  80. struct se_device *dev = cmd->se_dev;
  81. struct se_session *sess = cmd->se_sess;
  82. int ret;
  83. if (!sess)
  84. return 0;
  85. spin_lock(&dev->dev_reservation_lock);
  86. if (!dev->dev_reserved_node_acl || !sess) {
  87. spin_unlock(&dev->dev_reservation_lock);
  88. return 0;
  89. }
  90. if (dev->dev_reserved_node_acl != sess->se_node_acl) {
  91. spin_unlock(&dev->dev_reservation_lock);
  92. return -EINVAL;
  93. }
  94. if (!(dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID)) {
  95. spin_unlock(&dev->dev_reservation_lock);
  96. return 0;
  97. }
  98. ret = (dev->dev_res_bin_isid == sess->sess_bin_isid) ? 0 : -EINVAL;
  99. spin_unlock(&dev->dev_reservation_lock);
  100. return ret;
  101. }
  102. static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
  103. struct se_node_acl *, struct se_session *);
  104. static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
  105. static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
  106. {
  107. struct se_session *se_sess = cmd->se_sess;
  108. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  109. struct t10_pr_registration *pr_reg;
  110. struct t10_reservation *pr_tmpl = &su_dev->t10_pr;
  111. int crh = (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS);
  112. int conflict = 0;
  113. if (!crh)
  114. return -EINVAL;
  115. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  116. se_sess);
  117. if (pr_reg) {
  118. /*
  119. * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
  120. * behavior
  121. *
  122. * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
  123. * status, but no reservation shall be established and the
  124. * persistent reservation shall not be changed, if the command
  125. * is received from a) and b) below.
  126. *
  127. * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
  128. * status, but the persistent reservation shall not be released,
  129. * if the command is received from a) and b)
  130. *
  131. * a) An I_T nexus that is a persistent reservation holder; or
  132. * b) An I_T nexus that is registered if a registrants only or
  133. * all registrants type persistent reservation is present.
  134. *
  135. * In all other cases, a RESERVE(6) command, RESERVE(10) command,
  136. * RELEASE(6) command, or RELEASE(10) command shall be processed
  137. * as defined in SPC-2.
  138. */
  139. if (pr_reg->pr_res_holder) {
  140. core_scsi3_put_pr_reg(pr_reg);
  141. return 1;
  142. }
  143. if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
  144. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
  145. (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  146. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  147. core_scsi3_put_pr_reg(pr_reg);
  148. return 1;
  149. }
  150. core_scsi3_put_pr_reg(pr_reg);
  151. conflict = 1;
  152. } else {
  153. /*
  154. * Following spc2r20 5.5.1 Reservations overview:
  155. *
  156. * If a logical unit has executed a PERSISTENT RESERVE OUT
  157. * command with the REGISTER or the REGISTER AND IGNORE
  158. * EXISTING KEY service action and is still registered by any
  159. * initiator, all RESERVE commands and all RELEASE commands
  160. * regardless of initiator shall conflict and shall terminate
  161. * with a RESERVATION CONFLICT status.
  162. */
  163. spin_lock(&pr_tmpl->registration_lock);
  164. conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
  165. spin_unlock(&pr_tmpl->registration_lock);
  166. }
  167. if (conflict) {
  168. pr_err("Received legacy SPC-2 RESERVE/RELEASE"
  169. " while active SPC-3 registrations exist,"
  170. " returning RESERVATION_CONFLICT\n");
  171. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  172. return -EBUSY;
  173. }
  174. return 0;
  175. }
  176. int target_scsi2_reservation_release(struct se_cmd *cmd)
  177. {
  178. struct se_device *dev = cmd->se_dev;
  179. struct se_session *sess = cmd->se_sess;
  180. struct se_portal_group *tpg;
  181. int ret = 0, rc;
  182. if (!sess || !sess->se_tpg)
  183. goto out;
  184. rc = target_check_scsi2_reservation_conflict(cmd);
  185. if (rc == 1)
  186. goto out;
  187. else if (rc < 0) {
  188. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  189. ret = -EINVAL;
  190. goto out;
  191. }
  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. if (dev->dev_res_bin_isid != sess->sess_bin_isid)
  199. goto out_unlock;
  200. dev->dev_reserved_node_acl = NULL;
  201. dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
  202. if (dev->dev_flags & DF_SPC2_RESERVATIONS_WITH_ISID) {
  203. dev->dev_res_bin_isid = 0;
  204. dev->dev_flags &= ~DF_SPC2_RESERVATIONS_WITH_ISID;
  205. }
  206. tpg = sess->se_tpg;
  207. pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
  208. " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  209. cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
  210. sess->se_node_acl->initiatorname);
  211. out_unlock:
  212. spin_unlock(&dev->dev_reservation_lock);
  213. out:
  214. if (!ret)
  215. target_complete_cmd(cmd, GOOD);
  216. return ret;
  217. }
  218. int target_scsi2_reservation_reserve(struct se_cmd *cmd)
  219. {
  220. struct se_device *dev = cmd->se_dev;
  221. struct se_session *sess = cmd->se_sess;
  222. struct se_portal_group *tpg;
  223. int ret = 0, rc;
  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 || !sess->se_tpg)
  237. goto out;
  238. rc = target_check_scsi2_reservation_conflict(cmd);
  239. if (rc == 1)
  240. goto out;
  241. else if (rc < 0) {
  242. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  243. ret = -EINVAL;
  244. goto out;
  245. }
  246. ret = 0;
  247. tpg = sess->se_tpg;
  248. spin_lock(&dev->dev_reservation_lock);
  249. if (dev->dev_reserved_node_acl &&
  250. (dev->dev_reserved_node_acl != sess->se_node_acl)) {
  251. pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
  252. tpg->se_tpg_tfo->get_fabric_name());
  253. pr_err("Original reserver LUN: %u %s\n",
  254. cmd->se_lun->unpacked_lun,
  255. dev->dev_reserved_node_acl->initiatorname);
  256. pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
  257. " from %s \n", cmd->se_lun->unpacked_lun,
  258. cmd->se_deve->mapped_lun,
  259. sess->se_node_acl->initiatorname);
  260. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  261. ret = -EINVAL;
  262. goto out_unlock;
  263. }
  264. dev->dev_reserved_node_acl = sess->se_node_acl;
  265. dev->dev_flags |= DF_SPC2_RESERVATIONS;
  266. if (sess->sess_bin_isid != 0) {
  267. dev->dev_res_bin_isid = sess->sess_bin_isid;
  268. dev->dev_flags |= DF_SPC2_RESERVATIONS_WITH_ISID;
  269. }
  270. pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
  271. " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  272. cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
  273. sess->se_node_acl->initiatorname);
  274. out_unlock:
  275. spin_unlock(&dev->dev_reservation_lock);
  276. out:
  277. if (!ret)
  278. target_complete_cmd(cmd, GOOD);
  279. return ret;
  280. }
  281. /*
  282. * Begin SPC-3/SPC-4 Persistent Reservations emulation support
  283. *
  284. * This function is called by those initiator ports who are *NOT*
  285. * the active PR reservation holder when a reservation is present.
  286. */
  287. static int core_scsi3_pr_seq_non_holder(
  288. struct se_cmd *cmd,
  289. unsigned char *cdb,
  290. u32 pr_reg_type)
  291. {
  292. struct se_dev_entry *se_deve;
  293. struct se_session *se_sess = cmd->se_sess;
  294. int other_cdb = 0, ignore_reg;
  295. int registered_nexus = 0, ret = 1; /* Conflict by default */
  296. int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
  297. int we = 0; /* Write Exclusive */
  298. int legacy = 0; /* Act like a legacy device and return
  299. * RESERVATION CONFLICT on some CDBs */
  300. /*
  301. * A legacy SPC-2 reservation is being held.
  302. */
  303. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS)
  304. return core_scsi2_reservation_seq_non_holder(cmd,
  305. cdb, pr_reg_type);
  306. se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  307. /*
  308. * Determine if the registration should be ignored due to
  309. * non-matching ISIDs in core_scsi3_pr_reservation_check().
  310. */
  311. ignore_reg = (pr_reg_type & 0x80000000);
  312. if (ignore_reg)
  313. pr_reg_type &= ~0x80000000;
  314. switch (pr_reg_type) {
  315. case PR_TYPE_WRITE_EXCLUSIVE:
  316. we = 1;
  317. case PR_TYPE_EXCLUSIVE_ACCESS:
  318. /*
  319. * Some commands are only allowed for the persistent reservation
  320. * holder.
  321. */
  322. if ((se_deve->def_pr_registered) && !(ignore_reg))
  323. registered_nexus = 1;
  324. break;
  325. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  326. we = 1;
  327. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  328. /*
  329. * Some commands are only allowed for registered I_T Nexuses.
  330. */
  331. reg_only = 1;
  332. if ((se_deve->def_pr_registered) && !(ignore_reg))
  333. registered_nexus = 1;
  334. break;
  335. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  336. we = 1;
  337. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  338. /*
  339. * Each registered I_T Nexus is a reservation holder.
  340. */
  341. all_reg = 1;
  342. if ((se_deve->def_pr_registered) && !(ignore_reg))
  343. registered_nexus = 1;
  344. break;
  345. default:
  346. return -EINVAL;
  347. }
  348. /*
  349. * Referenced from spc4r17 table 45 for *NON* PR holder access
  350. */
  351. switch (cdb[0]) {
  352. case SECURITY_PROTOCOL_IN:
  353. if (registered_nexus)
  354. return 0;
  355. ret = (we) ? 0 : 1;
  356. break;
  357. case MODE_SENSE:
  358. case MODE_SENSE_10:
  359. case READ_ATTRIBUTE:
  360. case READ_BUFFER:
  361. case RECEIVE_DIAGNOSTIC:
  362. if (legacy) {
  363. ret = 1;
  364. break;
  365. }
  366. if (registered_nexus) {
  367. ret = 0;
  368. break;
  369. }
  370. ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
  371. break;
  372. case PERSISTENT_RESERVE_OUT:
  373. /*
  374. * This follows PERSISTENT_RESERVE_OUT service actions that
  375. * are allowed in the presence of various reservations.
  376. * See spc4r17, table 46
  377. */
  378. switch (cdb[1] & 0x1f) {
  379. case PRO_CLEAR:
  380. case PRO_PREEMPT:
  381. case PRO_PREEMPT_AND_ABORT:
  382. ret = (registered_nexus) ? 0 : 1;
  383. break;
  384. case PRO_REGISTER:
  385. case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
  386. ret = 0;
  387. break;
  388. case PRO_REGISTER_AND_MOVE:
  389. case PRO_RESERVE:
  390. ret = 1;
  391. break;
  392. case PRO_RELEASE:
  393. ret = (registered_nexus) ? 0 : 1;
  394. break;
  395. default:
  396. pr_err("Unknown PERSISTENT_RESERVE_OUT service"
  397. " action: 0x%02x\n", cdb[1] & 0x1f);
  398. return -EINVAL;
  399. }
  400. break;
  401. case RELEASE:
  402. case RELEASE_10:
  403. /* Handled by CRH=1 in target_scsi2_reservation_release() */
  404. ret = 0;
  405. break;
  406. case RESERVE:
  407. case RESERVE_10:
  408. /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
  409. ret = 0;
  410. break;
  411. case TEST_UNIT_READY:
  412. ret = (legacy) ? 1 : 0; /* Conflict for legacy */
  413. break;
  414. case MAINTENANCE_IN:
  415. switch (cdb[1] & 0x1f) {
  416. case MI_MANAGEMENT_PROTOCOL_IN:
  417. if (registered_nexus) {
  418. ret = 0;
  419. break;
  420. }
  421. ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
  422. break;
  423. case MI_REPORT_SUPPORTED_OPERATION_CODES:
  424. case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
  425. if (legacy) {
  426. ret = 1;
  427. break;
  428. }
  429. if (registered_nexus) {
  430. ret = 0;
  431. break;
  432. }
  433. ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
  434. break;
  435. case MI_REPORT_ALIASES:
  436. case MI_REPORT_IDENTIFYING_INFORMATION:
  437. case MI_REPORT_PRIORITY:
  438. case MI_REPORT_TARGET_PGS:
  439. case MI_REPORT_TIMESTAMP:
  440. ret = 0; /* Allowed */
  441. break;
  442. default:
  443. pr_err("Unknown MI Service Action: 0x%02x\n",
  444. (cdb[1] & 0x1f));
  445. return -EINVAL;
  446. }
  447. break;
  448. case ACCESS_CONTROL_IN:
  449. case ACCESS_CONTROL_OUT:
  450. case INQUIRY:
  451. case LOG_SENSE:
  452. case READ_MEDIA_SERIAL_NUMBER:
  453. case REPORT_LUNS:
  454. case REQUEST_SENSE:
  455. case PERSISTENT_RESERVE_IN:
  456. ret = 0; /*/ Allowed CDBs */
  457. break;
  458. default:
  459. other_cdb = 1;
  460. break;
  461. }
  462. /*
  463. * Case where the CDB is explicitly allowed in the above switch
  464. * statement.
  465. */
  466. if (!ret && !other_cdb) {
  467. pr_debug("Allowing explict CDB: 0x%02x for %s"
  468. " reservation holder\n", cdb[0],
  469. core_scsi3_pr_dump_type(pr_reg_type));
  470. return ret;
  471. }
  472. /*
  473. * Check if write exclusive initiator ports *NOT* holding the
  474. * WRITE_EXCLUSIVE_* reservation.
  475. */
  476. if (we && !registered_nexus) {
  477. if (cmd->data_direction == DMA_TO_DEVICE) {
  478. /*
  479. * Conflict for write exclusive
  480. */
  481. pr_debug("%s Conflict for unregistered nexus"
  482. " %s CDB: 0x%02x to %s reservation\n",
  483. transport_dump_cmd_direction(cmd),
  484. se_sess->se_node_acl->initiatorname, cdb[0],
  485. core_scsi3_pr_dump_type(pr_reg_type));
  486. return 1;
  487. } else {
  488. /*
  489. * Allow non WRITE CDBs for all Write Exclusive
  490. * PR TYPEs to pass for registered and
  491. * non-registered_nexuxes NOT holding the reservation.
  492. *
  493. * We only make noise for the unregisterd nexuses,
  494. * as we expect registered non-reservation holding
  495. * nexuses to issue CDBs.
  496. */
  497. if (!registered_nexus) {
  498. pr_debug("Allowing implict CDB: 0x%02x"
  499. " for %s reservation on unregistered"
  500. " nexus\n", cdb[0],
  501. core_scsi3_pr_dump_type(pr_reg_type));
  502. }
  503. return 0;
  504. }
  505. } else if ((reg_only) || (all_reg)) {
  506. if (registered_nexus) {
  507. /*
  508. * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
  509. * allow commands from registered nexuses.
  510. */
  511. pr_debug("Allowing implict CDB: 0x%02x for %s"
  512. " reservation\n", cdb[0],
  513. core_scsi3_pr_dump_type(pr_reg_type));
  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. 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. local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  1375. /*
  1376. * Allocate a struct pr_transport_id_holder and setup the
  1377. * local_node_acl and local_se_deve pointers and add to
  1378. * struct list_head tid_dest_list for add registration
  1379. * processing in the loop of tid_dest_list below.
  1380. */
  1381. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
  1382. if (!tidh_new) {
  1383. pr_err("Unable to allocate tidh_new\n");
  1384. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1385. return -EINVAL;
  1386. }
  1387. INIT_LIST_HEAD(&tidh_new->dest_list);
  1388. tidh_new->dest_tpg = tpg;
  1389. tidh_new->dest_node_acl = se_sess->se_node_acl;
  1390. tidh_new->dest_se_deve = local_se_deve;
  1391. local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1392. se_sess->se_node_acl, local_se_deve, l_isid,
  1393. sa_res_key, all_tg_pt, aptpl);
  1394. if (!local_pr_reg) {
  1395. kfree(tidh_new);
  1396. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1397. return -ENOMEM;
  1398. }
  1399. tidh_new->dest_pr_reg = local_pr_reg;
  1400. /*
  1401. * The local I_T nexus does not hold any configfs dependances,
  1402. * so we set tid_h->dest_local_nexus=1 to prevent the
  1403. * configfs_undepend_item() calls in the tid_dest_list loops below.
  1404. */
  1405. tidh_new->dest_local_nexus = 1;
  1406. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1407. if (cmd->data_length < 28) {
  1408. pr_warn("SPC-PR: Received PR OUT parameter list"
  1409. " length too small: %u\n", cmd->data_length);
  1410. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1411. ret = -EINVAL;
  1412. goto out;
  1413. }
  1414. buf = transport_kmap_data_sg(cmd);
  1415. /*
  1416. * For a PERSISTENT RESERVE OUT specify initiator ports payload,
  1417. * first extract TransportID Parameter Data Length, and make sure
  1418. * the value matches up to the SCSI expected data transfer length.
  1419. */
  1420. tpdl = (buf[24] & 0xff) << 24;
  1421. tpdl |= (buf[25] & 0xff) << 16;
  1422. tpdl |= (buf[26] & 0xff) << 8;
  1423. tpdl |= buf[27] & 0xff;
  1424. if ((tpdl + 28) != cmd->data_length) {
  1425. pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
  1426. " does not equal CDB data_length: %u\n", tpdl,
  1427. cmd->data_length);
  1428. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1429. ret = -EINVAL;
  1430. goto out;
  1431. }
  1432. /*
  1433. * Start processing the received transport IDs using the
  1434. * receiving I_T Nexus portal's fabric dependent methods to
  1435. * obtain the SCSI Initiator Port/Device Identifiers.
  1436. */
  1437. ptr = &buf[28];
  1438. while (tpdl > 0) {
  1439. proto_ident = (ptr[0] & 0x0f);
  1440. dest_tpg = NULL;
  1441. spin_lock(&dev->se_port_lock);
  1442. list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
  1443. tmp_tpg = tmp_port->sep_tpg;
  1444. if (!tmp_tpg)
  1445. continue;
  1446. tmp_tf_ops = tmp_tpg->se_tpg_tfo;
  1447. if (!tmp_tf_ops)
  1448. continue;
  1449. if (!tmp_tf_ops->get_fabric_proto_ident ||
  1450. !tmp_tf_ops->tpg_parse_pr_out_transport_id)
  1451. continue;
  1452. /*
  1453. * Look for the matching proto_ident provided by
  1454. * the received TransportID
  1455. */
  1456. tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
  1457. if (tmp_proto_ident != proto_ident)
  1458. continue;
  1459. dest_rtpi = tmp_port->sep_rtpi;
  1460. i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
  1461. tmp_tpg, (const char *)ptr, &tid_len,
  1462. &iport_ptr);
  1463. if (!i_str)
  1464. continue;
  1465. atomic_inc(&tmp_tpg->tpg_pr_ref_count);
  1466. smp_mb__after_atomic_inc();
  1467. spin_unlock(&dev->se_port_lock);
  1468. ret = core_scsi3_tpg_depend_item(tmp_tpg);
  1469. if (ret != 0) {
  1470. pr_err(" core_scsi3_tpg_depend_item()"
  1471. " for tmp_tpg\n");
  1472. atomic_dec(&tmp_tpg->tpg_pr_ref_count);
  1473. smp_mb__after_atomic_dec();
  1474. cmd->scsi_sense_reason =
  1475. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1476. ret = -EINVAL;
  1477. goto out;
  1478. }
  1479. /*
  1480. * Locate the destination initiator ACL to be registered
  1481. * from the decoded fabric module specific TransportID
  1482. * at *i_str.
  1483. */
  1484. spin_lock_irq(&tmp_tpg->acl_node_lock);
  1485. dest_node_acl = __core_tpg_get_initiator_node_acl(
  1486. tmp_tpg, i_str);
  1487. if (dest_node_acl) {
  1488. atomic_inc(&dest_node_acl->acl_pr_ref_count);
  1489. smp_mb__after_atomic_inc();
  1490. }
  1491. spin_unlock_irq(&tmp_tpg->acl_node_lock);
  1492. if (!dest_node_acl) {
  1493. core_scsi3_tpg_undepend_item(tmp_tpg);
  1494. spin_lock(&dev->se_port_lock);
  1495. continue;
  1496. }
  1497. ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
  1498. if (ret != 0) {
  1499. pr_err("configfs_depend_item() failed"
  1500. " for dest_node_acl->acl_group\n");
  1501. atomic_dec(&dest_node_acl->acl_pr_ref_count);
  1502. smp_mb__after_atomic_dec();
  1503. core_scsi3_tpg_undepend_item(tmp_tpg);
  1504. cmd->scsi_sense_reason =
  1505. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1506. ret = -EINVAL;
  1507. goto out;
  1508. }
  1509. dest_tpg = tmp_tpg;
  1510. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
  1511. " %s Port RTPI: %hu\n",
  1512. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1513. dest_node_acl->initiatorname, dest_rtpi);
  1514. spin_lock(&dev->se_port_lock);
  1515. break;
  1516. }
  1517. spin_unlock(&dev->se_port_lock);
  1518. if (!dest_tpg) {
  1519. pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
  1520. " dest_tpg\n");
  1521. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1522. ret = -EINVAL;
  1523. goto out;
  1524. }
  1525. pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
  1526. " tid_len: %d for %s + %s\n",
  1527. dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
  1528. tpdl, tid_len, i_str, iport_ptr);
  1529. if (tid_len > tpdl) {
  1530. pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
  1531. " %u for Transport ID: %s\n", tid_len, ptr);
  1532. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1533. core_scsi3_tpg_undepend_item(dest_tpg);
  1534. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1535. ret = -EINVAL;
  1536. goto out;
  1537. }
  1538. /*
  1539. * Locate the desintation struct se_dev_entry pointer for matching
  1540. * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
  1541. * Target Port.
  1542. */
  1543. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
  1544. dest_rtpi);
  1545. if (!dest_se_deve) {
  1546. pr_err("Unable to locate %s dest_se_deve"
  1547. " from destination RTPI: %hu\n",
  1548. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1549. dest_rtpi);
  1550. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1551. core_scsi3_tpg_undepend_item(dest_tpg);
  1552. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1553. ret = -EINVAL;
  1554. goto out;
  1555. }
  1556. ret = core_scsi3_lunacl_depend_item(dest_se_deve);
  1557. if (ret < 0) {
  1558. pr_err("core_scsi3_lunacl_depend_item()"
  1559. " failed\n");
  1560. atomic_dec(&dest_se_deve->pr_ref_count);
  1561. smp_mb__after_atomic_dec();
  1562. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1563. core_scsi3_tpg_undepend_item(dest_tpg);
  1564. cmd->scsi_sense_reason =
  1565. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1566. ret = -EINVAL;
  1567. goto out;
  1568. }
  1569. pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
  1570. " dest_se_deve mapped_lun: %u\n",
  1571. dest_tpg->se_tpg_tfo->get_fabric_name(),
  1572. dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
  1573. /*
  1574. * Skip any TransportIDs that already have a registration for
  1575. * this target port.
  1576. */
  1577. pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  1578. iport_ptr);
  1579. if (pr_reg_e) {
  1580. core_scsi3_put_pr_reg(pr_reg_e);
  1581. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1582. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1583. core_scsi3_tpg_undepend_item(dest_tpg);
  1584. ptr += tid_len;
  1585. tpdl -= tid_len;
  1586. tid_len = 0;
  1587. continue;
  1588. }
  1589. /*
  1590. * Allocate a struct pr_transport_id_holder and setup
  1591. * the dest_node_acl and dest_se_deve pointers for the
  1592. * loop below.
  1593. */
  1594. tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
  1595. GFP_KERNEL);
  1596. if (!tidh_new) {
  1597. pr_err("Unable to allocate tidh_new\n");
  1598. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1599. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1600. core_scsi3_tpg_undepend_item(dest_tpg);
  1601. cmd->scsi_sense_reason =
  1602. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1603. ret = -ENOMEM;
  1604. goto out;
  1605. }
  1606. INIT_LIST_HEAD(&tidh_new->dest_list);
  1607. tidh_new->dest_tpg = dest_tpg;
  1608. tidh_new->dest_node_acl = dest_node_acl;
  1609. tidh_new->dest_se_deve = dest_se_deve;
  1610. /*
  1611. * Allocate, but do NOT add the registration for the
  1612. * TransportID referenced SCSI Initiator port. This
  1613. * done because of the following from spc4r17 in section
  1614. * 6.14.3 wrt SPEC_I_PT:
  1615. *
  1616. * "If a registration fails for any initiator port (e.g., if th
  1617. * logical unit does not have enough resources available to
  1618. * hold the registration information), no registrations shall be
  1619. * made, and the command shall be terminated with
  1620. * CHECK CONDITION status."
  1621. *
  1622. * That means we call __core_scsi3_alloc_registration() here,
  1623. * and then call __core_scsi3_add_registration() in the
  1624. * 2nd loop which will never fail.
  1625. */
  1626. dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
  1627. dest_node_acl, dest_se_deve, iport_ptr,
  1628. sa_res_key, all_tg_pt, aptpl);
  1629. if (!dest_pr_reg) {
  1630. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1631. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1632. core_scsi3_tpg_undepend_item(dest_tpg);
  1633. kfree(tidh_new);
  1634. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1635. ret = -EINVAL;
  1636. goto out;
  1637. }
  1638. tidh_new->dest_pr_reg = dest_pr_reg;
  1639. list_add_tail(&tidh_new->dest_list, &tid_dest_list);
  1640. ptr += tid_len;
  1641. tpdl -= tid_len;
  1642. tid_len = 0;
  1643. }
  1644. transport_kunmap_data_sg(cmd);
  1645. /*
  1646. * Go ahead and create a registrations from tid_dest_list for the
  1647. * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
  1648. * and dest_se_deve.
  1649. *
  1650. * The SA Reservation Key from the PROUT is set for the
  1651. * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
  1652. * means that the TransportID Initiator port will be
  1653. * registered on all of the target ports in the SCSI target device
  1654. * ALL_TG_PT=0 means the registration will only be for the
  1655. * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
  1656. * was received.
  1657. */
  1658. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1659. dest_tpg = tidh->dest_tpg;
  1660. dest_node_acl = tidh->dest_node_acl;
  1661. dest_se_deve = tidh->dest_se_deve;
  1662. dest_pr_reg = tidh->dest_pr_reg;
  1663. dest_local_nexus = tidh->dest_local_nexus;
  1664. list_del(&tidh->dest_list);
  1665. kfree(tidh);
  1666. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  1667. prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
  1668. PR_REG_ISID_ID_LEN);
  1669. __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
  1670. dest_pr_reg, 0, 0);
  1671. pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
  1672. " registered Transport ID for Node: %s%s Mapped LUN:"
  1673. " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
  1674. dest_node_acl->initiatorname, (prf_isid) ?
  1675. &i_buf[0] : "", dest_se_deve->mapped_lun);
  1676. if (dest_local_nexus)
  1677. continue;
  1678. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1679. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1680. core_scsi3_tpg_undepend_item(dest_tpg);
  1681. }
  1682. return 0;
  1683. out:
  1684. transport_kunmap_data_sg(cmd);
  1685. /*
  1686. * For the failure case, release everything from tid_dest_list
  1687. * including *dest_pr_reg and the configfs dependances..
  1688. */
  1689. list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
  1690. dest_tpg = tidh->dest_tpg;
  1691. dest_node_acl = tidh->dest_node_acl;
  1692. dest_se_deve = tidh->dest_se_deve;
  1693. dest_pr_reg = tidh->dest_pr_reg;
  1694. dest_local_nexus = tidh->dest_local_nexus;
  1695. list_del(&tidh->dest_list);
  1696. kfree(tidh);
  1697. /*
  1698. * Release any extra ALL_TG_PT=1 registrations for
  1699. * the SPEC_I_PT=1 case.
  1700. */
  1701. list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
  1702. &dest_pr_reg->pr_reg_atp_list,
  1703. pr_reg_atp_mem_list) {
  1704. list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
  1705. core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
  1706. kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
  1707. }
  1708. kfree(dest_pr_reg->pr_aptpl_buf);
  1709. kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
  1710. if (dest_local_nexus)
  1711. continue;
  1712. core_scsi3_lunacl_undepend_item(dest_se_deve);
  1713. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  1714. core_scsi3_tpg_undepend_item(dest_tpg);
  1715. }
  1716. return ret;
  1717. }
  1718. /*
  1719. * Called with struct se_device->dev_reservation_lock held
  1720. */
  1721. static int __core_scsi3_update_aptpl_buf(
  1722. struct se_device *dev,
  1723. unsigned char *buf,
  1724. u32 pr_aptpl_buf_len,
  1725. int clear_aptpl_metadata)
  1726. {
  1727. struct se_lun *lun;
  1728. struct se_portal_group *tpg;
  1729. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1730. struct t10_pr_registration *pr_reg;
  1731. unsigned char tmp[512], isid_buf[32];
  1732. ssize_t len = 0;
  1733. int reg_count = 0;
  1734. memset(buf, 0, pr_aptpl_buf_len);
  1735. /*
  1736. * Called to clear metadata once APTPL has been deactivated.
  1737. */
  1738. if (clear_aptpl_metadata) {
  1739. snprintf(buf, pr_aptpl_buf_len,
  1740. "No Registrations or Reservations\n");
  1741. return 0;
  1742. }
  1743. /*
  1744. * Walk the registration list..
  1745. */
  1746. spin_lock(&su_dev->t10_pr.registration_lock);
  1747. list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
  1748. pr_reg_list) {
  1749. tmp[0] = '\0';
  1750. isid_buf[0] = '\0';
  1751. tpg = pr_reg->pr_reg_nacl->se_tpg;
  1752. lun = pr_reg->pr_reg_tg_pt_lun;
  1753. /*
  1754. * Write out any ISID value to APTPL metadata that was included
  1755. * in the original registration.
  1756. */
  1757. if (pr_reg->isid_present_at_reg)
  1758. snprintf(isid_buf, 32, "initiator_sid=%s\n",
  1759. pr_reg->pr_reg_isid);
  1760. /*
  1761. * Include special metadata if the pr_reg matches the
  1762. * reservation holder.
  1763. */
  1764. if (dev->dev_pr_res_holder == pr_reg) {
  1765. snprintf(tmp, 512, "PR_REG_START: %d"
  1766. "\ninitiator_fabric=%s\n"
  1767. "initiator_node=%s\n%s"
  1768. "sa_res_key=%llu\n"
  1769. "res_holder=1\nres_type=%02x\n"
  1770. "res_scope=%02x\nres_all_tg_pt=%d\n"
  1771. "mapped_lun=%u\n", reg_count,
  1772. tpg->se_tpg_tfo->get_fabric_name(),
  1773. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1774. pr_reg->pr_res_key, pr_reg->pr_res_type,
  1775. pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
  1776. pr_reg->pr_res_mapped_lun);
  1777. } else {
  1778. snprintf(tmp, 512, "PR_REG_START: %d\n"
  1779. "initiator_fabric=%s\ninitiator_node=%s\n%s"
  1780. "sa_res_key=%llu\nres_holder=0\n"
  1781. "res_all_tg_pt=%d\nmapped_lun=%u\n",
  1782. reg_count, tpg->se_tpg_tfo->get_fabric_name(),
  1783. pr_reg->pr_reg_nacl->initiatorname, isid_buf,
  1784. pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
  1785. pr_reg->pr_res_mapped_lun);
  1786. }
  1787. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1788. pr_err("Unable to update renaming"
  1789. " APTPL metadata\n");
  1790. spin_unlock(&su_dev->t10_pr.registration_lock);
  1791. return -EMSGSIZE;
  1792. }
  1793. len += sprintf(buf+len, "%s", tmp);
  1794. /*
  1795. * Include information about the associated SCSI target port.
  1796. */
  1797. snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
  1798. "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
  1799. " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
  1800. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1801. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1802. lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
  1803. if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
  1804. pr_err("Unable to update renaming"
  1805. " APTPL metadata\n");
  1806. spin_unlock(&su_dev->t10_pr.registration_lock);
  1807. return -EMSGSIZE;
  1808. }
  1809. len += sprintf(buf+len, "%s", tmp);
  1810. reg_count++;
  1811. }
  1812. spin_unlock(&su_dev->t10_pr.registration_lock);
  1813. if (!reg_count)
  1814. len += sprintf(buf+len, "No Registrations or Reservations");
  1815. return 0;
  1816. }
  1817. static int core_scsi3_update_aptpl_buf(
  1818. struct se_device *dev,
  1819. unsigned char *buf,
  1820. u32 pr_aptpl_buf_len,
  1821. int clear_aptpl_metadata)
  1822. {
  1823. int ret;
  1824. spin_lock(&dev->dev_reservation_lock);
  1825. ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
  1826. clear_aptpl_metadata);
  1827. spin_unlock(&dev->dev_reservation_lock);
  1828. return ret;
  1829. }
  1830. /*
  1831. * Called with struct se_device->aptpl_file_mutex held
  1832. */
  1833. static int __core_scsi3_write_aptpl_to_file(
  1834. struct se_device *dev,
  1835. unsigned char *buf,
  1836. u32 pr_aptpl_buf_len)
  1837. {
  1838. struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
  1839. struct file *file;
  1840. struct iovec iov[1];
  1841. mm_segment_t old_fs;
  1842. int flags = O_RDWR | O_CREAT | O_TRUNC;
  1843. char path[512];
  1844. int ret;
  1845. memset(iov, 0, sizeof(struct iovec));
  1846. memset(path, 0, 512);
  1847. if (strlen(&wwn->unit_serial[0]) >= 512) {
  1848. pr_err("WWN value for struct se_device does not fit"
  1849. " into path buffer\n");
  1850. return -EMSGSIZE;
  1851. }
  1852. snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
  1853. file = filp_open(path, flags, 0600);
  1854. if (IS_ERR(file) || !file || !file->f_dentry) {
  1855. pr_err("filp_open(%s) for APTPL metadata"
  1856. " failed\n", path);
  1857. return IS_ERR(file) ? PTR_ERR(file) : -ENOENT;
  1858. }
  1859. iov[0].iov_base = &buf[0];
  1860. if (!pr_aptpl_buf_len)
  1861. iov[0].iov_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
  1862. else
  1863. iov[0].iov_len = pr_aptpl_buf_len;
  1864. old_fs = get_fs();
  1865. set_fs(get_ds());
  1866. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  1867. set_fs(old_fs);
  1868. if (ret < 0) {
  1869. pr_debug("Error writing APTPL metadata file: %s\n", path);
  1870. filp_close(file, NULL);
  1871. return -EIO;
  1872. }
  1873. filp_close(file, NULL);
  1874. return 0;
  1875. }
  1876. static int core_scsi3_update_and_write_aptpl(
  1877. struct se_device *dev,
  1878. unsigned char *in_buf,
  1879. u32 in_pr_aptpl_buf_len)
  1880. {
  1881. unsigned char null_buf[64], *buf;
  1882. u32 pr_aptpl_buf_len;
  1883. int ret, clear_aptpl_metadata = 0;
  1884. /*
  1885. * Can be called with a NULL pointer from PROUT service action CLEAR
  1886. */
  1887. if (!in_buf) {
  1888. memset(null_buf, 0, 64);
  1889. buf = &null_buf[0];
  1890. /*
  1891. * This will clear the APTPL metadata to:
  1892. * "No Registrations or Reservations" status
  1893. */
  1894. pr_aptpl_buf_len = 64;
  1895. clear_aptpl_metadata = 1;
  1896. } else {
  1897. buf = in_buf;
  1898. pr_aptpl_buf_len = in_pr_aptpl_buf_len;
  1899. }
  1900. ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
  1901. clear_aptpl_metadata);
  1902. if (ret != 0)
  1903. return ret;
  1904. /*
  1905. * __core_scsi3_write_aptpl_to_file() will call strlen()
  1906. * on the passed buf to determine pr_aptpl_buf_len.
  1907. */
  1908. ret = __core_scsi3_write_aptpl_to_file(dev, buf, 0);
  1909. if (ret != 0)
  1910. return ret;
  1911. return ret;
  1912. }
  1913. static int core_scsi3_emulate_pro_register(
  1914. struct se_cmd *cmd,
  1915. u64 res_key,
  1916. u64 sa_res_key,
  1917. int aptpl,
  1918. int all_tg_pt,
  1919. int spec_i_pt,
  1920. int ignore_key)
  1921. {
  1922. struct se_session *se_sess = cmd->se_sess;
  1923. struct se_device *dev = cmd->se_dev;
  1924. struct se_dev_entry *se_deve;
  1925. struct se_lun *se_lun = cmd->se_lun;
  1926. struct se_portal_group *se_tpg;
  1927. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
  1928. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  1929. /* Used for APTPL metadata w/ UNREGISTER */
  1930. unsigned char *pr_aptpl_buf = NULL;
  1931. unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
  1932. int pr_holder = 0, ret = 0, type;
  1933. if (!se_sess || !se_lun) {
  1934. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  1935. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1936. return -EINVAL;
  1937. }
  1938. se_tpg = se_sess->se_tpg;
  1939. se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
  1940. if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
  1941. memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
  1942. se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
  1943. PR_REG_ISID_LEN);
  1944. isid_ptr = &isid_buf[0];
  1945. }
  1946. /*
  1947. * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
  1948. */
  1949. pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  1950. if (!pr_reg_e) {
  1951. if (res_key) {
  1952. pr_warn("SPC-3 PR: Reservation Key non-zero"
  1953. " for SA REGISTER, returning CONFLICT\n");
  1954. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  1955. return -EINVAL;
  1956. }
  1957. /*
  1958. * Do nothing but return GOOD status.
  1959. */
  1960. if (!sa_res_key)
  1961. return 0;
  1962. if (!spec_i_pt) {
  1963. /*
  1964. * Perform the Service Action REGISTER on the Initiator
  1965. * Port Endpoint that the PRO was received from on the
  1966. * Logical Unit of the SCSI device server.
  1967. */
  1968. ret = core_scsi3_alloc_registration(cmd->se_dev,
  1969. se_sess->se_node_acl, se_deve, isid_ptr,
  1970. sa_res_key, all_tg_pt, aptpl,
  1971. ignore_key, 0);
  1972. if (ret != 0) {
  1973. pr_err("Unable to allocate"
  1974. " struct t10_pr_registration\n");
  1975. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  1976. return -EINVAL;
  1977. }
  1978. } else {
  1979. /*
  1980. * Register both the Initiator port that received
  1981. * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
  1982. * TransportID from Parameter list and loop through
  1983. * fabric dependent parameter list while calling
  1984. * logic from of core_scsi3_alloc_registration() for
  1985. * each TransportID provided SCSI Initiator Port/Device
  1986. */
  1987. ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
  1988. isid_ptr, sa_res_key, all_tg_pt, aptpl);
  1989. if (ret != 0)
  1990. return ret;
  1991. }
  1992. /*
  1993. * Nothing left to do for the APTPL=0 case.
  1994. */
  1995. if (!aptpl) {
  1996. pr_tmpl->pr_aptpl_active = 0;
  1997. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  1998. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
  1999. " REGISTER\n");
  2000. return 0;
  2001. }
  2002. /*
  2003. * Locate the newly allocated local I_T Nexus *pr_reg, and
  2004. * update the APTPL metadata information using its
  2005. * preallocated *pr_reg->pr_aptpl_buf.
  2006. */
  2007. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
  2008. se_sess->se_node_acl, se_sess);
  2009. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2010. &pr_reg->pr_aptpl_buf[0],
  2011. pr_tmpl->pr_aptpl_buf_len);
  2012. if (!ret) {
  2013. pr_tmpl->pr_aptpl_active = 1;
  2014. pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
  2015. }
  2016. core_scsi3_put_pr_reg(pr_reg);
  2017. return ret;
  2018. } else {
  2019. /*
  2020. * Locate the existing *pr_reg via struct se_node_acl pointers
  2021. */
  2022. pr_reg = pr_reg_e;
  2023. type = pr_reg->pr_res_type;
  2024. if (!ignore_key) {
  2025. if (res_key != pr_reg->pr_res_key) {
  2026. pr_err("SPC-3 PR REGISTER: Received"
  2027. " res_key: 0x%016Lx does not match"
  2028. " existing SA REGISTER res_key:"
  2029. " 0x%016Lx\n", res_key,
  2030. pr_reg->pr_res_key);
  2031. core_scsi3_put_pr_reg(pr_reg);
  2032. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2033. return -EINVAL;
  2034. }
  2035. }
  2036. if (spec_i_pt) {
  2037. pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
  2038. " set while sa_res_key=0\n");
  2039. core_scsi3_put_pr_reg(pr_reg);
  2040. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2041. return -EINVAL;
  2042. }
  2043. /*
  2044. * An existing ALL_TG_PT=1 registration being released
  2045. * must also set ALL_TG_PT=1 in the incoming PROUT.
  2046. */
  2047. if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
  2048. pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
  2049. " registration exists, but ALL_TG_PT=1 bit not"
  2050. " present in received PROUT\n");
  2051. core_scsi3_put_pr_reg(pr_reg);
  2052. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2053. return -EINVAL;
  2054. }
  2055. /*
  2056. * Allocate APTPL metadata buffer used for UNREGISTER ops
  2057. */
  2058. if (aptpl) {
  2059. pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
  2060. GFP_KERNEL);
  2061. if (!pr_aptpl_buf) {
  2062. pr_err("Unable to allocate"
  2063. " pr_aptpl_buf\n");
  2064. core_scsi3_put_pr_reg(pr_reg);
  2065. cmd->scsi_sense_reason =
  2066. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2067. return -EINVAL;
  2068. }
  2069. }
  2070. /*
  2071. * sa_res_key=0 Unregister Reservation Key for registered I_T
  2072. * Nexus sa_res_key=1 Change Reservation Key for registered I_T
  2073. * Nexus.
  2074. */
  2075. if (!sa_res_key) {
  2076. pr_holder = core_scsi3_check_implict_release(
  2077. cmd->se_dev, pr_reg);
  2078. if (pr_holder < 0) {
  2079. kfree(pr_aptpl_buf);
  2080. core_scsi3_put_pr_reg(pr_reg);
  2081. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2082. return -EINVAL;
  2083. }
  2084. spin_lock(&pr_tmpl->registration_lock);
  2085. /*
  2086. * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
  2087. * and matching pr_res_key.
  2088. */
  2089. if (pr_reg->pr_reg_all_tg_pt) {
  2090. list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
  2091. &pr_tmpl->registration_list,
  2092. pr_reg_list) {
  2093. if (!pr_reg_p->pr_reg_all_tg_pt)
  2094. continue;
  2095. if (pr_reg_p->pr_res_key != res_key)
  2096. continue;
  2097. if (pr_reg == pr_reg_p)
  2098. continue;
  2099. if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
  2100. pr_reg_p->pr_reg_nacl->initiatorname))
  2101. continue;
  2102. __core_scsi3_free_registration(dev,
  2103. pr_reg_p, NULL, 0);
  2104. }
  2105. }
  2106. /*
  2107. * Release the calling I_T Nexus registration now..
  2108. */
  2109. __core_scsi3_free_registration(cmd->se_dev, pr_reg,
  2110. NULL, 1);
  2111. /*
  2112. * From spc4r17, section 5.7.11.3 Unregistering
  2113. *
  2114. * If the persistent reservation is a registrants only
  2115. * type, the device server shall establish a unit
  2116. * attention condition for the initiator port associated
  2117. * with every registered I_T nexus except for the I_T
  2118. * nexus on which the PERSISTENT RESERVE OUT command was
  2119. * received, with the additional sense code set to
  2120. * RESERVATIONS RELEASED.
  2121. */
  2122. if (pr_holder &&
  2123. ((type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
  2124. (type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY))) {
  2125. list_for_each_entry(pr_reg_p,
  2126. &pr_tmpl->registration_list,
  2127. pr_reg_list) {
  2128. core_scsi3_ua_allocate(
  2129. pr_reg_p->pr_reg_nacl,
  2130. pr_reg_p->pr_res_mapped_lun,
  2131. 0x2A,
  2132. ASCQ_2AH_RESERVATIONS_RELEASED);
  2133. }
  2134. }
  2135. spin_unlock(&pr_tmpl->registration_lock);
  2136. if (!aptpl) {
  2137. pr_tmpl->pr_aptpl_active = 0;
  2138. core_scsi3_update_and_write_aptpl(dev, NULL, 0);
  2139. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
  2140. " for UNREGISTER\n");
  2141. return 0;
  2142. }
  2143. ret = core_scsi3_update_and_write_aptpl(dev,
  2144. &pr_aptpl_buf[0],
  2145. pr_tmpl->pr_aptpl_buf_len);
  2146. if (!ret) {
  2147. pr_tmpl->pr_aptpl_active = 1;
  2148. pr_debug("SPC-3 PR: Set APTPL Bit Activated"
  2149. " for UNREGISTER\n");
  2150. }
  2151. kfree(pr_aptpl_buf);
  2152. return ret;
  2153. } else {
  2154. /*
  2155. * Increment PRgeneration counter for struct se_device"
  2156. * upon a successful REGISTER, see spc4r17 section 6.3.2
  2157. * READ_KEYS service action.
  2158. */
  2159. pr_reg->pr_res_generation = core_scsi3_pr_generation(
  2160. cmd->se_dev);
  2161. pr_reg->pr_res_key = sa_res_key;
  2162. pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
  2163. " Key for %s to: 0x%016Lx PRgeneration:"
  2164. " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
  2165. (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
  2166. pr_reg->pr_reg_nacl->initiatorname,
  2167. pr_reg->pr_res_key, pr_reg->pr_res_generation);
  2168. if (!aptpl) {
  2169. pr_tmpl->pr_aptpl_active = 0;
  2170. core_scsi3_update_and_write_aptpl(dev, NULL, 0);
  2171. core_scsi3_put_pr_reg(pr_reg);
  2172. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
  2173. " for REGISTER\n");
  2174. return 0;
  2175. }
  2176. ret = core_scsi3_update_and_write_aptpl(dev,
  2177. &pr_aptpl_buf[0],
  2178. pr_tmpl->pr_aptpl_buf_len);
  2179. if (!ret) {
  2180. pr_tmpl->pr_aptpl_active = 1;
  2181. pr_debug("SPC-3 PR: Set APTPL Bit Activated"
  2182. " for REGISTER\n");
  2183. }
  2184. kfree(pr_aptpl_buf);
  2185. core_scsi3_put_pr_reg(pr_reg);
  2186. }
  2187. }
  2188. return 0;
  2189. }
  2190. unsigned char *core_scsi3_pr_dump_type(int type)
  2191. {
  2192. switch (type) {
  2193. case PR_TYPE_WRITE_EXCLUSIVE:
  2194. return "Write Exclusive Access";
  2195. case PR_TYPE_EXCLUSIVE_ACCESS:
  2196. return "Exclusive Access";
  2197. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2198. return "Write Exclusive Access, Registrants Only";
  2199. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2200. return "Exclusive Access, Registrants Only";
  2201. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2202. return "Write Exclusive Access, All Registrants";
  2203. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2204. return "Exclusive Access, All Registrants";
  2205. default:
  2206. break;
  2207. }
  2208. return "Unknown SPC-3 PR Type";
  2209. }
  2210. static int core_scsi3_pro_reserve(
  2211. struct se_cmd *cmd,
  2212. struct se_device *dev,
  2213. int type,
  2214. int scope,
  2215. u64 res_key)
  2216. {
  2217. struct se_session *se_sess = cmd->se_sess;
  2218. struct se_lun *se_lun = cmd->se_lun;
  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. /*
  2230. * Locate the existing *pr_reg via struct se_node_acl pointers
  2231. */
  2232. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2233. se_sess);
  2234. if (!pr_reg) {
  2235. pr_err("SPC-3 PR: Unable to locate"
  2236. " PR_REGISTERED *pr_reg for RESERVE\n");
  2237. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2238. return -EINVAL;
  2239. }
  2240. /*
  2241. * From spc4r17 Section 5.7.9: Reserving:
  2242. *
  2243. * An application client creates a persistent reservation by issuing
  2244. * a PERSISTENT RESERVE OUT command with RESERVE service action through
  2245. * a registered I_T nexus with the following parameters:
  2246. * a) RESERVATION KEY set to the value of the reservation key that is
  2247. * registered with the logical unit for the I_T nexus; and
  2248. */
  2249. if (res_key != pr_reg->pr_res_key) {
  2250. pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
  2251. " does not match existing SA REGISTER res_key:"
  2252. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2253. core_scsi3_put_pr_reg(pr_reg);
  2254. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2255. return -EINVAL;
  2256. }
  2257. /*
  2258. * From spc4r17 Section 5.7.9: Reserving:
  2259. *
  2260. * From above:
  2261. * b) TYPE field and SCOPE field set to the persistent reservation
  2262. * being created.
  2263. *
  2264. * Only one persistent reservation is allowed at a time per logical unit
  2265. * and that persistent reservation has a scope of LU_SCOPE.
  2266. */
  2267. if (scope != PR_SCOPE_LU_SCOPE) {
  2268. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2269. core_scsi3_put_pr_reg(pr_reg);
  2270. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2271. return -EINVAL;
  2272. }
  2273. /*
  2274. * See if we have an existing PR reservation holder pointer at
  2275. * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
  2276. * *pr_res_holder.
  2277. */
  2278. spin_lock(&dev->dev_reservation_lock);
  2279. pr_res_holder = dev->dev_pr_res_holder;
  2280. if (pr_res_holder) {
  2281. /*
  2282. * From spc4r17 Section 5.7.9: Reserving:
  2283. *
  2284. * If the device server receives a PERSISTENT RESERVE OUT
  2285. * command from an I_T nexus other than a persistent reservation
  2286. * holder (see 5.7.10) that attempts to create a persistent
  2287. * reservation when a persistent reservation already exists for
  2288. * the logical unit, then the command shall be completed with
  2289. * RESERVATION CONFLICT status.
  2290. */
  2291. if (pr_res_holder != pr_reg) {
  2292. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2293. pr_err("SPC-3 PR: Attempted RESERVE from"
  2294. " [%s]: %s while reservation already held by"
  2295. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2296. cmd->se_tfo->get_fabric_name(),
  2297. se_sess->se_node_acl->initiatorname,
  2298. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2299. pr_res_holder->pr_reg_nacl->initiatorname);
  2300. spin_unlock(&dev->dev_reservation_lock);
  2301. core_scsi3_put_pr_reg(pr_reg);
  2302. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2303. return -EINVAL;
  2304. }
  2305. /*
  2306. * From spc4r17 Section 5.7.9: Reserving:
  2307. *
  2308. * If a persistent reservation holder attempts to modify the
  2309. * type or scope of an existing persistent reservation, the
  2310. * command shall be completed with RESERVATION CONFLICT status.
  2311. */
  2312. if ((pr_res_holder->pr_res_type != type) ||
  2313. (pr_res_holder->pr_res_scope != scope)) {
  2314. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2315. pr_err("SPC-3 PR: Attempted RESERVE from"
  2316. " [%s]: %s trying to change TYPE and/or SCOPE,"
  2317. " while reservation already held by [%s]: %s,"
  2318. " returning RESERVATION_CONFLICT\n",
  2319. cmd->se_tfo->get_fabric_name(),
  2320. se_sess->se_node_acl->initiatorname,
  2321. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2322. pr_res_holder->pr_reg_nacl->initiatorname);
  2323. spin_unlock(&dev->dev_reservation_lock);
  2324. core_scsi3_put_pr_reg(pr_reg);
  2325. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2326. return -EINVAL;
  2327. }
  2328. /*
  2329. * From spc4r17 Section 5.7.9: Reserving:
  2330. *
  2331. * If the device server receives a PERSISTENT RESERVE OUT
  2332. * command with RESERVE service action where the TYPE field and
  2333. * the SCOPE field contain the same values as the existing type
  2334. * and scope from a persistent reservation holder, it shall not
  2335. * make any change to the existing persistent reservation and
  2336. * shall completethe command with GOOD status.
  2337. */
  2338. spin_unlock(&dev->dev_reservation_lock);
  2339. core_scsi3_put_pr_reg(pr_reg);
  2340. return 0;
  2341. }
  2342. /*
  2343. * Otherwise, our *pr_reg becomes the PR reservation holder for said
  2344. * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
  2345. */
  2346. pr_reg->pr_res_scope = scope;
  2347. pr_reg->pr_res_type = type;
  2348. pr_reg->pr_res_holder = 1;
  2349. dev->dev_pr_res_holder = pr_reg;
  2350. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2351. PR_REG_ISID_ID_LEN);
  2352. pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
  2353. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2354. cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
  2355. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2356. pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
  2357. cmd->se_tfo->get_fabric_name(),
  2358. se_sess->se_node_acl->initiatorname,
  2359. (prf_isid) ? &i_buf[0] : "");
  2360. spin_unlock(&dev->dev_reservation_lock);
  2361. if (pr_tmpl->pr_aptpl_active) {
  2362. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2363. &pr_reg->pr_aptpl_buf[0],
  2364. pr_tmpl->pr_aptpl_buf_len);
  2365. if (!ret)
  2366. pr_debug("SPC-3 PR: Updated APTPL metadata"
  2367. " for RESERVE\n");
  2368. }
  2369. core_scsi3_put_pr_reg(pr_reg);
  2370. return 0;
  2371. }
  2372. static int core_scsi3_emulate_pro_reserve(
  2373. struct se_cmd *cmd,
  2374. int type,
  2375. int scope,
  2376. u64 res_key)
  2377. {
  2378. struct se_device *dev = cmd->se_dev;
  2379. int ret = 0;
  2380. switch (type) {
  2381. case PR_TYPE_WRITE_EXCLUSIVE:
  2382. case PR_TYPE_EXCLUSIVE_ACCESS:
  2383. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  2384. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  2385. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  2386. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  2387. ret = core_scsi3_pro_reserve(cmd, dev, type, scope, res_key);
  2388. break;
  2389. default:
  2390. pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
  2391. " 0x%02x\n", type);
  2392. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  2393. return -EINVAL;
  2394. }
  2395. return ret;
  2396. }
  2397. /*
  2398. * Called with struct se_device->dev_reservation_lock held.
  2399. */
  2400. static void __core_scsi3_complete_pro_release(
  2401. struct se_device *dev,
  2402. struct se_node_acl *se_nacl,
  2403. struct t10_pr_registration *pr_reg,
  2404. int explict)
  2405. {
  2406. struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
  2407. char i_buf[PR_REG_ISID_ID_LEN];
  2408. int prf_isid;
  2409. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2410. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2411. PR_REG_ISID_ID_LEN);
  2412. /*
  2413. * Go ahead and release the current PR reservation holder.
  2414. */
  2415. dev->dev_pr_res_holder = NULL;
  2416. pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
  2417. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2418. tfo->get_fabric_name(), (explict) ? "explict" : "implict",
  2419. core_scsi3_pr_dump_type(pr_reg->pr_res_type),
  2420. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2421. pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
  2422. tfo->get_fabric_name(), se_nacl->initiatorname,
  2423. (prf_isid) ? &i_buf[0] : "");
  2424. /*
  2425. * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
  2426. */
  2427. pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
  2428. }
  2429. static int core_scsi3_emulate_pro_release(
  2430. struct se_cmd *cmd,
  2431. int type,
  2432. int scope,
  2433. u64 res_key)
  2434. {
  2435. struct se_device *dev = cmd->se_dev;
  2436. struct se_session *se_sess = cmd->se_sess;
  2437. struct se_lun *se_lun = cmd->se_lun;
  2438. struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
  2439. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2440. int ret, all_reg = 0;
  2441. if (!se_sess || !se_lun) {
  2442. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  2443. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2444. return -EINVAL;
  2445. }
  2446. /*
  2447. * Locate the existing *pr_reg via struct se_node_acl pointers
  2448. */
  2449. pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
  2450. if (!pr_reg) {
  2451. pr_err("SPC-3 PR: Unable to locate"
  2452. " PR_REGISTERED *pr_reg for RELEASE\n");
  2453. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2454. return -EINVAL;
  2455. }
  2456. /*
  2457. * From spc4r17 Section 5.7.11.2 Releasing:
  2458. *
  2459. * If there is no persistent reservation or in response to a persistent
  2460. * reservation release request from a registered I_T nexus that is not a
  2461. * persistent reservation holder (see 5.7.10), the device server shall
  2462. * do the following:
  2463. *
  2464. * a) Not release the persistent reservation, if any;
  2465. * b) Not remove any registrations; and
  2466. * c) Complete the command with GOOD status.
  2467. */
  2468. spin_lock(&dev->dev_reservation_lock);
  2469. pr_res_holder = dev->dev_pr_res_holder;
  2470. if (!pr_res_holder) {
  2471. /*
  2472. * No persistent reservation, return GOOD status.
  2473. */
  2474. spin_unlock(&dev->dev_reservation_lock);
  2475. core_scsi3_put_pr_reg(pr_reg);
  2476. return 0;
  2477. }
  2478. if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2479. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
  2480. all_reg = 1;
  2481. if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
  2482. /*
  2483. * Non 'All Registrants' PR Type cases..
  2484. * Release request from a registered I_T nexus that is not a
  2485. * persistent reservation holder. return GOOD status.
  2486. */
  2487. spin_unlock(&dev->dev_reservation_lock);
  2488. core_scsi3_put_pr_reg(pr_reg);
  2489. return 0;
  2490. }
  2491. /*
  2492. * From spc4r17 Section 5.7.11.2 Releasing:
  2493. *
  2494. * Only the persistent reservation holder (see 5.7.10) is allowed to
  2495. * release a persistent reservation.
  2496. *
  2497. * An application client releases the persistent reservation by issuing
  2498. * a PERSISTENT RESERVE OUT command with RELEASE service action through
  2499. * an I_T nexus that is a persistent reservation holder with the
  2500. * following parameters:
  2501. *
  2502. * a) RESERVATION KEY field set to the value of the reservation key
  2503. * that is registered with the logical unit for the I_T nexus;
  2504. */
  2505. if (res_key != pr_reg->pr_res_key) {
  2506. pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
  2507. " does not match existing SA REGISTER res_key:"
  2508. " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  2509. spin_unlock(&dev->dev_reservation_lock);
  2510. core_scsi3_put_pr_reg(pr_reg);
  2511. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2512. return -EINVAL;
  2513. }
  2514. /*
  2515. * From spc4r17 Section 5.7.11.2 Releasing and above:
  2516. *
  2517. * b) TYPE field and SCOPE field set to match the persistent
  2518. * reservation being released.
  2519. */
  2520. if ((pr_res_holder->pr_res_type != type) ||
  2521. (pr_res_holder->pr_res_scope != scope)) {
  2522. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2523. pr_err("SPC-3 PR RELEASE: Attempted to release"
  2524. " reservation from [%s]: %s with different TYPE "
  2525. "and/or SCOPE while reservation already held by"
  2526. " [%s]: %s, returning RESERVATION_CONFLICT\n",
  2527. cmd->se_tfo->get_fabric_name(),
  2528. se_sess->se_node_acl->initiatorname,
  2529. pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  2530. pr_res_holder->pr_reg_nacl->initiatorname);
  2531. spin_unlock(&dev->dev_reservation_lock);
  2532. core_scsi3_put_pr_reg(pr_reg);
  2533. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2534. return -EINVAL;
  2535. }
  2536. /*
  2537. * In response to a persistent reservation release request from the
  2538. * persistent reservation holder the device server shall perform a
  2539. * release by doing the following as an uninterrupted series of actions:
  2540. * a) Release the persistent reservation;
  2541. * b) Not remove any registration(s);
  2542. * c) If the released persistent reservation is a registrants only type
  2543. * or all registrants type persistent reservation,
  2544. * the device server shall establish a unit attention condition for
  2545. * the initiator port associated with every regis-
  2546. * tered I_T nexus other than I_T nexus on which the PERSISTENT
  2547. * RESERVE OUT command with RELEASE service action was received,
  2548. * with the additional sense code set to RESERVATIONS RELEASED; and
  2549. * d) If the persistent reservation is of any other type, the device
  2550. * server shall not establish a unit attention condition.
  2551. */
  2552. __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
  2553. pr_reg, 1);
  2554. spin_unlock(&dev->dev_reservation_lock);
  2555. if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
  2556. (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
  2557. (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
  2558. (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  2559. /*
  2560. * If no UNIT ATTENTION conditions will be established for
  2561. * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
  2562. * go ahead and check for APTPL=1 update+write below
  2563. */
  2564. goto write_aptpl;
  2565. }
  2566. spin_lock(&pr_tmpl->registration_lock);
  2567. list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
  2568. pr_reg_list) {
  2569. /*
  2570. * Do not establish a UNIT ATTENTION condition
  2571. * for the calling I_T Nexus
  2572. */
  2573. if (pr_reg_p == pr_reg)
  2574. continue;
  2575. core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
  2576. pr_reg_p->pr_res_mapped_lun,
  2577. 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
  2578. }
  2579. spin_unlock(&pr_tmpl->registration_lock);
  2580. write_aptpl:
  2581. if (pr_tmpl->pr_aptpl_active) {
  2582. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2583. &pr_reg->pr_aptpl_buf[0],
  2584. pr_tmpl->pr_aptpl_buf_len);
  2585. if (!ret)
  2586. pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
  2587. }
  2588. core_scsi3_put_pr_reg(pr_reg);
  2589. return 0;
  2590. }
  2591. static int core_scsi3_emulate_pro_clear(
  2592. struct se_cmd *cmd,
  2593. u64 res_key)
  2594. {
  2595. struct se_device *dev = cmd->se_dev;
  2596. struct se_node_acl *pr_reg_nacl;
  2597. struct se_session *se_sess = cmd->se_sess;
  2598. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2599. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2600. u32 pr_res_mapped_lun = 0;
  2601. int calling_it_nexus = 0;
  2602. /*
  2603. * Locate the existing *pr_reg via struct se_node_acl pointers
  2604. */
  2605. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
  2606. se_sess->se_node_acl, se_sess);
  2607. if (!pr_reg_n) {
  2608. pr_err("SPC-3 PR: Unable to locate"
  2609. " PR_REGISTERED *pr_reg for CLEAR\n");
  2610. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2611. return -EINVAL;
  2612. }
  2613. /*
  2614. * From spc4r17 section 5.7.11.6, Clearing:
  2615. *
  2616. * Any application client may release the persistent reservation and
  2617. * remove all registrations from a device server by issuing a
  2618. * PERSISTENT RESERVE OUT command with CLEAR service action through a
  2619. * registered I_T nexus with the following parameter:
  2620. *
  2621. * a) RESERVATION KEY field set to the value of the reservation key
  2622. * that is registered with the logical unit for the I_T nexus.
  2623. */
  2624. if (res_key != pr_reg_n->pr_res_key) {
  2625. pr_err("SPC-3 PR REGISTER: Received"
  2626. " res_key: 0x%016Lx does not match"
  2627. " existing SA REGISTER res_key:"
  2628. " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
  2629. core_scsi3_put_pr_reg(pr_reg_n);
  2630. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2631. return -EINVAL;
  2632. }
  2633. /*
  2634. * a) Release the persistent reservation, if any;
  2635. */
  2636. spin_lock(&dev->dev_reservation_lock);
  2637. pr_res_holder = dev->dev_pr_res_holder;
  2638. if (pr_res_holder) {
  2639. struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
  2640. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  2641. pr_res_holder, 0);
  2642. }
  2643. spin_unlock(&dev->dev_reservation_lock);
  2644. /*
  2645. * b) Remove all registration(s) (see spc4r17 5.7.7);
  2646. */
  2647. spin_lock(&pr_tmpl->registration_lock);
  2648. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2649. &pr_tmpl->registration_list, pr_reg_list) {
  2650. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2651. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2652. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2653. __core_scsi3_free_registration(dev, pr_reg, NULL,
  2654. calling_it_nexus);
  2655. /*
  2656. * e) Establish a unit attention condition for the initiator
  2657. * port associated with every registered I_T nexus other
  2658. * than the I_T nexus on which the PERSISTENT RESERVE OUT
  2659. * command with CLEAR service action was received, with the
  2660. * additional sense code set to RESERVATIONS PREEMPTED.
  2661. */
  2662. if (!calling_it_nexus)
  2663. core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
  2664. 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
  2665. }
  2666. spin_unlock(&pr_tmpl->registration_lock);
  2667. pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
  2668. cmd->se_tfo->get_fabric_name());
  2669. if (pr_tmpl->pr_aptpl_active) {
  2670. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  2671. pr_debug("SPC-3 PR: Updated APTPL metadata"
  2672. " for CLEAR\n");
  2673. }
  2674. core_scsi3_pr_generation(dev);
  2675. return 0;
  2676. }
  2677. /*
  2678. * Called with struct se_device->dev_reservation_lock held.
  2679. */
  2680. static void __core_scsi3_complete_pro_preempt(
  2681. struct se_device *dev,
  2682. struct t10_pr_registration *pr_reg,
  2683. struct list_head *preempt_and_abort_list,
  2684. int type,
  2685. int scope,
  2686. int abort)
  2687. {
  2688. struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
  2689. struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
  2690. char i_buf[PR_REG_ISID_ID_LEN];
  2691. int prf_isid;
  2692. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  2693. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  2694. PR_REG_ISID_ID_LEN);
  2695. /*
  2696. * Do an implict RELEASE of the existing reservation.
  2697. */
  2698. if (dev->dev_pr_res_holder)
  2699. __core_scsi3_complete_pro_release(dev, nacl,
  2700. dev->dev_pr_res_holder, 0);
  2701. dev->dev_pr_res_holder = pr_reg;
  2702. pr_reg->pr_res_holder = 1;
  2703. pr_reg->pr_res_type = type;
  2704. pr_reg->pr_res_scope = scope;
  2705. pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
  2706. " reservation holder TYPE: %s ALL_TG_PT: %d\n",
  2707. tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
  2708. core_scsi3_pr_dump_type(type),
  2709. (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
  2710. pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
  2711. tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
  2712. nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
  2713. /*
  2714. * For PREEMPT_AND_ABORT, add the preempting reservation's
  2715. * struct t10_pr_registration to the list that will be compared
  2716. * against received CDBs..
  2717. */
  2718. if (preempt_and_abort_list)
  2719. list_add_tail(&pr_reg->pr_reg_abort_list,
  2720. preempt_and_abort_list);
  2721. }
  2722. static void core_scsi3_release_preempt_and_abort(
  2723. struct list_head *preempt_and_abort_list,
  2724. struct t10_pr_registration *pr_reg_holder)
  2725. {
  2726. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  2727. list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
  2728. pr_reg_abort_list) {
  2729. list_del(&pr_reg->pr_reg_abort_list);
  2730. if (pr_reg_holder == pr_reg)
  2731. continue;
  2732. if (pr_reg->pr_res_holder) {
  2733. pr_warn("pr_reg->pr_res_holder still set\n");
  2734. continue;
  2735. }
  2736. pr_reg->pr_reg_deve = NULL;
  2737. pr_reg->pr_reg_nacl = NULL;
  2738. kfree(pr_reg->pr_aptpl_buf);
  2739. kmem_cache_free(t10_pr_reg_cache, pr_reg);
  2740. }
  2741. }
  2742. static int core_scsi3_pro_preempt(
  2743. struct se_cmd *cmd,
  2744. int type,
  2745. int scope,
  2746. u64 res_key,
  2747. u64 sa_res_key,
  2748. int abort)
  2749. {
  2750. struct se_device *dev = cmd->se_dev;
  2751. struct se_node_acl *pr_reg_nacl;
  2752. struct se_session *se_sess = cmd->se_sess;
  2753. LIST_HEAD(preempt_and_abort_list);
  2754. struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
  2755. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  2756. u32 pr_res_mapped_lun = 0;
  2757. int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
  2758. int prh_type = 0, prh_scope = 0, ret;
  2759. if (!se_sess) {
  2760. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  2761. return -EINVAL;
  2762. }
  2763. pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  2764. se_sess);
  2765. if (!pr_reg_n) {
  2766. pr_err("SPC-3 PR: Unable to locate"
  2767. " PR_REGISTERED *pr_reg for PREEMPT%s\n",
  2768. (abort) ? "_AND_ABORT" : "");
  2769. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2770. return -EINVAL;
  2771. }
  2772. if (pr_reg_n->pr_res_key != res_key) {
  2773. core_scsi3_put_pr_reg(pr_reg_n);
  2774. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2775. return -EINVAL;
  2776. }
  2777. if (scope != PR_SCOPE_LU_SCOPE) {
  2778. pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
  2779. core_scsi3_put_pr_reg(pr_reg_n);
  2780. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2781. return -EINVAL;
  2782. }
  2783. spin_lock(&dev->dev_reservation_lock);
  2784. pr_res_holder = dev->dev_pr_res_holder;
  2785. if (pr_res_holder &&
  2786. ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  2787. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
  2788. all_reg = 1;
  2789. if (!all_reg && !sa_res_key) {
  2790. spin_unlock(&dev->dev_reservation_lock);
  2791. core_scsi3_put_pr_reg(pr_reg_n);
  2792. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  2793. return -EINVAL;
  2794. }
  2795. /*
  2796. * From spc4r17, section 5.7.11.4.4 Removing Registrations:
  2797. *
  2798. * If the SERVICE ACTION RESERVATION KEY field does not identify a
  2799. * persistent reservation holder or there is no persistent reservation
  2800. * holder (i.e., there is no persistent reservation), then the device
  2801. * server shall perform a preempt by doing the following in an
  2802. * uninterrupted series of actions. (See below..)
  2803. */
  2804. if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
  2805. /*
  2806. * No existing or SA Reservation Key matching reservations..
  2807. *
  2808. * PROUT SA PREEMPT with All Registrant type reservations are
  2809. * allowed to be processed without a matching SA Reservation Key
  2810. */
  2811. spin_lock(&pr_tmpl->registration_lock);
  2812. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2813. &pr_tmpl->registration_list, pr_reg_list) {
  2814. /*
  2815. * Removing of registrations in non all registrants
  2816. * type reservations without a matching SA reservation
  2817. * key.
  2818. *
  2819. * a) Remove the registrations for all I_T nexuses
  2820. * specified by the SERVICE ACTION RESERVATION KEY
  2821. * field;
  2822. * b) Ignore the contents of the SCOPE and TYPE fields;
  2823. * c) Process tasks as defined in 5.7.1; and
  2824. * d) Establish a unit attention condition for the
  2825. * initiator port associated with every I_T nexus
  2826. * that lost its registration other than the I_T
  2827. * nexus on which the PERSISTENT RESERVE OUT command
  2828. * was received, with the additional sense code set
  2829. * to REGISTRATIONS PREEMPTED.
  2830. */
  2831. if (!all_reg) {
  2832. if (pr_reg->pr_res_key != sa_res_key)
  2833. continue;
  2834. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2835. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2836. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2837. __core_scsi3_free_registration(dev, pr_reg,
  2838. (abort) ? &preempt_and_abort_list :
  2839. NULL, calling_it_nexus);
  2840. released_regs++;
  2841. } else {
  2842. /*
  2843. * Case for any existing all registrants type
  2844. * reservation, follow logic in spc4r17 section
  2845. * 5.7.11.4 Preempting, Table 52 and Figure 7.
  2846. *
  2847. * For a ZERO SA Reservation key, release
  2848. * all other registrations and do an implict
  2849. * release of active persistent reservation.
  2850. *
  2851. * For a non-ZERO SA Reservation key, only
  2852. * release the matching reservation key from
  2853. * registrations.
  2854. */
  2855. if ((sa_res_key) &&
  2856. (pr_reg->pr_res_key != sa_res_key))
  2857. continue;
  2858. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2859. if (calling_it_nexus)
  2860. continue;
  2861. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2862. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2863. __core_scsi3_free_registration(dev, pr_reg,
  2864. (abort) ? &preempt_and_abort_list :
  2865. NULL, 0);
  2866. released_regs++;
  2867. }
  2868. if (!calling_it_nexus)
  2869. core_scsi3_ua_allocate(pr_reg_nacl,
  2870. pr_res_mapped_lun, 0x2A,
  2871. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2872. }
  2873. spin_unlock(&pr_tmpl->registration_lock);
  2874. /*
  2875. * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
  2876. * a PREEMPT AND ABORT service action sets the SERVICE ACTION
  2877. * RESERVATION KEY field to a value that does not match any
  2878. * registered reservation key, then the device server shall
  2879. * complete the command with RESERVATION CONFLICT status.
  2880. */
  2881. if (!released_regs) {
  2882. spin_unlock(&dev->dev_reservation_lock);
  2883. core_scsi3_put_pr_reg(pr_reg_n);
  2884. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  2885. return -EINVAL;
  2886. }
  2887. /*
  2888. * For an existing all registrants type reservation
  2889. * with a zero SA rservation key, preempt the existing
  2890. * reservation with the new PR type and scope.
  2891. */
  2892. if (pr_res_holder && all_reg && !(sa_res_key)) {
  2893. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2894. (abort) ? &preempt_and_abort_list : NULL,
  2895. type, scope, abort);
  2896. if (abort)
  2897. core_scsi3_release_preempt_and_abort(
  2898. &preempt_and_abort_list, pr_reg_n);
  2899. }
  2900. spin_unlock(&dev->dev_reservation_lock);
  2901. if (pr_tmpl->pr_aptpl_active) {
  2902. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  2903. &pr_reg_n->pr_aptpl_buf[0],
  2904. pr_tmpl->pr_aptpl_buf_len);
  2905. if (!ret)
  2906. pr_debug("SPC-3 PR: Updated APTPL"
  2907. " metadata for PREEMPT%s\n", (abort) ?
  2908. "_AND_ABORT" : "");
  2909. }
  2910. core_scsi3_put_pr_reg(pr_reg_n);
  2911. core_scsi3_pr_generation(cmd->se_dev);
  2912. return 0;
  2913. }
  2914. /*
  2915. * The PREEMPTing SA reservation key matches that of the
  2916. * existing persistent reservation, first, we check if
  2917. * we are preempting our own reservation.
  2918. * From spc4r17, section 5.7.11.4.3 Preempting
  2919. * persistent reservations and registration handling
  2920. *
  2921. * If an all registrants persistent reservation is not
  2922. * present, it is not an error for the persistent
  2923. * reservation holder to preempt itself (i.e., a
  2924. * PERSISTENT RESERVE OUT with a PREEMPT service action
  2925. * or a PREEMPT AND ABORT service action with the
  2926. * SERVICE ACTION RESERVATION KEY value equal to the
  2927. * persistent reservation holder's reservation key that
  2928. * is received from the persistent reservation holder).
  2929. * In that case, the device server shall establish the
  2930. * new persistent reservation and maintain the
  2931. * registration.
  2932. */
  2933. prh_type = pr_res_holder->pr_res_type;
  2934. prh_scope = pr_res_holder->pr_res_scope;
  2935. /*
  2936. * If the SERVICE ACTION RESERVATION KEY field identifies a
  2937. * persistent reservation holder (see 5.7.10), the device
  2938. * server shall perform a preempt by doing the following as
  2939. * an uninterrupted series of actions:
  2940. *
  2941. * a) Release the persistent reservation for the holder
  2942. * identified by the SERVICE ACTION RESERVATION KEY field;
  2943. */
  2944. if (pr_reg_n != pr_res_holder)
  2945. __core_scsi3_complete_pro_release(dev,
  2946. pr_res_holder->pr_reg_nacl,
  2947. dev->dev_pr_res_holder, 0);
  2948. /*
  2949. * b) Remove the registrations for all I_T nexuses identified
  2950. * by the SERVICE ACTION RESERVATION KEY field, except the
  2951. * I_T nexus that is being used for the PERSISTENT RESERVE
  2952. * OUT command. If an all registrants persistent reservation
  2953. * is present and the SERVICE ACTION RESERVATION KEY field
  2954. * is set to zero, then all registrations shall be removed
  2955. * except for that of the I_T nexus that is being used for
  2956. * the PERSISTENT RESERVE OUT command;
  2957. */
  2958. spin_lock(&pr_tmpl->registration_lock);
  2959. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  2960. &pr_tmpl->registration_list, pr_reg_list) {
  2961. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  2962. if (calling_it_nexus)
  2963. continue;
  2964. if (pr_reg->pr_res_key != sa_res_key)
  2965. continue;
  2966. pr_reg_nacl = pr_reg->pr_reg_nacl;
  2967. pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
  2968. __core_scsi3_free_registration(dev, pr_reg,
  2969. (abort) ? &preempt_and_abort_list : NULL,
  2970. calling_it_nexus);
  2971. /*
  2972. * e) Establish a unit attention condition for the initiator
  2973. * port associated with every I_T nexus that lost its
  2974. * persistent reservation and/or registration, with the
  2975. * additional sense code set to REGISTRATIONS PREEMPTED;
  2976. */
  2977. core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
  2978. ASCQ_2AH_REGISTRATIONS_PREEMPTED);
  2979. }
  2980. spin_unlock(&pr_tmpl->registration_lock);
  2981. /*
  2982. * c) Establish a persistent reservation for the preempting
  2983. * I_T nexus using the contents of the SCOPE and TYPE fields;
  2984. */
  2985. __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
  2986. (abort) ? &preempt_and_abort_list : NULL,
  2987. type, scope, abort);
  2988. /*
  2989. * d) Process tasks as defined in 5.7.1;
  2990. * e) See above..
  2991. * f) If the type or scope has changed, then for every I_T nexus
  2992. * whose reservation key was not removed, except for the I_T
  2993. * nexus on which the PERSISTENT RESERVE OUT command was
  2994. * received, the device server shall establish a unit
  2995. * attention condition for the initiator port associated with
  2996. * that I_T nexus, with the additional sense code set to
  2997. * RESERVATIONS RELEASED. If the type or scope have not
  2998. * changed, then no unit attention condition(s) shall be
  2999. * established for this reason.
  3000. */
  3001. if ((prh_type != type) || (prh_scope != scope)) {
  3002. spin_lock(&pr_tmpl->registration_lock);
  3003. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  3004. &pr_tmpl->registration_list, pr_reg_list) {
  3005. calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
  3006. if (calling_it_nexus)
  3007. continue;
  3008. core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
  3009. pr_reg->pr_res_mapped_lun, 0x2A,
  3010. ASCQ_2AH_RESERVATIONS_RELEASED);
  3011. }
  3012. spin_unlock(&pr_tmpl->registration_lock);
  3013. }
  3014. spin_unlock(&dev->dev_reservation_lock);
  3015. /*
  3016. * Call LUN_RESET logic upon list of struct t10_pr_registration,
  3017. * All received CDBs for the matching existing reservation and
  3018. * registrations undergo ABORT_TASK logic.
  3019. *
  3020. * From there, core_scsi3_release_preempt_and_abort() will
  3021. * release every registration in the list (which have already
  3022. * been removed from the primary pr_reg list), except the
  3023. * new persistent reservation holder, the calling Initiator Port.
  3024. */
  3025. if (abort) {
  3026. core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
  3027. core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
  3028. pr_reg_n);
  3029. }
  3030. if (pr_tmpl->pr_aptpl_active) {
  3031. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  3032. &pr_reg_n->pr_aptpl_buf[0],
  3033. pr_tmpl->pr_aptpl_buf_len);
  3034. if (!ret)
  3035. pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
  3036. "%s\n", (abort) ? "_AND_ABORT" : "");
  3037. }
  3038. core_scsi3_put_pr_reg(pr_reg_n);
  3039. core_scsi3_pr_generation(cmd->se_dev);
  3040. return 0;
  3041. }
  3042. static int core_scsi3_emulate_pro_preempt(
  3043. struct se_cmd *cmd,
  3044. int type,
  3045. int scope,
  3046. u64 res_key,
  3047. u64 sa_res_key,
  3048. int abort)
  3049. {
  3050. int ret = 0;
  3051. switch (type) {
  3052. case PR_TYPE_WRITE_EXCLUSIVE:
  3053. case PR_TYPE_EXCLUSIVE_ACCESS:
  3054. case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
  3055. case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
  3056. case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
  3057. case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
  3058. ret = core_scsi3_pro_preempt(cmd, type, scope,
  3059. res_key, sa_res_key, abort);
  3060. break;
  3061. default:
  3062. pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
  3063. " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
  3064. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3065. return -EINVAL;
  3066. }
  3067. return ret;
  3068. }
  3069. static int core_scsi3_emulate_pro_register_and_move(
  3070. struct se_cmd *cmd,
  3071. u64 res_key,
  3072. u64 sa_res_key,
  3073. int aptpl,
  3074. int unreg)
  3075. {
  3076. struct se_session *se_sess = cmd->se_sess;
  3077. struct se_device *dev = cmd->se_dev;
  3078. struct se_dev_entry *dest_se_deve = NULL;
  3079. struct se_lun *se_lun = cmd->se_lun;
  3080. struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
  3081. struct se_port *se_port;
  3082. struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
  3083. struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
  3084. struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
  3085. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  3086. unsigned char *buf;
  3087. unsigned char *initiator_str;
  3088. char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
  3089. u32 tid_len, tmp_tid_len;
  3090. int new_reg = 0, type, scope, ret, matching_iname, prf_isid;
  3091. unsigned short rtpi;
  3092. unsigned char proto_ident;
  3093. if (!se_sess || !se_lun) {
  3094. pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
  3095. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3096. return -EINVAL;
  3097. }
  3098. memset(dest_iport, 0, 64);
  3099. memset(i_buf, 0, PR_REG_ISID_ID_LEN);
  3100. se_tpg = se_sess->se_tpg;
  3101. tf_ops = se_tpg->se_tpg_tfo;
  3102. /*
  3103. * Follow logic from spc4r17 Section 5.7.8, Table 50 --
  3104. * Register behaviors for a REGISTER AND MOVE service action
  3105. *
  3106. * Locate the existing *pr_reg via struct se_node_acl pointers
  3107. */
  3108. pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
  3109. se_sess);
  3110. if (!pr_reg) {
  3111. pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
  3112. " *pr_reg for REGISTER_AND_MOVE\n");
  3113. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3114. return -EINVAL;
  3115. }
  3116. /*
  3117. * The provided reservation key much match the existing reservation key
  3118. * provided during this initiator's I_T nexus registration.
  3119. */
  3120. if (res_key != pr_reg->pr_res_key) {
  3121. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
  3122. " res_key: 0x%016Lx does not match existing SA REGISTER"
  3123. " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
  3124. core_scsi3_put_pr_reg(pr_reg);
  3125. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3126. return -EINVAL;
  3127. }
  3128. /*
  3129. * The service active reservation key needs to be non zero
  3130. */
  3131. if (!sa_res_key) {
  3132. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
  3133. " sa_res_key\n");
  3134. core_scsi3_put_pr_reg(pr_reg);
  3135. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3136. return -EINVAL;
  3137. }
  3138. /*
  3139. * Determine the Relative Target Port Identifier where the reservation
  3140. * will be moved to for the TransportID containing SCSI initiator WWN
  3141. * information.
  3142. */
  3143. buf = transport_kmap_data_sg(cmd);
  3144. rtpi = (buf[18] & 0xff) << 8;
  3145. rtpi |= buf[19] & 0xff;
  3146. tid_len = (buf[20] & 0xff) << 24;
  3147. tid_len |= (buf[21] & 0xff) << 16;
  3148. tid_len |= (buf[22] & 0xff) << 8;
  3149. tid_len |= buf[23] & 0xff;
  3150. transport_kunmap_data_sg(cmd);
  3151. buf = NULL;
  3152. if ((tid_len + 24) != cmd->data_length) {
  3153. pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
  3154. " does not equal CDB data_length: %u\n", tid_len,
  3155. cmd->data_length);
  3156. core_scsi3_put_pr_reg(pr_reg);
  3157. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3158. return -EINVAL;
  3159. }
  3160. spin_lock(&dev->se_port_lock);
  3161. list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
  3162. if (se_port->sep_rtpi != rtpi)
  3163. continue;
  3164. dest_se_tpg = se_port->sep_tpg;
  3165. if (!dest_se_tpg)
  3166. continue;
  3167. dest_tf_ops = dest_se_tpg->se_tpg_tfo;
  3168. if (!dest_tf_ops)
  3169. continue;
  3170. atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
  3171. smp_mb__after_atomic_inc();
  3172. spin_unlock(&dev->se_port_lock);
  3173. ret = core_scsi3_tpg_depend_item(dest_se_tpg);
  3174. if (ret != 0) {
  3175. pr_err("core_scsi3_tpg_depend_item() failed"
  3176. " for dest_se_tpg\n");
  3177. atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
  3178. smp_mb__after_atomic_dec();
  3179. core_scsi3_put_pr_reg(pr_reg);
  3180. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3181. return -EINVAL;
  3182. }
  3183. spin_lock(&dev->se_port_lock);
  3184. break;
  3185. }
  3186. spin_unlock(&dev->se_port_lock);
  3187. if (!dest_se_tpg || !dest_tf_ops) {
  3188. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  3189. " fabric ops from Relative Target Port Identifier:"
  3190. " %hu\n", rtpi);
  3191. core_scsi3_put_pr_reg(pr_reg);
  3192. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3193. return -EINVAL;
  3194. }
  3195. buf = transport_kmap_data_sg(cmd);
  3196. proto_ident = (buf[24] & 0x0f);
  3197. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
  3198. " 0x%02x\n", proto_ident);
  3199. if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
  3200. pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
  3201. " proto_ident: 0x%02x does not match ident: 0x%02x"
  3202. " from fabric: %s\n", proto_ident,
  3203. dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
  3204. dest_tf_ops->get_fabric_name());
  3205. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3206. ret = -EINVAL;
  3207. goto out;
  3208. }
  3209. if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
  3210. pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
  3211. " containg a valid tpg_parse_pr_out_transport_id"
  3212. " function pointer\n");
  3213. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3214. ret = -EINVAL;
  3215. goto out;
  3216. }
  3217. initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
  3218. (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
  3219. if (!initiator_str) {
  3220. pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
  3221. " initiator_str from Transport ID\n");
  3222. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3223. ret = -EINVAL;
  3224. goto out;
  3225. }
  3226. transport_kunmap_data_sg(cmd);
  3227. buf = NULL;
  3228. pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
  3229. " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
  3230. "port" : "device", initiator_str, (iport_ptr != NULL) ?
  3231. iport_ptr : "");
  3232. /*
  3233. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3234. * action specifies a TransportID that is the same as the initiator port
  3235. * of the I_T nexus for the command received, then the command shall
  3236. * be terminated with CHECK CONDITION status, with the sense key set to
  3237. * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
  3238. * IN PARAMETER LIST.
  3239. */
  3240. pr_reg_nacl = pr_reg->pr_reg_nacl;
  3241. matching_iname = (!strcmp(initiator_str,
  3242. pr_reg_nacl->initiatorname)) ? 1 : 0;
  3243. if (!matching_iname)
  3244. goto after_iport_check;
  3245. if (!iport_ptr || !pr_reg->isid_present_at_reg) {
  3246. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
  3247. " matches: %s on received I_T Nexus\n", initiator_str,
  3248. pr_reg_nacl->initiatorname);
  3249. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3250. ret = -EINVAL;
  3251. goto out;
  3252. }
  3253. if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
  3254. pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
  3255. " matches: %s %s on received I_T Nexus\n",
  3256. initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
  3257. pr_reg->pr_reg_isid);
  3258. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3259. ret = -EINVAL;
  3260. goto out;
  3261. }
  3262. after_iport_check:
  3263. /*
  3264. * Locate the destination struct se_node_acl from the received Transport ID
  3265. */
  3266. spin_lock_irq(&dest_se_tpg->acl_node_lock);
  3267. dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
  3268. initiator_str);
  3269. if (dest_node_acl) {
  3270. atomic_inc(&dest_node_acl->acl_pr_ref_count);
  3271. smp_mb__after_atomic_inc();
  3272. }
  3273. spin_unlock_irq(&dest_se_tpg->acl_node_lock);
  3274. if (!dest_node_acl) {
  3275. pr_err("Unable to locate %s dest_node_acl for"
  3276. " TransportID%s\n", dest_tf_ops->get_fabric_name(),
  3277. initiator_str);
  3278. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3279. ret = -EINVAL;
  3280. goto out;
  3281. }
  3282. ret = core_scsi3_nodeacl_depend_item(dest_node_acl);
  3283. if (ret != 0) {
  3284. pr_err("core_scsi3_nodeacl_depend_item() for"
  3285. " dest_node_acl\n");
  3286. atomic_dec(&dest_node_acl->acl_pr_ref_count);
  3287. smp_mb__after_atomic_dec();
  3288. dest_node_acl = NULL;
  3289. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3290. ret = -EINVAL;
  3291. goto out;
  3292. }
  3293. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
  3294. " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
  3295. dest_node_acl->initiatorname);
  3296. /*
  3297. * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
  3298. * PORT IDENTIFIER.
  3299. */
  3300. dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
  3301. if (!dest_se_deve) {
  3302. pr_err("Unable to locate %s dest_se_deve from RTPI:"
  3303. " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
  3304. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3305. ret = -EINVAL;
  3306. goto out;
  3307. }
  3308. ret = core_scsi3_lunacl_depend_item(dest_se_deve);
  3309. if (ret < 0) {
  3310. pr_err("core_scsi3_lunacl_depend_item() failed\n");
  3311. atomic_dec(&dest_se_deve->pr_ref_count);
  3312. smp_mb__after_atomic_dec();
  3313. dest_se_deve = NULL;
  3314. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3315. ret = -EINVAL;
  3316. goto out;
  3317. }
  3318. pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
  3319. " ACL for dest_se_deve->mapped_lun: %u\n",
  3320. dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
  3321. dest_se_deve->mapped_lun);
  3322. /*
  3323. * A persistent reservation needs to already existing in order to
  3324. * successfully complete the REGISTER_AND_MOVE service action..
  3325. */
  3326. spin_lock(&dev->dev_reservation_lock);
  3327. pr_res_holder = dev->dev_pr_res_holder;
  3328. if (!pr_res_holder) {
  3329. pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
  3330. " currently held\n");
  3331. spin_unlock(&dev->dev_reservation_lock);
  3332. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3333. ret = -EINVAL;
  3334. goto out;
  3335. }
  3336. /*
  3337. * The received on I_T Nexus must be the reservation holder.
  3338. *
  3339. * From spc4r17 section 5.7.8 Table 50 --
  3340. * Register behaviors for a REGISTER AND MOVE service action
  3341. */
  3342. if (pr_res_holder != pr_reg) {
  3343. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
  3344. " Nexus is not reservation holder\n");
  3345. spin_unlock(&dev->dev_reservation_lock);
  3346. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3347. ret = -EINVAL;
  3348. goto out;
  3349. }
  3350. /*
  3351. * From spc4r17 section 5.7.8: registering and moving reservation
  3352. *
  3353. * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
  3354. * action is received and the established persistent reservation is a
  3355. * Write Exclusive - All Registrants type or Exclusive Access -
  3356. * All Registrants type reservation, then the command shall be completed
  3357. * with RESERVATION CONFLICT status.
  3358. */
  3359. if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3360. (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
  3361. pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
  3362. " reservation for type: %s\n",
  3363. core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
  3364. spin_unlock(&dev->dev_reservation_lock);
  3365. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3366. ret = -EINVAL;
  3367. goto out;
  3368. }
  3369. pr_res_nacl = pr_res_holder->pr_reg_nacl;
  3370. /*
  3371. * b) Ignore the contents of the (received) SCOPE and TYPE fields;
  3372. */
  3373. type = pr_res_holder->pr_res_type;
  3374. scope = pr_res_holder->pr_res_type;
  3375. /*
  3376. * c) Associate the reservation key specified in the SERVICE ACTION
  3377. * RESERVATION KEY field with the I_T nexus specified as the
  3378. * destination of the register and move, where:
  3379. * A) The I_T nexus is specified by the TransportID and the
  3380. * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
  3381. * B) Regardless of the TransportID format used, the association for
  3382. * the initiator port is based on either the initiator port name
  3383. * (see 3.1.71) on SCSI transport protocols where port names are
  3384. * required or the initiator port identifier (see 3.1.70) on SCSI
  3385. * transport protocols where port names are not required;
  3386. * d) Register the reservation key specified in the SERVICE ACTION
  3387. * RESERVATION KEY field;
  3388. * e) Retain the reservation key specified in the SERVICE ACTION
  3389. * RESERVATION KEY field and associated information;
  3390. *
  3391. * Also, It is not an error for a REGISTER AND MOVE service action to
  3392. * register an I_T nexus that is already registered with the same
  3393. * reservation key or a different reservation key.
  3394. */
  3395. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3396. iport_ptr);
  3397. if (!dest_pr_reg) {
  3398. ret = core_scsi3_alloc_registration(cmd->se_dev,
  3399. dest_node_acl, dest_se_deve, iport_ptr,
  3400. sa_res_key, 0, aptpl, 2, 1);
  3401. if (ret != 0) {
  3402. spin_unlock(&dev->dev_reservation_lock);
  3403. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3404. ret = -EINVAL;
  3405. goto out;
  3406. }
  3407. dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
  3408. iport_ptr);
  3409. new_reg = 1;
  3410. }
  3411. /*
  3412. * f) Release the persistent reservation for the persistent reservation
  3413. * holder (i.e., the I_T nexus on which the
  3414. */
  3415. __core_scsi3_complete_pro_release(dev, pr_res_nacl,
  3416. dev->dev_pr_res_holder, 0);
  3417. /*
  3418. * g) Move the persistent reservation to the specified I_T nexus using
  3419. * the same scope and type as the persistent reservation released in
  3420. * item f); and
  3421. */
  3422. dev->dev_pr_res_holder = dest_pr_reg;
  3423. dest_pr_reg->pr_res_holder = 1;
  3424. dest_pr_reg->pr_res_type = type;
  3425. pr_reg->pr_res_scope = scope;
  3426. prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
  3427. PR_REG_ISID_ID_LEN);
  3428. /*
  3429. * Increment PRGeneration for existing registrations..
  3430. */
  3431. if (!new_reg)
  3432. dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
  3433. spin_unlock(&dev->dev_reservation_lock);
  3434. pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
  3435. " created new reservation holder TYPE: %s on object RTPI:"
  3436. " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
  3437. core_scsi3_pr_dump_type(type), rtpi,
  3438. dest_pr_reg->pr_res_generation);
  3439. pr_debug("SPC-3 PR Successfully moved reservation from"
  3440. " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
  3441. tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
  3442. (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
  3443. dest_node_acl->initiatorname, (iport_ptr != NULL) ?
  3444. iport_ptr : "");
  3445. /*
  3446. * It is now safe to release configfs group dependencies for destination
  3447. * of Transport ID Initiator Device/Port Identifier
  3448. */
  3449. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3450. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3451. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3452. /*
  3453. * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
  3454. * nexus on which PERSISTENT RESERVE OUT command was received.
  3455. */
  3456. if (unreg) {
  3457. spin_lock(&pr_tmpl->registration_lock);
  3458. __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
  3459. spin_unlock(&pr_tmpl->registration_lock);
  3460. } else
  3461. core_scsi3_put_pr_reg(pr_reg);
  3462. /*
  3463. * Clear the APTPL metadata if APTPL has been disabled, otherwise
  3464. * write out the updated metadata to struct file for this SCSI device.
  3465. */
  3466. if (!aptpl) {
  3467. pr_tmpl->pr_aptpl_active = 0;
  3468. core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
  3469. pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
  3470. " REGISTER_AND_MOVE\n");
  3471. } else {
  3472. pr_tmpl->pr_aptpl_active = 1;
  3473. ret = core_scsi3_update_and_write_aptpl(cmd->se_dev,
  3474. &dest_pr_reg->pr_aptpl_buf[0],
  3475. pr_tmpl->pr_aptpl_buf_len);
  3476. if (!ret)
  3477. pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
  3478. " REGISTER_AND_MOVE\n");
  3479. }
  3480. transport_kunmap_data_sg(cmd);
  3481. core_scsi3_put_pr_reg(dest_pr_reg);
  3482. return 0;
  3483. out:
  3484. if (buf)
  3485. transport_kunmap_data_sg(cmd);
  3486. if (dest_se_deve)
  3487. core_scsi3_lunacl_undepend_item(dest_se_deve);
  3488. if (dest_node_acl)
  3489. core_scsi3_nodeacl_undepend_item(dest_node_acl);
  3490. core_scsi3_tpg_undepend_item(dest_se_tpg);
  3491. core_scsi3_put_pr_reg(pr_reg);
  3492. return ret;
  3493. }
  3494. static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
  3495. {
  3496. unsigned int __v1, __v2;
  3497. __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
  3498. __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
  3499. return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
  3500. }
  3501. /*
  3502. * See spc4r17 section 6.14 Table 170
  3503. */
  3504. int target_scsi3_emulate_pr_out(struct se_cmd *cmd)
  3505. {
  3506. unsigned char *cdb = &cmd->t_task_cdb[0];
  3507. unsigned char *buf;
  3508. u64 res_key, sa_res_key;
  3509. int sa, scope, type, aptpl;
  3510. int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
  3511. int ret;
  3512. /*
  3513. * Following spc2r20 5.5.1 Reservations overview:
  3514. *
  3515. * If a logical unit has been reserved by any RESERVE command and is
  3516. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3517. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3518. * initiator or service action and shall terminate with a RESERVATION
  3519. * CONFLICT status.
  3520. */
  3521. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
  3522. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3523. " SPC-2 reservation is held, returning"
  3524. " RESERVATION_CONFLICT\n");
  3525. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3526. ret = -EINVAL;
  3527. goto out;
  3528. }
  3529. /*
  3530. * FIXME: A NULL struct se_session pointer means an this is not coming from
  3531. * a $FABRIC_MOD's nexus, but from internal passthrough ops.
  3532. */
  3533. if (!cmd->se_sess) {
  3534. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  3535. ret = -EINVAL;
  3536. goto out;
  3537. }
  3538. if (cmd->data_length < 24) {
  3539. pr_warn("SPC-PR: Received PR OUT parameter list"
  3540. " length too small: %u\n", cmd->data_length);
  3541. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3542. ret = -EINVAL;
  3543. goto out;
  3544. }
  3545. /*
  3546. * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
  3547. */
  3548. sa = (cdb[1] & 0x1f);
  3549. scope = (cdb[2] & 0xf0);
  3550. type = (cdb[2] & 0x0f);
  3551. buf = transport_kmap_data_sg(cmd);
  3552. /*
  3553. * From PERSISTENT_RESERVE_OUT parameter list (payload)
  3554. */
  3555. res_key = core_scsi3_extract_reservation_key(&buf[0]);
  3556. sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
  3557. /*
  3558. * REGISTER_AND_MOVE uses a different SA parameter list containing
  3559. * SCSI TransportIDs.
  3560. */
  3561. if (sa != PRO_REGISTER_AND_MOVE) {
  3562. spec_i_pt = (buf[20] & 0x08);
  3563. all_tg_pt = (buf[20] & 0x04);
  3564. aptpl = (buf[20] & 0x01);
  3565. } else {
  3566. aptpl = (buf[17] & 0x01);
  3567. unreg = (buf[17] & 0x02);
  3568. }
  3569. transport_kunmap_data_sg(cmd);
  3570. buf = NULL;
  3571. /*
  3572. * SPEC_I_PT=1 is only valid for Service action: REGISTER
  3573. */
  3574. if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER)) {
  3575. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3576. ret = -EINVAL;
  3577. goto out;
  3578. }
  3579. /*
  3580. * From spc4r17 section 6.14:
  3581. *
  3582. * If the SPEC_I_PT bit is set to zero, the service action is not
  3583. * REGISTER AND MOVE, and the parameter list length is not 24, then
  3584. * the command shall be terminated with CHECK CONDITION status, with
  3585. * the sense key set to ILLEGAL REQUEST, and the additional sense
  3586. * code set to PARAMETER LIST LENGTH ERROR.
  3587. */
  3588. if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
  3589. (cmd->data_length != 24)) {
  3590. pr_warn("SPC-PR: Received PR OUT illegal parameter"
  3591. " list length: %u\n", cmd->data_length);
  3592. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  3593. ret = -EINVAL;
  3594. goto out;
  3595. }
  3596. /*
  3597. * (core_scsi3_emulate_pro_* function parameters
  3598. * are defined by spc4r17 Table 174:
  3599. * PERSISTENT_RESERVE_OUT service actions and valid parameters.
  3600. */
  3601. switch (sa) {
  3602. case PRO_REGISTER:
  3603. ret = core_scsi3_emulate_pro_register(cmd,
  3604. res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
  3605. break;
  3606. case PRO_RESERVE:
  3607. ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
  3608. break;
  3609. case PRO_RELEASE:
  3610. ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
  3611. break;
  3612. case PRO_CLEAR:
  3613. ret = core_scsi3_emulate_pro_clear(cmd, res_key);
  3614. break;
  3615. case PRO_PREEMPT:
  3616. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3617. res_key, sa_res_key, 0);
  3618. break;
  3619. case PRO_PREEMPT_AND_ABORT:
  3620. ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
  3621. res_key, sa_res_key, 1);
  3622. break;
  3623. case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
  3624. ret = core_scsi3_emulate_pro_register(cmd,
  3625. 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
  3626. break;
  3627. case PRO_REGISTER_AND_MOVE:
  3628. ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
  3629. sa_res_key, aptpl, unreg);
  3630. break;
  3631. default:
  3632. pr_err("Unknown PERSISTENT_RESERVE_OUT service"
  3633. " action: 0x%02x\n", cdb[1] & 0x1f);
  3634. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3635. ret = -EINVAL;
  3636. break;
  3637. }
  3638. out:
  3639. if (!ret)
  3640. target_complete_cmd(cmd, GOOD);
  3641. return ret;
  3642. }
  3643. /*
  3644. * PERSISTENT_RESERVE_IN Service Action READ_KEYS
  3645. *
  3646. * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
  3647. */
  3648. static int core_scsi3_pri_read_keys(struct se_cmd *cmd)
  3649. {
  3650. struct se_device *se_dev = cmd->se_dev;
  3651. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3652. struct t10_pr_registration *pr_reg;
  3653. unsigned char *buf;
  3654. u32 add_len = 0, off = 8;
  3655. if (cmd->data_length < 8) {
  3656. pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
  3657. " too small\n", cmd->data_length);
  3658. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3659. return -EINVAL;
  3660. }
  3661. buf = transport_kmap_data_sg(cmd);
  3662. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3663. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3664. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3665. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3666. spin_lock(&su_dev->t10_pr.registration_lock);
  3667. list_for_each_entry(pr_reg, &su_dev->t10_pr.registration_list,
  3668. pr_reg_list) {
  3669. /*
  3670. * Check for overflow of 8byte PRI READ_KEYS payload and
  3671. * next reservation key list descriptor.
  3672. */
  3673. if ((add_len + 8) > (cmd->data_length - 8))
  3674. break;
  3675. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3676. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3677. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3678. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3679. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3680. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3681. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3682. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3683. add_len += 8;
  3684. }
  3685. spin_unlock(&su_dev->t10_pr.registration_lock);
  3686. buf[4] = ((add_len >> 24) & 0xff);
  3687. buf[5] = ((add_len >> 16) & 0xff);
  3688. buf[6] = ((add_len >> 8) & 0xff);
  3689. buf[7] = (add_len & 0xff);
  3690. transport_kunmap_data_sg(cmd);
  3691. return 0;
  3692. }
  3693. /*
  3694. * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
  3695. *
  3696. * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
  3697. */
  3698. static int core_scsi3_pri_read_reservation(struct se_cmd *cmd)
  3699. {
  3700. struct se_device *se_dev = cmd->se_dev;
  3701. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3702. struct t10_pr_registration *pr_reg;
  3703. unsigned char *buf;
  3704. u64 pr_res_key;
  3705. u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
  3706. if (cmd->data_length < 8) {
  3707. pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
  3708. " too small\n", cmd->data_length);
  3709. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3710. return -EINVAL;
  3711. }
  3712. buf = transport_kmap_data_sg(cmd);
  3713. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3714. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3715. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3716. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3717. spin_lock(&se_dev->dev_reservation_lock);
  3718. pr_reg = se_dev->dev_pr_res_holder;
  3719. if (pr_reg) {
  3720. /*
  3721. * Set the hardcoded Additional Length
  3722. */
  3723. buf[4] = ((add_len >> 24) & 0xff);
  3724. buf[5] = ((add_len >> 16) & 0xff);
  3725. buf[6] = ((add_len >> 8) & 0xff);
  3726. buf[7] = (add_len & 0xff);
  3727. if (cmd->data_length < 22)
  3728. goto err;
  3729. /*
  3730. * Set the Reservation key.
  3731. *
  3732. * From spc4r17, section 5.7.10:
  3733. * A persistent reservation holder has its reservation key
  3734. * returned in the parameter data from a PERSISTENT
  3735. * RESERVE IN command with READ RESERVATION service action as
  3736. * follows:
  3737. * a) For a persistent reservation of the type Write Exclusive
  3738. * - All Registrants or Exclusive Access ­ All Regitrants,
  3739. * the reservation key shall be set to zero; or
  3740. * b) For all other persistent reservation types, the
  3741. * reservation key shall be set to the registered
  3742. * reservation key for the I_T nexus that holds the
  3743. * persistent reservation.
  3744. */
  3745. if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
  3746. (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
  3747. pr_res_key = 0;
  3748. else
  3749. pr_res_key = pr_reg->pr_res_key;
  3750. buf[8] = ((pr_res_key >> 56) & 0xff);
  3751. buf[9] = ((pr_res_key >> 48) & 0xff);
  3752. buf[10] = ((pr_res_key >> 40) & 0xff);
  3753. buf[11] = ((pr_res_key >> 32) & 0xff);
  3754. buf[12] = ((pr_res_key >> 24) & 0xff);
  3755. buf[13] = ((pr_res_key >> 16) & 0xff);
  3756. buf[14] = ((pr_res_key >> 8) & 0xff);
  3757. buf[15] = (pr_res_key & 0xff);
  3758. /*
  3759. * Set the SCOPE and TYPE
  3760. */
  3761. buf[21] = (pr_reg->pr_res_scope & 0xf0) |
  3762. (pr_reg->pr_res_type & 0x0f);
  3763. }
  3764. err:
  3765. spin_unlock(&se_dev->dev_reservation_lock);
  3766. transport_kunmap_data_sg(cmd);
  3767. return 0;
  3768. }
  3769. /*
  3770. * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
  3771. *
  3772. * See spc4r17 section 6.13.4 Table 165
  3773. */
  3774. static int core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
  3775. {
  3776. struct se_device *dev = cmd->se_dev;
  3777. struct t10_reservation *pr_tmpl = &dev->se_sub_dev->t10_pr;
  3778. unsigned char *buf;
  3779. u16 add_len = 8; /* Hardcoded to 8. */
  3780. if (cmd->data_length < 6) {
  3781. pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
  3782. " %u too small\n", cmd->data_length);
  3783. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3784. return -EINVAL;
  3785. }
  3786. buf = transport_kmap_data_sg(cmd);
  3787. buf[0] = ((add_len << 8) & 0xff);
  3788. buf[1] = (add_len & 0xff);
  3789. buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
  3790. buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
  3791. buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
  3792. buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
  3793. /*
  3794. * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
  3795. * set the TMV: Task Mask Valid bit.
  3796. */
  3797. buf[3] |= 0x80;
  3798. /*
  3799. * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
  3800. */
  3801. buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
  3802. /*
  3803. * PTPL_A: Persistence across Target Power Loss Active bit
  3804. */
  3805. if (pr_tmpl->pr_aptpl_active)
  3806. buf[3] |= 0x01;
  3807. /*
  3808. * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
  3809. */
  3810. buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3811. buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
  3812. buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
  3813. buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
  3814. buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
  3815. buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  3816. transport_kunmap_data_sg(cmd);
  3817. return 0;
  3818. }
  3819. /*
  3820. * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
  3821. *
  3822. * See spc4r17 section 6.13.5 Table 168 and 169
  3823. */
  3824. static int core_scsi3_pri_read_full_status(struct se_cmd *cmd)
  3825. {
  3826. struct se_device *se_dev = cmd->se_dev;
  3827. struct se_node_acl *se_nacl;
  3828. struct se_subsystem_dev *su_dev = se_dev->se_sub_dev;
  3829. struct se_portal_group *se_tpg;
  3830. struct t10_pr_registration *pr_reg, *pr_reg_tmp;
  3831. struct t10_reservation *pr_tmpl = &se_dev->se_sub_dev->t10_pr;
  3832. unsigned char *buf;
  3833. u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
  3834. u32 off = 8; /* off into first Full Status descriptor */
  3835. int format_code = 0;
  3836. if (cmd->data_length < 8) {
  3837. pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
  3838. " too small\n", cmd->data_length);
  3839. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3840. return -EINVAL;
  3841. }
  3842. buf = transport_kmap_data_sg(cmd);
  3843. buf[0] = ((su_dev->t10_pr.pr_generation >> 24) & 0xff);
  3844. buf[1] = ((su_dev->t10_pr.pr_generation >> 16) & 0xff);
  3845. buf[2] = ((su_dev->t10_pr.pr_generation >> 8) & 0xff);
  3846. buf[3] = (su_dev->t10_pr.pr_generation & 0xff);
  3847. spin_lock(&pr_tmpl->registration_lock);
  3848. list_for_each_entry_safe(pr_reg, pr_reg_tmp,
  3849. &pr_tmpl->registration_list, pr_reg_list) {
  3850. se_nacl = pr_reg->pr_reg_nacl;
  3851. se_tpg = pr_reg->pr_reg_nacl->se_tpg;
  3852. add_desc_len = 0;
  3853. atomic_inc(&pr_reg->pr_res_holders);
  3854. smp_mb__after_atomic_inc();
  3855. spin_unlock(&pr_tmpl->registration_lock);
  3856. /*
  3857. * Determine expected length of $FABRIC_MOD specific
  3858. * TransportID full status descriptor..
  3859. */
  3860. exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
  3861. se_tpg, se_nacl, pr_reg, &format_code);
  3862. if ((exp_desc_len + add_len) > cmd->data_length) {
  3863. pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
  3864. " out of buffer: %d\n", cmd->data_length);
  3865. spin_lock(&pr_tmpl->registration_lock);
  3866. atomic_dec(&pr_reg->pr_res_holders);
  3867. smp_mb__after_atomic_dec();
  3868. break;
  3869. }
  3870. /*
  3871. * Set RESERVATION KEY
  3872. */
  3873. buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
  3874. buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
  3875. buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
  3876. buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
  3877. buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
  3878. buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
  3879. buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
  3880. buf[off++] = (pr_reg->pr_res_key & 0xff);
  3881. off += 4; /* Skip Over Reserved area */
  3882. /*
  3883. * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
  3884. */
  3885. if (pr_reg->pr_reg_all_tg_pt)
  3886. buf[off] = 0x02;
  3887. /*
  3888. * The struct se_lun pointer will be present for the
  3889. * reservation holder for PR_HOLDER bit.
  3890. *
  3891. * Also, if this registration is the reservation
  3892. * holder, fill in SCOPE and TYPE in the next byte.
  3893. */
  3894. if (pr_reg->pr_res_holder) {
  3895. buf[off++] |= 0x01;
  3896. buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
  3897. (pr_reg->pr_res_type & 0x0f);
  3898. } else
  3899. off += 2;
  3900. off += 4; /* Skip over reserved area */
  3901. /*
  3902. * From spc4r17 6.3.15:
  3903. *
  3904. * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
  3905. * IDENTIFIER field contains the relative port identifier (see
  3906. * 3.1.120) of the target port that is part of the I_T nexus
  3907. * described by this full status descriptor. If the ALL_TG_PT
  3908. * bit is set to one, the contents of the RELATIVE TARGET PORT
  3909. * IDENTIFIER field are not defined by this standard.
  3910. */
  3911. if (!pr_reg->pr_reg_all_tg_pt) {
  3912. struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
  3913. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  3914. buf[off++] = (port->sep_rtpi & 0xff);
  3915. } else
  3916. off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
  3917. /*
  3918. * Now, have the $FABRIC_MOD fill in the protocol identifier
  3919. */
  3920. desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
  3921. se_nacl, pr_reg, &format_code, &buf[off+4]);
  3922. spin_lock(&pr_tmpl->registration_lock);
  3923. atomic_dec(&pr_reg->pr_res_holders);
  3924. smp_mb__after_atomic_dec();
  3925. /*
  3926. * Set the ADDITIONAL DESCRIPTOR LENGTH
  3927. */
  3928. buf[off++] = ((desc_len >> 24) & 0xff);
  3929. buf[off++] = ((desc_len >> 16) & 0xff);
  3930. buf[off++] = ((desc_len >> 8) & 0xff);
  3931. buf[off++] = (desc_len & 0xff);
  3932. /*
  3933. * Size of full desctipor header minus TransportID
  3934. * containing $FABRIC_MOD specific) initiator device/port
  3935. * WWN information.
  3936. *
  3937. * See spc4r17 Section 6.13.5 Table 169
  3938. */
  3939. add_desc_len = (24 + desc_len);
  3940. off += desc_len;
  3941. add_len += add_desc_len;
  3942. }
  3943. spin_unlock(&pr_tmpl->registration_lock);
  3944. /*
  3945. * Set ADDITIONAL_LENGTH
  3946. */
  3947. buf[4] = ((add_len >> 24) & 0xff);
  3948. buf[5] = ((add_len >> 16) & 0xff);
  3949. buf[6] = ((add_len >> 8) & 0xff);
  3950. buf[7] = (add_len & 0xff);
  3951. transport_kunmap_data_sg(cmd);
  3952. return 0;
  3953. }
  3954. int target_scsi3_emulate_pr_in(struct se_cmd *cmd)
  3955. {
  3956. int ret;
  3957. /*
  3958. * Following spc2r20 5.5.1 Reservations overview:
  3959. *
  3960. * If a logical unit has been reserved by any RESERVE command and is
  3961. * still reserved by any initiator, all PERSISTENT RESERVE IN and all
  3962. * PERSISTENT RESERVE OUT commands shall conflict regardless of
  3963. * initiator or service action and shall terminate with a RESERVATION
  3964. * CONFLICT status.
  3965. */
  3966. if (cmd->se_dev->dev_flags & DF_SPC2_RESERVATIONS) {
  3967. pr_err("Received PERSISTENT_RESERVE CDB while legacy"
  3968. " SPC-2 reservation is held, returning"
  3969. " RESERVATION_CONFLICT\n");
  3970. cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
  3971. return -EINVAL;
  3972. }
  3973. switch (cmd->t_task_cdb[1] & 0x1f) {
  3974. case PRI_READ_KEYS:
  3975. ret = core_scsi3_pri_read_keys(cmd);
  3976. break;
  3977. case PRI_READ_RESERVATION:
  3978. ret = core_scsi3_pri_read_reservation(cmd);
  3979. break;
  3980. case PRI_REPORT_CAPABILITIES:
  3981. ret = core_scsi3_pri_report_capabilities(cmd);
  3982. break;
  3983. case PRI_READ_FULL_STATUS:
  3984. ret = core_scsi3_pri_read_full_status(cmd);
  3985. break;
  3986. default:
  3987. pr_err("Unknown PERSISTENT_RESERVE_IN service"
  3988. " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
  3989. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  3990. ret = -EINVAL;
  3991. break;
  3992. }
  3993. if (!ret)
  3994. target_complete_cmd(cmd, GOOD);
  3995. return ret;
  3996. }
  3997. static int core_pt_reservation_check(struct se_cmd *cmd, u32 *pr_res_type)
  3998. {
  3999. return 0;
  4000. }
  4001. static int core_pt_seq_non_holder(
  4002. struct se_cmd *cmd,
  4003. unsigned char *cdb,
  4004. u32 pr_reg_type)
  4005. {
  4006. return 0;
  4007. }
  4008. int core_setup_reservations(struct se_device *dev, int force_pt)
  4009. {
  4010. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  4011. struct t10_reservation *rest = &su_dev->t10_pr;
  4012. /*
  4013. * If this device is from Target_Core_Mod/pSCSI, use the reservations
  4014. * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
  4015. * cause a problem because libata and some SATA RAID HBAs appear
  4016. * under Linux/SCSI, but to emulate reservations themselves.
  4017. */
  4018. if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  4019. !(dev->se_sub_dev->se_dev_attrib.emulate_reservations)) || force_pt) {
  4020. rest->res_type = SPC_PASSTHROUGH;
  4021. rest->pr_ops.t10_reservation_check = &core_pt_reservation_check;
  4022. rest->pr_ops.t10_seq_non_holder = &core_pt_seq_non_holder;
  4023. pr_debug("%s: Using SPC_PASSTHROUGH, no reservation"
  4024. " emulation\n", dev->transport->name);
  4025. return 0;
  4026. }
  4027. /*
  4028. * If SPC-3 or above is reported by real or emulated struct se_device,
  4029. * use emulated Persistent Reservations.
  4030. */
  4031. if (dev->transport->get_device_rev(dev) >= SCSI_3) {
  4032. rest->res_type = SPC3_PERSISTENT_RESERVATIONS;
  4033. rest->pr_ops.t10_reservation_check = &core_scsi3_pr_reservation_check;
  4034. rest->pr_ops.t10_seq_non_holder = &core_scsi3_pr_seq_non_holder;
  4035. pr_debug("%s: Using SPC3_PERSISTENT_RESERVATIONS"
  4036. " emulation\n", dev->transport->name);
  4037. } else {
  4038. rest->res_type = SPC2_RESERVATIONS;
  4039. rest->pr_ops.t10_reservation_check = &core_scsi2_reservation_check;
  4040. rest->pr_ops.t10_seq_non_holder =
  4041. &core_scsi2_reservation_seq_non_holder;
  4042. pr_debug("%s: Using SPC2_RESERVATIONS emulation\n",
  4043. dev->transport->name);
  4044. }
  4045. return 0;
  4046. }