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