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