target_core_pr.c 126 KB

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