Merge branch 'master' of git://git.kernel.org/pub/scm/linux/storage/multipath-tools/
[platform/upstream/multipath-tools.git] / libmultipath / checkers / directio.c
1 /*
2  * Copyright (c) 2005 Hannes Reinecke, Suse
3  */
4 #define _GNU_SOURCE
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/ioctl.h>
13 #include <linux/fs.h>
14 #include <errno.h>
15 #include <asm/unistd.h>
16 #include <libaio.h>
17
18 #include "checkers.h"
19 #include "../libmultipath/debug.h"
20
21 #define MSG_DIRECTIO_UNKNOWN    "directio checker is not available"
22 #define MSG_DIRECTIO_UP         "directio checker reports path is up"
23 #define MSG_DIRECTIO_DOWN       "directio checker reports path is down"
24 #define MSG_DIRECTIO_PENDING    "directio checker is waiting on aio"
25
26 #define LOG(prio, fmt, args...) condlog(prio, "directio: " fmt, ##args)
27
28 struct directio_context {
29         int             running;
30         int             reset_flags;
31         int             blksize;
32         unsigned char * buf;
33         unsigned char * ptr;
34         io_context_t    ioctx;
35         struct iocb     io;
36 };
37
38
39 int libcheck_init (struct checker * c)
40 {
41         unsigned long pgsize = getpagesize();
42         struct directio_context * ct;
43         long flags;
44
45         ct = malloc(sizeof(struct directio_context));
46         if (!ct)
47                 return 1;
48         memset(ct, 0, sizeof(struct directio_context));
49
50         if (io_setup(1, &ct->ioctx) != 0) {
51                 condlog(1, "io_setup failed");
52                 free(ct);
53                 return 1;
54         }
55
56         if (ioctl(c->fd, BLKBSZGET, &ct->blksize) < 0) {
57                 MSG(c, "cannot get blocksize, set default");
58                 ct->blksize = 512;
59         }
60         if (ct->blksize > 4096) {
61                 /*
62                  * Sanity check for DASD; BSZGET is broken
63                  */
64                 ct->blksize = 4096;
65         }
66         if (!ct->blksize)
67                 goto out;
68         ct->buf = (unsigned char *)malloc(ct->blksize + pgsize);
69         if (!ct->buf)
70                 goto out;
71
72         flags = fcntl(c->fd, F_GETFL);
73         if (flags < 0)
74                 goto out;
75         if (!(flags & O_DIRECT)) {
76                 flags |= O_DIRECT;
77                 if (fcntl(c->fd, F_SETFL, flags) < 0)
78                         goto out;
79                 ct->reset_flags = 1;
80         }
81
82         ct->ptr = (unsigned char *) (((unsigned long)ct->buf + pgsize - 1) &
83                   (~(pgsize - 1)));
84
85         /* Sucessfully initialized, return the context. */
86         c->context = (void *) ct;
87         return 0;
88
89 out:
90         if (ct->buf)
91                 free(ct->buf);
92         io_destroy(ct->ioctx);
93         free(ct);
94         return 1;
95 }
96
97 void libcheck_free (struct checker * c)
98 {
99         struct directio_context * ct = (struct directio_context *)c->context;
100         long flags;
101
102         if (!ct)
103                 return;
104
105         if (ct->reset_flags) {
106                 if ((flags = fcntl(c->fd, F_GETFL)) >= 0) {
107                         flags &= ~O_DIRECT;
108                         /* No point in checking for errors */
109                         fcntl(c->fd, F_SETFL, flags);
110                 }
111         }
112
113         if (ct->buf)
114                 free(ct->buf);
115         io_destroy(ct->ioctx);
116         free(ct);
117 }
118
119 static int
120 check_state(int fd, struct directio_context *ct, int sync)
121 {
122         struct timespec timeout = { .tv_nsec = 5 };
123         struct io_event event;
124         struct stat     sb;
125         int             rc = PATH_UNCHECKED;
126         long            r;
127
128         if (fstat(fd, &sb) == 0) {
129                 LOG(4, "called for %x", (unsigned) sb.st_rdev);
130         }
131         if (sync) {
132                 LOG(4, "called in synchronous mode");
133                 timeout.tv_sec  = ASYNC_TIMEOUT_SEC;
134                 timeout.tv_nsec = 0;
135         }
136
137         if (!ct->running) {
138                 struct iocb *ios[1] = { &ct->io };
139
140                 LOG(3, "starting new request");
141                 memset(&ct->io, 0, sizeof(struct iocb));
142                 io_prep_pread(&ct->io, fd, ct->ptr, ct->blksize, 0);
143                 if (io_submit(ct->ioctx, 1, ios) != 1) {
144                         LOG(3, "io_submit error %i", errno);
145                         return PATH_UNCHECKED;
146                 }
147         }
148         ct->running++;
149
150         errno = 0;
151         r = io_getevents(ct->ioctx, 1L, 1L, &event, &timeout);
152
153         if (r < 0 ) {
154                 LOG(3, "async io getevents returned %li (errno=%s)", r,
155                     strerror(errno));
156                 rc = PATH_UNCHECKED;
157         } else if (r < 1L) {
158                 if (ct->running > ASYNC_TIMEOUT_SEC || sync) {
159                         LOG(3, "abort check on timeout");
160                         rc = PATH_DOWN;
161                 } else {
162                         LOG(3, "async io pending");
163                         rc = PATH_PENDING;
164                 }
165         } else {
166                 LOG(3, "io finished %lu/%lu", event.res, event.res2);
167                 ct->running = 0;
168                 rc = (event.res == ct->blksize) ? PATH_UP : PATH_DOWN;
169         }
170
171         return rc;
172 }
173
174 int libcheck_check (struct checker * c)
175 {
176         int ret;
177         struct directio_context * ct = (struct directio_context *)c->context;
178
179         if (!ct)
180                 return PATH_UNCHECKED;
181
182         ret = check_state(c->fd, ct, c->sync);
183
184         switch (ret)
185         {
186         case PATH_UNCHECKED:
187                 MSG(c, MSG_DIRECTIO_UNKNOWN);
188                 break;
189         case PATH_DOWN:
190                 MSG(c, MSG_DIRECTIO_DOWN);
191                 break;
192         case PATH_UP:
193                 MSG(c, MSG_DIRECTIO_UP);
194                 break;
195         case PATH_PENDING:
196                 MSG(c, MSG_DIRECTIO_PENDING);
197                 break;
198         default:
199                 break;
200         }
201         return ret;
202 }