libmutipath: deprecate delay_*_checks
[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         /*
2126          * don't reinstate failed path, if its in stand-by
2127          * and if target supports only implicit tpgs mode.
2128          * this will prevent unnecessary i/o by dm on stand-by
2129          * paths if there are no other active paths in map.
2130          */
2131         disable_reinstate = (newstate == PATH_GHOST &&
2132                              pp->mpp->nr_active == 0 &&
2133                              path_get_tpgs(pp) == TPGS_IMPLICIT) ? 1 : 0;
2134
2135         pp->chkrstate = newstate;
2136         if (newstate != pp->state) {
2137                 int oldstate = pp->state;
2138                 pp->state = newstate;
2139
2140                 LOG_MSG(1, verbosity, pp);
2141
2142                 /*
2143                  * upon state change, reset the checkint
2144                  * to the shortest delay
2145                  */
2146                 conf = get_multipath_config();
2147                 pp->checkint = conf->checkint;
2148                 put_multipath_config(conf);
2149
2150                 if (newstate != PATH_UP && newstate != PATH_GHOST) {
2151                         /*
2152                          * proactively fail path in the DM
2153                          */
2154                         if (oldstate == PATH_UP ||
2155                             oldstate == PATH_GHOST)
2156                                 fail_path(pp, 1);
2157                         else
2158                                 fail_path(pp, 0);
2159
2160                         /*
2161                          * cancel scheduled failback
2162                          */
2163                         pp->mpp->failback_tick = 0;
2164
2165                         pp->mpp->stat_path_failures++;
2166                         return 1;
2167                 }
2168
2169                 if (newstate == PATH_UP || newstate == PATH_GHOST) {
2170                         if (pp->mpp->prflag) {
2171                                 /*
2172                                  * Check Persistent Reservation.
2173                                  */
2174                                 condlog(2, "%s: checking persistent "
2175                                         "reservation registration", pp->dev);
2176                                 mpath_pr_event_handle(pp);
2177                         }
2178                 }
2179
2180                 /*
2181                  * reinstate this path
2182                  */
2183                 if (oldstate != PATH_UP &&
2184                     oldstate != PATH_GHOST)
2185                         add_active = 1;
2186                 else
2187                         add_active = 0;
2188                 if (!disable_reinstate && reinstate_path(pp, add_active)) {
2189                         condlog(3, "%s: reload map", pp->dev);
2190                         ev_add_path(pp, vecs, 1);
2191                         pp->tick = 1;
2192                         return 0;
2193                 }
2194                 new_path_up = 1;
2195
2196                 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
2197                         chkr_new_path_up = 1;
2198
2199                 /*
2200                  * if at least one path is up in a group, and
2201                  * the group is disabled, re-enable it
2202                  */
2203                 if (newstate == PATH_UP)
2204                         enable_group(pp);
2205         }
2206         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
2207                 if ((pp->dmstate == PSTATE_FAILED ||
2208                     pp->dmstate == PSTATE_UNDEF) &&
2209                     !disable_reinstate) {
2210                         /* Clear IO errors */
2211                         if (reinstate_path(pp, 0)) {
2212                                 condlog(3, "%s: reload map", pp->dev);
2213                                 ev_add_path(pp, vecs, 1);
2214                                 pp->tick = 1;
2215                                 return 0;
2216                         }
2217                 } else {
2218                         LOG_MSG(4, verbosity, pp);
2219                         if (pp->checkint != max_checkint) {
2220                                 /*
2221                                  * double the next check delay.
2222                                  * max at conf->max_checkint
2223                                  */
2224                                 if (pp->checkint < (max_checkint / 2))
2225                                         pp->checkint = 2 * pp->checkint;
2226                                 else
2227                                         pp->checkint = max_checkint;
2228
2229                                 condlog(4, "%s: delay next check %is",
2230                                         pp->dev_t, pp->checkint);
2231                         }
2232                         pp->tick = pp->checkint;
2233                 }
2234         }
2235         else if (newstate != PATH_UP && newstate != PATH_GHOST) {
2236                 if (pp->dmstate == PSTATE_ACTIVE ||
2237                     pp->dmstate == PSTATE_UNDEF)
2238                         fail_path(pp, 0);
2239                 if (newstate == PATH_DOWN) {
2240                         int log_checker_err;
2241
2242                         conf = get_multipath_config();
2243                         log_checker_err = conf->log_checker_err;
2244                         put_multipath_config(conf);
2245                         if (log_checker_err == LOG_CHKR_ERR_ONCE)
2246                                 LOG_MSG(3, verbosity, pp);
2247                         else
2248                                 LOG_MSG(2, verbosity, pp);
2249                 }
2250         }
2251
2252         pp->state = newstate;
2253
2254         if (pp->mpp->wait_for_udev)
2255                 return 1;
2256         /*
2257          * path prio refreshing
2258          */
2259         condlog(4, "path prio refresh");
2260
2261         if (update_prio(pp, new_path_up) &&
2262             (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
2263              pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
2264                 update_path_groups(pp->mpp, vecs, !new_path_up);
2265         else if (need_switch_pathgroup(pp->mpp, 0)) {
2266                 if (pp->mpp->pgfailback > 0 &&
2267                     (new_path_up || pp->mpp->failback_tick <= 0))
2268                         pp->mpp->failback_tick =
2269                                 pp->mpp->pgfailback + 1;
2270                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
2271                          (chkr_new_path_up && followover_should_failback(pp)))
2272                         switch_pathgroup(pp->mpp);
2273         }
2274         return 1;
2275 }
2276
2277 static void *
2278 checkerloop (void *ap)
2279 {
2280         struct vectors *vecs;
2281         struct path *pp;
2282         int count = 0;
2283         unsigned int i;
2284         struct timespec last_time;
2285         struct config *conf;
2286         int foreign_tick = 0;
2287
2288         pthread_cleanup_push(rcu_unregister, NULL);
2289         rcu_register_thread();
2290         mlockall(MCL_CURRENT | MCL_FUTURE);
2291         vecs = (struct vectors *)ap;
2292         condlog(2, "path checkers start up");
2293
2294         /* Tweak start time for initial path check */
2295         if (clock_gettime(CLOCK_MONOTONIC, &last_time) != 0)
2296                 last_time.tv_sec = 0;
2297         else
2298                 last_time.tv_sec -= 1;
2299
2300         while (1) {
2301                 struct timespec diff_time, start_time, end_time;
2302                 int num_paths = 0, ticks = 0, strict_timing, rc = 0;
2303
2304                 if (clock_gettime(CLOCK_MONOTONIC, &start_time) != 0)
2305                         start_time.tv_sec = 0;
2306                 if (start_time.tv_sec && last_time.tv_sec) {
2307                         timespecsub(&start_time, &last_time, &diff_time);
2308                         condlog(4, "tick (%lu.%06lu secs)",
2309                                 diff_time.tv_sec, diff_time.tv_nsec / 1000);
2310                         last_time = start_time;
2311                         ticks = diff_time.tv_sec;
2312                 } else {
2313                         ticks = 1;
2314                         condlog(4, "tick (%d ticks)", ticks);
2315                 }
2316 #ifdef USE_SYSTEMD
2317                 if (use_watchdog)
2318                         sd_notify(0, "WATCHDOG=1");
2319 #endif
2320                 rc = set_config_state(DAEMON_RUNNING);
2321                 if (rc == ETIMEDOUT) {
2322                         condlog(4, "timeout waiting for DAEMON_IDLE");
2323                         continue;
2324                 } else if (rc == EINVAL)
2325                         /* daemon shutdown */
2326                         break;
2327
2328                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2329                 lock(&vecs->lock);
2330                 pthread_testcancel();
2331                 vector_foreach_slot (vecs->pathvec, pp, i) {
2332                         rc = check_path(vecs, pp, ticks);
2333                         if (rc < 0) {
2334                                 vector_del_slot(vecs->pathvec, i);
2335                                 free_path(pp);
2336                                 i--;
2337                         } else
2338                                 num_paths += rc;
2339                 }
2340                 lock_cleanup_pop(vecs->lock);
2341
2342                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2343                 lock(&vecs->lock);
2344                 pthread_testcancel();
2345                 defered_failback_tick(vecs->mpvec);
2346                 retry_count_tick(vecs->mpvec);
2347                 missing_uev_wait_tick(vecs);
2348                 ghost_delay_tick(vecs);
2349                 lock_cleanup_pop(vecs->lock);
2350
2351                 if (count)
2352                         count--;
2353                 else {
2354                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2355                         lock(&vecs->lock);
2356                         pthread_testcancel();
2357                         condlog(4, "map garbage collection");
2358                         mpvec_garbage_collector(vecs);
2359                         count = MAPGCINT;
2360                         lock_cleanup_pop(vecs->lock);
2361                 }
2362
2363                 diff_time.tv_nsec = 0;
2364                 if (start_time.tv_sec &&
2365                     clock_gettime(CLOCK_MONOTONIC, &end_time) == 0) {
2366                         timespecsub(&end_time, &start_time, &diff_time);
2367                         if (num_paths) {
2368                                 unsigned int max_checkint;
2369
2370                                 condlog(4, "checked %d path%s in %lu.%06lu secs",
2371                                         num_paths, num_paths > 1 ? "s" : "",
2372                                         diff_time.tv_sec,
2373                                         diff_time.tv_nsec / 1000);
2374                                 conf = get_multipath_config();
2375                                 max_checkint = conf->max_checkint;
2376                                 put_multipath_config(conf);
2377                                 if (diff_time.tv_sec > max_checkint)
2378                                         condlog(1, "path checkers took longer "
2379                                                 "than %lu seconds, consider "
2380                                                 "increasing max_polling_interval",
2381                                                 diff_time.tv_sec);
2382                         }
2383                 }
2384
2385                 if (foreign_tick == 0) {
2386                         conf = get_multipath_config();
2387                         foreign_tick = conf->max_checkint;
2388                         put_multipath_config(conf);
2389                 }
2390                 if (--foreign_tick == 0)
2391                         check_foreign();
2392
2393                 post_config_state(DAEMON_IDLE);
2394                 conf = get_multipath_config();
2395                 strict_timing = conf->strict_timing;
2396                 put_multipath_config(conf);
2397                 if (!strict_timing)
2398                         sleep(1);
2399                 else {
2400                         if (diff_time.tv_nsec) {
2401                                 diff_time.tv_sec = 0;
2402                                 diff_time.tv_nsec =
2403                                      1000UL * 1000 * 1000 - diff_time.tv_nsec;
2404                         } else
2405                                 diff_time.tv_sec = 1;
2406
2407                         condlog(3, "waiting for %lu.%06lu secs",
2408                                 diff_time.tv_sec,
2409                                 diff_time.tv_nsec / 1000);
2410                         if (nanosleep(&diff_time, NULL) != 0) {
2411                                 condlog(3, "nanosleep failed with error %d",
2412                                         errno);
2413                                 conf = get_multipath_config();
2414                                 conf->strict_timing = 0;
2415                                 put_multipath_config(conf);
2416                                 break;
2417                         }
2418                 }
2419         }
2420         pthread_cleanup_pop(1);
2421         return NULL;
2422 }
2423
2424 int
2425 configure (struct vectors * vecs)
2426 {
2427         struct multipath * mpp;
2428         struct path * pp;
2429         vector mpvec;
2430         int i, ret;
2431         struct config *conf;
2432         static int force_reload = FORCE_RELOAD_WEAK;
2433
2434         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
2435                 condlog(0, "couldn't allocate path vec in configure");
2436                 return 1;
2437         }
2438
2439         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc())) {
2440                 condlog(0, "couldn't allocate multipath vec in configure");
2441                 return 1;
2442         }
2443
2444         if (!(mpvec = vector_alloc())) {
2445                 condlog(0, "couldn't allocate new maps vec in configure");
2446                 return 1;
2447         }
2448
2449         /*
2450          * probe for current path (from sysfs) and map (from dm) sets
2451          */
2452         ret = path_discovery(vecs->pathvec, DI_ALL);
2453         if (ret < 0) {
2454                 condlog(0, "configure failed at path discovery");
2455                 goto fail;
2456         }
2457
2458         conf = get_multipath_config();
2459         pthread_cleanup_push(put_multipath_config, conf);
2460         vector_foreach_slot (vecs->pathvec, pp, i){
2461                 if (filter_path(conf, pp) > 0){
2462                         vector_del_slot(vecs->pathvec, i);
2463                         free_path(pp);
2464                         i--;
2465                 }
2466         }
2467         pthread_cleanup_pop(1);
2468
2469         if (map_discovery(vecs)) {
2470                 condlog(0, "configure failed at map discovery");
2471                 goto fail;
2472         }
2473
2474         /*
2475          * create new set of maps & push changed ones into dm
2476          * In the first call, use FORCE_RELOAD_WEAK to avoid making
2477          * superfluous ACT_RELOAD ioctls. Later calls are done
2478          * with FORCE_RELOAD_YES.
2479          */
2480         ret = coalesce_paths(vecs, mpvec, NULL, force_reload, CMD_NONE);
2481         if (force_reload == FORCE_RELOAD_WEAK)
2482                 force_reload = FORCE_RELOAD_YES;
2483         if (ret != CP_OK) {
2484                 condlog(0, "configure failed while coalescing paths");
2485                 goto fail;
2486         }
2487
2488         /*
2489          * may need to remove some maps which are no longer relevant
2490          * e.g., due to blacklist changes in conf file
2491          */
2492         if (coalesce_maps(vecs, mpvec)) {
2493                 condlog(0, "configure failed while coalescing maps");
2494                 goto fail;
2495         }
2496
2497         dm_lib_release();
2498
2499         sync_maps_state(mpvec);
2500         vector_foreach_slot(mpvec, mpp, i){
2501                 if (remember_wwid(mpp->wwid) == 1)
2502                         trigger_paths_udev_change(mpp, true);
2503                 update_map_pr(mpp);
2504         }
2505
2506         /*
2507          * purge dm of old maps
2508          */
2509         remove_maps(vecs);
2510
2511         /*
2512          * save new set of maps formed by considering current path state
2513          */
2514         vector_free(vecs->mpvec);
2515         vecs->mpvec = mpvec;
2516
2517         /*
2518          * start dm event waiter threads for these new maps
2519          */
2520         vector_foreach_slot(vecs->mpvec, mpp, i) {
2521                 if (wait_for_events(mpp, vecs)) {
2522                         remove_map(mpp, vecs, 1);
2523                         i--;
2524                         continue;
2525                 }
2526                 if (setup_multipath(vecs, mpp))
2527                         i--;
2528         }
2529         return 0;
2530
2531 fail:
2532         vector_free(mpvec);
2533         return 1;
2534 }
2535
2536 int
2537 need_to_delay_reconfig(struct vectors * vecs)
2538 {
2539         struct multipath *mpp;
2540         int i;
2541
2542         if (!VECTOR_SIZE(vecs->mpvec))
2543                 return 0;
2544
2545         vector_foreach_slot(vecs->mpvec, mpp, i) {
2546                 if (mpp->wait_for_udev)
2547                         return 1;
2548         }
2549         return 0;
2550 }
2551
2552 void rcu_free_config(struct rcu_head *head)
2553 {
2554         struct config *conf = container_of(head, struct config, rcu);
2555
2556         free_config(conf);
2557 }
2558
2559 int
2560 reconfigure (struct vectors * vecs)
2561 {
2562         struct config * old, *conf;
2563
2564         conf = load_config(DEFAULT_CONFIGFILE);
2565         if (!conf)
2566                 return 1;
2567
2568         /*
2569          * free old map and path vectors ... they use old conf state
2570          */
2571         if (VECTOR_SIZE(vecs->mpvec))
2572                 remove_maps_and_stop_waiters(vecs);
2573
2574         free_pathvec(vecs->pathvec, FREE_PATHS);
2575         vecs->pathvec = NULL;
2576         delete_all_foreign();
2577
2578         /* Re-read any timezone changes */
2579         tzset();
2580
2581         dm_tgt_version(conf->version, TGT_MPATH);
2582         if (verbosity)
2583                 conf->verbosity = verbosity;
2584         if (bindings_read_only)
2585                 conf->bindings_read_only = bindings_read_only;
2586         uxsock_timeout = conf->uxsock_timeout;
2587
2588         old = rcu_dereference(multipath_conf);
2589         rcu_assign_pointer(multipath_conf, conf);
2590         call_rcu(&old->rcu, rcu_free_config);
2591
2592         configure(vecs);
2593
2594
2595         return 0;
2596 }
2597
2598 static struct vectors *
2599 init_vecs (void)
2600 {
2601         struct vectors * vecs;
2602
2603         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
2604
2605         if (!vecs)
2606                 return NULL;
2607
2608         pthread_mutex_init(&vecs->lock.mutex, NULL);
2609
2610         return vecs;
2611 }
2612
2613 static void *
2614 signal_set(int signo, void (*func) (int))
2615 {
2616         int r;
2617         struct sigaction sig;
2618         struct sigaction osig;
2619
2620         sig.sa_handler = func;
2621         sigemptyset(&sig.sa_mask);
2622         sig.sa_flags = 0;
2623
2624         r = sigaction(signo, &sig, &osig);
2625
2626         if (r < 0)
2627                 return (SIG_ERR);
2628         else
2629                 return (osig.sa_handler);
2630 }
2631
2632 void
2633 handle_signals(bool nonfatal)
2634 {
2635         if (exit_sig) {
2636                 condlog(2, "exit (signal)");
2637                 exit_sig = 0;
2638                 exit_daemon();
2639         }
2640         if (!nonfatal)
2641                 return;
2642         if (reconfig_sig) {
2643                 condlog(2, "reconfigure (signal)");
2644                 set_config_state(DAEMON_CONFIGURE);
2645         }
2646         if (log_reset_sig) {
2647                 condlog(2, "reset log (signal)");
2648                 if (logsink == 1)
2649                         log_thread_reset();
2650         }
2651         reconfig_sig = 0;
2652         log_reset_sig = 0;
2653 }
2654
2655 static void
2656 sighup (int sig)
2657 {
2658         reconfig_sig = 1;
2659 }
2660
2661 static void
2662 sigend (int sig)
2663 {
2664         exit_sig = 1;
2665 }
2666
2667 static void
2668 sigusr1 (int sig)
2669 {
2670         log_reset_sig = 1;
2671 }
2672
2673 static void
2674 sigusr2 (int sig)
2675 {
2676         condlog(3, "SIGUSR2 received");
2677 }
2678
2679 static void
2680 signal_init(void)
2681 {
2682         sigset_t set;
2683
2684         /* block all signals */
2685         sigfillset(&set);
2686         /* SIGPIPE occurs if logging fails */
2687         sigdelset(&set, SIGPIPE);
2688         pthread_sigmask(SIG_SETMASK, &set, NULL);
2689
2690         /* Other signals will be unblocked in the uxlsnr thread */
2691         signal_set(SIGHUP, sighup);
2692         signal_set(SIGUSR1, sigusr1);
2693         signal_set(SIGUSR2, sigusr2);
2694         signal_set(SIGINT, sigend);
2695         signal_set(SIGTERM, sigend);
2696         signal_set(SIGPIPE, sigend);
2697 }
2698
2699 static void
2700 setscheduler (void)
2701 {
2702         int res;
2703         static struct sched_param sched_param = {
2704                 .sched_priority = 99
2705         };
2706
2707         res = sched_setscheduler (0, SCHED_RR, &sched_param);
2708
2709         if (res == -1)
2710                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
2711         return;
2712 }
2713
2714 static void
2715 set_oom_adj (void)
2716 {
2717 #ifdef OOM_SCORE_ADJ_MIN
2718         int retry = 1;
2719         char *file = "/proc/self/oom_score_adj";
2720         int score = OOM_SCORE_ADJ_MIN;
2721 #else
2722         int retry = 0;
2723         char *file = "/proc/self/oom_adj";
2724         int score = OOM_ADJUST_MIN;
2725 #endif
2726         FILE *fp;
2727         struct stat st;
2728         char *envp;
2729
2730         envp = getenv("OOMScoreAdjust");
2731         if (envp) {
2732                 condlog(3, "Using systemd provided OOMScoreAdjust");
2733                 return;
2734         }
2735         do {
2736                 if (stat(file, &st) == 0){
2737                         fp = fopen(file, "w");
2738                         if (!fp) {
2739                                 condlog(0, "couldn't fopen %s : %s", file,
2740                                         strerror(errno));
2741                                 return;
2742                         }
2743                         fprintf(fp, "%i", score);
2744                         fclose(fp);
2745                         return;
2746                 }
2747                 if (errno != ENOENT) {
2748                         condlog(0, "couldn't stat %s : %s", file,
2749                                 strerror(errno));
2750                         return;
2751                 }
2752 #ifdef OOM_ADJUST_MIN
2753                 file = "/proc/self/oom_adj";
2754                 score = OOM_ADJUST_MIN;
2755 #else
2756                 retry = 0;
2757 #endif
2758         } while (retry--);
2759         condlog(0, "couldn't adjust oom score");
2760 }
2761
2762 static int
2763 child (void * param)
2764 {
2765         pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr;
2766         pthread_attr_t log_attr, misc_attr, uevent_attr;
2767         struct vectors * vecs;
2768         struct multipath * mpp;
2769         int i;
2770 #ifdef USE_SYSTEMD
2771         unsigned long checkint;
2772         int startup_done = 0;
2773 #endif
2774         int rc;
2775         int pid_fd = -1;
2776         struct config *conf;
2777         char *envp;
2778         int queue_without_daemon;
2779         enum daemon_status state;
2780
2781         mlockall(MCL_CURRENT | MCL_FUTURE);
2782         signal_init();
2783         rcu_init();
2784
2785         setup_thread_attr(&misc_attr, 64 * 1024, 0);
2786         setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 0);
2787         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
2788         setup_thread_attr(&io_err_stat_attr, 32 * 1024, 0);
2789
2790         if (logsink == 1) {
2791                 setup_thread_attr(&log_attr, 64 * 1024, 0);
2792                 log_thread_start(&log_attr);
2793                 pthread_attr_destroy(&log_attr);
2794         }
2795         pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
2796         if (pid_fd < 0) {
2797                 condlog(1, "failed to create pidfile");
2798                 if (logsink == 1)
2799                         log_thread_stop();
2800                 exit(1);
2801         }
2802
2803         post_config_state(DAEMON_START);
2804
2805         condlog(2, "--------start up--------");
2806         condlog(2, "read " DEFAULT_CONFIGFILE);
2807
2808         conf = load_config(DEFAULT_CONFIGFILE);
2809         if (!conf)
2810                 goto failed;
2811
2812         if (verbosity)
2813                 conf->verbosity = verbosity;
2814         if (bindings_read_only)
2815                 conf->bindings_read_only = bindings_read_only;
2816         uxsock_timeout = conf->uxsock_timeout;
2817         rcu_assign_pointer(multipath_conf, conf);
2818         if (init_checkers(conf->multipath_dir)) {
2819                 condlog(0, "failed to initialize checkers");
2820                 goto failed;
2821         }
2822         if (init_prio(conf->multipath_dir)) {
2823                 condlog(0, "failed to initialize prioritizers");
2824                 goto failed;
2825         }
2826         /* Failing this is non-fatal */
2827
2828         init_foreign(conf->multipath_dir);
2829
2830         if (poll_dmevents)
2831                 poll_dmevents = dmevent_poll_supported();
2832         setlogmask(LOG_UPTO(conf->verbosity + 3));
2833
2834         envp = getenv("LimitNOFILE");
2835
2836         if (envp)
2837                 condlog(2,"Using systemd provided open fds limit of %s", envp);
2838         else
2839                 set_max_fds(conf->max_fds);
2840
2841         vecs = gvecs = init_vecs();
2842         if (!vecs)
2843                 goto failed;
2844
2845         setscheduler();
2846         set_oom_adj();
2847
2848 #ifdef USE_SYSTEMD
2849         envp = getenv("WATCHDOG_USEC");
2850         if (envp && sscanf(envp, "%lu", &checkint) == 1) {
2851                 /* Value is in microseconds */
2852                 conf->max_checkint = checkint / 1000000;
2853                 /* Rescale checkint */
2854                 if (conf->checkint > conf->max_checkint)
2855                         conf->checkint = conf->max_checkint;
2856                 else
2857                         conf->checkint = conf->max_checkint / 4;
2858                 condlog(3, "enabling watchdog, interval %d max %d",
2859                         conf->checkint, conf->max_checkint);
2860                 use_watchdog = conf->checkint;
2861         }
2862 #endif
2863         /*
2864          * Startup done, invalidate configuration
2865          */
2866         conf = NULL;
2867
2868         pthread_cleanup_push(config_cleanup, NULL);
2869         pthread_mutex_lock(&config_lock);
2870
2871         __post_config_state(DAEMON_IDLE);
2872         rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
2873         if (!rc) {
2874                 /* Wait for uxlsnr startup */
2875                 while (running_state == DAEMON_IDLE)
2876                         pthread_cond_wait(&config_cond, &config_lock);
2877                 state = running_state;
2878         }
2879         pthread_cleanup_pop(1);
2880
2881         if (rc) {
2882                 condlog(0, "failed to create cli listener: %d", rc);
2883                 goto failed;
2884         }
2885         else if (state != DAEMON_CONFIGURE) {
2886                 condlog(0, "cli listener failed to start");
2887                 goto failed;
2888         }
2889
2890         if (poll_dmevents) {
2891                 if (init_dmevent_waiter(vecs)) {
2892                         condlog(0, "failed to allocate dmevents waiter info");
2893                         goto failed;
2894                 }
2895                 if ((rc = pthread_create(&dmevent_thr, &misc_attr,
2896                                          wait_dmevents, NULL))) {
2897                         condlog(0, "failed to create dmevent waiter thread: %d",
2898                                 rc);
2899                         goto failed;
2900                 }
2901         }
2902
2903         /*
2904          * Start uevent listener early to catch events
2905          */
2906         if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
2907                 condlog(0, "failed to create uevent thread: %d", rc);
2908                 goto failed;
2909         }
2910         pthread_attr_destroy(&uevent_attr);
2911
2912         /*
2913          * start threads
2914          */
2915         if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
2916                 condlog(0,"failed to create checker loop thread: %d", rc);
2917                 goto failed;
2918         }
2919         if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
2920                 condlog(0, "failed to create uevent dispatcher: %d", rc);
2921                 goto failed;
2922         }
2923         pthread_attr_destroy(&misc_attr);
2924
2925         while (1) {
2926                 pthread_cleanup_push(config_cleanup, NULL);
2927                 pthread_mutex_lock(&config_lock);
2928                 while (running_state != DAEMON_CONFIGURE &&
2929                        running_state != DAEMON_SHUTDOWN)
2930                         pthread_cond_wait(&config_cond, &config_lock);
2931                 state = running_state;
2932                 pthread_cleanup_pop(1);
2933                 if (state == DAEMON_SHUTDOWN)
2934                         break;
2935                 if (state == DAEMON_CONFIGURE) {
2936                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2937                         lock(&vecs->lock);
2938                         pthread_testcancel();
2939                         if (!need_to_delay_reconfig(vecs)) {
2940                                 reconfigure(vecs);
2941                         } else {
2942                                 conf = get_multipath_config();
2943                                 conf->delayed_reconfig = 1;
2944                                 put_multipath_config(conf);
2945                         }
2946                         lock_cleanup_pop(vecs->lock);
2947                         post_config_state(DAEMON_IDLE);
2948 #ifdef USE_SYSTEMD
2949                         if (!startup_done) {
2950                                 sd_notify(0, "READY=1");
2951                                 startup_done = 1;
2952                         }
2953 #endif
2954                 }
2955         }
2956
2957         lock(&vecs->lock);
2958         conf = get_multipath_config();
2959         queue_without_daemon = conf->queue_without_daemon;
2960         put_multipath_config(conf);
2961         if (queue_without_daemon == QUE_NO_DAEMON_OFF)
2962                 vector_foreach_slot(vecs->mpvec, mpp, i)
2963                         dm_queue_if_no_path(mpp->alias, 0);
2964         remove_maps_and_stop_waiters(vecs);
2965         unlock(&vecs->lock);
2966
2967         pthread_cancel(check_thr);
2968         pthread_cancel(uevent_thr);
2969         pthread_cancel(uxlsnr_thr);
2970         pthread_cancel(uevq_thr);
2971         if (poll_dmevents)
2972                 pthread_cancel(dmevent_thr);
2973
2974         pthread_join(check_thr, NULL);
2975         pthread_join(uevent_thr, NULL);
2976         pthread_join(uxlsnr_thr, NULL);
2977         pthread_join(uevq_thr, NULL);
2978         if (poll_dmevents)
2979                 pthread_join(dmevent_thr, NULL);
2980
2981         stop_io_err_stat_thread();
2982
2983         lock(&vecs->lock);
2984         free_pathvec(vecs->pathvec, FREE_PATHS);
2985         vecs->pathvec = NULL;
2986         unlock(&vecs->lock);
2987
2988         pthread_mutex_destroy(&vecs->lock.mutex);
2989         FREE(vecs);
2990         vecs = NULL;
2991
2992         cleanup_foreign();
2993         cleanup_checkers();
2994         cleanup_prio();
2995         if (poll_dmevents)
2996                 cleanup_dmevent_waiter();
2997
2998         dm_lib_release();
2999         dm_lib_exit();
3000
3001         /* We're done here */
3002         condlog(3, "unlink pidfile");
3003         unlink(DEFAULT_PIDFILE);
3004
3005         condlog(2, "--------shut down-------");
3006
3007         if (logsink == 1)
3008                 log_thread_stop();
3009
3010         /*
3011          * Freeing config must be done after condlog() and dm_lib_exit(),
3012          * because logging functions like dlog() and dm_write_log()
3013          * reference the config.
3014          */
3015         conf = rcu_dereference(multipath_conf);
3016         rcu_assign_pointer(multipath_conf, NULL);
3017         call_rcu(&conf->rcu, rcu_free_config);
3018         udev_unref(udev);
3019         udev = NULL;
3020         pthread_attr_destroy(&waiter_attr);
3021         pthread_attr_destroy(&io_err_stat_attr);
3022 #ifdef _DEBUG_
3023         dbg_free_final(NULL);
3024 #endif
3025
3026 #ifdef USE_SYSTEMD
3027         sd_notify(0, "ERRNO=0");
3028 #endif
3029         exit(0);
3030
3031 failed:
3032 #ifdef USE_SYSTEMD
3033         sd_notify(0, "ERRNO=1");
3034 #endif
3035         if (pid_fd >= 0)
3036                 close(pid_fd);
3037         exit(1);
3038 }
3039
3040 static int
3041 daemonize(void)
3042 {
3043         int pid;
3044         int dev_null_fd;
3045
3046         if( (pid = fork()) < 0){
3047                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
3048                 return -1;
3049         }
3050         else if (pid != 0)
3051                 return pid;
3052
3053         setsid();
3054
3055         if ( (pid = fork()) < 0)
3056                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
3057         else if (pid != 0)
3058                 _exit(0);
3059
3060         if (chdir("/") < 0)
3061                 fprintf(stderr, "cannot chdir to '/', continuing\n");
3062
3063         dev_null_fd = open("/dev/null", O_RDWR);
3064         if (dev_null_fd < 0){
3065                 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
3066                         strerror(errno));
3067                 _exit(0);
3068         }
3069
3070         close(STDIN_FILENO);
3071         if (dup(dev_null_fd) < 0) {
3072                 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
3073                         strerror(errno));
3074                 _exit(0);
3075         }
3076         close(STDOUT_FILENO);
3077         if (dup(dev_null_fd) < 0) {
3078                 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
3079                         strerror(errno));
3080                 _exit(0);
3081         }
3082         close(STDERR_FILENO);
3083         if (dup(dev_null_fd) < 0) {
3084                 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
3085                         strerror(errno));
3086                 _exit(0);
3087         }
3088         close(dev_null_fd);
3089         daemon_pid = getpid();
3090         return 0;
3091 }
3092
3093 int
3094 main (int argc, char *argv[])
3095 {
3096         extern char *optarg;
3097         extern int optind;
3098         int arg;
3099         int err;
3100         int foreground = 0;
3101         struct config *conf;
3102
3103         ANNOTATE_BENIGN_RACE_SIZED(&multipath_conf, sizeof(multipath_conf),
3104                                    "Manipulated through RCU");
3105         ANNOTATE_BENIGN_RACE_SIZED(&uxsock_timeout, sizeof(uxsock_timeout),
3106                 "Suppress complaints about this scalar variable");
3107
3108         logsink = 1;
3109
3110         if (getuid() != 0) {
3111                 fprintf(stderr, "need to be root\n");
3112                 exit(1);
3113         }
3114
3115         /* make sure we don't lock any path */
3116         if (chdir("/") < 0)
3117                 fprintf(stderr, "can't chdir to root directory : %s\n",
3118                         strerror(errno));
3119         umask(umask(077) | 022);
3120
3121         pthread_cond_init_mono(&config_cond);
3122
3123         udev = udev_new();
3124         libmp_udev_set_sync_support(0);
3125
3126         while ((arg = getopt(argc, argv, ":dsv:k::Bniw")) != EOF ) {
3127                 switch(arg) {
3128                 case 'd':
3129                         foreground = 1;
3130                         if (logsink > 0)
3131                                 logsink = 0;
3132                         //debug=1; /* ### comment me out ### */
3133                         break;
3134                 case 'v':
3135                         if (sizeof(optarg) > sizeof(char *) ||
3136                             !isdigit(optarg[0]))
3137                                 exit(1);
3138
3139                         verbosity = atoi(optarg);
3140                         break;
3141                 case 's':
3142                         logsink = -1;
3143                         break;
3144                 case 'k':
3145                         logsink = 0;
3146                         conf = load_config(DEFAULT_CONFIGFILE);
3147                         if (!conf)
3148                                 exit(1);
3149                         if (verbosity)
3150                                 conf->verbosity = verbosity;
3151                         uxsock_timeout = conf->uxsock_timeout;
3152                         err = uxclnt(optarg, uxsock_timeout + 100);
3153                         free_config(conf);
3154                         return err;
3155                 case 'B':
3156                         bindings_read_only = 1;
3157                         break;
3158                 case 'n':
3159                         condlog(0, "WARNING: ignoring deprecated option -n, use 'ignore_wwids = no' instead");
3160                         break;
3161                 case 'w':
3162                         poll_dmevents = 0;
3163                         break;
3164                 default:
3165                         fprintf(stderr, "Invalid argument '-%c'\n",
3166                                 optopt);
3167                         exit(1);
3168                 }
3169         }
3170         if (optind < argc) {
3171                 char cmd[CMDSIZE];
3172                 char * s = cmd;
3173                 char * c = s;
3174
3175                 logsink = 0;
3176                 conf = load_config(DEFAULT_CONFIGFILE);
3177                 if (!conf)
3178                         exit(1);
3179                 if (verbosity)
3180                         conf->verbosity = verbosity;
3181                 uxsock_timeout = conf->uxsock_timeout;
3182                 memset(cmd, 0x0, CMDSIZE);
3183                 while (optind < argc) {
3184                         if (strchr(argv[optind], ' '))
3185                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
3186                         else
3187                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
3188                         optind++;
3189                 }
3190                 c += snprintf(c, s + CMDSIZE - c, "\n");
3191                 err = uxclnt(s, uxsock_timeout + 100);
3192                 free_config(conf);
3193                 return err;
3194         }
3195
3196         if (foreground) {
3197                 if (!isatty(fileno(stdout)))
3198                         setbuf(stdout, NULL);
3199                 err = 0;
3200                 daemon_pid = getpid();
3201         } else
3202                 err = daemonize();
3203
3204         if (err < 0)
3205                 /* error */
3206                 exit(1);
3207         else if (err > 0)
3208                 /* parent dies */
3209                 exit(0);
3210         else
3211                 /* child lives */
3212                 return (child(NULL));
3213 }
3214
3215 void *  mpath_pr_event_handler_fn (void * pathp )
3216 {
3217         struct multipath * mpp;
3218         int i, ret, isFound;
3219         struct path * pp = (struct path *)pathp;
3220         struct prout_param_descriptor *param;