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