2 * Copyright (c) 2005 Hannes Reinecke, Suse
12 #include <sys/ioctl.h>
15 #include <linux/kdev_t.h>
16 #include <asm/unistd.h>
20 #include "../libmultipath/debug.h"
22 #define MSG_DIRECTIO_UNKNOWN "directio checker is not available"
23 #define MSG_DIRECTIO_UP "directio checker reports path is up"
24 #define MSG_DIRECTIO_DOWN "directio checker reports path is down"
25 #define MSG_DIRECTIO_PENDING "directio checker is waiting on aio"
27 #define LOG(prio, fmt, args...) condlog(prio, "directio: " fmt, ##args)
29 struct directio_context {
40 int libcheck_init (struct checker * c)
42 unsigned long pgsize = getpagesize();
43 struct directio_context * ct;
46 ct = malloc(sizeof(struct directio_context));
49 memset(ct, 0, sizeof(struct directio_context));
51 if (io_setup(1, &ct->ioctx) != 0) {
52 condlog(1, "io_setup failed");
57 if (ioctl(c->fd, BLKBSZGET, &ct->blksize) < 0) {
58 MSG(c, "cannot get blocksize, set default");
61 if (ct->blksize > 4096) {
63 * Sanity check for DASD; BSZGET is broken
69 ct->buf = (unsigned char *)malloc(ct->blksize + pgsize);
73 flags = fcntl(c->fd, F_GETFL);
76 if (!(flags & O_DIRECT)) {
78 if (fcntl(c->fd, F_SETFL, flags) < 0)
83 ct->ptr = (unsigned char *) (((unsigned long)ct->buf + pgsize - 1) &
86 /* Sucessfully initialized, return the context. */
87 c->context = (void *) ct;
93 io_destroy(ct->ioctx);
98 void libcheck_free (struct checker * c)
100 struct directio_context * ct = (struct directio_context *)c->context;
106 if (ct->reset_flags) {
107 if ((flags = fcntl(c->fd, F_GETFL)) >= 0) {
109 /* No point in checking for errors */
110 fcntl(c->fd, F_SETFL, flags);
116 io_destroy(ct->ioctx);
121 check_state(int fd, struct directio_context *ct, int sync)
123 struct timespec timeout = { .tv_nsec = 5 };
124 struct io_event event;
126 int rc = PATH_UNCHECKED;
129 if (fstat(fd, &sb) == 0) {
130 LOG(4, "called for %x", (unsigned) sb.st_rdev);
133 LOG(4, "called in synchronous mode");
134 timeout.tv_sec = ASYNC_TIMEOUT_SEC;
139 struct iocb *ios[1] = { &ct->io };
141 LOG(3, "starting new request");
142 memset(&ct->io, 0, sizeof(struct iocb));
143 io_prep_pread(&ct->io, fd, ct->ptr, ct->blksize, 0);
144 if (io_submit(ct->ioctx, 1, ios) != 1) {
145 LOG(3, "io_submit error %i", errno);
146 return PATH_UNCHECKED;
151 r = io_getevents(ct->ioctx, 1L, 1L, &event, &timeout);
152 LOG(3, "async io getevents returns %li (errno=%s)", r, strerror(errno));
155 if (ct->running > ASYNC_TIMEOUT_SEC || sync) {
156 LOG(3, "abort check on timeout");
161 LOG(3, "io finished %lu/%lu", event.res, event.res2);
163 rc = (event.res == ct->blksize) ? PATH_UP : PATH_DOWN;
169 int libcheck_check (struct checker * c)
172 struct directio_context * ct = (struct directio_context *)c->context;
175 return PATH_UNCHECKED;
177 ret = check_state(c->fd, ct, c->sync);
182 MSG(c, MSG_DIRECTIO_UNKNOWN);
185 MSG(c, MSG_DIRECTIO_DOWN);
188 MSG(c, MSG_DIRECTIO_UP);
191 MSG(c, MSG_DIRECTIO_PENDING);