Merge remote-tracking branch 'hannes/for-christophe'
[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 <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, int timeout_secs)
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 > 0) {
132                 LOG(4, "called in synchronous mode");
133                 timeout.tv_sec  = timeout_secs;
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                 ct->running = 0;
157                 rc = PATH_UNCHECKED;
158         } else if (r < 1L) {
159                 if (ct->running > timeout_secs || sync) {
160                         struct iocb *ios[1] = { &ct->io };
161
162                         LOG(3, "abort check on timeout");
163                         r = io_cancel(ct->ioctx, ios[0], &event);
164                         /*
165                          * Only reset ct->running if we really
166                          * could abort the pending I/O
167                          */
168                         if (r)
169                                 LOG(3, "io_cancel error %i", errno);
170                         else
171                                 ct->running = 0;
172                         rc = PATH_DOWN;
173                 } else {
174                         LOG(3, "async io pending");
175                         rc = PATH_PENDING;
176                 }
177         } else {
178                 LOG(3, "io finished %lu/%lu", event.res, event.res2);
179                 ct->running = 0;
180                 rc = (event.res == ct->blksize) ? PATH_UP : PATH_DOWN;
181         }
182
183         return rc;
184 }
185
186 int libcheck_check (struct checker * c)
187 {
188         int ret;
189         struct directio_context * ct = (struct directio_context *)c->context;
190
191         if (!ct)
192                 return PATH_UNCHECKED;
193
194         ret = check_state(c->fd, ct, c->sync, c->timeout);
195
196         switch (ret)
197         {
198         case PATH_UNCHECKED:
199                 MSG(c, MSG_DIRECTIO_UNKNOWN);
200                 break;
201         case PATH_DOWN:
202                 MSG(c, MSG_DIRECTIO_DOWN);
203                 break;
204         case PATH_UP:
205                 MSG(c, MSG_DIRECTIO_UP);
206                 break;
207         case PATH_PENDING:
208                 MSG(c, MSG_DIRECTIO_PENDING);
209                 break;
210         default:
211                 break;
212         }
213         return ret;
214 }