target_core_pr.c 125 KB

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