94e419079bd5ed5a3ef1a52fffce86d198fd16c8
[multipath-tools/.git] / libmultipath / checkers / tur.c
1 /*
2  * Some code borrowed from sg-utils.
3  *
4  * Copyright (c) 2004 Christophe Varoqui
5  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/ioctl.h>
14 #include <errno.h>
15 #include <sys/time.h>
16 #include <pthread.h>
17
18 #include "checkers.h"
19
20 #include "../libmultipath/debug.h"
21 #include "../libmultipath/sg_include.h"
22 #include "../libmultipath/uevent.h"
23
24 #define TUR_CMD_LEN 6
25 #define HEAVY_CHECK_COUNT       10
26
27 #define MSG_TUR_UP      "tur checker reports path is up"
28 #define MSG_TUR_DOWN    "tur checker reports path is down"
29 #define MSG_TUR_GHOST   "tur checker reports path is in standby state"
30 #define MSG_TUR_RUNNING "tur checker still running"
31 #define MSG_TUR_TIMEOUT "tur checker timed out"
32 #define MSG_TUR_FAILED  "tur checker failed to initialize"
33
34 struct tur_checker_context {
35         dev_t devt;
36         int state;
37         int running;
38         int fd;
39         unsigned int timeout;
40         time_t time;
41         pthread_t thread;
42         pthread_mutex_t lock;
43         pthread_cond_t active;
44         pthread_spinlock_t hldr_lock;
45         int holders;
46         char message[CHECKER_MSG_LEN];
47 };
48
49 #define TUR_DEVT(c) major((c)->devt), minor((c)->devt)
50
51 int libcheck_init (struct checker * c)
52 {
53         struct tur_checker_context *ct;
54
55         ct = malloc(sizeof(struct tur_checker_context));
56         if (!ct)
57                 return 1;
58         memset(ct, 0, sizeof(struct tur_checker_context));
59
60         ct->state = PATH_UNCHECKED;
61         ct->fd = -1;
62         ct->holders = 1;
63         pthread_cond_init(&ct->active, NULL);
64         pthread_mutex_init(&ct->lock, NULL);
65         pthread_spin_init(&ct->hldr_lock, PTHREAD_PROCESS_PRIVATE);
66         c->context = ct;
67
68         return 0;
69 }
70
71 static void cleanup_context(struct tur_checker_context *ct)
72 {
73         pthread_mutex_destroy(&ct->lock);
74         pthread_cond_destroy(&ct->active);
75         pthread_spin_destroy(&ct->hldr_lock);
76         free(ct);
77 }
78
79 void libcheck_free (struct checker * c)
80 {
81         if (c->context) {
82                 struct tur_checker_context *ct = c->context;
83                 int holders;
84                 pthread_t thread;
85
86                 pthread_spin_lock(&ct->hldr_lock);
87                 ct->holders--;
88                 holders = ct->holders;
89                 thread = ct->thread;
90                 pthread_spin_unlock(&ct->hldr_lock);
91                 if (holders)
92                         pthread_cancel(thread);
93                 else
94                         cleanup_context(ct);
95                 c->context = NULL;
96         }
97         return;
98 }
99
100 void libcheck_repair (struct checker * c)
101 {
102         return;
103 }
104
105 #define TUR_MSG(msg, fmt, args...) snprintf(msg, CHECKER_MSG_LEN, fmt, ##args);
106
107 static int
108 tur_check(int fd, unsigned int timeout, char *msg)
109 {
110         struct sg_io_hdr io_hdr;
111         unsigned char turCmdBlk[TUR_CMD_LEN] = { 0x00, 0, 0, 0, 0, 0 };
112         unsigned char sense_buffer[32];
113         int retry_tur = 5;
114
115 retry:
116         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
117         memset(&sense_buffer, 0, 32);
118         io_hdr.interface_id = 'S';
119         io_hdr.cmd_len = sizeof (turCmdBlk);
120         io_hdr.mx_sb_len = sizeof (sense_buffer);
121         io_hdr.dxfer_direction = SG_DXFER_NONE;
122         io_hdr.cmdp = turCmdBlk;
123         io_hdr.sbp = sense_buffer;
124         io_hdr.timeout = timeout * 1000;
125         io_hdr.pack_id = 0;
126         if (ioctl(fd, SG_IO, &io_hdr) < 0) {
127                 TUR_MSG(msg, MSG_TUR_DOWN);
128                 return PATH_DOWN;
129         }
130         if ((io_hdr.status & 0x7e) == 0x18) {
131                 /*
132                  * SCSI-3 arrays might return
133                  * reservation conflict on TUR
134                  */
135                 TUR_MSG(msg, MSG_TUR_UP);
136                 return PATH_UP;
137         }
138         if (io_hdr.info & SG_INFO_OK_MASK) {
139                 int key = 0, asc, ascq;
140
141                 switch (io_hdr.host_status) {
142                 case DID_OK:
143                 case DID_NO_CONNECT:
144                 case DID_BAD_TARGET:
145                 case DID_ABORT:
146                 case DID_TRANSPORT_FAILFAST:
147                         break;
148                 default:
149                         /* Driver error, retry */
150                         if (--retry_tur)
151                                 goto retry;
152                         break;
153                 }
154                 if (io_hdr.sb_len_wr > 3) {
155                         if (io_hdr.sbp[0] == 0x72 || io_hdr.sbp[0] == 0x73) {
156                                 key = io_hdr.sbp[1] & 0x0f;
157                                 asc = io_hdr.sbp[2];
158                                 ascq = io_hdr.sbp[3];
159                         } else if (io_hdr.sb_len_wr > 13 &&
160                                    ((io_hdr.sbp[0] & 0x7f) == 0x70 ||
161                                     (io_hdr.sbp[0] & 0x7f) == 0x71)) {
162                                 key = io_hdr.sbp[2] & 0x0f;
163                                 asc = io_hdr.sbp[12];
164                                 ascq = io_hdr.sbp[13];
165                         }
166                 }
167                 if (key == 0x6) {
168                         /* Unit Attention, retry */
169                         if (--retry_tur)
170                                 goto retry;
171                 }
172                 else if (key == 0x2) {
173                         /* Not Ready */
174                         /* Note: Other ALUA states are either UP or DOWN */
175                         if( asc == 0x04 && ascq == 0x0b){
176                                 /*
177                                  * LOGICAL UNIT NOT ACCESSIBLE,
178                                  * TARGET PORT IN STANDBY STATE
179                                  */
180                                 TUR_MSG(msg, MSG_TUR_GHOST);
181                                 return PATH_GHOST;
182                         }
183                 }
184                 TUR_MSG(msg, MSG_TUR_DOWN);
185                 return PATH_DOWN;
186         }
187         TUR_MSG(msg, MSG_TUR_UP);
188         return PATH_UP;
189 }
190
191 #define tur_thread_cleanup_push(ct) pthread_cleanup_push(cleanup_func, ct)
192 #define tur_thread_cleanup_pop(ct) pthread_cleanup_pop(1)
193
194 static void cleanup_func(void *data)
195 {
196         int holders;
197         struct tur_checker_context *ct = data;
198         pthread_spin_lock(&ct->hldr_lock);
199         ct->holders--;
200         holders = ct->holders;
201         ct->thread = 0;
202         pthread_spin_unlock(&ct->hldr_lock);
203         if (!holders)
204                 cleanup_context(ct);
205 }
206
207 static void *tur_thread(void *ctx)
208 {
209         struct tur_checker_context *ct = ctx;
210         int state;
211
212         condlog(3, "%d:%d: tur checker starting up", TUR_DEVT(ct));
213
214         ct->message[0] = '\0';
215         /* This thread can be canceled, so setup clean up */
216         tur_thread_cleanup_push(ct)
217
218         /* TUR checker start up */
219         pthread_mutex_lock(&ct->lock);
220         ct->state = PATH_PENDING;
221         pthread_mutex_unlock(&ct->lock);
222
223         state = tur_check(ct->fd, ct->timeout, ct->message);
224
225         /* TUR checker done */
226         pthread_mutex_lock(&ct->lock);
227         ct->state = state;
228         pthread_cond_signal(&ct->active);
229         pthread_mutex_unlock(&ct->lock);
230
231         condlog(3, "%d:%d: tur checker finished, state %s",
232                 TUR_DEVT(ct), checker_state_name(state));
233         tur_thread_cleanup_pop(ct);
234         return ((void *)0);
235 }
236
237
238 static void tur_timeout(struct timespec *tsp)
239 {
240         struct timeval now;
241
242         gettimeofday(&now, NULL);
243         tsp->tv_sec = now.tv_sec;
244         tsp->tv_nsec = now.tv_usec * 1000;
245         tsp->tv_nsec += 1000000; /* 1 millisecond */
246 }
247
248 static void tur_set_async_timeout(struct checker *c)
249 {
250         struct tur_checker_context *ct = c->context;
251         struct timeval now;
252
253         gettimeofday(&now, NULL);
254         ct->time = now.tv_sec + c->timeout;
255 }
256
257 static int tur_check_async_timeout(struct checker *c)
258 {
259         struct tur_checker_context *ct = c->context;
260         struct timeval now;
261
262         gettimeofday(&now, NULL);
263         return (now.tv_sec > ct->time);
264 }
265
266 extern int
267 libcheck_check (struct checker * c)
268 {
269         struct tur_checker_context *ct = c->context;
270         struct timespec tsp;
271         struct stat sb;
272         pthread_attr_t attr;
273         int tur_status, r;
274
275
276         if (!ct)
277                 return PATH_UNCHECKED;
278
279         if (fstat(c->fd, &sb) == 0)
280                 ct->devt = sb.st_rdev;
281
282         if (c->sync)
283                 return tur_check(c->fd, c->timeout, c->message);
284
285         /*
286          * Async mode
287          */
288         r = pthread_mutex_lock(&ct->lock);
289         if (r != 0) {
290                 condlog(2, "%d:%d: tur mutex lock failed with %d",
291                         TUR_DEVT(ct), r);
292                 MSG(c, MSG_TUR_FAILED);
293                 return PATH_WILD;
294         }
295
296         if (ct->running) {
297                 /* Check if TUR checker is still running */
298                 if (ct->thread) {
299                         if (tur_check_async_timeout(c)) {
300                                 condlog(3, "%d:%d: tur checker timeout",
301                                         TUR_DEVT(ct));
302                                 pthread_cancel(ct->thread);
303                                 ct->running = 0;
304                                 MSG(c, MSG_TUR_TIMEOUT);
305                                 tur_status = PATH_TIMEOUT;
306                         } else {
307                                 condlog(3, "%d:%d: tur checker not finished",
308                                         TUR_DEVT(ct));
309                                 ct->running++;
310                                 tur_status = PATH_PENDING;
311                         }
312                 } else {
313                         /* TUR checker done */
314                         ct->running = 0;
315                         tur_status = ct->state;
316                         strncpy(c->message, ct->message, CHECKER_MSG_LEN);
317                         c->message[CHECKER_MSG_LEN - 1] = '\0';
318                 }
319                 pthread_mutex_unlock(&ct->lock);
320         } else {
321                 if (ct->thread) {
322                         /* pthread cancel failed. continue in sync mode */
323                         pthread_mutex_unlock(&ct->lock);
324                         condlog(3, "%d:%d: tur thread not responding",
325                                 TUR_DEVT(ct));
326                         return PATH_TIMEOUT;
327                 }
328                 /* Start new TUR checker */
329                 ct->state = PATH_UNCHECKED;
330                 ct->fd = c->fd;
331                 ct->timeout = c->timeout;
332                 pthread_spin_lock(&ct->hldr_lock);
333                 ct->holders++;
334                 pthread_spin_unlock(&ct->hldr_lock);
335                 tur_set_async_timeout(c);
336                 setup_thread_attr(&attr, 32 * 1024, 1);
337                 r = pthread_create(&ct->thread, &attr, tur_thread, ct);
338                 if (r) {
339                         pthread_spin_lock(&ct->hldr_lock);
340                         ct->holders--;
341                         pthread_spin_unlock(&ct->hldr_lock);
342                         pthread_mutex_unlock(&ct->lock);
343                         ct->thread = 0;
344                         condlog(3, "%d:%d: failed to start tur thread, using"
345                                 " sync mode", TUR_DEVT(ct));
346                         return tur_check(c->fd, c->timeout, c->message);
347                 }
348                 pthread_attr_destroy(&attr);
349                 tur_timeout(&tsp);
350                 r = pthread_cond_timedwait(&ct->active, &ct->lock, &tsp);
351                 tur_status = ct->state;
352                 strncpy(c->message, ct->message,CHECKER_MSG_LEN);
353                 c->message[CHECKER_MSG_LEN - 1] = '\0';
354                 pthread_mutex_unlock(&ct->lock);
355                 if (ct->thread &&
356                     (tur_status == PATH_PENDING || tur_status == PATH_UNCHECKED)) {
357                         condlog(3, "%d:%d: tur checker still running",
358                                 TUR_DEVT(ct));
359                         ct->running = 1;
360                         tur_status = PATH_PENDING;
361                 }
362         }
363
364         return tur_status;
365 }