target_core_alua.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /*******************************************************************************
  2. * Filename: target_core_alua.c
  3. *
  4. * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
  5. *
  6. * Copyright (c) 2009-2010 Rising Tide Systems
  7. * Copyright (c) 2009-2010 Linux-iSCSI.org
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. ******************************************************************************/
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/configfs.h>
  29. #include <linux/export.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <asm/unaligned.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_backend.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_configfs.h>
  37. #include "target_core_internal.h"
  38. #include "target_core_alua.h"
  39. #include "target_core_ua.h"
  40. static int core_alua_check_transition(int state, int *primary);
  41. static int core_alua_set_tg_pt_secondary_state(
  42. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  43. struct se_port *port, int explict, int offline);
  44. static u16 alua_lu_gps_counter;
  45. static u32 alua_lu_gps_count;
  46. static DEFINE_SPINLOCK(lu_gps_lock);
  47. static LIST_HEAD(lu_gps_list);
  48. struct t10_alua_lu_gp *default_lu_gp;
  49. /*
  50. * REPORT_TARGET_PORT_GROUPS
  51. *
  52. * See spc4r17 section 6.27
  53. */
  54. int target_emulate_report_target_port_groups(struct se_cmd *cmd)
  55. {
  56. struct se_device *dev = cmd->se_dev;
  57. struct se_port *port;
  58. struct t10_alua_tg_pt_gp *tg_pt_gp;
  59. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  60. unsigned char *buf;
  61. u32 rd_len = 0, off;
  62. int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
  63. /*
  64. * Skip over RESERVED area to first Target port group descriptor
  65. * depending on the PARAMETER DATA FORMAT type..
  66. */
  67. if (ext_hdr != 0)
  68. off = 8;
  69. else
  70. off = 4;
  71. if (cmd->data_length < off) {
  72. pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
  73. " small for %s header\n", cmd->data_length,
  74. (ext_hdr) ? "extended" : "normal");
  75. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  76. return -EINVAL;
  77. }
  78. buf = transport_kmap_data_sg(cmd);
  79. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  80. list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
  81. tg_pt_gp_list) {
  82. /*
  83. * Check if the Target port group and Target port descriptor list
  84. * based on tg_pt_gp_members count will fit into the response payload.
  85. * Otherwise, bump rd_len to let the initiator know we have exceeded
  86. * the allocation length and the response is truncated.
  87. */
  88. if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
  89. cmd->data_length) {
  90. rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
  91. continue;
  92. }
  93. /*
  94. * PREF: Preferred target port bit, determine if this
  95. * bit should be set for port group.
  96. */
  97. if (tg_pt_gp->tg_pt_gp_pref)
  98. buf[off] = 0x80;
  99. /*
  100. * Set the ASYMMETRIC ACCESS State
  101. */
  102. buf[off++] |= (atomic_read(
  103. &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
  104. /*
  105. * Set supported ASYMMETRIC ACCESS State bits
  106. */
  107. buf[off] = 0x80; /* T_SUP */
  108. buf[off] |= 0x40; /* O_SUP */
  109. buf[off] |= 0x8; /* U_SUP */
  110. buf[off] |= 0x4; /* S_SUP */
  111. buf[off] |= 0x2; /* AN_SUP */
  112. buf[off++] |= 0x1; /* AO_SUP */
  113. /*
  114. * TARGET PORT GROUP
  115. */
  116. buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
  117. buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
  118. off++; /* Skip over Reserved */
  119. /*
  120. * STATUS CODE
  121. */
  122. buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
  123. /*
  124. * Vendor Specific field
  125. */
  126. buf[off++] = 0x00;
  127. /*
  128. * TARGET PORT COUNT
  129. */
  130. buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
  131. rd_len += 8;
  132. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  133. list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  134. tg_pt_gp_mem_list) {
  135. port = tg_pt_gp_mem->tg_pt;
  136. /*
  137. * Start Target Port descriptor format
  138. *
  139. * See spc4r17 section 6.2.7 Table 247
  140. */
  141. off += 2; /* Skip over Obsolete */
  142. /*
  143. * Set RELATIVE TARGET PORT IDENTIFIER
  144. */
  145. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  146. buf[off++] = (port->sep_rtpi & 0xff);
  147. rd_len += 4;
  148. }
  149. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  150. }
  151. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  152. /*
  153. * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
  154. */
  155. put_unaligned_be32(rd_len, &buf[0]);
  156. /*
  157. * Fill in the Extended header parameter data format if requested
  158. */
  159. if (ext_hdr != 0) {
  160. buf[4] = 0x10;
  161. /*
  162. * Set the implict transition time (in seconds) for the application
  163. * client to use as a base for it's transition timeout value.
  164. *
  165. * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
  166. * this CDB was received upon to determine this value individually
  167. * for ALUA target port group.
  168. */
  169. port = cmd->se_lun->lun_sep;
  170. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  171. if (tg_pt_gp_mem) {
  172. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  173. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  174. if (tg_pt_gp)
  175. buf[5] = tg_pt_gp->tg_pt_gp_implict_trans_secs;
  176. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  177. }
  178. }
  179. transport_kunmap_data_sg(cmd);
  180. target_complete_cmd(cmd, GOOD);
  181. return 0;
  182. }
  183. /*
  184. * SET_TARGET_PORT_GROUPS for explict ALUA operation.
  185. *
  186. * See spc4r17 section 6.35
  187. */
  188. int target_emulate_set_target_port_groups(struct se_cmd *cmd)
  189. {
  190. struct se_device *dev = cmd->se_dev;
  191. struct se_port *port, *l_port = cmd->se_lun->lun_sep;
  192. struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
  193. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
  194. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
  195. unsigned char *buf;
  196. unsigned char *ptr;
  197. u32 len = 4; /* Skip over RESERVED area in header */
  198. int alua_access_state, primary = 0, rc;
  199. u16 tg_pt_id, rtpi;
  200. if (!l_port) {
  201. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  202. return -EINVAL;
  203. }
  204. if (cmd->data_length < 4) {
  205. pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
  206. " small\n", cmd->data_length);
  207. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  208. return -EINVAL;
  209. }
  210. buf = transport_kmap_data_sg(cmd);
  211. /*
  212. * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
  213. * for the local tg_pt_gp.
  214. */
  215. l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
  216. if (!l_tg_pt_gp_mem) {
  217. pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
  218. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  219. rc = -EINVAL;
  220. goto out;
  221. }
  222. spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  223. l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
  224. if (!l_tg_pt_gp) {
  225. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  226. pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
  227. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  228. rc = -EINVAL;
  229. goto out;
  230. }
  231. rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
  232. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  233. if (!rc) {
  234. pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
  235. " while TPGS_EXPLICT_ALUA is disabled\n");
  236. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  237. rc = -EINVAL;
  238. goto out;
  239. }
  240. ptr = &buf[4]; /* Skip over RESERVED area in header */
  241. while (len < cmd->data_length) {
  242. alua_access_state = (ptr[0] & 0x0f);
  243. /*
  244. * Check the received ALUA access state, and determine if
  245. * the state is a primary or secondary target port asymmetric
  246. * access state.
  247. */
  248. rc = core_alua_check_transition(alua_access_state, &primary);
  249. if (rc != 0) {
  250. /*
  251. * If the SET TARGET PORT GROUPS attempts to establish
  252. * an invalid combination of target port asymmetric
  253. * access states or attempts to establish an
  254. * unsupported target port asymmetric access state,
  255. * then the command shall be terminated with CHECK
  256. * CONDITION status, with the sense key set to ILLEGAL
  257. * REQUEST, and the additional sense code set to INVALID
  258. * FIELD IN PARAMETER LIST.
  259. */
  260. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  261. rc = -EINVAL;
  262. goto out;
  263. }
  264. rc = -1;
  265. /*
  266. * If the ASYMMETRIC ACCESS STATE field (see table 267)
  267. * specifies a primary target port asymmetric access state,
  268. * then the TARGET PORT GROUP OR TARGET PORT field specifies
  269. * a primary target port group for which the primary target
  270. * port asymmetric access state shall be changed. If the
  271. * ASYMMETRIC ACCESS STATE field specifies a secondary target
  272. * port asymmetric access state, then the TARGET PORT GROUP OR
  273. * TARGET PORT field specifies the relative target port
  274. * identifier (see 3.1.120) of the target port for which the
  275. * secondary target port asymmetric access state shall be
  276. * changed.
  277. */
  278. if (primary) {
  279. tg_pt_id = get_unaligned_be16(ptr + 2);
  280. /*
  281. * Locate the matching target port group ID from
  282. * the global tg_pt_gp list
  283. */
  284. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  285. list_for_each_entry(tg_pt_gp,
  286. &dev->t10_alua.tg_pt_gps_list,
  287. tg_pt_gp_list) {
  288. if (!tg_pt_gp->tg_pt_gp_valid_id)
  289. continue;
  290. if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
  291. continue;
  292. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  293. smp_mb__after_atomic_inc();
  294. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  295. rc = core_alua_do_port_transition(tg_pt_gp,
  296. dev, l_port, nacl,
  297. alua_access_state, 1);
  298. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  299. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  300. smp_mb__after_atomic_dec();
  301. break;
  302. }
  303. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  304. /*
  305. * If not matching target port group ID can be located
  306. * throw an exception with ASCQ: INVALID_PARAMETER_LIST
  307. */
  308. if (rc != 0) {
  309. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  310. rc = -EINVAL;
  311. goto out;
  312. }
  313. } else {
  314. /*
  315. * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
  316. * the Target Port in question for the the incoming
  317. * SET_TARGET_PORT_GROUPS op.
  318. */
  319. rtpi = get_unaligned_be16(ptr + 2);
  320. /*
  321. * Locate the matching relative target port identifier
  322. * for the struct se_device storage object.
  323. */
  324. spin_lock(&dev->se_port_lock);
  325. list_for_each_entry(port, &dev->dev_sep_list,
  326. sep_list) {
  327. if (port->sep_rtpi != rtpi)
  328. continue;
  329. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  330. spin_unlock(&dev->se_port_lock);
  331. rc = core_alua_set_tg_pt_secondary_state(
  332. tg_pt_gp_mem, port, 1, 1);
  333. spin_lock(&dev->se_port_lock);
  334. break;
  335. }
  336. spin_unlock(&dev->se_port_lock);
  337. /*
  338. * If not matching relative target port identifier can
  339. * be located, throw an exception with ASCQ:
  340. * INVALID_PARAMETER_LIST
  341. */
  342. if (rc != 0) {
  343. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  344. rc = -EINVAL;
  345. goto out;
  346. }
  347. }
  348. ptr += 4;
  349. len += 4;
  350. }
  351. out:
  352. transport_kunmap_data_sg(cmd);
  353. if (!rc)
  354. target_complete_cmd(cmd, GOOD);
  355. return rc;
  356. }
  357. static inline int core_alua_state_nonoptimized(
  358. struct se_cmd *cmd,
  359. unsigned char *cdb,
  360. int nonop_delay_msecs,
  361. u8 *alua_ascq)
  362. {
  363. /*
  364. * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
  365. * later to determine if processing of this cmd needs to be
  366. * temporarily delayed for the Active/NonOptimized primary access state.
  367. */
  368. cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
  369. cmd->alua_nonop_delay = nonop_delay_msecs;
  370. return 0;
  371. }
  372. static inline int core_alua_state_standby(
  373. struct se_cmd *cmd,
  374. unsigned char *cdb,
  375. u8 *alua_ascq)
  376. {
  377. /*
  378. * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
  379. * spc4r17 section 5.9.2.4.4
  380. */
  381. switch (cdb[0]) {
  382. case INQUIRY:
  383. case LOG_SELECT:
  384. case LOG_SENSE:
  385. case MODE_SELECT:
  386. case MODE_SENSE:
  387. case REPORT_LUNS:
  388. case RECEIVE_DIAGNOSTIC:
  389. case SEND_DIAGNOSTIC:
  390. case MAINTENANCE_IN:
  391. switch (cdb[1] & 0x1f) {
  392. case MI_REPORT_TARGET_PGS:
  393. return 0;
  394. default:
  395. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  396. return 1;
  397. }
  398. case MAINTENANCE_OUT:
  399. switch (cdb[1]) {
  400. case MO_SET_TARGET_PGS:
  401. return 0;
  402. default:
  403. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  404. return 1;
  405. }
  406. case REQUEST_SENSE:
  407. case PERSISTENT_RESERVE_IN:
  408. case PERSISTENT_RESERVE_OUT:
  409. case READ_BUFFER:
  410. case WRITE_BUFFER:
  411. return 0;
  412. default:
  413. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  414. return 1;
  415. }
  416. return 0;
  417. }
  418. static inline int core_alua_state_unavailable(
  419. struct se_cmd *cmd,
  420. unsigned char *cdb,
  421. u8 *alua_ascq)
  422. {
  423. /*
  424. * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
  425. * spc4r17 section 5.9.2.4.5
  426. */
  427. switch (cdb[0]) {
  428. case INQUIRY:
  429. case REPORT_LUNS:
  430. case MAINTENANCE_IN:
  431. switch (cdb[1] & 0x1f) {
  432. case MI_REPORT_TARGET_PGS:
  433. return 0;
  434. default:
  435. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  436. return 1;
  437. }
  438. case MAINTENANCE_OUT:
  439. switch (cdb[1]) {
  440. case MO_SET_TARGET_PGS:
  441. return 0;
  442. default:
  443. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  444. return 1;
  445. }
  446. case REQUEST_SENSE:
  447. case READ_BUFFER:
  448. case WRITE_BUFFER:
  449. return 0;
  450. default:
  451. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  452. return 1;
  453. }
  454. return 0;
  455. }
  456. static inline int core_alua_state_transition(
  457. struct se_cmd *cmd,
  458. unsigned char *cdb,
  459. u8 *alua_ascq)
  460. {
  461. /*
  462. * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
  463. * spc4r17 section 5.9.2.5
  464. */
  465. switch (cdb[0]) {
  466. case INQUIRY:
  467. case REPORT_LUNS:
  468. case MAINTENANCE_IN:
  469. switch (cdb[1] & 0x1f) {
  470. case MI_REPORT_TARGET_PGS:
  471. return 0;
  472. default:
  473. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  474. return 1;
  475. }
  476. case REQUEST_SENSE:
  477. case READ_BUFFER:
  478. case WRITE_BUFFER:
  479. return 0;
  480. default:
  481. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  482. return 1;
  483. }
  484. return 0;
  485. }
  486. /*
  487. * return 1: Is used to signal LUN not accecsable, and check condition/not ready
  488. * return 0: Used to signal success
  489. * reutrn -1: Used to signal failure, and invalid cdb field
  490. */
  491. int target_alua_state_check(struct se_cmd *cmd)
  492. {
  493. struct se_device *dev = cmd->se_dev;
  494. unsigned char *cdb = cmd->t_task_cdb;
  495. struct se_lun *lun = cmd->se_lun;
  496. struct se_port *port = lun->lun_sep;
  497. struct t10_alua_tg_pt_gp *tg_pt_gp;
  498. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  499. int out_alua_state, nonop_delay_msecs;
  500. u8 alua_ascq;
  501. int ret;
  502. if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
  503. return 0;
  504. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
  505. return 0;
  506. if (!port)
  507. return 0;
  508. /*
  509. * First, check for a struct se_port specific secondary ALUA target port
  510. * access state: OFFLINE
  511. */
  512. if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
  513. pr_debug("ALUA: Got secondary offline status for local"
  514. " target port\n");
  515. alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  516. ret = 1;
  517. goto out;
  518. }
  519. /*
  520. * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
  521. * ALUA target port group, to obtain current ALUA access state.
  522. * Otherwise look for the underlying struct se_device association with
  523. * a ALUA logical unit group.
  524. */
  525. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  526. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  527. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  528. out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  529. nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
  530. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  531. /*
  532. * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
  533. * statement so the compiler knows explicitly to check this case first.
  534. * For the Optimized ALUA access state case, we want to process the
  535. * incoming fabric cmd ASAP..
  536. */
  537. if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
  538. return 0;
  539. switch (out_alua_state) {
  540. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  541. ret = core_alua_state_nonoptimized(cmd, cdb,
  542. nonop_delay_msecs, &alua_ascq);
  543. break;
  544. case ALUA_ACCESS_STATE_STANDBY:
  545. ret = core_alua_state_standby(cmd, cdb, &alua_ascq);
  546. break;
  547. case ALUA_ACCESS_STATE_UNAVAILABLE:
  548. ret = core_alua_state_unavailable(cmd, cdb, &alua_ascq);
  549. break;
  550. case ALUA_ACCESS_STATE_TRANSITION:
  551. ret = core_alua_state_transition(cmd, cdb, &alua_ascq);
  552. break;
  553. /*
  554. * OFFLINE is a secondary ALUA target port group access state, that is
  555. * handled above with struct se_port->sep_tg_pt_secondary_offline=1
  556. */
  557. case ALUA_ACCESS_STATE_OFFLINE:
  558. default:
  559. pr_err("Unknown ALUA access state: 0x%02x\n",
  560. out_alua_state);
  561. ret = -EINVAL;
  562. break;
  563. }
  564. out:
  565. if (ret > 0) {
  566. /*
  567. * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
  568. * The ALUA additional sense code qualifier (ASCQ) is determined
  569. * by the ALUA primary or secondary access state..
  570. */
  571. pr_debug("[%s]: ALUA TG Port not available, "
  572. "SenseKey: NOT_READY, ASC/ASCQ: "
  573. "0x04/0x%02x\n",
  574. cmd->se_tfo->get_fabric_name(), alua_ascq);
  575. cmd->scsi_asc = 0x04;
  576. cmd->scsi_ascq = alua_ascq;
  577. }
  578. return ret;
  579. }
  580. /*
  581. * Check implict and explict ALUA state change request.
  582. */
  583. static int core_alua_check_transition(int state, int *primary)
  584. {
  585. switch (state) {
  586. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  587. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  588. case ALUA_ACCESS_STATE_STANDBY:
  589. case ALUA_ACCESS_STATE_UNAVAILABLE:
  590. /*
  591. * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
  592. * defined as primary target port asymmetric access states.
  593. */
  594. *primary = 1;
  595. break;
  596. case ALUA_ACCESS_STATE_OFFLINE:
  597. /*
  598. * OFFLINE state is defined as a secondary target port
  599. * asymmetric access state.
  600. */
  601. *primary = 0;
  602. break;
  603. default:
  604. pr_err("Unknown ALUA access state: 0x%02x\n", state);
  605. return -EINVAL;
  606. }
  607. return 0;
  608. }
  609. static char *core_alua_dump_state(int state)
  610. {
  611. switch (state) {
  612. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  613. return "Active/Optimized";
  614. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  615. return "Active/NonOptimized";
  616. case ALUA_ACCESS_STATE_STANDBY:
  617. return "Standby";
  618. case ALUA_ACCESS_STATE_UNAVAILABLE:
  619. return "Unavailable";
  620. case ALUA_ACCESS_STATE_OFFLINE:
  621. return "Offline";
  622. default:
  623. return "Unknown";
  624. }
  625. return NULL;
  626. }
  627. char *core_alua_dump_status(int status)
  628. {
  629. switch (status) {
  630. case ALUA_STATUS_NONE:
  631. return "None";
  632. case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
  633. return "Altered by Explict STPG";
  634. case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
  635. return "Altered by Implict ALUA";
  636. default:
  637. return "Unknown";
  638. }
  639. return NULL;
  640. }
  641. /*
  642. * Used by fabric modules to determine when we need to delay processing
  643. * for the Active/NonOptimized paths..
  644. */
  645. int core_alua_check_nonop_delay(
  646. struct se_cmd *cmd)
  647. {
  648. if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
  649. return 0;
  650. if (in_interrupt())
  651. return 0;
  652. /*
  653. * The ALUA Active/NonOptimized access state delay can be disabled
  654. * in via configfs with a value of zero
  655. */
  656. if (!cmd->alua_nonop_delay)
  657. return 0;
  658. /*
  659. * struct se_cmd->alua_nonop_delay gets set by a target port group
  660. * defined interval in core_alua_state_nonoptimized()
  661. */
  662. msleep_interruptible(cmd->alua_nonop_delay);
  663. return 0;
  664. }
  665. EXPORT_SYMBOL(core_alua_check_nonop_delay);
  666. /*
  667. * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
  668. *
  669. */
  670. static int core_alua_write_tpg_metadata(
  671. const char *path,
  672. unsigned char *md_buf,
  673. u32 md_buf_len)
  674. {
  675. mm_segment_t old_fs;
  676. struct file *file;
  677. struct iovec iov[1];
  678. int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
  679. memset(iov, 0, sizeof(struct iovec));
  680. file = filp_open(path, flags, 0600);
  681. if (IS_ERR(file) || !file || !file->f_dentry) {
  682. pr_err("filp_open(%s) for ALUA metadata failed\n",
  683. path);
  684. return -ENODEV;
  685. }
  686. iov[0].iov_base = &md_buf[0];
  687. iov[0].iov_len = md_buf_len;
  688. old_fs = get_fs();
  689. set_fs(get_ds());
  690. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  691. set_fs(old_fs);
  692. if (ret < 0) {
  693. pr_err("Error writing ALUA metadata file: %s\n", path);
  694. filp_close(file, NULL);
  695. return -EIO;
  696. }
  697. filp_close(file, NULL);
  698. return 0;
  699. }
  700. /*
  701. * Called with tg_pt_gp->tg_pt_gp_md_mutex held
  702. */
  703. static int core_alua_update_tpg_primary_metadata(
  704. struct t10_alua_tg_pt_gp *tg_pt_gp,
  705. int primary_state,
  706. unsigned char *md_buf)
  707. {
  708. struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
  709. char path[ALUA_METADATA_PATH_LEN];
  710. int len;
  711. memset(path, 0, ALUA_METADATA_PATH_LEN);
  712. len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
  713. "tg_pt_gp_id=%hu\n"
  714. "alua_access_state=0x%02x\n"
  715. "alua_access_status=0x%02x\n",
  716. tg_pt_gp->tg_pt_gp_id, primary_state,
  717. tg_pt_gp->tg_pt_gp_alua_access_status);
  718. snprintf(path, ALUA_METADATA_PATH_LEN,
  719. "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
  720. config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
  721. return core_alua_write_tpg_metadata(path, md_buf, len);
  722. }
  723. static int core_alua_do_transition_tg_pt(
  724. struct t10_alua_tg_pt_gp *tg_pt_gp,
  725. struct se_port *l_port,
  726. struct se_node_acl *nacl,
  727. unsigned char *md_buf,
  728. int new_state,
  729. int explict)
  730. {
  731. struct se_dev_entry *se_deve;
  732. struct se_lun_acl *lacl;
  733. struct se_port *port;
  734. struct t10_alua_tg_pt_gp_member *mem;
  735. int old_state = 0;
  736. /*
  737. * Save the old primary ALUA access state, and set the current state
  738. * to ALUA_ACCESS_STATE_TRANSITION.
  739. */
  740. old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  741. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  742. ALUA_ACCESS_STATE_TRANSITION);
  743. tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
  744. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  745. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  746. /*
  747. * Check for the optional ALUA primary state transition delay
  748. */
  749. if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
  750. msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  751. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  752. list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
  753. tg_pt_gp_mem_list) {
  754. port = mem->tg_pt;
  755. /*
  756. * After an implicit target port asymmetric access state
  757. * change, a device server shall establish a unit attention
  758. * condition for the initiator port associated with every I_T
  759. * nexus with the additional sense code set to ASYMMETRIC
  760. * ACCESS STATE CHAGED.
  761. *
  762. * After an explicit target port asymmetric access state
  763. * change, a device server shall establish a unit attention
  764. * condition with the additional sense code set to ASYMMETRIC
  765. * ACCESS STATE CHANGED for the initiator port associated with
  766. * every I_T nexus other than the I_T nexus on which the SET
  767. * TARGET PORT GROUPS command
  768. */
  769. atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
  770. smp_mb__after_atomic_inc();
  771. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  772. spin_lock_bh(&port->sep_alua_lock);
  773. list_for_each_entry(se_deve, &port->sep_alua_list,
  774. alua_port_list) {
  775. lacl = se_deve->se_lun_acl;
  776. /*
  777. * se_deve->se_lun_acl pointer may be NULL for a
  778. * entry created without explict Node+MappedLUN ACLs
  779. */
  780. if (!lacl)
  781. continue;
  782. if (explict &&
  783. (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
  784. (l_port != NULL) && (l_port == port))
  785. continue;
  786. core_scsi3_ua_allocate(lacl->se_lun_nacl,
  787. se_deve->mapped_lun, 0x2A,
  788. ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
  789. }
  790. spin_unlock_bh(&port->sep_alua_lock);
  791. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  792. atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
  793. smp_mb__after_atomic_dec();
  794. }
  795. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  796. /*
  797. * Update the ALUA metadata buf that has been allocated in
  798. * core_alua_do_port_transition(), this metadata will be written
  799. * to struct file.
  800. *
  801. * Note that there is the case where we do not want to update the
  802. * metadata when the saved metadata is being parsed in userspace
  803. * when setting the existing port access state and access status.
  804. *
  805. * Also note that the failure to write out the ALUA metadata to
  806. * struct file does NOT affect the actual ALUA transition.
  807. */
  808. if (tg_pt_gp->tg_pt_gp_write_metadata) {
  809. mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
  810. core_alua_update_tpg_primary_metadata(tg_pt_gp,
  811. new_state, md_buf);
  812. mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
  813. }
  814. /*
  815. * Set the current primary ALUA access state to the requested new state
  816. */
  817. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
  818. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  819. " from primary access state %s to %s\n", (explict) ? "explict" :
  820. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  821. tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
  822. core_alua_dump_state(new_state));
  823. return 0;
  824. }
  825. int core_alua_do_port_transition(
  826. struct t10_alua_tg_pt_gp *l_tg_pt_gp,
  827. struct se_device *l_dev,
  828. struct se_port *l_port,
  829. struct se_node_acl *l_nacl,
  830. int new_state,
  831. int explict)
  832. {
  833. struct se_device *dev;
  834. struct se_port *port;
  835. struct se_node_acl *nacl;
  836. struct t10_alua_lu_gp *lu_gp;
  837. struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
  838. struct t10_alua_tg_pt_gp *tg_pt_gp;
  839. unsigned char *md_buf;
  840. int primary;
  841. if (core_alua_check_transition(new_state, &primary) != 0)
  842. return -EINVAL;
  843. md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
  844. if (!md_buf) {
  845. pr_err("Unable to allocate buf for ALUA metadata\n");
  846. return -ENOMEM;
  847. }
  848. local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
  849. spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
  850. lu_gp = local_lu_gp_mem->lu_gp;
  851. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  852. smp_mb__after_atomic_inc();
  853. spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
  854. /*
  855. * For storage objects that are members of the 'default_lu_gp',
  856. * we only do transition on the passed *l_tp_pt_gp, and not
  857. * on all of the matching target port groups IDs in default_lu_gp.
  858. */
  859. if (!lu_gp->lu_gp_id) {
  860. /*
  861. * core_alua_do_transition_tg_pt() will always return
  862. * success.
  863. */
  864. core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
  865. md_buf, new_state, explict);
  866. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  867. smp_mb__after_atomic_dec();
  868. kfree(md_buf);
  869. return 0;
  870. }
  871. /*
  872. * For all other LU groups aside from 'default_lu_gp', walk all of
  873. * the associated storage objects looking for a matching target port
  874. * group ID from the local target port group.
  875. */
  876. spin_lock(&lu_gp->lu_gp_lock);
  877. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
  878. lu_gp_mem_list) {
  879. dev = lu_gp_mem->lu_gp_mem_dev;
  880. atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
  881. smp_mb__after_atomic_inc();
  882. spin_unlock(&lu_gp->lu_gp_lock);
  883. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  884. list_for_each_entry(tg_pt_gp,
  885. &dev->t10_alua.tg_pt_gps_list,
  886. tg_pt_gp_list) {
  887. if (!tg_pt_gp->tg_pt_gp_valid_id)
  888. continue;
  889. /*
  890. * If the target behavior port asymmetric access state
  891. * is changed for any target port group accessiable via
  892. * a logical unit within a LU group, the target port
  893. * behavior group asymmetric access states for the same
  894. * target port group accessible via other logical units
  895. * in that LU group will also change.
  896. */
  897. if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
  898. continue;
  899. if (l_tg_pt_gp == tg_pt_gp) {
  900. port = l_port;
  901. nacl = l_nacl;
  902. } else {
  903. port = NULL;
  904. nacl = NULL;
  905. }
  906. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  907. smp_mb__after_atomic_inc();
  908. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  909. /*
  910. * core_alua_do_transition_tg_pt() will always return
  911. * success.
  912. */
  913. core_alua_do_transition_tg_pt(tg_pt_gp, port,
  914. nacl, md_buf, new_state, explict);
  915. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  916. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  917. smp_mb__after_atomic_dec();
  918. }
  919. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  920. spin_lock(&lu_gp->lu_gp_lock);
  921. atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
  922. smp_mb__after_atomic_dec();
  923. }
  924. spin_unlock(&lu_gp->lu_gp_lock);
  925. pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
  926. " Group IDs: %hu %s transition to primary state: %s\n",
  927. config_item_name(&lu_gp->lu_gp_group.cg_item),
  928. l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
  929. core_alua_dump_state(new_state));
  930. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  931. smp_mb__after_atomic_dec();
  932. kfree(md_buf);
  933. return 0;
  934. }
  935. /*
  936. * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
  937. */
  938. static int core_alua_update_tpg_secondary_metadata(
  939. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  940. struct se_port *port,
  941. unsigned char *md_buf,
  942. u32 md_buf_len)
  943. {
  944. struct se_portal_group *se_tpg = port->sep_tpg;
  945. char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
  946. int len;
  947. memset(path, 0, ALUA_METADATA_PATH_LEN);
  948. memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
  949. len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
  950. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
  951. if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
  952. snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
  953. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  954. len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
  955. "alua_tg_pt_status=0x%02x\n",
  956. atomic_read(&port->sep_tg_pt_secondary_offline),
  957. port->sep_tg_pt_secondary_stat);
  958. snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
  959. se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
  960. port->sep_lun->unpacked_lun);
  961. return core_alua_write_tpg_metadata(path, md_buf, len);
  962. }
  963. static int core_alua_set_tg_pt_secondary_state(
  964. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  965. struct se_port *port,
  966. int explict,
  967. int offline)
  968. {
  969. struct t10_alua_tg_pt_gp *tg_pt_gp;
  970. unsigned char *md_buf;
  971. u32 md_buf_len;
  972. int trans_delay_msecs;
  973. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  974. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  975. if (!tg_pt_gp) {
  976. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  977. pr_err("Unable to complete secondary state"
  978. " transition\n");
  979. return -EINVAL;
  980. }
  981. trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
  982. /*
  983. * Set the secondary ALUA target port access state to OFFLINE
  984. * or release the previously secondary state for struct se_port
  985. */
  986. if (offline)
  987. atomic_set(&port->sep_tg_pt_secondary_offline, 1);
  988. else
  989. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  990. md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
  991. port->sep_tg_pt_secondary_stat = (explict) ?
  992. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  993. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  994. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  995. " to secondary access state: %s\n", (explict) ? "explict" :
  996. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  997. tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
  998. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  999. /*
  1000. * Do the optional transition delay after we set the secondary
  1001. * ALUA access state.
  1002. */
  1003. if (trans_delay_msecs != 0)
  1004. msleep_interruptible(trans_delay_msecs);
  1005. /*
  1006. * See if we need to update the ALUA fabric port metadata for
  1007. * secondary state and status
  1008. */
  1009. if (port->sep_tg_pt_secondary_write_md) {
  1010. md_buf = kzalloc(md_buf_len, GFP_KERNEL);
  1011. if (!md_buf) {
  1012. pr_err("Unable to allocate md_buf for"
  1013. " secondary ALUA access metadata\n");
  1014. return -ENOMEM;
  1015. }
  1016. mutex_lock(&port->sep_tg_pt_md_mutex);
  1017. core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
  1018. md_buf, md_buf_len);
  1019. mutex_unlock(&port->sep_tg_pt_md_mutex);
  1020. kfree(md_buf);
  1021. }
  1022. return 0;
  1023. }
  1024. struct t10_alua_lu_gp *
  1025. core_alua_allocate_lu_gp(const char *name, int def_group)
  1026. {
  1027. struct t10_alua_lu_gp *lu_gp;
  1028. lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
  1029. if (!lu_gp) {
  1030. pr_err("Unable to allocate struct t10_alua_lu_gp\n");
  1031. return ERR_PTR(-ENOMEM);
  1032. }
  1033. INIT_LIST_HEAD(&lu_gp->lu_gp_node);
  1034. INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
  1035. spin_lock_init(&lu_gp->lu_gp_lock);
  1036. atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
  1037. if (def_group) {
  1038. lu_gp->lu_gp_id = alua_lu_gps_counter++;
  1039. lu_gp->lu_gp_valid_id = 1;
  1040. alua_lu_gps_count++;
  1041. }
  1042. return lu_gp;
  1043. }
  1044. int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
  1045. {
  1046. struct t10_alua_lu_gp *lu_gp_tmp;
  1047. u16 lu_gp_id_tmp;
  1048. /*
  1049. * The lu_gp->lu_gp_id may only be set once..
  1050. */
  1051. if (lu_gp->lu_gp_valid_id) {
  1052. pr_warn("ALUA LU Group already has a valid ID,"
  1053. " ignoring request\n");
  1054. return -EINVAL;
  1055. }
  1056. spin_lock(&lu_gps_lock);
  1057. if (alua_lu_gps_count == 0x0000ffff) {
  1058. pr_err("Maximum ALUA alua_lu_gps_count:"
  1059. " 0x0000ffff reached\n");
  1060. spin_unlock(&lu_gps_lock);
  1061. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1062. return -ENOSPC;
  1063. }
  1064. again:
  1065. lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
  1066. alua_lu_gps_counter++;
  1067. list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
  1068. if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
  1069. if (!lu_gp_id)
  1070. goto again;
  1071. pr_warn("ALUA Logical Unit Group ID: %hu"
  1072. " already exists, ignoring request\n",
  1073. lu_gp_id);
  1074. spin_unlock(&lu_gps_lock);
  1075. return -EINVAL;
  1076. }
  1077. }
  1078. lu_gp->lu_gp_id = lu_gp_id_tmp;
  1079. lu_gp->lu_gp_valid_id = 1;
  1080. list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
  1081. alua_lu_gps_count++;
  1082. spin_unlock(&lu_gps_lock);
  1083. return 0;
  1084. }
  1085. static struct t10_alua_lu_gp_member *
  1086. core_alua_allocate_lu_gp_mem(struct se_device *dev)
  1087. {
  1088. struct t10_alua_lu_gp_member *lu_gp_mem;
  1089. lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
  1090. if (!lu_gp_mem) {
  1091. pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
  1092. return ERR_PTR(-ENOMEM);
  1093. }
  1094. INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
  1095. spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
  1096. atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
  1097. lu_gp_mem->lu_gp_mem_dev = dev;
  1098. dev->dev_alua_lu_gp_mem = lu_gp_mem;
  1099. return lu_gp_mem;
  1100. }
  1101. void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
  1102. {
  1103. struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
  1104. /*
  1105. * Once we have reached this point, config_item_put() has
  1106. * already been called from target_core_alua_drop_lu_gp().
  1107. *
  1108. * Here, we remove the *lu_gp from the global list so that
  1109. * no associations can be made while we are releasing
  1110. * struct t10_alua_lu_gp.
  1111. */
  1112. spin_lock(&lu_gps_lock);
  1113. list_del(&lu_gp->lu_gp_node);
  1114. alua_lu_gps_count--;
  1115. spin_unlock(&lu_gps_lock);
  1116. /*
  1117. * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
  1118. * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
  1119. * released with core_alua_put_lu_gp_from_name()
  1120. */
  1121. while (atomic_read(&lu_gp->lu_gp_ref_cnt))
  1122. cpu_relax();
  1123. /*
  1124. * Release reference to struct t10_alua_lu_gp * from all associated
  1125. * struct se_device.
  1126. */
  1127. spin_lock(&lu_gp->lu_gp_lock);
  1128. list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
  1129. &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1130. if (lu_gp_mem->lu_gp_assoc) {
  1131. list_del(&lu_gp_mem->lu_gp_mem_list);
  1132. lu_gp->lu_gp_members--;
  1133. lu_gp_mem->lu_gp_assoc = 0;
  1134. }
  1135. spin_unlock(&lu_gp->lu_gp_lock);
  1136. /*
  1137. *
  1138. * lu_gp_mem is associated with a single
  1139. * struct se_device->dev_alua_lu_gp_mem, and is released when
  1140. * struct se_device is released via core_alua_free_lu_gp_mem().
  1141. *
  1142. * If the passed lu_gp does NOT match the default_lu_gp, assume
  1143. * we want to re-assocate a given lu_gp_mem with default_lu_gp.
  1144. */
  1145. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1146. if (lu_gp != default_lu_gp)
  1147. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1148. default_lu_gp);
  1149. else
  1150. lu_gp_mem->lu_gp = NULL;
  1151. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1152. spin_lock(&lu_gp->lu_gp_lock);
  1153. }
  1154. spin_unlock(&lu_gp->lu_gp_lock);
  1155. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1156. }
  1157. void core_alua_free_lu_gp_mem(struct se_device *dev)
  1158. {
  1159. struct t10_alua_lu_gp *lu_gp;
  1160. struct t10_alua_lu_gp_member *lu_gp_mem;
  1161. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1162. if (!lu_gp_mem)
  1163. return;
  1164. while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
  1165. cpu_relax();
  1166. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1167. lu_gp = lu_gp_mem->lu_gp;
  1168. if (lu_gp) {
  1169. spin_lock(&lu_gp->lu_gp_lock);
  1170. if (lu_gp_mem->lu_gp_assoc) {
  1171. list_del(&lu_gp_mem->lu_gp_mem_list);
  1172. lu_gp->lu_gp_members--;
  1173. lu_gp_mem->lu_gp_assoc = 0;
  1174. }
  1175. spin_unlock(&lu_gp->lu_gp_lock);
  1176. lu_gp_mem->lu_gp = NULL;
  1177. }
  1178. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1179. kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
  1180. }
  1181. struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
  1182. {
  1183. struct t10_alua_lu_gp *lu_gp;
  1184. struct config_item *ci;
  1185. spin_lock(&lu_gps_lock);
  1186. list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
  1187. if (!lu_gp->lu_gp_valid_id)
  1188. continue;
  1189. ci = &lu_gp->lu_gp_group.cg_item;
  1190. if (!strcmp(config_item_name(ci), name)) {
  1191. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  1192. spin_unlock(&lu_gps_lock);
  1193. return lu_gp;
  1194. }
  1195. }
  1196. spin_unlock(&lu_gps_lock);
  1197. return NULL;
  1198. }
  1199. void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
  1200. {
  1201. spin_lock(&lu_gps_lock);
  1202. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  1203. spin_unlock(&lu_gps_lock);
  1204. }
  1205. /*
  1206. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1207. */
  1208. void __core_alua_attach_lu_gp_mem(
  1209. struct t10_alua_lu_gp_member *lu_gp_mem,
  1210. struct t10_alua_lu_gp *lu_gp)
  1211. {
  1212. spin_lock(&lu_gp->lu_gp_lock);
  1213. lu_gp_mem->lu_gp = lu_gp;
  1214. lu_gp_mem->lu_gp_assoc = 1;
  1215. list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
  1216. lu_gp->lu_gp_members++;
  1217. spin_unlock(&lu_gp->lu_gp_lock);
  1218. }
  1219. /*
  1220. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1221. */
  1222. void __core_alua_drop_lu_gp_mem(
  1223. struct t10_alua_lu_gp_member *lu_gp_mem,
  1224. struct t10_alua_lu_gp *lu_gp)
  1225. {
  1226. spin_lock(&lu_gp->lu_gp_lock);
  1227. list_del(&lu_gp_mem->lu_gp_mem_list);
  1228. lu_gp_mem->lu_gp = NULL;
  1229. lu_gp_mem->lu_gp_assoc = 0;
  1230. lu_gp->lu_gp_members--;
  1231. spin_unlock(&lu_gp->lu_gp_lock);
  1232. }
  1233. struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
  1234. const char *name, int def_group)
  1235. {
  1236. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1237. tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
  1238. if (!tg_pt_gp) {
  1239. pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
  1240. return NULL;
  1241. }
  1242. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
  1243. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
  1244. mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
  1245. spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
  1246. atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
  1247. tg_pt_gp->tg_pt_gp_dev = dev;
  1248. tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
  1249. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  1250. ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
  1251. /*
  1252. * Enable both explict and implict ALUA support by default
  1253. */
  1254. tg_pt_gp->tg_pt_gp_alua_access_type =
  1255. TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
  1256. /*
  1257. * Set the default Active/NonOptimized Delay in milliseconds
  1258. */
  1259. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
  1260. tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
  1261. tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
  1262. if (def_group) {
  1263. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1264. tg_pt_gp->tg_pt_gp_id =
  1265. dev->t10_alua.alua_tg_pt_gps_counter++;
  1266. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1267. dev->t10_alua.alua_tg_pt_gps_count++;
  1268. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1269. &dev->t10_alua.tg_pt_gps_list);
  1270. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1271. }
  1272. return tg_pt_gp;
  1273. }
  1274. int core_alua_set_tg_pt_gp_id(
  1275. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1276. u16 tg_pt_gp_id)
  1277. {
  1278. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1279. struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
  1280. u16 tg_pt_gp_id_tmp;
  1281. /*
  1282. * The tg_pt_gp->tg_pt_gp_id may only be set once..
  1283. */
  1284. if (tg_pt_gp->tg_pt_gp_valid_id) {
  1285. pr_warn("ALUA TG PT Group already has a valid ID,"
  1286. " ignoring request\n");
  1287. return -EINVAL;
  1288. }
  1289. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1290. if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
  1291. pr_err("Maximum ALUA alua_tg_pt_gps_count:"
  1292. " 0x0000ffff reached\n");
  1293. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1294. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1295. return -ENOSPC;
  1296. }
  1297. again:
  1298. tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
  1299. dev->t10_alua.alua_tg_pt_gps_counter++;
  1300. list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
  1301. tg_pt_gp_list) {
  1302. if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
  1303. if (!tg_pt_gp_id)
  1304. goto again;
  1305. pr_err("ALUA Target Port Group ID: %hu already"
  1306. " exists, ignoring request\n", tg_pt_gp_id);
  1307. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1308. return -EINVAL;
  1309. }
  1310. }
  1311. tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
  1312. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1313. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1314. &dev->t10_alua.tg_pt_gps_list);
  1315. dev->t10_alua.alua_tg_pt_gps_count++;
  1316. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1317. return 0;
  1318. }
  1319. struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
  1320. struct se_port *port)
  1321. {
  1322. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1323. tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
  1324. GFP_KERNEL);
  1325. if (!tg_pt_gp_mem) {
  1326. pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
  1327. return ERR_PTR(-ENOMEM);
  1328. }
  1329. INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1330. spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1331. atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
  1332. tg_pt_gp_mem->tg_pt = port;
  1333. port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
  1334. return tg_pt_gp_mem;
  1335. }
  1336. void core_alua_free_tg_pt_gp(
  1337. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1338. {
  1339. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1340. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
  1341. /*
  1342. * Once we have reached this point, config_item_put() has already
  1343. * been called from target_core_alua_drop_tg_pt_gp().
  1344. *
  1345. * Here we remove *tg_pt_gp from the global list so that
  1346. * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
  1347. * can be made while we are releasing struct t10_alua_tg_pt_gp.
  1348. */
  1349. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1350. list_del(&tg_pt_gp->tg_pt_gp_list);
  1351. dev->t10_alua.alua_tg_pt_gps_counter--;
  1352. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1353. /*
  1354. * Allow a struct t10_alua_tg_pt_gp_member * referenced by
  1355. * core_alua_get_tg_pt_gp_by_name() in
  1356. * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
  1357. * to be released with core_alua_put_tg_pt_gp_from_name().
  1358. */
  1359. while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
  1360. cpu_relax();
  1361. /*
  1362. * Release reference to struct t10_alua_tg_pt_gp from all associated
  1363. * struct se_port.
  1364. */
  1365. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1366. list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
  1367. &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
  1368. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1369. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1370. tg_pt_gp->tg_pt_gp_members--;
  1371. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1372. }
  1373. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1374. /*
  1375. * tg_pt_gp_mem is associated with a single
  1376. * se_port->sep_alua_tg_pt_gp_mem, and is released via
  1377. * core_alua_free_tg_pt_gp_mem().
  1378. *
  1379. * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
  1380. * assume we want to re-assocate a given tg_pt_gp_mem with
  1381. * default_tg_pt_gp.
  1382. */
  1383. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1384. if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
  1385. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1386. dev->t10_alua.default_tg_pt_gp);
  1387. } else
  1388. tg_pt_gp_mem->tg_pt_gp = NULL;
  1389. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1390. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1391. }
  1392. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1393. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1394. }
  1395. void core_alua_free_tg_pt_gp_mem(struct se_port *port)
  1396. {
  1397. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1398. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1399. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1400. if (!tg_pt_gp_mem)
  1401. return;
  1402. while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
  1403. cpu_relax();
  1404. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1405. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1406. if (tg_pt_gp) {
  1407. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1408. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1409. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1410. tg_pt_gp->tg_pt_gp_members--;
  1411. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1412. }
  1413. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1414. tg_pt_gp_mem->tg_pt_gp = NULL;
  1415. }
  1416. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1417. kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
  1418. }
  1419. static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
  1420. struct se_device *dev, const char *name)
  1421. {
  1422. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1423. struct config_item *ci;
  1424. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1425. list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
  1426. tg_pt_gp_list) {
  1427. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1428. continue;
  1429. ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1430. if (!strcmp(config_item_name(ci), name)) {
  1431. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1432. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1433. return tg_pt_gp;
  1434. }
  1435. }
  1436. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1437. return NULL;
  1438. }
  1439. static void core_alua_put_tg_pt_gp_from_name(
  1440. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1441. {
  1442. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1443. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1444. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1445. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1446. }
  1447. /*
  1448. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1449. */
  1450. void __core_alua_attach_tg_pt_gp_mem(
  1451. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1452. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1453. {
  1454. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1455. tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
  1456. tg_pt_gp_mem->tg_pt_gp_assoc = 1;
  1457. list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
  1458. &tg_pt_gp->tg_pt_gp_mem_list);
  1459. tg_pt_gp->tg_pt_gp_members++;
  1460. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1461. }
  1462. /*
  1463. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1464. */
  1465. static void __core_alua_drop_tg_pt_gp_mem(
  1466. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1467. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1468. {
  1469. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1470. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1471. tg_pt_gp_mem->tg_pt_gp = NULL;
  1472. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1473. tg_pt_gp->tg_pt_gp_members--;
  1474. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1475. }
  1476. ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
  1477. {
  1478. struct config_item *tg_pt_ci;
  1479. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1480. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1481. ssize_t len = 0;
  1482. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1483. if (!tg_pt_gp_mem)
  1484. return len;
  1485. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1486. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1487. if (tg_pt_gp) {
  1488. tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1489. len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
  1490. " %hu\nTG Port Primary Access State: %s\nTG Port "
  1491. "Primary Access Status: %s\nTG Port Secondary Access"
  1492. " State: %s\nTG Port Secondary Access Status: %s\n",
  1493. config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
  1494. core_alua_dump_state(atomic_read(
  1495. &tg_pt_gp->tg_pt_gp_alua_access_state)),
  1496. core_alua_dump_status(
  1497. tg_pt_gp->tg_pt_gp_alua_access_status),
  1498. (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
  1499. "Offline" : "None",
  1500. core_alua_dump_status(port->sep_tg_pt_secondary_stat));
  1501. }
  1502. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1503. return len;
  1504. }
  1505. ssize_t core_alua_store_tg_pt_gp_info(
  1506. struct se_port *port,
  1507. const char *page,
  1508. size_t count)
  1509. {
  1510. struct se_portal_group *tpg;
  1511. struct se_lun *lun;
  1512. struct se_device *dev = port->sep_lun->lun_se_dev;
  1513. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
  1514. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1515. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1516. int move = 0;
  1517. tpg = port->sep_tpg;
  1518. lun = port->sep_lun;
  1519. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1520. if (!tg_pt_gp_mem)
  1521. return 0;
  1522. if (count > TG_PT_GROUP_NAME_BUF) {
  1523. pr_err("ALUA Target Port Group alias too large!\n");
  1524. return -EINVAL;
  1525. }
  1526. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1527. memcpy(buf, page, count);
  1528. /*
  1529. * Any ALUA target port group alias besides "NULL" means we will be
  1530. * making a new group association.
  1531. */
  1532. if (strcmp(strstrip(buf), "NULL")) {
  1533. /*
  1534. * core_alua_get_tg_pt_gp_by_name() will increment reference to
  1535. * struct t10_alua_tg_pt_gp. This reference is released with
  1536. * core_alua_put_tg_pt_gp_from_name() below.
  1537. */
  1538. tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
  1539. strstrip(buf));
  1540. if (!tg_pt_gp_new)
  1541. return -ENODEV;
  1542. }
  1543. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1544. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1545. if (tg_pt_gp) {
  1546. /*
  1547. * Clearing an existing tg_pt_gp association, and replacing
  1548. * with the default_tg_pt_gp.
  1549. */
  1550. if (!tg_pt_gp_new) {
  1551. pr_debug("Target_Core_ConfigFS: Moving"
  1552. " %s/tpgt_%hu/%s from ALUA Target Port Group:"
  1553. " alua/%s, ID: %hu back to"
  1554. " default_tg_pt_gp\n",
  1555. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1556. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1557. config_item_name(&lun->lun_group.cg_item),
  1558. config_item_name(
  1559. &tg_pt_gp->tg_pt_gp_group.cg_item),
  1560. tg_pt_gp->tg_pt_gp_id);
  1561. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1562. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1563. dev->t10_alua.default_tg_pt_gp);
  1564. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1565. return count;
  1566. }
  1567. /*
  1568. * Removing existing association of tg_pt_gp_mem with tg_pt_gp
  1569. */
  1570. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1571. move = 1;
  1572. }
  1573. /*
  1574. * Associate tg_pt_gp_mem with tg_pt_gp_new.
  1575. */
  1576. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
  1577. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1578. pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
  1579. " Target Port Group: alua/%s, ID: %hu\n", (move) ?
  1580. "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1581. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1582. config_item_name(&lun->lun_group.cg_item),
  1583. config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
  1584. tg_pt_gp_new->tg_pt_gp_id);
  1585. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1586. return count;
  1587. }
  1588. ssize_t core_alua_show_access_type(
  1589. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1590. char *page)
  1591. {
  1592. if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
  1593. (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
  1594. return sprintf(page, "Implict and Explict\n");
  1595. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
  1596. return sprintf(page, "Implict\n");
  1597. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
  1598. return sprintf(page, "Explict\n");
  1599. else
  1600. return sprintf(page, "None\n");
  1601. }
  1602. ssize_t core_alua_store_access_type(
  1603. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1604. const char *page,
  1605. size_t count)
  1606. {
  1607. unsigned long tmp;
  1608. int ret;
  1609. ret = strict_strtoul(page, 0, &tmp);
  1610. if (ret < 0) {
  1611. pr_err("Unable to extract alua_access_type\n");
  1612. return -EINVAL;
  1613. }
  1614. if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
  1615. pr_err("Illegal value for alua_access_type:"
  1616. " %lu\n", tmp);
  1617. return -EINVAL;
  1618. }
  1619. if (tmp == 3)
  1620. tg_pt_gp->tg_pt_gp_alua_access_type =
  1621. TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
  1622. else if (tmp == 2)
  1623. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
  1624. else if (tmp == 1)
  1625. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
  1626. else
  1627. tg_pt_gp->tg_pt_gp_alua_access_type = 0;
  1628. return count;
  1629. }
  1630. ssize_t core_alua_show_nonop_delay_msecs(
  1631. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1632. char *page)
  1633. {
  1634. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
  1635. }
  1636. ssize_t core_alua_store_nonop_delay_msecs(
  1637. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1638. const char *page,
  1639. size_t count)
  1640. {
  1641. unsigned long tmp;
  1642. int ret;
  1643. ret = strict_strtoul(page, 0, &tmp);
  1644. if (ret < 0) {
  1645. pr_err("Unable to extract nonop_delay_msecs\n");
  1646. return -EINVAL;
  1647. }
  1648. if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
  1649. pr_err("Passed nonop_delay_msecs: %lu, exceeds"
  1650. " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
  1651. ALUA_MAX_NONOP_DELAY_MSECS);
  1652. return -EINVAL;
  1653. }
  1654. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
  1655. return count;
  1656. }
  1657. ssize_t core_alua_show_trans_delay_msecs(
  1658. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1659. char *page)
  1660. {
  1661. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  1662. }
  1663. ssize_t core_alua_store_trans_delay_msecs(
  1664. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1665. const char *page,
  1666. size_t count)
  1667. {
  1668. unsigned long tmp;
  1669. int ret;
  1670. ret = strict_strtoul(page, 0, &tmp);
  1671. if (ret < 0) {
  1672. pr_err("Unable to extract trans_delay_msecs\n");
  1673. return -EINVAL;
  1674. }
  1675. if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
  1676. pr_err("Passed trans_delay_msecs: %lu, exceeds"
  1677. " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
  1678. ALUA_MAX_TRANS_DELAY_MSECS);
  1679. return -EINVAL;
  1680. }
  1681. tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
  1682. return count;
  1683. }
  1684. ssize_t core_alua_show_implict_trans_secs(
  1685. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1686. char *page)
  1687. {
  1688. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
  1689. }
  1690. ssize_t core_alua_store_implict_trans_secs(
  1691. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1692. const char *page,
  1693. size_t count)
  1694. {
  1695. unsigned long tmp;
  1696. int ret;
  1697. ret = strict_strtoul(page, 0, &tmp);
  1698. if (ret < 0) {
  1699. pr_err("Unable to extract implict_trans_secs\n");
  1700. return -EINVAL;
  1701. }
  1702. if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
  1703. pr_err("Passed implict_trans_secs: %lu, exceeds"
  1704. " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
  1705. ALUA_MAX_IMPLICT_TRANS_SECS);
  1706. return -EINVAL;
  1707. }
  1708. tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
  1709. return count;
  1710. }
  1711. ssize_t core_alua_show_preferred_bit(
  1712. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1713. char *page)
  1714. {
  1715. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
  1716. }
  1717. ssize_t core_alua_store_preferred_bit(
  1718. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1719. const char *page,
  1720. size_t count)
  1721. {
  1722. unsigned long tmp;
  1723. int ret;
  1724. ret = strict_strtoul(page, 0, &tmp);
  1725. if (ret < 0) {
  1726. pr_err("Unable to extract preferred ALUA value\n");
  1727. return -EINVAL;
  1728. }
  1729. if ((tmp != 0) && (tmp != 1)) {
  1730. pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
  1731. return -EINVAL;
  1732. }
  1733. tg_pt_gp->tg_pt_gp_pref = (int)tmp;
  1734. return count;
  1735. }
  1736. ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
  1737. {
  1738. if (!lun->lun_sep)
  1739. return -ENODEV;
  1740. return sprintf(page, "%d\n",
  1741. atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
  1742. }
  1743. ssize_t core_alua_store_offline_bit(
  1744. struct se_lun *lun,
  1745. const char *page,
  1746. size_t count)
  1747. {
  1748. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1749. unsigned long tmp;
  1750. int ret;
  1751. if (!lun->lun_sep)
  1752. return -ENODEV;
  1753. ret = strict_strtoul(page, 0, &tmp);
  1754. if (ret < 0) {
  1755. pr_err("Unable to extract alua_tg_pt_offline value\n");
  1756. return -EINVAL;
  1757. }
  1758. if ((tmp != 0) && (tmp != 1)) {
  1759. pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
  1760. tmp);
  1761. return -EINVAL;
  1762. }
  1763. tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
  1764. if (!tg_pt_gp_mem) {
  1765. pr_err("Unable to locate *tg_pt_gp_mem\n");
  1766. return -EINVAL;
  1767. }
  1768. ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
  1769. lun->lun_sep, 0, (int)tmp);
  1770. if (ret < 0)
  1771. return -EINVAL;
  1772. return count;
  1773. }
  1774. ssize_t core_alua_show_secondary_status(
  1775. struct se_lun *lun,
  1776. char *page)
  1777. {
  1778. return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
  1779. }
  1780. ssize_t core_alua_store_secondary_status(
  1781. struct se_lun *lun,
  1782. const char *page,
  1783. size_t count)
  1784. {
  1785. unsigned long tmp;
  1786. int ret;
  1787. ret = strict_strtoul(page, 0, &tmp);
  1788. if (ret < 0) {
  1789. pr_err("Unable to extract alua_tg_pt_status\n");
  1790. return -EINVAL;
  1791. }
  1792. if ((tmp != ALUA_STATUS_NONE) &&
  1793. (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  1794. (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
  1795. pr_err("Illegal value for alua_tg_pt_status: %lu\n",
  1796. tmp);
  1797. return -EINVAL;
  1798. }
  1799. lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
  1800. return count;
  1801. }
  1802. ssize_t core_alua_show_secondary_write_metadata(
  1803. struct se_lun *lun,
  1804. char *page)
  1805. {
  1806. return sprintf(page, "%d\n",
  1807. lun->lun_sep->sep_tg_pt_secondary_write_md);
  1808. }
  1809. ssize_t core_alua_store_secondary_write_metadata(
  1810. struct se_lun *lun,
  1811. const char *page,
  1812. size_t count)
  1813. {
  1814. unsigned long tmp;
  1815. int ret;
  1816. ret = strict_strtoul(page, 0, &tmp);
  1817. if (ret < 0) {
  1818. pr_err("Unable to extract alua_tg_pt_write_md\n");
  1819. return -EINVAL;
  1820. }
  1821. if ((tmp != 0) && (tmp != 1)) {
  1822. pr_err("Illegal value for alua_tg_pt_write_md:"
  1823. " %lu\n", tmp);
  1824. return -EINVAL;
  1825. }
  1826. lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
  1827. return count;
  1828. }
  1829. int core_setup_alua(struct se_device *dev)
  1830. {
  1831. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
  1832. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  1833. struct t10_alua_lu_gp_member *lu_gp_mem;
  1834. /*
  1835. * Associate this struct se_device with the default ALUA
  1836. * LUN Group.
  1837. */
  1838. lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
  1839. if (IS_ERR(lu_gp_mem))
  1840. return PTR_ERR(lu_gp_mem);
  1841. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1842. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1843. default_lu_gp);
  1844. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1845. pr_debug("%s: Adding to default ALUA LU Group:"
  1846. " core/alua/lu_gps/default_lu_gp\n",
  1847. dev->transport->name);
  1848. }
  1849. return 0;
  1850. }