target_core_alua.c 61 KB

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