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