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