target_core_alua.c 61 KB

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