target_core_alua.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  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++] |= (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, valid_states,
  304. &primary, 1);
  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_ALUA)
  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, int explicit)
  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. if (!(valid & ALUA_T_SUP) || explicit)
  735. /*
  736. * Transitioning is set internally and by tcmu daemon,
  737. * and cannot be selected through a STPG.
  738. */
  739. goto not_supported;
  740. *primary = 0;
  741. break;
  742. default:
  743. pr_err("Unknown ALUA access state: 0x%02x\n", state);
  744. return TCM_INVALID_PARAMETER_LIST;
  745. }
  746. return 0;
  747. not_supported:
  748. pr_err("ALUA access state %s not supported",
  749. core_alua_dump_state(state));
  750. return TCM_INVALID_PARAMETER_LIST;
  751. }
  752. static char *core_alua_dump_state(int state)
  753. {
  754. switch (state) {
  755. case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
  756. return "Active/Optimized";
  757. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  758. return "Active/NonOptimized";
  759. case ALUA_ACCESS_STATE_LBA_DEPENDENT:
  760. return "LBA Dependent";
  761. case ALUA_ACCESS_STATE_STANDBY:
  762. return "Standby";
  763. case ALUA_ACCESS_STATE_UNAVAILABLE:
  764. return "Unavailable";
  765. case ALUA_ACCESS_STATE_OFFLINE:
  766. return "Offline";
  767. case ALUA_ACCESS_STATE_TRANSITION:
  768. return "Transitioning";
  769. default:
  770. return "Unknown";
  771. }
  772. return NULL;
  773. }
  774. char *core_alua_dump_status(int status)
  775. {
  776. switch (status) {
  777. case ALUA_STATUS_NONE:
  778. return "None";
  779. case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
  780. return "Altered by Explicit STPG";
  781. case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
  782. return "Altered by Implicit ALUA";
  783. default:
  784. return "Unknown";
  785. }
  786. return NULL;
  787. }
  788. /*
  789. * Used by fabric modules to determine when we need to delay processing
  790. * for the Active/NonOptimized paths..
  791. */
  792. int core_alua_check_nonop_delay(
  793. struct se_cmd *cmd)
  794. {
  795. if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
  796. return 0;
  797. if (in_interrupt())
  798. return 0;
  799. /*
  800. * The ALUA Active/NonOptimized access state delay can be disabled
  801. * in via configfs with a value of zero
  802. */
  803. if (!cmd->alua_nonop_delay)
  804. return 0;
  805. /*
  806. * struct se_cmd->alua_nonop_delay gets set by a target port group
  807. * defined interval in core_alua_state_nonoptimized()
  808. */
  809. msleep_interruptible(cmd->alua_nonop_delay);
  810. return 0;
  811. }
  812. EXPORT_SYMBOL(core_alua_check_nonop_delay);
  813. static int core_alua_write_tpg_metadata(
  814. const char *path,
  815. unsigned char *md_buf,
  816. u32 md_buf_len)
  817. {
  818. struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
  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, 0);
  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_md_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_pending_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 void core_alua_do_transition_tg_pt_work(struct work_struct *work)
  921. {
  922. struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
  923. struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work);
  924. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  925. bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
  926. ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
  927. /*
  928. * Update the ALUA metadata buf that has been allocated in
  929. * core_alua_do_port_transition(), this metadata will be written
  930. * to struct file.
  931. *
  932. * Note that there is the case where we do not want to update the
  933. * metadata when the saved metadata is being parsed in userspace
  934. * when setting the existing port access state and access status.
  935. *
  936. * Also note that the failure to write out the ALUA metadata to
  937. * struct file does NOT affect the actual ALUA transition.
  938. */
  939. if (tg_pt_gp->tg_pt_gp_write_metadata) {
  940. mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
  941. core_alua_update_tpg_primary_metadata(tg_pt_gp);
  942. mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
  943. }
  944. /*
  945. * Set the current primary ALUA access state to the requested new state
  946. */
  947. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  948. tg_pt_gp->tg_pt_gp_alua_pending_state);
  949. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  950. " from primary access state %s to %s\n", (explicit) ? "explicit" :
  951. "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  952. tg_pt_gp->tg_pt_gp_id,
  953. core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
  954. core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
  955. core_alua_queue_state_change_ua(tg_pt_gp);
  956. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  957. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  958. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  959. if (tg_pt_gp->tg_pt_gp_transition_complete)
  960. complete(tg_pt_gp->tg_pt_gp_transition_complete);
  961. }
  962. static int core_alua_do_transition_tg_pt(
  963. struct t10_alua_tg_pt_gp *tg_pt_gp,
  964. int new_state,
  965. int explicit)
  966. {
  967. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  968. DECLARE_COMPLETION_ONSTACK(wait);
  969. /* Nothing to be done here */
  970. if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
  971. return 0;
  972. if (explicit && new_state == ALUA_ACCESS_STATE_TRANSITION)
  973. return -EAGAIN;
  974. /*
  975. * Flush any pending transitions
  976. */
  977. if (!explicit)
  978. flush_work(&tg_pt_gp->tg_pt_gp_transition_work);
  979. /*
  980. * Save the old primary ALUA access state, and set the current state
  981. * to ALUA_ACCESS_STATE_TRANSITION.
  982. */
  983. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  984. ALUA_ACCESS_STATE_TRANSITION);
  985. tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
  986. ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
  987. ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
  988. core_alua_queue_state_change_ua(tg_pt_gp);
  989. if (new_state == ALUA_ACCESS_STATE_TRANSITION)
  990. return 0;
  991. tg_pt_gp->tg_pt_gp_alua_previous_state =
  992. atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  993. tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
  994. /*
  995. * Check for the optional ALUA primary state transition delay
  996. */
  997. if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
  998. msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  999. /*
  1000. * Take a reference for workqueue item
  1001. */
  1002. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1003. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1004. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1005. schedule_work(&tg_pt_gp->tg_pt_gp_transition_work);
  1006. if (explicit) {
  1007. tg_pt_gp->tg_pt_gp_transition_complete = &wait;
  1008. wait_for_completion(&wait);
  1009. tg_pt_gp->tg_pt_gp_transition_complete = NULL;
  1010. }
  1011. return 0;
  1012. }
  1013. int core_alua_do_port_transition(
  1014. struct t10_alua_tg_pt_gp *l_tg_pt_gp,
  1015. struct se_device *l_dev,
  1016. struct se_lun *l_lun,
  1017. struct se_node_acl *l_nacl,
  1018. int new_state,
  1019. int explicit)
  1020. {
  1021. struct se_device *dev;
  1022. struct t10_alua_lu_gp *lu_gp;
  1023. struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
  1024. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1025. int primary, valid_states, rc = 0;
  1026. if (l_dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
  1027. return -ENODEV;
  1028. valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
  1029. if (core_alua_check_transition(new_state, valid_states, &primary,
  1030. explicit) != 0)
  1031. return -EINVAL;
  1032. local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
  1033. spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
  1034. lu_gp = local_lu_gp_mem->lu_gp;
  1035. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  1036. spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
  1037. /*
  1038. * For storage objects that are members of the 'default_lu_gp',
  1039. * we only do transition on the passed *l_tp_pt_gp, and not
  1040. * on all of the matching target port groups IDs in default_lu_gp.
  1041. */
  1042. if (!lu_gp->lu_gp_id) {
  1043. /*
  1044. * core_alua_do_transition_tg_pt() will always return
  1045. * success.
  1046. */
  1047. l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
  1048. l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
  1049. rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
  1050. new_state, explicit);
  1051. atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
  1052. return rc;
  1053. }
  1054. /*
  1055. * For all other LU groups aside from 'default_lu_gp', walk all of
  1056. * the associated storage objects looking for a matching target port
  1057. * group ID from the local target port group.
  1058. */
  1059. spin_lock(&lu_gp->lu_gp_lock);
  1060. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
  1061. lu_gp_mem_list) {
  1062. dev = lu_gp_mem->lu_gp_mem_dev;
  1063. atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
  1064. spin_unlock(&lu_gp->lu_gp_lock);
  1065. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1066. list_for_each_entry(tg_pt_gp,
  1067. &dev->t10_alua.tg_pt_gps_list,
  1068. tg_pt_gp_list) {
  1069. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1070. continue;
  1071. /*
  1072. * If the target behavior port asymmetric access state
  1073. * is changed for any target port group accessible via
  1074. * a logical unit within a LU group, the target port
  1075. * behavior group asymmetric access states for the same
  1076. * target port group accessible via other logical units
  1077. * in that LU group will also change.
  1078. */
  1079. if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
  1080. continue;
  1081. if (l_tg_pt_gp == tg_pt_gp) {
  1082. tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
  1083. tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
  1084. } else {
  1085. tg_pt_gp->tg_pt_gp_alua_lun = NULL;
  1086. tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
  1087. }
  1088. atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1089. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1090. /*
  1091. * core_alua_do_transition_tg_pt() will always return
  1092. * success.
  1093. */
  1094. rc = core_alua_do_transition_tg_pt(tg_pt_gp,
  1095. new_state, explicit);
  1096. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1097. atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1098. if (rc)
  1099. break;
  1100. }
  1101. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1102. spin_lock(&lu_gp->lu_gp_lock);
  1103. atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
  1104. }
  1105. spin_unlock(&lu_gp->lu_gp_lock);
  1106. if (!rc) {
  1107. pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
  1108. " Group IDs: %hu %s transition to primary state: %s\n",
  1109. config_item_name(&lu_gp->lu_gp_group.cg_item),
  1110. l_tg_pt_gp->tg_pt_gp_id,
  1111. (explicit) ? "explicit" : "implicit",
  1112. core_alua_dump_state(new_state));
  1113. }
  1114. atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
  1115. return rc;
  1116. }
  1117. static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
  1118. {
  1119. struct se_portal_group *se_tpg = lun->lun_tpg;
  1120. unsigned char *md_buf;
  1121. char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
  1122. int len, rc;
  1123. mutex_lock(&lun->lun_tg_pt_md_mutex);
  1124. md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
  1125. if (!md_buf) {
  1126. pr_err("Unable to allocate buf for ALUA metadata\n");
  1127. rc = -ENOMEM;
  1128. goto out_unlock;
  1129. }
  1130. memset(path, 0, ALUA_METADATA_PATH_LEN);
  1131. memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
  1132. len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
  1133. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
  1134. if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
  1135. snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
  1136. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  1137. len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
  1138. "alua_tg_pt_status=0x%02x\n",
  1139. atomic_read(&lun->lun_tg_pt_secondary_offline),
  1140. lun->lun_tg_pt_secondary_stat);
  1141. snprintf(path, ALUA_METADATA_PATH_LEN, "%s/alua/%s/%s/lun_%llu",
  1142. db_root, se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
  1143. lun->unpacked_lun);
  1144. rc = core_alua_write_tpg_metadata(path, md_buf, len);
  1145. kfree(md_buf);
  1146. out_unlock:
  1147. mutex_unlock(&lun->lun_tg_pt_md_mutex);
  1148. return rc;
  1149. }
  1150. static int core_alua_set_tg_pt_secondary_state(
  1151. struct se_lun *lun,
  1152. int explicit,
  1153. int offline)
  1154. {
  1155. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1156. int trans_delay_msecs;
  1157. spin_lock(&lun->lun_tg_pt_gp_lock);
  1158. tg_pt_gp = lun->lun_tg_pt_gp;
  1159. if (!tg_pt_gp) {
  1160. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1161. pr_err("Unable to complete secondary state"
  1162. " transition\n");
  1163. return -EINVAL;
  1164. }
  1165. trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
  1166. /*
  1167. * Set the secondary ALUA target port access state to OFFLINE
  1168. * or release the previously secondary state for struct se_lun
  1169. */
  1170. if (offline)
  1171. atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
  1172. else
  1173. atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
  1174. lun->lun_tg_pt_secondary_stat = (explicit) ?
  1175. ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
  1176. ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
  1177. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  1178. " to secondary access state: %s\n", (explicit) ? "explicit" :
  1179. "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  1180. tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
  1181. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1182. /*
  1183. * Do the optional transition delay after we set the secondary
  1184. * ALUA access state.
  1185. */
  1186. if (trans_delay_msecs != 0)
  1187. msleep_interruptible(trans_delay_msecs);
  1188. /*
  1189. * See if we need to update the ALUA fabric port metadata for
  1190. * secondary state and status
  1191. */
  1192. if (lun->lun_tg_pt_secondary_write_md)
  1193. core_alua_update_tpg_secondary_metadata(lun);
  1194. return 0;
  1195. }
  1196. struct t10_alua_lba_map *
  1197. core_alua_allocate_lba_map(struct list_head *list,
  1198. u64 first_lba, u64 last_lba)
  1199. {
  1200. struct t10_alua_lba_map *lba_map;
  1201. lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
  1202. if (!lba_map) {
  1203. pr_err("Unable to allocate struct t10_alua_lba_map\n");
  1204. return ERR_PTR(-ENOMEM);
  1205. }
  1206. INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
  1207. lba_map->lba_map_first_lba = first_lba;
  1208. lba_map->lba_map_last_lba = last_lba;
  1209. list_add_tail(&lba_map->lba_map_list, list);
  1210. return lba_map;
  1211. }
  1212. int
  1213. core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
  1214. int pg_id, int state)
  1215. {
  1216. struct t10_alua_lba_map_member *lba_map_mem;
  1217. list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
  1218. lba_map_mem_list) {
  1219. if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
  1220. pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
  1221. return -EINVAL;
  1222. }
  1223. }
  1224. lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
  1225. if (!lba_map_mem) {
  1226. pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
  1227. return -ENOMEM;
  1228. }
  1229. lba_map_mem->lba_map_mem_alua_state = state;
  1230. lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
  1231. list_add_tail(&lba_map_mem->lba_map_mem_list,
  1232. &lba_map->lba_map_mem_list);
  1233. return 0;
  1234. }
  1235. void
  1236. core_alua_free_lba_map(struct list_head *lba_list)
  1237. {
  1238. struct t10_alua_lba_map *lba_map, *lba_map_tmp;
  1239. struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
  1240. list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
  1241. lba_map_list) {
  1242. list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
  1243. &lba_map->lba_map_mem_list,
  1244. lba_map_mem_list) {
  1245. list_del(&lba_map_mem->lba_map_mem_list);
  1246. kmem_cache_free(t10_alua_lba_map_mem_cache,
  1247. lba_map_mem);
  1248. }
  1249. list_del(&lba_map->lba_map_list);
  1250. kmem_cache_free(t10_alua_lba_map_cache, lba_map);
  1251. }
  1252. }
  1253. void
  1254. core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
  1255. int segment_size, int segment_mult)
  1256. {
  1257. struct list_head old_lba_map_list;
  1258. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1259. int activate = 0, supported;
  1260. INIT_LIST_HEAD(&old_lba_map_list);
  1261. spin_lock(&dev->t10_alua.lba_map_lock);
  1262. dev->t10_alua.lba_map_segment_size = segment_size;
  1263. dev->t10_alua.lba_map_segment_multiplier = segment_mult;
  1264. list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
  1265. if (lba_map_list) {
  1266. list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
  1267. activate = 1;
  1268. }
  1269. spin_unlock(&dev->t10_alua.lba_map_lock);
  1270. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1271. list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
  1272. tg_pt_gp_list) {
  1273. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1274. continue;
  1275. supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
  1276. if (activate)
  1277. supported |= ALUA_LBD_SUP;
  1278. else
  1279. supported &= ~ALUA_LBD_SUP;
  1280. tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
  1281. }
  1282. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1283. core_alua_free_lba_map(&old_lba_map_list);
  1284. }
  1285. struct t10_alua_lu_gp *
  1286. core_alua_allocate_lu_gp(const char *name, int def_group)
  1287. {
  1288. struct t10_alua_lu_gp *lu_gp;
  1289. lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
  1290. if (!lu_gp) {
  1291. pr_err("Unable to allocate struct t10_alua_lu_gp\n");
  1292. return ERR_PTR(-ENOMEM);
  1293. }
  1294. INIT_LIST_HEAD(&lu_gp->lu_gp_node);
  1295. INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
  1296. spin_lock_init(&lu_gp->lu_gp_lock);
  1297. atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
  1298. if (def_group) {
  1299. lu_gp->lu_gp_id = alua_lu_gps_counter++;
  1300. lu_gp->lu_gp_valid_id = 1;
  1301. alua_lu_gps_count++;
  1302. }
  1303. return lu_gp;
  1304. }
  1305. int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
  1306. {
  1307. struct t10_alua_lu_gp *lu_gp_tmp;
  1308. u16 lu_gp_id_tmp;
  1309. /*
  1310. * The lu_gp->lu_gp_id may only be set once..
  1311. */
  1312. if (lu_gp->lu_gp_valid_id) {
  1313. pr_warn("ALUA LU Group already has a valid ID,"
  1314. " ignoring request\n");
  1315. return -EINVAL;
  1316. }
  1317. spin_lock(&lu_gps_lock);
  1318. if (alua_lu_gps_count == 0x0000ffff) {
  1319. pr_err("Maximum ALUA alua_lu_gps_count:"
  1320. " 0x0000ffff reached\n");
  1321. spin_unlock(&lu_gps_lock);
  1322. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1323. return -ENOSPC;
  1324. }
  1325. again:
  1326. lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
  1327. alua_lu_gps_counter++;
  1328. list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
  1329. if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
  1330. if (!lu_gp_id)
  1331. goto again;
  1332. pr_warn("ALUA Logical Unit Group ID: %hu"
  1333. " already exists, ignoring request\n",
  1334. lu_gp_id);
  1335. spin_unlock(&lu_gps_lock);
  1336. return -EINVAL;
  1337. }
  1338. }
  1339. lu_gp->lu_gp_id = lu_gp_id_tmp;
  1340. lu_gp->lu_gp_valid_id = 1;
  1341. list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
  1342. alua_lu_gps_count++;
  1343. spin_unlock(&lu_gps_lock);
  1344. return 0;
  1345. }
  1346. static struct t10_alua_lu_gp_member *
  1347. core_alua_allocate_lu_gp_mem(struct se_device *dev)
  1348. {
  1349. struct t10_alua_lu_gp_member *lu_gp_mem;
  1350. lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
  1351. if (!lu_gp_mem) {
  1352. pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
  1353. return ERR_PTR(-ENOMEM);
  1354. }
  1355. INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
  1356. spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
  1357. atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
  1358. lu_gp_mem->lu_gp_mem_dev = dev;
  1359. dev->dev_alua_lu_gp_mem = lu_gp_mem;
  1360. return lu_gp_mem;
  1361. }
  1362. void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
  1363. {
  1364. struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
  1365. /*
  1366. * Once we have reached this point, config_item_put() has
  1367. * already been called from target_core_alua_drop_lu_gp().
  1368. *
  1369. * Here, we remove the *lu_gp from the global list so that
  1370. * no associations can be made while we are releasing
  1371. * struct t10_alua_lu_gp.
  1372. */
  1373. spin_lock(&lu_gps_lock);
  1374. list_del(&lu_gp->lu_gp_node);
  1375. alua_lu_gps_count--;
  1376. spin_unlock(&lu_gps_lock);
  1377. /*
  1378. * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
  1379. * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
  1380. * released with core_alua_put_lu_gp_from_name()
  1381. */
  1382. while (atomic_read(&lu_gp->lu_gp_ref_cnt))
  1383. cpu_relax();
  1384. /*
  1385. * Release reference to struct t10_alua_lu_gp * from all associated
  1386. * struct se_device.
  1387. */
  1388. spin_lock(&lu_gp->lu_gp_lock);
  1389. list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
  1390. &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1391. if (lu_gp_mem->lu_gp_assoc) {
  1392. list_del(&lu_gp_mem->lu_gp_mem_list);
  1393. lu_gp->lu_gp_members--;
  1394. lu_gp_mem->lu_gp_assoc = 0;
  1395. }
  1396. spin_unlock(&lu_gp->lu_gp_lock);
  1397. /*
  1398. *
  1399. * lu_gp_mem is associated with a single
  1400. * struct se_device->dev_alua_lu_gp_mem, and is released when
  1401. * struct se_device is released via core_alua_free_lu_gp_mem().
  1402. *
  1403. * If the passed lu_gp does NOT match the default_lu_gp, assume
  1404. * we want to re-associate a given lu_gp_mem with default_lu_gp.
  1405. */
  1406. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1407. if (lu_gp != default_lu_gp)
  1408. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1409. default_lu_gp);
  1410. else
  1411. lu_gp_mem->lu_gp = NULL;
  1412. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1413. spin_lock(&lu_gp->lu_gp_lock);
  1414. }
  1415. spin_unlock(&lu_gp->lu_gp_lock);
  1416. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1417. }
  1418. void core_alua_free_lu_gp_mem(struct se_device *dev)
  1419. {
  1420. struct t10_alua_lu_gp *lu_gp;
  1421. struct t10_alua_lu_gp_member *lu_gp_mem;
  1422. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1423. if (!lu_gp_mem)
  1424. return;
  1425. while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
  1426. cpu_relax();
  1427. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1428. lu_gp = lu_gp_mem->lu_gp;
  1429. if (lu_gp) {
  1430. spin_lock(&lu_gp->lu_gp_lock);
  1431. if (lu_gp_mem->lu_gp_assoc) {
  1432. list_del(&lu_gp_mem->lu_gp_mem_list);
  1433. lu_gp->lu_gp_members--;
  1434. lu_gp_mem->lu_gp_assoc = 0;
  1435. }
  1436. spin_unlock(&lu_gp->lu_gp_lock);
  1437. lu_gp_mem->lu_gp = NULL;
  1438. }
  1439. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1440. kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
  1441. }
  1442. struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
  1443. {
  1444. struct t10_alua_lu_gp *lu_gp;
  1445. struct config_item *ci;
  1446. spin_lock(&lu_gps_lock);
  1447. list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
  1448. if (!lu_gp->lu_gp_valid_id)
  1449. continue;
  1450. ci = &lu_gp->lu_gp_group.cg_item;
  1451. if (!strcmp(config_item_name(ci), name)) {
  1452. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  1453. spin_unlock(&lu_gps_lock);
  1454. return lu_gp;
  1455. }
  1456. }
  1457. spin_unlock(&lu_gps_lock);
  1458. return NULL;
  1459. }
  1460. void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
  1461. {
  1462. spin_lock(&lu_gps_lock);
  1463. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  1464. spin_unlock(&lu_gps_lock);
  1465. }
  1466. /*
  1467. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1468. */
  1469. void __core_alua_attach_lu_gp_mem(
  1470. struct t10_alua_lu_gp_member *lu_gp_mem,
  1471. struct t10_alua_lu_gp *lu_gp)
  1472. {
  1473. spin_lock(&lu_gp->lu_gp_lock);
  1474. lu_gp_mem->lu_gp = lu_gp;
  1475. lu_gp_mem->lu_gp_assoc = 1;
  1476. list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
  1477. lu_gp->lu_gp_members++;
  1478. spin_unlock(&lu_gp->lu_gp_lock);
  1479. }
  1480. /*
  1481. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1482. */
  1483. void __core_alua_drop_lu_gp_mem(
  1484. struct t10_alua_lu_gp_member *lu_gp_mem,
  1485. struct t10_alua_lu_gp *lu_gp)
  1486. {
  1487. spin_lock(&lu_gp->lu_gp_lock);
  1488. list_del(&lu_gp_mem->lu_gp_mem_list);
  1489. lu_gp_mem->lu_gp = NULL;
  1490. lu_gp_mem->lu_gp_assoc = 0;
  1491. lu_gp->lu_gp_members--;
  1492. spin_unlock(&lu_gp->lu_gp_lock);
  1493. }
  1494. struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
  1495. const char *name, int def_group)
  1496. {
  1497. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1498. tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
  1499. if (!tg_pt_gp) {
  1500. pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
  1501. return NULL;
  1502. }
  1503. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
  1504. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
  1505. mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
  1506. spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
  1507. atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
  1508. INIT_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
  1509. core_alua_do_transition_tg_pt_work);
  1510. tg_pt_gp->tg_pt_gp_dev = dev;
  1511. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  1512. ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
  1513. /*
  1514. * Enable both explicit and implicit ALUA support by default
  1515. */
  1516. tg_pt_gp->tg_pt_gp_alua_access_type =
  1517. TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
  1518. /*
  1519. * Set the default Active/NonOptimized Delay in milliseconds
  1520. */
  1521. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
  1522. tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
  1523. tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
  1524. /*
  1525. * Enable all supported states
  1526. */
  1527. tg_pt_gp->tg_pt_gp_alua_supported_states =
  1528. ALUA_T_SUP | ALUA_O_SUP |
  1529. ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
  1530. if (def_group) {
  1531. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1532. tg_pt_gp->tg_pt_gp_id =
  1533. dev->t10_alua.alua_tg_pt_gps_counter++;
  1534. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1535. dev->t10_alua.alua_tg_pt_gps_count++;
  1536. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1537. &dev->t10_alua.tg_pt_gps_list);
  1538. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1539. }
  1540. return tg_pt_gp;
  1541. }
  1542. int core_alua_set_tg_pt_gp_id(
  1543. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1544. u16 tg_pt_gp_id)
  1545. {
  1546. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1547. struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
  1548. u16 tg_pt_gp_id_tmp;
  1549. /*
  1550. * The tg_pt_gp->tg_pt_gp_id may only be set once..
  1551. */
  1552. if (tg_pt_gp->tg_pt_gp_valid_id) {
  1553. pr_warn("ALUA TG PT Group already has a valid ID,"
  1554. " ignoring request\n");
  1555. return -EINVAL;
  1556. }
  1557. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1558. if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
  1559. pr_err("Maximum ALUA alua_tg_pt_gps_count:"
  1560. " 0x0000ffff reached\n");
  1561. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1562. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1563. return -ENOSPC;
  1564. }
  1565. again:
  1566. tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
  1567. dev->t10_alua.alua_tg_pt_gps_counter++;
  1568. list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
  1569. tg_pt_gp_list) {
  1570. if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
  1571. if (!tg_pt_gp_id)
  1572. goto again;
  1573. pr_err("ALUA Target Port Group ID: %hu already"
  1574. " exists, ignoring request\n", tg_pt_gp_id);
  1575. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1576. return -EINVAL;
  1577. }
  1578. }
  1579. tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
  1580. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1581. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1582. &dev->t10_alua.tg_pt_gps_list);
  1583. dev->t10_alua.alua_tg_pt_gps_count++;
  1584. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1585. return 0;
  1586. }
  1587. void core_alua_free_tg_pt_gp(
  1588. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1589. {
  1590. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1591. struct se_lun *lun, *next;
  1592. /*
  1593. * Once we have reached this point, config_item_put() has already
  1594. * been called from target_core_alua_drop_tg_pt_gp().
  1595. *
  1596. * Here we remove *tg_pt_gp from the global list so that
  1597. * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
  1598. * can be made while we are releasing struct t10_alua_tg_pt_gp.
  1599. */
  1600. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1601. list_del(&tg_pt_gp->tg_pt_gp_list);
  1602. dev->t10_alua.alua_tg_pt_gps_counter--;
  1603. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1604. flush_work(&tg_pt_gp->tg_pt_gp_transition_work);
  1605. /*
  1606. * Allow a struct t10_alua_tg_pt_gp_member * referenced by
  1607. * core_alua_get_tg_pt_gp_by_name() in
  1608. * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
  1609. * to be released with core_alua_put_tg_pt_gp_from_name().
  1610. */
  1611. while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
  1612. cpu_relax();
  1613. /*
  1614. * Release reference to struct t10_alua_tg_pt_gp from all associated
  1615. * struct se_port.
  1616. */
  1617. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1618. list_for_each_entry_safe(lun, next,
  1619. &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
  1620. list_del_init(&lun->lun_tg_pt_gp_link);
  1621. tg_pt_gp->tg_pt_gp_members--;
  1622. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1623. /*
  1624. * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
  1625. * assume we want to re-associate a given tg_pt_gp_mem with
  1626. * default_tg_pt_gp.
  1627. */
  1628. spin_lock(&lun->lun_tg_pt_gp_lock);
  1629. if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
  1630. __target_attach_tg_pt_gp(lun,
  1631. dev->t10_alua.default_tg_pt_gp);
  1632. } else
  1633. lun->lun_tg_pt_gp = NULL;
  1634. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1635. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1636. }
  1637. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1638. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1639. }
  1640. static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
  1641. struct se_device *dev, const char *name)
  1642. {
  1643. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1644. struct config_item *ci;
  1645. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1646. list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
  1647. tg_pt_gp_list) {
  1648. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1649. continue;
  1650. ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1651. if (!strcmp(config_item_name(ci), name)) {
  1652. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1653. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1654. return tg_pt_gp;
  1655. }
  1656. }
  1657. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1658. return NULL;
  1659. }
  1660. static void core_alua_put_tg_pt_gp_from_name(
  1661. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1662. {
  1663. struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
  1664. spin_lock(&dev->t10_alua.tg_pt_gps_lock);
  1665. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1666. spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
  1667. }
  1668. static void __target_attach_tg_pt_gp(struct se_lun *lun,
  1669. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1670. {
  1671. struct se_dev_entry *se_deve;
  1672. assert_spin_locked(&lun->lun_tg_pt_gp_lock);
  1673. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1674. lun->lun_tg_pt_gp = tg_pt_gp;
  1675. list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
  1676. tg_pt_gp->tg_pt_gp_members++;
  1677. spin_lock(&lun->lun_deve_lock);
  1678. list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
  1679. core_scsi3_ua_allocate(se_deve, 0x3f,
  1680. ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED);
  1681. spin_unlock(&lun->lun_deve_lock);
  1682. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1683. }
  1684. void target_attach_tg_pt_gp(struct se_lun *lun,
  1685. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1686. {
  1687. spin_lock(&lun->lun_tg_pt_gp_lock);
  1688. __target_attach_tg_pt_gp(lun, tg_pt_gp);
  1689. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1690. }
  1691. static void __target_detach_tg_pt_gp(struct se_lun *lun,
  1692. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1693. {
  1694. assert_spin_locked(&lun->lun_tg_pt_gp_lock);
  1695. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1696. list_del_init(&lun->lun_tg_pt_gp_link);
  1697. tg_pt_gp->tg_pt_gp_members--;
  1698. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1699. lun->lun_tg_pt_gp = NULL;
  1700. }
  1701. void target_detach_tg_pt_gp(struct se_lun *lun)
  1702. {
  1703. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1704. spin_lock(&lun->lun_tg_pt_gp_lock);
  1705. tg_pt_gp = lun->lun_tg_pt_gp;
  1706. if (tg_pt_gp)
  1707. __target_detach_tg_pt_gp(lun, tg_pt_gp);
  1708. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1709. }
  1710. ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
  1711. {
  1712. struct config_item *tg_pt_ci;
  1713. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1714. ssize_t len = 0;
  1715. spin_lock(&lun->lun_tg_pt_gp_lock);
  1716. tg_pt_gp = lun->lun_tg_pt_gp;
  1717. if (tg_pt_gp) {
  1718. tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1719. len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
  1720. " %hu\nTG Port Primary Access State: %s\nTG Port "
  1721. "Primary Access Status: %s\nTG Port Secondary Access"
  1722. " State: %s\nTG Port Secondary Access Status: %s\n",
  1723. config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
  1724. core_alua_dump_state(atomic_read(
  1725. &tg_pt_gp->tg_pt_gp_alua_access_state)),
  1726. core_alua_dump_status(
  1727. tg_pt_gp->tg_pt_gp_alua_access_status),
  1728. atomic_read(&lun->lun_tg_pt_secondary_offline) ?
  1729. "Offline" : "None",
  1730. core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
  1731. }
  1732. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1733. return len;
  1734. }
  1735. ssize_t core_alua_store_tg_pt_gp_info(
  1736. struct se_lun *lun,
  1737. const char *page,
  1738. size_t count)
  1739. {
  1740. struct se_portal_group *tpg = lun->lun_tpg;
  1741. /*
  1742. * rcu_dereference_raw protected by se_lun->lun_group symlink
  1743. * reference to se_device->dev_group.
  1744. */
  1745. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  1746. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
  1747. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1748. int move = 0;
  1749. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
  1750. (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
  1751. return -ENODEV;
  1752. if (count > TG_PT_GROUP_NAME_BUF) {
  1753. pr_err("ALUA Target Port Group alias too large!\n");
  1754. return -EINVAL;
  1755. }
  1756. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1757. memcpy(buf, page, count);
  1758. /*
  1759. * Any ALUA target port group alias besides "NULL" means we will be
  1760. * making a new group association.
  1761. */
  1762. if (strcmp(strstrip(buf), "NULL")) {
  1763. /*
  1764. * core_alua_get_tg_pt_gp_by_name() will increment reference to
  1765. * struct t10_alua_tg_pt_gp. This reference is released with
  1766. * core_alua_put_tg_pt_gp_from_name() below.
  1767. */
  1768. tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
  1769. strstrip(buf));
  1770. if (!tg_pt_gp_new)
  1771. return -ENODEV;
  1772. }
  1773. spin_lock(&lun->lun_tg_pt_gp_lock);
  1774. tg_pt_gp = lun->lun_tg_pt_gp;
  1775. if (tg_pt_gp) {
  1776. /*
  1777. * Clearing an existing tg_pt_gp association, and replacing
  1778. * with the default_tg_pt_gp.
  1779. */
  1780. if (!tg_pt_gp_new) {
  1781. pr_debug("Target_Core_ConfigFS: Moving"
  1782. " %s/tpgt_%hu/%s from ALUA Target Port Group:"
  1783. " alua/%s, ID: %hu back to"
  1784. " default_tg_pt_gp\n",
  1785. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1786. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1787. config_item_name(&lun->lun_group.cg_item),
  1788. config_item_name(
  1789. &tg_pt_gp->tg_pt_gp_group.cg_item),
  1790. tg_pt_gp->tg_pt_gp_id);
  1791. __target_detach_tg_pt_gp(lun, tg_pt_gp);
  1792. __target_attach_tg_pt_gp(lun,
  1793. dev->t10_alua.default_tg_pt_gp);
  1794. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1795. return count;
  1796. }
  1797. __target_detach_tg_pt_gp(lun, tg_pt_gp);
  1798. move = 1;
  1799. }
  1800. __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
  1801. spin_unlock(&lun->lun_tg_pt_gp_lock);
  1802. pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
  1803. " Target Port Group: alua/%s, ID: %hu\n", (move) ?
  1804. "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1805. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1806. config_item_name(&lun->lun_group.cg_item),
  1807. config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
  1808. tg_pt_gp_new->tg_pt_gp_id);
  1809. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1810. return count;
  1811. }
  1812. ssize_t core_alua_show_access_type(
  1813. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1814. char *page)
  1815. {
  1816. if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
  1817. (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
  1818. return sprintf(page, "Implicit and Explicit\n");
  1819. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
  1820. return sprintf(page, "Implicit\n");
  1821. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
  1822. return sprintf(page, "Explicit\n");
  1823. else
  1824. return sprintf(page, "None\n");
  1825. }
  1826. ssize_t core_alua_store_access_type(
  1827. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1828. const char *page,
  1829. size_t count)
  1830. {
  1831. unsigned long tmp;
  1832. int ret;
  1833. ret = kstrtoul(page, 0, &tmp);
  1834. if (ret < 0) {
  1835. pr_err("Unable to extract alua_access_type\n");
  1836. return ret;
  1837. }
  1838. if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
  1839. pr_err("Illegal value for alua_access_type:"
  1840. " %lu\n", tmp);
  1841. return -EINVAL;
  1842. }
  1843. if (tmp == 3)
  1844. tg_pt_gp->tg_pt_gp_alua_access_type =
  1845. TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
  1846. else if (tmp == 2)
  1847. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
  1848. else if (tmp == 1)
  1849. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
  1850. else
  1851. tg_pt_gp->tg_pt_gp_alua_access_type = 0;
  1852. return count;
  1853. }
  1854. ssize_t core_alua_show_nonop_delay_msecs(
  1855. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1856. char *page)
  1857. {
  1858. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
  1859. }
  1860. ssize_t core_alua_store_nonop_delay_msecs(
  1861. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1862. const char *page,
  1863. size_t count)
  1864. {
  1865. unsigned long tmp;
  1866. int ret;
  1867. ret = kstrtoul(page, 0, &tmp);
  1868. if (ret < 0) {
  1869. pr_err("Unable to extract nonop_delay_msecs\n");
  1870. return ret;
  1871. }
  1872. if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
  1873. pr_err("Passed nonop_delay_msecs: %lu, exceeds"
  1874. " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
  1875. ALUA_MAX_NONOP_DELAY_MSECS);
  1876. return -EINVAL;
  1877. }
  1878. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
  1879. return count;
  1880. }
  1881. ssize_t core_alua_show_trans_delay_msecs(
  1882. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1883. char *page)
  1884. {
  1885. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  1886. }
  1887. ssize_t core_alua_store_trans_delay_msecs(
  1888. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1889. const char *page,
  1890. size_t count)
  1891. {
  1892. unsigned long tmp;
  1893. int ret;
  1894. ret = kstrtoul(page, 0, &tmp);
  1895. if (ret < 0) {
  1896. pr_err("Unable to extract trans_delay_msecs\n");
  1897. return ret;
  1898. }
  1899. if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
  1900. pr_err("Passed trans_delay_msecs: %lu, exceeds"
  1901. " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
  1902. ALUA_MAX_TRANS_DELAY_MSECS);
  1903. return -EINVAL;
  1904. }
  1905. tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
  1906. return count;
  1907. }
  1908. ssize_t core_alua_show_implicit_trans_secs(
  1909. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1910. char *page)
  1911. {
  1912. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
  1913. }
  1914. ssize_t core_alua_store_implicit_trans_secs(
  1915. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1916. const char *page,
  1917. size_t count)
  1918. {
  1919. unsigned long tmp;
  1920. int ret;
  1921. ret = kstrtoul(page, 0, &tmp);
  1922. if (ret < 0) {
  1923. pr_err("Unable to extract implicit_trans_secs\n");
  1924. return ret;
  1925. }
  1926. if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
  1927. pr_err("Passed implicit_trans_secs: %lu, exceeds"
  1928. " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
  1929. ALUA_MAX_IMPLICIT_TRANS_SECS);
  1930. return -EINVAL;
  1931. }
  1932. tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
  1933. return count;
  1934. }
  1935. ssize_t core_alua_show_preferred_bit(
  1936. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1937. char *page)
  1938. {
  1939. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
  1940. }
  1941. ssize_t core_alua_store_preferred_bit(
  1942. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1943. const char *page,
  1944. size_t count)
  1945. {
  1946. unsigned long tmp;
  1947. int ret;
  1948. ret = kstrtoul(page, 0, &tmp);
  1949. if (ret < 0) {
  1950. pr_err("Unable to extract preferred ALUA value\n");
  1951. return ret;
  1952. }
  1953. if ((tmp != 0) && (tmp != 1)) {
  1954. pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
  1955. return -EINVAL;
  1956. }
  1957. tg_pt_gp->tg_pt_gp_pref = (int)tmp;
  1958. return count;
  1959. }
  1960. ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
  1961. {
  1962. return sprintf(page, "%d\n",
  1963. atomic_read(&lun->lun_tg_pt_secondary_offline));
  1964. }
  1965. ssize_t core_alua_store_offline_bit(
  1966. struct se_lun *lun,
  1967. const char *page,
  1968. size_t count)
  1969. {
  1970. /*
  1971. * rcu_dereference_raw protected by se_lun->lun_group symlink
  1972. * reference to se_device->dev_group.
  1973. */
  1974. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  1975. unsigned long tmp;
  1976. int ret;
  1977. if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
  1978. (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
  1979. return -ENODEV;
  1980. ret = kstrtoul(page, 0, &tmp);
  1981. if (ret < 0) {
  1982. pr_err("Unable to extract alua_tg_pt_offline value\n");
  1983. return ret;
  1984. }
  1985. if ((tmp != 0) && (tmp != 1)) {
  1986. pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
  1987. tmp);
  1988. return -EINVAL;
  1989. }
  1990. ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
  1991. if (ret < 0)
  1992. return -EINVAL;
  1993. return count;
  1994. }
  1995. ssize_t core_alua_show_secondary_status(
  1996. struct se_lun *lun,
  1997. char *page)
  1998. {
  1999. return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
  2000. }
  2001. ssize_t core_alua_store_secondary_status(
  2002. struct se_lun *lun,
  2003. const char *page,
  2004. size_t count)
  2005. {
  2006. unsigned long tmp;
  2007. int ret;
  2008. ret = kstrtoul(page, 0, &tmp);
  2009. if (ret < 0) {
  2010. pr_err("Unable to extract alua_tg_pt_status\n");
  2011. return ret;
  2012. }
  2013. if ((tmp != ALUA_STATUS_NONE) &&
  2014. (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
  2015. (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
  2016. pr_err("Illegal value for alua_tg_pt_status: %lu\n",
  2017. tmp);
  2018. return -EINVAL;
  2019. }
  2020. lun->lun_tg_pt_secondary_stat = (int)tmp;
  2021. return count;
  2022. }
  2023. ssize_t core_alua_show_secondary_write_metadata(
  2024. struct se_lun *lun,
  2025. char *page)
  2026. {
  2027. return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
  2028. }
  2029. ssize_t core_alua_store_secondary_write_metadata(
  2030. struct se_lun *lun,
  2031. const char *page,
  2032. size_t count)
  2033. {
  2034. unsigned long tmp;
  2035. int ret;
  2036. ret = kstrtoul(page, 0, &tmp);
  2037. if (ret < 0) {
  2038. pr_err("Unable to extract alua_tg_pt_write_md\n");
  2039. return ret;
  2040. }
  2041. if ((tmp != 0) && (tmp != 1)) {
  2042. pr_err("Illegal value for alua_tg_pt_write_md:"
  2043. " %lu\n", tmp);
  2044. return -EINVAL;
  2045. }
  2046. lun->lun_tg_pt_secondary_write_md = (int)tmp;
  2047. return count;
  2048. }
  2049. int core_setup_alua(struct se_device *dev)
  2050. {
  2051. if (!(dev->transport->transport_flags &
  2052. TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
  2053. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  2054. struct t10_alua_lu_gp_member *lu_gp_mem;
  2055. /*
  2056. * Associate this struct se_device with the default ALUA
  2057. * LUN Group.
  2058. */
  2059. lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
  2060. if (IS_ERR(lu_gp_mem))
  2061. return PTR_ERR(lu_gp_mem);
  2062. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  2063. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  2064. default_lu_gp);
  2065. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  2066. pr_debug("%s: Adding to default ALUA LU Group:"
  2067. " core/alua/lu_gps/default_lu_gp\n",
  2068. dev->transport->name);
  2069. }
  2070. return 0;
  2071. }