libmultipath: fix marginal_paths nr_active check
[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_WAITING_TO_CHECK    -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         io_err_stat_log(2, "%s: enqueue path %s to check",
287                         path->mpp->alias, path->dev);
288         return 0;
289
290 unlock_destroy:
291         pthread_mutex_unlock(&paths->mutex);
292         destroy_directio_ctx(p);
293 free_ioerr_path:
294         free_io_err_stat_path(p);
295
296         return 1;
297 }
298
299 int io_err_stat_handle_pathfail(struct path *path)
300 {
301         struct timespec curr_time;
302
303         if (uatomic_read(&io_err_thread_running) == 0)
304                 return 1;
305
306         if (path->io_err_disable_reinstate) {
307                 io_err_stat_log(3, "%s: reinstate is already disabled",
308                                 path->dev);
309                 return 1;
310         }
311         if (path->io_err_pathfail_cnt < 0)
312                 return 1;
313
314         if (!path->mpp)
315                 return 1;
316         if (path->mpp->marginal_path_double_failed_time <= 0 ||
317                 path->mpp->marginal_path_err_sample_time <= 0 ||
318                 path->mpp->marginal_path_err_recheck_gap_time <= 0 ||
319                 path->mpp->marginal_path_err_rate_threshold < 0) {
320                 io_err_stat_log(4, "%s: parameter not set", path->mpp->alias);
321                 return 1;
322         }
323         if (path->mpp->marginal_path_err_sample_time < (2 * IOTIMEOUT_SEC)) {
324                 io_err_stat_log(2, "%s: marginal_path_err_sample_time should not less than %d",
325                                 path->mpp->alias, 2 * IOTIMEOUT_SEC);
326                 return 1;
327         }
328         /*
329          * The test should only be started for paths that have failed
330          * repeatedly in a certain time frame, so that we have reason
331          * to assume they're flaky. Without bother the admin to configure
332          * the repeated count threshold and time frame, we assume a path
333          * which fails at least twice within 60 seconds is flaky.
334          */
335         if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0)
336                 return 1;
337         if (path->io_err_pathfail_cnt == 0) {
338                 path->io_err_pathfail_cnt++;
339                 path->io_err_pathfail_starttime = curr_time.tv_sec;
340                 io_err_stat_log(5, "%s: start path flakiness pre-checking",
341                                 path->dev);
342                 return 0;
343         }
344         if ((curr_time.tv_sec - path->io_err_pathfail_starttime) >
345                         path->mpp->marginal_path_double_failed_time) {
346                 path->io_err_pathfail_cnt = 0;
347                 path->io_err_pathfail_starttime = curr_time.tv_sec;
348                 io_err_stat_log(5, "%s: restart path flakiness pre-checking",
349                                 path->dev);
350         }
351         path->io_err_pathfail_cnt++;
352         if (path->io_err_pathfail_cnt >= FLAKY_PATHFAIL_THRESHOLD) {
353                 path->io_err_disable_reinstate = 1;
354                 path->io_err_pathfail_cnt = PATH_IO_ERR_WAITING_TO_CHECK;
355                 /* enqueue path as soon as it comes up */
356                 path->io_err_dis_reinstate_time = 0;
357                 if (path->state != PATH_DOWN) {
358                         struct config *conf;
359                         int oldstate = path->state;
360                         int checkint;
361
362                         conf = get_multipath_config();
363                         checkint = conf->checkint;
364                         put_multipath_config(conf);
365                         io_err_stat_log(2, "%s: mark as failed", path->dev);
366                         path->mpp->stat_path_failures++;
367                         path->state = PATH_DOWN;
368                         path->dmstate = PSTATE_FAILED;
369                         if (oldstate == PATH_UP || oldstate == PATH_GHOST)
370                                 update_queue_mode_del_path(path->mpp);
371                         if (path->tick > checkint)
372                                 path->tick = checkint;
373                 }
374         }
375
376         return 0;
377 }
378
379 int need_io_err_check(struct path *pp)
380 {
381         struct timespec curr_time;
382         int r;
383
384         if (uatomic_read(&io_err_thread_running) == 0)
385                 return 0;
386         if (pp->mpp->nr_active <= 0) {
387                 io_err_stat_log(2, "%s: recover path early", pp->dev);
388                 goto recover;
389         }
390         if (pp->io_err_pathfail_cnt != PATH_IO_ERR_WAITING_TO_CHECK)
391                 return 1;
392         if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0 ||
393             (curr_time.tv_sec - pp->io_err_dis_reinstate_time) >
394                         pp->mpp->marginal_path_err_recheck_gap_time) {
395                 io_err_stat_log(4, "%s: reschedule checking after %d seconds",
396                                 pp->dev,
397                                 pp->mpp->marginal_path_err_recheck_gap_time);
398                 r = enqueue_io_err_stat_by_path(pp);
399                 /*
400                  * Enqueue fails because of internal error.
401                  * In this case , we recover this path
402                  * Or else,  return 1 to set path state to PATH_SHAKY
403                  */
404                 if (r == 1) {
405                         io_err_stat_log(3, "%s: enqueue fails, to recover",
406                                         pp->dev);
407                         goto recover;
408                 } else
409                         pp->io_err_pathfail_cnt = PATH_IO_ERR_IN_CHECKING;
410         }
411
412         return 1;
413
414 recover:
415         pp->io_err_pathfail_cnt = 0;
416         pp->io_err_disable_reinstate = 0;
417         return 0;
418 }
419
420 static int delete_io_err_stat_by_addr(struct io_err_stat_path *p)
421 {
422         int i;
423
424         i = find_slot(paths->pathvec, p);
425         if (i != -1)
426                 vector_del_slot(paths->pathvec, i);
427
428         destroy_directio_ctx(p);
429         free_io_err_stat_path(p);
430
431         return 0;
432 }
433
434 static void account_async_io_state(struct io_err_stat_path *pp, int rc)
435 {
436         switch (rc) {
437         case PATH_DOWN:
438         case PATH_TIMEOUT:
439                 pp->io_err_nr++;
440                 break;
441         case PATH_UNCHECKED:
442         case PATH_UP:
443         case PATH_PENDING:
444                 break;
445         default:
446                 break;
447         }
448 }
449
450 static int poll_io_err_stat(struct vectors *vecs, struct io_err_stat_path *pp)
451 {
452         struct timespec currtime, difftime;
453         struct path *path;
454         double err_rate;
455
456         if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
457                 return 1;
458         timespecsub(&currtime, &pp->start_time, &difftime);
459         if (difftime.tv_sec < pp->total_time)
460                 return 0;
461
462         io_err_stat_log(4, "%s: check end", pp->devname);
463
464         err_rate = pp->io_nr == 0 ? 0 : (pp->io_err_nr * 1000.0f) / pp->io_nr;
465         io_err_stat_log(3, "%s: IO error rate (%.1f/1000)",
466                         pp->devname, err_rate);
467         pthread_cleanup_push(cleanup_lock, &vecs->lock);
468         lock(&vecs->lock);
469         pthread_testcancel();
470         path = find_path_by_dev(vecs->pathvec, pp->devname);
471         if (!path) {
472                 io_err_stat_log(4, "path %s not found'", pp->devname);
473         } else if (err_rate <= pp->err_rate_threshold) {
474                 path->io_err_pathfail_cnt = 0;
475                 path->io_err_disable_reinstate = 0;
476                 io_err_stat_log(3, "%s: (%d/%d) good to enable reinstating",
477                                 pp->devname, pp->io_err_nr, pp->io_nr);
478                 /*
479                  * schedule path check as soon as possible to
480                  * update path state. Do NOT reinstate dm path here
481                  */
482                 path->tick = 1;
483
484         } else if (path->mpp && path->mpp->nr_active > 0) {
485                 io_err_stat_log(3, "%s: keep failing the dm path %s",
486                                 path->mpp->alias, path->dev);
487                 path->io_err_pathfail_cnt = PATH_IO_ERR_WAITING_TO_CHECK;
488                 path->io_err_disable_reinstate = 1;
489                 path->io_err_dis_reinstate_time = currtime.tv_sec;
490                 io_err_stat_log(3, "%s: disable reinstating of %s",
491                                 path->mpp->alias, path->dev);
492         } else {
493                 path->io_err_pathfail_cnt = 0;
494                 path->io_err_disable_reinstate = 0;
495                 io_err_stat_log(3, "%s: there is orphan path, enable reinstating",
496                                 pp->devname);
497         }
498         lock_cleanup_pop(vecs->lock);
499
500         delete_io_err_stat_by_addr(pp);
501
502         return 0;
503 }
504
505 static int send_each_async_io(struct dio_ctx *ct, int fd, char *dev)
506 {
507         int rc = -1;
508
509         if (ct->io_starttime.tv_nsec == 0 &&
510                         ct->io_starttime.tv_sec == 0) {
511                 struct iocb *ios[1] = { &ct->io };
512
513                 if (clock_gettime(CLOCK_MONOTONIC, &ct->io_starttime) != 0) {
514                         ct->io_starttime.tv_sec = 0;
515                         ct->io_starttime.tv_nsec = 0;
516                         return rc;
517                 }
518                 io_prep_pread(&ct->io, fd, ct->buf, ct->blksize, 0);
519                 if (io_submit(ioctx, 1, ios) != 1) {
520                         io_err_stat_log(5, "%s: io_submit error %i",
521                                         dev, errno);
522                         return rc;
523                 }
524                 rc = 0;
525         }
526
527         return rc;
528 }
529
530 static void send_batch_async_ios(struct io_err_stat_path *pp)
531 {
532         int i;
533         struct dio_ctx *ct;
534         struct timespec currtime, difftime;
535
536         if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
537                 return;
538         /*
539          * Give a free time for all IO to complete or timeout
540          */
541         if (pp->start_time.tv_sec != 0) {
542                 timespecsub(&currtime, &pp->start_time, &difftime);
543                 if (difftime.tv_sec + IOTIMEOUT_SEC >= pp->total_time)
544                         return;
545         }
546
547         for (i = 0; i < CONCUR_NR_EVENT; i++) {
548                 ct = pp->dio_ctx_array + i;
549                 if (!send_each_async_io(ct, pp->fd, pp->devname))
550                         pp->io_nr++;
551         }
552         if (pp->start_time.tv_sec == 0 && pp->start_time.tv_nsec == 0 &&
553                 clock_gettime(CLOCK_MONOTONIC, &pp->start_time)) {
554                 pp->start_time.tv_sec = 0;
555                 pp->start_time.tv_nsec = 0;
556         }
557 }
558
559 static int try_to_cancel_timeout_io(struct dio_ctx *ct, struct timespec *t,
560                 char *dev)
561 {
562         struct timespec difftime;
563         struct io_event event;
564         int             rc = PATH_UNCHECKED;
565         int             r;
566
567         if (ct->io_starttime.tv_sec == 0)
568                 return rc;
569         timespecsub(t, &ct->io_starttime, &difftime);
570         if (difftime.tv_sec > IOTIMEOUT_SEC) {
571                 struct iocb *ios[1] = { &ct->io };
572
573                 io_err_stat_log(5, "%s: abort check on timeout", dev);
574                 r = io_cancel(ioctx, ios[0], &event);
575                 if (r)
576                         io_err_stat_log(5, "%s: io_cancel error %i",
577                                         dev, errno);
578                 ct->io_starttime.tv_sec = 0;
579                 ct->io_starttime.tv_nsec = 0;
580                 rc = PATH_TIMEOUT;
581         } else {
582                 rc = PATH_PENDING;
583         }
584
585         return rc;
586 }
587
588 static void poll_async_io_timeout(void)
589 {
590         struct io_err_stat_path *pp;
591         struct timespec curr_time;
592         int             rc = PATH_UNCHECKED;
593         int             i, j;
594
595         if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0)
596                 return;
597         vector_foreach_slot(paths->pathvec, pp, i) {
598                 for (j = 0; j < CONCUR_NR_EVENT; j++) {
599                         rc = try_to_cancel_timeout_io(pp->dio_ctx_array + j,
600                                         &curr_time, pp->devname);
601                         account_async_io_state(pp, rc);
602                 }
603         }
604 }
605
606 static void cancel_inflight_io(struct io_err_stat_path *pp)
607 {
608         struct io_event event;
609         int i, r;
610
611         for (i = 0; i < CONCUR_NR_EVENT; i++) {
612                 struct dio_ctx *ct = pp->dio_ctx_array + i;
613                 struct iocb *ios[1] = { &ct->io };
614
615                 if (ct->io_starttime.tv_sec == 0
616                                 && ct->io_starttime.tv_nsec == 0)
617                         continue;
618                 io_err_stat_log(5, "%s: abort infligh io",
619                                 pp->devname);
620                 r = io_cancel(ioctx, ios[0], &event);
621                 if (r)
622                         io_err_stat_log(5, "%s: io_cancel error %d, %i",
623                                         pp->devname, r, errno);
624                 ct->io_starttime.tv_sec = 0;
625                 ct->io_starttime.tv_nsec = 0;
626         }
627 }
628
629 static inline int handle_done_dio_ctx(struct dio_ctx *ct, struct io_event *ev)
630 {
631         ct->io_starttime.tv_sec = 0;
632         ct->io_starttime.tv_nsec = 0;
633         return (ev->res == ct->blksize) ? PATH_UP : PATH_DOWN;
634 }
635
636 static void handle_async_io_done_event(struct io_event *io_evt)
637 {
638         struct io_err_stat_path *pp;
639         struct dio_ctx *ct;
640         int rc = PATH_UNCHECKED;
641         int i, j;
642
643         vector_foreach_slot(paths->pathvec, pp, i) {
644                 for (j = 0; j < CONCUR_NR_EVENT; j++) {
645                         ct = pp->dio_ctx_array + j;
646                         if (&ct->io == io_evt->obj) {
647                                 rc = handle_done_dio_ctx(ct, io_evt);
648                                 account_async_io_state(pp, rc);
649                                 return;
650                         }
651                 }
652         }
653 }
654
655 static void process_async_ios_event(int timeout_nsecs, char *dev)
656 {
657         struct io_event events[CONCUR_NR_EVENT];
658         int             i, n;
659         struct timespec timeout = { .tv_nsec = timeout_nsecs };
660
661         errno = 0;
662         n = io_getevents(ioctx, 1L, CONCUR_NR_EVENT, events, &timeout);
663         if (n < 0) {
664                 io_err_stat_log(3, "%s: async io events returned %d (errno=%s)",
665                                 dev, n, strerror(errno));
666         } else {
667                 for (i = 0; i < n; i++)
668                         handle_async_io_done_event(&events[i]);
669         }
670 }
671
672 static void service_paths(void)
673 {
674         struct io_err_stat_path *pp;
675         int i;
676
677         pthread_mutex_lock(&paths->mutex);
678         vector_foreach_slot(paths->pathvec, pp, i) {
679                 send_batch_async_ios(pp);
680                 process_async_ios_event(TIMEOUT_NO_IO_NSEC, pp->devname);
681                 poll_async_io_timeout();
682                 poll_io_err_stat(vecs, pp);
683         }
684         pthread_mutex_unlock(&paths->mutex);
685 }
686
687 static void cleanup_unlock(void *arg)
688 {
689         pthread_mutex_unlock((pthread_mutex_t*) arg);
690 }
691
692 static void cleanup_exited(void *arg)
693 {
694         uatomic_set(&io_err_thread_running, 0);
695 }
696
697 static void *io_err_stat_loop(void *data)
698 {
699         sigset_t set;
700
701         vecs = (struct vectors *)data;
702         pthread_cleanup_push(rcu_unregister, NULL);
703         rcu_register_thread();
704
705         pthread_cleanup_push(cleanup_exited, NULL);
706
707         sigfillset(&set);
708         sigdelset(&set, SIGUSR2);
709
710         mlockall(MCL_CURRENT | MCL_FUTURE);
711
712         pthread_mutex_lock(&io_err_thread_lock);
713         uatomic_set(&io_err_thread_running, 1);
714         pthread_cond_broadcast(&io_err_thread_cond);
715         pthread_mutex_unlock(&io_err_thread_lock);
716
717         while (1) {
718                 struct timespec ts;
719
720                 service_paths();
721
722                 ts.tv_sec = 0;
723                 ts.tv_nsec = 100 * 1000 * 1000;
724                 /*
725                  * pselect() with no fds, a timeout, and a sigmask:
726                  * sleep for 100ms and react on SIGUSR2.
727                  */
728                 pselect(1, NULL, NULL, NULL, &ts, &set);
729         }
730
731         pthread_cleanup_pop(1);
732         pthread_cleanup_pop(1);
733         return NULL;
734 }
735
736 int start_io_err_stat_thread(void *data)
737 {
738         int ret;
739
740         if (uatomic_read(&io_err_thread_running) == 1)
741                 return 0;
742
743         if (io_setup(CONCUR_NR_EVENT, &ioctx) != 0) {
744                 io_err_stat_log(4, "io_setup failed");
745                 return 1;
746         }
747         paths = alloc_pathvec();
748         if (!paths)
749                 goto destroy_ctx;
750
751         pthread_mutex_lock(&io_err_thread_lock);
752         pthread_cleanup_push(cleanup_unlock, &io_err_thread_lock);
753
754         ret = pthread_create(&io_err_stat_thr, &io_err_stat_attr,
755                              io_err_stat_loop, data);
756
757         while (!ret && !uatomic_read(&io_err_thread_running) &&
758                pthread_cond_wait(&io_err_thread_cond,
759                                  &io_err_thread_lock) == 0);
760
761         pthread_cleanup_pop(1);
762
763         if (ret) {
764                 io_err_stat_log(0, "cannot create io_error statistic thread");
765                 goto out_free;
766         }
767
768         io_err_stat_log(2, "io_error statistic thread started");
769         return 0;
770
771 out_free:
772         free_io_err_pathvec(paths);
773 destroy_ctx:
774         io_destroy(ioctx);
775         io_err_stat_log(0, "failed to start io_error statistic thread");
776         return 1;
777 }
778
779 void stop_io_err_stat_thread(void)
780 {
781         if (io_err_stat_thr == (pthread_t)0)
782                 return;
783
784         if (uatomic_read(&io_err_thread_running) == 1)
785                 pthread_cancel(io_err_stat_thr);
786
787         pthread_join(io_err_stat_thr, NULL);
788         free_io_err_pathvec(paths);
789         io_destroy(ioctx);
790 }