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