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