target_core_pr.c 126 KB

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