target_core_pr.c 130 KB

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