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