cc493c18f8cf38d583b3c3e2b4668d521bdabb0e
[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 = get_multipath_config();
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(2, "%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(2, "%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(2, "%s: delaying path addition until %s is fully initialized", pp->dev, mpp->alias);
915                 mpp->wait_for_udev = 2;
916                 orphan_path(pp, "waiting for create to complete");
917                 return 0;
918         }
919
920         pp->mpp = mpp;
921 rescan:
922         if (mpp) {
923                 if (pp->size && mpp->size != pp->size) {
924                         condlog(0, "%s: failed to add new path %s, "
925                                 "device size mismatch",
926                                 mpp->alias, pp->dev);
927                         int i = find_slot(vecs->pathvec, (void *)pp);
928                         if (i != -1)
929                                 vector_del_slot(vecs->pathvec, i);
930                         free_path(pp);
931                         return 1;
932                 }
933
934                 condlog(4,"%s: adopting all paths for path %s",
935                         mpp->alias, pp->dev);
936                 if (adopt_paths(vecs->pathvec, mpp))
937                         goto fail; /* leave path added to pathvec */
938
939                 verify_paths(mpp, vecs);
940                 mpp->action = ACT_RELOAD;
941                 extract_hwe_from_path(mpp);
942         } else {
943                 if (!should_multipath(pp, vecs->pathvec, vecs->mpvec)) {
944                         orphan_path(pp, "only one path");
945                         return 0;
946                 }
947                 condlog(4,"%s: creating new map", pp->dev);
948                 if ((mpp = add_map_with_path(vecs, pp, 1))) {
949                         mpp->action = ACT_CREATE;
950                         /*
951                          * We don't depend on ACT_CREATE, as domap will
952                          * set it to ACT_NOTHING when complete.
953                          */
954                         start_waiter = 1;
955                 }
956                 if (!start_waiter)
957                         goto fail; /* leave path added to pathvec */
958         }
959
960         /* persistent reservation check*/
961         mpath_pr_event_handle(pp);
962
963         if (!need_do_map)
964                 return 0;
965
966         if (!dm_map_present(mpp->alias)) {
967                 mpp->action = ACT_CREATE;
968                 start_waiter = 1;
969         }
970         /*
971          * push the map to the device-mapper
972          */
973         if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
974                 condlog(0, "%s: failed to setup map for addition of new "
975                         "path %s", mpp->alias, pp->dev);
976                 goto fail_map;
977         }
978         /*
979          * reload the map for the multipath mapped device
980          */
981 retry:
982         ret = domap(mpp, params, 1);
983         if (ret <= 0) {
984                 if (ret < 0 && retries-- > 0) {
985                         condlog(0, "%s: retry domap for addition of new "
986                                 "path %s", mpp->alias, pp->dev);
987                         sleep(1);
988                         goto retry;
989                 }
990                 condlog(0, "%s: failed in domap for addition of new "
991                         "path %s", mpp->alias, pp->dev);
992                 /*
993                  * deal with asynchronous uevents :((
994                  */
995                 if (mpp->action == ACT_RELOAD && retries-- > 0) {
996                         condlog(0, "%s: ev_add_path sleep", mpp->alias);
997                         sleep(1);
998                         update_mpp_paths(mpp, vecs->pathvec);
999                         goto rescan;
1000                 }
1001                 else if (mpp->action == ACT_RELOAD)
1002                         condlog(0, "%s: giving up reload", mpp->alias);
1003                 else
1004                         goto fail_map;
1005         }
1006         dm_lib_release();
1007
1008         if ((mpp->action == ACT_CREATE ||
1009              (mpp->action == ACT_NOTHING && start_waiter && !mpp->waiter)) &&
1010             wait_for_events(mpp, vecs))
1011                         goto fail_map;
1012
1013         /*
1014          * update our state from kernel regardless of create or reload
1015          */
1016         if (setup_multipath(vecs, mpp))
1017                 goto fail; /* if setup_multipath fails, it removes the map */
1018
1019         sync_map_state(mpp);
1020
1021         if (retries >= 0) {
1022                 condlog(2, "%s [%s]: path added to devmap %s",
1023                         pp->dev, pp->dev_t, mpp->alias);
1024                 return 0;
1025         } else
1026                 goto fail;
1027
1028 fail_map:
1029         remove_map(mpp, vecs, 1);
1030 fail:
1031         orphan_path(pp, "failed to add path");
1032         return 1;
1033 }
1034
1035 static int
1036 uev_remove_path (struct uevent *uev, struct vectors * vecs, int need_do_map)
1037 {
1038         struct path *pp;
1039         int ret;
1040
1041         condlog(2, "%s: remove path (uevent)", uev->kernel);
1042         delete_foreign(uev->udev);
1043
1044         pthread_cleanup_push(cleanup_lock, &vecs->lock);
1045         lock(&vecs->lock);
1046         pthread_testcancel();
1047         pp = find_path_by_dev(vecs->pathvec, uev->kernel);
1048         if (pp)
1049                 ret = ev_remove_path(pp, vecs, need_do_map);
1050         lock_cleanup_pop(vecs->lock);
1051         if (!pp) {
1052                 /* Not an error; path might have been purged earlier */
1053                 condlog(0, "%s: path already removed", uev->kernel);
1054                 return 0;
1055         }
1056         return ret;
1057 }
1058
1059 int
1060 ev_remove_path (struct path *pp, struct vectors * vecs, int need_do_map)
1061 {
1062         struct multipath * mpp;
1063         int i, retval = 0;
1064         char params[PARAMS_SIZE] = {0};
1065
1066         /*
1067          * avoid referring to the map of an orphaned path
1068          */
1069         if ((mpp = pp->mpp)) {
1070                 /*
1071                  * transform the mp->pg vector of vectors of paths
1072                  * into a mp->params string to feed the device-mapper
1073                  */
1074                 if (update_mpp_paths(mpp, vecs->pathvec)) {
1075                         condlog(0, "%s: failed to update paths",
1076                                 mpp->alias);
1077                         goto fail;
1078                 }
1079
1080                 /*
1081                  * Make sure mpp->hwe doesn't point to freed memory
1082                  * We call extract_hwe_from_path() below to restore mpp->hwe
1083                  */
1084                 if (mpp->hwe == pp->hwe)
1085                         mpp->hwe = NULL;
1086
1087                 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
1088                         vector_del_slot(mpp->paths, i);
1089
1090                 /*
1091                  * remove the map IF removing the last path
1092                  */
1093                 if (VECTOR_SIZE(mpp->paths) == 0) {
1094                         char alias[WWID_SIZE];
1095
1096                         /*
1097                          * flush_map will fail if the device is open
1098                          */
1099                         strncpy(alias, mpp->alias, WWID_SIZE);
1100                         if (mpp->flush_on_last_del == FLUSH_ENABLED) {
1101                                 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
1102                                 mpp->retry_tick = 0;
1103                                 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
1104                                 mpp->disable_queueing = 1;
1105                                 mpp->stat_map_failures++;
1106                                 dm_queue_if_no_path(mpp->alias, 0);
1107                         }
1108                         if (!flush_map(mpp, vecs, 1)) {
1109                                 condlog(2, "%s: removed map after"
1110                                         " removing all paths",
1111                                         alias);
1112                                 retval = 0;
1113                                 goto out;
1114                         }
1115                         /*
1116                          * Not an error, continue
1117                          */
1118                 }
1119
1120                 if (mpp->hwe == NULL)
1121                         extract_hwe_from_path(mpp);
1122
1123                 if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
1124                         condlog(0, "%s: failed to setup map for"
1125                                 " removal of path %s", mpp->alias, pp->dev);
1126                         goto fail;
1127                 }
1128
1129                 if (mpp->wait_for_udev) {
1130                         mpp->wait_for_udev = 2;
1131                         goto out;
1132                 }
1133
1134                 if (!need_do_map)
1135                         goto out;
1136                 /*
1137                  * reload the map
1138                  */
1139                 mpp->action = ACT_RELOAD;
1140                 if (domap(mpp, params, 1) <= 0) {
1141                         condlog(0, "%s: failed in domap for "
1142                                 "removal of path %s",
1143                                 mpp->alias, pp->dev);
1144                         retval = 1;
1145                 } else {
1146                         /*
1147                          * update our state from kernel
1148                          */
1149                         if (setup_multipath(vecs, mpp))
1150                                 return 1;
1151                         sync_map_state(mpp);
1152
1153                         condlog(2, "%s [%s]: path removed from map %s",
1154                                 pp->dev, pp->dev_t, mpp->alias);
1155                 }
1156         }
1157
1158 out:
1159         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
1160                 vector_del_slot(vecs->pathvec, i);
1161
1162         free_path(pp);
1163
1164         return retval;
1165
1166 fail:
1167         remove_map_and_stop_waiter(mpp, vecs);
1168         return 1;
1169 }
1170
1171 static int
1172 uev_update_path (struct uevent *uev, struct vectors * vecs)
1173 {
1174         int ro, retval = 0, rc;
1175         struct path * pp;
1176         struct config *conf;
1177         int disable_changed_wwids;
1178         int needs_reinit = 0;
1179
1180         switch ((rc = change_foreign(uev->udev))) {
1181         case FOREIGN_OK:
1182                 /* known foreign path, ignore event */
1183                 return 0;
1184         case FOREIGN_IGNORED:
1185                 break;
1186         case FOREIGN_ERR:
1187                 condlog(3, "%s: error in change_foreign", __func__);
1188                 break;
1189         default:
1190                 condlog(1, "%s: return code %d of change_forein is unsupported",
1191                         __func__, rc);
1192                 break;
1193         }
1194
1195         conf = get_multipath_config();
1196         disable_changed_wwids = conf->disable_changed_wwids;
1197         put_multipath_config(conf);
1198
1199         ro = uevent_get_disk_ro(uev);
1200
1201         pthread_cleanup_push(cleanup_lock, &vecs->lock);
1202         lock(&vecs->lock);
1203         pthread_testcancel();
1204
1205         pp = find_path_by_dev(vecs->pathvec, uev->kernel);
1206         if (pp) {
1207                 struct multipath *mpp = pp->mpp;
1208                 char wwid[WWID_SIZE];
1209
1210                 strcpy(wwid, pp->wwid);
1211                 get_uid(pp, pp->state, uev->udev);
1212
1213                 if (strncmp(wwid, pp->wwid, WWID_SIZE) != 0) {
1214                         condlog(0, "%s: path wwid changed from '%s' to '%s'. %s",
1215                                 uev->kernel, wwid, pp->wwid,
1216                                 (disable_changed_wwids ? "disallowing" :
1217                                  "continuing"));
1218                         if (disable_changed_wwids &&
1219                             (strlen(wwid) || pp->wwid_changed)) {
1220                                 strcpy(pp->wwid, wwid);
1221                                 if (!pp->wwid_changed) {
1222                                         pp->wwid_changed = 1;
1223                                         pp->tick = 1;
1224                                         if (pp->mpp)
1225                                                 dm_fail_path(pp->mpp->alias, pp->dev_t);
1226                                 }
1227                                 goto out;
1228                         } else if (!disable_changed_wwids)
1229                                 strcpy(pp->wwid, wwid);
1230                         else
1231                                 pp->wwid_changed = 0;
1232                 } else {
1233                         udev_device_unref(pp->udev);
1234                         pp->udev = udev_device_ref(uev->udev);
1235                         conf = get_multipath_config();
1236                         pthread_cleanup_push(put_multipath_config, conf);
1237                         if (pathinfo(pp, conf, DI_SYSFS|DI_NOIO) != PATHINFO_OK)
1238                                 condlog(1, "%s: pathinfo failed after change uevent",
1239                                         uev->kernel);
1240                         pthread_cleanup_pop(1);
1241                 }
1242
1243                 if (pp->initialized == INIT_REQUESTED_UDEV)
1244                         needs_reinit = 1;
1245                 else if (mpp && ro >= 0) {
1246                         condlog(2, "%s: update path write_protect to '%d' (uevent)", uev->kernel, ro);
1247
1248                         if (mpp->wait_for_udev)
1249                                 mpp->wait_for_udev = 2;
1250                         else {
1251                                 if (ro == 1)
1252                                         pp->mpp->force_readonly = 1;
1253                                 retval = reload_map(vecs, mpp, 0, 1);
1254                                 pp->mpp->force_readonly = 0;
1255                                 condlog(2, "%s: map %s reloaded (retval %d)",
1256                                         uev->kernel, mpp->alias, retval);
1257                         }
1258                 }
1259         }
1260 out:
1261         lock_cleanup_pop(vecs->lock);
1262         if (!pp) {
1263                 /* If the path is blacklisted, print a debug/non-default verbosity message. */
1264                 if (uev->udev) {
1265                         int flag = DI_SYSFS | DI_WWID;
1266
1267                         conf = get_multipath_config();
1268                         pthread_cleanup_push(put_multipath_config, conf);
1269                         retval = alloc_path_with_pathinfo(conf, uev->udev, uev->wwid, flag, NULL);
1270                         pthread_cleanup_pop(1);
1271
1272                         if (retval == PATHINFO_SKIPPED) {
1273                                 condlog(3, "%s: spurious uevent, path is blacklisted", uev->kernel);
1274                                 return 0;
1275                         }
1276                 }
1277
1278                 condlog(0, "%s: spurious uevent, path not found", uev->kernel);
1279         }
1280         if (needs_reinit)
1281                 retval = uev_add_path(uev, vecs, 1);
1282         return retval;
1283 }
1284
1285 static int
1286 uev_pathfail_check(struct uevent *uev, struct vectors *vecs)
1287 {
1288         char *action = NULL, *devt = NULL;
1289         struct path *pp;
1290         int r = 1;
1291
1292         action = uevent_get_dm_action(uev);
1293         if (!action)
1294                 return 1;
1295         if (strncmp(action, "PATH_FAILED", 11))
1296                 goto out;
1297         devt = uevent_get_dm_path(uev);
1298         if (!devt) {
1299                 condlog(3, "%s: No DM_PATH in uevent", uev->kernel);
1300                 goto out;
1301         }
1302
1303         pthread_cleanup_push(cleanup_lock, &vecs->lock);
1304         lock(&vecs->lock);
1305         pthread_testcancel();
1306         pp = find_path_by_devt(vecs->pathvec, devt);
1307         if (!pp)
1308                 goto out_lock;
1309         r = io_err_stat_handle_pathfail(pp);
1310         if (r)
1311                 condlog(3, "io_err_stat: %s: cannot handle pathfail uevent",
1312                                 pp->dev);
1313 out_lock:
1314         lock_cleanup_pop(vecs->lock);
1315         FREE(devt);
1316         FREE(action);
1317         return r;
1318 out:
1319         FREE(action);
1320         return 1;
1321 }
1322
1323 static int
1324 map_discovery (struct vectors * vecs)
1325 {
1326         struct multipath * mpp;
1327         unsigned int i;
1328
1329         if (dm_get_maps(vecs->mpvec))
1330                 return 1;
1331
1332         vector_foreach_slot (vecs->mpvec, mpp, i)
1333                 if (update_multipath_table(mpp, vecs->pathvec, 1) ||
1334                     update_multipath_status(mpp)) {
1335                         remove_map(mpp, vecs, 1);
1336                         i--;
1337                 }
1338
1339         return 0;
1340 }
1341
1342 int
1343 uxsock_trigger (char * str, char ** reply, int * len, bool is_root,
1344                 void * trigger_data)
1345 {
1346         struct vectors * vecs;
1347         int r;
1348
1349         *reply = NULL;
1350         *len = 0;
1351         vecs = (struct vectors *)trigger_data;
1352
1353         if ((str != NULL) && (is_root == false) &&
1354             (strncmp(str, "list", strlen("list")) != 0) &&
1355             (strncmp(str, "show", strlen("show")) != 0)) {
1356                 *reply = STRDUP("permission deny: need to be root");
1357                 if (*reply)
1358                         *len = strlen(*reply) + 1;
1359                 return 1;
1360         }
1361
1362         r = parse_cmd(str, reply, len, vecs, uxsock_timeout / 1000);
1363
1364         if (r > 0) {
1365                 if (r == ETIMEDOUT)
1366                         *reply = STRDUP("timeout\n");
1367                 else
1368                         *reply = STRDUP("fail\n");
1369                 if (*reply)
1370                         *len = strlen(*reply) + 1;
1371                 r = 1;
1372         }
1373         else if (!r && *len == 0) {
1374                 *reply = STRDUP("ok\n");
1375                 if (*reply)
1376                         *len = strlen(*reply) + 1;
1377                 r = 0;
1378         }
1379         /* else if (r < 0) leave *reply alone */
1380
1381         return r;
1382 }
1383
1384 int
1385 uev_trigger (struct uevent * uev, void * trigger_data)
1386 {
1387         int r = 0;
1388         struct vectors * vecs;
1389         struct uevent *merge_uev, *tmp;
1390
1391         vecs = (struct vectors *)trigger_data;
1392
1393         pthread_cleanup_push(config_cleanup, NULL);
1394         pthread_mutex_lock(&config_lock);
1395         if (running_state != DAEMON_IDLE &&
1396             running_state != DAEMON_RUNNING)
1397                 pthread_cond_wait(&config_cond, &config_lock);
1398         pthread_cleanup_pop(1);
1399
1400         if (running_state == DAEMON_SHUTDOWN)
1401                 return 0;
1402
1403         /*
1404          * device map event
1405          * Add events are ignored here as the tables
1406          * are not fully initialised then.
1407          */
1408         if (!strncmp(uev->kernel, "dm-", 3)) {
1409                 if (!uevent_is_mpath(uev)) {
1410                         if (!strncmp(uev->action, "change", 6))
1411                                 (void)add_foreign(uev->udev);
1412                         else if (!strncmp(uev->action, "remove", 6))
1413                                 (void)delete_foreign(uev->udev);
1414                         goto out;
1415                 }
1416                 if (!strncmp(uev->action, "change", 6)) {
1417                         r = uev_add_map(uev, vecs);
1418
1419                         /*
1420                          * the kernel-side dm-mpath issues a PATH_FAILED event
1421                          * when it encounters a path IO error. It is reason-
1422                          * able be the entry of path IO error accounting pro-
1423                          * cess.
1424                          */
1425                         uev_pathfail_check(uev, vecs);
1426                 } else if (!strncmp(uev->action, "remove", 6)) {
1427                         r = uev_remove_map(uev, vecs);
1428                 }
1429                 goto out;
1430         }
1431
1432         /*
1433          * path add/remove/change event, add/remove maybe merged
1434          */
1435         list_for_each_entry_safe(merge_uev, tmp, &uev->merge_node, node) {
1436                 if (!strncmp(merge_uev->action, "add", 3))
1437                         r += uev_add_path(merge_uev, vecs, 0);
1438                 if (!strncmp(merge_uev->action, "remove", 6))
1439                         r += uev_remove_path(merge_uev, vecs, 0);
1440         }
1441
1442         if (!strncmp(uev->action, "add", 3))
1443                 r += uev_add_path(uev, vecs, 1);
1444         if (!strncmp(uev->action, "remove", 6))
1445                 r += uev_remove_path(uev, vecs, 1);
1446         if (!strncmp(uev->action, "change", 6))
1447                 r += uev_update_path(uev, vecs);
1448
1449 out:
1450         return r;
1451 }
1452
1453 static void rcu_unregister(void *param)
1454 {
1455         rcu_unregister_thread();
1456 }
1457
1458 static void *
1459 ueventloop (void * ap)
1460 {
1461         struct udev *udev = ap;
1462
1463         pthread_cleanup_push(rcu_unregister, NULL);
1464         rcu_register_thread();
1465         if (uevent_listen(udev))
1466                 condlog(0, "error starting uevent listener");
1467         pthread_cleanup_pop(1);
1468         return NULL;
1469 }
1470
1471 static void *
1472 uevqloop (void * ap)
1473 {
1474         pthread_cleanup_push(rcu_unregister, NULL);
1475         rcu_register_thread();
1476         if (uevent_dispatch(&uev_trigger, ap))
1477                 condlog(0, "error starting uevent dispatcher");
1478         pthread_cleanup_pop(1);
1479         return NULL;
1480 }
1481 static void *
1482 uxlsnrloop (void * ap)
1483 {
1484         if (cli_init()) {
1485                 condlog(1, "Failed to init uxsock listener");
1486                 return NULL;
1487         }
1488         pthread_cleanup_push(rcu_unregister, NULL);
1489         rcu_register_thread();
1490         set_handler_callback(LIST+PATHS, cli_list_paths);
1491         set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
1492         set_handler_callback(LIST+PATHS+RAW+FMT, cli_list_paths_raw);
1493         set_handler_callback(LIST+PATH, cli_list_path);
1494         set_handler_callback(LIST+MAPS, cli_list_maps);
1495         set_handler_callback(LIST+STATUS, cli_list_status);
1496         set_unlocked_handler_callback(LIST+DAEMON, cli_list_daemon);
1497         set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
1498         set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
1499         set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
1500         set_handler_callback(LIST+MAPS+RAW+FMT, cli_list_maps_raw);
1501         set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1502         set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
1503         set_handler_callback(LIST+MAPS+JSON, cli_list_maps_json);
1504         set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1505         set_handler_callback(LIST+MAP+FMT, cli_list_map_fmt);
1506         set_handler_callback(LIST+MAP+RAW+FMT, cli_list_map_fmt);
1507         set_handler_callback(LIST+MAP+JSON, cli_list_map_json);
1508         set_handler_callback(LIST+CONFIG+LOCAL, cli_list_config_local);
1509         set_handler_callback(LIST+CONFIG, cli_list_config);
1510         set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
1511         set_handler_callback(LIST+DEVICES, cli_list_devices);
1512         set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
1513         set_handler_callback(RESET+MAPS+STATS, cli_reset_maps_stats);
1514         set_handler_callback(RESET+MAP+STATS, cli_reset_map_stats);
1515         set_handler_callback(ADD+PATH, cli_add_path);
1516         set_handler_callback(DEL+PATH, cli_del_path);
1517         set_handler_callback(ADD+MAP, cli_add_map);
1518         set_handler_callback(DEL+MAP, cli_del_map);
1519         set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
1520         set_unlocked_handler_callback(RECONFIGURE, cli_reconfigure);
1521         set_handler_callback(SUSPEND+MAP, cli_suspend);
1522         set_handler_callback(RESUME+MAP, cli_resume);
1523         set_handler_callback(RESIZE+MAP, cli_resize);
1524         set_handler_callback(RELOAD+MAP, cli_reload);
1525         set_handler_callback(RESET+MAP, cli_reassign);
1526         set_handler_callback(REINSTATE+PATH, cli_reinstate);
1527         set_handler_callback(FAIL+PATH, cli_fail);
1528         set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
1529         set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
1530         set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
1531         set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
1532         set_unlocked_handler_callback(QUIT, cli_quit);
1533         set_unlocked_handler_callback(SHUTDOWN, cli_shutdown);
1534         set_handler_callback(GETPRSTATUS+MAP, cli_getprstatus);
1535         set_handler_callback(SETPRSTATUS+MAP, cli_setprstatus);
1536         set_handler_callback(UNSETPRSTATUS+MAP, cli_unsetprstatus);
1537         set_handler_callback(FORCEQ+DAEMON, cli_force_no_daemon_q);
1538         set_handler_callback(RESTOREQ+DAEMON, cli_restore_no_daemon_q);
1539         set_handler_callback(GETPRKEY+MAP, cli_getprkey);
1540         set_handler_callback(SETPRKEY+MAP+KEY, cli_setprkey);
1541         set_handler_callback(UNSETPRKEY+MAP, cli_unsetprkey);
1542
1543         umask(077);
1544         uxsock_listen(&uxsock_trigger, ap);
1545         pthread_cleanup_pop(1);
1546         return NULL;
1547 }
1548
1549 void
1550 exit_daemon (void)
1551 {
1552         post_config_state(DAEMON_SHUTDOWN);
1553 }
1554
1555 static void
1556 fail_path (struct path * pp, int del_active)
1557 {
1558         if (!pp->mpp)
1559                 return;
1560
1561         condlog(2, "checker failed path %s in map %s",
1562                  pp->dev_t, pp->mpp->alias);
1563
1564         dm_fail_path(pp->mpp->alias, pp->dev_t);
1565         if (del_active)
1566                 update_queue_mode_del_path(pp->mpp);
1567 }
1568
1569 /*
1570  * caller must have locked the path list before calling that function
1571  */
1572 static int
1573 reinstate_path (struct path * pp, int add_active)
1574 {
1575         int ret = 0;
1576
1577         if (!pp->mpp)
1578                 return 0;
1579
1580         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t)) {
1581                 condlog(0, "%s: reinstate failed", pp->dev_t);
1582                 ret = 1;
1583         } else {
1584                 condlog(2, "%s: reinstated", pp->dev_t);
1585                 if (add_active)
1586                         update_queue_mode_add_path(pp->mpp);
1587         }
1588         return ret;
1589 }
1590
1591 static void
1592 enable_group(struct path * pp)
1593 {
1594         struct pathgroup * pgp;
1595
1596         /*
1597          * if path is added through uev_add_path, pgindex can be unset.
1598          * next update_strings() will set it, upon map reload event.
1599          *
1600          * we can safely return here, because upon map reload, all
1601          * PG will be enabled.
1602          */
1603         if (!pp->mpp->pg || !pp->pgindex)
1604                 return;
1605
1606         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1607
1608         if (pgp->status == PGSTATE_DISABLED) {
1609                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1610                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1611         }
1612 }
1613
1614 static void
1615 mpvec_garbage_collector (struct vectors * vecs)
1616 {
1617         struct multipath * mpp;
1618         unsigned int i;
1619
1620         if (!vecs->mpvec)
1621                 return;
1622
1623         vector_foreach_slot (vecs->mpvec, mpp, i) {
1624                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1625                         condlog(2, "%s: remove dead map", mpp->alias);
1626                         remove_map_and_stop_waiter(mpp, vecs);
1627                         i--;
1628                 }
1629         }
1630 }
1631
1632 /* This is called after a path has started working again. It the multipath
1633  * device for this path uses the followover failback type, and this is the
1634  * best pathgroup, and this is the first path in the pathgroup to come back
1635  * up, then switch to this pathgroup */
1636 static int
1637 followover_should_failback(struct path * pp)
1638 {
1639         struct pathgroup * pgp;
1640         struct path *pp1;
1641         int i;
1642
1643         if (pp->mpp->pgfailback != -FAILBACK_FOLLOWOVER ||
1644             !pp->mpp->pg || !pp->pgindex ||
1645             pp->pgindex != pp->mpp->bestpg)
1646                 return 0;
1647
1648         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1649         vector_foreach_slot(pgp->paths, pp1, i) {
1650                 if (pp1 == pp)
1651                         continue;
1652                 if (pp1->chkrstate != PATH_DOWN && pp1->chkrstate != PATH_SHAKY)
1653                         return 0;
1654         }
1655         return 1;
1656 }
1657
1658 static void
1659 missing_uev_wait_tick(struct vectors *vecs)
1660 {
1661         struct multipath * mpp;
1662         unsigned int i;
1663         int timed_out = 0, delayed_reconfig;
1664         struct config *conf;
1665
1666         vector_foreach_slot (vecs->mpvec, mpp, i) {
1667                 if (mpp->wait_for_udev && --mpp->uev_wait_tick <= 0) {
1668                         timed_out = 1;
1669                         condlog(0, "%s: timeout waiting on creation uevent. enabling reloads", mpp->alias);
1670                         if (mpp->wait_for_udev > 1 &&
1671                             update_map(mpp, vecs, 0)) {
1672                                 /* update_map removed map */
1673                                 i--;
1674                                 continue;
1675                         }
1676                         mpp->wait_for_udev = 0;
1677                 }
1678         }
1679
1680         conf = get_multipath_config();
1681         delayed_reconfig = conf->delayed_reconfig;
1682         put_multipath_config(conf);
1683         if (timed_out && delayed_reconfig &&
1684             !need_to_delay_reconfig(vecs)) {
1685                 condlog(2, "reconfigure (delayed)");
1686                 set_config_state(DAEMON_CONFIGURE);
1687         }
1688 }
1689
1690 static void
1691 ghost_delay_tick(struct vectors *vecs)
1692 {
1693         struct multipath * mpp;
1694         unsigned int i;
1695
1696         vector_foreach_slot (vecs->mpvec, mpp, i) {
1697                 if (mpp->ghost_delay_tick <= 0)
1698                         continue;
1699                 if (--mpp->ghost_delay_tick <= 0) {
1700                         condlog(0, "%s: timed out waiting for active path",
1701                                 mpp->alias);
1702                         mpp->force_udev_reload = 1;
1703                         if (update_map(mpp, vecs, 0) != 0) {
1704                                 /* update_map removed map */
1705                                 i--;
1706                                 continue;
1707                         }
1708                 }
1709         }
1710 }
1711
1712 static void
1713 defered_failback_tick (vector mpvec)
1714 {
1715         struct multipath * mpp;
1716         unsigned int i;
1717
1718         vector_foreach_slot (mpvec, mpp, i) {
1719                 /*
1720                  * deferred failback getting sooner
1721                  */
1722                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1723                         mpp->failback_tick--;
1724
1725                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1726                                 switch_pathgroup(mpp);
1727                 }
1728         }
1729 }
1730
1731 static void
1732 retry_count_tick(vector mpvec)
1733 {
1734         struct multipath *mpp;
1735         unsigned int i;
1736
1737         vector_foreach_slot (mpvec, mpp, i) {
1738                 if (mpp->retry_tick > 0) {
1739                         mpp->stat_total_queueing_time++;
1740                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1741                         if(--mpp->retry_tick == 0) {
1742                                 mpp->stat_map_failures++;
1743                                 dm_queue_if_no_path(mpp->alias, 0);
1744                                 condlog(2, "%s: Disable queueing", mpp->alias);
1745                         }
1746                 }
1747         }
1748 }
1749
1750 int update_prio(struct path *pp, int refresh_all)
1751 {
1752         int oldpriority;
1753         struct path *pp1;
1754         struct pathgroup * pgp;
1755         int i, j, changed = 0;
1756         struct config *conf;
1757
1758         if (refresh_all) {
1759                 vector_foreach_slot (pp->mpp->pg, pgp, i) {
1760                         vector_foreach_slot (pgp->paths, pp1, j) {
1761                                 oldpriority = pp1->priority;
1762                                 conf = get_multipath_config();
1763                                 pthread_cleanup_push(put_multipath_config,
1764                                                      conf);
1765                                 pathinfo(pp1, conf, DI_PRIO);
1766                                 pthread_cleanup_pop(1);
1767                                 if (pp1->priority != oldpriority)
1768                                         changed = 1;
1769                         }
1770                 }
1771                 return changed;
1772         }
1773         oldpriority = pp->priority;
1774         conf = get_multipath_config();
1775         pthread_cleanup_push(put_multipath_config, conf);
1776         if (pp->state != PATH_DOWN)
1777                 pathinfo(pp, conf, DI_PRIO);
1778         pthread_cleanup_pop(1);
1779
1780         if (pp->priority == oldpriority)
1781                 return 0;
1782         return 1;
1783 }
1784
1785 int update_path_groups(struct multipath *mpp, struct vectors *vecs, int refresh)
1786 {
1787         if (reload_map(vecs, mpp, refresh, 1))
1788                 return 1;
1789
1790         dm_lib_release();
1791         if (setup_multipath(vecs, mpp) != 0)
1792                 return 1;
1793         sync_map_state(mpp);
1794
1795         return 0;
1796 }
1797
1798 /*
1799  * Returns '1' if the path has been checked, '-1' if it was blacklisted
1800  * and '0' otherwise
1801  */
1802 int
1803 check_path (struct vectors * vecs, struct path * pp, int ticks)
1804 {
1805         int newstate;
1806         int new_path_up = 0;
1807         int chkr_new_path_up = 0;
1808         int add_active;
1809         int disable_reinstate = 0;
1810         int oldchkrstate = pp->chkrstate;
1811         int retrigger_tries, checkint;
1812         struct config *conf;
1813         int ret;
1814
1815         if ((pp->initialized == INIT_OK ||
1816              pp->initialized == INIT_REQUESTED_UDEV) && !pp->mpp)
1817                 return 0;
1818
1819         if (pp->tick)
1820                 pp->tick -= (pp->tick > ticks) ? ticks : pp->tick;
1821         if (pp->tick)
1822                 return 0; /* don't check this path yet */
1823
1824         conf = get_multipath_config();
1825         retrigger_tries = conf->retrigger_tries;
1826         checkint = conf->checkint;
1827         put_multipath_config(conf);
1828         if (!pp->mpp && pp->initialized == INIT_MISSING_UDEV &&
1829             pp->retriggers < retrigger_tries) {
1830                 condlog(2, "%s: triggering change event to reinitialize",
1831                         pp->dev);
1832                 pp->initialized = INIT_REQUESTED_UDEV;
1833                 pp->retriggers++;
1834                 sysfs_attr_set_value(pp->udev, "uevent", "change",
1835                                      strlen("change"));
1836                 return 0;
1837         }
1838
1839         /*
1840          * provision a next check soonest,
1841          * in case we exit abnormaly from here
1842          */
1843         pp->tick = checkint;
1844
1845         newstate = path_offline(pp);
1846         /*
1847          * Wait for uevent for removed paths;
1848          * some LLDDs like zfcp keep paths unavailable
1849          * without sending uevents.
1850          */
1851         if (newstate == PATH_REMOVED)
1852                 newstate = PATH_DOWN;
1853
1854         if (newstate == PATH_UP) {
1855                 conf = get_multipath_config();
1856                 pthread_cleanup_push(put_multipath_config, conf);
1857                 newstate = get_state(pp, conf, 1, newstate);
1858                 pthread_cleanup_pop(1);
1859         } else
1860                 checker_clear_message(&pp->checker);
1861
1862         if (pp->wwid_changed) {
1863                 condlog(2, "%s: path wwid has changed. Refusing to use",
1864                         pp->dev);
1865                 newstate = PATH_DOWN;
1866         }
1867
1868         if (newstate == PATH_WILD || newstate == PATH_UNCHECKED) {
1869                 condlog(2, "%s: unusable path", pp->dev);
1870                 conf = get_multipath_config();
1871                 pthread_cleanup_push(put_multipath_config, conf);
1872                 pathinfo(pp, conf, 0);
1873                 pthread_cleanup_pop(1);
1874                 return 1;
1875         }
1876         if (!pp->mpp) {
1877                 if (!strlen(pp->wwid) && pp->initialized != INIT_MISSING_UDEV &&
1878                     (newstate == PATH_UP || newstate == PATH_GHOST)) {
1879                         condlog(2, "%s: add missing path", pp->dev);
1880                         conf = get_multipath_config();
1881                         pthread_cleanup_push(put_multipath_config, conf);
1882                         ret = pathinfo(pp, conf, DI_ALL | DI_BLACKLIST);
1883                         pthread_cleanup_pop(1);
1884                         if (ret == PATHINFO_OK) {
1885                                 ev_add_path(pp, vecs, 1);
1886                                 pp->tick = 1;
1887                         } else if (ret == PATHINFO_SKIPPED)
1888                                 return -1;
1889                 }
1890                 return 0;
1891         }
1892         /*
1893          * Async IO in flight. Keep the previous path state
1894          * and reschedule as soon as possible
1895          */
1896         if (newstate == PATH_PENDING) {
1897                 pp->tick = 1;
1898                 return 0;
1899         }
1900         /*
1901          * Synchronize with kernel state
1902          */
1903         if (update_multipath_strings(pp->mpp, vecs->pathvec, 1)) {
1904                 condlog(1, "%s: Could not synchronize with kernel state",
1905                         pp->dev);
1906                 pp->dmstate = PSTATE_UNDEF;
1907         }
1908         /* if update_multipath_strings orphaned the path, quit early */
1909         if (!pp->mpp)
1910                 return 0;
1911
1912         if (pp->io_err_disable_reinstate && hit_io_err_recheck_time(pp)) {
1913                 pp->state = PATH_SHAKY;
1914                 /*
1915                  * to reschedule as soon as possible,so that this path can
1916                  * be recoverd in time
1917                  */
1918                 pp->tick = 1;
1919                 return 1;
1920         }
1921
1922         if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
1923              pp->wait_checks > 0) {
1924                 if (pp->mpp->nr_active > 0) {
1925                         pp->state = PATH_DELAYED;
1926                         pp->wait_checks--;
1927                         return 1;
1928                 } else
1929                         pp->wait_checks = 0;
1930         }
1931
1932         /*
1933          * don't reinstate failed path, if its in stand-by
1934          * and if target supports only implicit tpgs mode.
1935          * this will prevent unnecessary i/o by dm on stand-by
1936          * paths if there are no other active paths in map.
1937          */
1938         disable_reinstate = (newstate == PATH_GHOST &&
1939                             pp->mpp->nr_active == 0 &&
1940                             pp->tpgs == TPGS_IMPLICIT) ? 1 : 0;
1941
1942         pp->chkrstate = newstate;
1943         if (newstate != pp->state) {
1944                 int oldstate = pp->state;
1945                 pp->state = newstate;
1946
1947                 LOG_MSG(1, checker_message(&pp->checker));
1948
1949                 /*
1950                  * upon state change, reset the checkint
1951                  * to the shortest delay
1952                  */
1953                 conf = get_multipath_config();
1954                 pp->checkint = conf->checkint;
1955                 put_multipath_config(conf);
1956
1957                 if (newstate != PATH_UP && newstate != PATH_GHOST) {
1958                         /*
1959                          * proactively fail path in the DM
1960                          */
1961                         if (oldstate == PATH_UP ||
1962                             oldstate == PATH_GHOST) {
1963                                 fail_path(pp, 1);
1964                                 if (pp->mpp->delay_wait_checks > 0 &&
1965                                     pp->watch_checks > 0) {
1966                                         pp->wait_checks = pp->mpp->delay_wait_checks;
1967                                         pp->watch_checks = 0;
1968                                 }
1969                         }else
1970                                 fail_path(pp, 0);
1971
1972                         /*
1973                          * cancel scheduled failback
1974                          */
1975                         pp->mpp->failback_tick = 0;
1976
1977                         pp->mpp->stat_path_failures++;
1978                         return 1;
1979                 }
1980
1981                 if(newstate == PATH_UP || newstate == PATH_GHOST){
1982                         if ( pp->mpp && pp->mpp->prflag ){
1983                                 /*
1984                                  * Check Persistent Reservation.
1985                                  */
1986                         condlog(2, "%s: checking persistent reservation "
1987                                 "registration", pp->dev);
1988                         mpath_pr_event_handle(pp);
1989                         }
1990                 }
1991
1992                 /*
1993                  * reinstate this path
1994                  */
1995                 if (oldstate != PATH_UP &&
1996                     oldstate != PATH_GHOST) {
1997                         if (pp->mpp->delay_watch_checks > 0)
1998                                 pp->watch_checks = pp->mpp->delay_watch_checks;
1999                         add_active = 1;
2000                 } else {
2001                         if (pp->watch_checks > 0)
2002                                 pp->watch_checks--;
2003                         add_active = 0;
2004                 }
2005                 if (!disable_reinstate && reinstate_path(pp, add_active)) {
2006                         condlog(3, "%s: reload map", pp->dev);
2007                         ev_add_path(pp, vecs, 1);
2008                         pp->tick = 1;
2009                         return 0;
2010                 }
2011                 new_path_up = 1;
2012
2013                 if (oldchkrstate != PATH_UP && oldchkrstate != PATH_GHOST)
2014                         chkr_new_path_up = 1;
2015
2016                 /*
2017                  * if at least one path is up in a group, and
2018                  * the group is disabled, re-enable it
2019                  */
2020                 if (newstate == PATH_UP)
2021                         enable_group(pp);
2022         }
2023         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
2024                 if ((pp->dmstate == PSTATE_FAILED ||
2025                     pp->dmstate == PSTATE_UNDEF) &&
2026                     !disable_reinstate) {
2027                         /* Clear IO errors */
2028                         if (reinstate_path(pp, 0)) {
2029                                 condlog(3, "%s: reload map", pp->dev);
2030                                 ev_add_path(pp, vecs, 1);
2031                                 pp->tick = 1;
2032                                 return 0;
2033                         }
2034                 } else {
2035                         unsigned int max_checkint;
2036                         LOG_MSG(4, checker_message(&pp->checker));
2037                         conf = get_multipath_config();
2038                         max_checkint = conf->max_checkint;
2039                         put_multipath_config(conf);
2040                         if (pp->checkint != max_checkint) {
2041                                 /*
2042                                  * double the next check delay.
2043                                  * max at conf->max_checkint
2044                                  */
2045                                 if (pp->checkint < (max_checkint / 2))
2046                                         pp->checkint = 2 * pp->checkint;
2047                                 else
2048                                         pp->checkint = max_checkint;
2049
2050                                 condlog(4, "%s: delay next check %is",
2051                                         pp->dev_t, pp->checkint);
2052                         }
2053                         if (pp->watch_checks > 0)
2054                                 pp->watch_checks--;
2055                         pp->tick = pp->checkint;
2056                 }
2057         }
2058         else if (newstate != PATH_UP && newstate != PATH_GHOST) {
2059                 if (pp->dmstate == PSTATE_ACTIVE ||
2060                     pp->dmstate == PSTATE_UNDEF)
2061                         fail_path(pp, 0);
2062                 if (newstate == PATH_DOWN) {
2063                         int log_checker_err;
2064
2065                         conf = get_multipath_config();
2066                         log_checker_err = conf->log_checker_err;
2067                         put_multipath_config(conf);
2068                         if (log_checker_err == LOG_CHKR_ERR_ONCE)
2069                                 LOG_MSG(3, checker_message(&pp->checker));
2070                         else
2071                                 LOG_MSG(2, checker_message(&pp->checker));
2072                 }
2073         }
2074
2075         pp->state = newstate;
2076
2077         if (pp->mpp->wait_for_udev)
2078                 return 1;
2079         /*
2080          * path prio refreshing
2081          */
2082         condlog(4, "path prio refresh");
2083
2084         if (update_prio(pp, new_path_up) &&
2085             (pp->mpp->pgpolicyfn == (pgpolicyfn *)group_by_prio) &&
2086              pp->mpp->pgfailback == -FAILBACK_IMMEDIATE)
2087                 update_path_groups(pp->mpp, vecs, !new_path_up);
2088         else if (need_switch_pathgroup(pp->mpp, 0)) {
2089                 if (pp->mpp->pgfailback > 0 &&
2090                     (new_path_up || pp->mpp->failback_tick <= 0))
2091                         pp->mpp->failback_tick =
2092                                 pp->mpp->pgfailback + 1;
2093                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE ||
2094                          (chkr_new_path_up && followover_should_failback(pp)))
2095                         switch_pathgroup(pp->mpp);
2096         }
2097         return 1;
2098 }
2099
2100 static void init_path_check_interval(struct vectors *vecs)
2101 {
2102         struct config *conf;
2103         struct path *pp;
2104         unsigned int i;
2105
2106         vector_foreach_slot (vecs->pathvec, pp, i) {
2107                 conf = get_multipath_config();
2108                 pp->checkint = conf->checkint;
2109                 put_multipath_config(conf);
2110         }
2111 }
2112
2113 static void *
2114 checkerloop (void *ap)
2115 {
2116         struct vectors *vecs;
2117         struct path *pp;
2118         int count = 0;
2119         unsigned int i;
2120         struct timespec last_time;
2121         struct config *conf;
2122
2123         pthread_cleanup_push(rcu_unregister, NULL);
2124         rcu_register_thread();
2125         mlockall(MCL_CURRENT | MCL_FUTURE);
2126         vecs = (struct vectors *)ap;
2127         condlog(2, "path checkers start up");
2128
2129         /* Tweak start time for initial path check */
2130         if (clock_gettime(CLOCK_MONOTONIC, &last_time) != 0)
2131                 last_time.tv_sec = 0;
2132         else
2133                 last_time.tv_sec -= 1;
2134
2135         while (1) {
2136                 struct timespec diff_time, start_time, end_time;
2137                 int num_paths = 0, ticks = 0, strict_timing, rc = 0;
2138
2139                 if (clock_gettime(CLOCK_MONOTONIC, &start_time) != 0)
2140                         start_time.tv_sec = 0;
2141                 if (start_time.tv_sec && last_time.tv_sec) {
2142                         timespecsub(&start_time, &last_time, &diff_time);
2143                         condlog(4, "tick (%lu.%06lu secs)",
2144                                 diff_time.tv_sec, diff_time.tv_nsec / 1000);
2145                         last_time = start_time;
2146                         ticks = diff_time.tv_sec;
2147                 } else {
2148                         ticks = 1;
2149                         condlog(4, "tick (%d ticks)", ticks);
2150                 }
2151 #ifdef USE_SYSTEMD
2152                 if (use_watchdog)
2153                         sd_notify(0, "WATCHDOG=1");
2154 #endif
2155                 rc = set_config_state(DAEMON_RUNNING);
2156                 if (rc == ETIMEDOUT) {
2157                         condlog(4, "timeout waiting for DAEMON_IDLE");
2158                         continue;
2159                 }
2160
2161                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2162                 lock(&vecs->lock);
2163                 pthread_testcancel();
2164                 vector_foreach_slot (vecs->pathvec, pp, i) {
2165                         rc = check_path(vecs, pp, ticks);
2166                         if (rc < 0) {
2167                                 vector_del_slot(vecs->pathvec, i);
2168                                 free_path(pp);
2169                                 i--;
2170                         } else
2171                                 num_paths += rc;
2172                 }
2173                 lock_cleanup_pop(vecs->lock);
2174
2175                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
2176                 lock(&vecs->lock);
2177                 pthread_testcancel();
2178                 defered_failback_tick(vecs->mpvec);
2179                 retry_count_tick(vecs->mpvec);
2180                 missing_uev_wait_tick(vecs);
2181                 ghost_delay_tick(vecs);
2182                 lock_cleanup_pop(vecs->lock);
2183
2184                 if (count)
2185                         count--;
2186                 else {
2187                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2188                         lock(&vecs->lock);
2189                         pthread_testcancel();
2190                         condlog(4, "map garbage collection");
2191                         mpvec_garbage_collector(vecs);
2192                         count = MAPGCINT;
2193                         lock_cleanup_pop(vecs->lock);
2194                 }
2195
2196                 diff_time.tv_nsec = 0;
2197                 if (start_time.tv_sec &&
2198                     clock_gettime(CLOCK_MONOTONIC, &end_time) == 0) {
2199                         timespecsub(&end_time, &start_time, &diff_time);
2200                         if (num_paths) {
2201                                 unsigned int max_checkint;
2202
2203                                 condlog(3, "checked %d path%s in %lu.%06lu secs",
2204                                         num_paths, num_paths > 1 ? "s" : "",
2205                                         diff_time.tv_sec,
2206                                         diff_time.tv_nsec / 1000);
2207                                 conf = get_multipath_config();
2208                                 max_checkint = conf->max_checkint;
2209                                 put_multipath_config(conf);
2210                                 if (diff_time.tv_sec > max_checkint)
2211                                         condlog(1, "path checkers took longer "
2212                                                 "than %lu seconds, consider "
2213                                                 "increasing max_polling_interval",
2214                                                 diff_time.tv_sec);
2215                         }
2216                 }
2217                 check_foreign();
2218                 post_config_state(DAEMON_IDLE);
2219                 conf = get_multipath_config();
2220                 strict_timing = conf->strict_timing;
2221                 put_multipath_config(conf);
2222                 if (!strict_timing)
2223                         sleep(1);
2224                 else {
2225                         if (diff_time.tv_nsec) {
2226                                 diff_time.tv_sec = 0;
2227                                 diff_time.tv_nsec =
2228                                      1000UL * 1000 * 1000 - diff_time.tv_nsec;
2229                         } else
2230                                 diff_time.tv_sec = 1;
2231
2232                         condlog(3, "waiting for %lu.%06lu secs",
2233                                 diff_time.tv_sec,
2234                                 diff_time.tv_nsec / 1000);
2235                         if (nanosleep(&diff_time, NULL) != 0) {
2236                                 condlog(3, "nanosleep failed with error %d",
2237                                         errno);
2238                                 conf = get_multipath_config();
2239                                 conf->strict_timing = 0;
2240                                 put_multipath_config(conf);
2241                                 break;
2242                         }
2243                 }
2244         }
2245         pthread_cleanup_pop(1);
2246         return NULL;
2247 }
2248
2249 int
2250 configure (struct vectors * vecs)
2251 {
2252         struct multipath * mpp;
2253         struct path * pp;
2254         vector mpvec;
2255         int i, ret;
2256         struct config *conf;
2257         static int force_reload = FORCE_RELOAD_WEAK;
2258
2259         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc())) {
2260                 condlog(0, "couldn't allocate path vec in configure");
2261                 return 1;
2262         }
2263
2264         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc())) {
2265                 condlog(0, "couldn't allocate multipath vec in configure");
2266                 return 1;
2267         }
2268
2269         if (!(mpvec = vector_alloc())) {
2270                 condlog(0, "couldn't allocate new maps vec in configure");
2271                 return 1;
2272         }
2273
2274         /*
2275          * probe for current path (from sysfs) and map (from dm) sets
2276          */
2277         ret = path_discovery(vecs->pathvec, DI_ALL);
2278         if (ret < 0) {
2279                 condlog(0, "configure failed at path discovery");
2280                 return 1;
2281         }
2282
2283         vector_foreach_slot (vecs->pathvec, pp, i){
2284                 conf = get_multipath_config();
2285                 pthread_cleanup_push(put_multipath_config, conf);
2286                 if (filter_path(conf, pp) > 0){
2287                         vector_del_slot(vecs->pathvec, i);
2288                         free_path(pp);
2289                         i--;
2290                 }
2291                 else
2292                         pp->checkint = conf->checkint;
2293                 pthread_cleanup_pop(1);
2294         }
2295         if (map_discovery(vecs)) {
2296                 condlog(0, "configure failed at map discovery");
2297                 return 1;
2298         }
2299
2300         /*
2301          * create new set of maps & push changed ones into dm
2302          * In the first call, use FORCE_RELOAD_WEAK to avoid making
2303          * superfluous ACT_RELOAD ioctls. Later calls are done
2304          * with FORCE_RELOAD_YES.
2305          */
2306         ret = coalesce_paths(vecs, mpvec, NULL, force_reload, CMD_NONE);
2307         if (force_reload == FORCE_RELOAD_WEAK)
2308                 force_reload = FORCE_RELOAD_YES;
2309         if (ret) {
2310                 condlog(0, "configure failed while coalescing paths");
2311                 return 1;
2312         }
2313
2314         /*
2315          * may need to remove some maps which are no longer relevant
2316          * e.g., due to blacklist changes in conf file
2317          */
2318         if (coalesce_maps(vecs, mpvec)) {
2319                 condlog(0, "configure failed while coalescing maps");
2320                 return 1;
2321         }
2322
2323         dm_lib_release();
2324
2325         sync_maps_state(mpvec);
2326         vector_foreach_slot(mpvec, mpp, i){
2327                 if (remember_wwid(mpp->wwid) == 1)
2328                         trigger_paths_udev_change(mpp, true);
2329                 update_map_pr(mpp);
2330         }
2331
2332         /*
2333          * purge dm of old maps
2334          */
2335         remove_maps(vecs);
2336
2337         /*
2338          * save new set of maps formed by considering current path state
2339          */
2340         vector_free(vecs->mpvec);
2341         vecs->mpvec = mpvec;
2342
2343         /*
2344          * start dm event waiter threads for these new maps
2345          */
2346         vector_foreach_slot(vecs->mpvec, mpp, i) {
2347                 if (wait_for_events(mpp, vecs)) {
2348                         remove_map(mpp, vecs, 1);
2349                         i--;
2350                         continue;
2351                 }
2352                 if (setup_multipath(vecs, mpp))
2353                         i--;
2354         }
2355         return 0;
2356 }
2357
2358 int
2359 need_to_delay_reconfig(struct vectors * vecs)
2360 {
2361         struct multipath *mpp;
2362         int i;
2363
2364         if (!VECTOR_SIZE(vecs->mpvec))
2365                 return 0;
2366
2367         vector_foreach_slot(vecs->mpvec, mpp, i) {
2368                 if (mpp->wait_for_udev)
2369                         return 1;
2370         }
2371         return 0;
2372 }
2373
2374 void rcu_free_config(struct rcu_head *head)
2375 {
2376         struct config *conf = container_of(head, struct config, rcu);
2377
2378         free_config(conf);
2379 }
2380
2381 int
2382 reconfigure (struct vectors * vecs)
2383 {
2384         struct config * old, *conf;
2385
2386         conf = load_config(DEFAULT_CONFIGFILE);
2387         if (!conf)
2388                 return 1;
2389
2390         /*
2391          * free old map and path vectors ... they use old conf state
2392          */
2393         if (VECTOR_SIZE(vecs->mpvec))
2394                 remove_maps_and_stop_waiters(vecs);
2395
2396         free_pathvec(vecs->pathvec, FREE_PATHS);
2397         vecs->pathvec = NULL;
2398         delete_all_foreign();
2399
2400         /* Re-read any timezone changes */
2401         tzset();
2402
2403         dm_tgt_version(conf->version, TGT_MPATH);
2404         if (verbosity)
2405                 conf->verbosity = verbosity;
2406         if (bindings_read_only)
2407                 conf->bindings_read_only = bindings_read_only;
2408         uxsock_timeout = conf->uxsock_timeout;
2409
2410         old = rcu_dereference(multipath_conf);
2411         rcu_assign_pointer(multipath_conf, conf);
2412         call_rcu(&old->rcu, rcu_free_config);
2413
2414         configure(vecs);
2415
2416
2417         return 0;
2418 }
2419
2420 static struct vectors *
2421 init_vecs (void)
2422 {
2423         struct vectors * vecs;
2424
2425         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
2426
2427         if (!vecs)
2428                 return NULL;
2429
2430         pthread_mutex_init(&vecs->lock.mutex, NULL);
2431
2432         return vecs;
2433 }
2434
2435 static void *
2436 signal_set(int signo, void (*func) (int))
2437 {
2438         int r;
2439         struct sigaction sig;
2440         struct sigaction osig;
2441
2442         sig.sa_handler = func;
2443         sigemptyset(&sig.sa_mask);
2444         sig.sa_flags = 0;
2445
2446         r = sigaction(signo, &sig, &osig);
2447
2448         if (r < 0)
2449                 return (SIG_ERR);
2450         else
2451                 return (osig.sa_handler);
2452 }
2453
2454 void
2455 handle_signals(bool nonfatal)
2456 {
2457         if (exit_sig) {
2458                 condlog(2, "exit (signal)");
2459                 exit_sig = 0;
2460                 exit_daemon();
2461         }
2462         if (!nonfatal)
2463                 return;
2464         if (reconfig_sig) {
2465                 condlog(2, "reconfigure (signal)");
2466                 set_config_state(DAEMON_CONFIGURE);
2467         }
2468         if (log_reset_sig) {
2469                 condlog(2, "reset log (signal)");
2470                 if (logsink == 1)
2471                         log_thread_reset();
2472         }
2473         reconfig_sig = 0;
2474         log_reset_sig = 0;
2475 }
2476
2477 static void
2478 sighup (int sig)
2479 {
2480         reconfig_sig = 1;
2481 }
2482
2483 static void
2484 sigend (int sig)
2485 {
2486         exit_sig = 1;
2487 }
2488
2489 static void
2490 sigusr1 (int sig)
2491 {
2492         log_reset_sig = 1;
2493 }
2494
2495 static void
2496 sigusr2 (int sig)
2497 {
2498         condlog(3, "SIGUSR2 received");
2499 }
2500
2501 static void
2502 signal_init(void)
2503 {
2504         sigset_t set;
2505
2506         /* block all signals */
2507         sigfillset(&set);
2508         /* SIGPIPE occurs if logging fails */
2509         sigdelset(&set, SIGPIPE);
2510         pthread_sigmask(SIG_SETMASK, &set, NULL);
2511
2512         /* Other signals will be unblocked in the uxlsnr thread */
2513         signal_set(SIGHUP, sighup);
2514         signal_set(SIGUSR1, sigusr1);
2515         signal_set(SIGUSR2, sigusr2);
2516         signal_set(SIGINT, sigend);
2517         signal_set(SIGTERM, sigend);
2518         signal_set(SIGPIPE, sigend);
2519 }
2520
2521 static void
2522 setscheduler (void)
2523 {
2524         int res;
2525         static struct sched_param sched_param = {
2526                 .sched_priority = 99
2527         };
2528
2529         res = sched_setscheduler (0, SCHED_RR, &sched_param);
2530
2531         if (res == -1)
2532                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
2533         return;
2534 }
2535
2536 static void
2537 set_oom_adj (void)
2538 {
2539 #ifdef OOM_SCORE_ADJ_MIN
2540         int retry = 1;
2541         char *file = "/proc/self/oom_score_adj";
2542         int score = OOM_SCORE_ADJ_MIN;
2543 #else
2544         int retry = 0;
2545         char *file = "/proc/self/oom_adj";
2546         int score = OOM_ADJUST_MIN;
2547 #endif
2548         FILE *fp;
2549         struct stat st;
2550         char *envp;
2551
2552         envp = getenv("OOMScoreAdjust");
2553         if (envp) {
2554                 condlog(3, "Using systemd provided OOMScoreAdjust");
2555                 return;
2556         }
2557         do {
2558                 if (stat(file, &st) == 0){
2559                         fp = fopen(file, "w");
2560                         if (!fp) {
2561                                 condlog(0, "couldn't fopen %s : %s", file,
2562                                         strerror(errno));
2563                                 return;
2564                         }
2565                         fprintf(fp, "%i", score);
2566                         fclose(fp);
2567                         return;
2568                 }
2569                 if (errno != ENOENT) {
2570                         condlog(0, "couldn't stat %s : %s", file,
2571                                 strerror(errno));
2572                         return;
2573                 }
2574 #ifdef OOM_ADJUST_MIN
2575                 file = "/proc/self/oom_adj";
2576                 score = OOM_ADJUST_MIN;
2577 #else
2578                 retry = 0;
2579 #endif
2580         } while (retry--);
2581         condlog(0, "couldn't adjust oom score");
2582 }
2583
2584 static int
2585 child (void * param)
2586 {
2587         pthread_t check_thr, uevent_thr, uxlsnr_thr, uevq_thr, dmevent_thr;
2588         pthread_attr_t log_attr, misc_attr, uevent_attr;
2589         struct vectors * vecs;
2590         struct multipath * mpp;
2591         int i;
2592 #ifdef USE_SYSTEMD
2593         unsigned long checkint;
2594         int startup_done = 0;
2595 #endif
2596         int rc;
2597         int pid_fd = -1;
2598         struct config *conf;
2599         char *envp;
2600         int queue_without_daemon;
2601
2602         mlockall(MCL_CURRENT | MCL_FUTURE);
2603         signal_init();
2604         rcu_init();
2605
2606         setup_thread_attr(&misc_attr, 64 * 1024, 0);
2607         setup_thread_attr(&uevent_attr, DEFAULT_UEVENT_STACKSIZE * 1024, 0);
2608         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
2609         setup_thread_attr(&io_err_stat_attr, 32 * 1024, 0);
2610
2611         if (logsink == 1) {
2612                 setup_thread_attr(&log_attr, 64 * 1024, 0);
2613                 log_thread_start(&log_attr);
2614                 pthread_attr_destroy(&log_attr);
2615         }
2616         pid_fd = pidfile_create(DEFAULT_PIDFILE, daemon_pid);
2617         if (pid_fd < 0) {
2618                 condlog(1, "failed to create pidfile");
2619                 if (logsink == 1)
2620                         log_thread_stop();
2621                 exit(1);
2622         }
2623
2624         post_config_state(DAEMON_START);
2625
2626         condlog(2, "--------start up--------");
2627         condlog(2, "read " DEFAULT_CONFIGFILE);
2628
2629         conf = load_config(DEFAULT_CONFIGFILE);
2630         if (!conf)
2631                 goto failed;
2632
2633         if (verbosity)
2634                 conf->verbosity = verbosity;
2635         if (bindings_read_only)
2636                 conf->bindings_read_only = bindings_read_only;
2637         uxsock_timeout = conf->uxsock_timeout;
2638         rcu_assign_pointer(multipath_conf, conf);
2639         if (init_checkers(conf->multipath_dir)) {
2640                 condlog(0, "failed to initialize checkers");
2641                 goto failed;
2642         }
2643         if (init_prio(conf->multipath_dir)) {
2644                 condlog(0, "failed to initialize prioritizers");
2645                 goto failed;
2646         }
2647         /* Failing this is non-fatal */
2648
2649         init_foreign(conf->multipath_dir);
2650
2651         if (poll_dmevents)
2652                 poll_dmevents = dmevent_poll_supported();
2653         setlogmask(LOG_UPTO(conf->verbosity + 3));
2654
2655         envp = getenv("LimitNOFILE");
2656
2657         if (envp) {
2658                 condlog(2,"Using systemd provided open fds limit of %s", envp);
2659         } else if (conf->max_fds) {
2660                 struct rlimit fd_limit;
2661
2662                 if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
2663                         condlog(0, "can't get open fds limit: %s",
2664                                 strerror(errno));
2665                         fd_limit.rlim_cur = 0;
2666                         fd_limit.rlim_max = 0;
2667                 }
2668                 if (fd_limit.rlim_cur < conf->max_fds) {
2669                         fd_limit.rlim_cur = conf->max_fds;
2670                         if (fd_limit.rlim_max < conf->max_fds)
2671                                 fd_limit.rlim_max = conf->max_fds;
2672                         if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
2673                                 condlog(0, "can't set open fds limit to "
2674                                         "%lu/%lu : %s",
2675                                         fd_limit.rlim_cur, fd_limit.rlim_max,
2676                                         strerror(errno));
2677                         } else {
2678                                 condlog(3, "set open fds limit to %lu/%lu",
2679                                         fd_limit.rlim_cur, fd_limit.rlim_max);
2680                         }
2681                 }
2682
2683         }
2684
2685         vecs = gvecs = init_vecs();
2686         if (!vecs)
2687                 goto failed;
2688
2689         setscheduler();
2690         set_oom_adj();
2691
2692 #ifdef USE_SYSTEMD
2693         envp = getenv("WATCHDOG_USEC");
2694         if (envp && sscanf(envp, "%lu", &checkint) == 1) {
2695                 /* Value is in microseconds */
2696                 conf->max_checkint = checkint / 1000000;
2697                 /* Rescale checkint */
2698                 if (conf->checkint > conf->max_checkint)
2699                         conf->checkint = conf->max_checkint;
2700                 else
2701                         conf->checkint = conf->max_checkint / 4;
2702                 condlog(3, "enabling watchdog, interval %d max %d",
2703                         conf->checkint, conf->max_checkint);
2704                 use_watchdog = conf->checkint;
2705         }
2706 #endif
2707         /*
2708          * Startup done, invalidate configuration
2709          */
2710         conf = NULL;
2711
2712         /*
2713          * Signal start of configuration
2714          */
2715         post_config_state(DAEMON_CONFIGURE);
2716
2717         init_path_check_interval(vecs);
2718
2719         if (poll_dmevents) {
2720                 if (init_dmevent_waiter(vecs)) {
2721                         condlog(0, "failed to allocate dmevents waiter info");
2722                         goto failed;
2723                 }
2724                 if ((rc = pthread_create(&dmevent_thr, &misc_attr,
2725                                          wait_dmevents, NULL))) {
2726                         condlog(0, "failed to create dmevent waiter thread: %d",
2727                                 rc);
2728                         goto failed;
2729                 }
2730         }
2731
2732         /*
2733          * Start uevent listener early to catch events
2734          */
2735         if ((rc = pthread_create(&uevent_thr, &uevent_attr, ueventloop, udev))) {
2736                 condlog(0, "failed to create uevent thread: %d", rc);
2737                 goto failed;
2738         }
2739         pthread_attr_destroy(&uevent_attr);
2740         if ((rc = pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs))) {
2741                 condlog(0, "failed to create cli listener: %d", rc);
2742                 goto failed;
2743         }
2744
2745         /*
2746          * start threads
2747          */
2748         if ((rc = pthread_create(&check_thr, &misc_attr, checkerloop, vecs))) {
2749                 condlog(0,"failed to create checker loop thread: %d", rc);
2750                 goto failed;
2751         }
2752         if ((rc = pthread_create(&uevq_thr, &misc_attr, uevqloop, vecs))) {
2753                 condlog(0, "failed to create uevent dispatcher: %d", rc);
2754                 goto failed;
2755         }
2756         pthread_attr_destroy(&misc_attr);
2757
2758         while (running_state != DAEMON_SHUTDOWN) {
2759                 pthread_cleanup_push(config_cleanup, NULL);
2760                 pthread_mutex_lock(&config_lock);
2761                 if (running_state != DAEMON_CONFIGURE &&
2762                     running_state != DAEMON_SHUTDOWN) {
2763                         pthread_cond_wait(&config_cond, &config_lock);
2764                 }
2765                 pthread_cleanup_pop(1);
2766                 if (running_state == DAEMON_CONFIGURE) {
2767                         pthread_cleanup_push(cleanup_lock, &vecs->lock);
2768                         lock(&vecs->lock);
2769                         pthread_testcancel();
2770                         if (!need_to_delay_reconfig(vecs)) {
2771                                 reconfigure(vecs);
2772                         } else {
2773                                 conf = get_multipath_config();
2774                                 conf->delayed_reconfig = 1;
2775                                 put_multipath_config(conf);
2776                         }
2777                         lock_cleanup_pop(vecs->lock);
2778                         post_config_state(DAEMON_IDLE);
2779 #ifdef USE_SYSTEMD
2780                         if (!startup_done) {
2781                                 sd_notify(0, "READY=1");
2782                                 startup_done = 1;
2783                         }
2784 #endif
2785                 }
2786         }
2787
2788         lock(&vecs->lock);
2789         conf = get_multipath_config();
2790         queue_without_daemon = conf->queue_without_daemon;
2791         put_multipath_config(conf);
2792         if (queue_without_daemon == QUE_NO_DAEMON_OFF)
2793                 vector_foreach_slot(vecs->mpvec, mpp, i)
2794                         dm_queue_if_no_path(mpp->alias, 0);
2795         remove_maps_and_stop_waiters(vecs);
2796         unlock(&vecs->lock);
2797
2798         pthread_cancel(check_thr);
2799         pthread_cancel(uevent_thr);
2800         pthread_cancel(uxlsnr_thr);
2801         pthread_cancel(uevq_thr);
2802         if (poll_dmevents)
2803                 pthread_cancel(dmevent_thr);
2804
2805         pthread_join(check_thr, NULL);
2806         pthread_join(uevent_thr, NULL);
2807         pthread_join(uxlsnr_thr, NULL);
2808         pthread_join(uevq_thr, NULL);
2809         if (poll_dmevents)
2810                 pthread_join(dmevent_thr, NULL);
2811
2812         stop_io_err_stat_thread();
2813
2814         lock(&vecs->lock);
2815         free_pathvec(vecs->pathvec, FREE_PATHS);
2816         vecs->pathvec = NULL;
2817         unlock(&vecs->lock);
2818
2819         pthread_mutex_destroy(&vecs->lock.mutex);
2820         FREE(vecs);
2821         vecs = NULL;
2822
2823         cleanup_foreign();
2824         cleanup_checkers();
2825         cleanup_prio();
2826         if (poll_dmevents)
2827                 cleanup_dmevent_waiter();
2828
2829         dm_lib_release();
2830         dm_lib_exit();
2831
2832         /* We're done here */
2833         condlog(3, "unlink pidfile");
2834         unlink(DEFAULT_PIDFILE);
2835
2836         condlog(2, "--------shut down-------");
2837
2838         if (logsink == 1)
2839                 log_thread_stop();
2840
2841         /*
2842          * Freeing config must be done after condlog() and dm_lib_exit(),
2843          * because logging functions like dlog() and dm_write_log()
2844          * reference the config.
2845          */
2846         conf = rcu_dereference(multipath_conf);
2847         rcu_assign_pointer(multipath_conf, NULL);
2848         call_rcu(&conf->rcu, rcu_free_config);
2849         udev_unref(udev);
2850         udev = NULL;
2851         pthread_attr_destroy(&waiter_attr);
2852         pthread_attr_destroy(&io_err_stat_attr);
2853 #ifdef _DEBUG_
2854         dbg_free_final(NULL);
2855 #endif
2856
2857 #ifdef USE_SYSTEMD
2858         sd_notify(0, "ERRNO=0");
2859 #endif
2860         exit(0);
2861
2862 failed:
2863 #ifdef USE_SYSTEMD
2864         sd_notify(0, "ERRNO=1");
2865 #endif
2866         if (pid_fd >= 0)
2867                 close(pid_fd);
2868         exit(1);
2869 }
2870
2871 static int
2872 daemonize(void)
2873 {
2874         int pid;
2875         int dev_null_fd;
2876
2877         if( (pid = fork()) < 0){
2878                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
2879                 return -1;
2880         }
2881         else if (pid != 0)
2882                 return pid;
2883
2884         setsid();
2885
2886         if ( (pid = fork()) < 0)
2887                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
2888         else if (pid != 0)
2889                 _exit(0);
2890
2891         if (chdir("/") < 0)
2892                 fprintf(stderr, "cannot chdir to '/', continuing\n");
2893
2894         dev_null_fd = open("/dev/null", O_RDWR);
2895         if (dev_null_fd < 0){
2896                 fprintf(stderr, "cannot open /dev/null for input & output : %s\n",
2897                         strerror(errno));
2898                 _exit(0);
2899         }
2900
2901         close(STDIN_FILENO);
2902         if (dup(dev_null_fd) < 0) {
2903                 fprintf(stderr, "cannot dup /dev/null to stdin : %s\n",
2904                         strerror(errno));
2905                 _exit(0);
2906         }
2907         close(STDOUT_FILENO);
2908         if (dup(dev_null_fd) < 0) {
2909                 fprintf(stderr, "cannot dup /dev/null to stdout : %s\n",
2910                         strerror(errno));
2911                 _exit(0);
2912         }
2913         close(STDERR_FILENO);
2914         if (dup(dev_null_fd) < 0) {
2915                 fprintf(stderr, "cannot dup /dev/null to stderr : %s\n",
2916                         strerror(errno));
2917                 _exit(0);
2918         }
2919         close(dev_null_fd);
2920         daemon_pid = getpid();
2921         return 0;
2922 }
2923
2924 int
2925 main (int argc, char *argv[])
2926 {
2927         extern char *optarg;
2928         extern int optind;
2929         int arg;
2930         int err;
2931         int foreground = 0;
2932         struct config *conf;
2933
2934         ANNOTATE_BENIGN_RACE_SIZED(&multipath_conf, sizeof(multipath_conf),
2935                                    "Manipulated through RCU");
2936         ANNOTATE_BENIGN_RACE_SIZED(&running_state, sizeof(running_state),
2937                 "Suppress complaints about unprotected running_state reads");
2938         ANNOTATE_BENIGN_RACE_SIZED(&uxsock_timeout, sizeof(uxsock_timeout),
2939                 "Suppress complaints about this scalar variable");
2940
2941         logsink = 1;
2942
2943         if (getuid() != 0) {
2944                 fprintf(stderr, "need to be root\n");
2945                 exit(1);
2946         }
2947
2948         /* make sure we don't lock any path */
2949         if (chdir("/") < 0)
2950                 fprintf(stderr, "can't chdir to root directory : %s\n",
2951                         strerror(errno));
2952         umask(umask(077) | 022);
2953
2954         pthread_cond_init_mono(&config_cond);
2955
2956         udev = udev_new();
2957         libmp_udev_set_sync_support(0);
2958
2959         while ((arg = getopt(argc, argv, ":dsv:k::Bniw")) != EOF ) {
2960                 switch(arg) {
2961                 case 'd':
2962                         foreground = 1;
2963                         if (logsink > 0)
2964                                 logsink = 0;
2965                         //debug=1; /* ### comment me out ### */
2966                         break;
2967                 case 'v':
2968                         if (sizeof(optarg) > sizeof(char *) ||
2969                             !isdigit(optarg[0]))
2970                                 exit(1);
2971
2972                         verbosity = atoi(optarg);
2973                         break;
2974                 case 's':
2975                         logsink = -1;
2976                         break;
2977                 case 'k':
2978                         conf = load_config(DEFAULT_CONFIGFILE);
2979                         if (!conf)
2980                                 exit(1);
2981                         if (verbosity)
2982                                 conf->verbosity = verbosity;
2983                         uxsock_timeout = conf->uxsock_timeout;
2984                         uxclnt(optarg, uxsock_timeout + 100);
2985                         free_config(conf);
2986                         exit(0);
2987                 case 'B':
2988                         bindings_read_only = 1;
2989                         break;
2990                 case 'n':
2991                         condlog(0, "WARNING: ignoring deprecated option -n, use 'ignore_wwids = no' instead");
2992                         break;
2993                 case 'w':
2994                         poll_dmevents = 0;
2995                         break;
2996                 default:
2997                         fprintf(stderr, "Invalid argument '-%c'\n",
2998                                 optopt);
2999                         exit(1);
3000                 }
3001         }
3002         if (optind < argc) {
3003                 char cmd[CMDSIZE];
3004                 char * s = cmd;
3005                 char * c = s;
3006
3007                 conf = load_config(DEFAULT_CONFIGFILE);
3008                 if (!conf)
3009                         exit(1);
3010                 if (verbosity)
3011                         conf->verbosity = verbosity;
3012                 uxsock_timeout = conf->uxsock_timeout;
3013                 memset(cmd, 0x0, CMDSIZE);
3014                 while (optind < argc) {
3015                         if (strchr(argv[optind], ' '))
3016                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
3017                         else
3018                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
3019                         optind++;
3020                 }
3021                 c += snprintf(c, s + CMDSIZE - c, "\n");
3022                 uxclnt(s, uxsock_timeout + 100);
3023                 free_config(conf);
3024                 exit(0);
3025         }
3026
3027         if (foreground) {
3028                 if (!isatty(fileno(stdout)))
3029                         setbuf(stdout, NULL);
3030                 err = 0;
3031                 daemon_pid = getpid();
3032         } else
3033                 err = daemonize();
3034
3035         if (err < 0)
3036                 /* error */
3037                 exit(1);
3038         else if (err > 0)
3039                 /* parent dies */
3040                 exit(0);
3041         else
3042                 /* child lives */
3043                 return (child(NULL));
3044 }
3045
3046 void *  mpath_pr_event_handler_fn (void * pathp )
3047 {
3048         struct multipath * mpp;
3049         int i, ret, isFound;
3050         struct path * pp = (struct path *)pathp;
3051         struct prout_param_descriptor *param;
3052         struct prin_resp *resp;
3053
3054         rcu_register_thread();
3055         mpp = pp->mpp;
3056
3057         resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
3058         if (!resp){
3059                 condlog(0,"%s Alloc failed for prin response", pp->dev);
3060                 goto out;
3061         }
3062
3063         ret = prin_do_scsi_ioctl(pp->dev, MPATH_PRIN_RKEY_SA, resp, 0);
3064         if (ret != MPATH_PR_SUCCESS )
3065         {
3066                 condlog(0,"%s : pr in read keys service action failed. Error=%d", pp->dev, ret);
3067                 goto out;
3068         }
3069
3070         condlog(3, " event pr=%d addlen=%d",resp->prin_descriptor.prin_readkeys.prgeneration,
3071                         resp->prin_descriptor.prin_readkeys.additional_length );
3072
3073         if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
3074         {
3075                 condlog(1, "%s: No key found. Device may not be registered.", pp->dev);
3076                 ret = MPATH_PR_SUCCESS;
3077                 goto out;
3078         }
3079         condlog(2, "Multipath  reservation_key: 0x%" PRIx64 " ",
3080                 get_be64(mpp->reservation_key));
3081
3082         isFound =0;
3083         for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
3084         {
3085                 condlog(2, "PR IN READKEYS[%d]  reservation key:",i);
3086                 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , -1);
3087                 if (!memcmp(&mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
3088                 {
3089                         condlog(2, "%s: pr key found in prin readkeys response", mpp->alias);
3090                         isFound =1;
3091                         break;
3092                 }
3093         }
3094         if (!isFound)
3095         {
3096                 condlog(0, "%s: Either device not registered or ", pp->dev);
3097                 condlog(0, "host is not authorised for registration. Skip path");
3098                 ret = MPATH_PR_OTHER;
3099                 goto out;
3100         }
3101
3102         param= malloc(sizeof(struct prout_param_descriptor));
3103         memset(param, 0 , sizeof(struct prout_param_descriptor));
3104         param->sa_flags = mpp->sa_flags;
3105         memcpy(param->sa_key, &mpp->reservation_key, 8);
3106         param->num_transportid = 0;
3107
3108         condlog(3, "device %s:%s", pp->dev, pp->mpp->wwid);
3109
3110         ret = prout_do_scsi_ioctl(pp->dev, MPATH_PROUT_REG_IGN_SA, 0, 0, param, 0);
3111         if (ret != MPATH_PR_SUCCESS )
3112         {
3113                 condlog(0,"%s: Reservation registration failed. Error: %d", pp->dev, ret);
3114         }
3115         mpp->prflag = 1;
3116
3117         free(param);
3118 out:
3119         if (resp)
3120                 free(resp);
3121         rcu_unregister_thread();
3122         return NULL;
3123 }
3124
3125 int mpath_pr_event_handle(struct path *pp)
3126 {
3127         pthread_t thread;
3128         int rc;
3129         pthread_attr_t attr;
3130         struct multipath * mpp;
3131
3132         mpp = pp->mpp;
3133
3134         if (!get_be64(mpp->reservation_key))
3135                 return -1;
3136
3137         pthread_attr_init(&attr);
3138         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
3139
3140         rc = pthread_create(&thread, NULL , mpath_pr_event_handler_fn, pp);
3141         if (rc) {
3142                 condlog(0, "%s: ERROR; return code from pthread_create() is %d", pp->dev, rc);
3143                 return -1;
3144         }
3145         pthread_attr_destroy(&attr);
3146         rc = pthread_join(thread, NULL);
3147         return 0;
3148 }