target_core_alua.c 55 KB

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