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