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