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