target_core_pr.c 124 KB

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