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