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