target_core_alua.c 62 KB

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