target_core_alua.c 60 KB

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