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