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