2 * Some code borrowed from sg-utils.
4 * Copyright (c) 2004 Christophe Varoqui
13 #include <sys/ioctl.h>
14 #include <sys/sysmacros.h>
19 #include <urcu/uatomic.h>
23 #include "../libmultipath/debug.h"
24 #include "../libmultipath/sg_include.h"
25 #include "../libmultipath/util.h"
26 #include "../libmultipath/time-util.h"
27 #include "../libmultipath/util.h"
30 #define HEAVY_CHECK_COUNT 10
32 #define MSG_TUR_UP "tur checker reports path is up"
33 #define MSG_TUR_DOWN "tur checker reports path is down"
34 #define MSG_TUR_GHOST "tur checker reports path is in standby state"
35 #define MSG_TUR_RUNNING "tur checker still running"
36 #define MSG_TUR_TIMEOUT "tur checker timed out"
37 #define MSG_TUR_FAILED "tur checker failed to initialize"
39 struct tur_checker_context {
42 int running; /* uatomic access only */
48 pthread_cond_t active;
49 int holders; /* uatomic access only */
50 char message[CHECKER_MSG_LEN];
53 int libcheck_init (struct checker * c)
55 struct tur_checker_context *ct;
58 ct = malloc(sizeof(struct tur_checker_context));
61 memset(ct, 0, sizeof(struct tur_checker_context));
63 ct->state = PATH_UNCHECKED;
65 uatomic_set(&ct->holders, 1);
66 pthread_cond_init_mono(&ct->active);
67 pthread_mutex_init(&ct->lock, NULL);
68 if (fstat(c->fd, &sb) == 0)
69 ct->devt = sb.st_rdev;
75 static void cleanup_context(struct tur_checker_context *ct)
77 pthread_mutex_destroy(&ct->lock);
78 pthread_cond_destroy(&ct->active);
82 void libcheck_free (struct checker * c)
85 struct tur_checker_context *ct = c->context;
89 running = uatomic_xchg(&ct->running, 0);
91 pthread_cancel(ct->thread);
93 holders = uatomic_sub_return(&ct->holders, 1);
102 tur_check(int fd, unsigned int timeout, char *msg)
104 struct sg_io_hdr io_hdr;
105 unsigned char turCmdBlk[TUR_CMD_LEN] = { 0x00, 0, 0, 0, 0, 0 };
106 unsigned char sense_buffer[32];
110 memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
111 memset(&sense_buffer, 0, 32);
112 io_hdr.interface_id = 'S';
113 io_hdr.cmd_len = sizeof (turCmdBlk);
114 io_hdr.mx_sb_len = sizeof (sense_buffer);
115 io_hdr.dxfer_direction = SG_DXFER_NONE;
116 io_hdr.cmdp = turCmdBlk;
117 io_hdr.sbp = sense_buffer;
118 io_hdr.timeout = timeout * 1000;
120 if (ioctl(fd, SG_IO, &io_hdr) < 0) {
121 snprintf(msg, CHECKER_MSG_LEN, MSG_TUR_DOWN);
124 if ((io_hdr.status & 0x7e) == 0x18) {
126 * SCSI-3 arrays might return
127 * reservation conflict on TUR
129 snprintf(msg, CHECKER_MSG_LEN, MSG_TUR_UP);
132 if (io_hdr.info & SG_INFO_OK_MASK) {
133 int key = 0, asc, ascq;
135 switch (io_hdr.host_status) {
140 case DID_TRANSPORT_FAILFAST:
143 /* Driver error, retry */
148 if (io_hdr.sb_len_wr > 3) {
149 if (io_hdr.sbp[0] == 0x72 || io_hdr.sbp[0] == 0x73) {
150 key = io_hdr.sbp[1] & 0x0f;
152 ascq = io_hdr.sbp[3];
153 } else if (io_hdr.sb_len_wr > 13 &&
154 ((io_hdr.sbp[0] & 0x7f) == 0x70 ||
155 (io_hdr.sbp[0] & 0x7f) == 0x71)) {
156 key = io_hdr.sbp[2] & 0x0f;
157 asc = io_hdr.sbp[12];
158 ascq = io_hdr.sbp[13];
162 /* Unit Attention, retry */
166 else if (key == 0x2) {
168 /* Note: Other ALUA states are either UP or DOWN */
169 if( asc == 0x04 && ascq == 0x0b){
171 * LOGICAL UNIT NOT ACCESSIBLE,
172 * TARGET PORT IN STANDBY STATE
174 snprintf(msg, CHECKER_MSG_LEN, MSG_TUR_GHOST);
178 snprintf(msg, CHECKER_MSG_LEN, MSG_TUR_DOWN);
181 snprintf(msg, CHECKER_MSG_LEN, MSG_TUR_UP);
185 #define tur_thread_cleanup_push(ct) pthread_cleanup_push(cleanup_func, ct)
186 #define tur_thread_cleanup_pop(ct) pthread_cleanup_pop(1)
188 static void cleanup_func(void *data)
191 struct tur_checker_context *ct = data;
193 holders = uatomic_sub_return(&ct->holders, 1);
196 rcu_unregister_thread();
199 static void *tur_thread(void *ctx)
201 struct tur_checker_context *ct = ctx;
203 char msg[CHECKER_MSG_LEN];
205 /* This thread can be canceled, so setup clean up */
206 tur_thread_cleanup_push(ct);
207 rcu_register_thread();
209 condlog(3, "%d:%d : tur checker starting up", major(ct->devt),
212 state = tur_check(ct->fd, ct->timeout, msg);
213 pthread_testcancel();
215 /* TUR checker done */
216 pthread_mutex_lock(&ct->lock);
218 strlcpy(ct->message, msg, sizeof(ct->message));
219 pthread_cond_signal(&ct->active);
220 pthread_mutex_unlock(&ct->lock);
222 condlog(3, "%d:%d : tur checker finished, state %s", major(ct->devt),
223 minor(ct->devt), checker_state_name(state));
225 running = uatomic_xchg(&ct->running, 0);
229 tur_thread_cleanup_pop(ct);
235 static void tur_timeout(struct timespec *tsp)
237 clock_gettime(CLOCK_MONOTONIC, tsp);
238 tsp->tv_nsec += 1000 * 1000; /* 1 millisecond */
239 normalize_timespec(tsp);
242 static void tur_set_async_timeout(struct checker *c)
244 struct tur_checker_context *ct = c->context;
247 clock_gettime(CLOCK_MONOTONIC, &now);
248 ct->time = now.tv_sec + c->timeout;
251 static int tur_check_async_timeout(struct checker *c)
253 struct tur_checker_context *ct = c->context;
256 clock_gettime(CLOCK_MONOTONIC, &now);
257 return (now.tv_sec > ct->time);
260 int libcheck_check(struct checker * c)
262 struct tur_checker_context *ct = c->context;
268 return PATH_UNCHECKED;
271 return tur_check(c->fd, c->timeout, c->message);
277 if (tur_check_async_timeout(c)) {
278 int running = uatomic_xchg(&ct->running, 0);
280 pthread_cancel(ct->thread);
281 condlog(3, "%d:%d : tur checker timeout",
282 major(ct->devt), minor(ct->devt));
284 MSG(c, MSG_TUR_TIMEOUT);
285 tur_status = PATH_TIMEOUT;
286 } else if (uatomic_read(&ct->running) != 0) {
287 condlog(3, "%d:%d : tur checker not finished",
288 major(ct->devt), minor(ct->devt));
289 tur_status = PATH_PENDING;
291 /* TUR checker done */
293 pthread_mutex_lock(&ct->lock);
294 tur_status = ct->state;
295 strlcpy(c->message, ct->message, sizeof(c->message));
296 pthread_mutex_unlock(&ct->lock);
299 if (uatomic_read(&ct->holders) > 1) {
300 /* The thread has been cancelled but hasn't
301 * quilt. Fail back to synchronous mode */
302 condlog(3, "%d:%d : tur checker failing back to sync",
303 major(ct->devt), minor(ct->devt));
304 return tur_check(c->fd, c->timeout, c->message);
306 /* Start new TUR checker */
307 pthread_mutex_lock(&ct->lock);
308 tur_status = ct->state = PATH_PENDING;
309 ct->message[0] = '\0';
310 pthread_mutex_unlock(&ct->lock);
312 ct->timeout = c->timeout;
313 uatomic_add(&ct->holders, 1);
314 uatomic_set(&ct->running, 1);
315 tur_set_async_timeout(c);
316 setup_thread_attr(&attr, 32 * 1024, 1);
317 r = pthread_create(&ct->thread, &attr, tur_thread, ct);
318 pthread_attr_destroy(&attr);
320 uatomic_sub(&ct->holders, 1);
321 uatomic_set(&ct->running, 0);
323 condlog(3, "%d:%d : failed to start tur thread, using"
324 " sync mode", major(ct->devt), minor(ct->devt));
325 return tur_check(c->fd, c->timeout, c->message);
328 pthread_mutex_lock(&ct->lock);
329 if (ct->state == PATH_PENDING)
330 r = pthread_cond_timedwait(&ct->active, &ct->lock,
333 tur_status = ct->state;
334 strlcpy(c->message, ct->message, sizeof(c->message));
336 pthread_mutex_unlock(&ct->lock);
337 if (tur_status == PATH_PENDING) {
338 condlog(3, "%d:%d : tur checker still running",
339 major(ct->devt), minor(ct->devt));
341 int running = uatomic_xchg(&ct->running, 0);
343 pthread_cancel(ct->thread);