target_core_alua.c 54 KB

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