target_core_pr.c 132 KB

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