target_core_alua.c 59 KB

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