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