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