2 * Some code borrowed from sg-utils.
4 * Copyright (c) 2004 Christophe Varoqui
13 #include <sys/ioctl.h>
20 #include "../libmultipath/debug.h"
21 #include "../libmultipath/sg_include.h"
22 #include "../libmultipath/uevent.h"
25 #define HEAVY_CHECK_COUNT 10
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"
34 struct tur_checker_context {
43 pthread_cond_t active;
44 pthread_spinlock_t hldr_lock;
46 char message[CHECKER_MSG_LEN];
49 #define TUR_DEVT(c) major((c)->devt), minor((c)->devt)
51 int libcheck_init (struct checker * c)
53 struct tur_checker_context *ct;
55 ct = malloc(sizeof(struct tur_checker_context));
58 memset(ct, 0, sizeof(struct tur_checker_context));
60 ct->state = PATH_UNCHECKED;
63 pthread_cond_init(&ct->active, NULL);
64 pthread_mutex_init(&ct->lock, NULL);
65 pthread_spin_init(&ct->hldr_lock, PTHREAD_PROCESS_PRIVATE);
71 void cleanup_context(struct tur_checker_context *ct)
73 pthread_mutex_destroy(&ct->lock);
74 pthread_cond_destroy(&ct->active);
75 pthread_spin_destroy(&ct->hldr_lock);
79 void libcheck_free (struct checker * c)
82 struct tur_checker_context *ct = c->context;
86 pthread_spin_lock(&ct->hldr_lock);
88 holders = ct->holders;
90 pthread_spin_unlock(&ct->hldr_lock);
92 pthread_cancel(thread);
100 #define TUR_MSG(msg, fmt, args...) snprintf(msg, CHECKER_MSG_LEN, fmt, ##args);
103 tur_check(int fd, unsigned int timeout, char *msg)
105 struct sg_io_hdr io_hdr;
106 unsigned char turCmdBlk[TUR_CMD_LEN] = { 0x00, 0, 0, 0, 0, 0 };
107 unsigned char sense_buffer[32];
111 memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
112 memset(&sense_buffer, 0, 32);
113 io_hdr.interface_id = 'S';
114 io_hdr.cmd_len = sizeof (turCmdBlk);
115 io_hdr.mx_sb_len = sizeof (sense_buffer);
116 io_hdr.dxfer_direction = SG_DXFER_NONE;
117 io_hdr.cmdp = turCmdBlk;
118 io_hdr.sbp = sense_buffer;
119 io_hdr.timeout = timeout * 1000;
121 if (ioctl(fd, SG_IO, &io_hdr) < 0) {
122 TUR_MSG(msg, MSG_TUR_DOWN);
125 if ((io_hdr.status & 0x7e) == 0x18) {
127 * SCSI-3 arrays might return
128 * reservation conflict on TUR
130 TUR_MSG(msg, MSG_TUR_UP);
133 if (io_hdr.info & SG_INFO_OK_MASK) {
134 int key = 0, asc, ascq;
136 switch (io_hdr.host_status) {
141 case DID_TRANSPORT_FAILFAST:
144 /* Driver error, retry */
149 if (io_hdr.sb_len_wr > 3) {
150 if (io_hdr.sbp[0] == 0x72 || io_hdr.sbp[0] == 0x73) {
151 key = io_hdr.sbp[1] & 0x0f;
153 ascq = io_hdr.sbp[3];
154 } else if (io_hdr.sb_len_wr > 13 &&
155 ((io_hdr.sbp[0] & 0x7f) == 0x70 ||
156 (io_hdr.sbp[0] & 0x7f) == 0x71)) {
157 key = io_hdr.sbp[2] & 0x0f;
158 asc = io_hdr.sbp[12];
159 ascq = io_hdr.sbp[13];
163 /* Unit Attention, retry */
167 else if (key == 0x2) {
169 /* Note: Other ALUA states are either UP or DOWN */
170 if( asc == 0x04 && ascq == 0x0b){
172 * LOGICAL UNIT NOT ACCESSIBLE,
173 * TARGET PORT IN STANDBY STATE
175 TUR_MSG(msg, MSG_TUR_GHOST);
179 TUR_MSG(msg, MSG_TUR_DOWN);
182 TUR_MSG(msg, MSG_TUR_UP);
186 #define tur_thread_cleanup_push(ct) pthread_cleanup_push(cleanup_func, ct)
187 #define tur_thread_cleanup_pop(ct) pthread_cleanup_pop(1)
189 void cleanup_func(void *data)
192 struct tur_checker_context *ct = data;
193 pthread_spin_lock(&ct->hldr_lock);
195 holders = ct->holders;
197 pthread_spin_unlock(&ct->hldr_lock);
202 void *tur_thread(void *ctx)
204 struct tur_checker_context *ct = ctx;
207 condlog(3, "%d:%d: tur checker starting up", TUR_DEVT(ct));
209 ct->message[0] = '\0';
210 /* This thread can be canceled, so setup clean up */
211 tur_thread_cleanup_push(ct)
213 /* TUR checker start up */
214 pthread_mutex_lock(&ct->lock);
215 ct->state = PATH_PENDING;
216 pthread_mutex_unlock(&ct->lock);
218 state = tur_check(ct->fd, ct->timeout, ct->message);
220 /* TUR checker done */
221 pthread_mutex_lock(&ct->lock);
223 pthread_mutex_unlock(&ct->lock);
224 pthread_cond_signal(&ct->active);
226 condlog(3, "%d:%d: tur checker finished, state %s",
227 TUR_DEVT(ct), checker_state_name(state));
228 tur_thread_cleanup_pop(ct);
233 void tur_timeout(struct timespec *tsp)
237 gettimeofday(&now, NULL);
238 tsp->tv_sec = now.tv_sec;
239 tsp->tv_nsec = now.tv_usec * 1000;
240 tsp->tv_nsec += 1000000; /* 1 millisecond */
243 void tur_set_async_timeout(struct checker *c)
245 struct tur_checker_context *ct = c->context;
248 gettimeofday(&now, NULL);
249 ct->time = now.tv_sec + c->timeout;
252 int tur_check_async_timeout(struct checker *c)
254 struct tur_checker_context *ct = c->context;
257 gettimeofday(&now, NULL);
258 return (now.tv_sec > ct->time);
262 libcheck_check (struct checker * c)
264 struct tur_checker_context *ct = c->context;
272 return PATH_UNCHECKED;
274 if (fstat(c->fd, &sb) == 0)
275 ct->devt = sb.st_rdev;
278 return tur_check(c->fd, c->timeout, c->message);
283 r = pthread_mutex_lock(&ct->lock);
285 condlog(2, "%d:%d: tur mutex lock failed with %d",
287 MSG(c, MSG_TUR_FAILED);
292 /* Check if TUR checker is still running */
294 if (tur_check_async_timeout(c)) {
295 condlog(3, "%d:%d: tur checker timeout",
297 pthread_cancel(ct->thread);
299 MSG(c, MSG_TUR_TIMEOUT);
300 tur_status = PATH_TIMEOUT;
301 ct->state = PATH_UNCHECKED;
303 condlog(3, "%d:%d: tur checker not finished",
306 tur_status = PATH_PENDING;
309 /* TUR checker done */
311 tur_status = ct->state;
312 strncpy(c->message, ct->message, CHECKER_MSG_LEN);
313 c->message[CHECKER_MSG_LEN - 1] = '\0';
315 pthread_mutex_unlock(&ct->lock);
318 /* pthread cancel failed. continue in sync mode */
319 pthread_mutex_unlock(&ct->lock);
320 condlog(3, "%d:%d: tur thread not responding, "
321 "using sync mode", TUR_DEVT(ct));
322 return tur_check(c->fd, c->timeout, c->message);
324 /* Start new TUR checker */
325 ct->state = PATH_UNCHECKED;
327 ct->timeout = c->timeout;
328 pthread_spin_lock(&ct->hldr_lock);
330 pthread_spin_unlock(&ct->hldr_lock);
331 tur_set_async_timeout(c);
332 setup_thread_attr(&attr, 32 * 1024, 1);
333 r = pthread_create(&ct->thread, &attr, tur_thread, ct);
335 pthread_mutex_unlock(&ct->lock);
338 condlog(3, "%d:%d: failed to start tur thread, using"
339 " sync mode", TUR_DEVT(ct));
340 return tur_check(c->fd, c->timeout, c->message);
342 pthread_attr_destroy(&attr);
344 r = pthread_cond_timedwait(&ct->active, &ct->lock, &tsp);
345 tur_status = ct->state;
346 strncpy(c->message, ct->message,CHECKER_MSG_LEN);
347 c->message[CHECKER_MSG_LEN -1] = '\0';
348 pthread_mutex_unlock(&ct->lock);
350 (tur_status == PATH_PENDING || tur_status == PATH_UNCHECKED)) {
351 condlog(3, "%d:%d: tur checker still running",
354 tur_status = PATH_PENDING;