multipathd: move set_no_path_retry() back to libmultipath
[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, int add_active)
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                 if (add_active)
1632                         update_queue_mode_add_path(pp->mpp);
1633         }
1634         return ret;
1635 }
1636
1637 static void
1638 enable_group(struct path * pp)
1639 {
1640         struct pathgroup * pgp;
1641
1642         /*
1643          * if path is added through uev_add_path, pgindex can be unset.
1644          * next update_strings() will set it, upon map reload event.
1645          *
1646          * we can safely return here, because upon map reload, all
1647          * PG will be enabled.
1648          */
1649         if (!pp->mpp->pg || !pp->pgindex)
1650                 return;
1651
1652         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1653
1654         if (pgp->status == PGSTATE_DISABLED) {
1655                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1656                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1657         }
1658 }
1659
1660 static void
1661 mpvec_garbage_collector (struct vectors * vecs)
1662 {
1663         struct multipath * mpp;
1664         unsigned int i;
1665
1666         if (!vecs->mpvec)
1667                 return;
1668
1669         vector_foreach_slot (vecs->mpvec, mpp, i) {
1670                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1671                         condlog(2, "%s: remove dead map", mpp->alias);
1672                         remove_map_and_stop_waiter(mpp, vecs);
1673                         i--;
1674                 }
1675         }
1676 }
1677
1678 /* This is called after a path has started working again. It the multipath
1679  * device for this path uses the followover failback type, and this is the
1680  * best pathgroup, and this is the first path in the pathgroup to come back
1681  * up, then switch to this pathgroup */
1682 static int
1683 followover_should_failback(struct path * pp)
1684 {
1685         struct pathgroup * pgp;
1686         struct path *pp1;
1687         int i;
1688
1689         if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1690             !pp->mpp->pg || !pp->pgindex ||
1691             pp->pgindex != pp->mpp->bestpg)
1692                 return 0;
1693
1694         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1695         vector_foreach_slot(pgp->paths, pp1, i) {
1696                 if (pp1 == pp)
1697                         continue;
1698                 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1699                         return 0;
1700         }
1701         return 1;
1702 }
1703
1704 static void
1705 missing_uev_wait_tick(struct vectors *vecs)
1706 {
1707         struct multipath * mpp;
1708         unsigned int i;
1709         int timed_out = 0, delayed_reconfig;
1710         struct config *conf;
1711
1712         vector_foreach_slot (vecs->mpvec, mpp, i) {
1713                 if (mpp->wait_for_udev && --mpp->uev_wait_tick <= 0) {
1714                         timed_out = 1;
1715                         condlog(0, "%s: timeout waiting on creation uevent. enabling reloads", mpp->alias);
1716                         if (mpp->wait_for_udev > 1 &&
1717                             update_map(mpp, vecs, 0)) {
1718                                 /* update_map removed map */
1719                                 i--;
1720                                 continue;
1721                         }
1722                         mpp->wait_for_udev = 0;
1723                 }
1724         }
1725
1726         conf = get_multipath_config();
1727         delayed_reconfig = conf->delayed_reconfig;
1728         put_multipath_config(conf);
1729         if (timed_out && delayed_reconfig &&
1730             !need_to_delay_reconfig(vecs)) {
1731                 condlog(2, "reconfigure (delayed)");
1732                 set_config_state(DAEMON_CONFIGURE);
1733         }
1734 }
1735
1736 static void
1737 ghost_delay_tick(struct vectors *vecs)
1738 {
1739         struct multipath * mpp;
1740         unsigned int i;
1741
1742         vector_foreach_slot (vecs->mpvec, mpp, i) {
1743                 if (mpp->ghost_delay_tick <= 0)
1744                         continue;
1745                 if (--mpp->ghost_delay_tick <= 0) {
1746                         condlog(0, "%s: timed out waiting for active path",
1747                                 mpp->alias);
1748                         mpp->force_udev_reload = 1;
1749                         if (update_map(mpp, vecs, 0) != 0) {
1750                                 /* update_map removed map */
1751                                 i--;
1752                                 continue;
1753                         }
1754                 }
1755         }
1756 }
1757
1758 static void
1759 defered_failback_tick (vector mpvec)
1760 {
1761         struct multipath * mpp;
1762         unsigned int i;
1763
1764         vector_foreach_slot (mpvec, mpp, i) {
1765                 /*
1766                  * deferred failback getting sooner
1767                  */
1768                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1769                         mpp->failback_tick--;
1770
1771                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1772                                 switch_pathgroup(mpp);
1773                 }
1774         }
1775 }
1776
1777 static void
1778 retry_count_tick(vector mpvec)
1779 {
1780         struct multipath *mpp;
1781         unsigned int i;
1782
1783         vector_foreach_slot (mpvec, mpp, i) {
1784                 if (mpp->retry_tick > 0) {
1785                         mpp->stat_total_queueing_time++;
1786                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1787                         if(--mpp->retry_tick == 0) {
1788                                 mpp->stat_map_failures++;
1789                                 dm_queue_if_no_path(mpp->alias, 0);
1790                                 condlog(2, "%s: Disable queueing", mpp->alias);
1791                         }
1792                 }
1793         }
1794 }
1795
1796 int update_prio(struct path *pp, int refresh_all)
1797 {
1798         int oldpriority;
1799         struct path *pp1;
1800         struct pathgroup * pgp;
1801         int i, j, changed = 0;
1802         struct config *conf;
1803
1804         if (refresh_all) {
1805                 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1806                         vector_foreach_slot (pgp->paths, pp1, j) {
1807                                 oldpriority = pp1->priority;
1808                                 conf = get_multipath_config();
1809                                 pthread_cleanup_push(put_multipath_config,
1810                                                      conf);
1811                                 pathinfo(pp1, conf, DI_PRIO);
1812                                 pthread_cleanup_pop(1);
1813                                 if (pp1->priority != oldpriority)
1814                                         changed = 1;
1815                         }
1816                 }
1817                 return changed;
1818         }
1819         oldpriority = pp->priority;
1820         conf = get_multipath_config();
1821         pthread_cleanup_push(put_multipath_config, conf);
1822         if (pp->state != PATH_DOWN)
1823                 pathinfo(pp, conf, DI_PRIO);
1824         pthread_cleanup_pop(1);
1825
1826         if (pp->priority == oldpriority)
1827                 return 0;
1828         return 1;
1829 }
1830
1831 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1832 {
1833         if (reload_map(vecs, mpp, refresh, 1))
1834                 return 1;
1835
1836         dm_lib_release();
1837         if (setup_multipath(vecs, mpp) != 0)
1838                 return 2;
1839         sync_map_state(mpp);
1840
1841         return 0;
1842 }
1843
1844 static int check_path_reinstate_state(struct path * pp) {
1845         struct timespec curr_time;
1846
1847         /*
1848          * This function is only called when the path state changes
1849          * from "bad" to "good". pp->state reflects the *previous* state.
1850          * If this was "bad", we know that a failure must have occured
1851          * beforehand, and count that.
1852          * Note that we count path state _changes_ this way. If a path
1853          * remains in "bad" state, failure count is not increased.
1854          */
1855
1856         if (!((pp->mpp->san_path_err_threshold > 0) &&
1857                                 (pp->mpp->san_path_err_forget_rate > 0) &&
1858                                 (pp->mpp->san_path_err_recovery_time >0))) {
1859                 return 0;
1860         }
1861
1862         if (pp->disable_reinstate) {
1863                 /* If there are no other usable paths, reinstate the path */
1864                 if (pp->mpp->nr_active == 0) {
1865                         condlog(2, "%s : reinstating path early", pp->dev);
1866                         goto reinstate_path;
1867                 }
1868                 get_monotonic_time(&curr_time);
1869
1870                 /* If path became failed again or continue failed, should reset
1871                  * path san_path_err_forget_rate and path dis_reinstate_time to
1872                  * start a new stable check. 
1873                  */
1874                 if ((pp->state != PATH_UP) && (pp->state != PATH_GHOST) &&
1875                         (pp->state != PATH_DELAYED)) {
1876                         pp->san_path_err_forget_rate =
1877                                 pp->mpp->san_path_err_forget_rate;
1878                         pp->dis_reinstate_time = curr_time.tv_sec;
1879                 }
1880
1881                 if ((curr_time.tv_sec - pp->dis_reinstate_time ) > pp->mpp->san_path_err_recovery_time) {
1882                         condlog(2,"%s : reinstate the path after err recovery time", pp->dev);
1883                         goto reinstate_path;
1884                 }
1885                 return 1;
1886         }
1887         /* forget errors on a working path */
1888         if ((pp->state == PATH_UP || pp->state == PATH_GHOST) &&
1889                         pp->path_failures > 0) {
1890                 if (pp->san_path_err_forget_rate > 0){
1891                         pp->san_path_err_forget_rate--;
1892                 } else {
1893                         /* for every san_path_err_forget_rate number of
1894                          * successful path checks decrement path_failures by 1
1895                          */
1896                         pp->path_failures--;
1897                         pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
1898                 }
1899                 return 0;
1900         }
1901
1902         /* If the path isn't recovering from a failed state, do nothing */
1903         if (pp->state != PATH_DOWN && pp->state != PATH_SHAKY &&
1904                         pp->state != PATH_TIMEOUT)
1905                 return 0;
1906
1907         if (pp->path_failures == 0)
1908                 pp->san_path_err_forget_rate = pp->mpp->san_path_err_forget_rate;
1909
1910         pp->path_failures++;
1911
1912         /* if we don't know the currently time, we don't know how long to
1913          * delay the path, so there's no point in checking if we should
1914          */
1915
1916         get_monotonic_time(&curr_time);
1917         /* when path failures has exceeded the san_path_err_threshold
1918          * place the path in delayed state till san_path_err_recovery_time
1919          * so that the cutomer can rectify the issue within this time. After
1920          * the completion of san_path_err_recovery_time it should
1921          * automatically reinstate the path
1922          * (note: we know that san_path_err_threshold > 0 here).
1923          */
1924         if (pp->path_failures > (unsigned int)pp->mpp->san_path_err_threshold) {
1925                 condlog(2, "%s : hit error threshold. Delaying path reinstatement", pp->dev);
1926                 pp->dis_reinstate_time = curr_time.tv_sec;
1927                 pp->disable_reinstate = 1;
1928
1929                 return 1;
1930         } else {
1931                 return 0;
1932         }
1933
1934 reinstate_path:
1935         pp->path_failures = 0;
1936         pp->disable_reinstate = 0;
1937         pp->san_path_err_forget_rate = 0;
1938         return 0;
1939 }
1940
1941 static int
1942 should_skip_path(struct path *pp){
1943         if (marginal_path_check_enabled(pp->mpp)) {
1944                 if (pp->io_err_disable_reinstate && need_io_err_check(pp))
1945                         return 1;
1946         } else if (san_path_check_enabled(pp->mpp)) {
1947                 if (check_path_reinstate_state(pp))
1948                         return 1;
1949         }
1950         return 0;
1951 }
1952
1953 /*
1954  * Returns '1' if the path has been checked, '-1' if it was blacklisted
1955  * and '0' otherwise
1956  */
1957 int
1958 check_path (struct vectors * vecs, struct path * pp, unsigned int ticks)
1959 {
1960         int newstate;
1961         int new_path_up = 0;
1962         int chkr_new_path_up = 0;
1963         int add_active;
1964         int disable_reinstate = 0;
1965         int oldchkrstate = pp->chkrstate;
1966         int retrigger_tries, verbosity;
1967         unsigned int checkint, max_checkint;
1968         struct config *conf;
1969         int marginal_pathgroups, marginal_changed = 0;
1970         int ret;
1971
1972         if ((pp->initialized == INIT_OK ||
1973              pp->initialized == INIT_REQUESTED_UDEV) && !pp->mpp)
1974                 return 0;
1975
1976         if (pp->tick)
1977                 pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
1978         if (pp->tick)
1979                 return 0; /* don't check this path yet */
1980
1981         conf = get_multipath_config();
1982         retrigger_tries = conf->retrigger_tries;
1983         checkint = conf->checkint;
1984         max_checkint = conf->max_checkint;
1985         verbosity = conf->verbosity;
1986         marginal_pathgroups = conf->marginal_pathgroups;
1987         put_multipath_config(conf);
1988
1989         if (pp->checkint == CHECKINT_UNDEF) {
1990                 condlog(0, "%s: BUG: checkint is not set", pp->dev);
1991                 pp->checkint = checkint;
1992         };
1993
1994         if (!pp->mpp && pp->initialized == INIT_MISSING_UDEV) {
1995                 if (pp->retriggers < retrigger_tries) {
1996                         condlog(2, "%s: triggering change event to reinitialize",
1997                                 pp->dev);
1998                         pp->initialized = INIT_REQUESTED_UDEV;
1999                         pp->retriggers++;
2000                         sysfs_attr_set_value(pp->udev, "uevent", "change",
2001                                              strlen("change"));
2002                         return 0;
2003                 } else {
2004                         condlog(1, "%s: not initialized after %d udev retriggers",
2005                                 pp->dev, retrigger_tries);
2006                         /*
2007                          * Make sure that the "add missing path" code path
2008                          * below may reinstate the path later, if it ever
2009                          * comes up again.
2010                          * The WWID needs not be cleared; if it was set, the
2011                          * state hadn't been INIT_MISSING_UDEV in the first
2012                          * place.
2013                          */
2014                         pp->initialized = INIT_FAILED;
2015                         return 0;
2016                 }
2017         }
2018
2019         /*
2020          * provision a next check soonest,
2021          * in case we exit abnormaly from here
2022          */
2023         pp->tick = checkint;
2024
2025         newstate = path_offline(pp);
2026         if (newstate == PATH_UP) {
2027                 conf = get_multipath_config();
2028                 pthread_cleanup_push(put_multipath_config, conf);
2029                 newstate = get_state(pp, conf, 1, newstate);
2030                 pthread_cleanup_pop(1);
2031         } else {
2032                 checker_clear_message(&pp->checker);
2033                 condlog(3, "%s: state %s, checker not called",
2034                         pp->dev, checker_state_name(newstate));
2035         }
2036         /*
2037          * Wait for uevent for removed paths;
2038          * some LLDDs like zfcp keep paths unavailable
2039          * without sending uevents.
2040          */
2041         if (newstate == PATH_REMOVED)
2042                 newstate = PATH_DOWN;
2043
2044         if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
2045                 condlog(2, "%s: unusable path (%s) - checker failed",
2046                         pp->dev, checker_state_name(newstate));
2047                 LOG_MSG(2, verbosity, pp);
2048                 conf = get_multipath_config();
2049                 pthread_cleanup_push(put_multipath_config, conf);
2050                 pathinfo(pp, conf, 0);
2051                 pthread_cleanup_pop(1);
2052                 return 1;
2053         } else if ((newstate != PATH_UP && newstate != PATH_GHOST) &&
2054                         (pp->state == PATH_DELAYED)) {
2055                 /* If path state become failed again cancel path delay state */
2056                 pp->state = newstate;
2057                 return 1;
2058         }
2059         if (!pp->mpp) {
2060                 if (!strlen(pp->wwid) &&
2061                     (pp->initialized == INIT_FAILED ||
2062                      pp->initialized == INIT_NEW) &&
2063                     (newstate == PATH_UP || newstate == PATH_GHOST)) {
2064                         condlog(2, "%s: add missing path", pp->dev);
2065                         conf = get_multipath_config();
2066                         pthread_cleanup_push(put_multipath_config, conf);
2067                         ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
2068                         pthread_cleanup_pop(1);
2069                         /* INIT_OK implies ret == PATHINFO_OK */
2070                         if (pp->initialized == INIT_OK) {
2071                                 ev_add_path(pp, vecs, 1);
2072                                 pp->tick = 1;
2073                         } else {
2074                                 /*
2075                                  * We failed multiple times to initialize this
2076                                  * path properly. Don't re-check too often.
2077                                  */
2078                                 pp->checkint = max_checkint;
2079                                 if (ret == PATHINFO_SKIPPED)
2080                                         return -1;
2081                         }
2082                 }
2083                 return 0;
2084         }
2085         /*
2086          * Async IO in flight. Keep the previous path state
2087          * and reschedule as soon as possible
2088          */
2089         if (newstate == PATH_PENDING) {
2090                 pp->tick = 1;
2091                 return 0;
2092         }
2093         /*
2094          * Synchronize with kernel state
2095          */
2096         if (update_multipath_strings(pp->mpp, vecs->pathvec, 1)) {
2097                 condlog(1, "%s: Could not synchronize with kernel state",
2098                         pp->dev);
2099                 pp->dmstate = PSTATE_UNDEF;
2100         }
2101         /* if update_multipath_strings orphaned the path, quit early */
2102         if (!pp->mpp)
2103                 return 0;
2104         set_no_path_retry(pp->mpp);
2105
2106         if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
2107             (san_path_check_enabled(pp->mpp) ||
2108              marginal_path_check_enabled(pp->mpp))) {
2109                 int was_marginal = pp->marginal;
2110                 if (should_skip_path(pp)) {
2111                         if (!marginal_pathgroups) {
2112                                 if (marginal_path_check_enabled(pp->mpp))
2113                                         /* to reschedule as soon as possible,
2114                                          * so that this path can be recovered
2115                                          * in time */
2116                                         pp->tick = 1;
2117                                 pp->state = PATH_DELAYED;
2118                                 return 1;
2119                         }
2120                         if (!was_marginal) {
2121                                 pp->marginal = 1;
2122                                 marginal_changed = 1;
2123                         }
2124                 } else if (marginal_pathgroups && was_marginal) {
2125                         pp->marginal = 0;
2126                         marginal_changed = 1;
2127                 }
2128         }
2129
2130         /*
2131          * don't reinstate failed path, if its in stand-by
2132          * and if target supports only implicit tpgs mode.
2133          * this will prevent unnecessary i/o by dm on stand-by
2134          * paths if there are no other active paths in map.
2135          */
2136         disable_reinstate = (newstate == PATH_GHOST &&
2137                              pp->mpp->nr_active == 0 &&
2138                              path_get_tpgs(pp) == TPGS_IMPLICIT) ? 1 : 0;
2139
2140         pp->chkrstate = newstate;
2141         if (newstate != pp->state) {
2142                 int oldstate = pp->state;
2143                 pp->state = newstate;
2144
2145                 LOG_MSG(1, verbosity, pp);
2146
2147                 /*
2148                  * upon state change, reset the checkint
2149                  * to the shortest delay
2150                  */
2151                 conf = get_multipath_config();
2152                 pp->checkint = conf->checkint;
2153                 put_multipath_config(conf);
2154
2155                 if (newstate != PATH_UP && newstate != PATH_GHOST) {
2156                         /*
2157                          * proactively fail path in the DM
2158                          */
2159                         if (oldstate == PATH_UP ||
2160                             oldstate == PATH_GHOST)
2161                                 fail_path(pp, 1);
2162                         else
2163                                 fail_path(pp, 0);
2164
2165                         /*
2166                          * cancel scheduled failback
2167                          */
2168                         pp->mpp->failback_tick = 0;
2169
2170                         pp->mpp->stat_path_failures++;
2171                         return 1;
2172                 }
2173
2174                 if (newstate == PATH_UP || newstate == PATH_GHOST) {
2175                         if (pp->mpp->prflag) {
2176                                 /*
2177                                  * Check Persistent Reservation.
2178                                  */
2179                                 condlog(2, "%s: checking persistent "
2180                                         "reservation registration", pp->dev);
2181                                 mpath_pr_event_handle(pp);
2182                         }
2183                 }
2184
2185                 /*
2186                  * reinstate this path
2187                  */
2188                 if (oldstate != PATH_UP &&
2189                     oldstate != PATH_GHOST)
2190                         add_active = 1;
2191                 else
2192                         add_active = 0;
2193                 if (!disable_reinstate && reinstate_path(pp, add_active)) {
2194                         condlog(3, "%s: reload map", pp->dev);
2195                         ev_add_path(pp, vecs, 1);
2196                         pp->tick = 1;
2197                         return 0;
2198                 }
2199                 new_path_up = 1;
2200
2201                 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
2202                         chkr_new_path_up = 1;
2203
2204                 /*
2205                  * if at least one path is up in a group, and
2206                  * the group is disabled, re-enable it
2207                  */
2208                 if (newstate == PATH_UP)
2209                         enable_group(pp);
2210         }
2211         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
2212                 if ((pp->dmstate == PSTATE_FAILED ||
2213                     pp->dmstate == PSTATE_UNDEF) &&
2214                     !disable_reinstate) {
2215                         /* Clear IO errors */
2216                         if (reinstate_path(pp, 0)) {
2217                                 condlog(3, "%s: reload map", pp->dev);
2218                                 ev_add_path(pp, vecs, 1);
2219                                 pp->tick = 1;
2220                                 return 0;
2221                         }
2222                 } else {
2223                         LOG_MSG(4, verbosity, pp);
2224                         if (pp->checkint != max_checkint) {
2225                                 /*
2226                                  * double the next check delay.
2227                                  * max at conf->max_checkint
2228                                  */
2229                                 if (pp->checkint < (max_checkint / 2))
2230                                         pp->checkint = 2 * pp->checkint;
2231                                 else
2232                                         pp->checkint = max_checkint;
2233
2234                                 condlog(4, "%s: delay next check %is",
2235                                         pp->dev_t, pp->checkint);
2236                         }
2237                         pp->tick = pp->checkint;
2238                 }
2239         }
2240         else if (newstate != PATH_UP && newstate != PATH_GHOST) {
2241                 if (pp->dmstate == PSTATE_ACTIVE ||
2242                     pp->dmstate == PSTATE_UNDEF)
2243                         fail_path(pp, 0);
2244                 if (newstate == PATH_DOWN) {
2245                         int log_checker_err;
2246
2247                         conf = get_multipath_config();
2248                         log_checker_err = conf->log_checker_err;
2249                         put_multipath_config(conf);
2250                         if (log_checker_err == LOG_CHKR_ERR_ONCE)
2251                                 LOG_MSG(3, verbosity, pp);
2252                         else
2253                                 LOG_MSG(2, verbosity, pp);
2254                 }
2255         }
2256
2257         pp->state = newstate;
2258
2259         if (pp->mpp->wait_for_udev)
2260                 return 1;
2261         /*
2262          * path prio refreshing
2263          */
2264         condlog(4, "path prio refresh");
2265
2266         if (marginal_changed)
2267                 update_path_groups(pp->mpp, vecs, 1);
2268         else if (update_prio(pp, new_path_up) &&
2269             (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
2270              pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
2271                 update_path_groups(pp->mpp, vecs, !new_path_up);
2272         else if (need_switch_pathgroup(pp->mpp, 0)) {
2273                 if (pp->mpp->pgfailback > 0 &&
2274                     (new_path_up || pp->mpp->failback_tick <= 0))
2275                         pp->mpp->failback_tick =
2276                                 pp->mpp->pgfailback + 1;
2277                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
2278                          (chkr_new_path_up && followover_should_failback(pp)))
2279                         switch_pathgroup(pp->mpp);
2280         }
2281         return 1;
2282 }
2283
2284 static void *
2285 checkerloop (void *ap)
2286 {
2287         struct vectors *vecs;
2288         struct path *pp;
2289         int count = 0;
2290         unsigned int i;
2291         struct timespec last_time;
2292         struct config *conf;
2293         int foreign_tick = 0;
2294
2295         pthread_cleanup_push(rcu_unregister, NULL);
2296         rcu_register_thread();
2297         mlockall(MCL_CURRENT | MCL_FUTURE);
2298         vecs = (struct vectors *)ap;
2299         condlog(2, "path checkers start up");
2300
2301         /* Tweak start time for initial path check */
2302         get_monotonic_time(&last_time);
2303         last_time.tv_sec -= 1;
2304
2305         while (1) {
2306                 struct timespec diff_time, start_time, end_time;
2307                 int num_paths = 0, strict_timing, rc = 0;
2308                 unsigned int ticks = 0;
2309
2310                 get_monotonic_time(&start_time);
2311                 if (start_time.tv_sec && last_time.tv_sec) {
2312                         timespecsub(&start_time, &last_time, &diff_time);
2313                         condlog(4, "tick (%lu.%06lu secs)",
2314                                 diff_time.tv_sec, diff_time.tv_nsec / 1000);
2315                         last_time = start_time;
2316                         ticks = diff_time.tv_sec;
2317                 } else {
2318                         ticks = 1;
2319                         condlog(4, "tick (%d ticks)", ticks);
2320                 }
2321 #ifdef USE_SYSTEMD
2322                 if (use_watchdog)
2323                         sd_notify(0, "WATCHDOG=1");
2324 #endif
2325                 rc = set_config_state(DAEMON_RUNNING);
2326                 if (rc == ETIMEDOUT) {
2327                         condlog(4, "timeout waiting for DAEMON_IDLE");
2328                         continue;
2329                 } else if (rc == EINVAL)
2330                         /* daemon shutdown */
2331                         break;
2332
2333                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2334                 lock(&vecs->lock);
2335                 pthread_testcancel();
2336                 vector_foreach_slot (vecs->pathvec, pp, i) {
2337                         rc = check_path(vecs, pp, ticks);
2338                         if (rc < 0) {
2339                                 vector_del_slot(vecs->pathvec, i);
2340                                 free_path(pp);
2341                                 i--;
2342                         } else
2343                                 num_paths += rc;
2344                 }
2345                 lock_cleanup_pop(vecs->lock);
2346
2347                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2348                 lock(&vecs->lock);
2349                 pthread_testcancel();
2350                 defered_failback_tick(vecs->mpvec);
2351                 retry_count_tick(vecs->mpvec);
2352                 missing_uev_wait_tick(vecs);
2353                 ghost_delay_tick(vecs);
2354                 lock_cleanup_pop(vecs->lock);
2355
2356                 if (count)
2357                         count--;
2358                 else {
2359                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2360                         lock(&vecs->lock);
2361                         pthread_testcancel();
2362                         condlog(4, "map garbage collection");
2363                         mpvec_garbage_collector(vecs);
2364                         count = MAPGCINT;
2365                         lock_cleanup_pop(vecs->lock);
2366                 }
2367
2368                 diff_time.tv_nsec = 0;
2369                 if (start_time.tv_sec) {
2370                         get_monotonic_time(&end_time);
2371                         timespecsub(&end_time, &start_time, &diff_time);
2372                         if (num_paths) {
2373                                 unsigned int max_checkint;
2374
2375                                 condlog(4, "checked %d path%s in %lu.%06lu secs",
2376                                         num_paths, num_paths > 1 ? "s" : "",
2377                                         diff_time.tv_sec,
2378                                         diff_time.tv_nsec / 1000);
2379                                 conf = get_multipath_config();
2380                                 max_checkint = conf->max_checkint;
2381                                 put_multipath_config(conf);
2382                                 if (diff_time.tv_sec > max_checkint)
2383                                         condlog(1, "path checkers took longer "
2384                                                 "than %lu seconds, consider "
2385                                                 "increasing max_polling_interval",
2386                                                 diff_time.tv_sec);
2387                         }
2388                 }
2389
2390                 if (foreign_tick == 0) {
2391                         conf = get_multipath_config();
2392                         foreign_tick = conf->max_checkint;
2393                         put_multipath_config(conf);
2394                 }
2395                 if (--foreign_tick == 0)
2396                         check_foreign();
2397
2398                 post_config_state(DAEMON_IDLE);
2399                 conf = get_multipath_config();
2400                 strict_timing = conf->strict_timing;
2401                 put_multipath_config(conf);
2402                 if (!strict_timing)
2403                         sleep(1);
2404                 else {
2405                         if (diff_time.tv_nsec) {
2406                                 diff_time.tv_sec = 0;
2407                                 diff_time.tv_nsec =
2408                                      1000UL * 1000 * 1000 - diff_time.tv_nsec;
2409                         } else
2410                                 diff_time.tv_sec = 1;
2411
2412                         condlog(3, "waiting for %lu.%06lu secs",
2413                                 diff_time.tv_sec,
2414                                 diff_time.tv_nsec / 1000);
2415                         if (nanosleep(&diff_time, NULL) != 0) {
2416                                 condlog(3, "nanosleep failed with error %d",
2417                                         errno);
2418                                 conf = get_multipath_config();
2419                                 conf->strict_timing = 0;
2420                                 put_multipath_config(conf);
2421                                 break;
2422                         }
2423                 }
2424         }
2425         pthread_cleanup_pop(1);
2426         return NULL;
2427 }
2428
2429 int
2430 configure (struct vectors * vecs)
2431 {
2432         struct multipath * mpp;
2433         struct path * pp;
2434         vector mpvec;
2435         int i, ret;
2436         struct config *conf;
2437         static int force_reload = FORCE_RELOAD_WEAK;
2438
2439         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
2440                 condlog(0, "couldn't allocate path vec in configure");
2441                 return 1;
2442         }
2443
2444         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc())) {
2445                 condlog(0, "couldn't allocate multipath vec in configure");
2446                 return 1;
2447         }
2448
2449         if (!(mpvec = vector_alloc())) {
2450                 condlog(0, "couldn't allocate new maps vec in configure");
2451                 return 1;
2452         }
2453
2454         /*
2455          * probe for current path (from sysfs) and map (from dm) sets
2456          */
2457         ret = path_discovery(vecs->pathvec, DI_ALL);
2458         if (ret < 0) {
2459                 condlog(0, "configure failed at path discovery");
2460                 goto fail;
2461         }
2462
2463         conf = get_multipath_config();
2464         pthread_cleanup_push(put_multipath_config, conf);
2465         vector_foreach_slot (vecs->pathvec, pp, i){
2466                 if (filter_path(conf, pp) > 0){
2467                         vector_del_slot(vecs->pathvec, i);
2468                         free_path(pp);
2469                         i--;
2470                 }
2471         }
2472         pthread_cleanup_pop(1);
2473
2474         if (map_discovery(vecs)) {
2475                 condlog(0, "configure failed at map discovery");
2476                 goto fail;
2477         }
2478
2479         /*
2480          * create new set of maps & push changed ones into dm
2481          * In the first call, use FORCE_RELOAD_WEAK to avoid making
2482          * superfluous ACT_RELOAD ioctls. Later calls are done
2483          * with FORCE_RELOAD_YES.
2484          */
2485         ret = coalesce_paths(vecs, mpvec, NULL, force_reload, CMD_NONE);
2486         if (force_reload == FORCE_RELOAD_WEAK)
2487                 force_reload = FORCE_RELOAD_YES;
2488         if (ret != CP_OK) {
2489                 condlog(0, "configure failed while coalescing paths");
2490                 goto fail;
2491         }
2492
2493         /*
2494          * may need to remove some maps which are no longer relevant
2495          * e.g., due to blacklist changes in conf file
2496          */
2497         if (coalesce_maps(vecs, mpvec)) {
2498                 condlog(0, "configure failed while coalescing maps");
2499                 goto fail;
2500         }
2501
2502         dm_lib_release();
2503
2504         sync_maps_state(mpvec);
2505         vector_foreach_slot(mpvec, mpp, i){
2506                 if (remember_wwid(mpp->wwid) == 1)
2507                         trigger_paths_udev_change(mpp, true);
2508                 update_map_pr(mpp);
2509         }
2510
2511         /*
2512          * purge dm of old maps
2513          */
2514         remove_maps(vecs);
2515
2516         /*
2517          * save new set of maps formed by considering current path state
2518          */
2519         vector_free(vecs->mpvec);
2520         vecs->mpvec = mpvec;
2521
2522         /*
2523          * start dm event waiter threads for these new maps
2524          */
2525         vector_foreach_slot(vecs->mpvec, mpp, i) {
2526                 if (wait_for_events(mpp, vecs)) {
2527                         remove_map(mpp, vecs, 1);
2528                         i--;
2529                         continue;
2530                 }
2531                 if (setup_multipath(vecs, mpp))
2532                         i--;
2533         }
2534         return 0;
2535
2536 fail:
2537         vector_free(mpvec);
2538         return 1;
2539 }
2540
2541 int
2542 need_to_delay_reconfig(struct vectors * vecs)
2543 {
2544         struct multipath *mpp;
2545         int i;
2546
2547         if (!VECTOR_SIZE(vecs->mpvec))
2548                 return 0;
2549
2550         vector_foreach_slot(vecs->mpvec, mpp, i) {
2551                 if (mpp->wait_for_udev)
2552                         return 1;
2553         }
2554         return 0;
2555 }
2556
2557 void rcu_free_config(struct rcu_head *head)
2558 {
2559         struct config *conf = container_of(head, struct config, rcu);
2560
2561         free_config(conf);
2562 }
2563
2564 int
2565 reconfigure (struct vectors * vecs)
2566 {
2567         struct config * old, *conf;
2568
2569         conf = load_config(DEFAULT_CONFIGFILE);
2570         if (!conf)
2571                 return 1;
2572
2573         /*
2574          * free old map and path vectors ... they use old conf state
2575          */
2576         if (VECTOR_SIZE(vecs->mpvec))
2577                 remove_maps_and_stop_waiters(vecs);
2578
2579         free_pathvec(vecs->pathvec, FREE_PATHS);
2580         vecs->pathvec = NULL;
2581         delete_all_foreign();
2582
2583         reset_checker_classes();
2584         /* Re-read any timezone changes */
2585         tzset();
2586
2587         dm_tgt_version(conf->version, TGT_MPATH);
2588         if (verbosity)
2589                 conf->verbosity = verbosity;
2590         if (bindings_read_only)
2591                 conf->bindings_read_only = bindings_read_only;
2592         uxsock_timeout = conf->uxsock_timeout;
2593
2594         old = rcu_dereference(multipath_conf);
2595         conf->sequence_nr = old->sequence_nr + 1;
2596         rcu_assign_pointer(multipath_conf, conf);
2597         call_rcu(&old->rcu, rcu_free_config);
2598
2599         configure(vecs);
2600
2601
2602         return 0;
2603 }
2604
2605 static struct vectors *
2606 init_vecs (void)
2607 {
2608         struct vectors * vecs;
2609
2610         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
2611
2612         if (!vecs)
2613                 return NULL;
2614
2615         pthread_mutex_init(&vecs->lock.mutex, NULL);
2616
2617         return vecs;
2618 }
2619
2620 static void *
2621 signal_set(int signo, void (*func) (int))
2622 {
2623         int r;
2624         struct sigaction sig;
2625         struct sigaction osig;
2626
2627         sig.sa_handler = func;
2628         sigemptyset(&sig.sa_mask);
2629         sig.sa_flags = 0;
2630
2631         r = sigaction(signo, &sig, &osig);
2632
2633         if (r < 0)
2634                 return (SIG_ERR);
2635         else
2636                 return (osig.sa_handler);
2637 }
2638
2639 void
2640 handle_signals(bool nonfatal)
2641 {
2642         if (exit_sig) {
2643                 condlog(2, "exit (signal)");
2644                 exit_sig = 0;
2645                 exit_daemon();
2646         }
2647         if (!nonfatal)
2648                 return;
2649         if (reconfig_sig) {
2650                 condlog(2, "reconfigure (signal)");
2651                 set_config_state(DAEMON_CONFIGURE);
2652         }
2653         if (log_reset_sig) {
2654                 condlog(2, "reset log (signal)");
2655                 if (logsink == 1)
2656                         log_thread_reset();
2657         }
2658         reconfig_sig = 0;
2659         log_reset_sig = 0;
2660 }
2661
2662 static void
2663 sighup(__attribute__((unused)) int sig)
2664 {
2665         reconfig_sig = 1;
2666 }
2667
2668 static void
2669 sigend(__attribute__((unused)) int sig)
2670 {
2671         exit_sig = 1;
2672 }
2673
2674 static void
2675 sigusr1(__attribute__((unused)) int sig)
2676 {
2677         log_reset_sig = 1;
2678 }
2679
2680 static void
2681 sigusr2(__attribute__((unused)) int sig)
2682 {
2683         condlog(3, "SIGUSR2 received");
2684 }
2685
2686 static void
2687 signal_init(void)
2688 {
2689         sigset_t set;
2690
2691         /* block all signals */
2692         sigfillset(&set);
2693         /* SIGPIPE occurs if logging fails */
2694         sigdelset(&set, SIGPIPE);
2695         pthread_sigmask(SIG_SETMASK, &set, NULL);
2696
2697         /* Other signals will be unblocked in the uxlsnr thread */
2698         signal_set(SIGHUP, sighup);
2699         signal_set(SIGUSR1, sigusr1);
2700         signal_set(SIGUSR2, sigusr2);
2701         signal_set(SIGINT, sigend);
2702         signal_set(SIGTERM, sigend);
2703         signal_set(SIGPIPE, sigend);
2704 }
2705
2706 static void
2707 setscheduler (void)
2708 {
2709         int res;
2710         static struct sched_param sched_param = {
2711                 .sched_priority = 99
2712         };
2713
2714         res = sched_setscheduler (0, SCHED_RR, &sched_param);
2715
2716         if (res == -1)
2717                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
2718         return;
2719 }
2720
2721 static void
2722 set_oom_adj (void)
2723 {
2724 #ifdef OOM_SCORE_ADJ_MIN
2725         int retry = 1;
2726         char *file = "/proc/self/oom_score_adj";
2727         int score = OOM_SCORE_ADJ_MIN;
2728 #else
2729         int retry = 0;
2730         char *file = "/proc/self/oom_adj";
2731         int score = OOM_ADJUST_MIN;
2732 #endif
2733         FILE *fp;
2734         struct stat st;
2735         char *envp;
2736
2737         envp = getenv("OOMScoreAdjust");
2738         if (envp) {
2739                 condlog(3, "Using systemd provided OOMScoreAdjust");
2740                 return;
2741         }
2742         do {
2743                 if (stat(file, &st) == 0){
2744                         fp = fopen(file, "w");
2745                         if (!fp) {
2746                                 condlog(0, "couldn't fopen %s : %s", file,
2747                                         strerror(errno));
2748                                 return;
2749                         }
2750                         fprintf(fp, "%i", score);
2751                         fclose(fp);
2752                         return;
2753                 }
2754                 if (errno != ENOENT) {
2755                         condlog(0, "couldn't stat %s : %s", file,
2756                                 strerror(errno));
2757                         return;
2758                 }
2759 #ifdef OOM_ADJUST_MIN
2760                 file = "/proc/self/oom_adj";
2761                 score = OOM_ADJUST_MIN;
2762 #else
2763                 retry = 0;
2764 #endif
2765         } while (retry--);
2766         condlog(0, "couldn't adjust oom score");
2767 }
2768
2769 static int
2770 child (__attribute__((unused)) void *param)
2771 {
2772         pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr;
2773         pthread_attr_t log_attr, misc_attr, uevent_attr;
2774         struct vectors * vecs;
2775         struct multipath * mpp;
2776         int i;
2777 #ifdef USE_SYSTEMD
2778         unsigned long checkint;
2779         int startup_done = 0;
2780 #endif
2781         int rc;
2782         int pid_fd = -1;
2783         struct config *conf;
2784         char *envp;
2785         int queue_without_daemon;
2786         enum daemon_status state;
2787
2788         mlockall(MCL_CURRENT | MCL_FUTURE);
2789         signal_init();
2790         rcu_init();
2791
2792         setup_thread_attr(&misc_attr, 64 * 1024, 0);
2793         setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 0);
2794         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
2795         setup_thread_attr(&io_err_stat_attr, 32 * 1024, 0);
2796
2797         if (logsink == 1) {
2798                 setup_thread_attr(&log_attr, 64 * 1024, 0);
2799                 log_thread_start(&log_attr);
2800                 pthread_attr_destroy(&log_attr);
2801         }
2802         pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
2803         if (pid_fd < 0) {
2804                 condlog(1, "failed to create pidfile");
2805                 if (logsink == 1)
2806                         log_thread_stop();
2807                 exit(1);
2808         }
2809
2810         post_config_state(DAEMON_START);
2811
2812         condlog(2, "--------start up--------");
2813         condlog(2, "read " DEFAULT_CONFIGFILE);
2814
2815         conf = load_config(DEFAULT_CONFIGFILE);
2816         if (!conf)
2817                 goto failed;
2818
2819         if (verbosity)
2820                 conf->verbosity = verbosity;
2821         if (bindings_read_only)
2822                 conf->bindings_read_only = bindings_read_only;
2823         uxsock_timeout = conf->uxsock_timeout;
2824         rcu_assign_pointer(multipath_conf, conf);
2825         if (init_checkers(conf->multipath_dir)) {
2826                 condlog(0, "failed to initialize checkers");
2827                 goto failed;
2828         }
2829         if (init_prio(conf->multipath_dir)) {
2830                 condlog(0, "failed to initialize prioritizers");
2831                 goto failed;
2832         }
2833         /* Failing this is non-fatal */
2834
2835         init_foreign(conf->multipath_dir, conf->enable_foreign);
2836
2837         if (poll_dmevents)
2838                 poll_dmevents = dmevent_poll_supported();
2839         setlogmask(LOG_UPTO(conf->verbosity + 3));
2840
2841         envp = getenv("LimitNOFILE");
2842
2843         if (envp)
2844                 condlog(2,"Using systemd provided open fds limit of %s", envp);
2845         else
2846                 set_max_fds(conf->max_fds);
2847
2848         vecs = gvecs = init_vecs();
2849         if (!vecs)
2850                 goto failed;
2851
2852         setscheduler();
2853         set_oom_adj();
2854
2855 #ifdef USE_SYSTEMD
2856         envp = getenv("WATCHDOG_USEC");
2857         if (envp && sscanf(envp, "%lu", &checkint) == 1) {
2858                 /* Value is in microseconds */
2859                 conf->max_checkint = checkint / 1000000;
2860                 /* Rescale checkint */
2861                 if (conf->checkint > conf->max_checkint)
2862                         conf->checkint = conf->max_checkint;
2863                 else
2864                         conf->checkint = conf->max_checkint / 4;
2865                 condlog(3, "enabling watchdog, interval %d max %d",
2866                         conf->checkint, conf->max_checkint);
2867                 use_watchdog = conf->checkint;
2868         }
2869 #endif
2870         /*
2871          * Startup done, invalidate configuration
2872          */
2873         conf = NULL;
2874
2875         pthread_cleanup_push(config_cleanup, NULL);
2876         pthread_mutex_lock(&config_lock);
2877
2878         __post_config_state(DAEMON_IDLE);
2879         rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
2880         if (!rc) {
2881                 /* Wait for uxlsnr startup */
2882                 while (running_state == DAEMON_IDLE)
2883                         pthread_cond_wait(&config_cond, &config_lock);
2884                 state = running_state;
2885         }
2886         pthread_cleanup_pop(1);
2887
2888         if (rc) {
2889                 condlog(0, "failed to create cli listener: %d", rc);
2890                 goto failed;
2891         }
2892         else if (state != DAEMON_CONFIGURE) {
2893                 condlog(0, "cli listener failed to start");
2894                 goto failed;
2895         }
2896
2897         if (poll_dmevents) {
2898                 if (init_dmevent_waiter(vecs)) {
2899                         condlog(0, "failed to allocate dmevents waiter info");
2900                         goto failed;
2901                 }
2902                 if ((rc = pthread_create(&dmevent_thr, &misc_attr,
2903                                          wait_dmevents, NULL))) {
2904                         condlog(0, "failed to create dmevent waiter thread: %d",
2905                                 rc);
2906                         goto failed;
2907                 }
2908         }
2909
2910         /*
2911          * Start uevent listener early to catch events
2912          */
2913         if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
2914                 condlog(0, "failed to create uevent thread: %d", rc);
2915                 goto failed;
2916         }
2917         pthread_attr_destroy(&uevent_attr);
2918
2919         /*
2920          * start threads
2921          */
2922         if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
2923                 condlog(0,"failed to create checker loop thread: %d", rc);
2924                 goto failed;
2925         }
2926         if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
2927                 condlog(0, "failed to create uevent dispatcher: %d", rc);
2928                 goto failed;
2929         }
2930         pthread_attr_destroy(&misc_attr);
2931
2932         while (1) {
2933                 pthread_cleanup_push(config_cleanup, NULL);
2934                 pthread_mutex_lock(&config_lock);
2935                 while (running_state != DAEMON_CONFIGURE &&
2936                        running_state != DAEMON_SHUTDOWN)
2937                         pthread_cond_wait(&config_cond, &config_lock);
2938                 state = running_state;
2939                 pthread_cleanup_pop(1);
2940                 if (state == DAEMON_SHUTDOWN)
2941                         break;
2942                 if (state == DAEMON_CONFIGURE) {
2943                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2944                         lock(&vecs->lock);
2945                         pthread_testcancel();
2946                         if (!need_to_delay_reconfig(vecs)) {
2947                                 reconfigure(vecs);
2948                         } else {
2949                                 conf = get_multipath_config();
2950                                 conf->delayed_reconfig = 1;
2951                                 put_multipath_config(conf);
2952                         }
2953                         lock_cleanup_pop(vecs->lock);
2954                         post_config_state(DAEMON_IDLE);
2955 #ifdef USE_SYSTEMD
2956                         if (!startup_done) {
2957                                 sd_notify(0, "READY=1");
2958                                 startup_done = 1;
2959                         }
2960 #endif
2961                 }
2962         }
2963
2964         lock(&vecs->lock);
2965         conf = get_multipath_config();
2966         queue_without_daemon = conf->queue_without_daemon;
2967         put_multipath_config(conf);
2968         if (queue_without_daemon == QUE_NO_DAEMON_OFF)
2969                 vector_foreach_slot(vecs->mpvec, mpp, i)
2970                         dm_queue_if_no_path(mpp->alias, 0);
2971         remove_maps_and_stop_waiters(vecs);
2972         unlock(&vecs->lock);
2973
2974         pthread_cancel(check_thr);
2975         pthread_cancel(uevent_thr);
2976         pthread_cancel(uxlsnr_thr);
2977         pthread_cancel(uevq_thr);
2978         if (poll_dmevents)
2979                 pthread_cancel(dmevent_thr);
2980
2981         pthread_join(check_thr, NULL);
2982         pthread_join(uevent_thr, NULL);
2983         pthread_join(uxlsnr_thr, NULL);
2984         pthread_join(uevq_thr, NULL);
2985         if (poll_dmevents)
2986                 pthread_join(dmevent_thr, NULL);
2987
2988         stop_io_err_stat_thread();
2989
2990         lock(&vecs->lock);
2991         free_pathvec(vecs->pathvec, FREE_PATHS);
2992         vecs->pathvec = NULL;
2993         unlock(&vecs->lock);
2994
2995         pthread_mutex_destroy(&vecs->lock.mutex);
2996         FREE(vecs);
2997         vecs = NULL;
2998
2999         cleanup_foreign();
3000         cleanup_checkers();
3001         cleanup_prio();
3002         if (poll_dmevents)
3003                 cleanup_dmevent_waiter();
3004
3005         dm_lib_release();
3006         dm_lib_exit();
3007
3008         /* We're done here */
3009         condlog(3, "unlink pidfile");
3010         unlink(DEFAULT_PIDFILE);
3011
3012         condlog(2, "--------shut down-------");
3013
3014         if (logsink == 1)
3015                 log_thread_stop();
3016
3017         /*
3018          * Freeing config must be done after condlog() and dm_lib_exit(),
3019          * because logging functions like dlog() and dm_write_log()
3020          * reference the config.
3021          */
3022         conf = rcu_dereference(multipath_conf);
3023         rcu_assign_pointer(multipath_conf, NULL);
3024         call_rcu(&conf->rcu, rcu_free_config);
3025         udev_unref(udev);
3026         udev = NULL;
3027         pthread_attr_destroy(&waiter_attr);
3028         pthread_attr_destroy(&io_err_stat_attr);
3029 #ifdef _DEBUG_
3030         dbg_free_final(NULL);
3031 #endif
3032
3033 #ifdef USE_SYSTEMD
3034         sd_notify(0, "ERRNO=0");
3035 #endif
3036         exit(0);
3037
3038 failed:
3039 #ifdef USE_SYSTEMD
3040         sd_notify(0, "ERRNO=1");
3041 #endif
3042         if (pid_fd >= 0)
3043                 close(pid_fd);
3044         exit(1);
3045 }
3046
3047 static int
3048 daemonize(void)
3049 {
3050         int pid;
3051         int dev_null_fd;
3052
3053         if( (pid = fork()) < 0){
3054                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
3055                 return -1;
3056         }
3057         else if (pid != 0)
3058                 return pid;
3059
3060         setsid();
3061
3062         if ( (pid = fork()) < 0)
3063                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
3064         else if (pid != 0)
3065                 _exit(0);
3066
3067         if (chdir("/") < 0)
3068                 fprintf(stderr, "cannot chdir to '/', continuing\n");
3069
3070         dev_null_fd = open("/dev/null", O_RDWR);
3071         if (dev_null_fd < 0){
3072                 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
3073                         strerror(errno));
3074                 _exit(0);
3075         }
3076
3077         close(STDIN_FILENO);
3078         if (dup(dev_null_fd) < 0) {
3079                 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
3080                         strerror(errno));
3081                 _exit(0);
3082         }
3083         close(STDOUT_FILENO);
3084         if (dup(dev_null_fd) < 0) {
3085                 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
3086                         strerror(errno));
3087                 _exit(0);
3088         }
3089         close(STDERR_FILENO);
3090         if (dup(dev_null_fd) < 0) {
3091                 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
3092                         strerror(errno));
3093                 _exit(0);
3094         }
3095         close(dev_null_fd);
3096         daemon_pid = getpid();
3097         return 0;
3098 }
3099
3100 int
3101 main (int argc, char *argv[])
3102 {
3103         extern char *optarg;
3104         extern int optind;
3105         int arg;
3106         int err;
3107         int foreground = 0;
3108         struct config *conf;
3109
3110         ANNOTATE_BENIGN_RACE_SIZED(&multipath_conf, sizeof(multipath_conf),
3111                                    "Manipulated through RCU");
3112         ANNOTATE_BENIGN_RACE_SIZED(&uxsock_timeout, sizeof(uxsock_timeout),
3113                 "Suppress complaints about this scalar variable");
3114
3115         logsink = 1;
3116
3117         if (getuid() != 0) {
3118                 fprintf(stderr, "need to be root\n");
3119                 exit(1);
3120         }
3121
3122         /* make sure we don't lock any path */
3123         if (chdir("/") < 0)
3124                 fprintf(stderr, "can't chdir to root directory : %s\n",
3125                         strerror(errno));
3126         umask(umask(077) | 022);
3127
3128         pthread_cond_init_mono(&config_cond);
3129
3130         udev = udev_new();
3131         libmp_udev_set_sync_support(0);
3132
3133         while ((arg = getopt(argc, argv, ":dsv:k::Bniw")) != EOF ) {
3134                 switch(arg) {
3135                 case 'd':
3136                         foreground = 1;
3137                         if (logsink > 0)
3138                                 logsink = 0;
3139                         //debug=1; /* ### comment me out ### */
3140                         break;
3141                 case 'v':
3142                         if (sizeof(optarg) > sizeof(char *) ||
3143                             !isdigit(optarg[0]))
3144                                 exit(1);
3145
3146                         verbosity = atoi(optarg);
3147                         break;
3148                 case 's':
3149                         logsink = -1;
3150                         break;
3151                 case 'k':
3152                         logsink = 0;
3153                         conf = load_config(DEFAULT_CONFIGFILE);
3154                         if (!conf)
3155                                 exit(1);
3156                         if (verbosity)
3157                                 conf->verbosity = verbosity;
3158                         uxsock_timeout = conf->uxsock_timeout;
3159                         err = uxclnt(optarg, uxsock_timeout + 100);
3160                         free_config(conf);
3161                         return err;
3162                 case 'B':
3163                         bindings_read_only = 1;
3164                         break;
3165                 case 'n':
3166                         condlog(0, "WARNING: ignoring deprecated option -n, use 'ignore_wwids = no' instead");
3167                         break;
3168                 case 'w':
3169                         poll_dmevents = 0;
3170                         break;
3171                 default:
3172                         fprintf(stderr, "Invalid argument '-%c'\n",
3173                                 optopt);
3174                         exit(1);
3175                 }
3176         }
3177         if (optind < argc) {
3178                 char cmd[CMDSIZE];
3179                 char * s = cmd;
3180                 char * c = s;
3181
3182                 logsink = 0;
3183                 conf = load_config(DEFAULT_CONFIGFILE);
3184                 if (!conf)
3185                         exit(1);
3186                 if (verbosity)
3187                         conf->verbosity = verbosity;
3188                 uxsock_timeout = conf->uxsock_timeout;
3189                 memset(cmd, 0x0, CMDSIZE);
3190                 while (optind < argc) {
3191                         if (strchr(argv[optind], ' '))
3192                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
3193                         else
3194                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
3195                         optind++;
3196                 }
3197                 c += snprintf(c, s + CMDSIZE - c, "\n");
3198                 err = uxclnt(s, uxsock_timeout + 100);
3199                 free_config(conf);
3200                 return err;
3201         }
3202
3203         if (foreground) {
3204                 if (!isatty(fileno(stdout)))
3205                         setbuf(stdout, NULL);
3206                 err = 0;
3207                 daemon_pid = getpid();
3208         } else
3209                 err = daemonize();
3210
3211         if (err < 0)
3212                 /* error */
3213                 exit(1);
3214