target_core_alua.c 62 KB

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