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