target_core_alua.c 61 KB

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