target_core_pr.c 124 KB

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