target_core_alua.c 56 KB

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