target_core_alua.c 62 KB

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