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