target_core_pr.c 122 KB

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