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