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