Valgrind fixes
[multipath-tools/.git] / libmultipath / prioritizers / alua.c
1 /*
2  * (C) Copyright IBM Corp. 2004, 2005   All Rights Reserved.
3  *
4  * main.c
5  *
6  * Tool to make use of a SCSI-feature called Asymmetric Logical Unit Access.
7  * It determines the ALUA state of a device and prints a priority value to
8  * stdout.
9  *
10  * Author(s): Jan Kunigk
11  *            S. Bader <shbader@de.ibm.com>
12  * 
13  * This file is released under the GPL.
14  */
15 #include <stdio.h>
16
17 #include <debug.h>
18 #include <prio.h>
19
20 #include "alua.h"
21
22 #define ALUA_PRIO_NOT_SUPPORTED                 1
23 #define ALUA_PRIO_RTPG_FAILED                   2
24 #define ALUA_PRIO_GETAAS_FAILED                 3
25 #define ALUA_PRIO_TPGS_FAILED                   4
26 #define ALUA_PRIO_NO_INFORMATION                5
27
28 int
29 get_alua_info(int fd)
30 {
31         char *  aas_string[] = {
32                 [AAS_OPTIMIZED]         = "active/optimized",
33                 [AAS_NON_OPTIMIZED]     = "active/non-optimized",
34                 [AAS_STANDBY]           = "standby",
35                 [AAS_UNAVAILABLE]       = "unavailable",
36                 [AAS_TRANSITIONING]     = "transitioning between states",
37         };
38         int     rc;
39         int     tpg;
40
41         rc = get_target_port_group_support(fd);
42         if (rc < 0)
43                 return -ALUA_PRIO_TPGS_FAILED;
44
45         if (rc == TPGS_NONE)
46                 return -ALUA_PRIO_NOT_SUPPORTED;
47
48         tpg = get_target_port_group(fd);
49         if (tpg < 0)
50                 return -ALUA_PRIO_RTPG_FAILED;
51
52         condlog(3, "reported target port group is %i", tpg);
53         rc = get_asymmetric_access_state(fd, tpg);
54         if (rc < 0)
55                 return -ALUA_PRIO_GETAAS_FAILED;
56
57         condlog(3, "aas = [%s]",
58                 (rc < 4) ? aas_string[rc] : "invalid/reserved");
59         return rc;
60 }
61
62 int getprio (struct path * pp)
63 {
64         int rc;
65
66         if (pp->fd < 0)
67                 return -ALUA_PRIO_NO_INFORMATION;
68
69         rc = get_alua_info(pp->fd);
70         if (rc >= 0) {
71                 switch(rc) {
72                         case AAS_OPTIMIZED:
73                                 rc = 50;
74                                 break;
75                         case AAS_NON_OPTIMIZED:
76                                 rc = 10;
77                                 break;
78                         case AAS_STANDBY:
79                                 rc = 1;
80                                 break;
81                         default:
82                                 rc = 0;
83                 }
84         } else {
85                 switch(-rc) {
86                         case ALUA_PRIO_NOT_SUPPORTED:
87                                 condlog(0, "%s: alua not supported", pp->dev);
88                                 break;
89                         case ALUA_PRIO_RTPG_FAILED:
90                                 condlog(0, "%s: couldn't get target port group", pp->dev);
91                                 break;
92                         case ALUA_PRIO_GETAAS_FAILED:
93                                 condlog(0, "%s: couln't get asymmetric access state", pp->dev);
94                                 break;
95                         case ALUA_PRIO_TPGS_FAILED:
96                                 condlog(3, "%s: couln't get supported alua states", pp->dev);
97                                 break;
98                 }
99         }
100         return rc;
101 }