libmultipath: drop mpp->nr_active field
[multipath-tools/.git] / multipathd / main.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Kiyoshi Ueda, NEC
4  * Copyright (c) 2005 Benjamin Marzinski, Redhat
5  * Copyright (c) 2005 Edward Goggin, EMC
6  */
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <libdevmapper.h>
10 #include <sys/wait.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <limits.h>
16 #include <linux/oom.h>
17 #include <libudev.h>
18 #include <urcu.h>
19 #ifdef USE_SYSTEMD
20 #include <systemd/sd-daemon.h>
21 #endif
22 #include <semaphore.h>
23 #include <time.h>
24 #include <stdbool.h>
25
26 /*
27  * libmultipath
28  */
29 #include "time-util.h"
30
31 /*
32  * libcheckers
33  */
34 #include "checkers.h"
35
36 #ifdef USE_SYSTEMD
37 static int use_watchdog;
38 #endif
39
40 /*
41  * libmultipath
42  */
43 #include "parser.h"
44 #include "vector.h"
45 #include "memory.h"
46 #include "config.h"
47 #include "util.h"
48 #include "hwtable.h"
49 #include "defaults.h"
50 #include "structs.h"
51 #include "blacklist.h"
52 #include "structs_vec.h"
53 #include "dmparser.h"
54 #include "devmapper.h"
55 #include "sysfs.h"
56 #include "dict.h"
57 #include "discovery.h"
58 #include "debug.h"
59 #include "propsel.h"
60 #include "uevent.h"
61 #include "switchgroup.h"
62 #include "print.h"
63 #include "configure.h"
64 #include "prio.h"
65 #include "wwids.h"
66 #include "pgpolicies.h"
67 #include "uevent.h"
68 #include "log.h"
69 #include "uxsock.h"
70
71 #include "mpath_cmd.h"
72 #include "mpath_persist.h"
73
74 #include "prioritizers/alua_rtpg.h"
75
76 #include "main.h"
77 #include "pidfile.h"
78 #include "uxlsnr.h"
79 #include "uxclnt.h"
80 #include "cli.h"
81 #include "cli_handlers.h"
82 #include "lock.h"
83 #include "waiter.h"
84 #include "dmevents.h"
85 #include "io_err_stat.h"
86 #include "wwids.h"
87 #include "foreign.h"
88 #include "../third-party/valgrind/drd.h"
89
90 #define FILE_NAME_SIZE 256
91 #define CMDSIZE 160
92
93 #define LOG_MSG(lvl, verb, pp)                                  \
94 do {                                                            \
95         if (pp->mpp && checker_selected(&pp->checker) &&        \
96             lvl <= verb) {                                      \
97                 if (pp->offline)                                \
98                         condlog(lvl, "%s: %s - path offline",   \
99                                 pp->mpp->alias, pp->dev);       \
100                 else  {                                         \
101                         const char *__m =                       \
102                                 checker_message(&pp->checker);  \
103                                                                 \
104                         if (strlen(__m))                              \
105                                 condlog(lvl, "%s: %s - %s checker%s", \
106                                         pp->mpp->alias,               \
107                                         pp->dev,                      \
108                                         checker_name(&pp->checker),   \
109                                         __m);                         \
110                 }                                                     \
111         }                                                             \
112 } while(0)
113
114 struct mpath_event_param
115 {
116         char * devname;
117         struct multipath *mpp;
118 };
119
120 int logsink;
121 int uxsock_timeout;
122 int verbosity;
123 int bindings_read_only;
124 int ignore_new_devs;
125 #ifdef NO_DMEVENTS_POLL
126 int poll_dmevents = 0;
127 #else
128 int poll_dmevents = 1;
129 #endif
130 /* Don't access this variable without holding config_lock */
131 enum daemon_status running_state = DAEMON_INIT;
132 pid_t daemon_pid;
133 pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
134 pthread_cond_t config_cond;
135
136 static inline enum daemon_status get_running_state(void)
137 {
138         enum daemon_status st;
139
140         pthread_mutex_lock(&config_lock);
141         st = running_state;
142         pthread_mutex_unlock(&config_lock);
143         return st;
144 }
145
146 /*
147  * global copy of vecs for use in sig handlers
148  */
149 struct vectors * gvecs;
150
151 struct udev * udev;
152
153 struct config *multipath_conf;
154
155 /* Local variables */
156 static volatile sig_atomic_t exit_sig;
157 static volatile sig_atomic_t reconfig_sig;
158 static volatile sig_atomic_t log_reset_sig;
159
160 const char *
161 daemon_status(void)
162 {
163         switch (get_running_state()) {
164         case DAEMON_INIT:
165                 return "init";
166         case DAEMON_START:
167                 return "startup";
168         case DAEMON_CONFIGURE:
169                 return "configure";
170         case DAEMON_IDLE:
171                 return "idle";
172         case DAEMON_RUNNING:
173                 return "running";
174         case DAEMON_SHUTDOWN:
175                 return "shutdown";
176         }
177         return NULL;
178 }
179
180 /*
181  * I love you too, systemd ...
182  */
183 static const char *
184 sd_notify_status(enum daemon_status state)
185 {
186         switch (state) {
187         case DAEMON_INIT:
188                 return "STATUS=init";
189         case DAEMON_START:
190                 return "STATUS=startup";
191         case DAEMON_CONFIGURE:
192                 return "STATUS=configure";
193         case DAEMON_IDLE:
194         case DAEMON_RUNNING:
195                 return "STATUS=up";
196         case DAEMON_SHUTDOWN:
197                 return "STATUS=shutdown";
198         }
199         return NULL;
200 }
201
202 #ifdef USE_SYSTEMD
203 static void do_sd_notify(enum daemon_status old_state,
204                          enum daemon_status new_state)
205 {
206         /*
207          * Checkerloop switches back and forth between idle and running state.
208          * No need to tell systemd each time.
209          * These notifications cause a lot of overhead on dbus.
210          */
211         if ((new_state == DAEMON_IDLE || new_state == DAEMON_RUNNING) &&
212             (old_state == DAEMON_IDLE || old_state == DAEMON_RUNNING))
213                 return;
214         sd_notify(0, sd_notify_status(new_state));
215 }
216 #endif
217
218 static void config_cleanup(__attribute__((unused)) void *arg)
219 {
220         pthread_mutex_unlock(&config_lock);
221 }
222
223 /*
224  * If the current status is @oldstate, wait for at most @ms milliseconds
225  * for the state to change, and return the new state, which may still be
226  * @oldstate.
227  */
228 enum daemon_status wait_for_state_change_if(enum daemon_status oldstate,
229                                             unsigned long ms)
230 {
231         enum daemon_status st;
232         struct timespec tmo;
233
234         if (oldstate == DAEMON_SHUTDOWN)
235                 return DAEMON_SHUTDOWN;
236
237         pthread_mutex_lock(&config_lock);
238         pthread_cleanup_push(config_cleanup, NULL);
239         st = running_state;
240         if (st == oldstate && clock_gettime(CLOCK_MONOTONIC, &tmo) == 0) {
241                 tmo.tv_nsec += ms * 1000 * 1000;
242                 normalize_timespec(&tmo);
243                 (void)pthread_cond_timedwait(&config_cond, &config_lock, &tmo);
244                 st = running_state;
245         }
246         pthread_cleanup_pop(1);
247         return st;
248 }
249
250 /* must be called with config_lock held */
251 static void __post_config_state(enum daemon_status state)
252 {
253         if (state != running_state && running_state != DAEMON_SHUTDOWN) {
254                 enum daemon_status old_state = running_state;
255
256                 running_state = state;
257                 pthread_cond_broadcast(&config_cond);
258 #ifdef USE_SYSTEMD
259                 do_sd_notify(old_state, state);
260 #endif
261         }
262 }
263
264 void post_config_state(enum daemon_status state)
265 {
266         pthread_mutex_lock(&config_lock);
267         pthread_cleanup_push(config_cleanup, NULL);
268         __post_config_state(state);
269         pthread_cleanup_pop(1);
270 }
271
272 int set_config_state(enum daemon_status state)
273 {
274         int rc = 0;
275
276         pthread_cleanup_push(config_cleanup, NULL);
277         pthread_mutex_lock(&config_lock);
278         if (running_state != state) {
279                 enum daemon_status old_state = running_state;
280
281                 if (running_state == DAEMON_SHUTDOWN)
282                         rc = EINVAL;
283                 else if (running_state != DAEMON_IDLE) {
284                         struct timespec ts;
285
286                         get_monotonic_time(&ts);
287                         ts.tv_sec += 1;
288                         rc = pthread_cond_timedwait(&config_cond,
289                                                     &config_lock, &ts);
290                 }
291                 if (!rc && (running_state != DAEMON_SHUTDOWN)) {
292                         running_state = state;
293                         pthread_cond_broadcast(&config_cond);
294 #ifdef USE_SYSTEMD
295                         do_sd_notify(old_state, state);
296 #endif
297                 }
298         }
299         pthread_cleanup_pop(1);
300         return rc;
301 }
302
303 struct config *get_multipath_config(void)
304 {
305         rcu_read_lock();
306         return rcu_dereference(multipath_conf);
307 }
308
309 void put_multipath_config(__attribute__((unused)) void *arg)
310 {
311         rcu_read_unlock();
312 }
313
314 static int
315 need_switch_pathgroup (struct multipath * mpp, int refresh)
316 {
317         struct pathgroup * pgp;
318         struct path * pp;
319         unsigned int i, j;
320         struct config *conf;
321         int bestpg;
322
323         if (!mpp)
324                 return 0;
325
326         /*
327          * Refresh path priority values
328          */
329         if (refresh) {
330                 vector_foreach_slot (mpp->pg, pgp, i) {
331                         vector_foreach_slot (pgp->paths, pp, j) {
332                                 conf = get_multipath_config();
333                                 pthread_cleanup_push(put_multipath_config,
334                                                      conf);
335                                 pathinfo(pp, conf, DI_PRIO);
336                                 pthread_cleanup_pop(1);
337                         }
338                 }
339         }
340
341         if (!mpp->pg || VECTOR_SIZE(mpp->paths) == 0)
342                 return 0;
343
344         bestpg = select_path_group(mpp);
345         if (mpp->pgfailback == -FAILBACK_MANUAL)
346                 return 0;
347
348         mpp->bestpg = bestpg;
349         if (mpp->bestpg != mpp->nextpg)
350                 return 1;
351
352         return 0;
353 }
354
355 static void
356 switch_pathgroup (struct multipath * mpp)
357 {
358         mpp->stat_switchgroup++;
359         dm_switchgroup(mpp->alias, mpp->bestpg);
360         condlog(2, "%s: switch to path group #%i",
361                  mpp->alias, mpp->bestpg);
362 }
363
364 static int
365 wait_for_events(struct multipath *mpp, struct vectors *vecs)
366 {
367         if (poll_dmevents)
368                 return watch_dmevents(mpp->alias);
369         else
370                 return start_waiter_thread(mpp, vecs);
371 }
372
373 static void
374 remove_map_and_stop_waiter(struct multipath *mpp, struct vectors *vecs)
375 {
376         /* devices are automatically removed by the dmevent polling code,
377          * so they don't need to be manually removed here */
378         condlog(3, "%s: removing map from internal tables", mpp->alias);
379         if (!poll_dmevents)
380                 stop_waiter_thread(mpp);
381         remove_map(mpp, vecs, PURGE_VEC);
382 }
383
384 static void
385 remove_maps_and_stop_waiters(struct vectors *vecs)
386 {
387         int i;
388         struct multipath * mpp;
389
390         if (!vecs)
391                 return;
392
393         if (!poll_dmevents) {
394                 vector_foreach_slot(vecs->mpvec, mpp, i)
395                         stop_waiter_thread(mpp);
396         }
397         else
398                 unwatch_all_dmevents();
399
400         remove_maps(vecs);
401 }
402
403 static void
404 set_multipath_wwid (struct multipath * mpp)
405 {
406         if (strlen(mpp->wwid))
407                 return;
408
409         dm_get_uuid(mpp->alias, mpp->wwid, WWID_SIZE);
410 }
411
412 int __setup_multipath(struct vectors *vecs, struct multipath *mpp,
413                       int reset)
414 {
415         if (dm_get_info(mpp->alias, &mpp->dmi)) {
416                 /* Error accessing table */
417                 condlog(3, "%s: cannot access table", mpp->alias);
418                 goto out;
419         }
420
421         if (update_multipath_strings(mpp, vecs->pathvec, 1)) {
422                 condlog(0, "%s: failed to setup multipath", mpp->alias);
423                 goto out;
424         }
425
426         if (reset) {
427                 set_no_path_retry(mpp);
428                 if (VECTOR_SIZE(mpp->paths) != 0)
429                         dm_cancel_deferred_remove(mpp);
430         }
431
432         return 0;
433 out:
434         remove_map_and_stop_waiter(mpp, vecs);
435         return 1;
436 }
437
438 int update_multipath (struct vectors *vecs, char *mapname, int reset)
439 {
440         struct multipath *mpp;
441         struct pathgroup  *pgp;
442         struct path *pp;
443         int i, j;
444
445         mpp = find_mp_by_alias(vecs->mpvec, mapname);
446
447         if (!mpp) {
448                 condlog(3, "%s: multipath map not found", mapname);
449                 return 2;
450         }
451
452         if (__setup_multipath(vecs, mpp, reset))
453                 return 1; /* mpp freed in setup_multipath */
454
455         /*
456          * compare checkers states with DM states
457          */
458         vector_foreach_slot (mpp->pg, pgp, i) {
459                 vector_foreach_slot (pgp->paths, pp, j) {
460                         if (pp->dmstate != PSTATE_FAILED)
461                                 continue;
462
463                         if (pp->state != PATH_DOWN) {
464                                 struct config *conf;
465                                 int oldstate = pp->state;
466                                 unsigned int checkint;
467
468                                 conf = get_multipath_config();
469                                 checkint = conf->checkint;
470                                 put_multipath_config(conf);
471                                 condlog(2, "%s: mark as failed", pp->dev);
472                                 mpp->stat_path_failures++;
473                                 pp->state = PATH_DOWN;
474                                 if (oldstate == PATH_UP ||
475                                     oldstate == PATH_GHOST)
476                                         update_queue_mode_del_path(mpp);
477
478                                 /*
479                                  * if opportune,
480                                  * schedule the next check earlier
481                                  */
482                                 if (pp->tick > checkint)
483                                         pp->tick = checkint;
484                         }
485                 }
486         }
487         return 0;
488 }
489
490 static int
491 update_map (struct multipath *mpp, struct vectors *vecs, int new_map)
492 {
493         int retries = 3;
494         char params[PARAMS_SIZE] = {0};
495
496 retry:
497         condlog(4, "%s: updating new map", mpp->alias);
498         if (adopt_paths(vecs->pathvec, mpp)) {
499                 condlog(0, "%s: failed to adopt paths for new map update",
500                         mpp->alias);
501                 retries = -1;
502                 goto fail;
503         }
504         verify_paths(mpp, vecs);
505         mpp->action = ACT_RELOAD;
506
507         if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
508                 condlog(0, "%s: failed to setup new map in update", mpp->alias);
509                 retries = -1;
510                 goto fail;
511         }
512         if (domap(mpp, params, 1) == DOMAP_FAIL && retries-- > 0) {
513                 condlog(0, "%s: map_udate sleep", mpp->alias);
514                 sleep(1);
515                 goto retry;
516         }
517         dm_lib_release();
518
519 fail:
520         if (new_map && (retries < 0 || wait_for_events(mpp, vecs))) {
521                 condlog(0, "%s: failed to create new map", mpp->alias);
522                 remove_map(mpp, vecs, 1);
523                 return 1;
524         }
525
526         if (setup_multipath(vecs, mpp))
527                 return 1;
528
529         sync_map_state(mpp);
530
531         if (retries < 0)
532                 condlog(0, "%s: failed reload in new map update", mpp->alias);
533         return 0;
534 }
535
536 static struct multipath *
537 add_map_without_path (struct vectors *vecs, const char *alias)
538 {
539         struct multipath * mpp = alloc_multipath();
540         struct config *conf;
541
542         if (!mpp)
543                 return NULL;
544         if (!alias) {
545                 FREE(mpp);
546                 return NULL;
547         }
548
549         mpp->alias = STRDUP(alias);
550
551         if (dm_get_info(mpp->alias, &mpp->dmi)) {
552                 condlog(3, "%s: cannot access table", mpp->alias);
553                 goto out;
554         }
555         set_multipath_wwid(mpp);
556         conf = get_multipath_config();
557         mpp->mpe = find_mpe(conf->mptable, mpp->wwid);
558         put_multipath_config(conf);
559
560         if (update_multipath_table(mpp, vecs->pathvec, 1))
561                 goto out;
562         if (update_multipath_status(mpp))
563                 goto out;
564
565         if (!vector_alloc_slot(vecs->mpvec))
566                 goto out;
567
568         vector_set_slot(vecs->mpvec, mpp);
569
570         if (update_map(mpp, vecs, 1) != 0) /* map removed */
571                 return NULL;
572
573         return mpp;
574 out:
575         remove_map(mpp, vecs, PURGE_VEC);
576         return NULL;
577 }
578
579 static int
580 coalesce_maps(struct vectors *vecs, vector nmpv)
581 {
582         struct multipath * ompp;
583         vector ompv = vecs->mpvec;
584         unsigned int i, reassign_maps;
585         struct config *conf;
586
587         conf = get_multipath_config();
588         reassign_maps = conf->reassign_maps;
589         put_multipath_config(conf);
590         vector_foreach_slot (ompv, ompp, i) {
591                 condlog(3, "%s: coalesce map", ompp->alias);
592                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
593                         /*
594                          * remove all current maps not allowed by the
595                          * current configuration
596                          */
597                         if (dm_flush_map(ompp->alias)) {
598                                 condlog(0, "%s: unable to flush devmap",
599                                         ompp->alias);
600                                 /*
601                                  * may be just because the device is open
602                                  */
603                                 if (setup_multipath(vecs, ompp) != 0) {
604                                         i--;
605                                         continue;
606                                 }
607                                 if (!vector_alloc_slot(nmpv))
608                                         return 1;
609
610                                 vector_set_slot(nmpv, ompp);
611
612                                 vector_del_slot(ompv, i);
613                                 i--;
614                         }
615                         else {
616                                 dm_lib_release();
617                                 condlog(2, "%s devmap removed", ompp->alias);
618                         }
619                 } else if (reassign_maps) {
620                         condlog(3, "%s: Reassign existing device-mapper"
621                                 " devices", ompp->alias);
622                         dm_reassign(ompp->alias);
623                 }
624         }
625         return 0;
626 }
627
628 static void
629 sync_maps_state(vector mpvec)
630 {
631         unsigned int i;
632         struct multipath *mpp;
633
634         vector_foreach_slot (mpvec, mpp, i)
635                 sync_map_state(mpp);
636 }
637
638 static int
639 flush_map(struct multipath * mpp, struct vectors * vecs, int nopaths)
640 {
641         int r;
642
643         if (nopaths)
644                 r = dm_flush_map_nopaths(mpp->alias, mpp->deferred_remove);
645         else
646                 r = dm_flush_map(mpp->alias);
647         /*
648          * clear references to this map before flushing so we can ignore
649          * the spurious uevent we may generate with the dm_flush_map call below
650          */
651         if (r) {
652                 /*
653                  * May not really be an error -- if the map was already flushed
654                  * from the device mapper by dmsetup(8) for instance.
655                  */
656                 if (r == 1)
657                         condlog(0, "%s: can't flush", mpp->alias);
658                 else {
659                         condlog(2, "%s: devmap deferred remove", mpp->alias);
660                         mpp->deferred_remove = DEFERRED_REMOVE_IN_PROGRESS;
661                 }
662                 return r;
663         }
664         else {
665                 dm_lib_release();
666                 condlog(2, "%s: map flushed", mpp->alias);
667         }
668
669         orphan_paths(vecs->pathvec, mpp, "map flushed");
670         remove_map_and_stop_waiter(mpp, vecs);
671
672         return 0;
673 }
674
675 static int
676 uev_add_map (struct uevent * uev, struct vectors * vecs)
677 {
678         char *alias;
679         int major = -1, minor = -1, rc;
680
681         condlog(3, "%s: add map (uevent)", uev->kernel);
682         alias = uevent_get_dm_name(uev);
683         if (!alias) {
684                 condlog(3, "%s: No DM_NAME in uevent", uev->kernel);
685                 major = uevent_get_major(uev);
686                 minor = uevent_get_minor(uev);
687                 alias = dm_mapname(major, minor);
688                 if (!alias) {
689                         condlog(2, "%s: mapname not found for %d:%d",
690                                 uev->kernel, major, minor);
691                         return 1;
692                 }
693         }
694         pthread_cleanup_push(cleanup_lock, &vecs->lock);
695         lock(&vecs->lock);
696         pthread_testcancel();
697         rc = ev_add_map(uev->kernel, alias, vecs);
698         lock_cleanup_pop(vecs->lock);
699         FREE(alias);
700         return rc;
701 }
702
703 /*
704  * ev_add_map expects that the multipath device already exists in kernel
705  * before it is called. It just adds a device to multipathd or updates an
706  * existing device.
707  */
708 int
709 ev_add_map (char * dev, const char * alias, struct vectors * vecs)
710 {
711         struct multipath * mpp;
712         int delayed_reconfig, reassign_maps;
713         struct config *conf;
714
715         if (dm_is_mpath(alias) != 1) {
716                 condlog(4, "%s: not a multipath map", alias);
717                 return 0;
718         }
719
720         mpp = find_mp_by_alias(vecs->mpvec, alias);
721
722         if (mpp) {
723                 if (mpp->wait_for_udev > 1) {
724                         condlog(2, "%s: performing delayed actions",
725                                 mpp->alias);
726                         if (update_map(mpp, vecs, 0))
727                                 /* setup multipathd removed the map */
728                                 return 1;
729                 }
730                 conf = get_multipath_config();
731                 delayed_reconfig = conf->delayed_reconfig;
732                 reassign_maps = conf->reassign_maps;
733                 put_multipath_config(conf);
734                 if (mpp->wait_for_udev) {
735                         mpp->wait_for_udev = 0;
736                         if (delayed_reconfig &&
737                             !need_to_delay_reconfig(vecs)) {
738                                 condlog(2, "reconfigure (delayed)");
739                                 set_config_state(DAEMON_CONFIGURE);
740                                 return 0;
741                         }
742                 }
743                 /*
744                  * Not really an error -- we generate our own uevent
745                  * if we create a multipath mapped device as a result
746                  * of uev_add_path
747                  */
748                 if (reassign_maps) {
749                         condlog(3, "%s: Reassign existing device-mapper devices",
750                                 alias);
751                         dm_reassign(alias);
752                 }
753                 return 0;
754         }
755         condlog(2, "%s: adding map", alias);
756
757         /*
758          * now we can register the map
759          */
760         if ((mpp = add_map_without_path(vecs, alias))) {
761                 sync_map_state(mpp);
762                 condlog(2, "%s: devmap %s registered", alias, dev);
763                 return 0;
764         } else {
765                 condlog(2, "%s: ev_add_map failed", dev);
766                 return 1;
767         }
768 }
769
770 static int
771 uev_remove_map (struct uevent * uev, struct vectors * vecs)
772 {
773         char *alias;
774         int minor;
775         struct multipath *mpp;
776
777         condlog(3, "%s: remove map (uevent)", uev->kernel);
778         alias = uevent_get_dm_name(uev);
779         if (!alias) {
780                 condlog(3, "%s: No DM_NAME in uevent, ignoring", uev->kernel);
781                 return 0;
782         }
783         minor = uevent_get_minor(uev);
784
785         pthread_cleanup_push(cleanup_lock, &vecs->lock);
786         lock(&vecs->lock);
787         pthread_testcancel();
788         mpp = find_mp_by_minor(vecs->mpvec, minor);
789
790         if (!mpp) {
791                 condlog(2, "%s: devmap not registered, can't remove",
792                         uev->kernel);
793                 goto out;
794         }
795         if (strcmp(mpp->alias, alias)) {
796                 condlog(2, "%s: map alias mismatch: have \"%s\", got \"%s\")",
797                         uev->kernel, mpp->alias, alias);
798                 goto out;
799         }
800
801         remove_map_and_stop_waiter(mpp, vecs);
802 out:
803         lock_cleanup_pop(vecs->lock);
804         FREE(alias);
805         return 0;
806 }
807
808 /* Called from CLI handler */
809 int
810 ev_remove_map (char * devname, char * alias, int minor, struct vectors * vecs)
811 {
812         struct multipath * mpp;
813
814         mpp = find_mp_by_minor(vecs->mpvec, minor);
815
816         if (!mpp) {
817                 condlog(2, "%s: devmap not registered, can't remove",
818                         devname);
819                 return 1;
820         }
821         if (strcmp(mpp->alias, alias)) {
822                 condlog(2, "%s: minor number mismatch (map %d, event %d)",
823                         mpp->alias, mpp->dmi->minor, minor);
824                 return 1;
825         }
826         return flush_map(mpp, vecs, 0);
827 }
828
829 static int
830 uev_add_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
831 {
832         struct path *pp;
833         int ret = 0, i;
834         struct config *conf;
835
836         condlog(3, "%s: add path (uevent)", uev->kernel);
837         if (strstr(uev->kernel, "..") != NULL) {
838                 /*
839                  * Don't allow relative device names in the pathvec
840                  */
841                 condlog(0, "%s: path name is invalid", uev->kernel);
842                 return 1;
843         }
844
845         pthread_cleanup_push(cleanup_lock, &vecs->lock);
846         lock(&vecs->lock);
847         pthread_testcancel();
848         pp = find_path_by_dev(vecs->pathvec, uev->kernel);
849         if (pp) {
850                 int r;
851
852                 condlog(3, "%s: spurious uevent, path already in pathvec",
853                         uev->kernel);
854                 if (!pp->mpp && !strlen(pp->wwid)) {
855                         condlog(3, "%s: reinitialize path", uev->kernel);
856                         udev_device_unref(pp->udev);
857                         pp->udev = udev_device_ref(uev->udev);
858                         conf = get_multipath_config();
859                         pthread_cleanup_push(put_multipath_config, conf);
860                         r = pathinfo(pp, conf,
861                                      DI_ALL | DI_BLACKLIST);
862                         pthread_cleanup_pop(1);
863                         if (r == PATHINFO_OK)
864                                 ret = ev_add_path(pp, vecs, need_do_map);
865                         else if (r == PATHINFO_SKIPPED) {
866                                 condlog(3, "%s: remove blacklisted path",
867                                         uev->kernel);
868                                 i = find_slot(vecs->pathvec, (void *)pp);
869                                 if (i != -1)
870                                         vector_del_slot(vecs->pathvec, i);
871                                 free_path(pp);
872                         } else {
873                                 condlog(0, "%s: failed to reinitialize path",
874                                         uev->kernel);
875                                 ret = 1;
876                         }
877                 }
878         }
879         if (pp)
880                 goto out;
881
882         /*
883          * get path vital state
884          */
885         conf = get_multipath_config();
886         pthread_cleanup_push(put_multipath_config, conf);
887         ret = alloc_path_with_pathinfo(conf, uev->udev,
888                                        uev->wwid, DI_ALL, &pp);
889         pthread_cleanup_pop(1);
890         if (!pp) {
891                 if (ret == PATHINFO_SKIPPED)
892                         ret = 0;
893                 else {
894                         condlog(3, "%s: failed to get path info", uev->kernel);
895                         ret = 1;
896                 }
897                 goto out;
898         }
899         ret = store_path(vecs->pathvec, pp);
900         if (!ret) {
901                 conf = get_multipath_config();
902                 pp->checkint = conf->checkint;
903                 put_multipath_config(conf);
904                 ret = ev_add_path(pp, vecs, need_do_map);
905         } else {
906                 condlog(0, "%s: failed to store path info, "
907                         "dropping event",
908                         uev->kernel);
909                 free_path(pp);
910                 ret = 1;
911         }
912 out:
913         lock_cleanup_pop(vecs->lock);
914         return ret;
915 }
916
917 /*
918  * returns:
919  * 0: added
920  * 1: error
921  */
922 int
923 ev_add_path (struct path * pp, struct vectors * vecs, int need_do_map)
924 {
925         struct multipath * mpp;
926         char params[PARAMS_SIZE] = {0};
927         int retries = 3;
928         int start_waiter = 0;
929         int ret;
930
931         /*
932          * need path UID to go any further
933          */
934         if (strlen(pp->wwid) == 0) {
935                 condlog(0, "%s: failed to get path uid", pp->dev);
936                 goto fail; /* leave path added to pathvec */
937         }
938         mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
939         if (mpp && pp->size && mpp->size != pp->size) {
940                 condlog(0, "%s: failed to add new path %s, device size mismatch", mpp->alias, pp->dev);
941                 int i = find_slot(vecs->pathvec, (void *)pp);
942                 if (i != -1)
943                         vector_del_slot(vecs->pathvec, i);
944                 free_path(pp);
945                 return 1;
946         }
947         if (mpp && mpp->wait_for_udev &&
948             (pathcount(mpp, PATH_UP) > 0 ||
949              (pathcount(mpp, PATH_GHOST) > 0 &&
950               path_get_tpgs(pp) != TPGS_IMPLICIT &&
951               mpp->ghost_delay_tick <= 0))) {
952                 /* if wait_for_udev is set and valid paths exist */
953                 condlog(3, "%s: delaying path addition until %s is fully initialized",
954                         pp->dev, mpp->alias);
955                 mpp->wait_for_udev = 2;
956                 orphan_path(pp, "waiting for create to complete");
957                 return 0;
958         }
959
960         pp->mpp = mpp;
961 rescan:
962         if (mpp) {
963                 condlog(4,"%s: adopting all paths for path %s",
964                         mpp->alias, pp->dev);
965                 if (adopt_paths(vecs->pathvec, mpp))
966                         goto fail; /* leave path added to pathvec */
967
968                 verify_paths(mpp, vecs);
969                 mpp->action = ACT_RELOAD;
970         } else {
971                 if (!should_multipath(pp, vecs->pathvec, vecs->mpvec)) {
972                         orphan_path(pp, "only one path");
973                         return 0;
974                 }
975                 condlog(4,"%s: creating new map", pp->dev);
976                 if ((mpp = add_map_with_path(vecs, pp, 1))) {
977                         mpp->action = ACT_CREATE;
978                         /*
979                          * We don't depend on ACT_CREATE, as domap will
980                          * set it to ACT_NOTHING when complete.
981                          */
982                         start_waiter = 1;
983                 }
984                 if (!start_waiter)
985                         goto fail; /* leave path added to pathvec */
986         }
987
988         /* persistent reservation check*/
989         mpath_pr_event_handle(pp);
990
991         if (!need_do_map)
992                 return 0;
993
994         if (!dm_map_present(mpp->alias)) {
995                 mpp->action = ACT_CREATE;
996                 start_waiter = 1;
997         }
998         /*
999          * push the map to the device-mapper
1000          */
1001         if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1002                 condlog(0, "%s: failed to setup map for addition of new "
1003                         "path %s", mpp->alias, pp->dev);
1004                 goto fail_map;
1005         }
1006         /*
1007          * reload the map for the multipath mapped device
1008          */
1009         ret = domap(mpp, params, 1);
1010         while (ret == DOMAP_RETRY && retries-- > 0) {
1011                 condlog(0, "%s: retry domap for addition of new "
1012                         "path %s", mpp->alias, pp->dev);
1013                 sleep(1);
1014                 ret = domap(mpp, params, 1);
1015         }
1016         if (ret == DOMAP_FAIL || ret == DOMAP_RETRY) {
1017                 condlog(0, "%s: failed in domap for addition of new "
1018                         "path %s", mpp->alias, pp->dev);
1019                 /*
1020                  * deal with asynchronous uevents :((
1021                  */
1022                 if (mpp->action == ACT_RELOAD && retries-- > 0) {
1023                         condlog(0, "%s: ev_add_path sleep", mpp->alias);
1024                         sleep(1);
1025                         update_mpp_paths(mpp, vecs->pathvec);
1026                         goto rescan;
1027                 }
1028                 else if (mpp->action == ACT_RELOAD)
1029                         condlog(0, "%s: giving up reload", mpp->alias);
1030                 else
1031                         goto fail_map;
1032         }
1033         dm_lib_release();
1034
1035         if ((mpp->action == ACT_CREATE ||
1036              (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
1037             wait_for_events(mpp, vecs))
1038                         goto fail_map;
1039
1040         /*
1041          * update our state from kernel regardless of create or reload
1042          */
1043         if (setup_multipath(vecs, mpp))
1044                 goto fail; /* if setup_multipath fails, it removes the map */
1045
1046         sync_map_state(mpp);
1047
1048         if (retries >= 0) {
1049                 condlog(2, "%s [%s]: path added to devmap %s",
1050                         pp->dev, pp->dev_t, mpp->alias);
1051                 return 0;
1052         } else
1053                 goto fail;
1054
1055 fail_map:
1056         remove_map(mpp, vecs, 1);
1057 fail:
1058         orphan_path(pp, "failed to add path");
1059         return 1;
1060 }
1061
1062 static int
1063 uev_remove_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
1064 {
1065         struct path *pp;
1066         int ret;
1067
1068         condlog(3, "%s: remove path (uevent)", uev->kernel);
1069         delete_foreign(uev->udev);
1070
1071         pthread_cleanup_push(cleanup_lock, &vecs->lock);
1072         lock(&vecs->lock);
1073         pthread_testcancel();
1074         pp = find_path_by_dev(vecs->pathvec, uev->kernel);
1075         if (pp)
1076                 ret = ev_remove_path(pp, vecs, need_do_map);
1077         lock_cleanup_pop(vecs->lock);
1078         if (!pp) {
1079                 /* Not an error; path might have been purged earlier */
1080                 condlog(0, "%s: path already removed", uev->kernel);
1081                 return 0;
1082         }
1083         return ret;
1084 }
1085
1086 int
1087 ev_remove_path (struct path *pp, struct vectors * vecs, int need_do_map)
1088 {
1089         struct multipath * mpp;
1090         int i, retval = 0;
1091         char params[PARAMS_SIZE] = {0};
1092
1093         /*
1094          * avoid referring to the map of an orphaned path
1095          */
1096         if ((mpp = pp->mpp)) {
1097                 /*
1098                  * transform the mp->pg vector of vectors of paths
1099                  * into a mp->params string to feed the device-mapper
1100                  */
1101                 if (update_mpp_paths(mpp, vecs->pathvec)) {
1102                         condlog(0, "%s: failed to update paths",
1103                                 mpp->alias);
1104                         goto fail;
1105                 }
1106
1107                 /*
1108                  * Make sure mpp->hwe doesn't point to freed memory
1109                  * We call extract_hwe_from_path() below to restore mpp->hwe
1110                  */
1111                 if (mpp->hwe == pp->hwe)
1112                         mpp->hwe = NULL;
1113
1114                 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
1115                         vector_del_slot(mpp->paths, i);
1116
1117                 /*
1118                  * remove the map IF removing the last path
1119                  */
1120                 if (VECTOR_SIZE(mpp->paths) == 0) {
1121                         char alias[WWID_SIZE];
1122
1123                         /*
1124                          * flush_map will fail if the device is open
1125                          */
1126                         strlcpy(alias, mpp->alias, WWID_SIZE);
1127                         if (mpp->flush_on_last_del == FLUSH_ENABLED) {
1128                                 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
1129                                 mpp->retry_tick = 0;
1130                                 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
1131                                 mpp->disable_queueing = 1;
1132                                 mpp->stat_map_failures++;
1133                                 dm_queue_if_no_path(mpp->alias, 0);
1134                         }
1135                         if (!flush_map(mpp, vecs, 1)) {
1136                                 condlog(2, "%s: removed map after"
1137                                         " removing all paths",
1138                                         alias);
1139                                 retval = 0;
1140                                 goto out;
1141                         }
1142                         /*
1143                          * Not an error, continue
1144                          */
1145                 }
1146
1147                 if (mpp->hwe == NULL)
1148                         extract_hwe_from_path(mpp);
1149
1150                 if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1151                         condlog(0, "%s: failed to setup map for"
1152                                 " removal of path %s", mpp->alias, pp->dev);
1153                         goto fail;
1154                 }
1155
1156                 if (mpp->wait_for_udev) {
1157                         mpp->wait_for_udev = 2;
1158                         goto out;
1159                 }
1160
1161                 if (!need_do_map)
1162                         goto out;
1163                 /*
1164                  * reload the map
1165                  */
1166                 mpp->action = ACT_RELOAD;
1167                 if (domap(mpp, params, 1) == DOMAP_FAIL) {
1168                         condlog(0, "%s: failed in domap for "
1169                                 "removal of path %s",
1170                                 mpp->alias, pp->dev);
1171                         retval = 1;
1172                 } else {
1173                         /*
1174                          * update our state from kernel
1175                          */
1176                         if (setup_multipath(vecs, mpp))
1177                                 return 1;
1178                         sync_map_state(mpp);
1179
1180                         condlog(2, "%s [%s]: path removed from map %s",
1181                                 pp->dev, pp->dev_t, mpp->alias);
1182                 }
1183         }
1184
1185 out:
1186         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
1187                 vector_del_slot(vecs->pathvec, i);
1188
1189         free_path(pp);
1190
1191         return retval;
1192
1193 fail:
1194         remove_map_and_stop_waiter(mpp, vecs);
1195         return 1;
1196 }
1197
1198 static int
1199 uev_update_path (struct uevent *uev, struct vectors * vecs)
1200 {
1201         int ro, retval = 0, rc;
1202         struct path * pp;
1203         struct config *conf;
1204         int needs_reinit = 0;
1205
1206         switch ((rc = change_foreign(uev->udev))) {
1207         case FOREIGN_OK:
1208                 /* known foreign path, ignore event */
1209                 return 0;
1210         case FOREIGN_IGNORED:
1211                 break;
1212         case FOREIGN_ERR:
1213                 condlog(3, "%s: error in change_foreign", __func__);
1214                 break;
1215         default:
1216                 condlog(1, "%s: return code %d of change_forein is unsupported",
1217                         __func__, rc);
1218                 break;
1219         }
1220
1221         pthread_cleanup_push(cleanup_lock, &vecs->lock);
1222         lock(&vecs->lock);
1223         pthread_testcancel();
1224
1225         pp = find_path_by_dev(vecs->pathvec, uev->kernel);
1226         if (pp) {
1227                 struct multipath *mpp = pp->mpp;
1228                 char wwid[WWID_SIZE];
1229
1230                 if (pp->initialized == INIT_REQUESTED_UDEV) {
1231                         needs_reinit = 1;
1232                         goto out;
1233                 }
1234                 /* Don't deal with other types of failed initialization
1235                  * now. check_path will handle it */
1236                 if (!strlen(pp->wwid))
1237                         goto out;
1238
1239                 strcpy(wwid, pp->wwid);
1240                 rc = get_uid(pp, pp->state, uev->udev, 0);
1241
1242                 if (rc != 0)
1243                         strcpy(pp->wwid, wwid);
1244                 else if (strncmp(wwid, pp->wwid, WWID_SIZE) != 0) {
1245                         condlog(0, "%s: path wwid changed from '%s' to '%s'",
1246                                 uev->kernel, wwid, pp->wwid);
1247                         ev_remove_path(pp, vecs, 1);
1248                         needs_reinit = 1;
1249                         goto out;
1250                 } else {
1251                         udev_device_unref(pp->udev);
1252                         pp->udev = udev_device_ref(uev->udev);
1253                         conf = get_multipath_config();
1254                         pthread_cleanup_push(put_multipath_config, conf);
1255                         if (pathinfo(pp, conf, DI_SYSFS|DI_NOIO) != PATHINFO_OK)
1256                                 condlog(1, "%s: pathinfo failed after change uevent",
1257                                         uev->kernel);
1258                         pthread_cleanup_pop(1);
1259                 }
1260
1261                 ro = uevent_get_disk_ro(uev);
1262                 if (mpp && ro >= 0) {
1263                         condlog(2, "%s: update path write_protect to '%d' (uevent)", uev->kernel, ro);
1264
1265                         if (mpp->wait_for_udev)
1266                                 mpp->wait_for_udev = 2;
1267                         else {
1268                                 if (ro == 1)
1269                                         pp->mpp->force_readonly = 1;
1270                                 retval = update_path_groups(mpp, vecs, 0);
1271                                 if (retval == 2)
1272                                         condlog(2, "%s: map removed during reload", pp->dev);
1273                                 else {
1274                                         pp->mpp->force_readonly = 0;
1275                                         condlog(2, "%s: map %s reloaded (retval %d)", uev->kernel, mpp->alias, retval);
1276                                 }
1277                         }
1278                 }
1279         }
1280 out:
1281         lock_cleanup_pop(vecs->lock);
1282         if (!pp) {
1283                 /* If the path is blacklisted, print a debug/non-default verbosity message. */
1284                 if (uev->udev) {
1285                         int flag = DI_SYSFS | DI_WWID;
1286
1287                         conf = get_multipath_config();
1288                         pthread_cleanup_push(put_multipath_config, conf);
1289                         retval = alloc_path_with_pathinfo(conf, uev->udev, uev->wwid, flag, NULL);
1290                         pthread_cleanup_pop(1);
1291
1292                         if (retval == PATHINFO_SKIPPED) {
1293                                 condlog(3, "%s: spurious uevent, path is blacklisted", uev->kernel);
1294                                 return 0;
1295                         }
1296                 }
1297
1298                 condlog(0, "%s: spurious uevent, path not found", uev->kernel);
1299         }
1300         if (needs_reinit)
1301                 retval = uev_add_path(uev, vecs, 1);
1302         return retval;
1303 }
1304
1305 static int
1306 uev_pathfail_check(struct uevent *uev, struct vectors *vecs)
1307 {
1308         char *action = NULL, *devt = NULL;
1309         struct path *pp;
1310         int r = 1;
1311
1312         action = uevent_get_dm_action(uev);
1313         if (!action)
1314                 return 1;
1315         if (strncmp(action, "PATH_FAILED", 11))
1316                 goto out;
1317         devt = uevent_get_dm_path(uev);
1318         if (!devt) {
1319                 condlog(3, "%s: No DM_PATH in uevent", uev->kernel);
1320                 goto out;
1321         }
1322
1323         pthread_cleanup_push(cleanup_lock, &vecs->lock);
1324         lock(&vecs->lock);
1325         pthread_testcancel();
1326         pp = find_path_by_devt(vecs->pathvec, devt);
1327         if (!pp)
1328                 goto out_lock;
1329         r = io_err_stat_handle_pathfail(pp);
1330         if (r)
1331                 condlog(3, "io_err_stat: %s: cannot handle pathfail uevent",
1332                                 pp->dev);
1333 out_lock:
1334         lock_cleanup_pop(vecs->lock);
1335         FREE(devt);
1336         FREE(action);
1337         return r;
1338 out:
1339         FREE(action);
1340         return 1;
1341 }
1342
1343 static int
1344 map_discovery (struct vectors * vecs)
1345 {
1346         struct multipath * mpp;
1347         unsigned int i;
1348
1349         if (dm_get_maps(vecs->mpvec))
1350                 return 1;
1351
1352         vector_foreach_slot (vecs->mpvec, mpp, i)
1353                 if (update_multipath_table(mpp, vecs->pathvec, 1) ||
1354                     update_multipath_status(mpp)) {
1355                         remove_map(mpp, vecs, 1);
1356                         i--;
1357                 }
1358
1359         return 0;
1360 }
1361
1362 int
1363 uxsock_trigger (char * str, char ** reply, int * len, bool is_root,
1364                 void * trigger_data)
1365 {
1366         struct vectors * vecs;
1367         int r;
1368
1369         *reply = NULL;
1370         *len = 0;
1371         vecs = (struct vectors *)trigger_data;
1372
1373         if ((str != NULL) && (is_root == false) &&
1374             (strncmp(str, "list", strlen("list")) != 0) &&
1375             (strncmp(str, "show", strlen("show")) != 0)) {
1376                 *reply = STRDUP("permission deny: need to be root");
1377                 if (*reply)
1378                         *len = strlen(*reply) + 1;
1379                 return 1;
1380         }
1381
1382         r = parse_cmd(str, reply, len, vecs, uxsock_timeout / 1000);
1383
1384         if (r > 0) {
1385                 if (r == ETIMEDOUT)
1386                         *reply = STRDUP("timeout\n");
1387                 else
1388                         *reply = STRDUP("fail\n");
1389                 if (*reply)
1390                         *len = strlen(*reply) + 1;
1391                 r = 1;
1392         }
1393         else if (!r && *len == 0) {
1394                 *reply = STRDUP("ok\n");
1395                 if (*reply)
1396                         *len = strlen(*reply) + 1;
1397                 r = 0;
1398         }
1399         /* else if (r < 0) leave *reply alone */
1400
1401         return r;
1402 }
1403
1404 int
1405 uev_trigger (struct uevent * uev, void * trigger_data)
1406 {
1407         int r = 0;
1408         struct vectors * vecs;
1409         struct uevent *merge_uev, *tmp;
1410         enum daemon_status state;
1411
1412         vecs = (struct vectors *)trigger_data;
1413
1414         pthread_cleanup_push(config_cleanup, NULL);
1415         pthread_mutex_lock(&config_lock);
1416         while (running_state != DAEMON_IDLE &&
1417                running_state != DAEMON_RUNNING &&
1418                running_state != DAEMON_SHUTDOWN)
1419                 pthread_cond_wait(&config_cond, &config_lock);
1420         state = running_state;
1421         pthread_cleanup_pop(1);
1422
1423         if (state == DAEMON_SHUTDOWN)
1424                 return 0;
1425
1426         /*
1427          * device map event
1428          * Add events are ignored here as the tables
1429          * are not fully initialised then.
1430          */
1431         if (!strncmp(uev->kernel, "dm-", 3)) {
1432                 if (!uevent_is_mpath(uev)) {
1433                         if (!strncmp(uev->action, "change", 6))
1434                                 (void)add_foreign(uev->udev);
1435                         else if (!strncmp(uev->action, "remove", 6))
1436                                 (void)delete_foreign(uev->udev);
1437                         goto out;
1438                 }
1439                 if (!strncmp(uev->action, "change", 6)) {
1440                         r = uev_add_map(uev, vecs);
1441
1442                         /*
1443                          * the kernel-side dm-mpath issues a PATH_FAILED event
1444                          * when it encounters a path IO error. It is reason-
1445                          * able be the entry of path IO error accounting pro-
1446                          * cess.
1447                          */
1448                         uev_pathfail_check(uev, vecs);
1449                 } else if (!strncmp(uev->action, "remove", 6)) {
1450                         r = uev_remove_map(uev, vecs);
1451                 }
1452                 goto out;
1453         }
1454
1455         /*
1456          * path add/remove/change event, add/remove maybe merged
1457          */
1458         list_for_each_entry_safe(merge_uev, tmp, &uev->merge_node, node) {
1459                 if (!strncmp(merge_uev->action, "add", 3))
1460                         r += uev_add_path(merge_uev, vecs, 0);
1461                 if (!strncmp(merge_uev->action, "remove", 6))
1462                         r += uev_remove_path(merge_uev, vecs, 0);
1463         }
1464
1465         if (!strncmp(uev->action, "add", 3))
1466                 r += uev_add_path(uev, vecs, 1);
1467         if (!strncmp(uev->action, "remove", 6))
1468                 r += uev_remove_path(uev, vecs, 1);
1469         if (!strncmp(uev->action, "change", 6))
1470                 r += uev_update_path(uev, vecs);
1471
1472 out:
1473         return r;
1474 }
1475
1476 static void rcu_unregister(__attribute__((unused)) void *param)
1477 {
1478         rcu_unregister_thread();
1479 }
1480
1481 static void *
1482 ueventloop (void * ap)
1483 {
1484         struct udev *udev = ap;
1485
1486         pthread_cleanup_push(rcu_unregister, NULL);
1487         rcu_register_thread();
1488         if (uevent_listen(udev))
1489                 condlog(0, "error starting uevent listener");
1490         pthread_cleanup_pop(1);
1491         return NULL;
1492 }
1493
1494 static void *
1495 uevqloop (void * ap)
1496 {
1497         pthread_cleanup_push(rcu_unregister, NULL);
1498         rcu_register_thread();
1499         if (uevent_dispatch(&uev_trigger, ap))
1500                 condlog(0, "error starting uevent dispatcher");
1501         pthread_cleanup_pop(1);
1502         return NULL;
1503 }
1504 static void *
1505 uxlsnrloop (void * ap)
1506 {
1507         long ux_sock;
1508
1509         pthread_cleanup_push(rcu_unregister, NULL);
1510         rcu_register_thread();
1511
1512         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
1513         if (ux_sock == -1) {
1514                 condlog(1, "could not create uxsock: %d", errno);
1515                 exit_daemon();
1516                 goto out;
1517         }
1518         pthread_cleanup_push(uxsock_cleanup, (void *)ux_sock);
1519
1520         if (cli_init()) {
1521                 condlog(1, "Failed to init uxsock listener");
1522                 exit_daemon();
1523                 goto out_sock;
1524         }
1525
1526         /* Tell main thread that thread has started */
1527         post_config_state(DAEMON_CONFIGURE);
1528
1529         set_handler_callback(LIST+PATHS, cli_list_paths);
1530         set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
1531         set_handler_callback(LIST+PATHS+RAW+FMT, cli_list_paths_raw);
1532         set_handler_callback(LIST+PATH, cli_list_path);
1533         set_handler_callback(LIST+MAPS, cli_list_maps);
1534         set_handler_callback(LIST+STATUS, cli_list_status);
1535         set_unlocked_handler_callback(LIST+DAEMON, cli_list_daemon);
1536         set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
1537         set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
1538         set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
1539         set_handler_callback(LIST+MAPS+RAW+FMT, cli_list_maps_raw);
1540         set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1541         set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
1542         set_handler_callback(LIST+MAPS+JSON, cli_list_maps_json);
1543         set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1544         set_handler_callback(LIST+MAP+FMT, cli_list_map_fmt);
1545         set_handler_callback(LIST+MAP+RAW+FMT, cli_list_map_fmt);
1546         set_handler_callback(LIST+MAP+JSON, cli_list_map_json);
1547         set_handler_callback(LIST+CONFIG+LOCAL, cli_list_config_local);
1548         set_handler_callback(LIST+CONFIG, cli_list_config);
1549         set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
1550         set_handler_callback(LIST+DEVICES, cli_list_devices);
1551         set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
1552         set_handler_callback(RESET+MAPS+STATS, cli_reset_maps_stats);
1553         set_handler_callback(RESET+MAP+STATS, cli_reset_map_stats);
1554         set_handler_callback(ADD+PATH, cli_add_path);
1555         set_handler_callback(DEL+PATH, cli_del_path);
1556         set_handler_callback(ADD+MAP, cli_add_map);
1557         set_handler_callback(DEL+MAP, cli_del_map);
1558         set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
1559         set_unlocked_handler_callback(RECONFIGURE, cli_reconfigure);
1560         set_handler_callback(SUSPEND+MAP, cli_suspend);
1561         set_handler_callback(RESUME+MAP, cli_resume);
1562         set_handler_callback(RESIZE+MAP, cli_resize);
1563         set_handler_callback(RELOAD+MAP, cli_reload);
1564         set_handler_callback(RESET+MAP, cli_reassign);
1565         set_handler_callback(REINSTATE+PATH, cli_reinstate);
1566         set_handler_callback(FAIL+PATH, cli_fail);
1567         set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
1568         set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
1569         set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
1570         set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
1571         set_unlocked_handler_callback(QUIT, cli_quit);
1572         set_unlocked_handler_callback(SHUTDOWN, cli_shutdown);
1573         set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
1574         set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
1575         set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
1576         set_handler_callback(FORCEQ+DAEMON, cli_force_no_daemon_q);
1577         set_handler_callback(RESTOREQ+DAEMON, cli_restore_no_daemon_q);
1578         set_handler_callback(GETPRKEY+MAP, cli_getprkey);
1579         set_handler_callback(SETPRKEY+MAP+KEY, cli_setprkey);
1580         set_handler_callback(UNSETPRKEY+MAP, cli_unsetprkey);
1581         set_handler_callback(SETMARGINAL+PATH, cli_set_marginal);
1582         set_handler_callback(UNSETMARGINAL+PATH, cli_unset_marginal);
1583         set_handler_callback(UNSETMARGINAL+MAP, cli_unset_all_marginal);
1584
1585         umask(077);
1586         uxsock_listen(&uxsock_trigger, ux_sock, ap);
1587
1588 out_sock:
1589         pthread_cleanup_pop(1); /* uxsock_cleanup */
1590 out:
1591         pthread_cleanup_pop(1); /* rcu_unregister */
1592         return NULL;
1593 }
1594
1595 void
1596 exit_daemon (void)
1597 {
1598         post_config_state(DAEMON_SHUTDOWN);
1599 }
1600
1601 static void
1602 fail_path (struct path * pp, int del_active)
1603 {
1604         if (!pp->mpp)
1605                 return;
1606
1607         condlog(2, "checker failed path %s in map %s",
1608                  pp->dev_t, pp->mpp->alias);
1609
1610         dm_fail_path(pp->mpp->alias, pp->dev_t);
1611         if (del_active)
1612                 update_queue_mode_del_path(pp->mpp);
1613 }
1614
1615 /*
1616  * caller must have locked the path list before calling that function
1617  */
1618 static int
1619 reinstate_path (struct path * pp)
1620 {
1621         int ret = 0;
1622
1623         if (!pp->mpp)
1624                 return 0;
1625
1626         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t)) {
1627                 condlog(0, "%s: reinstate failed", pp->dev_t);
1628                 ret = 1;
1629         } else {
1630                 condlog(2, "%s: reinstated", pp->dev_t);
1631                 update_queue_mode_add_path(pp->mpp);
1632         }
1633         return ret;
1634 }
1635
1636 static void
1637 enable_group(struct path * pp)
1638 {
1639         struct pathgroup * pgp;
1640
1641         /*
1642          * if path is added through uev_add_path, pgindex can be unset.
1643          * next update_strings() will set it, upon map reload event.
1644          *
1645          * we can safely return here, because upon map reload, all
1646          * PG will be enabled.
1647          */
1648         if (!pp->mpp->pg || !pp->pgindex)
1649                 return;
1650
1651         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1652
1653         if (pgp->status == PGSTATE_DISABLED) {
1654                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1655                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1656         }
1657 }
1658
1659 static void
1660 mpvec_garbage_collector (struct vectors * vecs)
1661 {
1662         struct multipath * mpp;
1663         unsigned int i;
1664
1665         if (!vecs->mpvec)
1666                 return;
1667
1668         vector_foreach_slot (vecs->mpvec, mpp, i) {
1669                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1670                         condlog(2, "%s: remove dead map", mpp->alias);
1671                         remove_map_and_stop_waiter(mpp, vecs);
1672                         i--;
1673                 }
1674         }
1675 }
1676
1677 /* This is called after a path has started working again. It the multipath
1678  * device for this path uses the followover failback type, and this is the
1679  * best pathgroup, and this is the first path in the pathgroup to come back
1680  * up, then switch to this pathgroup */
1681 static int
1682 followover_should_failback(struct path * pp)
1683 {
1684         struct pathgroup * pgp;
1685         struct path *pp1;
1686         int i;
1687
1688         if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1689             !pp->mpp->pg || !pp->pgindex ||
1690             pp->pgindex != pp->mpp->bestpg)
1691                 return 0;
1692
1693         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1694         vector_foreach_slot(pgp->paths, pp1, i) {
1695                 if (pp1 == pp)
1696                         continue;
1697                 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1698                         return 0;
1699         }
1700         return 1;
1701 }
1702
1703 static void
1704 missing_uev_wait_tick(struct vectors *vecs)
1705 {
1706         struct multipath * mpp;
1707         unsigned int i;
1708         int timed_out = 0, delayed_reconfig;
1709         struct config *conf;
1710
1711         vector_foreach_slot (vecs->mpvec, mpp, i) {
1712                 if (mpp->wait_for_udev && --mpp->uev_wait_tick <= 0) {
1713                         timed_out = 1;
1714                         condlog(0, "%s: timeout waiting on creation uevent. enabling reloads", mpp->alias);
1715                         if (mpp->wait_for_udev > 1 &&
1716                             update_map(mpp, vecs, 0)) {
1717                                 /* update_map removed map */
1718                                 i--;
1719                                 continue;
1720                         }
1721                         mpp->wait_for_udev = 0;
1722                 }
1723         }
1724
1725         conf = get_multipath_config();
1726         delayed_reconfig = conf->delayed_reconfig;
1727         put_multipath_config(conf);
1728         if (timed_out && delayed_reconfig &&
1729             !need_to_delay_reconfig(vecs)) {
1730                 condlog(2, "reconfigure (delayed)");
1731                 set_config_state(DAEMON_CONFIGURE);
1732         }
1733 }
1734
1735 static void
1736 ghost_delay_tick(struct vectors *vecs)
1737 {
1738         struct multipath * mpp;
1739         unsigned int i;
1740
1741         vector_foreach_slot (vecs->mpvec, mpp, i) {
1742                 if (mpp->ghost_delay_tick <= 0)
1743                         continue;
1744                 if (--mpp->ghost_delay_tick <= 0) {
1745                         condlog(0, "%s: timed out waiting for active path",
1746                                 mpp->alias);
1747                         mpp->force_udev_reload = 1;
1748                         if (update_map(mpp, vecs, 0) != 0) {
1749                                 /* update_map removed map */
1750                                 i--;
1751                                 continue;
1752                         }
1753                 }
1754         }
1755 }
1756
1757 static void
1758 defered_failback_tick (vector mpvec)
1759 {
1760         struct multipath * mpp;
1761         unsigned int i;
1762
1763         vector_foreach_slot (mpvec, mpp, i) {
1764                 /*
1765                  * deferred failback getting sooner
1766                  */
1767                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1768                         mpp->failback_tick--;
1769
1770                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1771                                 switch_pathgroup(mpp);
1772                 }
1773         }
1774 }
1775
1776 static void
1777 retry_count_tick(vector mpvec)
1778 {
1779         struct multipath *mpp;
1780         unsigned int i;
1781
1782         vector_foreach_slot (mpvec, mpp, i) {
1783                 if (mpp->retry_tick > 0) {
1784                         mpp->stat_total_queueing_time++;
1785                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1786                         if(--mpp->retry_tick == 0) {
1787                                 mpp->stat_map_failures++;
1788                                 dm_queue_if_no_path(mpp->alias, 0);
1789                                 condlog(2, "%s: Disable queueing", mpp->alias);
1790                         }
1791                 }
1792         }
1793 }
1794
1795 int update_prio(struct path *pp, int refresh_all)
1796 {
1797         int oldpriority;
1798         struct path *pp1;
1799         struct pathgroup * pgp;
1800         int i, j, changed = 0;
1801         struct config *conf;
1802
1803         if (refresh_all) {
1804                 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1805                         vector_foreach_slot (pgp->paths, pp1, j) {
1806                                 oldpriority = pp1->priority;
1807                                 conf = get_multipath_config();
1808                                 pthread_cleanup_push(put_multipath_config,
1809                                                      conf);
1810                                 pathinfo(pp1, conf, DI_PRIO);
1811                                 pthread_cleanup_pop(1);
1812                                 if (pp1->priority != oldpriority)
1813                                         changed = 1;
1814                         }
1815                 }
1816                 return changed;
1817         }
1818         oldpriority = pp->priority;
1819         conf = get_multipath_config();
1820         pthread_cleanup_push(put_multipath_config, conf);
1821         if (pp->state != PATH_DOWN)
1822                 pathinfo(pp, conf, DI_PRIO);
1823         pthread_cleanup_pop(1);
1824
1825         if (pp->priority == oldpriority)
1826                 return 0;
1827         return 1;
1828 }
1829
1830 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1831 {
1832         if (reload_map(vecs, mpp, refresh, 1))
1833                 return 1;
1834
1835         dm_lib_release();
1836         if (setup_multipath(vecs, mpp) != 0)
1837                 return 2;
1838         sync_map_state(mpp);
1839
1840         return 0;
1841 }
1842
1843 static int check_path_reinstate_state(struct path * pp) {
1844         struct timespec curr_time;
1845
1846         /*
1847          * This function is only called when the path state changes
1848          * from "bad" to "good". pp->state reflects the *previous* state.
1849          * If this was "bad", we know that a failure must have occured
1850          * beforehand, and count that.
1851          * Note that we count path state _changes_ this way. If a path
1852          * remains in "bad" state, failure count is not increased.
1853          */
1854
1855         if (!((pp->mpp->san_path_err_threshold > 0) &&
1856                                 (pp->mpp->san_path_err_forget_rate > 0) &&
1857                                 (pp->mpp->san_path_err_recovery_time >0))) {
1858                 return 0;
1859         }
1860
1861         if (pp->disable_reinstate) {
1862                 /* If there are no other usable paths, reinstate the path */
1863                 if (count_active_paths(pp->mpp) == 0) {
1864                         condlog(2, "%s : reinstating path early", pp->dev);
1865                         goto reinstate_path;
1866                 }
1867                 get_monotonic_time(&curr_time);
1868
1869                 /* If path became failed again or continue failed, should reset
1870                  * path san_path_err_forget_rate and path dis_reinstate_time to
1871                  * start a new stable check. 
1872                  */
1873                 if ((pp->state != PATH_UP) && (pp->state != PATH_GHOST) &&
1874                         (pp->state != PATH_DELAYED)) {
1875                         pp->san_path_err_forget_rate =
1876                                 pp->mpp->san_path_err_forget_rate;
1877                         pp->dis_reinstate_time = curr_time.tv_sec;
1878                 }
1879
1880                 if ((curr_time.tv_sec - pp->dis_reinstate_time ) > pp->mpp->san_path_err_recovery_time) {
1881                         condlog(2,"%s : reinstate the path after err recovery time", pp->dev);
1882                         goto reinstate_path;
1883                 }
1884                 return 1;
1885         }
1886         /* forget errors on a working path */
1887         if ((pp->state == PATH_UP || pp->state == PATH_GHOST) &&
1888                         pp->path_failures > 0) {
1889                 if (pp->san_path_err_forget_rate > 0){
1890                         pp->san_path_err_forget_rate--;
1891                 } else {
1892                         /* for every san_path_err_forget_rate number of
1893                          * successful path checks decrement path_failures by 1
1894                          */
1895                         pp->path_failures--;
1896                         pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
1897                 }
1898                 return 0;
1899         }
1900
1901         /* If the path isn't recovering from a failed state, do nothing */
1902         if (pp->state != PATH_DOWN && pp->state != PATH_SHAKY &&
1903                         pp->state != PATH_TIMEOUT)
1904                 return 0;
1905
1906         if (pp->path_failures == 0)
1907                 pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
1908
1909         pp->path_failures++;
1910
1911         /* if we don't know the currently time, we don't know how long to
1912          * delay the path, so there's no point in checking if we should
1913          */
1914
1915         get_monotonic_time(&curr_time);
1916         /* when path failures has exceeded the san_path_err_threshold
1917          * place the path in delayed state till san_path_err_recovery_time
1918          * so that the cutomer can rectify the issue within this time. After
1919          * the completion of san_path_err_recovery_time it should
1920          * automatically reinstate the path
1921          * (note: we know that san_path_err_threshold > 0 here).
1922          */
1923         if (pp->path_failures > (unsigned int)pp->mpp->san_path_err_threshold) {
1924                 condlog(2, "%s : hit error threshold. Delaying path reinstatement", pp->dev);
1925                 pp->dis_reinstate_time = curr_time.tv_sec;
1926                 pp->disable_reinstate = 1;
1927
1928                 return 1;
1929         } else {
1930                 return 0;
1931         }
1932
1933 reinstate_path:
1934         pp->path_failures = 0;
1935         pp->disable_reinstate = 0;
1936         pp->san_path_err_forget_rate = 0;
1937         return 0;
1938 }
1939
1940 static int
1941 should_skip_path(struct path *pp){
1942         if (marginal_path_check_enabled(pp->mpp)) {
1943                 if (pp->io_err_disable_reinstate && need_io_err_check(pp))
1944                         return 1;
1945         } else if (san_path_check_enabled(pp->mpp)) {
1946                 if (check_path_reinstate_state(pp))
1947                         return 1;
1948         }
1949         return 0;
1950 }
1951
1952 /*
1953  * Returns '1' if the path has been checked, '-1' if it was blacklisted
1954  * and '0' otherwise
1955  */
1956 int
1957 check_path (struct vectors * vecs, struct path * pp, unsigned int ticks)
1958 {
1959         int newstate;
1960         int new_path_up = 0;
1961         int chkr_new_path_up = 0;
1962         int disable_reinstate = 0;
1963         int oldchkrstate = pp->chkrstate;
1964         int retrigger_tries, verbosity;
1965         unsigned int checkint, max_checkint;
1966         struct config *conf;
1967         int marginal_pathgroups, marginal_changed = 0;
1968         int ret;
1969
1970         if ((pp->initialized == INIT_OK ||
1971              pp->initialized == INIT_REQUESTED_UDEV) && !pp->mpp)
1972                 return 0;
1973
1974         if (pp->tick)
1975                 pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
1976         if (pp->tick)
1977                 return 0; /* don't check this path yet */
1978
1979         conf = get_multipath_config();
1980         retrigger_tries = conf->retrigger_tries;
1981         checkint = conf->checkint;
1982         max_checkint = conf->max_checkint;
1983         verbosity = conf->verbosity;
1984         marginal_pathgroups = conf->marginal_pathgroups;
1985         put_multipath_config(conf);
1986
1987         if (pp->checkint == CHECKINT_UNDEF) {
1988                 condlog(0, "%s: BUG: checkint is not set", pp->dev);
1989                 pp->checkint = checkint;
1990         };
1991
1992         if (!pp->mpp && pp->initialized == INIT_MISSING_UDEV) {
1993                 if (pp->retriggers < retrigger_tries) {
1994                         condlog(2, "%s: triggering change event to reinitialize",
1995                                 pp->dev);
1996                         pp->initialized = INIT_REQUESTED_UDEV;
1997                         pp->retriggers++;
1998                         sysfs_attr_set_value(pp->udev, "uevent", "change",
1999                                              strlen("change"));
2000                         return 0;
2001                 } else {
2002                         condlog(1, "%s: not initialized after %d udev retriggers",
2003                                 pp->dev, retrigger_tries);
2004                         /*
2005                          * Make sure that the "add missing path" code path
2006                          * below may reinstate the path later, if it ever
2007                          * comes up again.
2008                          * The WWID needs not be cleared; if it was set, the
2009                          * state hadn't been INIT_MISSING_UDEV in the first
2010                          * place.
2011                          */
2012                         pp->initialized = INIT_FAILED;
2013                         return 0;
2014                 }
2015         }
2016
2017         /*
2018          * provision a next check soonest,
2019          * in case we exit abnormaly from here
2020          */
2021         pp->tick = checkint;
2022
2023         newstate = path_offline(pp);
2024         if (newstate == PATH_UP) {
2025                 conf = get_multipath_config();
2026                 pthread_cleanup_push(put_multipath_config, conf);
2027                 newstate = get_state(pp, conf, 1, newstate);
2028                 pthread_cleanup_pop(1);
2029         } else {
2030                 checker_clear_message(&pp->checker);
2031                 condlog(3, "%s: state %s, checker not called",
2032                         pp->dev, checker_state_name(newstate));
2033         }
2034         /*
2035          * Wait for uevent for removed paths;
2036          * some LLDDs like zfcp keep paths unavailable
2037          * without sending uevents.
2038          */
2039         if (newstate == PATH_REMOVED)
2040                 newstate = PATH_DOWN;
2041
2042         if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
2043                 condlog(2, "%s: unusable path (%s) - checker failed",
2044                         pp->dev, checker_state_name(newstate));
2045                 LOG_MSG(2, verbosity, pp);
2046                 conf = get_multipath_config();
2047                 pthread_cleanup_push(put_multipath_config, conf);
2048                 pathinfo(pp, conf, 0);
2049                 pthread_cleanup_pop(1);
2050                 return 1;
2051         } else if ((newstate != PATH_UP && newstate != PATH_GHOST) &&
2052                         (pp->state == PATH_DELAYED)) {
2053                 /* If path state become failed again cancel path delay state */
2054                 pp->state = newstate;
2055                 return 1;
2056         }
2057         if (!pp->mpp) {
2058                 if (!strlen(pp->wwid) &&
2059                     (pp->initialized == INIT_FAILED ||
2060                      pp->initialized == INIT_NEW) &&
2061                     (newstate == PATH_UP || newstate == PATH_GHOST)) {
2062                         condlog(2, "%s: add missing path", pp->dev);
2063                         conf = get_multipath_config();
2064                         pthread_cleanup_push(put_multipath_config, conf);
2065                         ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
2066                         pthread_cleanup_pop(1);
2067                         /* INIT_OK implies ret == PATHINFO_OK */
2068                         if (pp->initialized == INIT_OK) {
2069                                 ev_add_path(pp, vecs, 1);
2070                                 pp->tick = 1;
2071                         } else {
2072                                 /*
2073                                  * We failed multiple times to initialize this
2074                                  * path properly. Don't re-check too often.
2075                                  */
2076                                 pp->checkint = max_checkint;
2077                                 if (ret == PATHINFO_SKIPPED)
2078                                         return -1;
2079                         }
2080                 }
2081                 return 0;
2082         }
2083         /*
2084          * Async IO in flight. Keep the previous path state
2085          * and reschedule as soon as possible
2086          */
2087         if (newstate == PATH_PENDING) {
2088                 pp->tick = 1;
2089                 return 0;
2090         }
2091         /*
2092          * Synchronize with kernel state
2093          */
2094         if (update_multipath_strings(pp->mpp, vecs->pathvec, 1)) {
2095                 condlog(1, "%s: Could not synchronize with kernel state",
2096                         pp->dev);
2097                 pp->dmstate = PSTATE_UNDEF;
2098         }
2099         /* if update_multipath_strings orphaned the path, quit early */
2100         if (!pp->mpp)
2101                 return 0;
2102         set_no_path_retry(pp->mpp);
2103
2104         if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
2105             (san_path_check_enabled(pp->mpp) ||
2106              marginal_path_check_enabled(pp->mpp))) {
2107                 int was_marginal = pp->marginal;
2108                 if (should_skip_path(pp)) {
2109                         if (!marginal_pathgroups) {
2110                                 if (marginal_path_check_enabled(pp->mpp))
2111                                         /* to reschedule as soon as possible,
2112                                          * so that this path can be recovered
2113                                          * in time */
2114                                         pp->tick = 1;
2115                                 pp->state = PATH_DELAYED;
2116                                 return 1;
2117                         }
2118                         if (!was_marginal) {
2119                                 pp->marginal = 1;
2120                                 marginal_changed = 1;
2121                         }
2122                 } else if (marginal_pathgroups && was_marginal) {
2123                         pp->marginal = 0;
2124                         marginal_changed = 1;
2125                 }
2126         }
2127
2128         /*
2129          * don't reinstate failed path, if its in stand-by
2130          * and if target supports only implicit tpgs mode.
2131          * this will prevent unnecessary i/o by dm on stand-by
2132          * paths if there are no other active paths in map.
2133          */
2134         disable_reinstate = (newstate == PATH_GHOST &&
2135                              count_active_paths(pp->mpp) == 0 &&
2136                              path_get_tpgs(pp) == TPGS_IMPLICIT) ? 1 : 0;
2137
2138         pp->chkrstate = newstate;
2139         if (newstate != pp->state) {
2140                 int oldstate = pp->state;
2141                 pp->state = newstate;
2142
2143                 LOG_MSG(1, verbosity, pp);
2144
2145                 /*
2146                  * upon state change, reset the checkint
2147                  * to the shortest delay
2148                  */
2149                 conf = get_multipath_config();
2150                 pp->checkint = conf->checkint;
2151                 put_multipath_config(conf);
2152
2153                 if (newstate != PATH_UP && newstate != PATH_GHOST) {
2154                         /*
2155                          * proactively fail path in the DM
2156                          */
2157                         if (oldstate == PATH_UP ||
2158                             oldstate == PATH_GHOST)
2159                                 fail_path(pp, 1);
2160                         else
2161                                 fail_path(pp, 0);
2162
2163                         /*
2164                          * cancel scheduled failback
2165                          */
2166                         pp->mpp->failback_tick = 0;
2167
2168                         pp->mpp->stat_path_failures++;
2169                         return 1;
2170                 }
2171
2172                 if (newstate == PATH_UP || newstate == PATH_GHOST) {
2173                         if (pp->mpp->prflag) {
2174                                 /*
2175                                  * Check Persistent Reservation.
2176                                  */
2177                                 condlog(2, "%s: checking persistent "
2178                                         "reservation registration", pp->dev);
2179                                 mpath_pr_event_handle(pp);
2180                         }
2181                 }
2182
2183                 /*
2184                  * reinstate this path
2185                  */
2186                 if (!disable_reinstate && reinstate_path(pp)) {
2187                         condlog(3, "%s: reload map", pp->dev);
2188                         ev_add_path(pp, vecs, 1);
2189                         pp->tick = 1;
2190                         return 0;
2191                 }
2192                 new_path_up = 1;
2193
2194                 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
2195                         chkr_new_path_up = 1;
2196
2197                 /*
2198                  * if at least one path is up in a group, and
2199                  * the group is disabled, re-enable it
2200                  */
2201                 if (newstate == PATH_UP)
2202                         enable_group(pp);
2203         }
2204         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
2205                 if ((pp->dmstate == PSTATE_FAILED ||
2206                     pp->dmstate == PSTATE_UNDEF) &&
2207                     !disable_reinstate) {
2208                         /* Clear IO errors */
2209                         if (reinstate_path(pp)) {
2210                                 condlog(3, "%s: reload map", pp->dev);
2211                                 ev_add_path(pp, vecs, 1);
2212                                 pp->tick = 1;
2213                                 return 0;
2214                         }
2215                 } else {
2216                         LOG_MSG(4, verbosity, pp);
2217                         if (pp->checkint != max_checkint) {
2218                                 /*
2219                                  * double the next check delay.
2220                                  * max at conf->max_checkint
2221                                  */
2222                                 if (pp->checkint < (max_checkint / 2))
2223                                         pp->checkint = 2 * pp->checkint;
2224                                 else
2225                                         pp->checkint = max_checkint;
2226
2227                                 condlog(4, "%s: delay next check %is",
2228                                         pp->dev_t, pp->checkint);
2229                         }
2230                         pp->tick = pp->checkint;
2231                 }
2232         }
2233         else if (newstate != PATH_UP && newstate != PATH_GHOST) {
2234                 if (pp->dmstate == PSTATE_ACTIVE ||
2235                     pp->dmstate == PSTATE_UNDEF)
2236                         fail_path(pp, 0);
2237                 if (newstate == PATH_DOWN) {
2238                         int log_checker_err;
2239
2240                         conf = get_multipath_config();
2241                         log_checker_err = conf->log_checker_err;
2242                         put_multipath_config(conf);
2243                         if (log_checker_err == LOG_CHKR_ERR_ONCE)
2244                                 LOG_MSG(3, verbosity, pp);
2245                         else
2246                                 LOG_MSG(2, verbosity, pp);
2247                 }
2248         }
2249
2250         pp->state = newstate;
2251
2252         if (pp->mpp->wait_for_udev)
2253                 return 1;
2254         /*
2255          * path prio refreshing
2256          */
2257         condlog(4, "path prio refresh");
2258
2259         if (marginal_changed)
2260                 update_path_groups(pp->mpp, vecs, 1);
2261         else if (update_prio(pp, new_path_up) &&
2262             (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
2263              pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
2264                 update_path_groups(pp->mpp, vecs, !new_path_up);
2265         else if (need_switch_pathgroup(pp->mpp, 0)) {
2266                 if (pp->mpp->pgfailback > 0 &&
2267                     (new_path_up || pp->mpp->failback_tick <= 0))
2268                         pp->mpp->failback_tick =
2269                                 pp->mpp->pgfailback + 1;
2270                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
2271                          (chkr_new_path_up && followover_should_failback(pp)))
2272                         switch_pathgroup(pp->mpp);
2273         }
2274         return 1;
2275 }
2276
2277 static void *
2278 checkerloop (void *ap)
2279 {
2280         struct vectors *vecs;
2281         struct path *pp;
2282         int count = 0;
2283         unsigned int i;
2284         struct timespec last_time;
2285         struct config *conf;
2286         int foreign_tick = 0;
2287
2288         pthread_cleanup_push(rcu_unregister, NULL);
2289         rcu_register_thread();
2290         mlockall(MCL_CURRENT | MCL_FUTURE);
2291         vecs = (struct vectors *)ap;
2292         condlog(2, "path checkers start up");
2293
2294         /* Tweak start time for initial path check */
2295         get_monotonic_time(&last_time);
2296         last_time.tv_sec -= 1;
2297
2298         while (1) {
2299                 struct timespec diff_time, start_time, end_time;
2300                 int num_paths = 0, strict_timing, rc = 0;
2301                 unsigned int ticks = 0;
2302
2303                 get_monotonic_time(&start_time);
2304                 if (start_time.tv_sec && last_time.tv_sec) {
2305                         timespecsub(&start_time, &last_time, &diff_time);
2306                         condlog(4, "tick (%lu.%06lu secs)",
2307                                 diff_time.tv_sec, diff_time.tv_nsec / 1000);
2308                         last_time = start_time;
2309                         ticks = diff_time.tv_sec;
2310                 } else {
2311                         ticks = 1;
2312                         condlog(4, "tick (%d ticks)", ticks);
2313                 }
2314 #ifdef USE_SYSTEMD
2315                 if (use_watchdog)
2316                         sd_notify(0, "WATCHDOG=1");
2317 #endif
2318                 rc = set_config_state(DAEMON_RUNNING);
2319                 if (rc == ETIMEDOUT) {
2320                         condlog(4, "timeout waiting for DAEMON_IDLE");
2321                         continue;
2322                 } else if (rc == EINVAL)
2323                         /* daemon shutdown */
2324                         break;
2325
2326                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2327                 lock(&vecs->lock);
2328                 pthread_testcancel();
2329                 vector_foreach_slot (vecs->pathvec, pp, i) {
2330                         rc = check_path(vecs, pp, ticks);
2331                         if (rc < 0) {
2332                                 vector_del_slot(vecs->pathvec, i);
2333                                 free_path(pp);
2334                                 i--;
2335                         } else
2336                                 num_paths += rc;
2337                 }
2338                 lock_cleanup_pop(vecs->lock);
2339
2340                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2341                 lock(&vecs->lock);
2342                 pthread_testcancel();
2343                 defered_failback_tick(vecs->mpvec);
2344                 retry_count_tick(vecs->mpvec);
2345                 missing_uev_wait_tick(vecs);
2346                 ghost_delay_tick(vecs);
2347                 lock_cleanup_pop(vecs->lock);
2348
2349                 if (count)
2350                         count--;
2351                 else {
2352                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2353                         lock(&vecs->lock);
2354                         pthread_testcancel();
2355                         condlog(4, "map garbage collection");
2356                         mpvec_garbage_collector(vecs);
2357                         count = MAPGCINT;
2358                         lock_cleanup_pop(vecs->lock);
2359                 }
2360
2361                 diff_time.tv_nsec = 0;
2362                 if (start_time.tv_sec) {
2363                         get_monotonic_time(&end_time);
2364                         timespecsub(&end_time, &start_time, &diff_time);
2365                         if (num_paths) {
2366                                 unsigned int max_checkint;
2367
2368                                 condlog(4, "checked %d path%s in %lu.%06lu secs",
2369                                         num_paths, num_paths > 1 ? "s" : "",
2370                                         diff_time.tv_sec,
2371                                         diff_time.tv_nsec / 1000);
2372                                 conf = get_multipath_config();
2373                                 max_checkint = conf->max_checkint;
2374                                 put_multipath_config(conf);
2375                                 if (diff_time.tv_sec > max_checkint)
2376                                         condlog(1, "path checkers took longer "
2377                                                 "than %lu seconds, consider "
2378                                                 "increasing max_polling_interval",
2379                                                 diff_time.tv_sec);
2380                         }
2381                 }
2382
2383                 if (foreign_tick == 0) {
2384                         conf = get_multipath_config();
2385                         foreign_tick = conf->max_checkint;
2386                         put_multipath_config(conf);
2387                 }
2388                 if (--foreign_tick == 0)
2389                         check_foreign();
2390
2391                 post_config_state(DAEMON_IDLE);
2392                 conf = get_multipath_config();
2393                 strict_timing = conf->strict_timing;
2394                 put_multipath_config(conf);
2395                 if (!strict_timing)
2396                         sleep(1);
2397                 else {
2398                         if (diff_time.tv_nsec) {
2399                                 diff_time.tv_sec = 0;
2400                                 diff_time.tv_nsec =
2401                                      1000UL * 1000 * 1000 - diff_time.tv_nsec;
2402                         } else
2403                                 diff_time.tv_sec = 1;
2404
2405                         condlog(3, "waiting for %lu.%06lu secs",
2406                                 diff_time.tv_sec,
2407                                 diff_time.tv_nsec / 1000);
2408                         if (nanosleep(&diff_time, NULL) != 0) {
2409                                 condlog(3, "nanosleep failed with error %d",
2410                                         errno);
2411                                 conf = get_multipath_config();
2412                                 conf->strict_timing = 0;
2413                                 put_multipath_config(conf);
2414                                 break;
2415                         }
2416                 }
2417         }
2418         pthread_cleanup_pop(1);
2419         return NULL;
2420 }
2421
2422 int
2423 configure (struct vectors * vecs)
2424 {
2425         struct multipath * mpp;
2426         struct path * pp;
2427         vector mpvec;
2428         int i, ret;
2429         struct config *conf;
2430         static int force_reload = FORCE_RELOAD_WEAK;
2431
2432         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
2433                 condlog(0, "couldn't allocate path vec in configure");
2434                 return 1;
2435         }
2436
2437         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc())) {
2438                 condlog(0, "couldn't allocate multipath vec in configure");
2439                 return 1;
2440         }
2441
2442         if (!(mpvec = vector_alloc())) {
2443                 condlog(0, "couldn't allocate new maps vec in configure");
2444                 return 1;
2445         }
2446
2447         /*
2448          * probe for current path (from sysfs) and map (from dm) sets
2449          */
2450         ret = path_discovery(vecs->pathvec, DI_ALL);
2451         if (ret < 0) {
2452                 condlog(0, "configure failed at path discovery");
2453                 goto fail;
2454         }
2455
2456         conf = get_multipath_config();
2457         pthread_cleanup_push(put_multipath_config, conf);
2458         vector_foreach_slot (vecs->pathvec, pp, i){
2459                 if (filter_path(conf, pp) > 0){
2460                         vector_del_slot(vecs->pathvec, i);
2461                         free_path(pp);
2462                         i--;
2463                 }
2464         }
2465         pthread_cleanup_pop(1);
2466
2467         if (map_discovery(vecs)) {
2468                 condlog(0, "configure failed at map discovery");
2469                 goto fail;
2470         }
2471
2472         /*
2473          * create new set of maps & push changed ones into dm
2474          * In the first call, use FORCE_RELOAD_WEAK to avoid making
2475          * superfluous ACT_RELOAD ioctls. Later calls are done
2476          * with FORCE_RELOAD_YES.
2477          */
2478         ret = coalesce_paths(vecs, mpvec, NULL, force_reload, CMD_NONE);
2479         if (force_reload == FORCE_RELOAD_WEAK)
2480                 force_reload = FORCE_RELOAD_YES;
2481         if (ret != CP_OK) {
2482                 condlog(0, "configure failed while coalescing paths");
2483                 goto fail;
2484         }
2485
2486         /*
2487          * may need to remove some maps which are no longer relevant
2488          * e.g., due to blacklist changes in conf file
2489          */
2490         if (coalesce_maps(vecs, mpvec)) {
2491                 condlog(0, "configure failed while coalescing maps");
2492                 goto fail;
2493         }
2494
2495         dm_lib_release();
2496
2497         sync_maps_state(mpvec);
2498         vector_foreach_slot(mpvec, mpp, i){
2499                 if (remember_wwid(mpp->wwid) == 1)
2500                         trigger_paths_udev_change(mpp, true);
2501                 update_map_pr(mpp);
2502         }
2503
2504         /*
2505          * purge dm of old maps
2506          */
2507         remove_maps(vecs);
2508
2509         /*
2510          * save new set of maps formed by considering current path state
2511          */
2512         vector_free(vecs->mpvec);
2513         vecs->mpvec = mpvec;
2514
2515         /*
2516          * start dm event waiter threads for these new maps
2517          */
2518         vector_foreach_slot(vecs->mpvec, mpp, i) {
2519                 if (wait_for_events(mpp, vecs)) {
2520                         remove_map(mpp, vecs, 1);
2521                         i--;
2522                         continue;
2523                 }
2524                 if (setup_multipath(vecs, mpp))
2525                         i--;
2526         }
2527         return 0;
2528
2529 fail:
2530         vector_free(mpvec);
2531         return 1;
2532 }
2533
2534 int
2535 need_to_delay_reconfig(struct vectors * vecs)
2536 {
2537         struct multipath *mpp;
2538         int i;
2539
2540         if (!VECTOR_SIZE(vecs->mpvec))
2541                 return 0;
2542
2543         vector_foreach_slot(vecs->mpvec, mpp, i) {
2544                 if (mpp->wait_for_udev)
2545                         return 1;
2546         }
2547         return 0;
2548 }
2549
2550 void rcu_free_config(struct rcu_head *head)
2551 {
2552         struct config *conf = container_of(head, struct config, rcu);
2553
2554         free_config(conf);
2555 }
2556
2557 int
2558 reconfigure (struct vectors * vecs)
2559 {
2560         struct config * old, *conf;
2561
2562         conf = load_config(DEFAULT_CONFIGFILE);
2563         if (!conf)
2564                 return 1;
2565
2566         /*
2567          * free old map and path vectors ... they use old conf state
2568          */
2569         if (VECTOR_SIZE(vecs->mpvec))
2570                 remove_maps_and_stop_waiters(vecs);
2571
2572         free_pathvec(vecs->pathvec, FREE_PATHS);
2573         vecs->pathvec = NULL;
2574         delete_all_foreign();
2575
2576         reset_checker_classes();
2577         /* Re-read any timezone changes */
2578         tzset();
2579
2580         dm_tgt_version(conf->version, TGT_MPATH);
2581         if (verbosity)
2582                 conf->verbosity = verbosity;
2583         if (bindings_read_only)
2584                 conf->bindings_read_only = bindings_read_only;
2585         uxsock_timeout = conf->uxsock_timeout;
2586
2587         old = rcu_dereference(multipath_conf);
2588         conf->sequence_nr = old->sequence_nr + 1;
2589         rcu_assign_pointer(multipath_conf, conf);
2590         call_rcu(&old->rcu, rcu_free_config);
2591
2592         configure(vecs);
2593
2594
2595         return 0;
2596 }
2597
2598 static struct vectors *
2599 init_vecs (void)
2600 {
2601         struct vectors * vecs;
2602
2603         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
2604
2605         if (!vecs)
2606                 return NULL;
2607
2608         pthread_mutex_init(&vecs->lock.mutex, NULL);
2609
2610         return vecs;
2611 }
2612
2613 static void *
2614 signal_set(int signo, void (*func) (int))
2615 {
2616         int r;
2617         struct sigaction sig;
2618         struct sigaction osig;
2619
2620         sig.sa_handler = func;
2621         sigemptyset(&sig.sa_mask);
2622         sig.sa_flags = 0;
2623
2624         r = sigaction(signo, &sig, &osig);
2625
2626         if (r < 0)
2627                 return (SIG_ERR);
2628         else
2629                 return (osig.sa_handler);
2630 }
2631
2632 void
2633 handle_signals(bool nonfatal)
2634 {
2635         if (exit_sig) {
2636                 condlog(2, "exit (signal)");
2637                 exit_sig = 0;
2638                 exit_daemon();
2639         }
2640         if (!nonfatal)
2641                 return;
2642         if (reconfig_sig) {
2643                 condlog(2, "reconfigure (signal)");
2644                 set_config_state(DAEMON_CONFIGURE);
2645         }
2646         if (log_reset_sig) {
2647                 condlog(2, "reset log (signal)");
2648                 if (logsink == 1)
2649                         log_thread_reset();
2650         }
2651         reconfig_sig = 0;
2652         log_reset_sig = 0;
2653 }
2654
2655 static void
2656 sighup(__attribute__((unused)) int sig)
2657 {
2658         reconfig_sig = 1;
2659 }
2660
2661 static void
2662 sigend(__attribute__((unused)) int sig)
2663 {
2664         exit_sig = 1;
2665 }
2666
2667 static void
2668 sigusr1(__attribute__((unused)) int sig)
2669 {
2670         log_reset_sig = 1;
2671 }
2672
2673 static void
2674 sigusr2(__attribute__((unused)) int sig)
2675 {
2676         condlog(3, "SIGUSR2 received");
2677 }
2678
2679 static void
2680 signal_init(void)
2681 {
2682         sigset_t set;
2683
2684         /* block all signals */
2685         sigfillset(&set);
2686         /* SIGPIPE occurs if logging fails */
2687         sigdelset(&set, SIGPIPE);
2688         pthread_sigmask(SIG_SETMASK, &set, NULL);
2689
2690         /* Other signals will be unblocked in the uxlsnr thread */
2691         signal_set(SIGHUP, sighup);
2692         signal_set(SIGUSR1, sigusr1);
2693         signal_set(SIGUSR2, sigusr2);
2694         signal_set(SIGINT, sigend);
2695         signal_set(SIGTERM, sigend);
2696         signal_set(SIGPIPE, sigend);
2697 }
2698
2699 static void
2700 setscheduler (void)
2701 {
2702         int res;
2703         static struct sched_param sched_param = {
2704                 .sched_priority = 99
2705         };
2706
2707         res = sched_setscheduler (0, SCHED_RR, &sched_param);
2708
2709         if (res == -1)
2710                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
2711         return;
2712 }
2713
2714 static void
2715 set_oom_adj (void)
2716 {
2717 #ifdef OOM_SCORE_ADJ_MIN
2718         int retry = 1;
2719         char *file = "/proc/self/oom_score_adj";
2720         int score = OOM_SCORE_ADJ_MIN;
2721 #else
2722         int retry = 0;
2723         char *file = "/proc/self/oom_adj";
2724         int score = OOM_ADJUST_MIN;
2725 #endif
2726         FILE *fp;
2727         struct stat st;
2728         char *envp;
2729
2730         envp = getenv("OOMScoreAdjust");
2731         if (envp) {
2732                 condlog(3, "Using systemd provided OOMScoreAdjust");
2733                 return;
2734         }
2735         do {
2736                 if (stat(file, &st) == 0){
2737                         fp = fopen(file, "w");
2738                         if (!fp) {
2739                                 condlog(0, "couldn't fopen %s : %s", file,
2740                                         strerror(errno));
2741                                 return;
2742                         }
2743                         fprintf(fp, "%i", score);
2744                         fclose(fp);
2745                         return;
2746                 }
2747                 if (errno != ENOENT) {
2748                         condlog(0, "couldn't stat %s : %s", file,
2749                                 strerror(errno));
2750                         return;
2751                 }
2752 #ifdef OOM_ADJUST_MIN
2753                 file = "/proc/self/oom_adj";
2754                 score = OOM_ADJUST_MIN;
2755 #else
2756                 retry = 0;
2757 #endif
2758         } while (retry--);
2759         condlog(0, "couldn't adjust oom score");
2760 }
2761
2762 static int
2763 child (__attribute__((unused)) void *param)
2764 {
2765         pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr;
2766         pthread_attr_t log_attr, misc_attr, uevent_attr;
2767         struct vectors * vecs;
2768         struct multipath * mpp;
2769         int i;
2770 #ifdef USE_SYSTEMD
2771         unsigned long checkint;
2772         int startup_done = 0;
2773 #endif
2774         int rc;
2775         int pid_fd = -1;
2776         struct config *conf;
2777         char *envp;
2778         int queue_without_daemon;
2779         enum daemon_status state;
2780
2781         mlockall(MCL_CURRENT | MCL_FUTURE);
2782         signal_init();
2783         rcu_init();
2784
2785         setup_thread_attr(&misc_attr, 64 * 1024, 0);
2786         setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 0);
2787         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
2788         setup_thread_attr(&io_err_stat_attr, 32 * 1024, 0);
2789
2790         if (logsink == 1) {
2791                 setup_thread_attr(&log_attr, 64 * 1024, 0);
2792                 log_thread_start(&log_attr);
2793                 pthread_attr_destroy(&log_attr);
2794         }
2795         pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
2796         if (pid_fd < 0) {
2797                 condlog(1, "failed to create pidfile");
2798                 if (logsink == 1)
2799                         log_thread_stop();
2800                 exit(1);
2801         }
2802
2803         post_config_state(DAEMON_START);
2804
2805         condlog(2, "--------start up--------");
2806         condlog(2, "read " DEFAULT_CONFIGFILE);
2807
2808         conf = load_config(DEFAULT_CONFIGFILE);
2809         if (!conf)
2810                 goto failed;
2811
2812         if (verbosity)
2813                 conf->verbosity = verbosity;
2814         if (bindings_read_only)
2815                 conf->bindings_read_only = bindings_read_only;
2816         uxsock_timeout = conf->uxsock_timeout;
2817         rcu_assign_pointer(multipath_conf, conf);
2818         if (init_checkers(conf->multipath_dir)) {
2819                 condlog(0, "failed to initialize checkers");
2820                 goto failed;
2821         }
2822         if (init_prio(conf->multipath_dir)) {
2823                 condlog(0, "failed to initialize prioritizers");
2824                 goto failed;
2825         }
2826         /* Failing this is non-fatal */
2827
2828         init_foreign(conf->multipath_dir, conf->enable_foreign);
2829
2830         if (poll_dmevents)
2831                 poll_dmevents = dmevent_poll_supported();
2832         setlogmask(LOG_UPTO(conf->verbosity + 3));
2833
2834         envp = getenv("LimitNOFILE");
2835
2836         if (envp)
2837                 condlog(2,"Using systemd provided open fds limit of %s", envp);
2838         else
2839                 set_max_fds(conf->max_fds);
2840
2841         vecs = gvecs = init_vecs();
2842         if (!vecs)
2843                 goto failed;
2844
2845         setscheduler();
2846         set_oom_adj();
2847
2848 #ifdef USE_SYSTEMD
2849         envp = getenv("WATCHDOG_USEC");
2850         if (envp && sscanf(envp, "%lu", &checkint) == 1) {
2851                 /* Value is in microseconds */
2852                 conf->max_checkint = checkint / 1000000;
2853                 /* Rescale checkint */
2854                 if (conf->checkint > conf->max_checkint)
2855                         conf->checkint = conf->max_checkint;
2856                 else
2857                         conf->checkint = conf->max_checkint / 4;
2858                 condlog(3, "enabling watchdog, interval %d max %d",
2859                         conf->checkint, conf->max_checkint);
2860                 use_watchdog = conf->checkint;
2861         }
2862 #endif
2863         /*
2864          * Startup done, invalidate configuration
2865          */
2866         conf = NULL;
2867
2868         pthread_cleanup_push(config_cleanup, NULL);
2869         pthread_mutex_lock(&config_lock);
2870
2871         __post_config_state(DAEMON_IDLE);
2872         rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
2873         if (!rc) {
2874                 /* Wait for uxlsnr startup */
2875                 while (running_state == DAEMON_IDLE)
2876                         pthread_cond_wait(&config_cond, &config_lock);
2877                 state = running_state;
2878         }
2879         pthread_cleanup_pop(1);
2880
2881         if (rc) {
2882                 condlog(0, "failed to create cli listener: %d", rc);
2883                 goto failed;
2884         }
2885         else if (state != DAEMON_CONFIGURE) {
2886                 condlog(0, "cli listener failed to start");
2887                 goto failed;
2888         }
2889
2890         if (poll_dmevents) {
2891                 if (init_dmevent_waiter(vecs)) {
2892                         condlog(0, "failed to allocate dmevents waiter info");
2893                         goto failed;
2894                 }
2895                 if ((rc = pthread_create(&dmevent_thr, &misc_attr,
2896                                          wait_dmevents, NULL))) {
2897                         condlog(0, "failed to create dmevent waiter thread: %d",
2898                                 rc);
2899                         goto failed;
2900                 }
2901         }
2902
2903         /*
2904          * Start uevent listener early to catch events
2905          */
2906         if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
2907                 condlog(0, "failed to create uevent thread: %d", rc);
2908                 goto failed;
2909         }
2910         pthread_attr_destroy(&uevent_attr);
2911
2912         /*
2913          * start threads
2914          */
2915         if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
2916                 condlog(0,"failed to create checker loop thread: %d", rc);
2917                 goto failed;
2918         }
2919         if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
2920                 condlog(0, "failed to create uevent dispatcher: %d", rc);
2921                 goto failed;
2922         }
2923         pthread_attr_destroy(&misc_attr);
2924
2925         while (1) {
2926                 pthread_cleanup_push(config_cleanup, NULL);
2927                 pthread_mutex_lock(&config_lock);
2928                 while (running_state != DAEMON_CONFIGURE &&
2929                        running_state != DAEMON_SHUTDOWN)
2930                         pthread_cond_wait(&config_cond, &config_lock);
2931                 state = running_state;
2932                 pthread_cleanup_pop(1);
2933                 if (state == DAEMON_SHUTDOWN)
2934                         break;
2935                 if (state == DAEMON_CONFIGURE) {
2936                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2937                         lock(&vecs->lock);
2938                         pthread_testcancel();
2939                         if (!need_to_delay_reconfig(vecs)) {
2940                                 reconfigure(vecs);
2941                         } else {
2942                                 conf = get_multipath_config();
2943                                 conf->delayed_reconfig = 1;
2944                                 put_multipath_config(conf);
2945                         }
2946                         lock_cleanup_pop(vecs->lock);
2947                         post_config_state(DAEMON_IDLE);
2948 #ifdef USE_SYSTEMD
2949                         if (!startup_done) {
2950                                 sd_notify(0, "READY=1");
2951                                 startup_done = 1;
2952                         }
2953 #endif
2954                 }
2955         }
2956
2957         lock(&vecs->lock);
2958         conf = get_multipath_config();
2959         queue_without_daemon = conf->queue_without_daemon;
2960         put_multipath_config(conf);
2961         if (queue_without_daemon == QUE_NO_DAEMON_OFF)
2962                 vector_foreach_slot(vecs->mpvec, mpp, i)
2963                         dm_queue_if_no_path(mpp->alias, 0);
2964         remove_maps_and_stop_waiters(vecs);
2965         unlock(&vecs->lock);
2966
2967         pthread_cancel(check_thr);
2968         pthread_cancel(uevent_thr);
2969         pthread_cancel(uxlsnr_thr);
2970         pthread_cancel(uevq_thr);
2971         if (poll_dmevents)
2972                 pthread_cancel(dmevent_thr);
2973
2974         pthread_join(check_thr, NULL);
2975         pthread_join(uevent_thr, NULL);
2976         pthread_join(uxlsnr_thr, NULL);
2977         pthread_join(uevq_thr, NULL);
2978         if (poll_dmevents)
2979                 pthread_join(dmevent_thr, NULL);
2980
2981         stop_io_err_stat_thread();
2982
2983         lock(&vecs->lock);
2984         free_pathvec(vecs->pathvec, FREE_PATHS);
2985         vecs->pathvec = NULL;
2986         unlock(&vecs->lock);
2987
2988         pthread_mutex_destroy(&vecs->lock.mutex);
2989         FREE(vecs);
2990         vecs = NULL;
2991
2992         cleanup_foreign();
2993         cleanup_checkers();
2994         cleanup_prio();
2995         if (poll_dmevents)
2996                 cleanup_dmevent_waiter();
2997
2998         dm_lib_release();
2999         dm_lib_exit();
3000
3001         /* We're done here */
3002         condlog(3, "unlink pidfile");
3003         unlink(DEFAULT_PIDFILE);
3004
3005         condlog(2, "--------shut down-------");
3006
3007         if (logsink == 1)
3008                 log_thread_stop();
3009
3010         /*
3011          * Freeing config must be done after condlog() and dm_lib_exit(),
3012          * because logging functions like dlog() and dm_write_log()
3013          * reference the config.
3014          */
3015         conf = rcu_dereference(multipath_conf);
3016         rcu_assign_pointer(multipath_conf, NULL);
3017         call_rcu(&conf->rcu, rcu_free_config);
3018         udev_unref(udev);
3019         udev = NULL;
3020         pthread_attr_destroy(&waiter_attr);
3021         pthread_attr_destroy(&io_err_stat_attr);
3022 #ifdef _DEBUG_
3023         dbg_free_final(NULL);
3024 #endif
3025
3026 #ifdef USE_SYSTEMD
3027         sd_notify(0, "ERRNO=0");
3028 #endif
3029         exit(0);
3030
3031 failed:
3032 #ifdef USE_SYSTEMD
3033         sd_notify(0, "ERRNO=1");
3034 #endif
3035         if (pid_fd >= 0)
3036                 close(pid_fd);
3037         exit(1);
3038 }
3039
3040 static int
3041 daemonize(void)
3042 {
3043         int pid;
3044         int dev_null_fd;
3045
3046         if( (pid = fork()) < 0){
3047                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
3048                 return -1;
3049         }
3050         else if (pid != 0)
3051                 return pid;
3052
3053         setsid();
3054
3055         if ( (pid = fork()) < 0)
3056                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
3057         else if (pid != 0)
3058                 _exit(0);
3059
3060         if (chdir("/") < 0)
3061                 fprintf(stderr, "cannot chdir to '/', continuing\n");
3062
3063         dev_null_fd = open("/dev/null", O_RDWR);
3064         if (dev_null_fd < 0){
3065                 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
3066                         strerror(errno));
3067                 _exit(0);
3068         }
3069
3070         close(STDIN_FILENO);
3071         if (dup(dev_null_fd) < 0) {
3072                 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
3073                         strerror(errno));
3074                 _exit(0);
3075         }
3076         close(STDOUT_FILENO);
3077         if (dup(dev_null_fd) < 0) {
3078                 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
3079                         strerror(errno));
3080                 _exit(0);
3081         }
3082         close(STDERR_FILENO);
3083         if (dup(dev_null_fd) < 0) {
3084                 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
3085                         strerror(errno));
3086                 _exit(0);
3087         }
3088         close(dev_null_fd);
3089         daemon_pid = getpid();
3090         return 0;
3091 }
3092
3093 int
3094 main (int argc, char *argv[])
3095 {
3096         extern char *optarg;
3097         extern int optind;
3098         int arg;
3099         int err;
3100         int foreground = 0;
3101         struct config *conf;
3102
3103         ANNOTATE_BENIGN_RACE_SIZED(&multipath_conf, sizeof(multipath_conf),
3104                                    "Manipulated through RCU");
3105         ANNOTATE_BENIGN_RACE_SIZED(&uxsock_timeout, sizeof(uxsock_timeout),
3106                 "Suppress complaints about this scalar variable");
3107
3108         logsink = 1;
3109
3110         if (getuid() != 0) {
3111                 fprintf(stderr, "need to be root\n");
3112                 exit(1);
3113         }
3114
3115         /* make sure we don't lock any path */
3116         if (chdir("/") < 0)
3117                 fprintf(stderr, "can't chdir to root directory : %s\n",
3118                         strerror(errno));
3119         umask(umask(077) | 022);
3120
3121         pthread_cond_init_mono(&config_cond);
3122
3123         udev = udev_new();
3124         libmp_udev_set_sync_support(0);
3125
3126         while ((arg = getopt(argc, argv, ":dsv:k::Bniw")) != EOF ) {
3127                 switch(arg) {
3128                 case 'd':
3129                         foreground = 1;
3130                         if (logsink > 0)
3131                                 logsink = 0;
3132                         //debug=1; /* ### comment me out ### */
3133                         break;
3134                 case 'v':
3135                         if (sizeof(optarg) > sizeof(char *) ||
3136                             !isdigit(optarg[0]))
3137                                 exit(1);
3138
3139                         verbosity = atoi(optarg);
3140                         break;
3141                 case 's':
3142                         logsink = -1;
3143                         break;
3144                 case 'k':
3145                         logsink = 0;
3146                         conf = load_config(DEFAULT_CONFIGFILE);
3147                         if (!conf)
3148                                 exit(1);
3149                         if (verbosity)
3150                                 conf->verbosity = verbosity;
3151                         uxsock_timeout = conf->uxsock_timeout;
3152                         err = uxclnt(optarg, uxsock_timeout + 100);
3153                         free_config(conf);
3154                         return err;
3155                 case 'B':
3156                         bindings_read_only = 1;
3157                         break;
3158                 case 'n':
3159                         condlog(0, "WARNING: ignoring deprecated option -n, use 'ignore_wwids = no' instead");
3160                         break;
3161                 case 'w':
3162                         poll_dmevents = 0;
3163                         break;
3164                 default:
3165                         fprintf(stderr, "Invalid argument '-%c'\n",
3166                                 optopt);
3167                         exit(1);
3168                 }
3169         }
3170         if (optind < argc) {
3171                 char cmd[CMDSIZE];
3172                 char * s = cmd;
3173                 char * c = s;
3174
3175                 logsink = 0;
3176                 conf = load_config(DEFAULT_CONFIGFILE);
3177                 if (!conf)
3178                         exit(1);
3179                 if (verbosity)
3180                         conf->verbosity = verbosity;
3181                 uxsock_timeout = conf->uxsock_timeout;
3182                 memset(cmd, 0x0, CMDSIZE);
3183                 while (optind < argc) {
3184                         if (strchr(argv[optind], ' '))
3185                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
3186                         else
3187                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
3188                         optind++;
3189                 }
3190                 c += snprintf(c, s + CMDSIZE - c, "\n");
3191                 err = uxclnt(s, uxsock_timeout + 100);
3192                 free_config(conf);
3193                 return err;
3194         }
3195
3196         if (foreground) {
3197                 if (!isatty(fileno(stdout)))
3198                         setbuf(stdout, NULL);
3199                 err = 0;
3200                 daemon_pid = getpid();
3201         } else
3202                 err = daemonize();
3203
3204         if (err < 0)
3205                 /* error */
3206                 exit(1);
3207         else if (err > 0)
3208                 /* parent dies */
3209                 exit(0);
3210         else
3211                 /* child lives */
3212                 return (child(NULL));
3213 }
3214
3215 void *  mpath_pr_event_handler_fn (void * pathp )