416e13a1626b154f1bd137a83807c76cef6b43de
[multipath-tools/.git] / libmultipath / io_err_stat.c
1 /*
2  * (C) Copyright HUAWEI Technology Corp. 2017, All Rights Reserved.
3  *
4  * io_err_stat.c
5  * version 1.0
6  *
7  * IO error stream statistic process for path failure event from kernel
8  *
9  * Author(s): Guan Junxiong 2017 <guanjunxiong@huawei.com>
10  *
11  * This file is released under the GPL version 2, or any later version.
12  */
13
14 #include <unistd.h>
15 #include <pthread.h>
16 #include <signal.h>
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
20 #include <linux/fs.h>
21 #include <libaio.h>
22 #include <errno.h>
23 #include <sys/mman.h>
24 #include <sys/select.h>
25
26 #include "vector.h"
27 #include "memory.h"
28 #include "checkers.h"
29 #include "config.h"
30 #include "structs.h"
31 #include "structs_vec.h"
32 #include "devmapper.h"
33 #include "debug.h"
34 #include "lock.h"
35 #include "time-util.h"
36 #include "io_err_stat.h"
37
38 #define IOTIMEOUT_SEC                   60
39 #define TIMEOUT_NO_IO_NSEC              10000000 /*10ms = 10000000ns*/
40 #define FLAKY_PATHFAIL_THRESHOLD        2
41 #define CONCUR_NR_EVENT                 32
42
43 #define PATH_IO_ERR_IN_CHECKING         -1
44 #define PATH_IO_ERR_IN_POLLING_RECHECK  -2
45
46 #define io_err_stat_log(prio, fmt, args...) \
47         condlog(prio, "io error statistic: " fmt, ##args)
48
49
50 struct io_err_stat_pathvec {
51         pthread_mutex_t mutex;
52         vector          pathvec;
53 };
54
55 struct dio_ctx {
56         struct timespec io_starttime;
57         int             blksize;
58         void            *buf;
59         struct iocb     io;
60 };
61
62 struct io_err_stat_path {
63         char            devname[FILE_NAME_SIZE];
64         int             fd;
65         struct dio_ctx  *dio_ctx_array;
66         int             io_err_nr;
67         int             io_nr;
68         struct timespec start_time;
69
70         int             total_time;
71         int             err_rate_threshold;
72 };
73
74 pthread_t               io_err_stat_thr;
75 pthread_attr_t          io_err_stat_attr;
76
77 static pthread_mutex_t io_err_thread_lock = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_cond_t io_err_thread_cond = PTHREAD_COND_INITIALIZER;
79 static int io_err_thread_running = 0;
80
81 static struct io_err_stat_pathvec *paths;
82 struct vectors *vecs;
83 io_context_t    ioctx;
84
85 static void cancel_inflight_io(struct io_err_stat_path *pp);
86
87 static void rcu_unregister(void *param)
88 {
89         rcu_unregister_thread();
90 }
91
92 struct io_err_stat_path *find_err_path_by_dev(vector pathvec, char *dev)
93 {
94         int i;
95         struct io_err_stat_path *pp;
96
97         if (!pathvec)
98                 return NULL;
99         vector_foreach_slot(pathvec, pp, i)
100                 if (!strcmp(pp->devname, dev))
101                         return pp;
102
103         io_err_stat_log(4, "%s: not found in check queue", dev);
104
105         return NULL;
106 }
107
108 static int init_each_dio_ctx(struct dio_ctx *ct, int blksize,
109                 unsigned long pgsize)
110 {
111         ct->blksize = blksize;
112         if (posix_memalign(&ct->buf, pgsize, blksize))
113                 return 1;
114         memset(ct->buf, 0, blksize);
115         ct->io_starttime.tv_sec = 0;
116         ct->io_starttime.tv_nsec = 0;
117
118         return 0;
119 }
120
121 static void deinit_each_dio_ctx(struct dio_ctx *ct)
122 {
123         if (ct->buf)
124                 free(ct->buf);
125 }
126
127 static int setup_directio_ctx(struct io_err_stat_path *p)
128 {
129         unsigned long pgsize = getpagesize();
130         char fpath[PATH_MAX];
131         int blksize = 0;
132         int i;
133
134         if (snprintf(fpath, PATH_MAX, "/dev/%s", p->devname) >= PATH_MAX)
135                 return 1;
136         if (p->fd < 0)
137                 p->fd = open(fpath, O_RDONLY | O_DIRECT);
138         if (p->fd < 0)
139                 return 1;
140
141         p->dio_ctx_array = MALLOC(sizeof(struct dio_ctx) * CONCUR_NR_EVENT);
142         if (!p->dio_ctx_array)
143                 goto fail_close;
144
145         if (ioctl(p->fd, BLKBSZGET, &blksize) < 0) {
146                 io_err_stat_log(4, "%s:cannot get blocksize, set default 512",
147                                 p->devname);
148                 blksize = 512;
149         }
150         if (!blksize)
151                 goto free_pdctx;
152
153         for (i = 0; i < CONCUR_NR_EVENT; i++) {
154                 if (init_each_dio_ctx(p->dio_ctx_array + i, blksize, pgsize))
155                         goto deinit;
156         }
157         return 0;
158
159 deinit:
160         for (i = 0; i < CONCUR_NR_EVENT; i++)
161                 deinit_each_dio_ctx(p->dio_ctx_array + i);
162 free_pdctx:
163         FREE(p->dio_ctx_array);
164 fail_close:
165         close(p->fd);
166
167         return 1;
168 }
169
170 static void destroy_directio_ctx(struct io_err_stat_path *p)
171 {
172         int i;
173
174         if (!p || !p->dio_ctx_array)
175                 return;
176         cancel_inflight_io(p);
177
178         for (i = 0; i < CONCUR_NR_EVENT; i++)
179                 deinit_each_dio_ctx(p->dio_ctx_array + i);
180         FREE(p->dio_ctx_array);
181
182         if (p->fd > 0)
183                 close(p->fd);
184 }
185
186 static struct io_err_stat_path *alloc_io_err_stat_path(void)
187 {
188         struct io_err_stat_path *p;
189
190         p = (struct io_err_stat_path *)MALLOC(sizeof(*p));
191         if (!p)
192                 return NULL;
193
194         memset(p->devname, 0, sizeof(p->devname));
195         p->io_err_nr = 0;
196         p->io_nr = 0;
197         p->total_time = 0;
198         p->start_time.tv_sec = 0;
199         p->start_time.tv_nsec = 0;
200         p->err_rate_threshold = 0;
201         p->fd = -1;
202
203         return p;
204 }
205
206 static void free_io_err_stat_path(struct io_err_stat_path *p)
207 {
208         FREE(p);
209 }
210
211 static struct io_err_stat_pathvec *alloc_pathvec(void)
212 {
213         struct io_err_stat_pathvec *p;
214         int r;
215
216         p = (struct io_err_stat_pathvec *)MALLOC(sizeof(*p));
217         if (!p)
218                 return NULL;
219         p->pathvec = vector_alloc();
220         if (!p->pathvec)
221                 goto out_free_struct_pathvec;
222         r = pthread_mutex_init(&p->mutex, NULL);
223         if (r)
224                 goto out_free_member_pathvec;
225
226         return p;
227
228 out_free_member_pathvec:
229         vector_free(p->pathvec);
230 out_free_struct_pathvec:
231         FREE(p);
232         return NULL;
233 }
234
235 static void free_io_err_pathvec(struct io_err_stat_pathvec *p)
236 {
237         struct io_err_stat_path *path;
238         int i;
239
240         if (!p)
241                 return;
242         pthread_mutex_destroy(&p->mutex);
243         if (!p->pathvec) {
244                 vector_foreach_slot(p->pathvec, path, i) {
245                         destroy_directio_ctx(path);
246                         free_io_err_stat_path(path);
247                 }
248                 vector_free(p->pathvec);
249         }
250         FREE(p);
251 }
252
253 /*
254  * return value
255  * 0: enqueue OK
256  * 1: fails because of internal error
257  */
258 static int enqueue_io_err_stat_by_path(struct path *path)
259 {
260         struct io_err_stat_path *p;
261
262         pthread_mutex_lock(&paths->mutex);
263         p = find_err_path_by_dev(paths->pathvec, path->dev);
264         if (p) {
265                 pthread_mutex_unlock(&paths->mutex);
266                 return 0;
267         }
268         pthread_mutex_unlock(&paths->mutex);
269
270         p = alloc_io_err_stat_path();
271         if (!p)
272                 return 1;
273
274         memcpy(p->devname, path->dev, sizeof(p->devname));
275         p->total_time = path->mpp->marginal_path_err_sample_time;
276         p->err_rate_threshold = path->mpp->marginal_path_err_rate_threshold;
277
278         if (setup_directio_ctx(p))
279                 goto free_ioerr_path;
280         pthread_mutex_lock(&paths->mutex);
281         if (!vector_alloc_slot(paths->pathvec))
282                 goto unlock_destroy;
283         vector_set_slot(paths->pathvec, p);
284         pthread_mutex_unlock(&paths->mutex);
285
286         if (!path->io_err_disable_reinstate) {
287                 /*
288                  *fail the path in the kernel for the time of the to make
289                  *the test more reliable
290                  */
291                 io_err_stat_log(3, "%s: fail dm path %s before checking",
292                                 path->mpp->alias, path->dev);
293                 path->io_err_disable_reinstate = 1;
294                 dm_fail_path(path->mpp->alias, path->dev_t);
295                 update_queue_mode_del_path(path->mpp);
296
297                 /*
298                  * schedule path check as soon as possible to
299                  * update path state to delayed state
300                  */
301                 path->tick = 1;
302
303         }
304         io_err_stat_log(2, "%s: enqueue path %s to check",
305                         path->mpp->alias, path->dev);
306         return 0;
307
308 unlock_destroy:
309         pthread_mutex_unlock(&paths->mutex);
310         destroy_directio_ctx(p);
311 free_ioerr_path:
312         free_io_err_stat_path(p);
313
314         return 1;
315 }
316
317 int io_err_stat_handle_pathfail(struct path *path)
318 {
319         struct timespec curr_time;
320         int res;
321
322         if (uatomic_read(&io_err_thread_running) == 0)
323                 return 1;
324
325         if (path->io_err_disable_reinstate) {
326                 io_err_stat_log(3, "%s: reinstate is already disabled",
327                                 path->dev);
328                 return 1;
329         }
330         if (path->io_err_pathfail_cnt < 0)
331                 return 1;
332
333         if (!path->mpp)
334                 return 1;
335         if (path->mpp->nr_active <= 1)
336                 return 1;
337         if (path->mpp->marginal_path_double_failed_time <= 0 ||
338                 path->mpp->marginal_path_err_sample_time <= 0 ||
339                 path->mpp->marginal_path_err_recheck_gap_time <= 0 ||
340                 path->mpp->marginal_path_err_rate_threshold < 0) {
341                 io_err_stat_log(4, "%s: parameter not set", path->mpp->alias);
342                 return 1;
343         }
344         if (path->mpp->marginal_path_err_sample_time < (2 * IOTIMEOUT_SEC)) {
345                 io_err_stat_log(2, "%s: marginal_path_err_sample_time should not less than %d",
346                                 path->mpp->alias, 2 * IOTIMEOUT_SEC);
347                 return 1;
348         }
349         /*
350          * The test should only be started for paths that have failed
351          * repeatedly in a certain time frame, so that we have reason
352          * to assume they're flaky. Without bother the admin to configure
353          * the repeated count threshold and time frame, we assume a path
354          * which fails at least twice within 60 seconds is flaky.
355          */
356         if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0)
357                 return 1;
358         if (path->io_err_pathfail_cnt == 0) {
359                 path->io_err_pathfail_cnt++;
360                 path->io_err_pathfail_starttime = curr_time.tv_sec;
361                 io_err_stat_log(5, "%s: start path flakiness pre-checking",
362                                 path->dev);
363                 return 0;
364         }
365         if ((curr_time.tv_sec - path->io_err_pathfail_starttime) >
366                         path->mpp->marginal_path_double_failed_time) {
367                 path->io_err_pathfail_cnt = 0;
368                 path->io_err_pathfail_starttime = curr_time.tv_sec;
369                 io_err_stat_log(5, "%s: restart path flakiness pre-checking",
370                                 path->dev);
371         }
372         path->io_err_pathfail_cnt++;
373         if (path->io_err_pathfail_cnt >= FLAKY_PATHFAIL_THRESHOLD) {
374                 res = enqueue_io_err_stat_by_path(path);
375                 if (!res)
376                         path->io_err_pathfail_cnt = PATH_IO_ERR_IN_CHECKING;
377                 else
378                         path->io_err_pathfail_cnt = 0;
379         }
380
381         return 0;
382 }
383
384 int hit_io_err_recheck_time(struct path *pp)
385 {
386         struct timespec curr_time;
387         int r;
388
389         if (uatomic_read(&io_err_thread_running) == 0)
390                 return 0;
391         if (pp->mpp->nr_active <= 0) {
392                 io_err_stat_log(2, "%s: recover path early", pp->dev);
393                 goto recover;
394         }
395         if (pp->io_err_pathfail_cnt != PATH_IO_ERR_IN_POLLING_RECHECK)
396                 return 1;
397         if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0 ||
398             (curr_time.tv_sec - pp->io_err_dis_reinstate_time) >
399                         pp->mpp->marginal_path_err_recheck_gap_time) {
400                 io_err_stat_log(4, "%s: reschedule checking after %d seconds",
401                                 pp->dev,
402                                 pp->mpp->marginal_path_err_recheck_gap_time);
403                 r = enqueue_io_err_stat_by_path(pp);
404                 /*
405                  * Enqueue fails because of internal error.
406                  * In this case , we recover this path
407                  * Or else,  return 1 to set path state to PATH_SHAKY
408                  */
409                 if (r == 1) {
410                         io_err_stat_log(3, "%s: enqueue fails, to recover",
411                                         pp->dev);
412                         goto recover;
413                 } else
414                         pp->io_err_pathfail_cnt = PATH_IO_ERR_IN_CHECKING;
415         }
416
417         return 1;
418
419 recover:
420         pp->io_err_pathfail_cnt = 0;
421         pp->io_err_disable_reinstate = 0;
422         return 0;
423 }
424
425 static int delete_io_err_stat_by_addr(struct io_err_stat_path *p)
426 {
427         int i;
428
429         i = find_slot(paths->pathvec, p);
430         if (i != -1)
431                 vector_del_slot(paths->pathvec, i);
432
433         destroy_directio_ctx(p);
434         free_io_err_stat_path(p);
435
436         return 0;
437 }
438
439 static void account_async_io_state(struct io_err_stat_path *pp, int rc)
440 {
441         switch (rc) {
442         case PATH_DOWN:
443         case PATH_TIMEOUT:
444                 pp->io_err_nr++;
445                 break;
446         case PATH_UNCHECKED:
447         case PATH_UP:
448         case PATH_PENDING:
449                 break;
450         default:
451                 break;
452         }
453 }
454
455 static int poll_io_err_stat(struct vectors *vecs, struct io_err_stat_path *pp)
456 {
457         struct timespec currtime, difftime;
458         struct path *path;
459         double err_rate;
460
461         if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
462                 return 1;
463         timespecsub(&currtime, &pp->start_time, &difftime);
464         if (difftime.tv_sec < pp->total_time)
465                 return 0;
466
467         io_err_stat_log(4, "%s: check end", pp->devname);
468
469         err_rate = pp->io_nr == 0 ? 0 : (pp->io_err_nr * 1000.0f) / pp->io_nr;
470         io_err_stat_log(3, "%s: IO error rate (%.1f/1000)",
471                         pp->devname, err_rate);
472         pthread_cleanup_push(cleanup_lock, &vecs->lock);
473         lock(&vecs->lock);
474         pthread_testcancel();
475         path = find_path_by_dev(vecs->pathvec, pp->devname);
476         if (!path) {
477                 io_err_stat_log(4, "path %s not found'", pp->devname);
478         } else if (err_rate <= pp->err_rate_threshold) {
479                 path->io_err_pathfail_cnt = 0;
480                 path->io_err_disable_reinstate = 0;
481                 io_err_stat_log(3, "%s: (%d/%d) good to enable reinstating",
482                                 pp->devname, pp->io_err_nr, pp->io_nr);
483                 /*
484                  * schedule path check as soon as possible to
485                  * update path state. Do NOT reinstate dm path here
486                  */
487                 path->tick = 1;
488
489         } else if (path->mpp && path->mpp->nr_active > 1) {
490                 io_err_stat_log(3, "%s: keep failing the dm path %s",
491                                 path->mpp->alias, path->dev);
492                 path->io_err_pathfail_cnt = PATH_IO_ERR_IN_POLLING_RECHECK;
493                 path->io_err_disable_reinstate = 1;
494                 path->io_err_dis_reinstate_time = currtime.tv_sec;
495                 io_err_stat_log(3, "%s: disable reinstating of %s",
496                                 path->mpp->alias, path->dev);
497         } else {
498                 path->io_err_pathfail_cnt = 0;
499                 path->io_err_disable_reinstate = 0;
500                 io_err_stat_log(3, "%s: there is orphan path, enable reinstating",
501                                 pp->devname);
502         }
503         lock_cleanup_pop(vecs->lock);
504
505         delete_io_err_stat_by_addr(pp);
506
507         return 0;
508 }
509
510 static int send_each_async_io(struct dio_ctx *ct, int fd, char *dev)
511 {
512         int rc = -1;
513
514         if (ct->io_starttime.tv_nsec == 0 &&
515                         ct->io_starttime.tv_sec == 0) {
516                 struct iocb *ios[1] = { &ct->io };
517
518                 if (clock_gettime(CLOCK_MONOTONIC, &ct->io_starttime) != 0) {
519                         ct->io_starttime.tv_sec = 0;
520                         ct->io_starttime.tv_nsec = 0;
521                         return rc;
522                 }
523                 io_prep_pread(&ct->io, fd, ct->buf, ct->blksize, 0);
524                 if (io_submit(ioctx, 1, ios) != 1) {
525                         io_err_stat_log(5, "%s: io_submit error %i",
526                                         dev, errno);
527                         return rc;
528                 }
529                 rc = 0;
530         }
531
532         return rc;
533 }
534
535 static void send_batch_async_ios(struct io_err_stat_path *pp)
536 {
537         int i;
538         struct dio_ctx *ct;
539         struct timespec currtime, difftime;
540
541         if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
542                 return;
543         /*
544          * Give a free time for all IO to complete or timeout
545          */
546         if (pp->start_time.tv_sec != 0) {
547                 timespecsub(&currtime, &pp->start_time, &difftime);
548                 if (difftime.tv_sec + IOTIMEOUT_SEC >= pp->total_time)
549                         return;
550         }
551
552         for (i = 0; i < CONCUR_NR_EVENT; i++) {
553                 ct = pp->dio_ctx_array + i;
554                 if (!send_each_async_io(ct, pp->fd, pp->devname))
555                         pp->io_nr++;
556         }
557         if (pp->start_time.tv_sec == 0 && pp->start_time.tv_nsec == 0 &&
558                 clock_gettime(CLOCK_MONOTONIC, &pp->start_time)) {
559                 pp->start_time.tv_sec = 0;
560                 pp->start_time.tv_nsec = 0;
561         }
562 }
563
564 static int try_to_cancel_timeout_io(struct dio_ctx *ct, struct timespec *t,
565                 char *dev)
566 {
567         struct timespec difftime;
568         struct io_event event;
569         int             rc = PATH_UNCHECKED;
570         int             r;
571
572         if (ct->io_starttime.tv_sec == 0)
573                 return rc;
574         timespecsub(t, &ct->io_starttime, &difftime);
575         if (difftime.tv_sec > IOTIMEOUT_SEC) {
576                 struct iocb *ios[1] = { &ct->io };
577
578                 io_err_stat_log(5, "%s: abort check on timeout", dev);
579                 r = io_cancel(ioctx, ios[0], &event);
580                 if (r)
581                         io_err_stat_log(5, "%s: io_cancel error %i",
582                                         dev, errno);
583                 ct->io_starttime.tv_sec = 0;
584                 ct->io_starttime.tv_nsec = 0;
585                 rc = PATH_TIMEOUT;
586         } else {
587                 rc = PATH_PENDING;
588         }
589
590         return rc;
591 }
592
593 static void poll_async_io_timeout(void)
594 {
595         struct io_err_stat_path *pp;
596         struct timespec curr_time;
597         int             rc = PATH_UNCHECKED;
598         int             i, j;
599
600         if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0)
601                 return;
602         vector_foreach_slot(paths->pathvec, pp, i) {
603                 for (j = 0; j < CONCUR_NR_EVENT; j++) {
604                         rc = try_to_cancel_timeout_io(pp->dio_ctx_array + j,
605                                         &curr_time, pp->devname);
606                         account_async_io_state(pp, rc);
607                 }
608         }
609 }
610
611 static void cancel_inflight_io(struct io_err_stat_path *pp)
612 {
613         struct io_event event;
614         int i, r;
615
616         for (i = 0; i < CONCUR_NR_EVENT; i++) {
617                 struct dio_ctx *ct = pp->dio_ctx_array + i;
618                 struct iocb *ios[1] = { &ct->io };
619
620                 if (ct->io_starttime.tv_sec == 0
621                                 && ct->io_starttime.tv_nsec == 0)
622                         continue;
623                 io_err_stat_log(5, "%s: abort infligh io",
624                                 pp->devname);
625                 r = io_cancel(ioctx, ios[0], &event);
626                 if (r)
627                         io_err_stat_log(5, "%s: io_cancel error %d, %i",
628                                         pp->devname, r, errno);
629                 ct->io_starttime.tv_sec = 0;
630                 ct->io_starttime.tv_nsec = 0;
631         }
632 }
633
634 static inline int handle_done_dio_ctx(struct dio_ctx *ct, struct io_event *ev)
635 {
636         ct->io_starttime.tv_sec = 0;
637         ct->io_starttime.tv_nsec = 0;
638         return (ev->res == ct->blksize) ? PATH_UP : PATH_DOWN;
639 }
640
641 static void handle_async_io_done_event(struct io_event *io_evt)
642 {
643         struct io_err_stat_path *pp;
644         struct dio_ctx *ct;
645         int rc = PATH_UNCHECKED;
646         int i, j;
647
648         vector_foreach_slot(paths->pathvec, pp, i) {
649                 for (j = 0; j < CONCUR_NR_EVENT; j++) {
650                         ct = pp->dio_ctx_array + j;
651                         if (&ct->io == io_evt->obj) {
652                                 rc = handle_done_dio_ctx(ct, io_evt);
653                                 account_async_io_state(pp, rc);
654                                 return;
655                         }
656                 }
657         }
658 }
659
660 static void process_async_ios_event(int timeout_nsecs, char *dev)
661 {
662         struct io_event events[CONCUR_NR_EVENT];
663         int             i, n;
664         struct timespec timeout = { .tv_nsec = timeout_nsecs };
665
666         errno = 0;
667         n = io_getevents(ioctx, 1L, CONCUR_NR_EVENT, events, &timeout);
668         if (n < 0) {
669                 io_err_stat_log(3, "%s: async io events returned %d (errno=%s)",
670                                 dev, n, strerror(errno));
671         } else {
672                 for (i = 0; i < n; i++)
673                         handle_async_io_done_event(&events[i]);
674         }
675 }
676
677 static void service_paths(void)
678 {
679         struct io_err_stat_path *pp;
680         int i;
681
682         pthread_mutex_lock(&paths->mutex);
683         vector_foreach_slot(paths->pathvec, pp, i) {
684                 send_batch_async_ios(pp);
685                 process_async_ios_event(TIMEOUT_NO_IO_NSEC, pp->devname);
686                 poll_async_io_timeout();
687                 poll_io_err_stat(vecs, pp);
688         }
689         pthread_mutex_unlock(&paths->mutex);
690 }
691
692 static void cleanup_unlock(void *arg)
693 {
694         pthread_mutex_unlock((pthread_mutex_t*) arg);
695 }
696
697 static void cleanup_exited(void *arg)
698 {
699         uatomic_set(&io_err_thread_running, 0);
700 }
701
702 static void *io_err_stat_loop(void *data)
703 {
704         sigset_t set;
705
706         vecs = (struct vectors *)data;
707         pthread_cleanup_push(rcu_unregister, NULL);
708         rcu_register_thread();
709
710         pthread_cleanup_push(cleanup_exited, NULL);
711
712         sigfillset(&set);
713         sigdelset(&set, SIGUSR2);
714
715         mlockall(MCL_CURRENT | MCL_FUTURE);
716
717         pthread_mutex_lock(&io_err_thread_lock);
718         uatomic_set(&io_err_thread_running, 1);
719         pthread_cond_broadcast(&io_err_thread_cond);
720         pthread_mutex_unlock(&io_err_thread_lock);
721
722         while (1) {
723                 struct timespec ts;
724
725                 service_paths();
726
727                 ts.tv_sec = 0;
728                 ts.tv_nsec = 100 * 1000 * 1000;
729                 /*
730                  * pselect() with no fds, a timeout, and a sigmask:
731                  * sleep for 100ms and react on SIGUSR2.
732                  */
733                 pselect(1, NULL, NULL, NULL, &ts, &set);
734         }
735
736         pthread_cleanup_pop(1);
737         pthread_cleanup_pop(1);
738         return NULL;
739 }
740
741 int start_io_err_stat_thread(void *data)
742 {
743         int ret;
744
745         if (uatomic_read(&io_err_thread_running) == 1)
746                 return 0;
747
748         if (io_setup(CONCUR_NR_EVENT, &ioctx) != 0) {
749                 io_err_stat_log(4, "io_setup failed");
750                 return 1;
751         }
752         paths = alloc_pathvec();
753         if (!paths)
754                 goto destroy_ctx;
755
756         pthread_mutex_lock(&io_err_thread_lock);
757         pthread_cleanup_push(cleanup_unlock, &io_err_thread_lock);
758
759         ret = pthread_create(&io_err_stat_thr, &io_err_stat_attr,
760                              io_err_stat_loop, data);
761
762         while (!ret && !uatomic_read(&io_err_thread_running) &&
763                pthread_cond_wait(&io_err_thread_cond,
764                                  &io_err_thread_lock) == 0);
765
766         pthread_cleanup_pop(1);
767
768         if (ret) {
769                 io_err_stat_log(0, "cannot create io_error statistic thread");
770                 goto out_free;
771         }
772
773         io_err_stat_log(2, "io_error statistic thread started");
774         return 0;
775
776 out_free:
777         free_io_err_pathvec(paths);
778 destroy_ctx:
779         io_destroy(ioctx);
780         io_err_stat_log(0, "failed to start io_error statistic thread");
781         return 1;
782 }
783
784 void stop_io_err_stat_thread(void)
785 {
786         if (io_err_stat_thr == (pthread_t)0)
787                 return;
788
789         if (uatomic_read(&io_err_thread_running) == 1)
790                 pthread_cancel(io_err_stat_thr);
791
792         pthread_join(io_err_stat_thr, NULL);
793         free_io_err_pathvec(paths);
794         io_destroy(ioctx);
795 }